From bdfbb504b75fc6478a7031cdfe8a00c372b43e30 Mon Sep 17 00:00:00 2001 From: Marin Atanasov <8436925+tyxla@users.noreply.github.com> Date: Tue, 14 Jul 2026 12:01:03 +0300 Subject: [PATCH] Block Supports: Improve handling of block class name to avoid fatal --- .../block-supports/block-style-variations.php | 7 ++- src/wp-includes/block-supports/layout.php | 4 +- .../block-supports/block-style-variations.php | 43 +++++++++++++++++++ tests/phpunit/tests/block-supports/layout.php | 25 +++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/block-supports/block-style-variations.php b/src/wp-includes/block-supports/block-style-variations.php index f508003417f2c..5daccd629c055 100644 --- a/src/wp-includes/block-supports/block-style-variations.php +++ b/src/wp-includes/block-supports/block-style-variations.php @@ -223,11 +223,16 @@ function wp_render_block_style_variation_class_name( $block_content, $block ) { return $block_content; } + $block_class_name = $block['attrs']['className']; + if ( ! is_string( $block_class_name ) ) { + return $block_content; + } + /* * Matches a class prefixed by `is-style`, followed by the * variation slug, then `--`, and finally an instance number. */ - preg_match( '/\bis-style-(\S+?--\d+)\b/', $block['attrs']['className'], $matches ); + preg_match( '/\bis-style-(\S+?--\d+)\b/', $block_class_name, $matches ); if ( empty( $matches ) ) { return $block_content; diff --git a/src/wp-includes/block-supports/layout.php b/src/wp-includes/block-supports/layout.php index 158ae68fb3c59..105f4e28ecc1c 100644 --- a/src/wp-includes/block-supports/layout.php +++ b/src/wp-includes/block-supports/layout.php @@ -1160,7 +1160,9 @@ function wp_render_layout_support_flag( $block_content, $block ) { // Check if the block has an active style variation with a blockGap value. // Only check the registry if the className contains a variation class to avoid unnecessary lookups. $variation_block_gap_value = null; - $block_class_name = $block['attrs']['className'] ?? ''; + $block_class_name = is_string( $block['attrs']['className'] ?? null ) + ? $block['attrs']['className'] + : ''; if ( $block_class_name && str_contains( $block_class_name, 'is-style-' ) && $block_name ) { $styles_registry = WP_Block_Styles_Registry::get_instance(); $registered_styles = $styles_registry->get_registered_styles_for_block( $block_name ); diff --git a/tests/phpunit/tests/block-supports/block-style-variations.php b/tests/phpunit/tests/block-supports/block-style-variations.php index 0c56d0531829b..0627820bd8d4d 100644 --- a/tests/phpunit/tests/block-supports/block-style-variations.php +++ b/tests/phpunit/tests/block-supports/block-style-variations.php @@ -320,4 +320,47 @@ public function test_block_style_variation_ref_values() { $this->assertSameSetsWithIndex( $expected, $variation_data, 'Variation data with resolved ref values does not match' ); } + + /** + * Tests that a non-string `className` attribute does not cause a fatal + * error and the block content is returned unmodified. + * + * @covers ::wp_render_block_style_variation_class_name + */ + public function test_block_style_variation_class_name_with_non_string_class_name() { + $block = array( + 'blockName' => 'core/paragraph', + 'attrs' => array( + 'className' => array( '0', '1' ), + ), + ); + + $block_content = "

Test

\n"; + + $this->assertSame( + $block_content, + wp_render_block_style_variation_class_name( $block_content, $block ), + 'Block content should be returned unchanged when className is not a string' + ); + } + + /** + * Tests to ensure that there are no references to an undefined array key + * if `className` is not assigned. + * + * @covers ::wp_render_block_style_variation_class_name + */ + public function test_block_style_variation_class_name_with_missing_class_name() { + $block = array( + 'blockName' => 'core/paragraph', + 'attrs' => array(), + ); + + $block_content = "

Test

\n"; + + $this->assertSame( + $block_content, + wp_render_block_style_variation_class_name( $block_content, $block ) + ); + } } diff --git a/tests/phpunit/tests/block-supports/layout.php b/tests/phpunit/tests/block-supports/layout.php index c52c0aca656a1..95a8577730e21 100644 --- a/tests/phpunit/tests/block-supports/layout.php +++ b/tests/phpunit/tests/block-supports/layout.php @@ -1053,4 +1053,29 @@ public function data_get_block_style_variation_name_from_registered_style() { ), ); } + + /** + * Tests that a non-string `className` attribute does not cause a fatal + * when checking for style variation layout styles. + * + * @covers ::wp_render_layout_support_flag + */ + public function test_layout_support_flag_with_non_string_class_name() { + $block_content = '
'; + $block = array( + 'blockName' => 'core/group', + 'attrs' => array( + 'className' => array( '0', '1' ), + 'layout' => array( + 'type' => 'constrained', + ), + ), + ); + + $this->assertSame( + '
', + wp_render_layout_support_flag( $block_content, $block ), + 'Layout support should render the expected markup when className is not a string' + ); + } }