));
add_filter('script_loader_tag', array('Perfmatters\LazyLoad', 'async_script'), 10, 2);
add_action('wp_head', array('Perfmatters\LazyLoad', 'lazyload_css'), PHP_INT_MAX);
//images only
if(!empty(Config::$options['lazyload']['lazy_loading'])) {
add_filter('wp_get_attachment_image_attributes', function($attr) {
unset($attr['loading']);
return $attr;
});
}
}
//lazy load buffer
public static function lazyload_buffer($html) {
$buffer = Utilities::clean_html($html);
//replace image tags
if(!empty(Config::$options['lazyload']['lazy_loading'])) {
$html = self::lazyload_parent_exclusions($html, $buffer);
$html = self::lazyload_pictures($html, $buffer);
$html = self::lazyload_background_images($html, $buffer);
$html = self::lazyload_images($html, $buffer);
}
//replace css background elements
if(!empty(Config::$options['lazyload']['css_background_images'])) {
$html = self::lazyload_css_background_images($html, $buffer);
}
return $html;
}
//enqueue lazy load script
public static function enqueue_scripts() {
wp_register_script('perfmatters-lazy-load', plugins_url('perfmatters/js/lazyload.min.js'), array(), PERFMATTERS_VERSION, true);
wp_enqueue_script('perfmatters-lazy-load');
wp_add_inline_script('perfmatters-lazy-load', self::inline_js(), 'before');
}
//add async tag to enqueued script
public static function async_script($tag, $handle) {
if($handle !== 'perfmatters-lazy-load') {
return $tag;
}
return str_replace(' src', ' async src', $tag);
}
//print lazy load styles
public static function lazyload_css() {
//print noscript styles
if(apply_filters('perfmatters_lazyload_noscript', true)) {
echo '';
}
$styles = '';
//youtube thumbnails
if(!empty(Config::$options['lazyload']['lazy_loading_iframes']) && !empty(Config::$options['lazyload']['youtube_preview_thumbnails'])) {
$styles.= '.perfmatters-lazy-youtube{position:relative;width:100%;max-width:100%;height:0;padding-bottom:56.23%;overflow:hidden}.perfmatters-lazy-youtube img{position:absolute;top:0;right:0;bottom:0;left:0;display:block;width:100%;max-width:100%;height:auto;margin:auto;border:none;cursor:pointer;transition:.5s all;-webkit-transition:.5s all;-moz-transition:.5s all}.perfmatters-lazy-youtube img:hover{-webkit-filter:brightness(75%)}.perfmatters-lazy-youtube .play{position:absolute;top:50%;left:50%;right:auto;width:68px;height:48px;margin-left:-34px;margin-top:-24px;background:url('.plugins_url('perfmatters/img/youtube.svg').') no-repeat;background-position:center;background-size:cover;pointer-events:none;filter:grayscale(1)}.perfmatters-lazy-youtube:hover .play{filter:grayscale(0)}.perfmatters-lazy-youtube iframe{position:absolute;top:0;left:0;width:100%;height:100%;z-index:99}';
if(current_theme_supports('responsive-embeds')) {
$styles.= '.wp-has-aspect-ratio .wp-block-embed__wrapper{position:relative;}.wp-has-aspect-ratio .perfmatters-lazy-youtube{position:absolute;top:0;right:0;bottom:0;left:0;width:100%;height:100%;padding-bottom:0}';
}
}
//fade in effect
if(!empty(Config::$options['lazyload']['fade_in'])) {
$styles.= '.perfmatters-lazy.pmloaded,.perfmatters-lazy.pmloaded>img,.perfmatters-lazy>img.pmloaded,.perfmatters-lazy[data-ll-status=entered]{animation:' . apply_filters('perfmatters_fade_in_speed', 500) . 'ms pmFadeIn}@keyframes pmFadeIn{0%{opacity:0}100%{opacity:1}}';
}
//css background images
if(!empty(Config::$options['lazyload']['css_background_images'])) {
$styles.='body .perfmatters-lazy-css-bg:not([data-ll-status=entered]),body .perfmatters-lazy-css-bg:not([data-ll-status=entered]) *,body .perfmatters-lazy-css-bg:not([data-ll-status=entered])::before,body .perfmatters-lazy-css-bg:not([data-ll-status=entered])::after{background-image:none!important;will-change:transform;transition:opacity 0.025s ease-in,transform 0.025s ease-in!important;}';
}
//print styles
if(!empty($styles)) {
echo '';
}
}
//inline lazy load js
private static function inline_js() {
$threshold = apply_filters('perfmatters_lazyload_threshold', !empty(Config::$options['lazyload']['threshold']) ? Config::$options['lazyload']['threshold'] : '0px');
if(ctype_digit($threshold)) {
$threshold.= 'px';
}
//declare lazy load options
$output = 'window.lazyLoadOptions={elements_selector:"img[data-src],.perfmatters-lazy,.perfmatters-lazy-css-bg",thresholds:"' . $threshold . ' 0px",class_loading:"pmloading",class_loaded:"pmloaded",callback_loaded:function(element){if(element.tagName==="IFRAME"){if(element.classList.contains("pmloaded")){if(typeof window.jQuery!="undefined"){if(jQuery.fn.fitVids){jQuery(element).parent().fitVids()}}}}}};';
//lazy loader initialized
$output.= 'window.addEventListener("LazyLoad::Initialized",function(e){var lazyLoadInstance=e.detail.instance;';
//dom monitoring
if(!empty(Config::$options['lazyload']['lazy_loading_dom_monitoring'])) {
$output.= 'var target=document.querySelector("body");var observer=new MutationObserver(function(mutations){lazyLoadInstance.update()});var config={childList:!0,subtree:!0};observer.observe(target,config);';
}
$output.= '});';
//youtube thumbnails
if(!empty(Config::$options['lazyload']['lazy_loading_iframes']) && !empty(Config::$options['lazyload']['youtube_preview_thumbnails'])) {
$autoplay = apply_filters('perfmatters_lazyload_youtube_autoplay', true);
$output.= 'function perfmattersLazyLoadYouTube(e){var t=document.createElement("iframe"),r="ID?";r+=0===e.dataset.query.length?"":e.dataset.query+"&"' . ($autoplay ? ',r+="autoplay=1"' : '') . ',t.setAttribute("src",r.replace("ID",e.dataset.src)),t.setAttribute("frameborder","0"),t.setAttribute("allowfullscreen","1"),t.setAttribute("allow","accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"),e.replaceChild(t,e.firstChild)}';
}
return $output;
}
//lazy load img tags
private static function lazyload_images($html, $buffer) {
//match all img tags
preg_match_all('#]+?)\/?>#is', $buffer, $images, PREG_SET_ORDER);
if(!empty($images)) {
$lazy_image_count = 0;
$exclude_leading_images = apply_filters('perfmatters_exclude_leading_images', Config::$options['lazyload']['exclude_leading_images'] ?? 0);
$leading_image_exclusions = apply_filters('perfmatters_leading_image_exclusions', array(
'data-perfmatters-leading-skip'
));
//remove any duplicate images
$images = array_unique($images, SORT_REGULAR);
//loop through images
foreach($images as $image) {
$leading = true;
//check for leading image exclusion
if(!empty($leading_image_exclusions) && is_array($leading_image_exclusions)) {
foreach($leading_image_exclusions as $exclusion) {
if(strpos($image[0], $exclusion) !== false) {
$leading = false;
}
}
}
//skip leading images
if($leading) {
$lazy_image_count++;
if($lazy_image_count <= $exclude_leading_images) {
continue;
}
}
//prepare lazy load image
$lazy_image = self::lazyload_image($image);
//replace image in html
$html = str_replace($image[0], $lazy_image, $html);
}
}
return $html;
}
//lazy load picture tags for webp
private static function lazyload_pictures($html, $buffer) {
//match all picture tags
preg_match_all('#