PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/memuller.web/wp_site
PHP | 275 lines | 156 code | 20 blank | 99 comment | 44 complexity | f5ba0a3aed1280266e37a7b5ff4d2bd9 MD5 | raw file
  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. $plugin_options = MPSUM_Updates_Manager::get_options( 'plugins' );
  120. if ( false !== $key = array_search( $plugin, $plugin_options ) ) {
  121. $enable_url = add_query_arg( array( 'action' => 'allow-update-selected', '_mpsum' => wp_create_nonce( 'mpsum_plugin_update' ), 'checked' => array( $plugin ) ) );
  122. $settings[] = sprintf( '<a href="%s">%s</a>', esc_url( $enable_url ), esc_html__( 'Allow Updates', 'stops-core-theme-and-plugin-updates' ) );
  123. } else {
  124. //Disable Link
  125. $disable_url = add_query_arg( array( 'action' => 'disallow-update-selected', '_mpsum' => wp_create_nonce( 'mpsum_plugin_update' ), 'checked' => array( $plugin ) ) );
  126. $settings[] = sprintf( '<a href="%s">%s</a>', esc_url( $disable_url ), esc_html__( 'Disallow Updates', 'stops-core-theme-and-plugin-updates' ) );
  127. //Automatic Link
  128. $plugin_automatic_options = MPSUM_Updates_Manager::get_options( 'plugins_automatic' );
  129. $core_options = MPSUM_Updates_Manager::get_options( 'core' );
  130. if ( isset( $core_options[ 'automatic_plugin_updates' ] ) && 'individual' == $core_options[ 'automatic_plugin_updates' ] ) {
  131. if ( in_array( $plugin, $plugin_automatic_options ) ) {
  132. //Disable Link
  133. $disable_automatic_url = add_query_arg( array( 'action' => 'disallow-automatic-selected', '_mpsum' => wp_create_nonce( 'mpsum_plugin_update' ), 'checked' => array( $plugin ) ) );
  134. $settings[] = sprintf( '<a href="%s">%s</a>', esc_url( $disable_automatic_url ), esc_html__( 'Disallow Automatic Updates', 'stops-core-theme-and-plugin-updates' ) );
  135. } else {
  136. //Enable Link
  137. $enable_automatic_url = add_query_arg( array( 'action' => 'allow-automatic-selected', '_mpsum' => wp_create_nonce( 'mpsum_plugin_update' ), 'checked' => array( $plugin ) ) );
  138. $settings[] = sprintf( '<a href="%s">%s</a>', esc_url( $enable_automatic_url ), esc_html__( 'Enable Automatic Updates', 'stops-core-theme-and-plugin-updates' ) );
  139. }
  140. }
  141. }
  142. return $settings;
  143. }
  144. /**
  145. * Save the plugin options based on the passed action.
  146. *
  147. * Save the plugin options based on the passed action.
  148. *
  149. * @since 5.0.0
  150. * @access private
  151. * @see maybe_save_plugin_options
  152. * @param string $action Action to take action on
  153. *
  154. */
  155. private function save_plugin_update_options( $action ) {
  156. //Check capability
  157. $capability = 'update_plugins'; //On single site, admins can use this, on multisite, only network admins can
  158. if ( !current_user_can( $capability ) ) return;
  159. $plugins = isset( $_REQUEST[ 'checked' ] ) ? (array) $_REQUEST[ 'checked' ] : array();
  160. $plugin_options = MPSUM_Updates_Manager::get_options( 'plugins' );
  161. $plugin_automatic_options = MPSUM_Updates_Manager::get_options( 'plugins_automatic' );
  162. switch( $action ) {
  163. case 'disallow-update-selected':
  164. foreach( $plugins as $plugin ) {
  165. $plugin_options[] = $plugin;
  166. if ( ( $key = array_search( $plugin, $plugin_automatic_options ) ) !== false ) {
  167. unset( $plugin_automatic_options[ $key ] );
  168. }
  169. }
  170. break;
  171. case 'allow-update-selected':
  172. foreach( $plugins as $plugin ) {
  173. if ( ( $key = array_search( $plugin, $plugin_options ) ) !== false ) {
  174. unset( $plugin_options[ $key ] );
  175. }
  176. }
  177. break;
  178. case 'allow-automatic-selected':
  179. foreach( $plugins as $plugin ) {
  180. $plugin_automatic_options[] = $plugin;
  181. if ( ( $key = array_search( $plugin, $plugin_options ) ) !== false ) {
  182. unset( $plugin_options[ $key ] );
  183. }
  184. }
  185. break;
  186. case 'disallow-automatic-selected':
  187. foreach( $plugins as $plugin ) {
  188. if ( ( $key = array_search( $plugin, $plugin_automatic_options ) ) !== false ) {
  189. unset( $plugin_automatic_options[ $key ] );
  190. }
  191. }
  192. break;
  193. default:
  194. return;
  195. }
  196. //Check nonce
  197. check_admin_referer( 'mpsum_plugin_update', '_mpsum' );
  198. //Update option
  199. $plugin_options = array_values( array_unique( $plugin_options ) );
  200. $plugin_automatic_options = array_values( array_unique( $plugin_automatic_options ) );
  201. $options = MPSUM_Updates_Manager::get_options();
  202. $options[ 'plugins' ] = $plugin_options;
  203. $options[ 'plugins_automatic' ] = $plugin_automatic_options;
  204. MPSUM_Updates_Manager::update_options( $options );
  205. }
  206. /**
  207. * Output the HTML interface for the plugins tab.
  208. *
  209. * Output the HTML interface for the plugins tab.
  210. *
  211. * @since 5.0.0
  212. * @access public
  213. * @see __construct
  214. * @internal Uses the mpsum_admin_tab_plugins action
  215. */
  216. public function tab_output_plugins() {
  217. if ( isset( $_GET[ 'action' ] ) ) {
  218. $message = __( 'Settings have been updated.', 'stops-core-theme-and-plugin-updates' );
  219. if ( 'allow-automatic-selected' == $_GET[ 'action' ] ) {
  220. $message = __( 'The selected plugins have had automatic updates enabled.', 'stops-core-theme-and-plugin-updates' );
  221. } elseif( 'disallow-automatic-selected' == $_GET[ 'action' ] ) {
  222. $message = __( 'The selected plugins have had automatic updates disabled.', 'stops-core-theme-and-plugin-updates' );
  223. } elseif( 'disallow-update-selected' == $_GET[ 'action' ] ) {
  224. $message = __( 'The selected plugin updates have been disabled.', 'stops-core-theme-and-plugin-updates' );
  225. } elseif( 'allow-update-selected' == $_GET[ 'action' ] ) {
  226. $message = __( 'The selected plugin updates have been enabled.', 'stops-core-theme-and-plugin-updates' );
  227. }
  228. ?>
  229. <div class="updated"><p><strong><?php echo esc_html( $message ); ?></strong></p></div>
  230. <?php
  231. }
  232. ?>
  233. <form action="<?php echo esc_url( add_query_arg( array() ) ); ?>" method="post">
  234. <?php
  235. $plugin_status = isset( $_GET[ 'plugin_status' ] ) ? $_GET[ 'plugin_status' ] : false;
  236. if ( false !== $plugin_status ) {
  237. printf( '<input type="hidden" name="plugin_status" value="%s" />', esc_attr( $plugin_status ) );
  238. }
  239. wp_nonce_field( 'mpsum_plugin_update', '_mpsum' );
  240. ?>
  241. <h3><?php esc_html_e( 'Plugin Update Options', 'stops-core-theme-and-plugin-updates' ); ?></h3>
  242. <?php
  243. $core_options = MPSUM_Updates_Manager::get_options( 'core' );
  244. if ( false === $this->can_update() ) {
  245. printf( '<div class="error"><p><strong>%s</strong></p></div>', esc_html__( 'All plugin updates have been disabled.', 'stops-core-theme-and-plugin-updates' ) );
  246. }
  247. $plugin_table = new MPSUM_Plugins_List_Table( $args = array( 'screen' => $this->slug, 'tab' => $this->tab ) );
  248. $plugin_table->prepare_items();
  249. $plugin_table->views();
  250. $plugin_table->display();
  251. ?>
  252. </form>
  253. <?php
  254. } //end tab_output_plugins
  255. }