PageRenderTime 21ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/stops-core-theme-and-plugin-updates/includes/MPSUM_Admin_Plugins.php

https://bitbucket.org/suroj/nepalayatravels
PHP | 251 lines | 137 code | 19 blank | 95 comment | 37 complexity | b72953bec5c0e26cd5a2497084fa3ec7 MD5 | raw file
Possible License(s): MIT, GPL-3.0, GPL-2.0, BSD-3-Clause, Apache-2.0
  1. <?php
  2. /**
  3. * Controls the plugins tab
  4. *
  5. * Controls the plugins tab and handles the saving of its options.
  6. *
  7. * @since 5.0.0
  8. *
  9. * @package WordPress
  10. */
  11. class MPSUM_Admin_Plugins {
  12. /**
  13. * Holds the slug to the admin panel page
  14. *
  15. * @since 5.0.0
  16. * @access private
  17. * @var string $slug
  18. */
  19. private $slug = '';
  20. /**
  21. * Holds the tab name
  22. *
  23. * @since 5.0.0
  24. * @access static
  25. * @var string $tab
  26. */
  27. private $tab = 'plugins';
  28. /**
  29. * Class constructor.
  30. *
  31. * Initialize the class
  32. *
  33. * @since 5.0.0
  34. * @access public
  35. *
  36. * @param string $slug Slug to the admin panel page
  37. */
  38. public function __construct( $slug = '' ) {
  39. $this->slug = $slug;
  40. //Admin Tab Actions
  41. add_action( 'mpsum_admin_tab_plugins', array( $this, 'tab_output_plugins' ) );
  42. add_filter( 'mpsum_plugin_action_links', array( $this, 'plugin_action_links' ), 11, 2 );
  43. add_action( 'admin_init', array( $this, 'maybe_save_plugin_options' ) );
  44. }
  45. /**
  46. * Determine whether the plugins can be updated or not.
  47. *
  48. * Determine whether the plugins can be updated or not.
  49. *
  50. * @since 5.0.0
  51. * @access private
  52. *
  53. * @return bool True if the plugins can be updated, false if not.
  54. */
  55. private function can_update() {
  56. $core_options = MPSUM_Updates_Manager::get_options( 'core' );
  57. if ( isset( $core_options[ 'all_updates' ] ) && 'off' == $core_options[ 'all_updates' ] ) {
  58. return false;
  59. }
  60. if ( isset( $core_options[ 'plugin_updates' ] ) && 'off' == $core_options[ 'plugin_updates' ] ) {
  61. return false;
  62. }
  63. return true;
  64. }
  65. /**
  66. * Determine whether the save the plugin options or not.
  67. *
  68. * Determine whether the save the plugin options or not.
  69. *
  70. * @since 5.0.0
  71. * @access public
  72. * @see __construct
  73. * @internal Uses admin_init action
  74. *
  75. */
  76. public function maybe_save_plugin_options() {
  77. if ( !current_user_can( 'update_plugins' ) ) return;
  78. if ( !isset( $_GET[ 'page' ] ) || $_GET[ 'page' ] != $this->slug ) return;
  79. if ( !isset( $_GET[ 'tab' ] ) || $_GET[ 'tab' ] != $this->tab ) return;
  80. if ( !isset( $_REQUEST[ 'action' ] ) && ! isset( $_REQUEST[ 'action2' ] ) ) return;
  81. if ( !isset( $_REQUEST[ '_mpsum' ] ) ) return;
  82. if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST[ 'action' ] )
  83. $action = $_REQUEST[ 'action' ];
  84. if ( isset( $_REQUEST[ 'action2' ] ) && -1 != $_REQUEST[ 'action2' ] )
  85. $action = $_REQUEST[ 'action2' ];
  86. //Build Query Args
  87. $paged = isset( $_GET[ 'paged' ] ) ? absint( $_GET[ 'paged' ] ) : false;
  88. $query_args = array();
  89. $query_args[ 'page' ] = $this->slug;
  90. if ( false !== $paged ) {
  91. $query_args[ 'paged' ] = $paged;
  92. }
  93. $query_args[ 'action' ] = $action;
  94. $query_args[ 'tab' ] = $this->tab;
  95. $plugin_status = isset( $_REQUEST[ 'plugin_status' ] ) ? $_REQUEST[ 'plugin_status' ] : false;
  96. if ( false !== $plugin_status ) {
  97. $query_args[ 'plugin_status' ] = $plugin_status;
  98. }
  99. //Save theme options
  100. $this->save_plugin_update_options( $action );
  101. //Redirect back to settings screen
  102. wp_redirect( esc_url_raw( add_query_arg( $query_args, MPSUM_Admin::get_url() ) ) );
  103. exit;
  104. }
  105. /**
  106. * Outputs the plugin action links beneath each plugin row.
  107. *
  108. * Outputs the plugin action links beneath each plugin row.
  109. *
  110. * @since 5.0.0
  111. * @access public
  112. * @see __construct
  113. * @internal uses mpsum_plugin_action_links filter
  114. *
  115. * @param array $settings Array of settings to output.
  116. * @param string $plugin The relative plugin path.
  117. */
  118. public function plugin_action_links( $settings, $plugin ) {
  119. return $settings;
  120. }
  121. /**
  122. * Save the plugin options based on the passed action.
  123. *
  124. * Save the plugin options based on the passed action.
  125. *
  126. * @since 5.0.0
  127. * @access private
  128. * @see maybe_save_plugin_options
  129. * @param string $action Action to take action on
  130. *
  131. */
  132. private function save_plugin_update_options( $action ) {
  133. //Check capability
  134. $capability = 'update_plugins'; //On single site, admins can use this, on multisite, only network admins can
  135. if ( !current_user_can( $capability ) ) return;
  136. $plugins = isset( $_REQUEST[ 'checked' ] ) ? (array) $_REQUEST[ 'checked' ] : array();
  137. $plugin_options = MPSUM_Updates_Manager::get_options( 'plugins' );
  138. $plugin_automatic_options = MPSUM_Updates_Manager::get_options( 'plugins_automatic' );
  139. switch( $action ) {
  140. case 'disallow-update-selected':
  141. foreach( $plugins as $plugin ) {
  142. $plugin_options[] = $plugin;
  143. if ( ( $key = array_search( $plugin, $plugin_automatic_options ) ) !== false ) {
  144. unset( $plugin_automatic_options[ $key ] );
  145. }
  146. }
  147. break;
  148. case 'allow-update-selected':
  149. foreach( $plugins as $plugin ) {
  150. if ( ( $key = array_search( $plugin, $plugin_options ) ) !== false ) {
  151. unset( $plugin_options[ $key ] );
  152. }
  153. }
  154. break;
  155. case 'allow-automatic-selected':
  156. foreach( $plugins as $plugin ) {
  157. $plugin_automatic_options[] = $plugin;
  158. if ( ( $key = array_search( $plugin, $plugin_options ) ) !== false ) {
  159. unset( $plugin_options[ $key ] );
  160. }
  161. }
  162. break;
  163. case 'disallow-automatic-selected':
  164. foreach( $plugins as $plugin ) {
  165. if ( ( $key = array_search( $plugin, $plugin_automatic_options ) ) !== false ) {
  166. unset( $plugin_automatic_options[ $key ] );
  167. }
  168. }
  169. break;
  170. default:
  171. return;
  172. }
  173. //Check nonce
  174. check_admin_referer( 'mpsum_plugin_update', '_mpsum' );
  175. //Update option
  176. $plugin_options = array_values( array_unique( $plugin_options ) );
  177. $plugin_automatic_options = array_values( array_unique( $plugin_automatic_options ) );
  178. $options = MPSUM_Updates_Manager::get_options();
  179. $options[ 'plugins' ] = $plugin_options;
  180. $options[ 'plugins_automatic' ] = $plugin_automatic_options;
  181. MPSUM_Updates_Manager::update_options( $options );
  182. }
  183. /**
  184. * Output the HTML interface for the plugins tab.
  185. *
  186. * Output the HTML interface for the plugins tab.
  187. *
  188. * @since 5.0.0
  189. * @access public
  190. * @see __construct
  191. * @internal Uses the mpsum_admin_tab_plugins action
  192. */
  193. public function tab_output_plugins() {
  194. if ( isset( $_GET[ 'action' ] ) ) {
  195. $message = __( 'Settings have been updated.', 'stops-core-theme-and-plugin-updates' );
  196. if ( 'allow-automatic-selected' == $_GET[ 'action' ] ) {
  197. $message = __( 'The selected plugins have had automatic updates enabled.', 'stops-core-theme-and-plugin-updates' );
  198. } elseif( 'disallow-automatic-selected' == $_GET[ 'action' ] ) {
  199. $message = __( 'The selected plugins have had automatic updates disabled.', 'stops-core-theme-and-plugin-updates' );
  200. } elseif( 'disallow-update-selected' == $_GET[ 'action' ] ) {
  201. $message = __( 'The selected plugin updates have been disabled.', 'stops-core-theme-and-plugin-updates' );
  202. } elseif( 'allow-update-selected' == $_GET[ 'action' ] ) {
  203. $message = __( 'The selected plugin updates have been enabled.', 'stops-core-theme-and-plugin-updates' );
  204. }
  205. ?>
  206. <div class="updated"><p><strong><?php echo esc_html( $message ); ?></strong></p></div>
  207. <?php
  208. }
  209. ?>
  210. <form action="<?php echo esc_url( add_query_arg( array() ) ); ?>" method="post">
  211. <?php
  212. $plugin_status = isset( $_GET[ 'plugin_status' ] ) ? $_GET[ 'plugin_status' ] : false;
  213. if ( false !== $plugin_status ) {
  214. printf( '<input type="hidden" name="plugin_status" value="%s" />', esc_attr( $plugin_status ) );
  215. }
  216. wp_nonce_field( 'mpsum_plugin_update', '_mpsum' );
  217. ?>
  218. <h3><?php esc_html_e( 'Plugin Update Options', 'stops-core-theme-and-plugin-updates' ); ?></h3>
  219. <?php
  220. $core_options = MPSUM_Updates_Manager::get_options( 'core' );
  221. if ( false === $this->can_update() ) {
  222. printf( '<div class="error"><p><strong>%s</strong></p></div>', esc_html__( 'All plugin updates have been disabled.', 'stops-core-theme-and-plugin-updates' ) );
  223. }
  224. $plugin_table = new MPSUM_Plugins_List_Table( $args = array( 'screen' => $this->slug, 'tab' => $this->tab ) );
  225. $plugin_table->prepare_items();
  226. $plugin_table->views();
  227. $plugin_table->display();
  228. ?>
  229. </form>
  230. <?php
  231. } //end tab_output_plugins
  232. }