Yoast\WP\SEO\Builders

Indexable_Link_Builder::gather_images()protectedYoast 1.0

Gathers all images from content.

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

Возвращает

int[]. An associated array of image IDs, keyed by their URLs.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->gather_images( $content );
$content(строка) (обязательный)
The content.

Код Indexable_Link_Builder::gather_images() Yoast 23.0

protected function gather_images( $content ) {

	/**
	 * Filter 'wpseo_force_creating_and_using_attachment_indexables' - Filters if we should use attachment indexables to find all content images. Instead of scanning the content.
	 *
	 * The default value is false.
	 *
	 * @since 21.1
	 */
	$should_not_parse_content = \apply_filters( 'wpseo_force_creating_and_using_attachment_indexables', false );

	/**
	 * Filter 'wpseo_force_skip_image_content_parsing' - Filters if we should force skip scanning the content to parse images.
	 * This filter can be used if the regex gives a faster result than scanning the code.
	 *
	 * The default value is false.
	 *
	 * @since 21.1
	 */
	$should_not_parse_content = \apply_filters( 'wpseo_force_skip_image_content_parsing', $should_not_parse_content );
	if ( ! $should_not_parse_content && \class_exists( WP_HTML_Tag_Processor::class ) ) {
		return $this->gather_images_wp( $content );
	}

	if ( ! $should_not_parse_content && \class_exists( DOMDocument::class ) ) {
		return $this->gather_images_DOMDocument( $content );
	}

	if ( \strpos( $content, 'src' ) === false ) {
		// Nothing to do.
		return [];
	}

	$images = [];
	$regexp = '<img\s[^>]*src=("??)([^" >]*?)\\1[^>]*>';
	// Used modifiers iU to match case insensitive and make greedy quantifiers lazy.
	if ( \preg_match_all( "/$regexp/iU", $content, $matches, \PREG_SET_ORDER ) ) {
		foreach ( $matches as $match ) {
			$images[ $match[2] ] = 0;
		}
	}

	return $images;
}