Skip to content
Closed
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@
#############################################################################
SELECTIVE EXCLUSIONS
Exclude specific files for specific sniffs and/or exclude sub-groups in sniffs.

These exclusions are listed ordered by alphabetic sniff name.
#############################################################################
-->
Expand Down Expand Up @@ -266,8 +266,9 @@

<!-- Goto is an effective way to handle errors in decoders which expect valid bytes
without impacting the fast path while avoiding bloating the code with redundant
and risky handling code. Exclude forbidding goto in UTF-8 fallback code. -->
and risky handling code. Exclude forbidding goto in parser code. -->
<exclude-pattern>/wp-includes/compat-utf8\.php</exclude-pattern>
<exclude-pattern>/wp-includes/class-wp-block-processor\.php</exclude-pattern>
</rule>

<!-- Exclude sample config from modernization to prevent breaking CI workflows based on WP-CLI scaffold.
Expand Down
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
11 changes: 11 additions & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2376,6 +2376,17 @@ function render_block( $parsed_block ) {
/**
* Parses blocks out of a content string.
*
* Given an HTML document, this function fully-parses block content, producing
* a tree of blocks and their contents, as well as top-level non-block content,
* which will appear as a block with no `blockName`.
*
* This function can be memory heavy for certain documents, particularly those
* with deeply-nested blocks or blocks with extensive attribute values. Further,
* this function must parse an entire document in one atomic operation.
*
* If the entire parsed document is not necessary, consider using {@see WP_Block_Processor}
* instead, as it provides a streaming and low-overhead interface for finding blocks.
*
* @since 5.0.0
*
* @param string $content Post content.
Expand Down
Loading
Loading