From 79772361e6e21c6d20698af0a7c9ba688bbc9063 Mon Sep 17 00:00:00 2001 From: Ella Date: Tue, 9 Jul 2024 10:24:19 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@58691 git-svn-id: https://core.svn.wordpress.org/trunk@58093 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 9cf590bb83..b894ed6f42 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.7-alpha-58690'; +$wp_version = '6.7-alpha-58691'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.