diff --git a/src/wp-includes/class-wp-block-styles-registry.php b/src/wp-includes/class-wp-block-styles-registry.php index 67d6998fc5..272e400f8c 100644 --- a/src/wp-includes/class-wp-block-styles-registry.php +++ b/src/wp-includes/class-wp-block-styles-registry.php @@ -62,6 +62,15 @@ final class WP_Block_Styles_Registry { return false; } + if ( str_contains( $style_properties['name'], ' ' ) ) { + _doing_it_wrong( + __METHOD__, + __( 'Block style name must not contain any spaces.' ), + '5.9.0' + ); + return false; + } + $block_style_name = $style_properties['name']; if ( ! isset( $this->registered_block_styles[ $block_name ] ) ) { diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 18463671d4..b9efb5ee68 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -8,7 +8,7 @@ */ /** - * Tests for register_block_type(), unregister_block_type(), get_dynamic_block_names(). + * Tests for register_block_type(), unregister_block_type(), get_dynamic_block_names() and register_block_style(). * * @since 5.0.0 * @@ -559,4 +559,61 @@ class Tests_Blocks_Register extends WP_UnitTestCase { $this->assertSame( 3, $result->api_version ); } + + /** + * Test case to validate `_doing_it_wrong()` when block style name attribute + * contains one or more spaces. + * + * @dataProvider data_register_block_style_name_contain_spaces + * + * @ticket 54296 + * + * @covers ::register_block_style + * + * @expectedIncorrectUsage WP_Block_Styles_Registry::register + * @param array $block_styles Array of block styles to test. + */ + public function test_register_block_style_name_contain_spaces( array $block_styles ) { + register_block_style( 'core/query', $block_styles ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_register_block_style_name_contain_spaces() { + return array( + 'multiple spaces' => array( + array( + 'name' => 'style-class-1 style-class-2', + 'label' => 'Custom Style Label', + ), + ), + 'single space' => array( + array( + 'name' => 'style-class-1 style-class-2', + 'label' => 'Custom Style Label', + ), + ), + ); + } + + /** + * Test case to validate no `_doing_it_wrong()` happens when there is + * no empty space. + * + * @ticket 54296 + * + * @covers ::register_block_style + */ + public function test_register_block_style_name_without_spaces() { + $block_styles = array( + 'name' => 'style-class-1', + 'label' => 'Custom Style Label', + ); + + $actual = register_block_style( 'core/query', $block_styles ); + $this->assertTrue( $actual ); + } }