Skip to content

Commit

Permalink
Block Styles: Ensure unique classname generation for variations (Word…
Browse files Browse the repository at this point in the history
…Press#64511)

* Block Styles: Ensure unique classname generation for variations

* Add backport changelog

Co-authored-by: aaronrobertshaw <[email protected]>
Co-authored-by: ramonjd <[email protected]>
Co-authored-by: carolinan <[email protected]>
Co-authored-by: asafm7 <[email protected]>
  • Loading branch information
5 people authored and bph committed Aug 31, 2024
1 parent f8b70f2 commit d2608c8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
3 changes: 3 additions & 0 deletions backport-changelog/6.7/7200.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/7200

* https://github.com/WordPress/gutenberg/pull/64511
11 changes: 6 additions & 5 deletions lib/block-supports/block-style-variations.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
*
* @since 6.6.0
*
* @deprecated 6.7.0
*
* @param array $block Block object.
* @param string $variation Slug for the block style variation.
*
* @return string The unique variation name.
*/
function gutenberg_create_block_style_variation_instance_name( $block, $variation ) {
_deprecated_function( __FUNCTION__, '6.7.0' );
return $variation . '--' . md5( serialize( $block ) );
}

Expand Down Expand Up @@ -119,7 +122,7 @@ function gutenberg_render_block_style_variation_support_styles( $parsed_block )
// theme_json data.
gutenberg_resolve_block_style_variation_ref_values( $variation_data, $theme_json );

$variation_instance = gutenberg_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 @@ -224,11 +227,9 @@ function gutenberg_render_block_style_variation_class_name( $block_content, $blo

/*
* Matches a class prefixed by `is-style`, followed by the
* variation slug, then `--`, and finally a hash.
*
* See `gutenberg_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

0 comments on commit d2608c8

Please sign in to comment.