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/block-template-utils.php

    r56724 r56805  
    550550    }
    551551
    552     $blocks               = parse_blocks( $template_content );
    553     $before_block_visitor = make_before_block_visitor( $template );
    554     $after_block_visitor  = make_after_block_visitor( $template );
    555     $template->content    = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
     552    $before_block_visitor = '_inject_theme_attribute_in_template_part_block';
     553    $after_block_visitor  = null;
     554    $hooked_blocks        = get_hooked_blocks();
     555    if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
     556        $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template );
     557        $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $template );
     558    }
     559    $blocks            = parse_blocks( $template_content );
     560    $template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
    556561
    557562    return $template;
Note: See TracChangeset for help on using the changeset viewer.