WP_HTML_Processor::next_tag()publicWP 6.4.0

Finds the next tag matching the $query.

Метод класса: WP_HTML_Processor{}

Хуков нет.

Возвращает

true|false. Whether a tag was matched.

Использование

$WP_HTML_Processor = new WP_HTML_Processor();
$WP_HTML_Processor->next_tag( $query );
$query(массив|строка|null)

Which tag name to find, having which class, etc.

По умолчанию: to find any tag

  • tag_name(строка|null)
    Which tag to find, or null for "any tag."

  • tag_closers(строка)
    'visit' to pause at tag closers, 'skip' or unset to only visit openers.

  • match_offset(int|null)
    Find the Nth tag matching all search criteria.
    1 for "first" tag, 3 for "third," etc.
    По умолчанию: first tag

  • class_name(строка|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', '*' ).

Список изменений

С версии 6.4.0 Введена.
С версии 6.6.0 Visits all tokens, including virtual ones.

Код WP_HTML_Processor::next_tag() WP 6.6.2

public function next_tag( $query = null ) {
	$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;
	}

	$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( $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;
}