Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 16 additions & 30 deletions src/wp-content/themes/twentytwentyone/inc/template-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,46 +361,32 @@ function twenty_twenty_one_get_non_latin_css( $type = 'front-end' ) {
* @return bool Returns true if a block was located & printed, otherwise false.
*/
function twenty_twenty_one_print_first_instance_of_block( $block_name, $content = null, $instances = 1 ) {
$instances_count = 0;
$blocks_content = '';

if ( ! $content ) {
$content = get_the_content();
}

// Parse blocks in the content.
$blocks = parse_blocks( $content );
$processor = new WP_Block_Processor( $content );
$instance_count = 0;

// Loop blocks.
foreach ( $blocks as $block ) {
if ( str_ends_with( $block_name, '*' ) ) {
// Scan for blocks whose block type matches the prefix.
$prefix = rtrim( $block_name, '*' );

// Confidence check.
if ( ! isset( $block['blockName'] ) ) {
continue;
}

// Check if this the block matches the $block_name.
$is_matching_block = false;

// If the block ends with *, try to match the first portion.
if ( '*' === $block_name[-1] ) {
$is_matching_block = 0 === strpos( $block['blockName'], rtrim( $block_name, '*' ) );
} else {
$is_matching_block = $block_name === $block['blockName'];
}

if ( $is_matching_block ) {
// Increment count.
++$instances_count;

// Add the block HTML.
$blocks_content .= render_block( $block );

// Break the loop if the $instances count was reached.
if ( $instances_count >= $instances ) {
break;
while ( $instance_count < $instances && $processor->next_block() ) {
$matched_block_type = $processor->get_printable_block_type();
if ( str_starts_with( $matched_block_type, $prefix ) ) {
$blocks_content .= render_block( $processor->extract_block() );
++$instance_count;
}
}
} else {
// Scan for blocks of the exact block type.
while ( $instance_count < $instances && $processor->next_block( $block_name ) ) {
$blocks_content .= render_block( $processor->extract_block() );
++$instance_count;
}
}

if ( $blocks_content ) {
Expand Down
Loading