| @@ -9,10 +9,10 @@ | ||
| 9 | 9 | * Update the block content with elements class names. |
| 10 | 10 | * |
| 11 | 11 | * @deprecated 6.6.0 Use `gutenberg_render_elements_class_name` instead. |
| 12 | 12 | * |
| 13 | - * @param string $block_content Rendered block content. | |
| 14 | - * @return string Filtered block content. | |
| 13 | + * @param string $block_content Rendered block content. | |
| 14 | + * @return string Filtered block content. | |
| 15 | 15 | */ |
| 16 | 16 | function gutenberg_render_elements_support( $block_content ) { |
| 17 | 17 | _deprecated_function( __FUNCTION__, '6.6.0', 'gutenberg_render_elements_class_name' ); |
| 18 | 18 | return $block_content; |
| @@ -20,12 +20,12 @@ | ||
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | 22 | * Determines whether an elements class name should be added to the block. |
| 23 | 23 | * |
| 24 | - * @param array $block Block object. | |
| 25 | - * @param array $options Per element type options e.g. whether to skip serialization. | |
| 24 | + * @param array $block Block object. | |
| 25 | + * @param array $options Per element type options e.g. whether to skip serialization. | |
| 26 | 26 | * |
| 27 | - * @return boolean Whether the block needs an elements class name. | |
| 27 | + * @return boolean Whether the block needs an elements class name. | |
| 28 | 28 | */ |
| 29 | 29 | function gutenberg_should_add_elements_class_name( $block, $options ) { |
| 30 | 30 | if ( ! isset( $block['attrs']['style']['elements'] ) ) { |
| 31 | 31 | return false; |
| @@ -192,9 +192,9 @@ | ||
| 192 | 192 | 'context' => 'block-supports', |
| 193 | 193 | ) |
| 194 | 194 | ); |
| 195 | 195 | |
| 196 | - if ( isset( $element_style_object[':hover'] ) ) { | |
| 196 | + if ( isset( $element_style_object[':hover'], $element_config['hover_selector'] ) ) { | |
| 197 | 197 | gutenberg_style_engine_get_styles( |
| 198 | 198 | $element_style_object[':hover'], |
| 199 | 199 | array( |
| 200 | 200 | 'selector' => $element_config['hover_selector'], |
| @@ -231,25 +231,45 @@ | ||
| 231 | 231 | * block's markup. |
| 232 | 232 | * |
| 233 | 233 | * @see gutenberg_render_elements_support_styles |
| 234 | 234 | * |
| 235 | - * @param string $block_content Rendered block content. | |
| 236 | - * @param array $block Block object. | |
| 235 | + * @param string $block_content Rendered block content. | |
| 236 | + * @param array $block Block object. | |
| 237 | + * @return string Filtered block content. | |
| 237 | 238 | * |
| 238 | - * @return string Filtered block content. | |
| 239 | + * @phpstan-param array{ | |
| 240 | + * attrs: array{ | |
| 241 | + * className?: string, | |
| 242 | + * ... | |
| 243 | + * }, | |
| 244 | + * ... | |
| 245 | + * } $block | |
| 239 | 246 | */ |
| 240 | 247 | function gutenberg_render_elements_class_name( $block_content, $block ) { |
| 241 | - $class_string = $block['attrs']['className'] ?? ''; | |
| 242 | - preg_match( '/\bwp-elements-\S+\b/', $class_string, $matches ); | |
| 248 | + $class_name_attr = $block['attrs']['className'] ?? null; | |
| 249 | + $class_name_prefix = 'wp-elements-'; | |
| 250 | + if ( ! is_string( $class_name_attr ) || ! str_contains( $class_name_attr, $class_name_prefix ) ) { | |
| 251 | + return $block_content; | |
| 252 | + } | |
| 243 | 253 | |
| 244 | - if ( empty( $matches ) ) { | |
| 254 | + // Parse out the 'wp-elements-*' class name. | |
| 255 | + $matched_class_name = null; | |
| 256 | + $token_delimiter = " \t\f\r\n"; | |
| 257 | + $class_token = strtok( $class_name_attr, $token_delimiter ); | |
| 258 | + while ( false !== $class_token ) { | |
| 259 | + if ( str_starts_with( $class_token, $class_name_prefix ) ) { | |
| 260 | + $matched_class_name = $class_token; | |
| 261 | + break; | |
| 262 | + } | |
| 263 | + $class_token = strtok( $token_delimiter ); | |
| 264 | + } | |
| 265 | + if ( null === $matched_class_name ) { | |
| 245 | 266 | return $block_content; |
| 246 | 267 | } |
| 247 | 268 | |
| 248 | 269 | $tags = new WP_HTML_Tag_Processor( $block_content ); |
| 249 | - | |
| 250 | 270 | if ( $tags->next_tag() ) { |
| 251 | - $tags->add_class( $matches[0] ); | |
| 271 | + $tags->add_class( $matched_class_name ); | |
| 252 | 272 | } |
| 253 | 273 | |
| 254 | 274 | return $tags->get_updated_html(); |
| 255 | 275 | } |