Make WordPress Core


Ignore:
Timestamp:
07/09/2024 04:06:20 PM (21 months ago)
Author:
hellofromTonya
Message:

Editor: Fix ref values within block style variations.

Fixes an issue where block style variations that use ref values to reference styles elsewhere in theme.json weren't working.

Retrieves reference values when generating styles for block style variations. How? When retrieving variation data, to generate styles from, also retrieve any referenced values.

Reviewed by audrasjb.
Merges [58691] to the 6.6 branch.

Props aaronrobertshaw, andrewserong, ramonopoly.
Fixes #61589.

Location:
branches/6.6
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.6

  • branches/6.6/src/wp-includes/block-supports/block-style-variations.php

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