wppaste
WordPress

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
Finds the next tag matching the $query.

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|null

    Which tag to find, or null for "any tag." @type string $tag_closers 'visit' to pause at tag closers, 'skip' or unset to only visit openers.
  • $match_offsetint|null

    Find the Nth tag matching all search criteria. 1 for "first" tag, 3 for "third," etc. Defaults to first tag.
  • $class_namestring|null

    Tag 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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
11–70

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksdo_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.

WhenInstructionsCalls 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–50strtoupper(), ->next_token()
!isset($query) && $query !== null && is_array($query) && $query && !$match_offset30–41none
$query !== null && is_array($query) && isset($query) && $query && !$match_offset36–47strtoupper()
!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–53strtoupper(), ->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–57strtoupper(), ->next_token(), ->get_token_type(), ->has_class(), ->is_tag_closer()
$query !== null && is_array($query) && isset($query) && ->next_token() && !isset($needs_class)44–58strtoupper(), ->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–62strtoupper(), ->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 === 049–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 === 053–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 === 055–66strtoupper(), ->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 === 059–70strtoupper(), ->next_token(), ->get_token_type(), ->is_tag_closer(), ->has_class(), ->matches_breadcrumbs()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev12311–7030
8.512311–7030
8.412311–7030
8.312311–7030
8.212311–70302 more instructions than PHP 8.1
8.112110–70302 fewer instructions than PHP 7.4
7.412310–7230

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

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

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

6.6.0
Visits all tokens, including virtual ones.from the docblock
6.4.0
Introduced.from the docblock

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.