PageRenderTime 30ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/memuller.web/wp_site
PHP | 280 lines | 159 code | 22 blank | 99 comment | 44 complexity | 4a63368f8838de9d82468fb40b8db490 MD5 | raw file
  1. <?php
  2. /**
  3. * Controls the themes tab
  4. *
  5. * Controls the themes tab and handles the saving of its options.
  6. *
  7. * @since 5.0.0
  8. *
  9. * @package WordPress
  10. */
  11. class MPSUM_Admin_Themes {
  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 = 'themes';
  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_themes', array( $this, 'tab_output_themes' ) );
  42. add_filter( 'mpsum_theme_action_links', array( $this, 'theme_action_links' ), 11, 2 );
  43. add_action( 'admin_init', array( $this, 'maybe_save_theme_options' ) );
  44. }
  45. /**
  46. * Determine whether the themes can be updated or not.
  47. *
  48. * Determine whether the themes can be updated or not.
  49. *
  50. * @since 5.0.0
  51. * @access private
  52. *
  53. * @return bool True if the themes 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[ 'theme_updates' ] ) && 'off' == $core_options[ 'theme_updates' ] ) {
  61. return false;
  62. }
  63. return true;
  64. }
  65. /**
  66. * Determine whether the save the theme options or not.
  67. *
  68. * Determine whether the save the theme 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_theme_options() {
  77. if ( !current_user_can( 'update_themes' ) ) 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. $theme_status = isset( $_REQUEST[ 'theme_status' ] ) ? $_REQUEST[ 'theme_status' ] : false;
  96. if ( false !== $theme_status ) {
  97. $query_args[ 'theme_status' ] = $theme_status;
  98. }
  99. //Save theme options
  100. $this->save_theme_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. * Save the theme options based on the passed action.
  107. *
  108. * Save the theme options based on the passed action.
  109. *
  110. * @since 5.0.0
  111. * @access private
  112. * @see maybe_save_theme_options
  113. * @param string $action Action to take action on
  114. *
  115. */
  116. private function save_theme_update_options( $action ) {
  117. //Check capability
  118. $capability = 'update_themes'; //On single site, admins can use this, on multisite, only network admins can
  119. if ( !current_user_can( $capability ) ) return;
  120. $themes = isset( $_REQUEST[ 'checked' ] ) ? (array) $_REQUEST[ 'checked' ] : array();
  121. $theme_options = MPSUM_Updates_Manager::get_options( 'themes' );
  122. $theme_automatic_options = MPSUM_Updates_Manager::get_options( 'themes_automatic' );
  123. switch( $action ) {
  124. case 'disallow-update-selected':
  125. foreach( $themes as $theme ) {
  126. $theme_options[] = $theme;
  127. if ( ( $key = array_search( $theme, $theme_automatic_options ) ) !== false ) {
  128. unset( $theme_automatic_options[ $key ] );
  129. }
  130. }
  131. break;
  132. case 'allow-update-selected':
  133. foreach( $themes as $theme ) {
  134. if ( ( $key = array_search( $theme, $theme_options ) ) !== false ) {
  135. unset( $theme_options[ $key ] );
  136. }
  137. }
  138. break;
  139. case 'allow-automatic-selected':
  140. foreach( $themes as $theme ) {
  141. $theme_automatic_options[] = $theme;
  142. if ( ( $key = array_search( $theme, $theme_options ) ) !== false ) {
  143. unset( $theme_options[ $key ] );
  144. }
  145. }
  146. break;
  147. case 'disallow-automatic-selected':
  148. foreach( $themes as $theme ) {
  149. if ( ( $key = array_search( $theme, $theme_automatic_options ) ) !== false ) {
  150. unset( $theme_automatic_options[ $key ] );
  151. }
  152. }
  153. break;
  154. default:
  155. return;
  156. }
  157. //Check nonce
  158. check_admin_referer( 'mpsum_theme_update', '_mpsum' );
  159. //Update option
  160. $theme_options = array_values( array_unique( $theme_options ) );
  161. $theme_automatic_options = array_values( array_unique( $theme_automatic_options ) );
  162. $options = MPSUM_Updates_Manager::get_options();
  163. $options[ 'themes' ] = $theme_options;
  164. $options[ 'themes_automatic' ] = $theme_automatic_options;
  165. MPSUM_Updates_Manager::update_options( $options );
  166. }
  167. /**
  168. * Output the HTML interface for the themes tab.
  169. *
  170. * Output the HTML interface for the themes tab.
  171. *
  172. * @since 5.0.0
  173. * @access public
  174. * @see __construct
  175. * @internal Uses the mpsum_admin_tab_themes action
  176. */
  177. public function tab_output_themes() {
  178. if ( isset( $_GET[ 'action' ] ) ) {
  179. $message = __( 'Settings have been updated.', 'stops-core-theme-and-plugin-updates' );
  180. if ( 'allow-automatic-selected' == $_GET[ 'action' ] ) {
  181. $message = __( 'The selected themes have had automatic updates enabled.', 'stops-core-theme-and-plugin-updates' );
  182. } elseif( 'disallow-automatic-selected' == $_GET[ 'action' ] ) {
  183. $message = __( 'The selected themes have had automatic updates disabled.', 'stops-core-theme-and-plugin-updates' );
  184. } elseif( 'disallow-update-selected' == $_GET[ 'action' ] ) {
  185. $message = __( 'The selected theme updates have been disabled.', 'stops-core-theme-and-plugin-updates' );
  186. } elseif( 'allow-update-selected' == $_GET[ 'action' ] ) {
  187. $message = __( 'The selected theme updates have been enabled.', 'stops-core-theme-and-plugin-updates' );
  188. }
  189. ?>
  190. <div class="updated"><p><strong><?php echo esc_html( $message ); ?></strong></p></div>
  191. <?php
  192. }
  193. ?>
  194. <form action="<?php echo esc_url( add_query_arg( array() ) ); ?>" method="post">
  195. <?php
  196. $theme_status = isset( $_GET[ 'theme_status' ] ) ? $_GET[ 'theme_status' ] : false;
  197. if ( false !== $theme_status ) {
  198. printf( '<input type="hidden" name="theme_status" value="%s" />', esc_attr( $theme_status ) );
  199. }
  200. wp_nonce_field( 'mpsum_theme_update', '_mpsum' );
  201. ?>
  202. <h3><?php esc_html_e( 'Theme Update Options', 'stops-core-theme-and-plugin-updates' ); ?></h3>
  203. <?php
  204. $core_options = MPSUM_Updates_Manager::get_options( 'core' );
  205. if ( false === $this->can_update() ) {
  206. printf( '<div class="error"><p><strong>%s</strong></p></div>', esc_html__( 'All theme updates have been disabled.', 'stops-core-theme-and-plugin-updates' ) );
  207. }
  208. $theme_table = new MPSUM_Themes_List_Table( $args = array( 'screen' => $this->slug, 'tab' => $this->tab ) );
  209. $theme_table->prepare_items();
  210. $theme_table->views();
  211. $theme_table->display();
  212. ?>
  213. </form>
  214. <?php
  215. } //end tab_output_plugins
  216. /**
  217. * Outputs the theme action links beneath each theme row.
  218. *
  219. * Outputs the theme action links beneath each theme row.
  220. *
  221. * @since 5.0.0
  222. * @access public
  223. * @see __construct
  224. * @internal uses mpsum_theme_action_links filter
  225. *
  226. * @param array $settings Array of settings to output.
  227. * @param WP_Theme $theme The theme object to take action on.
  228. */
  229. public function theme_action_links( $settings, $theme ) {
  230. $stylesheet = $theme->get_stylesheet();
  231. $theme_options = MPSUM_Updates_Manager::get_options( 'themes' );
  232. if ( false !== $key = array_search( $stylesheet, $theme_options ) ) {
  233. $enable_url = add_query_arg( array( 'action' => 'allow-update-selected', '_mpsum' => wp_create_nonce( 'mpsum_theme_update' ), 'checked' => array( $stylesheet ) ) );
  234. $enable_url = remove_query_arg( 'disabled', $enable_url );
  235. $settings[] = sprintf( '<a href="%s">%s</a>', esc_url( $enable_url ), esc_html__( 'Allow Updates', 'stops-core-theme-and-plugin-updates' ) );
  236. } else {
  237. //Disable Link
  238. $disable_url = add_query_arg( array( 'action' => 'disallow-update-selected', '_mpsum' => wp_create_nonce( 'mpsum_theme_update' ), 'checked' => array( $stylesheet ) ) );
  239. $disable_url = remove_query_arg( 'disabled', $disable_url );
  240. $settings[] = sprintf( '<a href="%s">%s</a>', esc_url( $disable_url ), esc_html__( 'Disallow Updates', 'stops-core-theme-and-plugin-updates' ) );
  241. //Automatic Link
  242. $theme_automatic_options = MPSUM_Updates_Manager::get_options( 'themes_automatic' );
  243. $core_options = MPSUM_Updates_Manager::get_options( 'core' );
  244. if ( isset( $core_options[ 'automatic_theme_updates' ] ) && 'individual' == $core_options[ 'automatic_theme_updates' ] ) {
  245. if ( in_array( $stylesheet, $theme_automatic_options ) ) {
  246. //Disable Link
  247. $disable_automatic_url = add_query_arg( array( 'action' => 'disallow-automatic-selected', '_mpsum' => wp_create_nonce( 'mpsum_theme_update' ), 'checked' => array( $stylesheet ) ) );
  248. $settings[] = sprintf( '<a href="%s">%s</a>', esc_url( $disable_automatic_url ), esc_html__( 'Disallow Automatic Updates', 'stops-core-theme-and-plugin-updates' ) );
  249. } else {
  250. //Enable Link
  251. $enable_automatic_url = add_query_arg( array( 'action' => 'allow-automatic-selected', '_mpsum' => wp_create_nonce( 'mpsum_theme_update' ), 'checked' => array( $stylesheet ) ) );
  252. $settings[] = sprintf( '<a href="%s">%s</a>', esc_url( $enable_automatic_url ), esc_html__( 'Enable Automatic Updates', 'stops-core-theme-and-plugin-updates' ) );
  253. }
  254. }
  255. }
  256. return $settings;
  257. }
  258. }