Make WordPress Core


Ignore:
Timestamp:
08/29/2024 05:39:22 AM (15 months ago)
Author:
ramonopoly
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-supports/block-style-variations.php

    r58850 r58951  
    77 * @since 6.6.0
    88 */
    9 
    10 /**
    11  * Generate block style variation instance name.
    12  *
    13  * @since 6.6.0
    14  * @access private
    15  *
    16  * @param array  $block     Block object.
    17  * @param string $variation Slug for the block style variation.
    18  *
    19  * @return string The unique variation name.
    20  */
    21 function wp_create_block_style_variation_instance_name( $block, $variation ) {
    22     return $variation . '--' . md5( serialize( $block ) );
    23 }
    249
    2510/**
     
    125110    wp_resolve_block_style_variation_ref_values( $variation_data, $theme_json );
    126111
    127     $variation_instance = wp_create_block_style_variation_instance_name( $parsed_block, $variation );
     112    $variation_instance = wp_unique_id( $variation . '--' );
    128113    $class_name         = "is-style-$variation_instance";
    129114    $updated_class_name = $parsed_block['attrs']['className'] . " $class_name";
     
    231216    /*
    232217     * Matches a class prefixed by `is-style`, followed by the
    233      * variation slug, then `--`, and finally a hash.
    234      *
    235      * See `wp_create_block_style_variation_instance_name` for class generation.
    236      */
    237     preg_match( '/\bis-style-(\S+?--\w+)\b/', $block['attrs']['className'], $matches );
     218     * variation slug, then `--`, and finally an instance number.
     219     */
     220    preg_match( '/\bis-style-(\S+?--\d+)\b/', $block['attrs']['className'], $matches );
    238221
    239222    if ( empty( $matches ) ) {
Note: See TracChangeset for help on using the changeset viewer.