Skip to content

Commit

Permalink
Block Styles: Ensure unique classname generation for variations
Browse files Browse the repository at this point in the history
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.



git-svn-id: https://develop.svn.wordpress.org/trunk@58951 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
ramonjd committed Aug 29, 2024
1 parent e2c4f64 commit 0a9dcb4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
23 changes: 3 additions & 20 deletions src/wp-includes/block-supports/block-style-variations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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";

Expand Down Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions src/wp-includes/deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Tests_Block_Supports_WpCreateBlockStyleVariationInstanceName extends WP_Un
* @ticket 61312
*
* @covers ::wp_create_block_style_variation_instance_name
*
* @expectedDeprecated wp_create_block_style_variation_instance_name
*/
public function test_block_style_variation_instance_name_generation() {
$block = array( 'name' => 'test/block' );
Expand Down

0 comments on commit 0a9dcb4

Please sign in to comment.