false,
];
$args = wp_parse_args( $args, $defaults );
if ( ! preg_match_all( '@@iUs', $buffer, $iframes, PREG_SET_ORDER ) ) {
return $html;
}
$iframes = array_unique( $iframes, SORT_REGULAR );
foreach ( $iframes as $iframe ) {
if ( $this->isIframeExcluded( $iframe ) ) {
continue;
}
// Given the previous regex pattern, $iframe['atts'] starts with a whitespace character.
if ( ! preg_match( '@\ssrc\s*=\s*(\'|")(?.*)\1@iUs', $iframe['atts'], $atts ) ) {
continue;
}
$iframe['src'] = trim( $atts['src'] );
if ( '' === $iframe['src'] ) {
continue;
}
if ( $args['youtube'] ) {
$iframe_lazyload = $this->replaceYoutubeThumbnail( $iframe );
}
if ( empty( $iframe_lazyload ) ) {
$iframe_lazyload = $this->replaceIframe( $iframe );
}
$html = str_replace( $iframe[0], $iframe_lazyload, $html );
unset( $iframe_lazyload );
}
return $html;
}
/**
* Checks if the provided iframe is excluded from lazyload
*
* @param array $iframe Array of matched patterns.
* @return boolean
*/
public function isIframeExcluded( $iframe ) {
foreach ( $this->getExcludedPatterns() as $excluded_pattern ) {
if ( strpos( $iframe[0], $excluded_pattern ) !== false ) {
return true;
}
}
return false;
}
/**
* Gets patterns excluded from lazyload for iframes
*
* @since 2.1.1
*
* @return array
*/
private function getExcludedPatterns() {
/**
* Filters the patterns excluded from lazyload for iframes
*
* @since 2.1.1
*
* @param array $excluded_patterns Array of excluded patterns.
*/
return apply_filters(
'rocket_lazyload_iframe_excluded_patterns',
[
'gform_ajax_frame',
'data-no-lazy=',
'recaptcha/api/fallback',
'loading="eager"',
'data-skip-lazy',
'skip-lazy',
'google_ads_iframe_',
]
);
}
/**
* Applies lazyload on the iframe provided
*
* @param array $iframe Array of matched elements.
* @return string
*/
private function replaceIframe( $iframe ) {
/**
* Filter the LazyLoad placeholder on src attribute
*
* @since 1.0
*
* @param string $placeholder placeholder that will be printed.
*/
$placeholder = apply_filters( 'rocket_lazyload_placeholder', 'about:blank' );
$placeholder_atts = str_replace( $iframe['src'], $placeholder, $iframe['atts'] );
$iframe_lazyload = str_replace( $iframe['atts'], $placeholder_atts . ' data-rocket-lazyload="fitvidscompatible" data-lazy-src="' . esc_url( $iframe['src'] ) . '"', $iframe[0] );
if ( ! preg_match( '@\sloading\s*=\s*(\'|")(?:lazy|auto)\1@i', $iframe_lazyload ) ) {
$iframe_lazyload = str_replace( '