Skip to content

Conversation

@oandregal
Copy link
Member

@oandregal oandregal commented Jun 6, 2023

Trac ticket https://core.trac.wordpress.org/ticket/58462
Backports WordPress/gutenberg#49807

What?

This PR makes sure unregistered block style variations are ignored before processing any data coming from theme.json.

Why?

  • The current state prevents registered style variations from working in the editor when an unregistered style variation is found in a theme.json dataset.
  • It also enqueues CSS rules to the front-end that don't have a selector, so they are invalid.

How?

By making sure the style variation is not added to the allowed list, unless it's registered for that particular block.

Testing Instructions

  • Make sure your environment logs the errors to the screen by setting WP_DEBUG to true.
  • Add the following two variations to the theme.json of TwentyTwentyThree:
{
  "styles": {
    "blocks": {
      "core/quote": {
        "variations": {
          "plain": { "color": { "background": "indigo", "text": "gold" } },
          "unregistered": { "color": { "background": "red", "text": "blue" } }
        } 
      }
    }
  }
}
  • Create a post that contains a quote and style it with the plain variation.

The expected result is that:

  • The styles are applied to quote blocks with plain style variation in editors and front.
  • The wp-block-quote-inline-css stylesheet in the front-end doesn't have a style rule such as { background-color: red; color: blue} (note the lack of CSS selector).
  • There is no error reported in class-wp-theme-json.php for the editors or front. In trunk, you'll see something like this:

image

Verify that upon updating the colors of the style variation via the global styles sidebar (blocks > quote > style variation plain), the user-provided colors are rendered everywhere (front & editors).

Commit message

Ignore unregistered block style variations from `theme.json`.

This PR makes sure unregistered block style variations declared via `theme.json` are ignored. It fixes an issue by style variations don't work in the editor and CSS rules without a selector are output to the front-end.

Props isabel_brison.
Fixes #58462.

Copy link
Contributor

@tellthemachines tellthemachines left a comment

Choose a reason for hiding this comment

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

Code changes look good (they match the original PR)

Testing in the post editor with the provided variations added to TT3 I can confirm the styles don't load in trunk and a few "undefined index" warnings pop up. With this branch, there are no warnings and the styles are loaded correctly.

I wasn't able to test on the site editor because it's not working on my local dev env; the page is unresponsive and there's a console error Uncaught TypeError: t.toString is not a function at e.stringifyFunction (<anonymous>:1:4066)
I'll give it a go again tomorrow or perhaps someone else can help test? Unless this is an actual problem with trunk and not just my local env acting up 😅

@oandregal
Copy link
Member Author

@tellthemachines would you be able to test the site editor again and report back? It works fine for me.

Copy link
Contributor

@tellthemachines tellthemachines 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 the PR @oandregal !

I've retested and can confirm that everything is working correctly in the site editor - with the provided theme.json code sample, plain quote styles display correctly and there are no PHP warnings output.

@oandregal
Copy link
Member Author

@oandregal oandregal closed this Jun 14, 2023
@oandregal oandregal deleted the update/theme-json-unregistered-style-variations branch June 14, 2023 07:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects

Development

Successfully merging this pull request may close these issues.

2 participants