Make WordPress Core

Changeset 58691 for trunk


Ignore:
Timestamp:
07/09/2024 10:22:10 AM (19 months ago)
Author:
ellatrix
Message:

Section Styles: Fix ref values within block style variations.

Fixes #61589.
Fixes https://github.com/WordPress/wordpress-develop/pull/6989.
See https://github.com/WordPress/gutenberg/pull/63172.

Props aaronrobertshaw, andrewserong, ramonopoly.

Location:
trunk
Files:
2 edited

Legend:

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

    r58466 r58691  
    4141}
    4242
     43/**
     44 * Recursively resolves any `ref` values within a block style variation's data.
     45 *
     46 * @since 6.6.0
     47 * @access private
     48 *
     49 * @param array $variation_data Reference to the variation data being processed.
     50 * @param array $theme_json     Theme.json data to retrieve referenced values from.
     51 */
     52function wp_resolve_block_style_variation_ref_values( &$variation_data, $theme_json ) {
     53    foreach ( $variation_data as $key => &$value ) {
     54        // Only need to potentially process arrays.
     55        if ( is_array( $value ) ) {
     56            // If ref value is set, attempt to find its matching value and update it.
     57            if ( array_key_exists( 'ref', $value ) ) {
     58                // Clean up any invalid ref value.
     59                if ( empty( $value['ref'] ) || ! is_string( $value['ref'] ) ) {
     60                    unset( $variation_data[ $key ] );
     61                }
     62
     63                $value_path = explode( '.', $value['ref'] ?? '' );
     64                $ref_value  = _wp_array_get( $theme_json, $value_path );
     65
     66                // Only update the current value if the referenced path matched a value.
     67                if ( null === $ref_value ) {
     68                    unset( $variation_data[ $key ] );
     69                } else {
     70                    $value = $ref_value;
     71                }
     72            } else {
     73                // Recursively look for ref instances.
     74                wp_resolve_block_style_variation_ref_values( $value, $theme_json );
     75            }
     76        }
     77    }
     78}
    4379/**
    4480 * Render the block style variation's styles.
     
    82118        return $parsed_block;
    83119    }
     120
     121    /*
     122     * Recursively resolve any ref values with the appropriate value within the
     123     * theme_json data.
     124     */
     125    wp_resolve_block_style_variation_ref_values( $variation_data, $theme_json );
    84126
    85127    $variation_instance = wp_create_block_style_variation_instance_name( $parsed_block, $variation );
  • trunk/tests/phpunit/tests/block-supports/block-style-variations.php

    r58466 r58691  
    160160        $this->assertSameSetsWithIndex( $expected, $group_styles, 'Variation data does not match' );
    161161    }
     162
     163    /**
     164     * Tests that block style variations resolve any `ref` values when generating styles.
     165     *
     166     * @ticket 61589
     167     */
     168    public function test_block_style_variation_ref_values() {
     169        switch_theme( 'block-theme' );
     170
     171        $variation_data = array(
     172            'color'    => array(
     173                'text'       => array(
     174                    'ref' => 'styles.does-not-exist',
     175                ),
     176                'background' => array(
     177                    'ref' => 'styles.blocks.core/group.variations.block-style-variation-a.color.text',
     178                ),
     179            ),
     180            'blocks'   => array(
     181                'core/heading' => array(
     182                    'color' => array(
     183                        'text'       => array(
     184                            'ref' => 'styles.blocks.core/group.variations.block-style-variation-a.color.background',
     185                        ),
     186                        'background' => array(
     187                            'ref' => '',
     188                        ),
     189                    ),
     190                ),
     191            ),
     192            'elements' => array(
     193                'link' => array(
     194                    'color'  => array(
     195                        'text'       => array(
     196                            'ref' => 'styles.blocks.core/group.variations.block-style-variation-b.color.text',
     197                        ),
     198                        'background' => array(
     199                            'ref' => null,
     200                        ),
     201                    ),
     202                    ':hover' => array(
     203                        'color' => array(
     204                            'text' => array(
     205                                'ref' => 'styles.blocks.core/group.variations.block-style-variation-b.color.background',
     206                            ),
     207                        ),
     208                    ),
     209                ),
     210            ),
     211        );
     212
     213        $theme_json = WP_Theme_JSON_Resolver::get_theme_data()->get_raw_data();
     214
     215        wp_resolve_block_style_variation_ref_values( $variation_data, $theme_json );
     216
     217        $expected = array(
     218            'color'    => array( 'background' => 'plum' ),
     219            'blocks'   => array(
     220                'core/heading' => array(
     221                    'color' => array( 'text' => 'indigo' ),
     222                ),
     223            ),
     224            'elements' => array(
     225                'link' => array(
     226                    'color'  => array( 'text' => 'lightblue' ),
     227                    ':hover' => array(
     228                        'color' => array( 'text' => 'midnightblue' ),
     229                    ),
     230                ),
     231            ),
     232        );
     233
     234        $this->assertSameSetsWithIndex( $expected, $variation_data, 'Variation data with resolved ref values does not match' );
     235    }
    162236}
Note: See TracChangeset for help on using the changeset viewer.