Skip to content

Commit

Permalink
Site Editor: Resolve homepage template on server-side
Browse files Browse the repository at this point in the history
Backports change from Gutenberg to support server-side home template resolution in the Site Editor. Original PR WordPress/gutenberg#38817.

Props Mamaduka.
See #55505.




git-svn-id: https://develop.svn.wordpress.org/trunk@53093 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
gziolo committed Apr 7, 2022
1 parent e34d775 commit e3cf5c4
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/wp-admin/site-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@
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';
Expand Down Expand Up @@ -56,6 +70,7 @@ static function( $classes ) {
'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(),
);
Expand Down
32 changes: 32 additions & 0 deletions src/wp-includes/block-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
38 changes: 38 additions & 0 deletions tests/phpunit/tests/block-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,42 @@ public function test_template_remains_unchanged_if_templates_array_is_empty() {
$resolved_template_path = locate_block_template( '', 'search', array() );
$this->assertSame( '', $resolved_template_path );
}

/**
* Covers: https://github.com/WordPress/gutenberg/pull/38817.
*
* @ticket 55505
*/
public function test_resolve_home_block_template_default_hierarchy() {
$template = _resolve_home_block_template();

$this->assertSame( 'wp_template', $template['postType'] );
$this->assertSame( get_stylesheet() . '//index', $template['postId'] );
}

/**
* @ticket 55505
*/
public function test_resolve_home_block_template_static_homepage() {
$post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', $post_id );

$template = _resolve_home_block_template();

$this->assertSame( 'page', $template['postType'] );
$this->assertSame( $post_id, $template['postId'] );

delete_option( 'show_on_front', 'page' );
}

/**
* @ticket 55505
*/
public function test_resolve_home_block_template_no_resolution() {
switch_theme( 'stylesheetonly' );
$template = _resolve_home_block_template();

$this->assertNull( $template );
}
}

0 comments on commit e3cf5c4

Please sign in to comment.