wp/wp-admin/site-editor.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
--- a/wp/wp-admin/site-editor.php	Fri Sep 05 18:40:08 2025 +0200
+++ b/wp/wp-admin/site-editor.php	Fri Sep 05 18:52:52 2025 +0200
@@ -19,19 +19,102 @@
 	);
 }
 
-$is_template_part        = isset( $_GET['postType'] ) && 'wp_template_part' === sanitize_key( $_GET['postType'] );
-$is_template_part_path   = isset( $_GET['path'] ) && 'wp_template_partall' === sanitize_key( $_GET['path'] );
-$is_template_part_editor = $is_template_part || $is_template_part_path;
-$is_patterns             = isset( $_GET['postType'] ) && 'wp_block' === sanitize_key( $_GET['postType'] );
-$is_patterns_path        = isset( $_GET['path'] ) && 'patterns' === sanitize_key( $_GET['path'] );
-$is_patterns_editor      = $is_patterns || $is_patterns_path;
+/**
+ * Maps old site editor urls to the new updated ones.
+ *
+ * @since 6.8.0
+ * @access private
+ *
+ * @global string $pagenow The filename of the current screen.
+ *
+ * @return string|false The new URL to redirect to, or false if no redirection is needed.
+ */
+function _wp_get_site_editor_redirection_url() {
+	global $pagenow;
+	if ( 'site-editor.php' !== $pagenow || isset( $_REQUEST['p'] ) || empty( $_SERVER['QUERY_STRING'] ) ) {
+		return false;
+	}
+
+	// The following redirects are for the new permalinks in the site editor.
+	if ( isset( $_REQUEST['postType'] ) && 'wp_navigation' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) {
+		return add_query_arg( array( 'p' => '/wp_navigation/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) );
+	}
+
+	if ( isset( $_REQUEST['postType'] ) && 'wp_navigation' === $_REQUEST['postType'] && empty( $_REQUEST['postId'] ) ) {
+		return add_query_arg( array( 'p' => '/navigation' ), remove_query_arg( 'postType' ) );
+	}
+
+	if ( isset( $_REQUEST['path'] ) && '/wp_global_styles' === $_REQUEST['path'] ) {
+		return add_query_arg( array( 'p' => '/styles' ), remove_query_arg( 'path' ) );
+	}
+
+	if ( isset( $_REQUEST['postType'] ) && 'page' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) {
+		return add_query_arg( array( 'p' => '/page' ), remove_query_arg( 'postType' ) );
+	}
+
+	if ( isset( $_REQUEST['postType'] ) && 'page' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) {
+		return add_query_arg( array( 'p' => '/page/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) );
+	}
+
+	if ( isset( $_REQUEST['postType'] ) && 'wp_template' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) {
+		return add_query_arg( array( 'p' => '/template' ), remove_query_arg( 'postType' ) );
+	}
+
+	if ( isset( $_REQUEST['postType'] ) && 'wp_template' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) {
+		return add_query_arg( array( 'p' => '/wp_template/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) );
+	}
 
-if ( ! wp_is_block_theme() ) {
-	if ( ! current_theme_supports( 'block-template-parts' ) && $is_template_part_editor ) {
-		wp_die( __( 'The theme you are currently using is not compatible with the Site Editor.' ) );
-	} elseif ( ! $is_patterns_editor && ! $is_template_part_editor ) {
-		wp_die( __( 'The theme you are currently using is not compatible with the Site Editor.' ) );
+	if ( isset( $_REQUEST['postType'] ) && 'wp_block' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) {
+		return add_query_arg( array( 'p' => '/pattern' ), remove_query_arg( 'postType' ) );
+	}
+
+	if ( isset( $_REQUEST['postType'] ) && 'wp_block' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) {
+		return add_query_arg( array( 'p' => '/wp_block/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) );
+	}
+
+	if ( isset( $_REQUEST['postType'] ) && 'wp_template_part' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) {
+		return add_query_arg( array( 'p' => '/pattern' ) );
+	}
+
+	if ( isset( $_REQUEST['postType'] ) && 'wp_template_part' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) {
+		return add_query_arg( array( 'p' => '/wp_template_part/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) );
+	}
+
+	// The following redirects are for backward compatibility with the old site editor URLs.
+	if ( isset( $_REQUEST['path'] ) && '/wp_template_part/all' === $_REQUEST['path'] ) {
+		return add_query_arg(
+			array(
+				'p'        => '/pattern',
+				'postType' => 'wp_template_part',
+			),
+			remove_query_arg( 'path' )
+		);
 	}
+
+	if ( isset( $_REQUEST['path'] ) && '/page' === $_REQUEST['path'] ) {
+		return add_query_arg( array( 'p' => '/page' ), remove_query_arg( 'path' ) );
+	}
+
+	if ( isset( $_REQUEST['path'] ) && '/wp_template' === $_REQUEST['path'] ) {
+		return add_query_arg( array( 'p' => '/template' ), remove_query_arg( 'path' ) );
+	}
+
+	if ( isset( $_REQUEST['path'] ) && '/patterns' === $_REQUEST['path'] ) {
+		return add_query_arg( array( 'p' => '/pattern' ), remove_query_arg( 'path' ) );
+	}
+
+	if ( isset( $_REQUEST['path'] ) && '/navigation' === $_REQUEST['path'] ) {
+		return add_query_arg( array( 'p' => '/navigation' ), remove_query_arg( 'path' ) );
+	}
+
+	return add_query_arg( array( 'p' => '/' ) );
+}
+
+// Redirect to the site editor to the new URLs if needed.
+$redirection = _wp_get_site_editor_redirection_url();
+if ( false !== $redirection ) {
+	wp_safe_redirect( $redirection );
+	exit;
 }
 
 // Used in the HTML title tag.
@@ -56,7 +139,15 @@
 	$indexed_template_types[] = $template_type;
 }
 
-$block_editor_context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) );
+$context_settings = array( 'name' => 'core/edit-site' );
+
+if ( ! empty( $_GET['postId'] ) && is_numeric( $_GET['postId'] ) ) {
+	$context_settings['post'] = get_post( (int) $_GET['postId'] );
+} elseif ( isset( $_GET['p'] ) && preg_match( '/^\/page\/(\d+)$/', $_GET['p'], $matches ) ) {
+	$context_settings['post'] = get_post( (int) $matches[1] );
+}
+
+$block_editor_context = new WP_Block_Editor_Context( $context_settings );
 $custom_settings      = array(
 	'siteUrl'                   => site_url(),
 	'postsPerPage'              => get_option( 'posts_per_page' ),
@@ -88,16 +179,18 @@
 );
 
 $preload_paths = array(
-	array( '/wp/v2/media', 'OPTIONS' ),
+	array( rest_get_route_for_post_type_items( 'attachment' ), 'OPTIONS' ),
+	array( rest_get_route_for_post_type_items( 'page' ), 'OPTIONS' ),
 	'/wp/v2/types?context=view',
 	'/wp/v2/types/wp_template?context=edit',
-	'/wp/v2/types/wp_template-part?context=edit',
+	'/wp/v2/types/wp_template_part?context=edit',
 	'/wp/v2/templates?context=edit&per_page=-1',
 	'/wp/v2/template-parts?context=edit&per_page=-1',
 	'/wp/v2/themes?context=edit&status=active',
 	'/wp/v2/global-styles/' . $active_global_styles_id . '?context=edit',
-	'/wp/v2/global-styles/' . $active_global_styles_id,
-	'/wp/v2/global-styles/themes/' . $active_theme,
+	array( '/wp/v2/global-styles/' . $active_global_styles_id, 'OPTIONS' ),
+	'/wp/v2/global-styles/themes/' . $active_theme . '?context=view',
+	'/wp/v2/global-styles/themes/' . $active_theme . '/variations?context=view',
 	array( $navigation_rest_route, 'OPTIONS' ),
 	array(
 		add_query_arg(
@@ -114,8 +207,48 @@
 		),
 		'GET',
 	),
+	'/wp/v2/settings',
+	array( '/wp/v2/settings', 'OPTIONS' ),
+	// Used by getBlockPatternCategories in useBlockEditorSettings.
+	'/wp/v2/block-patterns/categories',
+	// @see packages/core-data/src/entities.js
+	'/?_fields=' . implode(
+		',',
+		array(
+			'description',
+			'gmt_offset',
+			'home',
+			'name',
+			'site_icon',
+			'site_icon_url',
+			'site_logo',
+			'timezone_string',
+			'url',
+			'page_for_posts',
+			'page_on_front',
+			'show_on_front',
+		)
+	),
 );
 
+if ( $block_editor_context->post ) {
+	$route_for_post = rest_get_route_for_post( $block_editor_context->post );
+	if ( $route_for_post ) {
+		$preload_paths[] = add_query_arg( 'context', 'edit', $route_for_post );
+		if ( 'page' === $block_editor_context->post->post_type ) {
+			$preload_paths[] = add_query_arg(
+				'slug',
+				// @see https://github.com/WordPress/gutenberg/blob/e093fefd041eb6cc4a4e7f67b92ab54fd75c8858/packages/core-data/src/private-selectors.ts#L244-L254
+				empty( $block_editor_context->post->post_name ) ? 'page' : 'page-' . $block_editor_context->post->post_name,
+				'/wp/v2/templates/lookup'
+			);
+		}
+	}
+} else {
+	$preload_paths[] = '/wp/v2/templates/lookup?slug=front-page';
+	$preload_paths[] = '/wp/v2/templates/lookup?slug=home';
+}
+
 block_editor_rest_api_preload( $preload_paths, $block_editor_context );
 
 wp_add_inline_script(
@@ -134,6 +267,24 @@
 	'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
 );
 
+// Preload server-registered block bindings sources.
+$registered_sources = get_all_registered_block_bindings_sources();
+if ( ! empty( $registered_sources ) ) {
+	$filtered_sources = array();
+	foreach ( $registered_sources as $source ) {
+		$filtered_sources[] = array(
+			'name'        => $source->name,
+			'label'       => $source->label,
+			'usesContext' => $source->uses_context,
+		);
+	}
+	$script = sprintf( 'for ( const source of %s ) { wp.blocks.registerBlockBindingsSource( source ); }', wp_json_encode( $filtered_sources ) );
+	wp_add_inline_script(
+		'wp-blocks',
+		$script
+	);
+}
+
 wp_add_inline_script(
 	'wp-blocks',
 	sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( isset( $editor_settings['blockCategories'] ) ? $editor_settings['blockCategories'] : array() ) ),
@@ -162,7 +313,7 @@
 <div class="edit-site" id="site-editor">
 	<?php // JavaScript is disabled. ?>
 	<div class="wrap hide-if-js site-editor-no-js">
-		<h1 class="wp-heading-inline"><?php _e( 'Edit site' ); ?></h1>
+		<h1 class="wp-heading-inline"><?php _e( 'Edit Site' ); ?></h1>
 		<?php
 		/**
 		 * Filters the message displayed in the site editor interface when JavaScript is