themes.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?php
  2. /**
  3. * Multisite themes administration panel.
  4. *
  5. * @package WordPress
  6. * @subpackage Multisite
  7. * @since 3.1.0
  8. */
  9. /** Load WordPress Administration Bootstrap */
  10. require_once __DIR__ . '/admin.php';
  11. if ( ! current_user_can( 'manage_network_themes' ) ) {
  12. wp_die( __( 'Sorry, you are not allowed to manage network themes.' ) );
  13. }
  14. $wp_list_table = _get_list_table( 'WP_MS_Themes_List_Table' );
  15. $pagenum = $wp_list_table->get_pagenum();
  16. $action = $wp_list_table->current_action();
  17. $s = isset( $_REQUEST['s'] ) ? $_REQUEST['s'] : '';
  18. // Clean up request URI from temporary args for screen options/paging uri's to work as expected.
  19. $temp_args = array(
  20. 'enabled',
  21. 'disabled',
  22. 'deleted',
  23. 'error',
  24. 'enabled-auto-update',
  25. 'disabled-auto-update',
  26. );
  27. $_SERVER['REQUEST_URI'] = remove_query_arg( $temp_args, $_SERVER['REQUEST_URI'] );
  28. $referer = remove_query_arg( $temp_args, wp_get_referer() );
  29. if ( $action ) {
  30. switch ( $action ) {
  31. case 'enable':
  32. check_admin_referer( 'enable-theme_' . $_GET['theme'] );
  33. WP_Theme::network_enable_theme( $_GET['theme'] );
  34. if ( false === strpos( $referer, '/network/themes.php' ) ) {
  35. wp_redirect( network_admin_url( 'themes.php?enabled=1' ) );
  36. } else {
  37. wp_safe_redirect( add_query_arg( 'enabled', 1, $referer ) );
  38. }
  39. exit;
  40. case 'disable':
  41. check_admin_referer( 'disable-theme_' . $_GET['theme'] );
  42. WP_Theme::network_disable_theme( $_GET['theme'] );
  43. wp_safe_redirect( add_query_arg( 'disabled', '1', $referer ) );
  44. exit;
  45. case 'enable-selected':
  46. check_admin_referer( 'bulk-themes' );
  47. $themes = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array();
  48. if ( empty( $themes ) ) {
  49. wp_safe_redirect( add_query_arg( 'error', 'none', $referer ) );
  50. exit;
  51. }
  52. WP_Theme::network_enable_theme( (array) $themes );
  53. wp_safe_redirect( add_query_arg( 'enabled', count( $themes ), $referer ) );
  54. exit;
  55. case 'disable-selected':
  56. check_admin_referer( 'bulk-themes' );
  57. $themes = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array();
  58. if ( empty( $themes ) ) {
  59. wp_safe_redirect( add_query_arg( 'error', 'none', $referer ) );
  60. exit;
  61. }
  62. WP_Theme::network_disable_theme( (array) $themes );
  63. wp_safe_redirect( add_query_arg( 'disabled', count( $themes ), $referer ) );
  64. exit;
  65. case 'update-selected':
  66. check_admin_referer( 'bulk-themes' );
  67. if ( isset( $_GET['themes'] ) ) {
  68. $themes = explode( ',', $_GET['themes'] );
  69. } elseif ( isset( $_POST['checked'] ) ) {
  70. $themes = (array) $_POST['checked'];
  71. } else {
  72. $themes = array();
  73. }
  74. // Used in the HTML title tag.
  75. $title = __( 'Update Themes' );
  76. $parent_file = 'themes.php';
  77. require_once ABSPATH . 'wp-admin/admin-header.php';
  78. echo '<div class="wrap">';
  79. echo '<h1>' . esc_html( $title ) . '</h1>';
  80. $url = self_admin_url( 'update.php?action=update-selected-themes&amp;themes=' . urlencode( implode( ',', $themes ) ) );
  81. $url = wp_nonce_url( $url, 'bulk-update-themes' );
  82. echo "<iframe src='$url' style='width: 100%; height:100%; min-height:850px;'></iframe>";
  83. echo '</div>';
  84. require_once ABSPATH . 'wp-admin/admin-footer.php';
  85. exit;
  86. case 'delete-selected':
  87. if ( ! current_user_can( 'delete_themes' ) ) {
  88. wp_die( __( 'Sorry, you are not allowed to delete themes for this site.' ) );
  89. }
  90. check_admin_referer( 'bulk-themes' );
  91. $themes = isset( $_REQUEST['checked'] ) ? (array) $_REQUEST['checked'] : array();
  92. if ( empty( $themes ) ) {
  93. wp_safe_redirect( add_query_arg( 'error', 'none', $referer ) );
  94. exit;
  95. }
  96. $themes = array_diff( $themes, array( get_option( 'stylesheet' ), get_option( 'template' ) ) );
  97. if ( empty( $themes ) ) {
  98. wp_safe_redirect( add_query_arg( 'error', 'main', $referer ) );
  99. exit;
  100. }
  101. $theme_info = array();
  102. foreach ( $themes as $key => $theme ) {
  103. $theme_info[ $theme ] = wp_get_theme( $theme );
  104. }
  105. require ABSPATH . 'wp-admin/update.php';
  106. $parent_file = 'themes.php';
  107. if ( ! isset( $_REQUEST['verify-delete'] ) ) {
  108. wp_enqueue_script( 'jquery' );
  109. require_once ABSPATH . 'wp-admin/admin-header.php';
  110. $themes_to_delete = count( $themes );
  111. ?>
  112. <div class="wrap">
  113. <?php if ( 1 === $themes_to_delete ) : ?>
  114. <h1><?php _e( 'Delete Theme' ); ?></h1>
  115. <div class="error"><p><strong><?php _e( 'Caution:' ); ?></strong> <?php _e( 'This theme may be active on other sites in the network.' ); ?></p></div>
  116. <p><?php _e( 'You are about to remove the following theme:' ); ?></p>
  117. <?php else : ?>
  118. <h1><?php _e( 'Delete Themes' ); ?></h1>
  119. <div class="error"><p><strong><?php _e( 'Caution:' ); ?></strong> <?php _e( 'These themes may be active on other sites in the network.' ); ?></p></div>
  120. <p><?php _e( 'You are about to remove the following themes:' ); ?></p>
  121. <?php endif; ?>
  122. <ul class="ul-disc">
  123. <?php
  124. foreach ( $theme_info as $theme ) {
  125. echo '<li>' . sprintf(
  126. /* translators: 1: Theme name, 2: Theme author. */
  127. _x( '%1$s by %2$s', 'theme' ),
  128. '<strong>' . $theme->display( 'Name' ) . '</strong>',
  129. '<em>' . $theme->display( 'Author' ) . '</em>'
  130. ) . '</li>';
  131. }
  132. ?>
  133. </ul>
  134. <?php if ( 1 === $themes_to_delete ) : ?>
  135. <p><?php _e( 'Are you sure you want to delete this theme?' ); ?></p>
  136. <?php else : ?>
  137. <p><?php _e( 'Are you sure you want to delete these themes?' ); ?></p>
  138. <?php endif; ?>
  139. <form method="post" action="<?php echo esc_url( $_SERVER['REQUEST_URI'] ); ?>" style="display:inline;">
  140. <input type="hidden" name="verify-delete" value="1" />
  141. <input type="hidden" name="action" value="delete-selected" />
  142. <?php
  143. foreach ( (array) $themes as $theme ) {
  144. echo '<input type="hidden" name="checked[]" value="' . esc_attr( $theme ) . '" />';
  145. }
  146. wp_nonce_field( 'bulk-themes' );
  147. if ( 1 === $themes_to_delete ) {
  148. submit_button( __( 'Yes, delete this theme' ), '', 'submit', false );
  149. } else {
  150. submit_button( __( 'Yes, delete these themes' ), '', 'submit', false );
  151. }
  152. ?>
  153. </form>
  154. <?php $referer = wp_get_referer(); ?>
  155. <form method="post" action="<?php echo $referer ? esc_url( $referer ) : ''; ?>" style="display:inline;">
  156. <?php submit_button( __( 'No, return me to the theme list' ), '', 'submit', false ); ?>
  157. </form>
  158. </div>
  159. <?php
  160. require_once ABSPATH . 'wp-admin/admin-footer.php';
  161. exit;
  162. } // End if verify-delete.
  163. foreach ( $themes as $theme ) {
  164. $delete_result = delete_theme(
  165. $theme,
  166. esc_url(
  167. add_query_arg(
  168. array(
  169. 'verify-delete' => 1,
  170. 'action' => 'delete-selected',
  171. 'checked' => $_REQUEST['checked'],
  172. '_wpnonce' => $_REQUEST['_wpnonce'],
  173. ),
  174. network_admin_url( 'themes.php' )
  175. )
  176. )
  177. );
  178. }
  179. $paged = ( $_REQUEST['paged'] ) ? $_REQUEST['paged'] : 1;
  180. wp_redirect(
  181. add_query_arg(
  182. array(
  183. 'deleted' => count( $themes ),
  184. 'paged' => $paged,
  185. 's' => $s,
  186. ),
  187. network_admin_url( 'themes.php' )
  188. )
  189. );
  190. exit;
  191. case 'enable-auto-update':
  192. case 'disable-auto-update':
  193. case 'enable-auto-update-selected':
  194. case 'disable-auto-update-selected':
  195. if ( ! ( current_user_can( 'update_themes' ) && wp_is_auto_update_enabled_for_type( 'theme' ) ) ) {
  196. wp_die( __( 'Sorry, you are not allowed to change themes automatic update settings.' ) );
  197. }
  198. if ( 'enable-auto-update' === $action || 'disable-auto-update' === $action ) {
  199. check_admin_referer( 'updates' );
  200. } else {
  201. if ( empty( $_POST['checked'] ) ) {
  202. // Nothing to do.
  203. wp_safe_redirect( add_query_arg( 'error', 'none', $referer ) );
  204. exit;
  205. }
  206. check_admin_referer( 'bulk-themes' );
  207. }
  208. $auto_updates = (array) get_site_option( 'auto_update_themes', array() );
  209. if ( 'enable-auto-update' === $action ) {
  210. $auto_updates[] = $_GET['theme'];
  211. $auto_updates = array_unique( $auto_updates );
  212. $referer = add_query_arg( 'enabled-auto-update', 1, $referer );
  213. } elseif ( 'disable-auto-update' === $action ) {
  214. $auto_updates = array_diff( $auto_updates, array( $_GET['theme'] ) );
  215. $referer = add_query_arg( 'disabled-auto-update', 1, $referer );
  216. } else {
  217. // Bulk enable/disable.
  218. $themes = (array) wp_unslash( $_POST['checked'] );
  219. if ( 'enable-auto-update-selected' === $action ) {
  220. $auto_updates = array_merge( $auto_updates, $themes );
  221. $auto_updates = array_unique( $auto_updates );
  222. $referer = add_query_arg( 'enabled-auto-update', count( $themes ), $referer );
  223. } else {
  224. $auto_updates = array_diff( $auto_updates, $themes );
  225. $referer = add_query_arg( 'disabled-auto-update', count( $themes ), $referer );
  226. }
  227. }
  228. $all_items = wp_get_themes();
  229. // Remove themes that don't exist or have been deleted since the option was last updated.
  230. $auto_updates = array_intersect( $auto_updates, array_keys( $all_items ) );
  231. update_site_option( 'auto_update_themes', $auto_updates );
  232. wp_safe_redirect( $referer );
  233. exit;
  234. default:
  235. $themes = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array();
  236. if ( empty( $themes ) ) {
  237. wp_safe_redirect( add_query_arg( 'error', 'none', $referer ) );
  238. exit;
  239. }
  240. check_admin_referer( 'bulk-themes' );
  241. /** This action is documented in wp-admin/network/site-themes.php */
  242. $referer = apply_filters( 'handle_network_bulk_actions-' . get_current_screen()->id, $referer, $action, $themes ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  243. wp_safe_redirect( $referer );
  244. exit;
  245. }
  246. }
  247. $wp_list_table->prepare_items();
  248. add_thickbox();
  249. add_screen_option( 'per_page' );
  250. get_current_screen()->add_help_tab(
  251. array(
  252. 'id' => 'overview',
  253. 'title' => __( 'Overview' ),
  254. 'content' =>
  255. '<p>' . __( 'This screen enables and disables the inclusion of themes available to choose in the Appearance menu for each site. It does not activate or deactivate which theme a site is currently using.' ) . '</p>' .
  256. '<p>' . __( 'If the network admin disables a theme that is in use, it can still remain selected on that site. If another theme is chosen, the disabled theme will not appear in the site&#8217;s Appearance > Themes screen.' ) . '</p>' .
  257. '<p>' . __( 'Themes can be enabled on a site by site basis by the network admin on the Edit Site screen (which has a Themes tab); get there via the Edit action link on the All Sites screen. Only network admins are able to install or edit themes.' ) . '</p>',
  258. )
  259. );
  260. $help_sidebar_autoupdates = '';
  261. if ( current_user_can( 'update_themes' ) && wp_is_auto_update_enabled_for_type( 'theme' ) ) {
  262. get_current_screen()->add_help_tab(
  263. array(
  264. 'id' => 'plugins-themes-auto-updates',
  265. 'title' => __( 'Auto-updates' ),
  266. 'content' =>
  267. '<p>' . __( 'Auto-updates can be enabled or disabled for each individual theme. Themes with auto-updates enabled will display the estimated date of the next auto-update. Auto-updates depends on the WP-Cron task scheduling system.' ) . '</p>' .
  268. '<p>' . __( 'Please note: Third-party themes and plugins, or custom code, may override WordPress scheduling.' ) . '</p>',
  269. )
  270. );
  271. $help_sidebar_autoupdates = '<p>' . __( '<a href="https://wordpress.org/support/article/plugins-themes-auto-updates/">Learn more: Auto-updates documentation</a>' ) . '</p>';
  272. }
  273. get_current_screen()->set_help_sidebar(
  274. '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
  275. '<p>' . __( '<a href="https://codex.wordpress.org/Network_Admin_Themes_Screen">Documentation on Network Themes</a>' ) . '</p>' .
  276. $help_sidebar_autoupdates .
  277. '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
  278. );
  279. get_current_screen()->set_screen_reader_content(
  280. array(
  281. 'heading_views' => __( 'Filter themes list' ),
  282. 'heading_pagination' => __( 'Themes list navigation' ),
  283. 'heading_list' => __( 'Themes list' ),
  284. )
  285. );
  286. // Used in the HTML title tag.
  287. $title = __( 'Themes' );
  288. $parent_file = 'themes.php';
  289. wp_enqueue_script( 'updates' );
  290. wp_enqueue_script( 'theme-preview' );
  291. require_once ABSPATH . 'wp-admin/admin-header.php';
  292. ?>
  293. <div class="wrap">
  294. <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
  295. <?php if ( current_user_can( 'install_themes' ) ) : ?>
  296. <a href="theme-install.php" class="page-title-action"><?php echo esc_html_x( 'Add New', 'theme' ); ?></a>
  297. <?php endif; ?>
  298. <?php
  299. if ( isset( $_REQUEST['s'] ) && strlen( $_REQUEST['s'] ) ) {
  300. echo '<span class="subtitle">';
  301. printf(
  302. /* translators: %s: Search query. */
  303. __( 'Search results for: %s' ),
  304. '<strong>' . esc_html( $s ) . '</strong>'
  305. );
  306. echo '</span>';
  307. }
  308. ?>
  309. <hr class="wp-header-end">
  310. <?php
  311. if ( isset( $_GET['enabled'] ) ) {
  312. $enabled = absint( $_GET['enabled'] );
  313. if ( 1 === $enabled ) {
  314. $message = __( 'Theme enabled.' );
  315. } else {
  316. /* translators: %s: Number of themes. */
  317. $message = _n( '%s theme enabled.', '%s themes enabled.', $enabled );
  318. }
  319. echo '<div id="message" class="updated notice is-dismissible"><p>' . sprintf( $message, number_format_i18n( $enabled ) ) . '</p></div>';
  320. } elseif ( isset( $_GET['disabled'] ) ) {
  321. $disabled = absint( $_GET['disabled'] );
  322. if ( 1 === $disabled ) {
  323. $message = __( 'Theme disabled.' );
  324. } else {
  325. /* translators: %s: Number of themes. */
  326. $message = _n( '%s theme disabled.', '%s themes disabled.', $disabled );
  327. }
  328. echo '<div id="message" class="updated notice is-dismissible"><p>' . sprintf( $message, number_format_i18n( $disabled ) ) . '</p></div>';
  329. } elseif ( isset( $_GET['deleted'] ) ) {
  330. $deleted = absint( $_GET['deleted'] );
  331. if ( 1 === $deleted ) {
  332. $message = __( 'Theme deleted.' );
  333. } else {
  334. /* translators: %s: Number of themes. */
  335. $message = _n( '%s theme deleted.', '%s themes deleted.', $deleted );
  336. }
  337. echo '<div id="message" class="updated notice is-dismissible"><p>' . sprintf( $message, number_format_i18n( $deleted ) ) . '</p></div>';
  338. } elseif ( isset( $_GET['enabled-auto-update'] ) ) {
  339. $enabled = absint( $_GET['enabled-auto-update'] );
  340. if ( 1 === $enabled ) {
  341. $message = __( 'Theme will be auto-updated.' );
  342. } else {
  343. /* translators: %s: Number of themes. */
  344. $message = _n( '%s theme will be auto-updated.', '%s themes will be auto-updated.', $enabled );
  345. }
  346. echo '<div id="message" class="updated notice is-dismissible"><p>' . sprintf( $message, number_format_i18n( $enabled ) ) . '</p></div>';
  347. } elseif ( isset( $_GET['disabled-auto-update'] ) ) {
  348. $disabled = absint( $_GET['disabled-auto-update'] );
  349. if ( 1 === $disabled ) {
  350. $message = __( 'Theme will no longer be auto-updated.' );
  351. } else {
  352. /* translators: %s: Number of themes. */
  353. $message = _n( '%s theme will no longer be auto-updated.', '%s themes will no longer be auto-updated.', $disabled );
  354. }
  355. echo '<div id="message" class="updated notice is-dismissible"><p>' . sprintf( $message, number_format_i18n( $disabled ) ) . '</p></div>';
  356. } elseif ( isset( $_GET['error'] ) && 'none' === $_GET['error'] ) {
  357. echo '<div id="message" class="error notice is-dismissible"><p>' . __( 'No theme selected.' ) . '</p></div>';
  358. } elseif ( isset( $_GET['error'] ) && 'main' === $_GET['error'] ) {
  359. echo '<div class="error notice is-dismissible"><p>' . __( 'You cannot delete a theme while it is active on the main site.' ) . '</p></div>';
  360. }
  361. ?>
  362. <form method="get">
  363. <?php $wp_list_table->search_box( __( 'Search Installed Themes' ), 'theme' ); ?>
  364. </form>
  365. <?php
  366. $wp_list_table->views();
  367. if ( 'broken' === $status ) {
  368. echo '<p class="clear">' . __( 'The following themes are installed but incomplete.' ) . '</p>';
  369. }
  370. ?>
  371. <form id="bulk-action-form" method="post">
  372. <input type="hidden" name="theme_status" value="<?php echo esc_attr( $status ); ?>" />
  373. <input type="hidden" name="paged" value="<?php echo esc_attr( $page ); ?>" />
  374. <?php $wp_list_table->display(); ?>
  375. </form>
  376. </div>
  377. <?php
  378. wp_print_request_filesystem_credentials_modal();
  379. wp_print_admin_notice_templates();
  380. wp_print_update_row_templates();
  381. require_once ABSPATH . 'wp-admin/admin-footer.php';