Make WordPress Core

Changeset 55673


Ignore:
Timestamp:
04/21/2023 10:41:58 AM (17 months ago)
Author:
gziolo
Message:

Editor: Add selectors field to block type definition

Adds support for the new selectors property for block types. It adds it to the allowed metadata when registering a block type, makes the WP_Block_Type class aware of it, exposes it through the block types REST API, and the get_block_editor_server_block_settings function.

Corresponding work in the Gutenberg plugin: https://github.com/WordPress/gutenberg/pull/46496.

Fixes #57585.
Props aaronrobertshaw, hellofromTonya.

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/post.php

    r55365 r55673  
    21792179 *
    21802180 * @since 5.0.0
     2181 * @since 6.3.0 Added `selectors` field.
    21812182 *
    21822183 * @return array An associative array of registered block data.
     
    21932194        'provides_context' => 'providesContext',
    21942195        'uses_context'     => 'usesContext',
     2196        'selectors'        => 'selectors',
    21952197        'supports'         => 'supports',
    21962198        'category'         => 'category',
  • trunk/src/wp-includes/blocks.php

    r55641 r55673  
    301301 * @since 5.9.0 Added support for `variations` and `viewScript` fields.
    302302 * @since 6.1.0 Added support for `render` field.
     303 * @since 6.3.0 Added `selectors` field.
    303304 *
    304305 * @param string $file_or_folder Path to the JSON file with metadata definition for
     
    383384        'providesContext' => 'provides_context',
    384385        'usesContext'     => 'uses_context',
     386        'selectors'       => 'selectors',
    385387        'supports'        => 'supports',
    386388        'styles'          => 'styles',
  • trunk/src/wp-includes/class-wp-block-type.php

    r54895 r55673  
    117117     */
    118118    public $variations = array();
     119
     120    /**
     121     * Custom CSS selectors for theme.json style generation.
     122     *
     123     * @since 6.3.0
     124     * @var array
     125     */
     126    public $selectors = array();
    119127
    120128    /**
     
    246254     *              `editor_style_handles`, and `style_handles` properties.
    247255     *              Deprecated the `editor_script`, `script`, `view_script`, `editor_style`, and `style` properties.
     256     * @since 6.3.0 Added the `selectors` property.
    248257     *
    249258     * @see register_block_type()
     
    269278     *     @type array[]       $styles                   Alternative block styles.
    270279     *     @type array[]       $variations               Block variations.
     280     *     @type array         $selectors                Custom CSS selectors for theme.json style generation.
    271281     *     @type array|null    $supports                 Supported features.
    272282     *     @type array|null    $example                  Structured data for the block preview.
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php

    r54670 r55673  
    238238     * @since 5.5.0
    239239     * @since 5.9.0 Renamed `$block_type` to `$item` to match parent class for PHP 8 named parameter support.
     240     * @since 6.3.0 Added `selectors` field.
    240241     *
    241242     * @param WP_Block_Type   $item    Block type data.
     
    279280                'provides_context',
    280281                'uses_context',
     282                'selectors',
    281283                'supports',
    282284                'styles',
     
    380382     *
    381383     * @since 5.5.0
     384     * @since 6.3.0 Added `selectors` field.
    382385     *
    383386     * @return array Item schema data.
     
    516519                        'type' => 'string',
    517520                    ),
     521                    'context'     => array( 'embed', 'view', 'edit' ),
     522                    'readonly'    => true,
     523                ),
     524                'selectors'             => array(
     525                    'description' => __( 'Custom CSS selectors.' ),
     526                    'type'        => 'object',
     527                    'default'     => array(),
     528                    'properties'  => array(),
    518529                    'context'     => array( 'embed', 'view', 'edit' ),
    519530                    'readonly'    => true,
  • trunk/tests/phpunit/data/blocks/notice/block.json

    r54155 r55673  
    2727            "type": "string"
    2828        }
     29    },
     30    "selectors": {
     31        "root": ".wp-block-notice"
    2932    },
    3033    "supports": {
  • trunk/tests/phpunit/tests/admin/includesPost.php

    r54244 r55673  
    827827            'render_callback' => 'foo',
    828828            'ancestor'        => array( 'core/test-ancestor' ),
     829            'selectors'       => array( 'root' => '.wp-block-test' ),
    829830        );
    830831
     
    846847                ),
    847848                'usesContext' => array(),
     849                'selectors'   => array( 'root' => '.wp-block-test' ),
    848850                'category'    => 'common',
    849851                'styles'      => array(),
  • trunk/tests/phpunit/tests/blocks/register.php

    r55486 r55673  
    484484     * @ticket 50263
    485485     * @ticket 50328
     486     * @ticket 57585
    486487     */
    487488    public function test_block_registers_with_metadata_fixture() {
     
    516517        );
    517518        $this->assertSameSets( array( 'groupId' ), $result->uses_context );
     519        // @ticket 57585
     520        $this->assertSame(
     521            array( 'root' => '.wp-block-notice' ),
     522            $result->selectors,
     523            'Block type should contain selectors from metadata.'
     524        );
    518525        $this->assertSame(
    519526            array(
  • trunk/tests/phpunit/tests/rest-api/rest-block-type-controller.php

    r55457 r55673  
    197197    /**
    198198     * @ticket 47620
     199     * @ticket 57585
    199200     */
    200201    public function test_get_item_invalid() {
     
    217218            'parent'           => 'invalid_parent',
    218219            'ancestor'         => 'invalid_ancestor',
     220            'selectors'        => 'invalid_selectors',
    219221            'supports'         => 'invalid_supports',
    220222            'styles'           => array(),
     
    248250        $this->assertSameSets( array( 'invalid_parent' ), $data['parent'] );
    249251        $this->assertSameSets( array( 'invalid_ancestor' ), $data['ancestor'] );
     252        $this->assertSameSets( array(), $data['selectors'], 'invalid selectors defaults to empty array' );
    250253        $this->assertSameSets( array(), $data['supports'] );
    251254        $this->assertSameSets( array(), $data['styles'] );
     
    266269    /**
    267270     * @ticket 47620
     271     * @ticket 57585
    268272     */
    269273    public function test_get_item_defaults() {
     
    285289            'parent'           => false,
    286290            'ancestor'         => false,
     291            'selectors'        => false,
    287292            'supports'         => false,
    288293            'styles'           => false,
     
    317322        $this->assertSameSets( array(), $data['parent'] );
    318323        $this->assertSameSets( array(), $data['ancestor'] );
     324        $this->assertSameSets( array(), $data['selectors'], 'selectors defaults to empty array' );
    319325        $this->assertSameSets( array(), $data['supports'] );
    320326        $this->assertSameSets( array(), $data['styles'] );
     
    535541    /**
    536542     * @ticket 47620
     543     * @ticket 57585
    537544     */
    538545    public function test_get_item_schema() {
     
    542549        $data       = $response->get_data();
    543550        $properties = $data['schema']['properties'];
    544         $this->assertCount( 28, $properties );
     551        $this->assertCount( 29, $properties );
    545552        $this->assertArrayHasKey( 'api_version', $properties );
    546553        $this->assertArrayHasKey( 'title', $properties );
     
    552559        $this->assertArrayHasKey( 'name', $properties );
    553560        $this->assertArrayHasKey( 'attributes', $properties );
     561        $this->assertArrayHasKey( 'selectors', $properties, 'schema must contain selectors' );
    554562        $this->assertArrayHasKey( 'supports', $properties );
    555563        $this->assertArrayHasKey( 'category', $properties );
Note: See TracChangeset for help on using the changeset viewer.