lazy_load_enabled = $is_enabled; $this->lazy_load_native = $is_native; $this->content = $content; $this->exclude_if_atts = apply_filters( 'breeze_excluded_attributes', array( 'data-src', 'data-no-lazy', 'data-lazy-original', 'data-lazy-src', 'data-lazysrc', 'data-lazyload', 'data-bgposition', 'data-envira-src', 'fullurl', 'lazy-slider-img', 'data-srcset', 'data-spai', 'data-src-webp', 'data-srcset-webp', 'data-src-img', 'data-srcset-img', ) ); } /** * Apply lazy load library option. * * @return false|string * @access public * @since 1.2.0 */ public function apply_lazy_load_feature() { $content = $this->content; if ( defined( 'REST_REQUEST' ) || is_feed() || is_admin() || is_comment_feed() || is_preview() || is_robots() || is_trackback() ) { return $content; } if ( false === $this->lazy_load_enabled ) { return $content; } if ( '' === trim( $content ) ) { return $content; } // If this option is set to true then loading="lazy" attribute will be use instead. // The native lazy load is not yet supported by all browsers. ( As of February 2021, 73% of browsers support lazy loading. ) $use_native = apply_filters( 'breeze_use_native_lazy_load', $this->lazy_load_native ); if ( version_compare( PHP_VERSION, '8.2.0', '<' ) ) { $content = mb_convert_encoding( $content, 'HTML-ENTITIES', 'UTF-8' ); } else { $content = mb_encode_numericentity( $content, array( 0x80, 0x10FFFF, 0, ~0 ), 'UTF-8' ); } /** * Fetch all images */ preg_match_all( '/]+>/i', $content, $img_matches ); // Remove any image tags that have \ inside as it's probably targeted by other scripts $img_matches[0] = array_filter( $img_matches[0], function( $tag ) { return strpos( $tag, '\\' ) === false; } ); // Check if images available if ( ! empty( $img_matches[0] ) ) { // Check if required native ll if ( true === $use_native ) { foreach ( $img_matches[0] as $img_match ) { // If we can't find loading attr, we append it if ( ! preg_match( '/loading=[\'"]\s*lazy\s*[\'"]/i', $img_match ) ) { // Forming the new string $img_match_new = preg_replace( '/exclude_if_atts ) && ! empty( $this->exclude_if_atts ) ) { $exclude_it = false; foreach ( $this->exclude_if_atts as $ex_attr ) { preg_match( '/' . $ex_attr . '=(?:"|\')(.+?)(?:"|\')/', $img_match, $attr_value ); if ( ! empty( $attr_value[1] ) ) { $exclude_it = true; } } if ( true === $exclude_it ) { continue; } } // Get the image URL preg_match( '/src=(?:"|\')(.+?)(?:"|\')/', $img_match, $src_value ); $current_src = ! empty( $src_value[1] ) ? $src_value[1] : ''; if ( true !== $this->excluded_images( $current_src ) ) { // Add lazy-load data attribute. $img_match_new = preg_replace( '/()/', '$1$3', $img_match_new ); preg_match( '/width=(?:"|\')(.+?)(?:"|\')/', $img_match, $width_value ); preg_match( '/height=(?:"|\')(.+?)(?:"|\')/', $img_match, $height_value ); $get_width = ! empty( $width_value[1] ) ? $width_value[1] : ''; $get_height = ! empty( $height_value[1] ) ? $height_value[1] : ''; $placeholder = $this->generate_simple_placeholder( $get_width, $get_height ); // Add placeholder image as source $img_match_new = preg_replace( '/()/', '$1$3', $img_match_new ); // Add lazy-load CSS class. $img_match_new = preg_replace( '/(/isU', $content, $iframe_matches ); foreach ( $iframe_matches[0] as $iframe_tag ) { $src = preg_replace( '/^.*src=\"([^\"]+)\".*$/isU', '$1', $iframe_tag ); $allowed_url = false; foreach ( $allowed_iframes_url as $iframe_url ) { if ( false !== strpos( $src, $iframe_url ) ) { $allowed_url = true; break; } } if ( true === $allowed_url ) { // Video Link $video_link = explode( '/', $src ); $video_id = end( $video_link ); // Get classes $current_classes = $this->format_tag_ll_classes( $iframe_tag ); // Forming iframe tag $iframe_tag_new = preg_replace( '/