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

Update WP_Theme_JSON API so presets are always keyed by origin #32622

Merged
merged 4 commits into from
Jun 14, 2021

Conversation

oandregal
Copy link
Member

@oandregal oandregal commented Jun 11, 2021

This PR updates the implementation we introduced in #32358 so presets are always keyed by origin.

What's the issue

In #32358 we updated the internal shape of the presets to be keyed by origin, one of core, theme, user. While consumers (core, theme, user data) continue to use the unkeyed format via a theme.json structure, internally, we adapted it to become:

{
  "color": {
    "palette": {
      "core": [
        { "slug": "color-1", "value": "value-1" },
        { "slug": "color-2", "value": "value-2" }
      ],
      "theme": [
        { "slug": "color-3", "value": "value-3" },
        { "slug": "color-4", "value": "value-4" }
      ],
      "user": [
        { "slug": "color-5", "value": "value-5" },
        { "slug": "color-6", "value": "value-6" }
      ],
    }
  }
}

One of the unintended consequences of that PR was that we made a requirement for consumers of the API to pass the data through the merge method ― otherwise the presets won't be in the proper shape. This piece of code:

$theme_json = new WP_Theme_JSON( $some_data_here );
$theme_json->get_settings();

would yield:

{
  "color": {
    "palette": [
      { "slug": "color-1", "value": "value-2" },
      { "slug": "color-2", "value": "value-2" }
    ]
  }
}

Instead of:

{
  "color": {
    "palette": {
      "theme": [
        { "slug": "color-1", "value": "value-2" },
        { "slug": "color-2", "value": "value-2" }
      ]
    }
  }
}

What this PR changes

This PR assigns an origin in the object constructor instead and uses it to adapt the preset shape as part of the initial processing. Then, the merge algorithm is updated to simply make the origin part of the path to the setting.

How to test

I've tested a number of scenarios in the post & site editors using TwentyTwentyOne and TT1-blocks:

  • a theme that does not define any color palette => core colors are shown
  • a theme with an empty array as color palette => no colors are shown
  • a theme with a color palette => the theme colors are shown
  • add user colors via the site editor => the modifications are shown everywhere

I've also verified that the classes and custom properties for core are present in addition to theme CSS.

As with #32358 this should be tested with 5.7. In WordPress 5.8 the list of colors shown include both core and theme, but this is expected because this reverts a change we landed in 5.8 and we still need to port it to core ― once we do, it'll work as 5.7.

We need to store the presets by keys upon object construction
to make sure the in-memory shape of the structure is always correct.
@oandregal oandregal self-assigned this Jun 11, 2021
@oandregal oandregal added the Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json label Jun 11, 2021
@oandregal oandregal added the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Jun 11, 2021
Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

I'm approving but I honestly don't really know how to test this properly (the palettes seem to work with or without it)

