Skip to content

Commit

Permalink
Section Styles: Fix ref values within block style variations.
Browse files Browse the repository at this point in the history
Fixes #61589.
Fixes WordPress/wordpress-develop#6989.
See WordPress/gutenberg#63172.

Props aaronrobertshaw, andrewserong, ramonopoly.


Built from https://develop.svn.wordpress.org/trunk@58691


git-svn-id: http://core.svn.wordpress.org/trunk@58093 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
ellatrix committed Jul 9, 2024
1 parent 5e060bc commit c56593d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
42 changes: 42 additions & 0 deletions wp-includes/block-supports/block-style-variations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c56593d

Please sign in to comment.