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

Section Styles: Switch away from using init for variation registration #62640

Merged

Conversation

aaronrobertshaw
Copy link
Contributor

What?

Switches away from using init action to register block style variations.

This PR is a backport to Gutenberg of changes proposed in WordPress/wordpress-develop#6844.

Why?

The init hook has proven problematic, because it's also fired during installation, see slack conversation

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:

The 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 the functions.php (example from TwentyTwentyFour). This approach raised issues: the init 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:

  • It doesn't use the wp_theme_json_data_* filters. Instead, this registers the variations directly in the WP_Theme_JSON_Resolver, saving the extra processing required by the filters.
  • It also registers the block style variations in the global styles endpoint, so user updates are reflected immediately in the UI without requiring a page reload.

Testing Instructions

Test

  1. Register style variations in all the places the theme is allowed to (use TwentyTwentyFour, aka TT4).

In the theme.json file of the theme, paste the following under styles.blocks.variations:

"theme": {
   "title": "Theme",
    "blockTypes": [ "core/group" ],
    "color": {
            "background": "crimson"
    }
}

In the styles/ember.json (theme style variation) file, paste the following under styles.blocks.variations:

"theme": {
   "title": "Theme",
    "blockTypes": [ "core/group" ],
    "color": {
            "background": "darkseagreen"
    }
}

Create a new file called styles/partial.json (partial theme.json), and paste the following:

{
        "$schema": "https://schemas.wp.org/trunk/theme.json",
        "version": 2,
        "title": "Partial",
        "blockTypes": [ "core/group" ],
        "styles": {
                "color": {
                        "background": "deepskyblue"
                }
        }
}
  1. Go to the post editor, add two group blocks. Verify the "Theme" and "Partial" variations are present:
Captura de ecrã 2024-06-17, às 18 07 53
  1. Apply them to each of the blocks and verify colors are reflected properly everywhere (editor & front):
Captura de ecrã 2024-06-17, às 18 08 27
  1. Go to the site editor and apply the "Ember" style variation. The expected result is that the block with the "Theme" block variation has now the "darkseagreen" background color:
Captura de ecrã 2024-06-17, às 18 11 13
  1. Verify that user changes to the block style variations defined are reflected immediately in the site editor:
Gravacao.do.ecra.2024-06-17.as.18.12.25.mov
@aaronrobertshaw aaronrobertshaw added [Type] Bug An existing feature does not function as intended Backport to WP 6.6 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json labels Jun 18, 2024
@aaronrobertshaw aaronrobertshaw self-assigned this Jun 18, 2024
Copy link

github-actions bot commented Jun 18, 2024

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: aaronrobertshaw <aaronrobertshaw@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>

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

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.

This is testing nicely for me, and like I mentioned on the core PR, I think it makes good sense to move this logic to be a side effect of resolving the data.

✅ Theme and Partials are working nicely in the post editor and site frontend
✅ Style variation applies correctly in the site editor and saves and is output by user global styles correctly

LGTM! 🚀

@ellatrix
Copy link
Member

Good to merge? I'm cherry-picking for Beta 3 in a bit.

@aaronrobertshaw aaronrobertshaw merged commit 880cf3e into trunk Jun 18, 2024
65 checks passed
@aaronrobertshaw aaronrobertshaw deleted the update/switch-away-from-init-for-variation-registration branch June 18, 2024 06:26
@github-actions github-actions bot added this to the Gutenberg 18.7 milestone Jun 18, 2024
@aaronrobertshaw
Copy link
Contributor Author

Good to merge? I'm cherry-picking for Beta 3 in a bit.

✅ Done.

Thanks Ella. Just confirmed the core PR should be committed shortly as well.

@ellatrix
Copy link
Member

Oh sorry, this is only PHP backports right?

@ellatrix ellatrix removed the Backport to WP 6.6 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Bug An existing feature does not function as intended
3 participants