Skip to content

Commit 9c01698

Browse files
committed
Adjust Block Processor logic to only scan top-level blocks.
1 parent 2568a41 commit 9c01698

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
@@ -372,21 +372,27 @@ function twenty_twenty_one_print_first_instance_of_block( $block_name, $content
372372
$processor = new WP_Block_Processor( $content );
373373
$instance_count = 0;
374374

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

0 commit comments

Comments
 (0)