From 9fd50db9784d9a1c1f035fd88237f6cbcb5d751a Mon Sep 17 00:00:00 2001 From: gziolo Date: Thu, 7 Apr 2022 13:35:02 +0000 Subject: [PATCH] Site Editor: Resolve homepage template on server-side Backports change from Gutenberg to support server-side home template resolution in the Site Editor. Original PR https://github.com/WordPress/gutenberg/pull/38817. Props Mamaduka. See #55505. Built from https://develop.svn.wordpress.org/trunk@53093 git-svn-id: http://core.svn.wordpress.org/trunk@52682 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/site-editor.php | 15 +++++++++++++++ wp-includes/block-template.php | 32 ++++++++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/wp-admin/site-editor.php b/wp-admin/site-editor.php index f9d2605b23..66b07d915a 100644 --- a/wp-admin/site-editor.php +++ b/wp-admin/site-editor.php @@ -23,6 +23,20 @@ if ( ! wp_is_block_theme() ) { wp_die( __( 'The theme you are currently using is not compatible with Full Site Editing.' ) ); } +/** + * Do a server-side redirection if missing `postType` and `postId` + * query args when visiting Site Editor. + */ +$home_template = _resolve_home_block_template(); +if ( $home_template && empty( $_GET['postType'] ) && empty( $_GET['postId'] ) ) { + $redirect_url = add_query_arg( + $home_template, + admin_url( 'site-editor.php' ) + ); + wp_safe_redirect( $redirect_url ); + exit; +} + // Used in the HTML title tag. $title = __( 'Editor (beta)' ); $parent_file = 'themes.php'; @@ -56,6 +70,7 @@ $custom_settings = array( 'styles' => get_block_editor_theme_styles(), 'defaultTemplateTypes' => $indexed_template_types, 'defaultTemplatePartAreas' => get_allowed_block_template_part_areas(), + '__unstableHomeTemplate' => $home_template, '__experimentalBlockPatterns' => WP_Block_Patterns_Registry::get_instance()->get_all_registered(), '__experimentalBlockPatternCategories' => WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(), ); diff --git a/wp-includes/block-template.php b/wp-includes/block-template.php index 7bea75dca4..7bd5684ed2 100644 --- a/wp-includes/block-template.php +++ b/wp-includes/block-template.php @@ -331,3 +331,35 @@ function _resolve_template_for_new_post( $wp_query ) { $wp_query->set( 'post_status', 'auto-draft' ); } } + +/** + * Returns the correct template for the site's home page. + * + * @access private + * @since 6.0.0 + * + * @return array|null A template object, or null if none could be found. + */ +function _resolve_home_block_template() { + $show_on_front = get_option( 'show_on_front' ); + $front_page_id = get_option( 'page_on_front' ); + + if ( 'page' === $show_on_front && $front_page_id ) { + return array( + 'postType' => 'page', + 'postId' => $front_page_id, + ); + } + + $hierarchy = array( 'front-page', 'home', 'index' ); + $template = resolve_block_template( 'home', $hierarchy, '' ); + + if ( ! $template ) { + return null; + } + + return array( + 'postType' => 'wp_template', + 'postId' => $template->id, + ); +} diff --git a/wp-includes/version.php b/wp-includes/version.php index 3b3626eaaf..3f6e015d2b 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.0-alpha-53092'; +$wp_version = '6.0-alpha-53093'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.