From ad7fd3174b52bfbbaf39b24a786c6fcb88e78af0 Mon Sep 17 00:00:00 2001 From: Jogesh Date: Mon, 20 Jan 2020 13:22:34 +0530 Subject: [PATCH] Update - Lazy load for iframe --- README.md | 2 +- omni-lazy-load.php | 2 +- public/class-omni-lazyload-public.php | 56 +++++++++++++++++++++------ 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 1d8e73d..bb9de5d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Omni Lazy Load -The best plugin to for Images to boost WordPress Performance. +The best plugin to for Images and IFrames to boost WordPress Performance. ## Installation diff --git a/omni-lazy-load.php b/omni-lazy-load.php index 74a2c9f..b2fe3f4 100644 --- a/omni-lazy-load.php +++ b/omni-lazy-load.php @@ -7,7 +7,7 @@ * @wordpress-plugin * Plugin Name: Omni Lazy Load * Plugin URI: https://webomnizz.com/wordpress-plugins/omni-lazy-load-image/ - * Description: WordPress lazy loader for Images to boost your website performance. + * Description: WordPress lazy loader for Images and IFrames to boost your website performance. * Version: 1.0.0 * Author: Jogesh * Author URI: https://webomnizz.com/ diff --git a/public/class-omni-lazyload-public.php b/public/class-omni-lazyload-public.php index 46ea744..42d21a3 100644 --- a/public/class-omni-lazyload-public.php +++ b/public/class-omni-lazyload-public.php @@ -95,22 +95,56 @@ public function the_content_filter( $content ) { $dom = new DOMDocument(); @$dom->loadHTML($content); + // Apply Lazy Image foreach ($dom->getElementsByTagName('img') as $node) { + $this->lazy_image( $node ); + } - $oldsrc = $node->getAttribute('src'); - $old_srcset = $node->getAttribute('srcset'); - $old_classes = $node->getAttribute('class'); - - $node->setAttribute("data-src", $oldsrc ); - $node->setAttribute("data-srcset", $old_srcset); - - $node->removeAttribute("srcset"); - $node->removeAttribute("src"); - - $node->setAttribute("class", trim($old_classes) . " lazyload"); + // Apply Lazy IFrame + foreach ($dom->getElementsByTagName('iframe') as $node) { + $this->lazy_iframe( $node ); } $newHtml = preg_replace('/^/', '', str_replace( array('', '', '', ''), array('', '', '', ''), $dom->saveHTML())); return $newHtml; } + + /** + * Apply Lazyloading on image + * + * @param object $node + * @param string $class + * + * @since 1.0.0 + * @return void + */ + private function lazy_image( $node, $class = 'lazyload' ) { + + $oldsrc = $node->getAttribute('src'); + $old_srcset = $node->getAttribute('srcset'); + $old_classes = $node->getAttribute('class'); + + $node->setAttribute("data-src", $oldsrc ); + $node->setAttribute("data-srcset", $old_srcset); + + $node->removeAttribute("srcset"); + $node->removeAttribute("src"); + + $node->setAttribute("class", trim($old_classes) . " {$class}"); + } + + /** + * Apply Lazyloading on iframe + * + * @param object $node + * @param string $class + * + * @since 1.0.0 + * @return void + */ + private function lazy_iframe( $node, $class = 'lazyload' ) { + + $old_classes = $node->getAttribute('class'); + $node->setAttribute("class", trim($old_classes) . " {$class}"); + } }