Make WordPress Core


Ignore:
Timestamp:
10/09/2023 04:38:25 PM (10 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/class-wp-block-patterns-registry.php

    r56649 r56805  
    154154
    155155    /**
     156     * Prepares the content of a block pattern. If hooked blocks are registered, they get injected into the pattern,
     157     * when they met the defined criteria.
     158     *
     159     * @since 6.4.0
     160     *
     161     * @param array $pattern       Registered pattern properties.
     162     * @param array $hooked_blocks The list of hooked blocks.
     163     * @return string The content of the block pattern.
     164     */
     165    private function prepare_content( $pattern, $hooked_blocks ) {
     166        $content = $pattern['content'];
     167        if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
     168            $blocks               = parse_blocks( $content );
     169            $before_block_visitor = make_before_block_visitor( $hooked_blocks, $pattern );
     170            $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $pattern );
     171            $content              = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
     172        }
     173
     174        return $content;
     175    }
     176
     177    /**
    156178     * Retrieves an array containing the properties of a registered block pattern.
    157179     *
     
    166188        }
    167189
    168         $pattern              = $this->registered_patterns[ $pattern_name ];
    169         $blocks               = parse_blocks( $pattern['content'] );
    170         $before_block_visitor = make_before_block_visitor( $pattern );
    171         $after_block_visitor  = make_after_block_visitor( $pattern );
    172         $pattern['content']   = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
     190        $pattern            = $this->registered_patterns[ $pattern_name ];
     191        $pattern['content'] = $this->prepare_content( $pattern, get_hooked_blocks() );
    173192
    174193        return $pattern;
     
    185204     */
    186205    public function get_all_registered( $outside_init_only = false ) {
    187         $patterns = array_values(
     206        $patterns      = array_values(
    188207            $outside_init_only
    189208                ? $this->registered_patterns_outside_init
    190209                : $this->registered_patterns
    191210        );
    192 
     211        $hooked_blocks = get_hooked_blocks();
    193212        foreach ( $patterns as $index => $pattern ) {
    194             $blocks                        = parse_blocks( $pattern['content'] );
    195             $before_block_visitor          = make_before_block_visitor( $pattern );
    196             $after_block_visitor           = make_after_block_visitor( $pattern );
    197             $patterns[ $index ]['content'] = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
     213            $patterns[ $index ]['content'] = $this->prepare_content( $pattern, $hooked_blocks );
    198214        }
    199215        return $patterns;
Note: See TracChangeset for help on using the changeset viewer.