Skip to content

Commit e3ad7a0

Browse files
Editor: fix blockGap styles in block style variations.
Ensures that when a block style variation declares a blockGap value, the correct layout styles are output for the variation. Props isabel_brison, aaronrobertshaw. Fixes #64533. git-svn-id: https://develop.svn.wordpress.org/trunk@61564 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 13eac41 commit e3ad7a0

3 files changed

Lines changed: 90 additions & 18 deletions

File tree

src/wp-includes/block-supports/block-style-variations.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,13 @@ function wp_render_block_style_variation_support_styles( $parsed_block ) {
142142
);
143143

144144
$config = array(
145-
'version' => WP_Theme_JSON::LATEST_SCHEMA,
146-
'styles' => array(
145+
'version' => WP_Theme_JSON::LATEST_SCHEMA,
146+
'settings' => array(
147+
'spacing' => array(
148+
'blockGap' => true,
149+
),
150+
),
151+
'styles' => array(
147152
'elements' => $elements_data,
148153
'blocks' => $blocks_data,
149154
),

src/wp-includes/class-wp-theme-json.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,7 +1615,7 @@ protected function get_layout_styles( $block_metadata, $options = array() ) {
16151615
$selector = $block_metadata['selector'] ?? '';
16161616
$has_block_gap_support = isset( $this->theme_json['settings']['spacing']['blockGap'] );
16171617
$has_fallback_gap_support = ! $has_block_gap_support; // This setting isn't useful yet: it exists as a placeholder for a future explicit fallback gap styles support.
1618-
$node = _wp_array_get( $this->theme_json, $block_metadata['path'], array() );
1618+
$node = $options['node'] ?? _wp_array_get( $this->theme_json, $block_metadata['path'], array() );
16191619
$layout_definitions = wp_get_layout_definitions();
16201620
$layout_selector_pattern = '/^[a-zA-Z0-9\-\.\,\ *+>:\(\)]*$/'; // Allow alphanumeric classnames, spaces, wildcard, sibling, child combinator and pseudo class selectors.
16211621

@@ -2356,18 +2356,6 @@ protected static function compute_style_properties( $styles, $settings = array()
23562356
continue;
23572357
}
23582358

2359-
/*
2360-
* Look up protected properties, keyed by value path.
2361-
* Skip protected properties that are explicitly set to `null`.
2362-
*/
2363-
$path_string = implode( '.', $value_path );
2364-
if (
2365-
isset( static::PROTECTED_PROPERTIES[ $path_string ] ) &&
2366-
_wp_array_get( $settings, static::PROTECTED_PROPERTIES[ $path_string ], null ) === null
2367-
) {
2368-
continue;
2369-
}
2370-
23712359
// Calculates fluid typography rules where available.
23722360
if ( 'font-size' === $css_property ) {
23732361
/*
@@ -2811,8 +2799,9 @@ public function get_styles_for_block( $block_metadata ) {
28112799
$is_root_selector = static::ROOT_BLOCK_SELECTOR === $selector;
28122800

28132801
// If there are style variations, generate the declarations for them, including any feature selectors the block may have.
2814-
$style_variation_declarations = array();
2815-
$style_variation_custom_css = array();
2802+
$style_variation_declarations = array();
2803+
$style_variation_custom_css = array();
2804+
$style_variation_layout_metadata = array();
28162805
if ( ! empty( $block_metadata['variations'] ) ) {
28172806
foreach ( $block_metadata['variations'] as $style_variation ) {
28182807
$style_variation_node = _wp_array_get( $this->theme_json, $style_variation['path'], array() );
@@ -2852,6 +2841,18 @@ static function ( $split_selector ) use ( $clean_style_variation_selector ) {
28522841
if ( isset( $style_variation_node['css'] ) ) {
28532842
$style_variation_custom_css[ $style_variation['selector'] ] = $this->process_blocks_custom_css( $style_variation_node['css'], $style_variation['selector'] );
28542843
}
2844+
2845+
// Store variation metadata and node for layout styles generation.
2846+
// Only store if the variation has blockGap defined.
2847+
if ( isset( $style_variation_node['spacing']['blockGap'] ) ) {
2848+
// Append block selector to the variation selector for proper targeting.
2849+
$variation_metadata_with_selector = $style_variation;
2850+
$variation_metadata_with_selector['selector'] = $style_variation['selector'] . $block_metadata['css'];
2851+
$style_variation_layout_metadata[ $style_variation['selector'] ] = array(
2852+
'metadata' => $variation_metadata_with_selector,
2853+
'node' => $style_variation_node,
2854+
);
2855+
}
28552856
}
28562857
}
28572858
/*
@@ -3004,6 +3005,10 @@ static function ( $pseudo_selector ) use ( $selector ) {
30043005
// 6. Generate and append the style variation rulesets.
30053006
foreach ( $style_variation_declarations as $style_variation_selector => $individual_style_variation_declarations ) {
30063007
$block_rules .= static::to_ruleset( ":root :where($style_variation_selector)", $individual_style_variation_declarations );
3008+
if ( isset( $style_variation_layout_metadata[ $style_variation_selector ] ) ) {
3009+
$variation_data = $style_variation_layout_metadata[ $style_variation_selector ];
3010+
$block_rules .= $this->get_layout_styles( $variation_data['metadata'], array( 'node' => $variation_data['node'] ) );
3011+
}
30073012
if ( isset( $style_variation_custom_css[ $style_variation_selector ] ) ) {
30083013
$block_rules .= $style_variation_custom_css[ $style_variation_selector ];
30093014
}
@@ -3079,7 +3084,7 @@ public function get_root_layout_rules( $selector, $block_metadata, $options = ar
30793084
$css .= '.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }';
30803085
}
30813086

3082-
// Block gap styles will be output unless explicitly set to `null`. See static::PROTECTED_PROPERTIES.
3087+
// Block gap styles will be output unless explicitly set to `null`.
30833088
if ( isset( $this->theme_json['settings']['spacing']['blockGap'] ) ) {
30843089
$block_gap_value = static::get_property_value( $this->theme_json, array( 'styles', 'spacing', 'blockGap' ) );
30853090
$css .= ":where(.wp-site-blocks) > * { margin-block-start: $block_gap_value; margin-block-end: 0; }";

tests/phpunit/tests/theme/wpThemeJson.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4731,6 +4731,68 @@ public function test_get_styles_for_block_with_style_variations_and_custom_selec
47314731
$this->assertSame( $expected, $actual_styles );
47324732
}
47334733

4734+
/**
4735+
* Tests that block style variations with blockGap generate proper layout styles.
4736+
*
4737+
* @ticket 64533
4738+
*/
4739+
public function test_get_styles_for_block_with_style_variations_and_block_gap() {
4740+
register_block_style(
4741+
'core/group',
4742+
array(
4743+
'name' => 'withGap',
4744+
'label' => 'With Gap',
4745+
)
4746+
);
4747+
4748+
$theme_json = new WP_Theme_JSON(
4749+
array(
4750+
'version' => WP_Theme_JSON::LATEST_SCHEMA,
4751+
'settings' => array(
4752+
'spacing' => array(
4753+
'blockGap' => true,
4754+
),
4755+
),
4756+
'styles' => array(
4757+
'blocks' => array(
4758+
'core/group' => array(
4759+
'variations' => array(
4760+
'withGap' => array(
4761+
'color' => array(
4762+
'background' => 'tomato',
4763+
),
4764+
'spacing' => array(
4765+
'blockGap' => '5rem',
4766+
),
4767+
),
4768+
),
4769+
),
4770+
),
4771+
),
4772+
)
4773+
);
4774+
4775+
$metadata = array(
4776+
'name' => 'core/group',
4777+
'path' => array( 'styles', 'blocks', 'core/group' ),
4778+
'selector' => '.wp-block-group',
4779+
'css' => '.wp-block-group',
4780+
'variations' => array(
4781+
array(
4782+
'path' => array( 'styles', 'blocks', 'core/group', 'variations', 'withGap' ),
4783+
'selector' => '.is-style-withGap.wp-block-group',
4784+
),
4785+
),
4786+
);
4787+
4788+
$actual_styles = $theme_json->get_styles_for_block( $metadata );
4789+
4790+
unregister_block_style( 'core/group', 'withGap' );
4791+
4792+
$expected = ':root :where(.is-style-withGap.wp-block-group){background-color: tomato;}:root :where(.is-style-withGap.wp-block-group.wp-block-group-is-layout-flow) > :first-child{margin-block-start: 0;}:root :where(.is-style-withGap.wp-block-group.wp-block-group-is-layout-flow) > :last-child{margin-block-end: 0;}:root :where(.is-style-withGap.wp-block-group.wp-block-group-is-layout-flow) > *{margin-block-start: 5rem;margin-block-end: 0;}:root :where(.is-style-withGap.wp-block-group.wp-block-group-is-layout-constrained) > :first-child{margin-block-start: 0;}:root :where(.is-style-withGap.wp-block-group.wp-block-group-is-layout-constrained) > :last-child{margin-block-end: 0;}:root :where(.is-style-withGap.wp-block-group.wp-block-group-is-layout-constrained) > *{margin-block-start: 5rem;margin-block-end: 0;}:root :where(.is-style-withGap.wp-block-group.wp-block-group-is-layout-flex){gap: 5rem;}:root :where(.is-style-withGap.wp-block-group.wp-block-group-is-layout-grid){gap: 5rem;}';
4793+
$this->assertSame( $expected, $actual_styles );
4794+
}
4795+
47344796
public function test_block_style_variations() {
47354797
wp_set_current_user( static::$administrator_id );
47364798

0 commit comments

Comments
 (0)