Make WordPress Core

Changeset 58186

Timestamp:
05/23/2024 06:33:11 PM (2 months ago)
Author:
Bernhard Reiter
Message:

Block Hooks API: Insert metadata at the same time as hooked blocks.

The Block Hooks UI relies on the ignoredHookedBlocks metadata when determining whether to show the toggle in the Site Editor. The problem is that for uncustomized templates we don't add this metadata.

Currently the visitor functions have a default callback of insert_hooked_blocks and we only add the metadata when we're writing to the database via an available filter in the template controller.

This changeset creates a new callback which both inserts the hooked blocks and adds the ignoredHookedBlocks metadata to the anchor block, and uses this new callback explicitly in the visitor functions that are run upon reading from the database.

We continue to set the ignoredHookedBlocks metadata when writing to the database, i.e. this operation happens twice. Although not ideal, this is necessary to cover the following scenarios:

  • When the user adds an anchor block within the editor, we still need to add the ignoredHookedBlocks meta to it to prevent hooked blocks hooking on to it unexpectedly on the frontend. This is required to keep parity between the frontend and editor.
  • When a user writes template data to the database directly through the API (instead of the editor), we need to again ensure we're not inserting hooked blocks unexpectedly.

It is worth noting that with this change, the first hooked block to insert relative to its anchor block will be accepted. Any additional blocks of the same type (e.g. a second core/loginout block) trying to hook onto the same anchor block will be ignored, irrespective of the position.

Props tomjcafferkey, bernhard-reiter, gziolo.
Fixes #59574.

Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template-utils.php

    r58084 r58186  
    599599    $hooked_blocks        = get_hooked_blocks();
    600600    if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
    601         $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template );
    602         $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $template );
     601        $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template );
     602        $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $template );
    603603    }
    604604    $blocks            = parse_blocks( $template->content );
     
    985985    $hooked_blocks = get_hooked_blocks();
    986986    if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
    987         $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template );
    988         $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $template );
     987        $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template );
     988        $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $template );
    989989        $blocks               = parse_blocks( $template->content );
    990990        $template->content    = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
  • trunk/src/wp-includes/blocks.php

    r58084 r58186  
    859859 * @access private
    860860 *
    861  * @param array                   $parsed_anchor_block The anchor block, in parsed block array format.
    862  * @param string                  $relative_position   The relative position of the hooked blocks.
    863  *                                                     Can be one of 'before', 'after', 'first_child', or 'last_child'.
    864  * @param array                   $hooked_blocks       An array of hooked block types, grouped by anchor block and relative position.
    865  * @param WP_Block_Template|array $context             The block template, template part, or pattern that the anchor block belongs to.
     861 * @param array                   $parsed_anchor_block The anchor block, in parsed block array format.
     862 * @param string                  $relative_position   The relative position of the hooked blocks.
     863 *                                                     Can be one of 'before', 'after', 'first_child', or 'last_child'.
     864 * @param array                   $hooked_blocks       An array of hooked block types, grouped by anchor block and relative position.
     865 * @param WP_Block_Template|array $context             The block template, template part, or pattern that the anchor block belongs to.
    866866 * @return string
    867867 */
     
    950950 * @access private
    951951 *
    952  * @param array                   $parsed_anchor_block The anchor block, in parsed block array format.
    953  * @param string                  $relative_position   The relative position of the hooked blocks.
    954  *                                                     Can be one of 'before', 'after', 'first_child', or 'last_child'.
    955  * @param array                   $hooked_blocks       An array of hooked block types, grouped by anchor block and relative position.
    956  * @param WP_Block_Template|array $context             The block template, template part, or pattern that the anchor block belongs to.
    957  * @return string An empty string.
     952 * @param array                   $parsed_anchor_block The anchor block, in parsed block array format.
     953 * @param string                  $relative_position   The relative position of the hooked blocks.
     954 *                                                     Can be one of 'before', 'after', 'first_child', or 'last_child'.
     955 * @param array                   $hooked_blocks       An array of hooked block types, grouped by anchor block and relative position.
     956 * @param WP_Block_Template|array $context             The block template, template part, or pattern that the anchor block belongs to.
     957 * @return string mpty string.
    958958 */
    959959function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) {
     
    10011001    // Markup for the hooked blocks has already been created (in `insert_hooked_blocks`).
    10021002    return '';
     1003
     1004
     1005
     1006
     1007
     1008
     1009
     1010
     1011
     1012
     1013
     1014
     1015
     1016
     1017
     1018
     1019
     1020
     1021
     1022
     1023
     1024
     1025
    10031026}
    10041027
  • trunk/src/wp-includes/class-wp-block-patterns-registry.php

    r57731 r58186  
    175175        $after_block_visitor  = null;
    176176        if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
    177             $before_block_visitor = make_before_block_visitor( $hooked_blocks, $pattern );
    178             $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $pattern );
     177            $before_block_visitor = make_before_block_visitor( $hooked_blocks, $pattern );
     178            $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $pattern );
    179179        }
    180180        $blocks  = parse_blocks( $content );
  • trunk/tests/phpunit/tests/blocks/getHookedBlocks.php

    r57627 r58186  
    152152        );
    153153        $this->assertStringContainsString(
    154             '<!-- wp:post-content {"layout":{"type":"constrained"}} /-->'
     154            '<!-- wp:post-content {"layout":{"type":"constrained"}} /-->'
    155155            . '<!-- wp:tests/hooked-after /-->',
    156156            $template->content
     
    181181        $this->assertStringContainsString(
    182182            '<!-- wp:tests/hooked-before /-->'
    183             . '<!-- wp:navigation {"layout":{"type":"flex","setCascadingProperties":true,"justifyContent":"right"}} /-->',
     183            . '<!-- wp:navigation {"layout":{"type":"flex","setCascadingProperties":true,"justifyContent":"right"}} /-->',
    184184            $template->content
    185185        );
     
    222222        );
    223223        $this->assertStringContainsString(
    224             '<!-- wp:comments -->'
     224            '<!-- wp:comments -->'
    225225            . '<div class="wp-block-comments">'
    226226            . '<!-- wp:tests/hooked-first-child /-->',
Note: See TracChangeset for help on using the changeset viewer.