Skip to content

Commit

Permalink
Edit Site: Fix template lookup (#22954)
Browse files Browse the repository at this point in the history
## Description
This PR fixes two issues with template lookup:

### 1. Templates with `index.html` block template fallback

When using a theme that came with an `index.html` but not with a `front-page.html` block template, the site editor would show the following `Notice: Undefined index: front-page in /var/www/html/wp-content/plugins/gutenberg/lib/edit-site-page.php on line 162`.
 
The reason is that I was setting the wrong key here: https://github.com/WordPress/gutenberg/blob/710373b254fbcd15d524afdeb31da0d93c4defd4/lib/edit-site-page.php#L158

This would use the name of the template that was _found_, not the name of the key that was looked for. So if we're looking up `front-page`, but don't find any template, and [thus fall](https://github.com/WordPress/gutenberg/blob/710373b254fbcd15d524afdeb31da0d93c4defd4/lib/template-loader.php#L226) back to `index`, the template will be stored under the `index` key, rather than the front-end one. 

The fix for this is to use the template we were looking for as key (rather than the name of the template we actually found).

### 2. Templates without `index.html` block template fallback

When using a theme that doesn't come with any block templates at all, we need to provide a blank `index` template when the user first opens the site editor (see [discussion](#22893 (comment))).

Co-authored-by: Enrique Piqueras <epiqueras@users.noreply.github.com>
  • Loading branch information
2 people authored and oandregal committed Jun 9, 2020
1 parent aa066b4 commit fe867fc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/edit-site-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ function gutenberg_edit_site_init( $hook ) {

$current_template = gutenberg_find_template_post_and_parts( $template_type );
if ( isset( $current_template ) ) {
$template_ids[ $current_template['template_post']->post_name ] = $current_template['template_post']->ID;
$template_part_ids = $template_part_ids + $current_template['template_part_ids'];
$template_ids[ $template_type ] = $current_template['template_post']->ID;
$template_part_ids = $template_part_ids + $current_template['template_part_ids'];
}
}

Expand Down
15 changes: 15 additions & 0 deletions lib/template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,21 @@ function gutenberg_find_template_post_and_parts( $template_type, $template_hiera
}
}

// If we haven't found any template post by here, it means that this theme doesn't even come with a fallback
// `index.html` block template. We create one so that people that are trying to access the editor are greeted
// with a blank page rather than an error.
if ( ! $current_template_post && ( is_admin() || defined( 'REST_REQUEST' ) ) ) {
$current_template_post = array(
'post_title' => 'index',
'post_status' => 'auto-draft',
'post_type' => 'wp_template',
'post_name' => 'index',
);
$current_template_post = get_post(
wp_insert_post( $current_template_post )
);
}

if ( $current_template_post ) {
$template_part_ids = array();
if ( is_admin() ) {
Expand Down

0 comments on commit fe867fc

Please sign in to comment.