Skip to content

Add: Frontend section presets output#42124

Merged
jorgefilipecosta merged 2 commits intotrunkfrom
add/frontend-section-presets-output
Sep 23, 2022
Merged

Add: Frontend section presets output#42124
jorgefilipecosta merged 2 commits intotrunkfrom
add/frontend-section-presets-output

Conversation

@jorgefilipecosta
Copy link
Copy Markdown
Member

@jorgefilipecosta jorgefilipecosta commented Jul 4, 2022

Part of #40318.

This PR allows a block to define the preset settings of its nested blocks using the same shape as theme.json.

Continuation of #40547.

Shape

<!-- wp:group {"settings":{"blocks":{"core/button":{"color":{"palette":{"custom":[{"slug":"button-red","color":"red","name":"button red"},{"slug":"button-blue","color":"blue","name":"button blue"}]}}}},"color":{"palette":{"custom":[{"slug":"global-aquamarine","color":"aquamarine","name":"Global aquamarine"},{"slug":"global-pink","color":"pink","name":"Global Pink"}]}}}} -->
<div class="wp-block-group"><!-- wp:paragraph -->
<p>Leaf paragraph of inner group block.</p>
<!-- /wp:paragraph -->

<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"backgroundColor":"button-blue"} -->
<div class="wp-block-button"><a class="wp-block-button__link has-button-blue-background-color has-background wp-element-button">blue</a></div>
<!-- /wp:button -->

<!-- wp:button {"backgroundColor":"button-red"} -->
<div class="wp-block-button"><a class="wp-block-button__link has-button-red-background-color has-background wp-element-button">red</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->

<!-- wp:paragraph {"backgroundColor":"global-aquamarine"} -->
<p class="has-global-aquamarine-background-color has-background">global-aquamarine</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"backgroundColor":"global-pink"} -->
<p class="has-global-pink-background-color has-background">global-pink</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->

Formatted json:

{
   "settings":{
      "blocks":{
         "core/button":{
            "color":{
               "palette":{
                  "custom":[
                     {
                        "slug":"button-red",
                        "color":"red",
                        "name":"button red"
                     },
                     {
                        "slug":"button-blue",
                        "color":"blue",
                        "name":"button blue"
                     }
                  ]
               }
            }
         }
      },
      "color":{
         "palette":{
            "custom":[
               {
                  "slug":"global-aquamarine",
                  "color":"aquamarine",
                  "name":"Global aquamarine"
               },
               {
                  "slug":"global-pink",
                  "color":"pink",
                  "name":"Global Pink"
               }
            ]
         }
      }
   }
}

Engine output

