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

Fix template resolution to give precedence to child theme PHP templates over parent theme block templates with equal specificity (alternative approach) #1985

Closed
wants to merge 5 commits into from
Closed

Fix template resolution to give precedence to child theme PHP templates over parent theme block templates with equal specificity (alternative approach) #1985

wants to merge 5 commits into from

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Nov 30, 2021

Alternative approach to #1961. For a general idea of the challenges that a solution to this issue faces, and some design choices, see the code comments in the diff.

Make sure to smoke test with an FSE theme.

Description of the issue (as found in the corresponding Trac ticket) below:


When a theme has a PHP template of a certain specificity (e.g. page-home.php), and it happens to be a child theme of another theme which has a block template for the same specificity (e.g. page-home.html), WordPress will currently pick the parent theme’s block template over the child theme's PHP template to render the page.

This is a regression. If the PHP and block template have equal specificity, the child theme's template should be used. This issue was fixed before in WordPress/gutenberg#31123. The relevant logic has since been modified and eventually backported to Core , so the fix now needs to happen in Core (plus GB's pre-WP-5.9 compat layer, but that's somewhat secondary).

We have a unit test for this (but obviously had to disable it). The unit test existed in Gutenberg before but didn’t fail there due to a faulty test setup. In fact, the issue was only found while backporting the unit test.

Trac ticket: https://core.trac.wordpress.org/ticket/54515


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@ockham
Copy link
Contributor Author

ockham commented Dec 2, 2021

cc/ @WordPress/gutenberg-core for review 🙂

// Is our candidate block template's slug identical to our PHP fallback template's?
if (
count( $templates ) &&
$fallback_template_slug === $templates[0]->slug &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible that the fallback is actually different than the block template slug but has more priority. Ex: single.php, index.html. Which one would win here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it possible that the fallback is actually different than the block template slug but has more priority.

No, we prevent that by truncating the list of candidate templates that we pass from locate_block_template() to resolve_block_template() to only include equal or higher specificity than the PHP fallback template:

if ( $template ) {
/*
* locate_template() has found a PHP template at the path specified by $template.
* That means that we have a fallback candidate if we cannot find a block template
* with higher specificity.
*
* Thus, before looking for matching block themes, we shorten our list of candidate
* templates accordingly.
*/
// Locate the index of $template (without the theme directory path) in $templates.
$relative_template_path = str_replace(
array( get_stylesheet_directory() . '/', get_template_directory() . '/' ),
'',
$template
);
$index = array_search( $relative_template_path, $templates, true );
// If the template hiearchy algorithm has successfully located a PHP template file,
// we will only consider block templates with higher or equal specificity.
$templates = array_slice( $templates, 0, $index + 1 );
}
$block_template = resolve_block_template( $type, $templates );

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok I guess this looks good to me then.

@ockham
Copy link
Contributor Author

ockham commented Dec 2, 2021

Back-backport to GB: WordPress/gutenberg#37074

@audrasjb
Copy link
Contributor

audrasjb commented Dec 2, 2021

Committed in https://core.trac.wordpress.org/changeset/52308

@audrasjb audrasjb closed this Dec 2, 2021
@ockham ockham deleted the fix/template-hierarchy-algorithm-child-theme-php-templates-alternative branch December 3, 2021 11:00
ockham added a commit to WordPress/gutenberg that referenced this pull request Dec 6, 2021
…mplates over parent theme block templates with equal specificity (#37074)

Back-backport of WordPress/wordpress-develop#1985. See there for more background and details.
noisysocks pushed a commit to WordPress/gutenberg that referenced this pull request Dec 13, 2021
…mplates over parent theme block templates with equal specificity (#37074)

Back-backport of WordPress/wordpress-develop#1985. See there for more background and details.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants