WPSEO_Sitemap_Image_Parser::parse_html_images
Parse <img /> tags in content.
Метод класса: WPSEO_Sitemap_Image_Parser{}
Хуков нет.
Возвращает
Массив.
Использование
// private - только в коде основоного (родительского) класса $result = $this->parse_html_images( $content );
- $content(строка) (обязательный)
- Content string to parse.
Код WPSEO_Sitemap_Image_Parser::parse_html_images() WPSEO Sitemap Image Parser::parse html images Yoast 27.4
private function parse_html_images( $content ) {
$images = [];
if ( ! class_exists( 'DOMDocument' ) ) {
return $images;
}
if ( empty( $content ) ) {
return $images;
}
// Prevent DOMDocument from bubbling warnings about invalid HTML.
libxml_use_internal_errors( true );
$post_dom = new DOMDocument();
$post_dom->loadHTML( '<?xml encoding="' . $this->charset . '">' . $content );
// Clear the errors, so they don't get kept in memory.
libxml_clear_errors();
/**
* Image attribute.
*
* @var DOMElement $img
*/
foreach ( $post_dom->getElementsByTagName( 'img' ) as $img ) {
$src = $img->getAttribute( 'src' );
if ( empty( $src ) ) {
continue;
}
$class = $img->getAttribute( 'class' );
if ( // This detects WP-inserted images, which we need to upsize. R.
! empty( $class )
&& ( strpos( $class, 'size-full' ) === false )
&& preg_match( '|wp-image-(?P<id>\d+)|', $class, $matches )
&& get_post_status( $matches['id'] )
) {
$query_params = wp_parse_url( $src, PHP_URL_QUERY );
$src = $this->image_url( $matches['id'] );
if ( $query_params ) {
$src .= '?' . $query_params;
}
}
$src = $this->get_absolute_url( $src );
if ( strpos( $src, $this->host ) === false ) {
continue;
}
if ( $src !== esc_url( $src, null, 'attribute' ) ) {
continue;
}
$images[] = [
'src' => $src,
];
}
return $images;
}