site-themes.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * Edit Site Themes Administration Screen
  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_sites' ) ) {
  12. wp_die( __( 'Sorry, you are not allowed to manage themes for this site.' ) );
  13. }
  14. get_current_screen()->add_help_tab( get_site_screen_help_tab_args() );
  15. get_current_screen()->set_help_sidebar( get_site_screen_help_sidebar_content() );
  16. get_current_screen()->set_screen_reader_content(
  17. array(
  18. 'heading_views' => __( 'Filter site themes list' ),
  19. 'heading_pagination' => __( 'Site themes list navigation' ),
  20. 'heading_list' => __( 'Site themes list' ),
  21. )
  22. );
  23. $wp_list_table = _get_list_table( 'WP_MS_Themes_List_Table' );
  24. $action = $wp_list_table->current_action();
  25. $s = isset( $_REQUEST['s'] ) ? $_REQUEST['s'] : '';
  26. // Clean up request URI from temporary args for screen options/paging uri's to work as expected.
  27. $temp_args = array( 'enabled', 'disabled', 'error' );
  28. $_SERVER['REQUEST_URI'] = remove_query_arg( $temp_args, $_SERVER['REQUEST_URI'] );
  29. $referer = remove_query_arg( $temp_args, wp_get_referer() );
  30. if ( ! empty( $_REQUEST['paged'] ) ) {
  31. $referer = add_query_arg( 'paged', (int) $_REQUEST['paged'], $referer );
  32. }
  33. $id = isset( $_REQUEST['id'] ) ? (int) $_REQUEST['id'] : 0;
  34. if ( ! $id ) {
  35. wp_die( __( 'Invalid site ID.' ) );
  36. }
  37. $wp_list_table->prepare_items();
  38. $details = get_site( $id );
  39. if ( ! $details ) {
  40. wp_die( __( 'The requested site does not exist.' ) );
  41. }
  42. if ( ! can_edit_network( $details->site_id ) ) {
  43. wp_die( __( 'Sorry, you are not allowed to access this page.' ), 403 );
  44. }
  45. $is_main_site = is_main_site( $id );
  46. if ( $action ) {
  47. switch_to_blog( $id );
  48. $allowed_themes = get_option( 'allowedthemes' );
  49. switch ( $action ) {
  50. case 'enable':
  51. check_admin_referer( 'enable-theme_' . $_GET['theme'] );
  52. $theme = $_GET['theme'];
  53. $action = 'enabled';
  54. $n = 1;
  55. if ( ! $allowed_themes ) {
  56. $allowed_themes = array( $theme => true );
  57. } else {
  58. $allowed_themes[ $theme ] = true;
  59. }
  60. break;
  61. case 'disable':
  62. check_admin_referer( 'disable-theme_' . $_GET['theme'] );
  63. $theme = $_GET['theme'];
  64. $action = 'disabled';
  65. $n = 1;
  66. if ( ! $allowed_themes ) {
  67. $allowed_themes = array();
  68. } else {
  69. unset( $allowed_themes[ $theme ] );
  70. }
  71. break;
  72. case 'enable-selected':
  73. check_admin_referer( 'bulk-themes' );
  74. if ( isset( $_POST['checked'] ) ) {
  75. $themes = (array) $_POST['checked'];
  76. $action = 'enabled';
  77. $n = count( $themes );
  78. foreach ( (array) $themes as $theme ) {
  79. $allowed_themes[ $theme ] = true;
  80. }
  81. } else {
  82. $action = 'error';
  83. $n = 'none';
  84. }
  85. break;
  86. case 'disable-selected':
  87. check_admin_referer( 'bulk-themes' );
  88. if ( isset( $_POST['checked'] ) ) {
  89. $themes = (array) $_POST['checked'];
  90. $action = 'disabled';
  91. $n = count( $themes );
  92. foreach ( (array) $themes as $theme ) {
  93. unset( $allowed_themes[ $theme ] );
  94. }
  95. } else {
  96. $action = 'error';
  97. $n = 'none';
  98. }
  99. break;
  100. default:
  101. if ( isset( $_POST['checked'] ) ) {
  102. check_admin_referer( 'bulk-themes' );
  103. $themes = (array) $_POST['checked'];
  104. $n = count( $themes );
  105. $screen = get_current_screen()->id;
  106. /**
  107. * Fires when a custom bulk action should be handled.
  108. *
  109. * The redirect link should be modified with success or failure feedback
  110. * from the action to be used to display feedback to the user.
  111. *
  112. * The dynamic portion of the hook name, `$screen`, refers to the current screen ID.
  113. *
  114. * @since 4.7.0
  115. *
  116. * @param string $redirect_url The redirect URL.
  117. * @param string $action The action being taken.
  118. * @param array $items The items to take the action on.
  119. * @param int $site_id The site ID.
  120. */
  121. $referer = apply_filters( "handle_network_bulk_actions-{$screen}", $referer, $action, $themes, $id ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  122. } else {
  123. $action = 'error';
  124. $n = 'none';
  125. }
  126. }
  127. update_option( 'allowedthemes', $allowed_themes );
  128. restore_current_blog();
  129. wp_safe_redirect(
  130. add_query_arg(
  131. array(
  132. 'id' => $id,
  133. $action => $n,
  134. ),
  135. $referer
  136. )
  137. );
  138. exit;
  139. }
  140. if ( isset( $_GET['action'] ) && 'update-site' === $_GET['action'] ) {
  141. wp_safe_redirect( $referer );
  142. exit;
  143. }
  144. add_thickbox();
  145. add_screen_option( 'per_page' );
  146. // Used in the HTML title tag.
  147. /* translators: %s: Site title. */
  148. $title = sprintf( __( 'Edit Site: %s' ), esc_html( $details->blogname ) );
  149. $parent_file = 'sites.php';
  150. $submenu_file = 'sites.php';
  151. require_once ABSPATH . 'wp-admin/admin-header.php'; ?>
  152. <div class="wrap">
  153. <h1 id="edit-site"><?php echo $title; ?></h1>
  154. <p class="edit-site-actions"><a href="<?php echo esc_url( get_home_url( $id, '/' ) ); ?>"><?php _e( 'Visit' ); ?></a> | <a href="<?php echo esc_url( get_admin_url( $id ) ); ?>"><?php _e( 'Dashboard' ); ?></a></p>
  155. <?php
  156. network_edit_site_nav(
  157. array(
  158. 'blog_id' => $id,
  159. 'selected' => 'site-themes',
  160. )
  161. );
  162. if ( isset( $_GET['enabled'] ) ) {
  163. $enabled = absint( $_GET['enabled'] );
  164. if ( 1 === $enabled ) {
  165. $message = __( 'Theme enabled.' );
  166. } else {
  167. /* translators: %s: Number of themes. */
  168. $message = _n( '%s theme enabled.', '%s themes enabled.', $enabled );
  169. }
  170. echo '<div id="message" class="updated notice is-dismissible"><p>' . sprintf( $message, number_format_i18n( $enabled ) ) . '</p></div>';
  171. } elseif ( isset( $_GET['disabled'] ) ) {
  172. $disabled = absint( $_GET['disabled'] );
  173. if ( 1 === $disabled ) {
  174. $message = __( 'Theme disabled.' );
  175. } else {
  176. /* translators: %s: Number of themes. */
  177. $message = _n( '%s theme disabled.', '%s themes disabled.', $disabled );
  178. }
  179. echo '<div id="message" class="updated notice is-dismissible"><p>' . sprintf( $message, number_format_i18n( $disabled ) ) . '</p></div>';
  180. } elseif ( isset( $_GET['error'] ) && 'none' === $_GET['error'] ) {
  181. echo '<div id="message" class="error notice is-dismissible"><p>' . __( 'No theme selected.' ) . '</p></div>';
  182. }
  183. ?>
  184. <p><?php _e( 'Network enabled themes are not shown on this screen.' ); ?></p>
  185. <form method="get">
  186. <?php $wp_list_table->search_box( __( 'Search Installed Themes' ), 'theme' ); ?>
  187. <input type="hidden" name="id" value="<?php echo esc_attr( $id ); ?>" />
  188. </form>
  189. <?php $wp_list_table->views(); ?>
  190. <form method="post" action="site-themes.php?action=update-site">
  191. <input type="hidden" name="id" value="<?php echo esc_attr( $id ); ?>" />
  192. <?php $wp_list_table->display(); ?>
  193. </form>
  194. </div>
  195. <?php require_once ABSPATH . 'wp-admin/admin-footer.php'; ?>