.wp-block-level-presets-4884af9a06466a917323943b61ae5826, .wp-block-level-presets-4884af9a06466a917323943b61ae5826 [class*="wp-block"], .wp-block-level-presets-4884af9a06466a917323943b61ae5826 .wp-block-button .wp-block-button__link, .wp-block-level-presets-4884af9a06466a917323943b61ae5826 h1, .wp-block-level-presets-4884af9a06466a917323943b61ae5826 h2, .wp-block-level-presets-4884af9a06466a917323943b61ae5826 h3, .wp-block-level-presets-4884af9a06466a917323943b61ae5826 h4, .wp-block-level-presets-4884af9a06466a917323943b61ae5826 h5, .wp-block-level-presets-4884af9a06466a917323943b61ae5826 h6, .wp-block-level-presets-4884af9a06466a917323943b61ae5826 ol, .wp-block-level-presets-4884af9a06466a917323943b61ae5826 ul, .wp-block-level-presets-4884af9a06466a917323943b61ae5826 p, .wp-block-level-presets-4884af9a06466a917323943b61ae5826 .wp-block-table > table{
    --wp--preset--color--global-aquamarine: aquamarine;
    --wp--preset--color--global-pink: pink;
}
.wp-block-level-presets-4884af9a06466a917323943b61ae5826 .wp-block-button .wp-block-button__link{
    --wp--preset--color--button-red: red;
    --wp--preset--color--button-blue: blue;
}
.wp-block-level-presets-4884af9a06466a917323943b61ae5826.has-global-aquamarine-color,.wp-block-level-presets-4884af9a06466a917323943b61ae5826 *.has-global-aquamarine-color{
    color: var(--wp--preset--color--global-aquamarine) !important;
}
.wp-block-level-presets-4884af9a06466a917323943b61ae5826.has-global-pink-color,.wp-block-level-presets-4884af9a06466a917323943b61ae5826 *.has-global-pink-color{
    color: var(--wp--preset--color--global-pink) !important;
}
.wp-block-level-presets-4884af9a06466a917323943b61ae5826.has-global-aquamarine-background-color,.wp-block-level-presets-4884af9a06466a917323943b61ae5826 *.has-global-aquamarine-background-color{
    background-color: var(--wp--preset--color--global-aquamarine) !important;
}
.wp-block-level-presets-4884af9a06466a917323943b61ae5826.has-global-pink-background-color,.wp-block-level-presets-4884af9a06466a917323943b61ae5826 *.has-global-pink-background-color{
    background-color: var(--wp--preset--color--global-pink) !important;
}
.wp-block-level-presets-4884af9a06466a917323943b61ae5826.has-global-aquamarine-border-color,.wp-block-level-presets-4884af9a06466a917323943b61ae5826 *.has-global-aquamarine-border-color{
    border-color: var(--wp--preset--color--global-aquamarine) !important;
}
.wp-block-level-presets-4884af9a06466a917323943b61ae5826.has-global-pink-border-color,.wp-block-level-presets-4884af9a06466a917323943b61ae5826 *.has-global-pink-border-color{
    border-color: var(--wp--preset--color--global-pink) !important;
}
.wp-block-level-presets-4884af9a06466a917323943b61ae5826 .wp-block-button .wp-block-button__link.has-button-red-color{
    color: var(--wp--preset--color--button-red) !important;
}
.wp-block-level-presets-4884af9a06466a917323943b61ae5826 .wp-block-button .wp-block-button__link.has-button-blue-color{
    color: var(--wp--preset--color--button-blue) !important;
}
.wp-block-level-presets-4884af9a06466a917323943b61ae5826 .wp-block-button .wp-block-button__link.has-button-red-background-color{
    background-color: var(--wp--preset--color--button-red) !important;
}
.wp-block-level-presets-4884af9a06466a917323943b61ae5826 .wp-block-button .wp-block-button__link.has-button-blue-background-color{
    background-color: var(--wp--preset--color--button-blue) !important;
}
.wp-block-level-presets-4884af9a06466a917323943b61ae5826 .wp-block-button .wp-block-button__link.has-button-red-border-color{
    border-color: var(--wp--preset--color--button-red) !important;
}
.wp-block-level-presets-4884af9a06466a917323943b61ae5826 .wp-block-button .wp-block-button__link.has-button-blue-border-color{
    border-color: var(--wp--preset--color--button-blue) !important;
}
  
 

What?

It follows what was defined in #40547. The presets of a section overwrite everything set on the theme.json, even block-specific presets, so for the global presets of a section, we need to overwrite any block-specific settings as discussed in the other PR, which makes a complex CSS rule.
It follows the algorithms that were discussed with @oandregal and @youknowriad.

Testing

I pasted the group block above in the editor and verified the output on the front was the one also shared above and identical to the following screenshot:
image

