-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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 flash when clicking template name in the editor when a plugin registered template matches a default WP theme template #66359
Conversation
…emplate from the theme when 'page' post type templates are queried
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
…e to use for filtering
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tested this PR according to the provided instructions, and it appears to resolve the flickering issue.
Please see my note below.
Thanks!
lib/compat/wordpress-6.7/compat.php
Outdated
@@ -40,7 +40,7 @@ function _gutenberg_add_block_templates_from_registry( $query_result, $query, $t | |||
} | |||
|
|||
if ( ! isset( $query['wp_id'] ) ) { | |||
$template_files = _gutenberg_get_block_templates_files( $template_type, $query ); | |||
$template_files = _gutenberg_get_block_templates_files( $template_type ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the issue is related to filtering templates by post_type
, what if we try removing only the problematic post_type
key instead of the entire $query
array?
This might resolve the issue while maintaining relatively good performance, as it would remove the need to fetch all block template files.
What do you think, @Aljullu?
$template_files = _gutenberg_get_block_templates_files( $template_type ); | |
// Inline comment explaining why the $query is not used. | |
$template_files_query = $query; | |
unset( $template_files_query['post_type'] ); | |
$template_files = _gutenberg_get_block_templates_files( $template_type, $template_files_query ); |
Additionally, adding an inline comment to explain why the query array isn’t passed to the _gutenberg_get_block_templates_files()
function would make it easier to understand the code and reduce the risk of it being overwritten later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, I applied the changes in 1641d59.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to add a unit test to prevent this bug from reoccurring, but it’s not a blocker.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call, I added the tests directly to the backport PR in WordPress/wordpress-develop#7676. I'm happy to add tests in Gutenberg too, but I see there are no unit tests for this part of the codebase in Gutenberg, so I assumed it's better to write them in WordPress directly? What do you think? 🤔
Flaky tests detected in f1ec716. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11571372008
|
Thanks for the review, @anton-vlasenko! This is ready for another look, I also created the PR in WP core: WordPress/wordpress-develop#7676. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Great job!
…istered template matches a default WP theme template (WordPress#66359) * Fix _gutenberg_get_block_templates_files() so it returns the 'page' template from the theme when 'page' post type templates are queried * Try different approach: don't use query to get the templates reference to use for filtering * Only unset post_type from the query * Add backport file
What?
Fixes #65584.
Backport PR WordPress/wordpress-develop#7676.
Many themes (including TT4) have templates for specific post types (ie:
page.html
) but don't specify theirpostTypes
intheme.json
. That's because in some cases it's not needed (ie:page.html
is automatically used for pages,single.html
is automatically used for posts, etc.).That caused the templates endpoint to filter out plugin-registered templates incorrectly. If the
$query
passed to the endpoint contained apost_type
, theme templates that didn't explicitly match that post type would be ignored. So if a theme had apage.html
and a plugin registered apage
template, the one from the plugin would be used, which is incorrect.In the UI, that caused a visual glitch when clicking on the Template setting in the post/page editor.
How?
Removing
$query
from_gutenberg_get_block_templates_files()
so we get all templates, not only the ones matching the specific$post_type
. When getting all templates from the theme, we can correctly filter out any duplicate coming from a plugin.Testing Instructions
Screenshots or screencast
Enregistrament.de.pantalla.del.2024-10-23.10-13-53.webm