Skip to content

Commit e45892b

Browse files
committed
Editor: Fix wp-elements-* CSS class name collisions for identical blocks.
The `wp_get_elements_class_name()` function previously generated CSS class names by hashing the serialized block data via `md5()`. Identical blocks received the same `wp-elements-*` class name and the Style Engine deduplicated their CSS rules into one, causing a parent block's element style (e.g. link color) to cascade down and override a child block's identical style due to CSS source order. The function is updated to use `wp_unique_prefixed_id()` instead, generating sequential unique class names (`wp-elements-1`, `wp-elements-2`, etc.) that match the block editor's JavaScript implementation. The now-unused `$parsed_block` parameter is removed from the function signature. PHPStan rule level 10 errors are also resolved in the related code. See #64898. Developed in WordPress/wordpress-develop#12126. Follow-up to r53260, r58074. Props tusharbharti, westonruter, wildworks. Fixes #65435. Built from https://develop.svn.wordpress.org/trunk@62520 git-svn-id: http://core.svn.wordpress.org/trunk@61801 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 parent 73f8bbb commit e45892b

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

wp-includes/block-supports/elements.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
* @since 6.0.0
1313
* @access private
1414
*
15-
* @param array $block Block object.
1615
* @return string The unique class name.
1716
*/
18-
function wp_get_elements_class_name( $block ) {
19-
return 'wp-elements-' . md5( serialize( $block ) );
17+
function wp_get_elements_class_name(): string {
18+
return wp_unique_prefixed_id( 'wp-elements-' );
2019
}
2120

2221
/**
@@ -109,6 +108,29 @@ function wp_should_add_elements_class_name( $block, $options ) {
109108
*
110109
* @param array $parsed_block The parsed block.
111110
* @return array The same parsed block with elements classname added if appropriate.
111+
*
112+
* @phpstan-param array{
113+
* blockName: string,
114+
* attrs: array{
115+
* className?: string,
116+
* style?: array{
117+
* elements?: array<string, array{
118+
* ":hover"?: array<string, string>,
119+
* ...
120+
* }>,
121+
* },
122+
* ...
123+
* },
124+
* ...
125+
* } $parsed_block
126+
* @phpstan-return array{
127+
* blockName: string,
128+
* attrs: array{
129+
* className?: string,
130+
* ...
131+
* },
132+
* ...
133+
* }
112134
*/
113135
function wp_render_elements_support_styles( $parsed_block ) {
114136
/*
@@ -129,9 +151,12 @@ function wp_render_elements_support_styles( $parsed_block ) {
129151
);
130152
}
131153

132-
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $parsed_block['blockName'] );
133-
$element_block_styles = $parsed_block['attrs']['style']['elements'] ?? null;
154+
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $parsed_block['blockName'] );
155+
if ( ! $block_type ) {
156+
return $parsed_block;
157+
}
134158

159+
$element_block_styles = $parsed_block['attrs']['style']['elements'] ?? null;
135160
if ( ! $element_block_styles ) {
136161
return $parsed_block;
137162
}
@@ -157,7 +182,7 @@ function wp_render_elements_support_styles( $parsed_block ) {
157182
return $parsed_block;
158183
}
159184

160-
$class_name = wp_get_elements_class_name( $parsed_block );
185+
$class_name = wp_get_elements_class_name();
161186
$updated_class_name = isset( $parsed_block['attrs']['className'] ) ? $parsed_block['attrs']['className'] . " $class_name" : $class_name;
162187

163188
_wp_array_set( $parsed_block, array( 'attrs', 'className' ), $updated_class_name );
@@ -197,7 +222,7 @@ function wp_render_elements_support_styles( $parsed_block ) {
197222
)
198223
);
199224

200-
if ( isset( $element_style_object[':hover'] ) ) {
225+
if ( isset( $element_style_object[':hover'], $element_config['hover_selector'] ) ) {
201226
wp_style_engine_get_styles(
202227
$element_style_object[':hover'],
203228
array(

wp-includes/version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @global string $wp_version
1818
*/
19-
$wp_version = '7.1-alpha-62519';
19+
$wp_version = '7.1-alpha-62520';
2020

2121
/**
2222
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

0 commit comments

Comments
 (0)