WP_HTML_Processor::next_tag( array|string|null $query = null ): bool
- Since
- 6.4.0, 6.6.0
- Source
wp-includes/html-api/class-wp-html-processor.php:690
Compatibility
- WordPress
- since 6.6.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$queryarray|string|nulloptional- Which tag name to find, having which class, etc. Default is to find any tag.Default:
null$tag_namestring|nullWhich tag to find, ornullfor "any tag." @type string $tag_closers 'visit' to pause at tag closers, 'skip' or unset to only visit openers.$match_offsetint|nullFind the Nth tag matching all search criteria. 1 for "first" tag, 3 for "third," etc. Defaults to first tag.$class_namestring|nullTag must contain this whole class name to match.$breadcrumbsstring[]DOM sub-path at which element is found, e.g.array( 'FIGURE', 'IMG' ). May also contain the wildcard*which matches a single element, e.g.array( 'SECTION', '*' ).
Return value
bool- Whether a tag was matched.
Performance profile
How much work a call to WP_HTML_Processor::next_tag() 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
- 11–70
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 123.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_action()one call below WP_HTML_Processor::next_tag()
Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 17 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_HTML_Processor::next_tag() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!->next_token() | 11–44 | ->next_token() |
->next_token() | 18–47 | ->next_token(), ->get_token_type(), ->is_tag_closer() |
$query !== null && !is_array($query) | 19–24 | __(), _doing_it_wrong() |
$query !== null && is_array($query) && isset($query) && !->next_token() | 28–50 | strtoupper(), ->next_token() |
!isset($query) && $query !== null && is_array($query) && $query && !$match_offset | 30–41 | none |
$query !== null && is_array($query) && isset($query) && $query && !$match_offset | 36–47 | strtoupper() |
!isset($query) && $query !== null && is_array($query) && ->next_token() && isset($needs_class) && ->has_class() | 37–51 | ->next_token(), ->get_token_type(), ->has_class(), ->is_tag_closer() |
$query !== null && is_array($query) && ->next_token() && !isset($needs_class) | 38–52 | ->next_token(), ->get_token_type(), ->get_token_name(), ->is_tag_closer() |
$query !== null && is_array($query) && ->next_token() && !isset($needs_class) | 39–53 | strtoupper(), ->next_token(), ->get_token_type(), ->is_tag_closer() |
$query !== null && is_array($query) && ->next_token() && isset($needs_class) && ->has_class() | 42–56 | ->next_token(), ->get_token_type(), ->get_token_name(), ->has_class(), ->is_tag_closer() |
$query !== null && is_array($query) && ->next_token() && isset($needs_class) && ->has_class() | 43–57 | strtoupper(), ->next_token(), ->get_token_type(), ->has_class(), ->is_tag_closer() |
$query !== null && is_array($query) && isset($query) && ->next_token() && !isset($needs_class) | 44–58 | strtoupper(), ->next_token(), ->get_token_type(), ->get_token_name(), ->is_tag_closer() |
5 further outcomes, up to 70 instructions
$query !== null && is_array($query) && isset($query) && ->next_token() && isset($needs_class) && ->has_class() | 48–62 | strtoupper(), ->next_token(), ->get_token_type(), ->get_token_name(), ->has_class(), ->is_tag_closer() |
!isset($query) && $query !== null && is_array($query) && $query && $match_offset && ->next_token() && !->is_tag_closer() && !isset($needs_class) && ->matches_breadcrumbs() && $match_offset === 0 | 49–60 | ->next_token(), ->get_token_type(), ->is_tag_closer(), ->matches_breadcrumbs() |
!isset($query) && $query !== null && is_array($query) && $query && $match_offset && ->next_token() && !->is_tag_closer() && isset($needs_class) && ->has_class() && ->matches_breadcrumbs() && $match_offset === 0 | 53–64 | ->next_token(), ->get_token_type(), ->is_tag_closer(), ->has_class(), ->matches_breadcrumbs() |
$query !== null && is_array($query) && isset($query) && $query && $match_offset && ->next_token() && !->is_tag_closer() && !isset($needs_class) && ->matches_breadcrumbs() && $match_offset === 0 | 55–66 | strtoupper(), ->next_token(), ->get_token_type(), ->is_tag_closer(), ->matches_breadcrumbs() |
$query !== null && is_array($query) && isset($query) && $query && $match_offset && ->next_token() && !->is_tag_closer() && isset($needs_class) && ->has_class() && ->matches_breadcrumbs() && $match_offset === 0 | 59–70 | strtoupper(), ->next_token(), ->get_token_type(), ->is_tag_closer(), ->has_class(), ->matches_breadcrumbs() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 123 | 11–70 | 30 | |
| 8.5 | 123 | 11–70 | 30 | |
| 8.4 | 123 | 11–70 | 30 | |
| 8.3 | 123 | 11–70 | 30 | |
| 8.2 | 123 | 11–70 | 30 | 2 more instructions than PHP 8.1 |
| 8.1 | 121 | 10–70 | 30 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 123 | 10–72 | 30 |
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 · 8
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- WP_HTML_Processor::next_token()Finds the next token in the HTML document.
- WP_HTML_Processor::get_token_type()Indicates the kind of matched token, if any.
- WP_HTML_Processor::is_tag_closer()Indicates if the current tag token is a tag closer.
- WP_HTML_Processor::get_token_name()Returns the node name represented by the token.
- WP_HTML_Processor::has_class()Returns if a matched tag contains the given ASCII case-insensitive class name.
- WP_HTML_Processor::matches_breadcrumbs()Indicates if the currently-matched tag matches the given breadcrumbs.
Source code
public function next_tag( $query = null ): bool { $visit_closers = isset( $query['tag_closers'] ) && 'visit' === $query['tag_closers']; if ( null === $query ) { while ( $this->next_token() ) { if ( '#tag' !== $this->get_token_type() ) { continue; } if ( ! $this->is_tag_closer() || $visit_closers ) { return true; } } return false; } if ( is_string( $query ) ) { $query = array( 'breadcrumbs' => array( $query ) ); } if ( ! is_array( $query ) ) { _doing_it_wrong( __METHOD__, __( 'Please pass a query array to this function.' ), '6.4.0' ); return false; } if ( isset( $query['tag_name'] ) ) { $query['tag_name'] = strtoupper( $query['tag_name'] ); } $needs_class = ( isset( $query['class_name'] ) && is_string( $query['class_name'] ) ) ? $query['class_name'] : null; if ( ! ( array_key_exists( 'breadcrumbs', $query ) && is_array( $query['breadcrumbs'] ) ) ) { while ( $this->next_token() ) { if ( '#tag' !== $this->get_token_type() ) { continue; } if ( isset( $query['tag_name'] ) && $query['tag_name'] !== $this->get_token_name() ) { continue; } if ( isset( $needs_class ) && ! $this->has_class( $needs_class ) ) { continue; } if ( ! $this->is_tag_closer() || $visit_closers ) { return true; } } return false; } $breadcrumbs = $query['breadcrumbs']; $match_offset = isset( $query['match_offset'] ) ? (int) $query['match_offset'] : 1; while ( $match_offset > 0 && $this->next_token() ) { if ( '#tag' !== $this->get_token_type() || $this->is_tag_closer() ) { continue; } if ( isset( $needs_class ) && ! $this->has_class( $needs_class ) ) { continue; } if ( $this->matches_breadcrumbs( $breadcrumbs ) && 0 === --$match_offset ) { return true; } } return false; }Changelog
Introduced in 6.4.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/html-api/class-wp-html-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.