// Internally, presets are keyed by origin.
$nodes = self::get_setting_nodes( $this->theme_json );
foreach ( $nodes as $node ) {
foreach ( self::PRESETS_METADATA as $preset ) {
Copy link
Member

Choose a reason for hiding this comment

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

Not all presets are keyed by origin, only colors, gradients, font sizes, and font families are. For example, duotone is not. I think we need to take that into account inside the look.

Copy link
Member Author

Choose a reason for hiding this comment

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

duotone is not part of PRESETS_METADATA as far as I can see?

Copy link
Contributor

Choose a reason for hiding this comment

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

should all presets be keyed? Why have special cases?

Copy link
Member

Choose a reason for hiding this comment

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

duotone is not part of PRESETS_METADATA as far as I can see?

Yes, I wrongly thought it was part of presets metadata.

should all presets be keyed? Why have special cases?

The rationale was that duotone was dynamically injected there was no need to output class variables etc. Also, duotone does not references or contains a name the direct colors are store in attributes "duotone":["#000097","#ff4747]. We don't have a need for this system there. But for consistency reasons, we may reconsider.

lib/class-wp-theme-json-gutenberg.php Show resolved Hide resolved
lib/class-wp-theme-json-gutenberg.php Outdated Show resolved Hide resolved
lib/class-wp-theme-json-gutenberg.php Outdated Show resolved Hide resolved
Copy link
Member

@jorgefilipecosta jorgefilipecosta left a comment

Choose a reason for hiding this comment

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

Thank you for this enhancement it worked well on my tests 👍

@oandregal oandregal merged commit 9b078d2 into trunk Jun 14, 2021
@oandregal oandregal deleted the update/origins-for-theme-json branch June 14, 2021 08:50
@youknowriad youknowriad removed the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Jun 14, 2021
@Mamaduka Mamaduka added this to the Gutenberg 10.9 milestone Jun 15, 2021
pento pushed a commit to WordPress/wordpress-develop that referenced this pull request Jun 15, 2021
…ta 2.

This includes:

**Various**

 - Fix multi selection for nested blocks WordPress/gutenberg#32536
 - Consistently show the drop indicator while dragging blocks WordPress/gutenberg#31896
 - Fix horizontal drop indicator WordPress/gutenberg#32589
 - Fix Safari flickering issue WordPress/gutenberg#32581
 - Silence useSelect zombie bug errors WordPress/gutenberg#32088

**Template Editor**

 - Clarify the template creation modal WordPress/gutenberg#32427
 - Only add skip links for block templates WordPress/gutenberg#32451

**Widgets Editor**

 - Add block breadcrumb WordPress/gutenberg#32498 WordPress/gutenberg#32528 WordPress/gutenberg#32569
 - Saved deleted and restored widgets. WordPress/gutenberg#32534
 - Fix unsaved changes detection WordPress/gutenberg#32573
 - Fix button spacing in the header WordPress/gutenberg#32585
 - Avoid extra undo levels WordPress/gutenberg#32572
 - Move Legacy Widget block to the `@wordpress/widgets` package WordPress/gutenberg#32501
 - Fix Social Links color inheritance WordPress/gutenberg#32625
 - Use Button appender WordPress/gutenberg#32580

**Global Styles (theme.json)**
 
 - Separate the presets per origin in the block editor settings WordPress/gutenberg#32358 WordPress/gutenberg#32622
 - Use CSS Custom Properties for the preset styles WordPress/gutenberg#32627

**Performance**

 - Remove is-typing classname to improve typing performance WordPress/gutenberg#32567

Props nosolosw, jorgefilipecosta, aristath, ntsekouras, peterwilsoncc, mcsf.
See #53397.


git-svn-id: https://develop.svn.wordpress.org/trunk@51149 602fd350-edb4-49c9-b593-d223f7449a82
nylen pushed a commit to nylen/wordpress-develop-svn that referenced this pull request Jun 15, 2021
…ta 2.

This includes:

**Various**

 - Fix multi selection for nested blocks WordPress/gutenberg#32536
 - Consistently show the drop indicator while dragging blocks WordPress/gutenberg#31896
 - Fix horizontal drop indicator WordPress/gutenberg#32589
 - Fix Safari flickering issue WordPress/gutenberg#32581
 - Silence useSelect zombie bug errors WordPress/gutenberg#32088

**Template Editor**

 - Clarify the template creation modal WordPress/gutenberg#32427
 - Only add skip links for block templates WordPress/gutenberg#32451

**Widgets Editor**

 - Add block breadcrumb WordPress/gutenberg#32498 WordPress/gutenberg#32528 WordPress/gutenberg#32569
 - Saved deleted and restored widgets. WordPress/gutenberg#32534
 - Fix unsaved changes detection WordPress/gutenberg#32573
 - Fix button spacing in the header WordPress/gutenberg#32585
 - Avoid extra undo levels WordPress/gutenberg#32572
 - Move Legacy Widget block to the `@wordpress/widgets` package WordPress/gutenberg#32501
 - Fix Social Links color inheritance WordPress/gutenberg#32625
 - Use Button appender WordPress/gutenberg#32580

**Global Styles (theme.json)**
 
 - Separate the presets per origin in the block editor settings WordPress/gutenberg#32358 WordPress/gutenberg#32622
 - Use CSS Custom Properties for the preset styles WordPress/gutenberg#32627

**Performance**

 - Remove is-typing classname to improve typing performance WordPress/gutenberg#32567

Props nosolosw, jorgefilipecosta, aristath, ntsekouras, peterwilsoncc, mcsf.
See #53397.


git-svn-id: https://develop.svn.wordpress.org/trunk@51149 602fd350-edb4-49c9-b593-d223f7449a82
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Jun 15, 2021
…ta 2.

This includes:

**Various**

 - Fix multi selection for nested blocks WordPress/gutenberg#32536
 - Consistently show the drop indicator while dragging blocks WordPress/gutenberg#31896
 - Fix horizontal drop indicator WordPress/gutenberg#32589
 - Fix Safari flickering issue WordPress/gutenberg#32581
 - Silence useSelect zombie bug errors WordPress/gutenberg#32088

**Template Editor**

 - Clarify the template creation modal WordPress/gutenberg#32427
 - Only add skip links for block templates WordPress/gutenberg#32451

**Widgets Editor**

 - Add block breadcrumb WordPress/gutenberg#32498 WordPress/gutenberg#32528 WordPress/gutenberg#32569
 - Saved deleted and restored widgets. WordPress/gutenberg#32534
 - Fix unsaved changes detection WordPress/gutenberg#32573
 - Fix button spacing in the header WordPress/gutenberg#32585
 - Avoid extra undo levels WordPress/gutenberg#32572
 - Move Legacy Widget block to the `@wordpress/widgets` package WordPress/gutenberg#32501
 - Fix Social Links color inheritance WordPress/gutenberg#32625
 - Use Button appender WordPress/gutenberg#32580

**Global Styles (theme.json)**
 
 - Separate the presets per origin in the block editor settings WordPress/gutenberg#32358 WordPress/gutenberg#32622
 - Use CSS Custom Properties for the preset styles WordPress/gutenberg#32627

**Performance**

 - Remove is-typing classname to improve typing performance WordPress/gutenberg#32567

Props nosolosw, jorgefilipecosta, aristath, ntsekouras, peterwilsoncc, mcsf.
See #53397.

Built from https://develop.svn.wordpress.org/trunk@51149


git-svn-id: http://core.svn.wordpress.org/trunk@50758 1a063a9b-81f0-0310-95a4-ce76da25c4cd
gMagicScott pushed a commit to gMagicScott/core.wordpress-mirror that referenced this pull request Jun 15, 2021
…ta 2.

This includes:

**Various**

 - Fix multi selection for nested blocks WordPress/gutenberg#32536
 - Consistently show the drop indicator while dragging blocks WordPress/gutenberg#31896
 - Fix horizontal drop indicator WordPress/gutenberg#32589
 - Fix Safari flickering issue WordPress/gutenberg#32581
 - Silence useSelect zombie bug errors WordPress/gutenberg#32088

**Template Editor**

 - Clarify the template creation modal WordPress/gutenberg#32427
 - Only add skip links for block templates WordPress/gutenberg#32451

**Widgets Editor**

 - Add block breadcrumb WordPress/gutenberg#32498 WordPress/gutenberg#32528 WordPress/gutenberg#32569
 - Saved deleted and restored widgets. WordPress/gutenberg#32534
 - Fix unsaved changes detection WordPress/gutenberg#32573
 - Fix button spacing in the header WordPress/gutenberg#32585
 - Avoid extra undo levels WordPress/gutenberg#32572
 - Move Legacy Widget block to the `@wordpress/widgets` package WordPress/gutenberg#32501
 - Fix Social Links color inheritance WordPress/gutenberg#32625
 - Use Button appender WordPress/gutenberg#32580

**Global Styles (theme.json)**
 
 - Separate the presets per origin in the block editor settings WordPress/gutenberg#32358 WordPress/gutenberg#32622
 - Use CSS Custom Properties for the preset styles WordPress/gutenberg#32627

**Performance**

 - Remove is-typing classname to improve typing performance WordPress/gutenberg#32567

Props nosolosw, jorgefilipecosta, aristath, ntsekouras, peterwilsoncc, mcsf.
See #53397.

Built from https://develop.svn.wordpress.org/trunk@51149


git-svn-id: https://core.svn.wordpress.org/trunk@50758 1a063a9b-81f0-0310-95a4-ce76da25c4cd
F-Wilke pushed a commit to FiliagoDev/WordPress that referenced this pull request Jul 31, 2021
…ta 2.

This includes:

**Various**

 - Fix multi selection for nested blocks WordPress/gutenberg#32536
 - Consistently show the drop indicator while dragging blocks WordPress/gutenberg#31896
 - Fix horizontal drop indicator WordPress/gutenberg#32589
 - Fix Safari flickering issue WordPress/gutenberg#32581
 - Silence useSelect zombie bug errors WordPress/gutenberg#32088

**Template Editor**

 - Clarify the template creation modal WordPress/gutenberg#32427
 - Only add skip links for block templates WordPress/gutenberg#32451

**Widgets Editor**

 - Add block breadcrumb WordPress/gutenberg#32498 WordPress/gutenberg#32528 WordPress/gutenberg#32569
 - Saved deleted and restored widgets. WordPress/gutenberg#32534
 - Fix unsaved changes detection WordPress/gutenberg#32573
 - Fix button spacing in the header WordPress/gutenberg#32585
 - Avoid extra undo levels WordPress/gutenberg#32572
 - Move Legacy Widget block to the `@wordpress/widgets` package WordPress/gutenberg#32501
 - Fix Social Links color inheritance WordPress/gutenberg#32625
 - Use Button appender WordPress/gutenberg#32580

**Global Styles (theme.json)**
 
 - Separate the presets per origin in the block editor settings WordPress/gutenberg#32358 WordPress/gutenberg#32622
 - Use CSS Custom Properties for the preset styles WordPress/gutenberg#32627

**Performance**

 - Remove is-typing classname to improve typing performance WordPress/gutenberg#32567

Props nosolosw, jorgefilipecosta, aristath, ntsekouras, peterwilsoncc, mcsf.
See #53397.

Built from https://develop.svn.wordpress.org/trunk@51149


git-svn-id: http://core.svn.wordpress.org/trunk@50758 1a063a9b-81f0-0310-95a4-ce76da25c4cd
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants