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 #1961

Closed
wants to merge 12 commits into from
Closed

Fix template resolution to give precedence to child theme PHP templates over parent theme block templates with equal specificity #1961

wants to merge 12 commits into from

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Nov 27, 2021

WIP. 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 Nov 27, 2021

My current approach would change the semantics of _get_block_templates_files() which is already used by GB, so we'd need to figure something out there. (Add under a new name or whatnot.) I'll try to get this fix into shape first and then think about the details of backward compat.

@ockham ockham changed the title Fix/template hierarchy algorithm child theme php templates Fix template hierarchy algorithm to give precedence to child theme PHP templates over parent theme block templates with equal specificity Nov 29, 2021
@ockham ockham changed the title Fix template hierarchy algorithm to give precedence to child theme PHP templates over parent theme block templates with equal specificity Fix template resolution to give precedence to child theme PHP templates over parent theme block templates with equal specificity Nov 29, 2021
@ockham
Copy link
Contributor Author

ockham commented Nov 29, 2021

Potential TODO: Merge the logic from locate_block_template into the corresponding logic that we now have in get_block_templates().

* }
* @param array $template_type wp_template or wp_template_part.
*
* @return array Templates.
*/
function get_block_templates( $query = array(), $template_type = 'wp_template' ) {
function get_block_templates( $query = array(), $template_type = 'wp_template', $child_theme_fallback_php_template = null ) {
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 don't like how I had to change the function signature of get_block_templates() -- which is, after all, supposed to be a general-purpose function to get all templates for a given theme (DB or file-based).

I'll try a different approach (where the filtering will hopefully happen in a later stage, i.e. in resolve_block_template -- even though that'll require some redundant checks) in a separate PR.

@ockham
Copy link
Contributor Author

ockham commented Nov 30, 2021

Closing in favor of #1985.

@ockham ockham closed this Nov 30, 2021
@ockham ockham deleted the fix/template-hierarchy-algorithm-child-theme-php-templates branch November 30, 2021 20:20
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.

1 participant