Make WordPress Core


Ignore:
Timestamp:
09/21/2023 04:16:05 PM (5 months ago)
Author:
Bernhard Reiter
Message:

Blocks: Implement automatic block insertion into Block Hooks.

Block Hooks allow a third-party block to specify a position relative to a given block into which it will then be automatically inserted (e.g. a "Like" button block can ask to be inserted after the Post Content block, or an eCommerce shopping cart block can ask to be inserted after the Navigation block).

The underlying idea is to provide an extensibility mechanism for Block Themes, in analogy to WordPress' Hooks concept that has allowed extending Classic Themes through filters and actions.

The two core tenets for Block Hooks are:

  1. Insertion into the frontend should happen right after a plugin containing a hooked block is activated (i.e. the user isn't required to insert the block manually in the editor first); similarly, disabling the plugin should remove the hooked block from the frontend.
  2. The user has the ultimate power to customize that automatic insertion: The hooked block is also visible in the editor, and the user's decision to persist, dismiss (i.e. remove), customize, or move it will be respected (and reflected on the frontend).

To account for both tenets, the tradeoff was made to limit automatic block insertion to unmodified templates (and template parts, respectively). The reason for this is that the simplest way of storing the information whether a block has been persisted to (or dismissed from) a given template (or part) is right in the template markup.

To accommodate for that tradeoff, UI controls (toggles) are being added to increase visibility of hooked blocks, and to allow for their later insertion into templates (or parts) that already have been modified by the user.

For hooked blocks to appear both in the frontend and in the editor (see tenet number 2), they need to be inserted into both the frontend markup and the REST API (templates and patterns endpoints) equally. As a consequence, this means that automatic insertion couldn't (only) be implemented at block render stage, as for the editor, the serialized (but unrendered) markup needs to be modified.

Furthermore, hooked blocks also have to be inserted into block patterns. Since practically no filters exist for the patterns registry, this has to be done in the registry's get_registered and get_all_registered methods.

Props gziolo.
Fixes #59313.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/blocks/serialize.php

    r56644 r56649  
    6363     * @covers ::traverse_and_serialize_blocks
    6464     */
    65     public function test_traverse_and_serialize_blocks() {
     65    public function test_traverse_and_serialize_blocks_pre_callback_modifies_current_block() {
    6666        $markup = "<!-- wp:outer --><!-- wp:inner {\"key\":\"value\"} -->Example.<!-- /wp:inner -->\n\nExample.\n\n<!-- wp:void /--><!-- /wp:outer -->";
    6767        $blocks = parse_blocks( $markup );
     
    8282
    8383    /**
     84     * @ticket 59313
     85     *
     86     * @covers ::traverse_and_serialize_blocks
     87     */
     88    public function test_traverse_and_serialize_blocks_pre_callback_prepends_to_inner_block() {
     89        $markup = "<!-- wp:outer --><!-- wp:inner {\"key\":\"value\"} -->Example.<!-- /wp:inner -->\n\nExample.\n\n<!-- wp:void /--><!-- /wp:outer -->";
     90        $blocks = parse_blocks( $markup );
     91
     92        $actual = traverse_and_serialize_blocks( $blocks, array( __CLASS__, 'insert_next_to_inner_block_callback' ) );
     93
     94        $this->assertSame(
     95            "<!-- wp:outer --><!-- wp:tests/inserted-block /--><!-- wp:inner {\"key\":\"value\"} -->Example.<!-- /wp:inner -->\n\nExample.\n\n<!-- wp:void /--><!-- /wp:outer -->",
     96            $actual
     97        );
     98    }
     99
     100    /**
     101     * @ticket 59313
     102     *
     103     * @covers ::traverse_and_serialize_blocks
     104     */
     105    public function test_traverse_and_serialize_blocks_post_callback_appends_to_inner_block() {
     106        $markup = "<!-- wp:outer --><!-- wp:inner {\"key\":\"value\"} -->Example.<!-- /wp:inner -->\n\nExample.\n\n<!-- wp:void /--><!-- /wp:outer -->";
     107        $blocks = parse_blocks( $markup );
     108
     109        $actual = traverse_and_serialize_blocks( $blocks, null, array( __CLASS__, 'insert_next_to_inner_block_callback' ) );
     110
     111        $this->assertSame(
     112            "<!-- wp:outer --><!-- wp:inner {\"key\":\"value\"} -->Example.<!-- /wp:inner --><!-- wp:tests/inserted-block /-->\n\nExample.\n\n<!-- wp:void /--><!-- /wp:outer -->",
     113            $actual
     114        );
     115    }
     116
     117    public static function insert_next_to_inner_block_callback( $block ) {
     118        if ( 'core/inner' !== $block['blockName'] ) {
     119            return '';
     120        }
     121
     122        return get_comment_delimited_block_content( 'tests/inserted-block', array(), '' );
     123    }
     124
     125    /**
     126     * @ticket 59313
     127     *
     128     * @covers ::traverse_and_serialize_blocks
     129     */
     130    public function test_traverse_and_serialize_blocks_pre_callback_prepends_to_child_blocks() {
     131        $markup = "<!-- wp:outer --><!-- wp:inner {\"key\":\"value\"} -->Example.<!-- /wp:inner -->\n\nExample.\n\n<!-- wp:void /--><!-- /wp:outer -->";
     132        $blocks = parse_blocks( $markup );
     133
     134        $actual = traverse_and_serialize_blocks( $blocks, array( __CLASS__, 'insert_next_to_child_blocks_callback' ) );
     135
     136        $this->assertSame(
     137            "<!-- wp:outer --><!-- wp:tests/inserted-block {\"parent\":\"core/outer\"} /--><!-- wp:inner {\"key\":\"value\"} -->Example.<!-- /wp:inner -->\n\nExample.\n\n<!-- wp:tests/inserted-block {\"parent\":\"core/outer\"} /--><!-- wp:void /--><!-- /wp:outer -->",
     138            $actual
     139        );
     140    }
     141
     142    /**
     143     * @ticket 59313
     144     *
     145     * @covers ::traverse_and_serialize_blocks
     146     */
     147    public function test_traverse_and_serialize_blocks_post_callback_appends_to_child_blocks() {
     148        $markup = "<!-- wp:outer --><!-- wp:inner {\"key\":\"value\"} -->Example.<!-- /wp:inner -->\n\nExample.\n\n<!-- wp:void /--><!-- /wp:outer -->";
     149        $blocks = parse_blocks( $markup );
     150
     151        $actual = traverse_and_serialize_blocks( $blocks, null, array( __CLASS__, 'insert_next_to_child_blocks_callback' ) );
     152
     153        $this->assertSame(
     154            "<!-- wp:outer --><!-- wp:inner {\"key\":\"value\"} -->Example.<!-- /wp:inner --><!-- wp:tests/inserted-block {\"parent\":\"core/outer\"} /-->\n\nExample.\n\n<!-- wp:void /--><!-- wp:tests/inserted-block {\"parent\":\"core/outer\"} /--><!-- /wp:outer -->",
     155            $actual
     156        );
     157    }
     158
     159    public static function insert_next_to_child_blocks_callback( $block, $parent_block ) {
     160        if ( ! isset( $parent_block ) ) {
     161            return '';
     162        }
     163
     164        return get_comment_delimited_block_content(
     165            'tests/inserted-block',
     166            array(
     167                'parent' => $parent_block['blockName'],
     168            ),
     169            ''
     170        );
     171    }
     172
     173    /**
     174     * @ticket 59313
     175     *
     176     * @covers ::traverse_and_serialize_blocks
     177     */
     178    public function test_traverse_and_serialize_blocks_pre_callback_prepends_if_prev_block() {
     179        $markup = "<!-- wp:outer --><!-- wp:inner {\"key\":\"value\"} -->Example.<!-- /wp:inner -->\n\nExample.\n\n<!-- wp:void /--><!-- /wp:outer -->";
     180        $blocks = parse_blocks( $markup );
     181
     182        $actual = traverse_and_serialize_blocks( $blocks, array( __CLASS__, 'insert_next_to_if_prev_or_next_block_callback' ) );
     183
     184        $this->assertSame(
     185            "<!-- wp:outer --><!-- wp:inner {\"key\":\"value\"} -->Example.<!-- /wp:inner -->\n\nExample.\n\n<!-- wp:tests/inserted-block {\"prev_or_next\":\"core/inner\"} /--><!-- wp:void /--><!-- /wp:outer -->",
     186            $actual
     187        );
     188    }
     189
     190    /**
     191     * @ticket 59313
     192     *
     193     * @covers ::traverse_and_serialize_blocks
     194     */
     195    public function test_traverse_and_serialize_blocks_post_callback_appends_if_prev_block() {
     196        $markup = "<!-- wp:outer --><!-- wp:inner {\"key\":\"value\"} -->Example.<!-- /wp:inner -->\n\nExample.\n\n<!-- wp:void /--><!-- /wp:outer -->";
     197        $blocks = parse_blocks( $markup );
     198
     199        $actual = traverse_and_serialize_blocks( $blocks, null, array( __CLASS__, 'insert_next_to_if_prev_or_next_block_callback' ) );
     200
     201        $this->assertSame(
     202            "<!-- wp:outer --><!-- wp:inner {\"key\":\"value\"} -->Example.<!-- /wp:inner --><!-- wp:tests/inserted-block {\"prev_or_next\":\"core/void\"} /-->\n\nExample.\n\n<!-- wp:void /--><!-- /wp:outer -->",
     203            $actual
     204        );
     205    }
     206
     207    public static function insert_next_to_if_prev_or_next_block_callback( $block, $parent_block, $prev_or_next ) {
     208        if ( ! isset( $prev_or_next ) ) {
     209            return '';
     210        }
     211
     212        return get_comment_delimited_block_content(
     213            'tests/inserted-block',
     214            array(
     215                'prev_or_next' => $prev_or_next['blockName'],
     216            ),
     217            ''
     218        );
     219    }
     220
     221    /**
    84222     * @ticket 59327
    85223     * @ticket 59412
Note: See TracChangeset for help on using the changeset viewer.