Make WordPress Core

Changeset 56587


Ignore:
Timestamp:
09/14/2023 01:23:13 PM (3 years ago)
Author:
Bernhard Reiter
Message:

General: Add block_hooks field to block type registration, REST API.

In order to implement Block Hooks, we need to add a new block_hooks field to the WP_Block_Type class, as well as to block registration related functions, the block types REST API controller, etc.

Props gziolo.
Fixes #59346. See #59313.

Location:
trunk
Files:
8 edited

Legend:

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

    r56176 r56587  
    22082208 * @since 5.0.0
    22092209 * @since 6.3.0 Added `selectors` field.
     2210 * @since 6.4.0 Added `block_hooks` field.
    22102211 *
    22112212 * @return array An associative array of registered block data.
    … …  
    22222223                'provides_context' => 'providesContext',
    22232224                'uses_context'     => 'usesContext',
     2225                'block_hooks'      => 'blockHooks',
    22242226                'selectors'        => 'selectors',
    22252227                'supports'         => 'supports',
  • trunk/src/wp-includes/blocks.php

    r56560 r56587  
    343343 * @since 6.1.0 Added support for `render` field.
    344344 * @since 6.3.0 Added `selectors` field.
     345 * @since 6.4.0 Added support for `blockHooks` field.
    345346 *
    346347 * @param string $file_or_folder Path to the JSON file with metadata definition for
    … …  
    514515        }
    515516
     517        if ( ! empty( $metadata['blockHooks'] ) ) {
     518                /**
     519                 * Map camelCased position string (from block.json) to snake_cased block type position.
     520                 *
     521                 * @var array
     522                 */
     523                $position_mappings = array(
     524                        'before'     => 'before',
     525                        'after'      => 'after',
     526                        'firstChild' => 'first_child',
     527                        'lastChild'  => 'last_child',
     528                );
     529
     530                $settings['block_hooks'] = array();
     531                foreach ( $metadata['blockHooks'] as $anchor_block_name => $position ) {
     532                        // Avoid infinite recursion (hooking to itself).
     533                        if ( $metadata['name'] === $anchor_block_name ) {
     534                                _doing_it_wrong(
     535                                        __METHOD__,
     536                                        __( 'Cannot hook block to itself.', 'gutenberg' ),
     537                                        '6.4.0'
     538                                );
     539                                continue;
     540                        }
     541
     542                        if ( ! isset( $position_mappings[ $position ] ) ) {
     543                                continue;
     544                        }
     545
     546                        $settings['block_hooks'][ $anchor_block_name ] = $position_mappings[ $position ];
     547                }
     548        }
     549
    516550        if ( ! empty( $metadata['render'] ) ) {
    517551                $template_path = wp_normalize_path(
  • trunk/src/wp-includes/class-wp-block-type.php

    r56414 r56587  
    173173         */
    174174        public $provides_context = null;
     175
     176        /**
     177         * Block hooks for this block type.
     178         *
     179         * A block hook is specified by a block type and a relative position.
     180         * The hooked block will be automatically inserted in the given position
     181         * next to the "anchor" block whenever the latter is encountered.
     182         *
     183         * @since 6.4.0
     184         * @var array[]
     185         */
     186        public $block_hooks = array();
    175187
    176188        /**
    … …  
    255267         *              Deprecated the `editor_script`, `script`, `view_script`, `editor_style`, and `style` properties.
    256268         * @since 6.3.0 Added the `selectors` property.
     269         * @since 6.4.0 Added the `block_hooks` property.
    257270         *
    258271         * @see register_block_type()
    … …  
    285298         *     @type string[]      $uses_context             Context values inherited by blocks of this type.
    286299         *     @type string[]|null $provides_context         Context provided by blocks of this type.
     300         *     @type array[]       $block_hooks              Block hooks.
    287301         *     @type string[]      $editor_script_handles    Block type editor only script handles.
    288302         *     @type string[]      $script_handles           Block type front end and editor script handles.
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php

    r56586 r56587  
    292292                                'style_handles',
    293293                                'variations',
     294                                'block_hooks',
    294295                        ),
    295296                        $deprecated_fields
    … …  
    707708                                'keywords'              => $keywords_definition,
    708709                                'example'               => $example_definition,
     710                                'block_hooks'           => array(
     711                                        'description'       => __( 'This block is automatically inserted near any occurence of the block types used as keys of this map, into a relative position given by the corresponding value.' ),
     712                                        'type'              => 'object',
     713                                        'patternProperties' => array(
     714                                                '^[a-zA-Z0-9-]+/[a-zA-Z0-9-]+$' => array(
     715                                                        'type' => 'string',
     716                                                        'enum' => array( 'before', 'after', 'first_child', 'last_child' ),
     717                                                ),
     718                                        ),
     719                                        'default'           => array(),
     720                                        'context'           => array( 'embed', 'view', 'edit' ),
     721                                        'readonly'          => true,
     722                                ),
    709723                        ),
    710724                );
  • trunk/tests/phpunit/data/blocks/notice/block.json

    r55673 r56587  
    3030        "selectors": {
    3131                "root": ".wp-block-notice"
     32        },
     33        "blockHooks": {
     34                "core/post-content": "before"
    3235        },
    3336        "supports": {
  • trunk/tests/phpunit/tests/admin/includesPost.php

    r56559 r56587  
    919919                        'ancestor'        => array( 'core/test-ancestor' ),
    920920                        'selectors'       => array( 'root' => '.wp-block-test' ),
     921                        'block_hooks'     => array( 'core/post-content' => 'before' ),
    921922                );
    922923
    … …  
    938939                                ),
    939940                                'usesContext' => array(),
     941                                'blockHooks'  => array( 'core/post-content' => 'before' ),
    940942                                'selectors'   => array( 'root' => '.wp-block-test' ),
    941943                                'category'    => 'common',
  • trunk/tests/phpunit/tests/blocks/register.php

    r56559 r56587  
    645645                        'Block type should contain selectors from metadata.'
    646646                );
     647                // @ticket 59346
     648                $this->assertSame(
     649                        array( 'core/post-content' => 'before' ),
     650                        $result->block_hooks,
     651                        'Block type should contain block hooks from metadata.'
     652                );
    647653                $this->assertSame(
    648654                        array(
  • trunk/tests/phpunit/tests/rest-api/rest-block-type-controller.php

    r56548 r56587  
    196196         * @ticket 47620
    197197         * @ticket 57585
     198         * @ticket 59346
    198199         */
    199200        public function test_get_item_invalid() {
    … …  
    206207                        'provides_context' => 'invalid_provides_context',
    207208                        'uses_context'     => 'invalid_uses_context',
     209                        'block_hooks'      => 'invalid_block_hooks',
    208210                        'category'         => true,
    209211                        'editor_script'    => true,
    … …  
    245247                );
    246248                $this->assertSameSets( array( 'invalid_uses_context' ), $data['uses_context'] );
     249                $this->assertSameSets( array(), $data['block_hooks'], 'invalid block_hooks defaults to empty array' );
    247250                $this->assertSameSets( array( 'invalid_keywords' ), $data['keywords'] );
    248251                $this->assertSameSets( array( 'invalid_parent' ), $data['parent'] );
    … …  
    267270         * @ticket 47620
    268271         * @ticket 57585
     272         * @ticket 59346
    269273         */
    270274        public function test_get_item_defaults() {
    … …  
    277281                        'provides_context' => false,
    278282                        'uses_context'     => false,
     283                        'block_hooks'      => false,
    279284                        'category'         => false,
    280285                        'editor_script'    => false,
    … …  
    315320                );
    316321                $this->assertSameSets( array(), $data['provides_context'] );
     322                $this->assertSameSets( array(), $data['block_hooks'], 'block_hooks defaults to empty array' );
    317323                $this->assertSameSets( array(), $data['uses_context'] );
    318324                $this->assertSameSets( array(), $data['keywords'] );
    … …  
    539545         * @ticket 47620
    540546         * @ticket 57585
     547         * @ticket 59346
    541548         */
    542549        public function test_get_item_schema() {
    … …  
    546553                $data       = $response->get_data();
    547554                $properties = $data['schema']['properties'];
    548                 $this->assertCount( 29, $properties );
     555                $this->assertCount( 30, $properties );
    549556                $this->assertArrayHasKey( 'api_version', $properties );
    550557                $this->assertArrayHasKey( 'title', $properties );
    … …  
    569576                $this->assertArrayHasKey( 'uses_context', $properties );
    570577                $this->assertArrayHasKey( 'provides_context', $properties );
     578                $this->assertArrayHasKey( 'block_hooks', $properties );
    571579                $this->assertArrayHasKey( 'variations', $properties );
    572580                $this->assertArrayHasKey( 'ancestor', $properties );
    … …  
    665673         *
    666674         * @since 5.5.0
     675         * @since 6.4.0 Added the `block_hooks` extra field.
    667676         *
    668677         * @param WP_Block_Type $block_type Sample block type.
    … …  
    691700                        'provides_context',
    692701                        'uses_context',
     702                        'block_hooks',
    693703                        'supports',
    694704                        'styles',
Note: See TracChangeset for help on using the changeset viewer.