From 98b50226521bfed303c744353187387e48184a8d Mon Sep 17 00:00:00 2001 From: hellofromTonya Date: Tue, 9 Jul 2024 16:08:15 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/branches/6.6@58699 git-svn-id: http://core.svn.wordpress.org/branches/6.6@58101 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../block-supports/block-style-variations.php | 42 +++++++++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/wp-includes/block-supports/block-style-variations.php b/wp-includes/block-supports/block-style-variations.php index 5d92ecfb12..77659bf86f 100644 --- a/wp-includes/block-supports/block-style-variations.php +++ b/wp-includes/block-supports/block-style-variations.php @@ -40,6 +40,42 @@ function wp_get_block_style_variation_name_from_class( $class_string ) { return $matches[1] ?? null; } +/** + * Recursively resolves any `ref` values within a block style variation's data. + * + * @since 6.6.0 + * @access private + * + * @param array $variation_data Reference to the variation data being processed. + * @param array $theme_json Theme.json data to retrieve referenced values from. + */ +function wp_resolve_block_style_variation_ref_values( &$variation_data, $theme_json ) { + foreach ( $variation_data as $key => &$value ) { + // Only need to potentially process arrays. + if ( is_array( $value ) ) { + // If ref value is set, attempt to find its matching value and update it. + if ( array_key_exists( 'ref', $value ) ) { + // Clean up any invalid ref value. + if ( empty( $value['ref'] ) || ! is_string( $value['ref'] ) ) { + unset( $variation_data[ $key ] ); + } + + $value_path = explode( '.', $value['ref'] ?? '' ); + $ref_value = _wp_array_get( $theme_json, $value_path ); + + // Only update the current value if the referenced path matched a value. + if ( null === $ref_value ) { + unset( $variation_data[ $key ] ); + } else { + $value = $ref_value; + } + } else { + // Recursively look for ref instances. + wp_resolve_block_style_variation_ref_values( $value, $theme_json ); + } + } + } +} /** * Render the block style variation's styles. * @@ -82,6 +118,12 @@ function wp_render_block_style_variation_support_styles( $parsed_block ) { return $parsed_block; } + /* + * Recursively resolve any ref values with the appropriate value within the + * theme_json data. + */ + wp_resolve_block_style_variation_ref_values( $variation_data, $theme_json ); + $variation_instance = wp_create_block_style_variation_instance_name( $parsed_block, $variation ); $class_name = "is-style-$variation_instance"; $updated_class_name = $parsed_block['attrs']['className'] . " $class_name"; diff --git a/wp-includes/version.php b/wp-includes/version.php index 9e3a651f6c..88f42997e6 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.6-RC2-58698'; +$wp_version = '6.6-RC2-58699'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.