WP_Block_Processor::next_token(): bool
- Since
- 6.9.0
- Source
wp-includes/class-wp-block-processor.php:736
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
- Scaling
- Scales with input
- Instructions
- 3–205
- Plugin surface
- None
- Called by
- 2
Touches nothing outside its own arguments.
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 466.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 3–43 | none |
$at && $comment_opening_at !== false | 51–62 | strspn() |
$at && $comment_opening_at !== false && $opening_whitespace_length !== 0 && $wp_prefix_at | 70 | strspn(), substr_compare() |
$at && $comment_opening_at !== false && $opening_whitespace_length !== 0 && !$wp_prefix_at && !$start_of_namespace | 78–86 | strspn(), strspn() |
$at && $comment_opening_at !== false && $opening_whitespace_length !== 0 && $wp_prefix_at && !$start_of_namespace | 86–94 | strspn(), substr_compare(), strspn() |
$at && $comment_opening_at !== false && !$end && $opening_whitespace_length !== 0 && !$wp_prefix_at && !$start_of_namespace | 95–189 | strspn(), strspn(), strspn() |
$at && $comment_opening_at !== false && !$end && $opening_whitespace_length !== 0 && $wp_prefix_at && !$start_of_namespace | 103–197 | strspn(), substr_compare(), strspn(), strspn() |
$at && $comment_opening_at !== false && !$end && $opening_whitespace_length !== 0 && !$wp_prefix_at && !$start_of_namespace && !$start_of_name | 110–204 | strspn(), 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_delimiter | 159–190 | strspn(), 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_delimiter | 167–198 | strspn(), 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_delimiter | 174–205 | strspn(), 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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 466 | 3–205 | 59 | |
| 8.5 | 466 | 3–205 | 59 | 1 more instruction than PHP 8.4 |
| 8.4 | 465 | 3–204 | 59 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 471 | 3–210 | 59 | |
| 8.2 | 471 | 3–210 | 59 | 3 more instructions than PHP 8.1 |
| 8.1 | 468 | 3–213 | 59 | |
| 7.4 | 468 | 3–213 | 59 |
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
- str_ends_with()Polyfill for `str_ends_with()` function added in PHP 8.0.
- WP_Block_Processor::find_html_comment_end()Returns the byte-offset after the ending character of an HTML comment, assuming the proper starting byte offset.
Used by · 2
- WP_Block_Processor::extract_full_block_and_advance()Extracts a block object, and all inner content, starting at a matched opening block delimiter, or at a matched top-level HTML span as freeform HTML content.
- WP_Block_Processor::next_delimiter()Advance to the next block delimiter in a document, indicating if one was found.
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.
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.