Make WordPress Core


Ignore:
Timestamp:
02/13/2024 03:10:21 PM (3 months ago)
Author:
Bernhard Reiter
Message:

Block Hooks: Set ignoredHookedBlocks metadata upon saving.

Decouple hooked blocks insertion from setting the metadata.ignoredHookedBlocks attribute on anchor blocks, and perform the latter step upon saving a template or template part to the database. This ensures that anchor blocks that have been newly added to a template (or part) on the client side will also get ignoredHookedBlocks metadata set correctly, thus preserving editor/front-end parity. Hooked block insertion, on the other hand, will continue to happen upon reading a template (or part).

Props gziolo, tomjcafferkey.
Fixes #60506.

File:
1 edited

Legend:

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

    r57590 r57627  
    852852
    853853/**
    854  * Conditionally returns the markup for a given hooked block.
    855  *
    856  * Accepts three arguments: A hooked block, its type, and a reference to an anchor block.
    857  * If the anchor block has already been processed, and the given hooked block type is in the list
    858  * of ignored hooked blocks, an empty string is returned.
    859  *
    860  * The hooked block type is specified separately as it's possible that a filter might've modified
    861  * the hooked block such that `$hooked_block['blockName']` does no longer reflect the original type.
    862  *
    863  * This function is meant for internal use only.
    864  *
    865  * @since 6.5.0
    866  * @access private
    867  *
    868  * @param array  $hooked_block      The hooked block, represented as a parsed block array.
    869  * @param string $hooked_block_type The type of the hooked block. This could be different from
    870  *                                  $hooked_block['blockName'], as a filter might've modified the latter.
    871  * @param array  $anchor_block      The anchor block, represented as a parsed block array.
    872  *                                  Passed by reference.
    873  * @return string The markup for the given hooked block, or an empty string if the block is ignored.
    874  */
    875 function get_hooked_block_markup( $hooked_block, $hooked_block_type, &$anchor_block ) {
    876     if ( ! isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ) {
    877         $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array();
    878     }
    879 
    880     if ( in_array( $hooked_block_type, $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], true ) ) {
    881         return '';
    882     }
    883 
    884     // The following is only needed for the REST API endpoint.
    885     // However, its presence does not affect the frontend.
    886     $anchor_block['attrs']['metadata']['ignoredHookedBlocks'][] = $hooked_block_type;
    887 
    888     return serialize_block( $hooked_block );
    889 }
    890 
    891 /**
    892854 * Returns the markup for blocks hooked to the given anchor block in a specific relative position.
    893855 *
     
    947909
    948910        // It's possible that the `hooked_block_{$hooked_block_type}` filter returned a block of a different type,
    949         // so we need to pass the original $hooked_block_type as well.
    950         $markup .= get_hooked_block_markup( $parsed_hooked_block, $hooked_block_type, $parsed_anchor_block );
     911        // so we explicitly look for the original `$hooked_block_type` in the `ignoredHookedBlocks` metadata.
     912        if (
     913            ! isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ||
     914            ! in_array( $hooked_block_type, $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'], true )
     915        ) {
     916            $markup .= serialize_block( $parsed_hooked_block );
     917        }
    951918    }
    952919
    953920    return $markup;
     921}
     922
     923/**
     924 * Adds a list of hooked block types to an anchor block's ignored hooked block types.
     925 *
     926 * This function is meant for internal use only.
     927 *
     928 * @since 6.5.0
     929 * @access private
     930 *
     931 * @param array                   $parsed_anchor_block The anchor block, in parsed block array format.
     932 * @param string                  $relative_position   The relative position of the hooked blocks.
     933 *                                                     Can be one of 'before', 'after', 'first_child', or 'last_child'.
     934 * @param array                   $hooked_blocks       An array of hooked block types, grouped by anchor block and relative position.
     935 * @param WP_Block_Template|array $context             The block template, template part, or pattern that the anchor block belongs to.
     936 * @return string An empty string.
     937 */
     938function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) {
     939    $anchor_block_type  = $parsed_anchor_block['blockName'];
     940    $hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
     941        ? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
     942        : array();
     943
     944    /** This filter is documented in wp-includes/blocks.php */
     945    $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
     946    if ( empty( $hooked_block_types ) ) {
     947        return '';
     948    }
     949
     950    $previously_ignored_hooked_blocks = isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] )
     951        ? $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks']
     952        : array();
     953
     954    $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array_unique(
     955        array_merge(
     956            $previously_ignored_hooked_blocks,
     957            $hooked_block_types
     958        )
     959    );
     960
     961    // Markup for the hooked blocks has already been created (in `insert_hooked_blocks`).
     962    return '';
    954963}
    955964
     
    964973 *
    965974 * @since 6.4.0
     975 * @since 6.5.0 Added $callback argument.
    966976 * @access private
    967977 *
     
    969979 * @param WP_Block_Template|WP_Post|array $context       A block template, template part, `wp_navigation` post object,
    970980 *                                                       or pattern that the blocks belong to.
     981 * @param callable                        $callback      A function that will be called for each block to generate
     982 *                                                       the markup for a given list of blocks that are hooked to it.
     983 *                                                       Default: 'insert_hooked_blocks'.
    971984 * @return callable A function that returns the serialized markup for the given block,
    972985 *                  including the markup for any hooked blocks before it.
    973986 */
    974 function make_before_block_visitor( $hooked_blocks, $context ) {
     987function make_before_block_visitor( $hooked_blocks, $context, $callback = 'insert_hooked_blocks' ) {
    975988    /**
    976989     * Injects hooked blocks before the given block, injects the `theme` attribute into Template Part blocks, and returns the serialized markup.
     
    985998     * @return string The serialized markup for the given block, with the markup for any hooked blocks prepended to it.
    986999     */
    987     return function ( &$block, &$parent_block = null, $prev = null ) use ( $hooked_blocks, $context ) {
     1000    return function ( &$block, &$parent_block = null, $prev = null ) use ( $hooked_blocks, $context, $callback ) {
    9881001        _inject_theme_attribute_in_template_part_block( $block );
    9891002
     
    9921005        if ( $parent_block && ! $prev ) {
    9931006            // Candidate for first-child insertion.
    994             $markup .= insert_hooked_blocks( $parent_block, 'first_child', $hooked_blocks, $context );
    995         }
    996 
    997         $markup .= insert_hooked_blocks( $block, 'before', $hooked_blocks, $context );
     1007            $markup .= call_user_func_array(
     1008                $callback,
     1009                array( &$parent_block, 'first_child', $hooked_blocks, $context )
     1010            );
     1011        }
     1012
     1013        $markup .= call_user_func_array(
     1014            $callback,
     1015            array( &$block, 'before', $hooked_blocks, $context )
     1016        );
    9981017
    9991018        return $markup;
     
    10111030 *
    10121031 * @since 6.4.0
     1032 * @since 6.5.0 Added $callback argument.
    10131033 * @access private
    10141034 *
     
    10161036 * @param WP_Block_Template|WP_Post|array $context       A block template, template part, `wp_navigation` post object,
    10171037 *                                                       or pattern that the blocks belong to.
     1038 * @param callable                        $callback      A function that will be called for each block to generate
     1039 *                                                       the markup for a given list of blocks that are hooked to it.
     1040 *                                                       Default: 'insert_hooked_blocks'.
    10181041 * @return callable A function that returns the serialized markup for the given block,
    10191042 *                  including the markup for any hooked blocks after it.
    10201043 */
    1021 function make_after_block_visitor( $hooked_blocks, $context ) {
     1044function make_after_block_visitor( $hooked_blocks, $context, $callback = 'insert_hooked_blocks' ) {
    10221045    /**
    10231046     * Injects hooked blocks after the given block, and returns the serialized markup.
     
    10311054     * @return string The serialized markup for the given block, with the markup for any hooked blocks appended to it.
    10321055     */
    1033     return function ( &$block, &$parent_block = null, $next = null ) use ( $hooked_blocks, $context ) {
    1034         $markup = insert_hooked_blocks( $block, 'after', $hooked_blocks, $context );
     1056    return function ( &$block, &$parent_block = null, $next = null ) use ( $hooked_blocks, $context, $callback ) {
     1057        $markup = call_user_func_array(
     1058            $callback,
     1059            array( &$block, 'after', $hooked_blocks, $context )
     1060        );
    10351061
    10361062        if ( $parent_block && ! $next ) {
    10371063            // Candidate for last-child insertion.
    1038             $markup .= insert_hooked_blocks( $parent_block, 'last_child', $hooked_blocks, $context );
     1064            $markup .= call_user_func_array(
     1065                $callback,
     1066                array( &$parent_block, 'last_child', $hooked_blocks, $context )
     1067            );
    10391068        }
    10401069
Note: See TracChangeset for help on using the changeset viewer.