]*)>(?:.+)?<\/script>/Umsi', '', $html );
$clean_buffer = preg_replace( '##Umsi', '', $clean_buffer );
if ( ! preg_match_all( '#
\s.+)\s?/?>#iUs', $clean_buffer, $images, PREG_SET_ORDER ) ) {
return $html;
}
$images = array_unique( $images, SORT_REGULAR );
foreach ( $images as $image ) {
$image = $this->canLazyload( $image );
if ( ! $image ) {
continue;
}
$image_lazyload = $this->replaceImage( $image, $use_native );
if ( ! $use_native ) {
$image_lazyload .= $this->noscript( $image[0] );
}
$html = str_replace( $image[0], $image_lazyload, $html );
unset( $image_lazyload );
}
return $html;
}
/**
* Applies lazyload on background images defined in style attributes
*
* @param string $html Original HTML.
* @param string $buffer Content to parse.
* @return string
*/
public function lazyloadBackgroundImages( $html, $buffer ) {
if ( ! preg_match_all( '#<(?div|figure|section|span|li|a)\s+(?[^>]+[\'"\s])?style\s*=\s*([\'"])(?.*?)\3(?[^>]*)>#is', $buffer, $elements, PREG_SET_ORDER ) ) {
return $html;
}
foreach ( $elements as $element ) {
if ( $this->isExcluded( $element['before'] . $element['after'], $this->getExcludedAttributes() ) ) {
continue;
}
if ( ! preg_match( '#background-image\s*:\s*(?\s*url\s*\((?[^)]+)\))\s*;?#is', $element['styles'], $url ) ) {
continue;
}
$url['url'] = esc_url(
trim(
wp_strip_all_tags(
html_entity_decode(
$url['url'],
ENT_QUOTES | ENT_HTML5
)
),
'\'" '
)
);
if ( $this->isExcluded( $url['url'], $this->getExcludedSrc() ) ) {
continue;
}
$lazy_bg = $this->addLazyCLass( $element[0] );
$lazy_bg = str_replace( $url[0], '', $lazy_bg );
$lazy_bg = str_replace( '<' . $element['tag'], '<' . $element['tag'] . ' data-bg="' . esc_attr( $url['url'] ) . '"', $lazy_bg );
$html = str_replace( $element[0], $lazy_bg, $html );
unset( $lazy_bg );
}
return $html;
}
/**
* Add the identifier class to the element
*
* @param string $element Element to add the class to.
* @return string
*/
private function addLazyClass( $element ) {
$class = $this->getClasses( $element );
if ( empty( $class ) ) {
return preg_replace( '#<(img|div|figure|section|li|span|a)([^>]*)>#is', '<\1 class="rocket-lazyload"\2>', $element );
}
if ( empty( $class['attribute'] ) || empty( $class['classes'] ) ) {
return str_replace( $class['attribute'], 'class="rocket-lazyload"', $element );
}
$quotes = $this->getAttributeQuotes( $class['classes'] );
$classes = $this->trimOuterQuotes( $class['classes'], $quotes );
if ( empty( $classes ) ) {
return str_replace( $class['attribute'], 'class="rocket-lazyload"', $element );
}
$classes .= ' rocket-lazyload';
return str_replace(
$class['attribute'],
'class=' . $this->normalizeClasses( $classes, $quotes ),
$element
);
}
/**
* Gets the attribute value's outer quotation mark, if one exists, i.e. " or '.
*
* @param string $attribute_value The target attribute's value.
*
* @return bool|string quotation character; else false when no quotation mark.
*/
private function getAttributeQuotes( $attribute_value ) {
$attribute_value = trim( $attribute_value );
$first_char = $attribute_value[0];
if ( '"' === $first_char || "'" === $first_char ) {
return $first_char;
}
return false;
}
/**
* Gets the class attribute and values from the given element, if it exists.
*
* @param string $element Given HTML element to extract classes from.
*
* @return bool|string[] {
* @type string $attribute Class attribute and value, e.g. class="value"
* @type string $classes String of class attribute's value(s)
* }; else, false when no class attribute exists.
*/
private function getClasses( $element ) {
if ( ! preg_match( '#class\s*=\s*(?["\'].*?["\']|[^\s]+)#is', $element, $class ) ) {
return false;
}
if ( empty( $class ) ) {
return false;
}
if ( ! isset( $class['classes'] ) ) {
return false;
}
return [
'attribute' => $class[0],
'classes' => $class['classes'],
];
}
/**
* Removes outer single or double quotations.
*
* @param string $string String to strip quotes from.
* @param string $quotes The outer quotes to remove.
*
* @return string string without quotes.
*/
private function trimOuterQuotes( $string, $quotes ) {
$string = trim( $string );
if ( empty( $string ) ) {
return '';
}
if ( empty( $quotes ) ) {
return $string;
}
$string = ltrim( $string, $quotes );
$string = rtrim( $string, $quotes );
return trim( $string );
}
/**
* Normalizes the class attribute values to ensure well-formed.
*
* @param string $classes String of class attribute value(s).
* @param string|bool $quotes Optional. Quotation mark to wrap around the classes.
*
* @return string well-formed class attributes.
*/
private function normalizeClasses( $classes, $quotes = '"' ) {
$array_of_classes = $this->stringToArray( $classes );
$classes = implode( ' ', $array_of_classes );
if ( false === $quotes ) {
$quotes = '"';
}
return $quotes . $classes . $quotes;
}
/**
* Converts the given string into an array of strings.
*
* Note:
* 1. Removes empties.
* 2. Trims each string.
*
* @param string $string The target string to convert.
* @param string $delimiter Optional. Default: ' ' empty string.
*
* @return array An array of trimmed strings.
*/
private function stringToArray( $string, $delimiter = ' ' ) {
if ( empty( $string ) ) {
return [];
}
$array = explode( $delimiter, $string );
$array = array_map( 'trim', $array );
// Remove empties.
return array_filter( $array );
}
/**
* Applies lazyload on picture elements found in the HTML.
*
* @param string $html Original HTML.
* @param string $buffer Content to parse.
* @return string
*/
public function lazyloadPictures( $html, $buffer ) {
if ( ! preg_match_all( '#(?.*)#iUs', $buffer, $pictures, PREG_SET_ORDER ) ) {
return $html;
}
$pictures = array_unique( $pictures, SORT_REGULAR );
$excluded = array_merge( $this->getExcludedAttributes(), $this->getExcludedSrc() );
foreach ( $pictures as $picture ) {
if ( $this->isExcluded( $picture[0], $excluded ) ) {
if ( ! preg_match( '#
\s.+)\s?/?>#iUs', $picture[0], $img ) ) {
continue;
}
$img = $this->canLazyload( $img );
if ( ! $img ) {
continue;
}
$nolazy_picture = str_replace( '
\s.+)>#iUs', $picture['sources'], $sources, PREG_SET_ORDER ) ) {
$lazy_sources = 0;
$sources = array_unique( $sources, SORT_REGULAR );
$lazy_picture = $picture[0];
foreach ( $sources as $source ) {
$lazyload_srcset = preg_replace( '/([\s"\'])srcset/i', '\1data-lazy-srcset', $source[0] );
$lazy_picture = str_replace( $source[0], $lazyload_srcset, $lazy_picture );
unset( $lazyload_srcset );
$lazy_sources++;
}
if ( 0 === $lazy_sources ) {
continue;
}
$html = str_replace( $picture[0], $lazy_picture, $html );
}
if ( ! preg_match( '#
\s.+)\s?/?>#iUs', $picture[0], $img ) ) {
continue;
}
$img = $this->canLazyload( $img );
if ( ! $img ) {
continue;
}
$img_lazy = $this->replaceImage( $img, false );
$img_lazy .= $this->noscript( $img[0] );
$safe_img = str_replace( '/', '\/', preg_quote( $img[0], '#' ) );
$html = preg_replace( '#