Make WordPress Core

Changeset 58951


Ignore:
Timestamp:
08/29/2024 05:39:22 AM (3 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.

Location:
trunk
Files:
3 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 ) ) {
  • trunk/src/wp-includes/deprecated.php

    r58703 r58951  
    63886388    }
    63896389}
     6390
     6391/**
     6392 * Generate block style variation instance name.
     6393 *
     6394 * @since 6.6.0
     6395 * @deprecated 6.7.0 Use `wp_unique_id( $variation . '--' )` instead.
     6396 *
     6397 * @access private
     6398 *
     6399 * @param array  $block     Block object.
     6400 * @param string $variation Slug for the block style variation.
     6401 *
     6402 * @return string The unique variation name.
     6403 */
     6404function wp_create_block_style_variation_instance_name( $block, $variation ) {
     6405    _deprecated_function( __FUNCTION__, '6.7.0', 'wp_unique_id' );
     6406    return $variation . '--' . md5( serialize( $block ) );
     6407}
  • trunk/tests/phpunit/tests/block-supports/wpCreateBlockStyleVariationInstanceName.php

    r58264 r58951  
    1414     *
    1515     * @covers ::wp_create_block_style_variation_instance_name
     16     *
     17     * @expectedDeprecated wp_create_block_style_variation_instance_name
    1618     */
    1719    public function test_block_style_variation_instance_name_generation() {
Note: See TracChangeset for help on using the changeset viewer.