Skip to content

Conversation

@ramonjd
Copy link
Member

@ramonjd ramonjd commented Nov 21, 2025

Tip

This PR is largish, but intentionally so. It handles the migration from template hooks (which was buggy) and ensures the feature works only when the content experiment is activated.

What?

This PR:

These changes ensure that patterns returned in registered theme templates and template parts have pattern metadata, so that themes that use patterns automatically opt into the content only behaviour.

In Core this is done in WP_REST_Registered_Templates_Controller

Why?

Extending the class is required as parsing and re-serializing blocks disrupts the render chain, in particular it breaks shortcodes in patterns and also render_block_core/pattern hooks.

See:

Thanks to @dd32 for helping debug.

How?

When the active_templates experiment is enabled, WordPress uses a different REST controller (Gutenberg_REST_Registered_Templates_Controller) to fetch theme templates.

Registered theme patterns are returned from the /wp/v2/registered-templates endpoint.

Tests have been added to cover both scenarios (with the experiment on and off).

Testing Instructions

Check out this PR and head over to /wp-admin/admin.php?page=gutenberg-experiments to enable the contentOnly: Make patterns contentOnly by default upon insertion experiment.

Screenshot 2025-11-05 at 2 25 21 pm

I'm using TT4 to test.

Head over to /wp-admin/site-editor.php?p=%2Ftemplate

In the browser network console, you can check the response of /wp/v2/registered-templates to see if the template content contains pattern metadata (search for metadata)

Now edit the All Archives template (or any that contains a pattern).

In the list view, check that the post 3 col pattern is in content only mode, that is, that it only displays text and other blocks (not containers and so on)

Now enable the template activation experiment and test again.

Check that patterns still work by running through the test steps in #73375

Ensure that with the content only experiment OFF you cannot reproduce these test results.

Screenshots or screencast

Before After
Screenshot 2025-11-10 at 12 38 42 pm Screenshot 2025-11-10 at 12 36 33 pm

…o extend the template controllers taking into account the template activate experiment. This is required as parsing and re-serializing blocks disrupts the render chain, in particular it breaks shortcodes in patterns and also render_block_core/pattern hooks, because the wrapping pattern was not there.
…for 'gutenberg-content-only-pattern-insertion'. This change modifies the `prepare_item_for_response` method in both the block patterns and templates controllers to conditionally resolve pattern blocks based on the experiment's status. Additionally, updates to the test cases ensure the experiment is enabled during tests, improving performance and reliability.
@github-actions
Copy link

github-actions bot commented Nov 21, 2025

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ramonjd <[email protected]>
Co-authored-by: andrewserong <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@ramonjd ramonjd self-assigned this Nov 21, 2025
@ramonjd ramonjd added [Type] Bug An existing feature does not function as intended Backwards Compatibility Issues or PRs that impact backwards compatability [Feature] Patterns A collection of blocks that can be synced (previously reusable blocks) or unsynced labels Nov 21, 2025
@@ -0,0 +1,355 @@
<?php
Copy link
Member Author

Choose a reason for hiding this comment

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

Test overkill? Maybe.

But given the experiment ping pong and the class juggling with template activation, best to be safe.

…nsistently resolve pattern blocks using `gutenberg_resolve_pattern_blocks`. Removed conditional checks for the 'gutenberg-content-only-pattern-insertion' experiment in the `prepare_item_for_response` methods. Updated related test cases to remove experiment assertions, enhancing test clarity and performance.
@github-actions
Copy link

github-actions bot commented Nov 23, 2025

Flaky tests detected in 51279fa.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/19723850485
📝 Reported issues:

Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

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

Looking pretty good so far! I haven't tested manually yet, but I noticed an issue in the rebase where the class-gutenberg-rest-static-templates-controller.php file still exists instead of being renamed. I've left a comment about whether we should retain that name for now, or include the renaming in this PR.

I like the inclusion of the tests, as you mention, while they're a little verbose, good to include some coverage given all the back-and-forth!

- Replaced the `Gutenberg_REST_Registered_Templates_Controller` with `Gutenberg_REST_Static_Templates_Controller` in the load file.
- Deleted the `class-gutenberg-rest-registered-templates-controller.php` file.
- Updated the `class-gutenberg-rest-static-templates-controller.php` to extend the correct base class.
- Made minor adjustments in the `class-gutenberg-rest-templates-controller-7-0.php` for clarity in the code comments.
- Adjusted the template activation process to reflect the new controller structure.
… 7.0

- Added a note indicating that there are no changes in this class that need to be backported to core.
- Minor formatting adjustment in the code for improved readability.
- Added registration for the Registered Templates Parts REST API routes.
- Updated the `gutenberg_modify_wp_template_post_type_args_7_0` function to simplify its parameters.
- Introduced a new function `gutenberg_modify_wp_template_part_post_type_args_7_0` for handling template part registration.
- Updated PHPUnit tests to verify pattern resolution in template parts via the REST API.
Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

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

Thanks for all the back and forth on this one, this looks good to go to me! Smoke tested to ensure that the contentOnly experiment is still working nicely with patterns contained in template parts (i.e. footer in TT4), and included in templates (e.g. archive in TT4). Toggling the experiment on and off is working as expected.

LGTM! 🚀

@ramonjd
Copy link
Member Author

ramonjd commented Nov 27, 2025

Thank you for helping get this one across the line @andrewserong

Hopefully we won't break wordpress.org 😄 (I tested this against the https://github.com/WordPress/wporg-parent-2021/tree/trunk branch and it looks okay)

@ramonjd ramonjd merged commit 75c0cb1 into trunk Nov 27, 2025
38 of 39 checks passed
@ramonjd ramonjd deleted the update/pattern-resolution-in-templates branch November 27, 2025 21:50
@github-actions github-actions bot added this to the Gutenberg 22.3 milestone Nov 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Backwards Compatibility Issues or PRs that impact backwards compatability [Feature] Patterns A collection of blocks that can be synced (previously reusable blocks) or unsynced [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants