Skip to content

Commit e3cf5c4

Browse files
committed
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 WordPress/gutenberg#38817. Props Mamaduka. See #55505. git-svn-id: https://develop.svn.wordpress.org/trunk@53093 602fd350-edb4-49c9-b593-d223f7449a82
1 parent e34d775 commit e3cf5c4

3 files changed

Lines changed: 85 additions & 0 deletions

File tree

src/wp-admin/site-editor.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@
2323
wp_die( __( 'The theme you are currently using is not compatible with Full Site Editing.' ) );
2424
}
2525

26+
/**
27+
* Do a server-side redirection if missing `postType` and `postId`
28+
* query args when visiting Site Editor.
29+
*/
30+
$home_template = _resolve_home_block_template();
31+
if ( $home_template && empty( $_GET['postType'] ) && empty( $_GET['postId'] ) ) {
32+
$redirect_url = add_query_arg(
33+
$home_template,
34+
admin_url( 'site-editor.php' )
35+
);
36+
wp_safe_redirect( $redirect_url );
37+
exit;
38+
}
39+
2640
// Used in the HTML title tag.
2741
$title = __( 'Editor (beta)' );
2842
$parent_file = 'themes.php';
@@ -56,6 +70,7 @@ static function( $classes ) {
5670
'styles' => get_block_editor_theme_styles(),
5771
'defaultTemplateTypes' => $indexed_template_types,
5872
'defaultTemplatePartAreas' => get_allowed_block_template_part_areas(),
73+
'__unstableHomeTemplate' => $home_template,
5974
'__experimentalBlockPatterns' => WP_Block_Patterns_Registry::get_instance()->get_all_registered(),
6075
'__experimentalBlockPatternCategories' => WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(),
6176
);

src/wp-includes/block-template.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,35 @@ function _resolve_template_for_new_post( $wp_query ) {
331331
$wp_query->set( 'post_status', 'auto-draft' );
332332
}
333333
}
334+
335+
/**
336+
* Returns the correct template for the site's home page.
337+
*
338+
* @access private
339+
* @since 6.0.0
340+
*
341+
* @return array|null A template object, or null if none could be found.
342+
*/
343+
function _resolve_home_block_template() {
344+
$show_on_front = get_option( 'show_on_front' );
345+
$front_page_id = get_option( 'page_on_front' );
346+
347+
if ( 'page' === $show_on_front && $front_page_id ) {
348+
return array(
349+
'postType' => 'page',
350+
'postId' => $front_page_id,
351+
);
352+
}
353+
354+
$hierarchy = array( 'front-page', 'home', 'index' );
355+
$template = resolve_block_template( 'home', $hierarchy, '' );
356+
357+
if ( ! $template ) {
358+
return null;
359+
}
360+
361+
return array(
362+
'postType' => 'wp_template',
363+
'postId' => $template->id,
364+
);
365+
}

tests/phpunit/tests/block-template.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,42 @@ public function test_template_remains_unchanged_if_templates_array_is_empty() {
188188
$resolved_template_path = locate_block_template( '', 'search', array() );
189189
$this->assertSame( '', $resolved_template_path );
190190
}
191+
192+
/**
193+
* Covers: https://github.com/WordPress/gutenberg/pull/38817.
194+
*
195+
* @ticket 55505
196+
*/
197+
public function test_resolve_home_block_template_default_hierarchy() {
198+
$template = _resolve_home_block_template();
199+
200+
$this->assertSame( 'wp_template', $template['postType'] );
201+
$this->assertSame( get_stylesheet() . '//index', $template['postId'] );
202+
}
203+
204+
/**
205+
* @ticket 55505
206+
*/
207+
public function test_resolve_home_block_template_static_homepage() {
208+
$post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
209+
update_option( 'show_on_front', 'page' );
210+
update_option( 'page_on_front', $post_id );
211+
212+
$template = _resolve_home_block_template();
213+
214+
$this->assertSame( 'page', $template['postType'] );
215+
$this->assertSame( $post_id, $template['postId'] );
216+
217+
delete_option( 'show_on_front', 'page' );
218+
}
219+
220+
/**
221+
* @ticket 55505
222+
*/
223+
public function test_resolve_home_block_template_no_resolution() {
224+
switch_theme( 'stylesheetonly' );
225+
$template = _resolve_home_block_template();
226+
227+
$this->assertNull( $template );
228+
}
191229
}

0 commit comments

Comments
 (0)