Make WordPress Core

Changeset 57521


Ignore:
Timestamp:
02/02/2024 12:59:21 PM (15 months ago)
Author:
gziolo
Message:

Editor: Add allowed_blocks field to block registration and REST API

There is a new block.json field called allowedBlocks, added in Gutenberg in https://github.com/WordPress/gutenberg/pull/58262. This adds support for this new field also on the server.

Props: gziolo, jsnajdr.
Fixes #60403.

Location:
trunk
Files:
5 edited

Legend:

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

    r57239 r57521  
    23042304        'example'          => 'example',
    23052305        'variations'       => 'variations',
     2306        'allowed_blocks'   => 'allowedBlocks',
    23062307    );
    23072308
  • trunk/src/wp-includes/blocks.php

    r57493 r57521  
    328328 * @since 6.3.0 Added `selectors` field.
    329329 * @since 6.4.0 Added support for `blockHooks` field.
    330  * @since 6.5.0 Added support for `viewStyle` field.
     330 * @since 6.5.0 Added support for `allowedBlocks` and `viewStyle` fields.
    331331 *
    332332 * @param string $file_or_folder Path to the JSON file with metadata definition for
     
    425425        'variations'      => 'variations',
    426426        'example'         => 'example',
     427        'allowedBlocks'   => 'allowed_blocks',
    427428    );
    428429    $textdomain        = ! empty( $metadata['textdomain'] ) ? $metadata['textdomain'] : null;
  • trunk/src/wp-includes/class-wp-block-type.php

    r57493 r57521  
    6868     */
    6969    public $ancestor = null;
     70
     71    /**
     72     * Limits which block types can be inserted as children of this block type.
     73     *
     74     * @since 6.5.0
     75     * @var string[]|null
     76     */
     77    public $allowed_blocks = null;
    7078
    7179    /**
     
    304312     *     @type string[]|null $ancestor                 Setting ancestor makes a block available only inside the specified
    305313     *                                                   block types at any position of the ancestor's block subtree.
     314     *     @type string[]|null $allowed_blocks           Limits which block types can be inserted as children of this block type.
    306315     *     @type string|null   $icon                     Block type icon.
    307316     *     @type string        $description              A detailed block type description.
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php

    r57493 r57521  
    281281                'parent',
    282282                'ancestor',
     283                'allowed_blocks',
    283284                'provides_context',
    284285                'uses_context',
     
    724725                    'readonly'    => true,
    725726                ),
     727                'allowed_blocks'        => array(
     728                    'description' => __( 'Allowed child block types.' ),
     729                    'type'        => array( 'array', 'null' ),
     730                    'items'       => array(
     731                        'type'    => 'string',
     732                        'pattern' => self::NAME_PATTERN,
     733                    ),
     734                    'default'     => null,
     735                    'context'     => array( 'embed', 'view', 'edit' ),
     736                    'readonly'    => true,
     737                ),
    726738                'keywords'              => $keywords_definition,
    727739                'example'               => $example_definition,
  • trunk/tests/phpunit/tests/rest-api/rest-block-type-controller.php

    r57493 r57521  
    208208            'parent'           => 'invalid_parent',
    209209            'ancestor'         => 'invalid_ancestor',
     210            'allowed_blocks'   => 'invalid_allowed_blocks',
    210211            'icon'             => true,
    211212            'description'      => true,
     
    238239        $this->assertSameSets( array( 'invalid_parent' ), $data['parent'] );
    239240        $this->assertSameSets( array( 'invalid_ancestor' ), $data['ancestor'] );
     241        $this->assertSameSets( array( 'invalid_allowed_blocks' ), $data['allowed_blocks'] );
    240242        $this->assertNull( $data['icon'] );
    241243        $this->assertSame( '1', $data['description'] );
     
    284286            'parent'           => false,
    285287            'ancestor'         => false,
     288            'allowed_blocks'   => false,
    286289            'icon'             => false,
    287290            'description'      => false,
     
    314317        $this->assertSameSets( array(), $data['parent'] );
    315318        $this->assertSameSets( array(), $data['ancestor'] );
     319        $this->assertSameSets( array(), $data['allowed_blocks'] );
    316320        $this->assertNull( $data['icon'] );
    317321        $this->assertSame( '', $data['description'] );
     
    551555     * @ticket 57585
    552556     * @ticket 59346
     557     * @ticket 60403
    553558     */
    554559    public function test_get_item_schema() {
     
    558563        $data       = $response->get_data();
    559564        $properties = $data['schema']['properties'];
    560         $this->assertCount( 31, $properties );
     565        $this->assertCount( 32, $properties );
    561566        $this->assertArrayHasKey( 'api_version', $properties );
    562567        $this->assertArrayHasKey( 'name', $properties );
     
    565570        $this->assertArrayHasKey( 'parent', $properties );
    566571        $this->assertArrayHasKey( 'ancestor', $properties );
     572        $this->assertArrayHasKey( 'allowed_blocks', $properties );
    567573        $this->assertArrayHasKey( 'icon', $properties );
    568574        $this->assertArrayHasKey( 'description', $properties );
     
    697703            'parent',
    698704            'ancestor',
     705            'allowedBlocks',
    699706            'icon',
    700707            'description',
Note: See TracChangeset for help on using the changeset viewer.