Make WordPress Core


Ignore:
Timestamp:
05/23/2024 06:33:11 PM (10 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.

File:
1 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, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
     602        $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
    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, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
     988        $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
    989989        $blocks               = parse_blocks( $template->content );
    990990        $template->content    = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
Note: See TracChangeset for help on using the changeset viewer.