Changeset 2716731
- Timestamp:
- 04/30/2022 10:08:08 PM (4 years ago)
- Location:
- ng-lazyload/trunk
- Files:
-
- 2 added
- 3 edited
-
NG-Lazyload.php (modified) (3 diffs)
-
assets (added)
-
assets/icon-128x128.png (added)
-
plugin.js (modified) (1 diff)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
ng-lazyload/trunk/NG-Lazyload.php
r2250901 r2716731 5 5 Description: Implements lazyload for thumbnails and content images 6 6 Author: Nikita Menshutin 7 Version: 1. 77 Version: 1.8 8 8 Author URI: http://nikita.global 9 9 … … 16 16 @link http://nikita.global 17 17 * */ 18 defined('ABSPATH') or die("No script kiddies please!"); 19 if (!class_exists("nglazyload")) { 20 /** 21 * Our main class goes here 22 * 23 * @category NikitaGlobal 24 * @package NikitaGlobal 25 * @author Nikita Menshutin <wpplugins@nikita.global> 26 * @license http://opensource.org/licenses/gpl-license.php GNU Public License 27 * @link http://nikita.global 28 */ 29 class Nglazyload 30 { 31 /** 32 * Construct method 33 * 34 * @return void 35 */ 36 public function __construct() 37 { 38 $this->prefix = 'nglazyload'; 39 $this->version = '1.7'; 40 add_action('wp_enqueue_scripts', array($this, 'scripts')); 41 add_filter( 42 'post_thumbnail_html', 43 array( 44 $this, 45 'filterContentTags', 46 ) 47 ); 48 add_filter( 49 'wp_get_attachment_image_attributes', 50 array( 51 $this, 52 'thumbnailFilter', 53 ), 54 10, 55 3 56 ); 57 add_filter('the_content', array($this, 'filterContentTags')); 58 add_filter('the_content', array($this, 'filterContentBackgroundImages')); 59 } 60 61 /** 62 * Filtering thumbnail attributes 63 * 64 * @param array $attr attributes 65 * @param object $attachment att 66 * @param array $size size 67 * 68 * @return array with added lazyload attributes 69 */ 70 public function thumbnailFilter($attr, $attachment, $size) 71 { 72 $attr[NGLL::dataAttr()] = $attr['src']; 73 $attr['src'] = NGLL::dataImg(); 74 return $attr; 75 } 76 77 /** 78 * Replacing all background images in styles with 79 * lazy-load attributes 80 * 81 * @param string $content html content 82 * 83 * @return string updated images if any 84 */ 85 public function filterContentBackgroundImages($content) 86 { 87 $match = '#<[^>]*background\-image[^url]*url[^(]*\(([^\)]*)\)[^>]*>#'; 88 return preg_replace_callback( 89 $match, 90 function ($matches) { 91 $newtag = $matches[0]; 92 $url = $matches[1]; 93 $newtag = str_replace($url, NGLL::dataImg(), $newtag); 94 $newtag = str_replace( 95 '>', 96 NGLL::dataAttrValue($url, true) . 97 '>', 98 $newtag 99 ); 100 return $newtag; 101 }, 102 $content 103 ); 104 } 105 106 /** 107 * Replacing all images with 108 * lazy-load attributes 109 * 110 * @param string $content html content 111 * 112 * @return string updated images if any 113 */ 114 public function filterContentTags($content) 115 { 116 $xmlprefix = '<?xml encoding="utf-8" ?>'; 117 $doc = new DOMDocument('1.0', 'UTF-8'); 118 @$doc->loadHTML( 119 $xmlprefix . $content //, 120 // LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD 121 ); 122 $images = $doc->getElementsByTagName('img'); 123 if ($images->length == 0) { 124 return $content; 125 } 126 foreach ($images as $image) { 127 $src = $image->getAttribute('src'); 128 $image->setAttribute('src', NGLL::dataImg()); 129 $image->setAttribute(NGLL::dataAttr(), $src); 130 } 131 return html_entity_decode( 132 str_replace( 133 $xmlprefix, 134 '', 135 preg_replace( 136 '~<(?:!DOCTYPE|/?(?:html|body))[^>]*>\s*~i', 137 '', 138 $doc->saveHTML() 139 ) 140 ) 141 ); 142 } 143 144 /** 145 * Enqueue plugin.js 146 * 147 * @return void 148 */ 149 public function scripts() 150 { 151 wp_register_style($this->prefix.'css', 152 plugin_dir_url(__FILE__).'/nglazyload.css', 153 array(), 154 $this->version 155 ); 156 wp_enqueue_style($this->prefix.'css'); 157 wp_register_script( 158 $this->prefix, 159 plugin_dir_url(__FILE__) . '/plugin.js', 160 array('jquery'), 161 $this->version, 162 true 163 ); 164 wp_localize_script( 165 $this->prefix, 166 $this->prefix, 167 array( 168 'ajax_url' => admin_url('admin-ajax.php'), 169 ) 170 ); 171 wp_enqueue_script($this->prefix); 172 } 173 } 18 defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); 19 if ( ! class_exists( 'nglazyload' ) ) { 20 /** 21 * Our main class goes here 22 * 23 * @category NikitaGlobal 24 * @package NikitaGlobal 25 * @author Nikita Menshutin <wpplugins@nikita.global> 26 * @license http://opensource.org/licenses/gpl-license.php GNU Public License 27 * @link http://nikita.global 28 */ 29 class Nglazyload { 30 31 /** 32 * Construct method 33 * 34 * @return void 35 */ 36 public function __construct() { 37 $this->prefix = 'nglazyload'; 38 $this->version = '1.7'; 39 add_action( 'wp_enqueue_scripts', array( $this, 'scripts' ) ); 40 add_filter( 41 'post_thumbnail_html', 42 array( 43 $this, 44 'filterContentTags', 45 ) 46 ); 47 add_filter( 48 'wp_get_attachment_image_attributes', 49 array( 50 $this, 51 'thumbnailFilter', 52 ), 53 999, 54 3 55 ); 56 add_filter( 'the_content', array( $this, 'filterContentTags' ) ); 57 add_filter( 'the_content', array( $this, 'filterContentBackgroundImages' ) ); 58 } 59 60 /** 61 * Filtering thumbnail attributes 62 * 63 * @param array $attr attributes 64 * @param object $attachment att 65 * @param array $size size 66 * 67 * @return array with added lazyload attributes 68 */ 69 public function thumbnailFilter( $attr, $attachment, $size ) { 70 $attr[ NGLL::dataAttr() ] = $attr['src']; 71 $attr['src'] = NGLL::dataImg(); 72 return $attr; 73 } 74 75 /** 76 * Replacing all background images in styles with 77 * lazy-load attributes 78 * 79 * @param string $content html content 80 * 81 * @return string updated images if any 82 */ 83 public function filterContentBackgroundImages( $content ) { 84 $match = '#<[^>]*background\-image[^url]*url[^(]*\(([^\)]*)\)[^>]*>#'; 85 return preg_replace_callback( 86 $match, 87 function ( $matches ) { 88 $newtag = $matches[0]; 89 $url = $matches[1]; 90 $newtag = str_replace( $url, NGLL::dataImg(), $newtag ); 91 $newtag = str_replace( 92 '>', 93 NGLL::dataAttrValue( $url, true ) . 94 '>', 95 $newtag 96 ); 97 return $newtag; 98 }, 99 $content 100 ); 101 } 102 103 /** 104 * Replacing all images with 105 * lazy-load attributes 106 * 107 * @param string $content html content 108 * 109 * @return string updated images if any 110 */ 111 public function filterContentTags( $content ) { 112 $xmlprefix = '<?xml encoding="utf-8" ?>'; 113 $doc = new DOMDocument( '1.0', 'UTF-8' ); 114 @$doc->loadHTML( 115 $xmlprefix . $content // , 116 // LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD 117 ); 118 $images = $doc->getElementsByTagName( 'img' ); 119 if ( $images->length == 0 ) { 120 return $content; 121 } 122 foreach ( $images as $image ) { 123 if ( $image->hasAttribute( NGLL::dataAttr() ) ) { 124 continue; 125 } 126 $src = $image->getAttribute( 'src' ); 127 $image->setAttribute( 'src', NGLL::dataImg() ); 128 $image->setAttribute( NGLL::dataAttr(), $src ); 129 } 130 return html_entity_decode( 131 str_replace( 132 $xmlprefix, 133 '', 134 preg_replace( 135 '~<(?:!DOCTYPE|/?(?:html|body))[^>]*>\s*~i', 136 '', 137 $doc->saveHTML() 138 ) 139 ) 140 ); 141 } 142 143 /** 144 * Enqueue plugin.js 145 * 146 * @return void 147 */ 148 public function scripts() { 149 wp_register_style( 150 $this->prefix . 'css', 151 plugin_dir_url( __FILE__ ) . '/nglazyload.css', 152 array(), 153 $this->version 154 ); 155 wp_enqueue_style( $this->prefix . 'css' ); 156 wp_register_script( 157 $this->prefix, 158 plugin_dir_url( __FILE__ ) . '/plugin.js', 159 array( 'jquery' ), 160 $this->version, 161 true 162 ); 163 wp_localize_script( 164 $this->prefix, 165 $this->prefix, 166 array( 167 'ajax_url' => admin_url( 'admin-ajax.php' ), 168 ) 169 ); 170 wp_enqueue_script( $this->prefix ); 171 } 172 } 174 173 } 175 174 new nglazyload(); … … 184 183 * @link http://nikita.global 185 184 */ 186 abstract class NGLL 187 { 188 /** 189 * Attribue name 190 * 191 * @return string attribute name 192 */ 193 public static function dataAttr() 194 { 195 return 'data-ngll-src'; 196 } 197 198 /** 199 * Generate data html tag attribute for real image 200 * 201 * @param string $src string with link 202 * @param bool $background if tag for background image 203 * 204 * @return string data attr 205 */ 206 public static function dataAttrValue($src, $background = false) 207 { 208 $suffix = ''; 209 if ($background) { 210 $suffix = 'b'; 211 } 212 return ' ' . self::dataAttr() . $suffix . '="' . $src . '"'; 213 } 214 215 /** 216 * Base64 encoded 1x1 white gif 217 * The smallest possible image src 218 * 219 * @return string pixel 220 */ 221 public static function dataImg() 222 { 223 return 'data:image/gif;base64,' . 224 'R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='; 225 } 185 abstract class NGLL { 186 187 /** 188 * Attribue name 189 * 190 * @return string attribute name 191 */ 192 public static function dataAttr() { 193 return 'data-ngll-src'; 194 } 195 196 /** 197 * Generate data html tag attribute for real image 198 * 199 * @param string $src string with link 200 * @param bool $background if tag for background image 201 * 202 * @return string data attr 203 */ 204 public static function dataAttrValue( $src, $background = false ) { 205 $suffix = ''; 206 if ( $background ) { 207 $suffix = 'b'; 208 } 209 return ' ' . self::dataAttr() . $suffix . '="' . $src . '"'; 210 } 211 212 /** 213 * Base64 encoded 1x1 white gif 214 * The smallest possible image src 215 * 216 * @return string pixel 217 */ 218 public static function dataImg() { 219 return 'data:image/gif;base64,' . 220 'R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='; 221 } 226 222 } -
ng-lazyload/trunk/plugin.js
r2250901 r2716731 29 29 30 30 jQuery.fn.isInViewport = function () { 31 varelementTop = jQuery(this).offset().top;32 varelementBottom = elementTop + jQuery(this).outerHeight();33 varviewportTop = jQuery(window).scrollTop();34 varviewportBottom = viewportTop + jQuery(window).height()*2;31 let elementTop = jQuery(this).offset().top; 32 let elementBottom = elementTop + jQuery(this).outerHeight(); 33 let viewportTop = jQuery(window).scrollTop(); 34 let viewportBottom = viewportTop + jQuery(window).height()*2; 35 35 return elementBottom > viewportTop && elementTop < viewportBottom; 36 36 }; -
ng-lazyload/trunk/readme.txt
r2250901 r2716731 6 6 Author: Nikita Menshutin 7 7 Requires at least: 3.6 8 Tested up to: 5. 3.29 Stable tag: 1. 68 Tested up to: 5.9.3 9 Stable tag: 1.8 10 10 Requires PHP: 5.6 11 11 Version: 1.4 … … 38 38 39 39 == Changelog == 40 = 1.8 (2022-05-01) 41 * Tested up to 5.9.3 42 * Minor js updates 43 * Fixed one filters' conflict 40 44 41 45 = 1.7 (2020-02-26)
Note: See TracChangeset
for help on using the changeset viewer.