Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FSE: Verify if php template exists for a hybrid/universal theme with a block-based parent #31123

Merged
merged 7 commits into from
Apr 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/full-site-editing/template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function gutenberg_add_template_loader_filters() {
add_filter( str_replace( '-', '', $template_type ) . '_template', 'gutenberg_override_query_template', 20, 3 );
}
}

add_action( 'wp_loaded', 'gutenberg_add_template_loader_filters' );

/**
Expand Down Expand Up @@ -69,6 +70,19 @@ function gutenberg_override_query_template( $template, $type, array $templates =
$current_template_slug = basename( $template, '.php' );
$current_block_template_slug = is_object( $current_template ) ? $current_template->slug : false;
foreach ( $templates as $template_item ) {

// if the theme is a child theme we want to check if a php template exists
// and that a corresponding block template from the theme and not the parent doesn't exist.
$has_php_template = file_exists( get_stylesheet_directory() . '/' . $type . '.php' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a better option than using $template?

Copy link
Contributor Author

@MaggieCabrera MaggieCabrera Apr 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$template will pull from the parent if the child doesn't have a template defined for a specific page since it uses locate_template(). With get_stylesheet_directory() we will be sure the path is not from the parent.

$has_block_template = false;
$block_template = _gutenberg_get_template_file( 'wp_template', $type );
if ( null !== $block_template && wp_get_theme()->get_stylesheet() === $block_template['theme'] ) {
$has_block_template = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if lines 77-81 would be better encapsulated in a separate function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about but I didn't do it because I thought it was very unlikely that such a function would be reused since it would be very specific. And the rest of the gutenberg_override_query_template function is using similar checks so I thought this way was a bit more consistent. I'm happy to do it but I'm not sure what we gain from doing so.

}
if ( is_child_theme() && ( $has_php_template && ! $has_block_template ) ) {
return $template;
}

$template_item_slug = gutenberg_strip_php_suffix( $template_item );

// Break the loop if the block-template matches the template slug.
Expand Down Expand Up @@ -129,7 +143,7 @@ function gutenberg_override_query_template( $template, $type, array $templates =
}

/**
* Return the correct 'wp_template' to render fot the request template type.
* Return the correct 'wp_template' to render for the request template type.
*
* Accepts an optional $template_hierarchy argument as a hint.
*
Expand Down