Make WordPress Core

Changeset 61564


Ignore:
Timestamp:
01/30/2026 02:09:32 AM (10 months ago)
Author:
isabel_brison
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-supports/block-style-variations.php

    r59549 r61564  
    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,
  • trunk/src/wp-includes/class-wp-theme-json.php

    r61473 r61564  
    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.
    … …  
    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 ) {
    … …  
    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 ) {
    … …  
    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'] );
     2843                                }
     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                                        );
    28542855                                }
    28552856                        }
    … …  
    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 ];
    … …  
    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' ) );
  • trunk/tests/phpunit/tests/theme/wpThemeJson.php

    r61473 r61564  
    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 );
Note: See TracChangeset for help on using the changeset viewer.