Make WordPress Core


Ignore:
Timestamp:
10/09/2023 04:38:25 PM (19 months ago)
Author:
Bernhard Reiter
Message:

Blocks: Call get_hooked_blocks only once per template/part/pattern.

Prior to this changeset, get_hooked_blocks was called four times for every parsed block in each template, template part, and pattern. With this changeset applied, get_hooked_blocks is called only once per template, template part, or pattern.

Additionally, get_hooked_blocks is called only once when returning the list of all registered patterns. (The latter modification brings the implementation closer to its state prior to Block Hooks.)

Finally, when there are no registered hooked blocks or hooked_block_types filters, parsing, hooked block insertion, and re-serializing is skipped altogether.

Props gziolo, flixos90, joemcgill, dmsnell, spacedmonkey, hellofromtonya.
Fixes #59383.

File:
1 edited

Legend:

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

    r56753 r56805  
    724724
    725725/**
    726  * Retrieves block types hooked into the given block, grouped by their relative position.
     726 * Retrieves block types hooked into the given block, grouped by anchor block type and the relative position.
    727727 *
    728728 * @since 6.4.0
    729729 *
    730  * @param string $name Block type name including namespace.
    731  * @return array[] Array of block types grouped by their relative position.
    732  */
    733 function get_hooked_blocks( $name ) {
     730 * @return array[] Array of block types grouped by anchor block type and the relative position.
     731 */
     732function get_hooked_blocks() {
    734733    $block_types   = WP_Block_Type_Registry::get_instance()->get_all_registered();
    735734    $hooked_blocks = array();
     
    739738        }
    740739        foreach ( $block_type->block_hooks as $anchor_block_type => $relative_position ) {
    741             if ( $anchor_block_type !== $name ) {
    742                 continue;
    743             }
    744             if ( ! isset( $hooked_blocks[ $relative_position ] ) ) {
    745                 $hooked_blocks[ $relative_position ] = array();
    746             }
    747             $hooked_blocks[ $relative_position ][] = $block_type->name;
    748         }
    749     }
     740            if ( ! isset( $hooked_blocks[ $anchor_block_type ] ) ) {
     741                $hooked_blocks[ $anchor_block_type ] = array();
     742            }
     743            if ( ! isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] ) ) {
     744                $hooked_blocks[ $anchor_block_type ][ $relative_position ] = array();
     745            }
     746            $hooked_blocks[ $anchor_block_type ][ $relative_position ][] = $block_type->name;
     747        }
     748    }
     749
    750750    return $hooked_blocks;
    751751}
     
    761761 * @access private
    762762 *
    763  * @param WP_Block_Template|array $context A block template, template part, or pattern that the blocks belong to.
     763 * @param array                   $hooked_blocks An array of blocks hooked to another given block.
     764 * @param WP_Block_Template|array $context       A block template, template part, or pattern that the blocks belong to.
    764765 * @return callable A function that returns the serialized markup for the given block,
    765766 *                  including the markup for any hooked blocks before it.
    766767 */
    767 function make_before_block_visitor( $context ) {
     768function make_before_block_visitor( $hooked_blocks, $context ) {
    768769    /**
    769770     * Injects hooked blocks before the given block, injects the `theme` attribute into Template Part blocks, and returns the serialized markup.
     
    778779     * @return string The serialized markup for the given block, with the markup for any hooked blocks prepended to it.
    779780     */
    780     return function ( &$block, $parent_block = null, $prev = null ) use ( $context ) {
     781    return function ( &$block, $parent_block = null, $prev = null ) use ( $hooked_blocks, $context ) {
    781782        _inject_theme_attribute_in_template_part_block( $block );
    782783
     
    787788            $relative_position  = 'first_child';
    788789            $anchor_block_type  = $parent_block['blockName'];
    789             $hooked_block_types = get_hooked_blocks( $anchor_block_type );
    790             $hooked_block_types = isset( $hooked_block_types[ $relative_position ] )
    791                 ? $hooked_block_types[ $relative_position ]
     790            $hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
     791                ? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
    792792                : array();
    793793
     
    811811        $relative_position  = 'before';
    812812        $anchor_block_type  = $block['blockName'];
    813         $hooked_block_types = get_hooked_blocks( $anchor_block_type );
    814         $hooked_block_types = isset( $hooked_block_types[ $relative_position ] )
    815             ? $hooked_block_types[ $relative_position ]
     813        $hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
     814            ? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
    816815            : array();
    817816
     
    836835 * @access private
    837836 *
    838  * @param WP_Block_Template|array $context A block template, template part, or pattern that the blocks belong to.
     837 * @param array                   $hooked_blocks An array of blocks hooked to another block.
     838 * @param WP_Block_Template|array $context       A block template, template part, or pattern that the blocks belong to.
    839839 * @return callable A function that returns the serialized markup for the given block,
    840840 *                  including the markup for any hooked blocks after it.
    841841 */
    842 function make_after_block_visitor( $context ) {
     842function make_after_block_visitor( $hooked_blocks, $context ) {
    843843    /**
    844844     * Injects hooked blocks after the given block, and returns the serialized markup.
     
    852852     * @return string The serialized markup for the given block, with the markup for any hooked blocks appended to it.
    853853     */
    854     return function ( &$block, $parent_block = null, $next = null ) use ( $context ) {
     854    return function ( &$block, $parent_block = null, $next = null ) use ( $hooked_blocks, $context ) {
    855855        $markup = '';
    856856
    857857        $relative_position  = 'after';
    858858        $anchor_block_type  = $block['blockName'];
    859         $hooked_block_types = get_hooked_blocks( $anchor_block_type );
    860         $hooked_block_types = isset( $hooked_block_types[ $relative_position ] )
    861             ? $hooked_block_types[ $relative_position ]
    862             : array();
     859        $hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
     860                ? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
     861                : array();
    863862
    864863        /** This filter is documented in wp-includes/blocks.php */
     
    872871            $relative_position  = 'last_child';
    873872            $anchor_block_type  = $parent_block['blockName'];
    874             $hooked_block_types = get_hooked_blocks( $anchor_block_type );
    875             $hooked_block_types = isset( $hooked_block_types[ $relative_position ] )
    876                 ? $hooked_block_types[ $relative_position ]
     873            $hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
     874                ? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
    877875                : array();
    878876
Note: See TracChangeset for help on using the changeset viewer.