-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Do not use init
to register block style variations defined via theme.json
#6844
Conversation
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 Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
6ea0eca
to
3ea8e91
Compare
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
24f5103
to
f6a4382
Compare
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, @oandregal. I'm not sure I like the idea of adding side effects to the methods that are intended to return the parsed WP_Theme_JSON
objects from each origin.
I think the previous approach of having a dedicated function that has the responsibility for registering style variations is a much better design, we just need to find a better way to handle when that function is called, since the init
hook is too broad, and even then, only really because of the need to get user data.
I think adding an early return to wp_register_block_style_variations_from_theme()
if wp_installing()
returns true is probably a better initial improvement. We're previously taken this approach to avoid building variations of the template part block (see ref).
Can you provide more context about how this variation data gets used? If it's mainly for ensuring styles are enqueued, perhaps we could do this on the enqueue_block_assets
hook at priority 9, so these get registered just before wp_enqueue_registered_block_scripts_and_styles()
is called.
Agreed, this aspect isn't ideal.
This was the thinking behind the original switch to using the
This could be a good fallback option. One of the problems with leveraging Leaving the side-effect issue aside, the approach in this PR suffers the flip side of the above issue. We need to know the scenarios in which block style variations should be registered. This is much more easily reconciled and reasoned about. The happy path for the feature can easily tested.
The block style variations need to be registered to the block styles registry so that their data is not incorrectly stripped during the sanitization of theme.json data. In this context the two are fairly closely linked. This sanitization of variations was deemed important to maintain from a security perspective.
Unfortunately, there is a little more to it than that. As noted, the registration is also required for the sanitization of theme.json data not just ensuring stylesheets are enqueued. André and I have at different times in the implementation of the feature explored different actions and filters to hook onto but haven't found a workable alternative to I'd like to stress that I agree that having registration as a side effect of processing the theme.json data isn't ideal. However, I think for beta 3, we are best served to proceed with the approach in this PR. This is on a couple of grounds:
The downside other than the design involving a side effect is that we need to ensure the registration occurs everywhere needed. This can be offset by ensuring the happy path for the feature is fully functional. I believe the testing of the feature prior to the switch to using Ultimately, this PR's approach helps lower our exposure to unexpected consequences of leveraging |
On balance, I agree, I think registering block variations as a side effect of retrieving resolved theme JSON data is preferable to using |
src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php
Outdated
Show resolved
Hide resolved
My reading is that the pros of this PR's approach outnumber the current, demonstrated pros of using the Furthermore, the limitations — the introduction of side-effects to these methods — are recognized, and are also isolated to specific methods, which appears to me to be more information than thus far is known about the consequences of registering variations in the Therefore it sounds like a viable path to document these limitations and come up with a plan to iterate if folks want this feature to make it for 6.6.
Just so I understand, in the case of the Global Styles controller, is the processing of theme.json data required to generate an accurate REST transaction? |
What's really needed for an accurate REST transaction is for block style variations to be registered at the time the global styles post type is saved as well as prepared for response. Without being registered the block style variation's data will be sanitized and stripped out, creating the issue/inaccuracy. Any variations registered within the theme data, need to be registered here, in addition to the existing registration from the user origin. As the theme data can be filtered, I think it the retrieval via |
Thanks for the explainer. I was just trying to square it in my brain whether, in the case of the controller, it's really a |
I've created a matching Gutenberg PR for these changes: WordPress/gutenberg#62640 Initial smoke tests look good. I'll start giving it a thorough run and review shortly. |
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 given this a deeper test. It's all working as advertised.
After throwing several rounds of more contrived variation partials, data etc., I didn't encounter any issues.
✅ Variations register correctly and are available in both editors
✅ Customizations to variations in global styles are reflected in the editor
✅ Variation customizations are saved and applied on the frontend correctly
As discussed in this comment, we should proceed with this change. It appears we have consensus on that approach as well, with a more deliberate revisiting of init
in the near future.
Screen.Recording.2024-06-18.at.3.52.09.PM.mp4
After the latest tweaks I've given this another run. It tests well still and the comment tweaks are a nice touch. Thanks! LGTM 🚢 |
The different alternatives have pros/cons, none is perfect. Given what people has shared and the current time-constraints, I agree we should go ahead with this approach. |
Committed at https://core.trac.wordpress.org/changeset/58429 |
Trac ticket https://core.trac.wordpress.org/ticket/61451
Trac ticket https://core.trac.wordpress.org/ticket/61312
What
Register the block style variation coming from theme.json (theme's
theme.json
, user'stheme.json
, and theme.json file partials) in theWP_Theme_JSON_Resolver
.Why
The
init
hook has proven problematic, because it's also fired during installation, see slack conversation cc @swissspidy.How
The main advantage of this approach is that the block style variations are registered when the data is read, so we don't have to do any extra data reads or any more theme.json processing than we already do:
theme
&custom
origins: 5d18c8cThe disadvantage is that these block style variations are not registered everywhere, so we have to do that in the specific places that are not yet covered:
Context
Initially, this feature worked by registering the block style variations using the
wp_theme_json_data_*
filters, see https://core.trac.wordpress.org/changeset/58264. This approach proved problematic for this flow: the user updates the styles of any of them via the global styles sidebar.This feature was updated to use the
init
hook at https://core.trac.wordpress.org/changeset/58394 The rationale was that this hook is what themes currently do to register the block styles variations via thefunctions.php
(example from TwentyTwentyFour). This approach raised issues: theinit
hook is called during installation, hence the database is not set up — which this feature requires, as it retrieves the existing global styles for user data.The approach in this PR is similar to how it initially worked, but with a couple of tweaks:
wp_theme_json_data_*
filters. Instead, this registers the variations directly in theWP_Theme_JSON_Resolver
, saving the extra processing required by the filters.Test
In the
theme.json
file of the theme, paste the following understyles.blocks.variations
:In the
styles/ember.json
(theme style variation) file, paste the following understyles.blocks.variations
:Create a new file called
styles/partial.json
(partial theme.json), and paste the following:Gravacao.do.ecra.2024-06-17.as.18.12.25.mov
Commit
Commit message, to make it easy for committers: