Block Styles: Ensure unique classname generation for variations

This commit simplifies block style variation class name generation to ensure unique class names by replacing the hashing of block attributes in the block style variation class names with a call to `wp_unique_id`.

Doing so avoids potential for non-unique class names and conflicting styles when exact copies of a block are inserted via a repeated pattern.


Props aaronrobertshaw, martinkrcho, mukesh27, peterwilsoncc, ramonopoly.

Fixes #61877.


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


git-svn-id: http://core.svn.wordpress.org/trunk@58347 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ramonopoly 2024-08-29 05:41:10 +00:00
parent 06e50d2cbb
commit 94b9ccd020
3 changed files with 22 additions and 21 deletions

View File

@ -7,21 +7,6 @@
* @since 6.6.0 * @since 6.6.0
*/ */
/**
* Generate block style variation instance name.
*
* @since 6.6.0
* @access private
*
* @param array $block Block object.
* @param string $variation Slug for the block style variation.
*
* @return string The unique variation name.
*/
function wp_create_block_style_variation_instance_name( $block, $variation ) {
return $variation . '--' . md5( serialize( $block ) );
}
/** /**
* Determines the block style variation names within a CSS class string. * Determines the block style variation names within a CSS class string.
* *
@ -124,7 +109,7 @@ function wp_render_block_style_variation_support_styles( $parsed_block ) {
*/ */
wp_resolve_block_style_variation_ref_values( $variation_data, $theme_json ); wp_resolve_block_style_variation_ref_values( $variation_data, $theme_json );
$variation_instance = wp_create_block_style_variation_instance_name( $parsed_block, $variation ); $variation_instance = wp_unique_id( $variation . '--' );
$class_name = "is-style-$variation_instance"; $class_name = "is-style-$variation_instance";
$updated_class_name = $parsed_block['attrs']['className'] . " $class_name"; $updated_class_name = $parsed_block['attrs']['className'] . " $class_name";
@ -230,11 +215,9 @@ function wp_render_block_style_variation_class_name( $block_content, $block ) {
/* /*
* Matches a class prefixed by `is-style`, followed by the * Matches a class prefixed by `is-style`, followed by the
* variation slug, then `--`, and finally a hash. * variation slug, then `--`, and finally an instance number.
*
* See `wp_create_block_style_variation_instance_name` for class generation.
*/ */
preg_match( '/\bis-style-(\S+?--\w+)\b/', $block['attrs']['className'], $matches ); preg_match( '/\bis-style-(\S+?--\d+)\b/', $block['attrs']['className'], $matches );
if ( empty( $matches ) ) { if ( empty( $matches ) ) {
return $block_content; return $block_content;

View File

@ -6387,3 +6387,21 @@ function wp_enqueue_global_styles_custom_css() {
wp_add_inline_style( 'global-styles', $custom_css ); wp_add_inline_style( 'global-styles', $custom_css );
} }
} }
/**
* Generate block style variation instance name.
*
* @since 6.6.0
* @deprecated 6.7.0 Use `wp_unique_id( $variation . '--' )` instead.
*
* @access private
*
* @param array $block Block object.
* @param string $variation Slug for the block style variation.
*
* @return string The unique variation name.
*/
function wp_create_block_style_variation_instance_name( $block, $variation ) {
_deprecated_function( __FUNCTION__, '6.7.0', 'wp_unique_id' );
return $variation . '--' . md5( serialize( $block ) );
}

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.7-alpha-58950'; $wp_version = '6.7-alpha-58951';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.