ml ', 'html class="' . $add_classes . '" ', $match[0] );
}
$content = str_replace( $match[0], $new_tag, $content );
}
}
return $content;
}
/**
* Adds a filter with detected images tags and the content.
*
* @param string $content The HTML content.
*
* @return mixed
*/
public function process_images_from_content( $content ) {
if ( self::should_ignore_image_tags() ) {
return $content;
}
$images = self::parse_images_from_html( $content );
if ( empty( $images ) ) {
return $content;
}
return apply_filters( 'optml_content_images_tags', $content, $images );
}
/**
* Check if we are on a amp endpoint.
*
* IMPORTANT: This needs to be used after parse_query hook, otherwise will return false positives.
*
* @return bool
*/
public static function should_ignore_image_tags() {
// Ignore image tag replacement in feed context as we don't need it.
if ( is_feed() ) {
return true;
}
// Ignore image tags replacement in amp context as they are not available.
if ( function_exists( 'is_amp_endpoint' ) ) {
return is_amp_endpoint();
}
if ( function_exists( 'ampforwp_is_amp_endpoint' ) ) {
return ampforwp_is_amp_endpoint();
}
return apply_filters( 'optml_should_ignore_image_tags', false ) === true;
}
/**
* Match all images and any relevant tags in a block of HTML.
*
* @param string $content Some HTML.
*
* @return array An array of $images matches, where $images[0] is
* an array of full matches, and the link_url, img_tag,
* and img_url keys are arrays of those matches.
*/
public static function parse_images_from_html( $content ) {
$images = [];
$header_start = null;
$header_end = null;
if ( preg_match( '//ismU', $content, $matches, PREG_OFFSET_CAPTURE ) === 1 ) {
$header_start = $matches[0][1];
$header_end = $header_start + strlen( $matches[0][0] );
}
$regex = '/(?:]+?href=["|\'](?P[^\s]+?)["|\'][^>]*?>\s*)?(?P(?: