Editor: fix custom CSS output for block style variations.

Adds custom CSS to the block style variation rules output by `get_styles_for_block` in class `WP_Theme_JSON`.

Props isabel_brison, aaronrobertshaw.
Fixes #61425.


git-svn-id: https://develop.svn.wordpress.org/trunk@58421 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Isabel Brison 2024-06-17 03:47:37 +00:00
parent fa7902c8c8
commit c00ad5aa48

View File

@ -2647,6 +2647,7 @@ class WP_Theme_JSON {
* @since 6.1.0
* @since 6.6.0 Setting a min-height of HTML when root styles have a background gradient or image.
* Updated general global styles specificity to 0-1-0.
* Fixed custom CSS output in block style variations.
*
* @param array $block_metadata Metadata about the block to get styles for.
*
@ -2662,6 +2663,7 @@ class WP_Theme_JSON {
// If there are style variations, generate the declarations for them, including any feature selectors the block may have.
$style_variation_declarations = array();
$style_variation_custom_css = array();
if ( ! empty( $block_metadata['variations'] ) ) {
foreach ( $block_metadata['variations'] as $style_variation ) {
$style_variation_node = _wp_array_get( $this->theme_json, $style_variation['path'], array() );
@ -2691,6 +2693,10 @@ class WP_Theme_JSON {
// Compute declarations for remaining styles not covered by feature level selectors.
$style_variation_declarations[ $style_variation['selector'] ] = static::compute_style_properties( $style_variation_node, $settings, null, $this->theme_json );
// Store custom CSS for the style variation.
if ( isset( $style_variation_node['css'] ) ) {
$style_variation_custom_css[ $style_variation['selector'] ] = $this->process_blocks_custom_css( $style_variation_node['css'], $style_variation['selector'] );
}
}
}
/*
@ -2819,6 +2825,14 @@ class WP_Theme_JSON {
// 6. Generate and append the style variation rulesets.
foreach ( $style_variation_declarations as $style_variation_selector => $individual_style_variation_declarations ) {
$block_rules .= static::to_ruleset( ":root :where($style_variation_selector)", $individual_style_variation_declarations );
if ( isset( $style_variation_custom_css[ $style_variation_selector ] ) ) {
$block_rules .= $style_variation_custom_css[ $style_variation_selector ];
}
}
// 7. Generate and append any custom CSS rules pertaining to nested block style variations.
if ( isset( $node['css'] ) && ! $is_root_selector ) {
$block_rules .= $this->process_blocks_custom_css( $node['css'], $selector );
}
return $block_rules;