[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/ -> site-editor.php (source)

   1  <?php
   2  /**
   3   * Site Editor administration screen.
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  global $editor_styles;
  10  
  11  /** WordPress Administration Bootstrap */
  12  require_once  __DIR__ . '/admin.php';
  13  
  14  if ( ! current_user_can( 'edit_theme_options' ) ) {
  15      wp_die(
  16          '<h1>' . __( 'You need a higher level of permission.' ) . '</h1>' .
  17          '<p>' . __( 'Sorry, you are not allowed to edit theme options on this site.' ) . '</p>',
  18          403
  19      );
  20  }
  21  
  22  /**
  23   * Maps old site editor urls to the new updated ones.
  24   *
  25   * @since 6.8.0
  26   * @access private
  27   *
  28   * @global string $pagenow The filename of the current screen.
  29   *
  30   * @return string|false The new URL to redirect to, or false if no redirection is needed.
  31   */
  32  function _wp_get_site_editor_redirection_url() {
  33      global $pagenow;
  34      if ( 'site-editor.php' !== $pagenow || isset( $_REQUEST['p'] ) || empty( $_SERVER['QUERY_STRING'] ) ) {
  35          return false;
  36      }
  37  
  38      // The following redirects are for the new permalinks in the site editor.
  39      if ( isset( $_REQUEST['postType'] ) && 'wp_navigation' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) {
  40          return add_query_arg( array( 'p' => '/wp_navigation/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) );
  41      }
  42  
  43      if ( isset( $_REQUEST['postType'] ) && 'wp_navigation' === $_REQUEST['postType'] && empty( $_REQUEST['postId'] ) ) {
  44          return add_query_arg( array( 'p' => '/navigation' ), remove_query_arg( 'postType' ) );
  45      }
  46  
  47      if ( isset( $_REQUEST['path'] ) && '/wp_global_styles' === $_REQUEST['path'] ) {
  48          return add_query_arg( array( 'p' => '/styles' ), remove_query_arg( 'path' ) );
  49      }
  50  
  51      if ( isset( $_REQUEST['postType'] ) && 'page' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) {
  52          return add_query_arg( array( 'p' => '/page' ), remove_query_arg( 'postType' ) );
  53      }
  54  
  55      if ( isset( $_REQUEST['postType'] ) && 'page' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) {
  56          return add_query_arg( array( 'p' => '/page/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) );
  57      }
  58  
  59      if ( isset( $_REQUEST['postType'] ) && 'wp_template' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) {
  60          return add_query_arg( array( 'p' => '/template' ), remove_query_arg( 'postType' ) );
  61      }
  62  
  63      if ( isset( $_REQUEST['postType'] ) && 'wp_template' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) {
  64          return add_query_arg( array( 'p' => '/wp_template/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) );
  65      }
  66  
  67      if ( isset( $_REQUEST['postType'] ) && 'wp_block' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) {
  68          return add_query_arg( array( 'p' => '/pattern' ), remove_query_arg( 'postType' ) );
  69      }
  70  
  71      if ( isset( $_REQUEST['postType'] ) && 'wp_block' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) {
  72          return add_query_arg( array( 'p' => '/wp_block/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) );
  73      }
  74  
  75      if ( isset( $_REQUEST['postType'] ) && 'wp_template_part' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) {
  76          return add_query_arg( array( 'p' => '/pattern' ) );
  77      }
  78  
  79      if ( isset( $_REQUEST['postType'] ) && 'wp_template_part' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) {
  80          return add_query_arg( array( 'p' => '/wp_template_part/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) );
  81      }
  82  
  83      // The following redirects are for backward compatibility with the old site editor URLs.
  84      if ( isset( $_REQUEST['path'] ) && '/wp_template_part/all' === $_REQUEST['path'] ) {
  85          return add_query_arg(
  86              array(
  87                  'p'        => '/pattern',
  88                  'postType' => 'wp_template_part',
  89              ),
  90              remove_query_arg( 'path' )
  91          );
  92      }
  93  
  94      if ( isset( $_REQUEST['path'] ) && '/page' === $_REQUEST['path'] ) {
  95          return add_query_arg( array( 'p' => '/page' ), remove_query_arg( 'path' ) );
  96      }
  97  
  98      if ( isset( $_REQUEST['path'] ) && '/wp_template' === $_REQUEST['path'] ) {
  99          return add_query_arg( array( 'p' => '/template' ), remove_query_arg( 'path' ) );
 100      }
 101  
 102      if ( isset( $_REQUEST['path'] ) && '/patterns' === $_REQUEST['path'] ) {
 103          return add_query_arg( array( 'p' => '/pattern' ), remove_query_arg( 'path' ) );
 104      }
 105  
 106      if ( isset( $_REQUEST['path'] ) && '/navigation' === $_REQUEST['path'] ) {
 107          return add_query_arg( array( 'p' => '/navigation' ), remove_query_arg( 'path' ) );
 108      }
 109  
 110      return add_query_arg( array( 'p' => '/' ) );
 111  }
 112  
 113  // Redirect to the site editor to the new URLs if needed.
 114  $redirection = _wp_get_site_editor_redirection_url();
 115  if ( false !== $redirection ) {
 116      wp_safe_redirect( $redirection );
 117      exit;
 118  }
 119  
 120  // Used in the HTML title tag.
 121  $title       = _x( 'Editor', 'site editor title tag' );
 122  $parent_file = 'themes.php';
 123  
 124  // Flag that we're loading the block editor.
 125  $current_screen = get_current_screen();
 126  $current_screen->is_block_editor( true );
 127  
 128  // Default to is-fullscreen-mode to avoid jumps in the UI.
 129  add_filter(
 130      'admin_body_class',
 131      static function ( $classes ) {
 132          return "$classes is-fullscreen-mode";
 133      }
 134  );
 135  
 136  $indexed_template_types = array();
 137  foreach ( get_default_block_template_types() as $slug => $template_type ) {
 138      $template_type['slug']    = (string) $slug;
 139      $indexed_template_types[] = $template_type;
 140  }
 141  
 142  $context_settings = array( 'name' => 'core/edit-site' );
 143  
 144  if ( ! empty( $_GET['postId'] ) && is_numeric( $_GET['postId'] ) ) {
 145      $context_settings['post'] = get_post( (int) $_GET['postId'] );
 146  } elseif ( isset( $_GET['p'] ) && preg_match( '/^\/page\/(\d+)$/', $_GET['p'], $matches ) ) {
 147      $context_settings['post'] = get_post( (int) $matches[1] );
 148  }
 149  
 150  $block_editor_context = new WP_Block_Editor_Context( $context_settings );
 151  $custom_settings      = array(
 152      'siteUrl'                   => site_url(),
 153      'postsPerPage'              => get_option( 'posts_per_page' ),
 154      'styles'                    => get_block_editor_theme_styles(),
 155      'defaultTemplateTypes'      => $indexed_template_types,
 156      'defaultTemplatePartAreas'  => get_allowed_block_template_part_areas(),
 157      'supportsLayout'            => wp_theme_has_theme_json(),
 158      'supportsTemplatePartsMode' => ! wp_is_block_theme() && current_theme_supports( 'block-template-parts' ),
 159  );
 160  
 161  // Add additional back-compat patterns registered by `current_screen` et al.
 162  $custom_settings['__experimentalAdditionalBlockPatterns']          = WP_Block_Patterns_Registry::get_instance()->get_all_registered( true );
 163  $custom_settings['__experimentalAdditionalBlockPatternCategories'] = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered( true );
 164  
 165  $editor_settings = get_block_editor_settings( $custom_settings, $block_editor_context );
 166  
 167  if ( isset( $_GET['postType'] ) && ! isset( $_GET['postId'] ) ) {
 168      $post_type = get_post_type_object( $_GET['postType'] );
 169      if ( ! $post_type ) {
 170          wp_die( __( 'Invalid post type.' ) );
 171      }
 172  }
 173  
 174  $active_global_styles_id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
 175  $active_theme            = get_stylesheet();
 176  
 177  $navigation_rest_route = rest_get_route_for_post_type_items(
 178      'wp_navigation'
 179  );
 180  
 181  $preload_paths = array(
 182      array( rest_get_route_for_post_type_items( 'attachment' ), 'OPTIONS' ),
 183      array( rest_get_route_for_post_type_items( 'page' ), 'OPTIONS' ),
 184      '/wp/v2/types?context=view',
 185      '/wp/v2/types/wp_template?context=edit',
 186      '/wp/v2/types/wp_template_part?context=edit',
 187      '/wp/v2/templates?context=edit&per_page=-1',
 188      '/wp/v2/template-parts?context=edit&per_page=-1',
 189      '/wp/v2/themes?context=edit&status=active',
 190      '/wp/v2/global-styles/' . $active_global_styles_id . '?context=edit',
 191      array( '/wp/v2/global-styles/' . $active_global_styles_id, 'OPTIONS' ),
 192      '/wp/v2/global-styles/themes/' . $active_theme . '?context=view',
 193      '/wp/v2/global-styles/themes/' . $active_theme . '/variations?context=view',
 194      array( $navigation_rest_route, 'OPTIONS' ),
 195      array(
 196          add_query_arg(
 197              array(
 198                  'context'   => 'edit',
 199                  'per_page'  => 100,
 200                  'order'     => 'desc',
 201                  'orderby'   => 'date',
 202                  // array indices are required to avoid query being encoded and not matching in cache.
 203                  'status[0]' => 'publish',
 204                  'status[1]' => 'draft',
 205              ),
 206              $navigation_rest_route
 207          ),
 208          'GET',
 209      ),
 210      '/wp/v2/settings',
 211      array( '/wp/v2/settings', 'OPTIONS' ),
 212      // Used by getBlockPatternCategories in useBlockEditorSettings.
 213      '/wp/v2/block-patterns/categories',
 214      // @see packages/core-data/src/entities.js
 215      '/?_fields=' . implode(
 216          ',',
 217          array(
 218              'description',
 219              'gmt_offset',
 220              'home',
 221              'image_sizes',
 222              'image_size_threshold',
 223              'image_output_formats',
 224              'jpeg_interlaced',
 225              'png_interlaced',
 226              'gif_interlaced',
 227              'name',
 228              'site_icon',
 229              'site_icon_url',
 230              'site_logo',
 231              'timezone_string',
 232              'url',
 233              'page_for_posts',
 234              'page_on_front',
 235              'show_on_front',
 236          )
 237      ),
 238  );
 239  
 240  if ( $block_editor_context->post ) {
 241      $route_for_post = rest_get_route_for_post( $block_editor_context->post );
 242      if ( $route_for_post ) {
 243          $preload_paths[] = add_query_arg( 'context', 'edit', $route_for_post );
 244          if ( 'page' === $block_editor_context->post->post_type ) {
 245              $preload_paths[] = add_query_arg(
 246                  'slug',
 247                  // @link https://github.com/WordPress/gutenberg/blob/e093fefd041eb6cc4a4e7f67b92ab54fd75c8858/packages/core-data/src/private-selectors.ts#L244-L254
 248                  empty( $block_editor_context->post->post_name ) ? 'page' : 'page-' . $block_editor_context->post->post_name,
 249                  '/wp/v2/templates/lookup'
 250              );
 251          }
 252      }
 253  } else {
 254      $preload_paths[] = '/wp/v2/templates/lookup?slug=front-page';
 255      $preload_paths[] = '/wp/v2/templates/lookup?slug=home';
 256  }
 257  
 258  block_editor_rest_api_preload( $preload_paths, $block_editor_context );
 259  
 260  wp_add_inline_script(
 261      'wp-edit-site',
 262      sprintf(
 263          'wp.domReady( function() {
 264              wp.editSite.initializeEditor( "site-editor", %s );
 265          } );',
 266          wp_json_encode( $editor_settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
 267      )
 268  );
 269  
 270  // Preload server-registered block schemas.
 271  wp_add_inline_script(
 272      'wp-blocks',
 273      'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) . ');'
 274  );
 275  
 276  // Preload server-registered block bindings sources.
 277  $registered_sources = get_all_registered_block_bindings_sources();
 278  if ( ! empty( $registered_sources ) ) {
 279      $filtered_sources = array();
 280      foreach ( $registered_sources as $source ) {
 281          $filtered_sources[] = array(
 282              'name'        => $source->name,
 283              'label'       => $source->label,
 284              'usesContext' => $source->uses_context,
 285          );
 286      }
 287      $script = sprintf( 'for ( const source of %s ) { wp.blocks.registerBlockBindingsSource( source ); }', wp_json_encode( $filtered_sources, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) );
 288      wp_add_inline_script(
 289          'wp-blocks',
 290          $script
 291      );
 292  }
 293  
 294  wp_add_inline_script(
 295      'wp-blocks',
 296      sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( $editor_settings['blockCategories'] ?? array(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) ),
 297      'after'
 298  );
 299  
 300  wp_enqueue_script( 'wp-edit-site' );
 301  wp_enqueue_script( 'wp-format-library' );
 302  wp_enqueue_style( 'wp-edit-site' );
 303  wp_enqueue_style( 'wp-format-library' );
 304  wp_enqueue_media();
 305  
 306  if (
 307      current_theme_supports( 'wp-block-styles' ) &&
 308      ( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 )
 309  ) {
 310      wp_enqueue_style( 'wp-block-library-theme' );
 311  }
 312  
 313  /** This action is documented in wp-admin/edit-form-blocks.php */
 314  do_action( 'enqueue_block_editor_assets' );
 315  
 316  require_once  ABSPATH . 'wp-admin/admin-header.php';
 317  ?>
 318  
 319  <div class="edit-site" id="site-editor">
 320      <?php // JavaScript is disabled. ?>
 321      <div class="wrap hide-if-js site-editor-no-js">
 322          <h1 class="wp-heading-inline"><?php _e( 'Edit Site' ); ?></h1>
 323          <?php
 324          /**
 325           * Filters the message displayed in the site editor interface when JavaScript is
 326           * not enabled in the browser.
 327           *
 328           * @since 6.3.0
 329           *
 330           * @param string  $message The message being displayed.
 331           * @param WP_Post $post    The post being edited.
 332           */
 333          $message = apply_filters( 'site_editor_no_javascript_message', __( 'The site editor requires JavaScript. Please enable JavaScript in your browser settings.' ), $post );
 334          wp_admin_notice(
 335              $message,
 336              array(
 337                  'type'               => 'error',
 338                  'additional_classes' => array( 'hide-if-js' ),
 339              )
 340          );
 341          ?>
 342      </div>
 343  </div>
 344  
 345  <?php
 346  
 347  require_once  ABSPATH . 'wp-admin/admin-footer.php';


Generated : Tue May 5 08:20:14 2026 Cross-referenced by PHPXref