@jorgefilipecosta jorgefilipecosta force-pushed the add/frontend-section-presets-output branch from dee4708 to 97499c4 Compare September 22, 2022 15:37
foreach ( $selectors as $inner ) {
$outer = trim( $outer );
$inner = trim( $inner );
if ( empty( $outer ) ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is purely a style choice, it's totally up to you. Personally, I find this more readable:

if ( empty( $outer ) && empty( $inner) ) { }
else if ( empty( $outer ) ) {}
else if ( empty( $inner ) ) {}
else {}

* // $merged is '.a > .x, .a .y, .b .c > .x, .b .c .y'
* </code>
*
* @since 5.9.0
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it removing this and add a comment with what changed? I understand the change is making sure no empty scope ends up being used, but not sure why we didn't that before.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't think the changes we are making to the function are worth a comment. We are just fixing some cases with empty selectors.

@oandregal
Copy link
Copy Markdown
Member

It seems the selectors for the CSS Custom Properties are scoped the other way around:

.wp-settings-4884af9a06466a917323943b61ae5826,
[class*="wp-block"] .wp-settings-4884af9a06466a917323943b61ae5826,
li .wp-settings-4884af9a06466a917323943b61ae5826,
.wp-block-button .wp-block-button__link .wp-settings-4884af9a06466a917323943b61ae5826,
h1 .wp-settings-4884af9a06466a917323943b61ae5826,
h2 .wp-settings-4884af9a06466a917323943b61ae5826,
h3 .wp-settings-4884af9a06466a917323943b61ae5826,
h4 .wp-settings-4884af9a06466a917323943b61ae5826,
h5 .wp-settings-4884af9a06466a917323943b61ae5826,
h6 .wp-settings-4884af9a06466a917323943b61ae5826,
ol .wp-settings-4884af9a06466a917323943b61ae5826,
ul .wp-settings-4884af9a06466a917323943b61ae5826,
p .wp-settings-4884af9a06466a917323943b61ae5826,
.wp-block-table > table .wp-settings-4884af9a06466a917323943b61ae5826,
.wp-settings-4884af9a06466a917323943b61ae5826 * {
  --wp--preset--color--global-aquamarine: aquamarine;
  --wp--preset--color--global-pink: pink;
}

.wp-settings-4884af9a06466a917323943b61ae5826 .wp-block-button .wp-block-button__link {
  --wp--preset--color--button-red: red;
  --wp--preset--color--button-blue: blue;
}```

$style_nodes = static::get_style_nodes( $this->theme_json, $blocks_metadata );
$setting_nodes = static::get_setting_nodes( $this->theme_json, $blocks_metadata );

$root_style_key = array_search( static::ROOT_BLOCK_SELECTOR, array_column( $style_nodes, 'selector' ), true );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice one ❤️ Wasn't super comfortable hard-coding the 0 index.

}
$stylesheet .= $this->get_block_classes( $style_nodes );
} elseif ( in_array( 'base-layout-styles', $types, true ) ) {
$root_selector = static::ROOT_BLOCK_SELECTOR;
Copy link
Copy Markdown
Member

@oandregal oandregal Sep 22, 2022

Choose a reason for hiding this comment

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

This gist of code is not used at the moment, correct? My understanding is that it is just a way to make the scope and root_selector options work with this part of the code as well. I wasn't sure how to test this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, it is not used at the moment, but it is to make sure the function also works with base-layout-styles origin. I don't think it will be needed but I preferred to make the function consistent.

} elseif ( in_array( 'base-layout-styles', $types, true ) ) {
$root_selector = static::ROOT_BLOCK_SELECTOR;
$columns_selector = '.wp-block-columns';
if ( ! empty( $options['scope'] ) ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Shouldn't this be?

Suggested change
if ( ! empty( $options['scope'] ) ) {
if ( ! empty( $options['root_selector'] ) ) {

Copy link
Copy Markdown
Member

@oandregal oandregal 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 quick turnaround, Jorge!

I've left some minor comments, but I won't be available tomorrow to review/re-approve, so I trust they'll be addressed.

I'm more comfortable with this approach, as it doesn't introduce new APIs just yet. Through the conversations in this thread, I've learned that WP_Theme_JSON is no longer agnostic of the context it runs into, and the changes here go a step in the direction of making them agnostic again. It's far for complete, though.

@jorgefilipecosta
Copy link
Copy Markdown
Member Author

Thank you a lot for the review @oandregal!

It seems the selectors for the CSS Custom Properties are scoped the other way around:

The parameters order was wrong on the last commit. It is fixed.

All the feedback was applied!

@ockham
Copy link
Copy Markdown
Contributor

ockham commented Sep 26, 2022

IIUC, we'll want to include this in GB 14.2 in order to unblock this wordpress-develop PR.

cc/ @michalczaplinski

@michalczaplinski
Copy link
Copy Markdown
Contributor

Cherry-picked this into release/14.2 in 09b46dd 👍

@oandregal
Copy link
Copy Markdown
Member

Hey, while working on #46579 I realized this PR was not backported to WordPress 6.1. When we land that PR and then backport to WordPress 6.2 everything will be fine, but I wanted to raise in case you think this should be ported for a potential WordPress 6.1.X release.

@Mamaduka
Copy link
Copy Markdown
Member

Mamaduka commented Feb 6, 2023

@jorgefilipecosta, does this change have a WP core backport PR? Unfortunately, I wasn't able to find any.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Group Affects the Group Block (and row, stack and grid variants) [Block] Pattern Affects the Patterns Block 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.

8 participants