Make WordPress Core

Changeset 52261


Ignore:
Timestamp:
11/28/2021 01:51:23 PM (12 months ago)
Author:
audrasjb
Message:

Editor: Ensure block style name does not contain spaces before creating the class.

This change adds a check to ensure there is no spaces in block style variation names before generating the class. Returns false and a _doing_it_wrong() message if a space is detected.

Props amustaque97, costdev, hellofromtonya, pbiron, SergeyBiryukov, afragen.
Fixes #54296.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-block-styles-registry.php

    r52236 r52261  
    5959                __( 'Block style name must be a string.' ),
    6060                '5.3.0'
     61            );
     62            return false;
     63        }
     64
     65        if ( str_contains( $style_properties['name'], ' ' ) ) {
     66            _doing_it_wrong(
     67                __METHOD__,
     68                __( 'Block style name must not contain any spaces.' ),
     69                '5.9.0'
    6170            );
    6271            return false;
  • trunk/tests/phpunit/tests/blocks/register.php

    r52010 r52261  
    99
    1010/**
    11  * Tests for register_block_type(), unregister_block_type(), get_dynamic_block_names().
     11 * Tests for register_block_type(), unregister_block_type(), get_dynamic_block_names() and register_block_style().
    1212 *
    1313 * @since 5.0.0
     
    560560        $this->assertSame( 3, $result->api_version );
    561561    }
     562
     563    /**
     564     * Test case to validate `_doing_it_wrong()` when block style name attribute
     565     * contains one or more spaces.
     566     *
     567     * @dataProvider data_register_block_style_name_contain_spaces
     568     *
     569     * @ticket 54296
     570     *
     571     * @covers ::register_block_style
     572     *
     573     * @expectedIncorrectUsage WP_Block_Styles_Registry::register
     574     * @param array $block_styles Array of block styles to test.
     575     */
     576    public function test_register_block_style_name_contain_spaces( array $block_styles ) {
     577        register_block_style( 'core/query', $block_styles );
     578    }
     579
     580    /**
     581     * Data provider.
     582     *
     583     * @return array
     584     */
     585    public function data_register_block_style_name_contain_spaces() {
     586        return array(
     587            'multiple spaces' => array(
     588                array(
     589                    'name'  => 'style-class-1    style-class-2',
     590                    'label' => 'Custom Style Label',
     591                ),
     592            ),
     593            'single space'    => array(
     594                array(
     595                    'name'  => 'style-class-1 style-class-2',
     596                    'label' => 'Custom Style Label',
     597                ),
     598            ),
     599        );
     600    }
     601
     602    /**
     603     * Test case to validate no `_doing_it_wrong()` happens when there is
     604     * no empty space.
     605     *
     606     * @ticket 54296
     607     *
     608     * @covers ::register_block_style
     609     */
     610    public function test_register_block_style_name_without_spaces() {
     611        $block_styles = array(
     612            'name'  => 'style-class-1',
     613            'label' => 'Custom Style Label',
     614        );
     615
     616        $actual = register_block_style( 'core/query', $block_styles );
     617        $this->assertTrue( $actual );
     618    }
    562619}
Note: See TracChangeset for help on using the changeset viewer.