WP_Block_Processor::next_delimiter( string|null $block_name = null ): bool

Advance to the next block delimiter in a document, indicating if one was found.

Description

Delimiters may include invalid JSON. This parser does not attempt to parse the JSON attributes until requested; when invalid, the attributes will be null. This matches the behavior of parse_blocks(). To visit freeform HTML content, pass the wildcard “*” as the block type.

Use this function to walk through the block delimiters in a document.

Example delimiters:

<!-- wp:paragraph {"dropCap": true} -->
<!-- wp:separator /-->
<!-- /wp:paragraph -->

// If the wildcard `*` is provided as the block type, freeform content is matched.
<⃨h⃨2⃨>⃨M⃨y⃨ ⃨s⃨y⃨n⃨o⃨p⃨s⃨i⃨s⃨<⃨/⃨h⃨2⃨>⃨\⃨n⃨<!-- wp:my/table-of-contents /-->

// Inner HTML is never freeform content, and will not be matched even with the wildcard.
...</ul><⃨!⃨-⃨-⃨ ⃨/⃨w⃨p⃨:⃨l⃨i⃨s⃨t⃨ ⃨-⃨-⃨>⃨<!-- wp:paragraph --><p>

Example:

$html      = '<!-- wp:void /-->\n<!-- wp:void /-->';
$processor = new WP_Block_Processor( $html );
while ( $processor->next_delimiter() {
    // Runs twice, seeing both void blocks of type “core/void.”
}

$processor = new WP_Block_Processor( $html );
while ( $processor->next_delimiter( '*' ) ) {
    // Runs thrice, seeing the void block, the newline span, and the void block.
}

Parameters

$block_namestring|nulloptional
Keep searching until a block of this name is found.
Defaults to visit every block regardless of type.

Default:null

Return

bool Whether a block delimiter was matched.

Source

public function next_delimiter( ?string $block_name = null ): bool {
	if ( ! isset( $block_name ) ) {
		while ( $this->next_token() ) {
			if ( ! $this->is_html() ) {
				return true;
			}
		}

		return false;
	}

	while ( $this->next_token() ) {
		if ( $this->is_block_type( $block_name ) ) {
			return true;
		}
	}

	return false;
}

Changelog

VersionDescription
6.9.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.