From 94b9ccd020ad100ac4dc054b990892a00b7a77c1 Mon Sep 17 00:00:00 2001 From: ramonopoly Date: Thu, 29 Aug 2024 05:41:10 +0000 Subject: [PATCH] Block Styles: Ensure unique classname generation for variations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../block-supports/block-style-variations.php | 23 +++---------------- wp-includes/deprecated.php | 18 +++++++++++++++ wp-includes/version.php | 2 +- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/wp-includes/block-supports/block-style-variations.php b/wp-includes/block-supports/block-style-variations.php index 2e86cb71af..4397a34af9 100644 --- a/wp-includes/block-supports/block-style-variations.php +++ b/wp-includes/block-supports/block-style-variations.php @@ -7,21 +7,6 @@ * @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. * @@ -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 ); - $variation_instance = wp_create_block_style_variation_instance_name( $parsed_block, $variation ); + $variation_instance = wp_unique_id( $variation . '--' ); $class_name = "is-style-$variation_instance"; $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 - * variation slug, then `--`, and finally a hash. - * - * See `wp_create_block_style_variation_instance_name` for class generation. + * variation slug, then `--`, and finally an instance number. */ - 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 ) ) { return $block_content; diff --git a/wp-includes/deprecated.php b/wp-includes/deprecated.php index 457b7fc136..be92ba6796 100644 --- a/wp-includes/deprecated.php +++ b/wp-includes/deprecated.php @@ -6387,3 +6387,21 @@ function wp_enqueue_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 ) ); +} diff --git a/wp-includes/version.php b/wp-includes/version.php index a18d4771e3..58d205bc82 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @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.