Skip to content

Commit 48f7e63

Browse files
committed
Adjust Block Processor logic to only scan top-level blocks.
1 parent 67b1cf7 commit 48f7e63

1 file changed

Lines changed: 21 additions & 15 deletions

File tree

src/wp-content/themes/twentytwentyone/inc/template-functions.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -371,21 +371,27 @@ function twenty_twenty_one_print_first_instance_of_block( $block_name, $content
371371
$processor = new WP_Block_Processor( $content );
372372
$instance_count = 0;
373373

374-
if ( str_ends_with( $block_name, '*' ) ) {
375-
// Scan for blocks whose block type matches the prefix.
376-
$prefix = rtrim( $block_name, '*' );
377-
378-
while ( $instance_count < $instances && $processor->next_block() ) {
379-
$matched_block_type = $processor->get_printable_block_type();
380-
if ( str_starts_with( $matched_block_type, $prefix ) ) {
381-
$blocks_content .= render_block( $processor->extract_full_block_and_advance() );
382-
++$instance_count;
383-
}
384-
}
385-
} else {
386-
// Scan for blocks of the exact block type.
387-
while ( $instance_count < $instances && $processor->next_block( $block_name ) ) {
388-
$blocks_content .= render_block( $processor->extract_full_block_and_advance() );
374+
// Scan for blocks whose block type matches the prefix.
375+
$prefix = rtrim( $block_name, '*' );
376+
$match_fully = $prefix === $block_name;
377+
378+
// Loop over top-level blocks.
379+
while (
380+
$processor->next_block() &&
381+
null !== ( $block = $processor->extract_full_block_and_advance() ) &&
382+
$instance_count < $instances
383+
) {
384+
if (
385+
/*
386+
* Prefix matches with a wildcard require printing the block name,
387+
* while full block-type matching can be delegated to the processor.
388+
* In each case, the condition only holds when the match is successful.
389+
*/
390+
$match_fully
391+
? $processor->is_block_type( $block_name )
392+
: str_starts_with( $processor->get_printable_block_type(), $prefix )
393+
) {
394+
$blocks_content .= render_block( $block );
389395
++$instance_count;
390396
}
391397
}

0 commit comments

Comments
 (0)