WP_HTML_Processor::next_tag( array|string|null $query = null ): bool

In this article

Finds the next tag matching the $query.

Parameters

$queryarray|string|nulloptional
Which tag name to find, having which class, etc. Default is to find any tag.
  • tag_name string|null
    Which tag to find, or null for "any tag."
  • match_offset int|null
    Find the Nth tag matching all search criteria.
    1 for "first" tag, 3 for "third," etc.
    Defaults to first tag.
  • class_name string|null
    Tag must contain this whole class name to match.
  • breadcrumbs string[]
    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', '*' ).

Default:null

Return

bool Whether a tag was matched.

Source

public function next_tag( $query = null ) {
	if ( null === $query ) {
		while ( $this->step() ) {
			if ( '#tag' !== $this->get_token_type() ) {
				continue;
			}

			if ( ! $this->is_tag_closer() ) {
				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 ( ! ( array_key_exists( 'breadcrumbs', $query ) && is_array( $query['breadcrumbs'] ) ) ) {
		while ( $this->step() ) {
			if ( '#tag' !== $this->get_token_type() ) {
				continue;
			}

			if ( ! $this->is_tag_closer() ) {
				return true;
			}
		}

		return false;
	}

	if ( isset( $query['tag_closers'] ) && 'visit' === $query['tag_closers'] ) {
		_doing_it_wrong(
			__METHOD__,
			__( 'Cannot visit tag closers in HTML Processor.' ),
			'6.4.0'
		);
		return false;
	}

	$breadcrumbs  = $query['breadcrumbs'];
	$match_offset = isset( $query['match_offset'] ) ? (int) $query['match_offset'] : 1;

	while ( $match_offset > 0 && $this->step() ) {
		if ( '#tag' !== $this->get_token_type() ) {
			continue;
		}

		if ( $this->matches_breadcrumbs( $breadcrumbs ) && 0 === --$match_offset ) {
			return true;
		}
	}

	return false;
}

Changelog

VersionDescription
6.4.0Introduced.

User Contributed Notes

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