wppaste
WordPress

WP_Block_Processor::next_token(): bool

Since
6.9.0
Source
wp-includes/class-wp-block-processor.php:736
Advance to the next block delimiter or HTML span in a document, indicating if one was found.

Description

This function steps through every syntactic chunk in a document. This includes explicit block comment delimiters, freeform non-block content, and inner HTML segments.

Example tokens:

<!-- wp:paragraph {"dropCap": true} -->
<!-- wp:separator /-->
<!-- /wp:paragraph -->
<p>Normal HTML content</p>
Plaintext content too!

Example:

// Find span containing wrapping HTML element surrounding inner blocks.
$processor = new WP_Block_Processor( $html );
if ( ! $processor->next_block( 'gallery' ) ) {
 return null;
}

$containing_span = null;
while ( $processor->next_token() && $processor->is_html() ) {
 $containing_span = $processor->get_span();
}

This method will visit all HTML spans including those forming freeform non-block content as well as those which are part of a block’s inner HTML.

Compatibility

WordPress
since 6.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 3 of the 5 tracked releases, added in 6.9.0, and compiles on PHP 7.4 through 8.6-dev.

Return value

bool
Whether a token was matched or the end of the document was reached without finding any.

Performance profile

How much work a call to WP_Block_Processor::next_token() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
3–205

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 466.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 11 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Block_Processor::next_token() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always3–43none
$at && $comment_opening_at !== false51–62strspn()
$at && $comment_opening_at !== false && $opening_whitespace_length !== 0 && $wp_prefix_at70strspn(), substr_compare()
$at && $comment_opening_at !== false && $opening_whitespace_length !== 0 && !$wp_prefix_at && !$start_of_namespace78–86strspn(), strspn()
$at && $comment_opening_at !== false && $opening_whitespace_length !== 0 && $wp_prefix_at && !$start_of_namespace86–94strspn(), substr_compare(), strspn()
$at && $comment_opening_at !== false && !$end && $opening_whitespace_length !== 0 && !$wp_prefix_at && !$start_of_namespace95–189strspn(), strspn(), strspn()
$at && $comment_opening_at !== false && !$end && $opening_whitespace_length !== 0 && $wp_prefix_at && !$start_of_namespace103–197strspn(), substr_compare(), strspn(), strspn()
$at && $comment_opening_at !== false && !$end && $opening_whitespace_length !== 0 && !$wp_prefix_at && !$start_of_namespace && !$start_of_name110–204strspn(), strspn(), strspn(), strspn()
$at && $comment_opening_at !== false && !$end && $opening_whitespace_length !== 0 && !$wp_prefix_at && !$start_of_namespace && $after_name_whitespace_length !== 0 && $comment_closing_at !== false && !$after_prev_delimiter159–190strspn(), strspn(), strspn(), array_pop(), array_pop()
$at && $comment_opening_at !== false && !$end && $opening_whitespace_length !== 0 && $wp_prefix_at && !$start_of_namespace && $after_name_whitespace_length !== 0 && $comment_closing_at !== false && !$after_prev_delimiter167–198strspn(), substr_compare(), strspn(), strspn(), array_pop(), array_pop()
$at && $comment_opening_at !== false && !$end && $opening_whitespace_length !== 0 && !$wp_prefix_at && !$start_of_namespace && !$start_of_name && $after_name_whitespace_length !== 0 && $comment_closing_at !== false && !$after_prev_delimiter174–205strspn(), strspn(), strspn(), strspn(), array_pop(), array_pop()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev4663–20559
8.54663–205591 more instruction than PHP 8.4
8.44653–204596 fewer instructions than PHP 8.3
8.34713–21059
8.24713–210593 more instructions than PHP 8.1
8.14683–21359
7.44683–21359

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 2

Used by · 2

Source code

	public function next_token(): bool {		if ( $this->last_error || self::COMPLETE === $this->state || self::INCOMPLETE_INPUT === $this->state ) {			return false;		} 		// Void tokens automatically pop off the stack of open blocks.		if ( $this->was_void ) {			array_pop( $this->open_blocks_at );			array_pop( $this->open_blocks_length );			$this->was_void = false;		} 		$text = $this->source_text;		$end  = strlen( $text ); 		/*		 * Because HTML spans are inferred after finding the next delimiter, it means that		 * the parser must transition out of that HTML state and reuse the token boundaries		 * it found after the HTML span. If those boundaries are before the end of the		 * document it implies that a real delimiter was found; otherwise this must be the		 * terminating HTML span and the parsing is complete.		 */		if ( self::HTML_SPAN === $this->state ) {			if ( $this->matched_delimiter_at >= $end ) {				$this->state = self::COMPLETE;				return false;			} 			switch ( $this->next_stack_op ) {				case 'void':					$this->was_void             = true;					$this->open_blocks_at[]     = $this->namespace_at;					$this->open_blocks_length[] = $this->name_at + $this->name_length - $this->namespace_at;					break; 				case 'push':					$this->open_blocks_at[]     = $this->namespace_at;					$this->open_blocks_length[] = $this->name_at + $this->name_length - $this->namespace_at;					break; 				case 'pop':					array_pop( $this->open_blocks_at );					array_pop( $this->open_blocks_length );					break;			} 			$this->next_stack_op = null;			$this->state         = self::MATCHED;			return true;		} 		$this->state          = self::READY;		$after_prev_delimiter = $this->matched_delimiter_at + $this->matched_delimiter_length;		$at                   = $after_prev_delimiter; 		while ( $at < $end ) {			/*			 * Find the next possible start of a delimiter.			 *			 * This follows the behavior in the official block parser, which segments a post			 * by the block comment delimiters. It is possible for an HTML attribute to contain			 * what looks like a block comment delimiter but which is actually an HTML attribute			 * value. In such a case, the parser here will break apart the HTML and create the			 * block boundary inside the HTML attribute. In other words, the block parser			 * isolates sections of HTML from each other, even if that leads to malformed markup.			 *			 * For a more robust parse, scan through the document with the HTML API and parse			 * comments once they are matched to see if they are also block delimiters. In			 * practice, this nuance has not caused any known problems since developing blocks.			 *			 * <⃨!⃨-⃨-⃨ /wp:core/paragraph {"dropCap":true} /-->			 */			$comment_opening_at = strpos( $text, '<!--', $at ); 			/*			 * Even if the start of a potential block delimiter is not found, the document			 * might end in a prefix of such, and in that case there is incomplete input.			 */			if ( false === $comment_opening_at ) {				if ( str_ends_with( $text, '<!-' ) ) {

Changelog

Introduced in 6.9.0. Unchanged from 6.9.7 through 7.1.0.

  1. 6.9.7
  2. 7.0.4
  3. 7.1.0

Signature, return type and hooks compared across 3 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-block-processor.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.