-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Templates API: Fix template files query by post-type #61244
Conversation
b6ba41c
to
bd46b25
Compare
// The custom templates with no associated post-types are available for all post-types. | ||
if ( $post_type && ! isset( $candidate['postTypes'] ) && $is_custom ) { | ||
$template_files[ $template_slug ] = $candidate; | ||
} |
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.
This is the actual fix. Everything else is just a boilerplate we have to use to override the core.
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 think it would be helpful to mark the start and end of the new code being inserted with a @core-merge
comment or similar, so we can see what's being added without manually running a diff.
(I did look for a way to override these functions with less duplicated code, but didn't see one.)
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.
That's a good point; I totally forgot about those.
Yeah, couldn't come up with a better override for this case :/
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. |
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.
Thanks for this fix!
Following the replication instructions from #61031 (comment), this fixes the issue for me. Block templates that aren't declared in theme.json are available to both posts and pages for swapping templates.
I left some suggestions for consolidating and commenting to more easily see what code is actually different compared to the equivalent core functions. But overall I think this approach makes sense and can be backported for WP 6.5.3, so I'll go ahead and approve.
$default_template_types = array(); | ||
if ( 'wp_template' === $template_type ) { | ||
$default_template_types = get_default_block_template_types(); | ||
} |
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 looks like $default_template_types
is being used only by the new code below and is only needed when 'wp_template' === $template_type
.
Perhaps move $default_template_types
into the existing if ( 'wp_template_part' === $template_type ) {
conditional block, right above $is_custom
to consolidate the fix?
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 tried to avoid calling the function in a loop. Its values can be retrieved at the top level.
// The custom templates with no associated post-types are available for all post-types. | ||
if ( $post_type && ! isset( $candidate['postTypes'] ) && $is_custom ) { | ||
$template_files[ $template_slug ] = $candidate; | ||
} |
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 think it would be helpful to mark the start and end of the new code being inserted with a @core-merge
comment or similar, so we can see what's being added without manually running a diff.
(I did look for a way to override these functions with less duplicated code, but didn't see one.)
* over the theme-provided ones, so we skip querying and building them. | ||
*/ | ||
$query['slug__not_in'] = wp_list_pluck( $query_result, 'slug' ); | ||
$template_files = _gutenberg_get_block_templates_files( $template_type, $query ); |
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.
Same as above, I think we should mark what's being changed with a @core-merge
comment or similar.
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.
There is no need here. I had to duplicate the whole gutenberg_get_block_templates
just for this line override :/
855e996
to
60f8453
Compare
PR now includes PHPUnit tests. It modifies the existing |
Trac ticket: https://core.trac.wordpress.org/ticket/61110 |
Thanks for having a look, @oandregal! Do you have any ideas about what the bottleneck could be here?
The WP 6.5.3 RC is scheduled for today, so we'll have to make a call if this fix will be included in it. Update: The effect in the core is smaller. Screenshot |
I've looked into the "Swap templates" behavior. According to #51477 the button should be present if:
Is this correct? Based on the feedback at #61031 (comment) the "Swap templates" button wasn't available when the theme didn't register any custom template. Register as in: declare After this PR, block themes that don't register custom templates via the I understand how it's confusing that there's no "Swap button" but the custom templates are still displayed in the site editor. In my view, unregistered custom templates shouldn't be listed in the site editor. This is the core issue we should clarify: how a block theme register custom templates? What should happen when a theme doesn't register the custom templates via I haven't been involved in templates work, so asking for feedback on this before jumping into implementation/code review. @ntsekouras @youknowriad @ockham @Mamaduka @creativecoder @carolinan |
@oandregal From my perspective the problem here is that the behavior changed in 6.5. Prior to that the With 6.5 they now only seem to show up if they are defined in |
@fabiankaegy so this PR fixes that, correct? |
@ntsekouras Yes it does. I understood the comment from @oandregal as questioning whether this was indeed a bug or whether this was the intended behavior |
@ntsekouras, this PR restores behavior from pre-6.5 versions. IIRC, the P.S. Behavior is similar to how classic themes handled it. When the |
Some thread in slack Summarizing it a bit: Thanks to mamaduka, I learned more about this. Before the "swap button" PR, users were able to do this, just using a list not a preview. So, we need to fix this user-facing issue. The fix in this PRIn addition to update the visualization, the Swap button PR switched which templates were shown: before, we merged data from different sources instead of taking it only from available templates. The swap button now only uses Before this PR, the response was a empty array: Gravacao.do.ecra.2024-05-02.as.11.51.38.movAfter this PR, the response also returns the custom templates that don't have any post type attached: Gravacao.do.ecra.2024-05-02.as.11.53.31.movI mentioned in slack that this is unexpected to me. I also mentioned that I lack much context about this feature, so I welcome more feedback from people that actually worked on this. If this the proper fix (endpoint providing all the custom templates that don't declare a post type?), I can look at the implementation to see if there's any chance to improve the performance regression. Alternative fix?I wonder if there's an alternative fix where we do at the client-level what we did before the swap button PR: take data from available templates (the templates that are registered for a post type) + take other custom templates that are postType unbound from elsewhere. |
I think we need to decide if adding a cc @WordPress/theme-team |
Exactly this. If we want to make that change that fine. But it needs to be opt in. Like for example behind a From my perspective what's in 6.5 today is broken compared to earlier version and we should ensure that we ship this fix |
Actually, you've convinced me about this already :) I don't think it's a requirement: this provides metadata information about the custom templates. Even if we wanted, this ship already sailed. The question for me is how to fix this for users. I see two alternatives: either make the |
Restoring behavior pre-changelog 55687 made the most sense when investigating the issue. Why?
|
It's good to see that the performance impact of this fix is less/minimal in the wordpress-develop PR. But I worry that the comparatively worse Gutenberg performance results could become the core results after all of the PHP and JS package changes in Gutenberg trunk land in core trunk. I don't have a lot of experience monitoring the performance tests, is that a reasonable fear? Personally I'm leaning toward punting this fix from the 6.5.3 release until we have more time to investigate and continue discussing alternatives. |
Personally, that's fine with me as well since there's not enough time to gather more feedback. Re-performance tests: I don't think " trunk " shows the real picture now. The code there runs with regression. If the original change accounted for the scenario we're trying to fix here, the original perf results would have been different. |
Thanks for the discussion. For me, the former is more logical. if there's no "post type" for a template, it just means "all post types" and the endpoint should reflect that. (there might be other places where we want the templates for a given post type, and we shouldn't have to add logic any time we want that). Now, If I understand properly, this has some frontend performance impact. I'd like to understand how much are we talking? Is there a way (I mean some code/caching) to avoid the regression? |
I'm AFK today, but as I shared before, the performance effect shouldn't be a concern here. Why?
|
See WordPress/gutenberg#61244. See #6468. Fixes #61110. Props mamaduka, mukesh27, grantmkin, vcanales, ellatrix, oandregal. git-svn-id: https://develop.svn.wordpress.org/trunk@58323 602fd350-edb4-49c9-b593-d223f7449a82
See WordPress/gutenberg#61244. See WordPress/wordpress-develop#6468. Fixes #61110. Props mamaduka, mukesh27, grantmkin, vcanales, ellatrix, oandregal. Built from https://develop.svn.wordpress.org/trunk@58323 git-svn-id: http://core.svn.wordpress.org/trunk@57780 1a063a9b-81f0-0310-95a4-ce76da25c4cd
See WordPress/gutenberg#61244. See WordPress/wordpress-develop#6468. Fixes #61110. Props mamaduka, mukesh27, grantmkin, vcanales, ellatrix, oandregal. Built from https://develop.svn.wordpress.org/trunk@58323 git-svn-id: https://core.svn.wordpress.org/trunk@57780 1a063a9b-81f0-0310-95a4-ce76da25c4cd
What?
Fixes #61031.
PR fixes the "block template" files query by post-type when a theme doesn't provide
customTemplates
mapping in the theme.json`The PHP side regression was introduced in 55687 changeset but became more evident after recent editor updates. See #61031 (comment).
Why?
The custom templates with no associated post-types should be available for all post-types.
How?
Update
_get_block_templates_files
to match the original logic. The actual fix is just a few lines, but I had to copy multiple functions to allow overriding core.Testing Instructions
customTemplates
setting.customTemplates
setting are visible.Testing Instructions for Keyboard
Same.
Screenshots or screencast
Templates with
theme.json
settingTemplates without
theme.json
setting