Lazy Load by WP Rocket - Version 1.0.1

Version Description

  • 16 jul. 2014
  • Fix bug: when a IMG tag or content (widget or post) contains the string "data-no-lazy", all IMG tags were ignored instead of one.
  • Security fix: The preg_replace() could lead to a XSS vuln, thanks to Alexander Concha
  • Code compliance
Download this release

Release Info

Developer wp_media
Plugin Icon 128x128 Lazy Load by WP Rocket
Version 1.0.1
Comparing to
See all releases

Version 1.0.1

Files changed (2) hide show
  1. readme.txt +55 -0
  2. rocket-lazy-load.php +181 -0
readme.txt ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Rocket Lazy Load ===
2
+ Contributors: juliobox, geekpress, wp_media
3
+ Tags: lazyload, lazy load, images, thumbnail, thumbnails, smiley, smilies, avatar, gravatar
4
+ Requires at least: 3.0
5
+ Tested up to: 3.9.1
6
+ Stable tag: 1.0.1
7
+
8
+ The tiny Lazy Load script for WordPress without jQuery or others libraries.
9
+
10
+ == Description ==
11
+
12
+ Lazy Load displays images on a page only when they are visible to the user. This reduces the number of HTTP requests mechanism and improves the loading time.
13
+
14
+ This plugin works on thumbnails, all images in a post content or in a widget text, avatars and smilies. No JavaScript library such as jQuery is used and the script weighs less than 2kb.
15
+
16
+ Simply install the plugin to enjoy a faster website. No options are available : you install it and the plugin takes care of everything.
17
+
18
+ This script is used by WP Rocket plugin cache : http://wp-rocket.me
19
+
20
+ == Installation ==
21
+
22
+ 1. Upload the complete `rocket-lazy-load` folder to the `/wp-content/plugins/` directory
23
+ 2. Activate the plugin through the 'Plugins' menu in WordPress
24
+
25
+ == Frequently Asked Questions ==
26
+
27
+ = How can i deactivate Lazy Load on some pages? =
28
+
29
+ You can use <em>do_rocket_lazyload</em> filter.
30
+
31
+ Here, an example to put in functions.php files :
32
+ `
33
+ add_action( 'wp', 'deactivate_rocket_lazyload_on_single' );
34
+ function deactivate_rocket_lazyload_on_single() {
35
+ if ( is_single() ) {
36
+ add_filter( 'do_rocket_lazyload', '__return_false' );
37
+ }
38
+ }
39
+ `
40
+
41
+ = How can i deactivate Lazy Load on some images? =
42
+
43
+ Simply add a 'data-no-lazy="1"' property in you IMG tag.
44
+
45
+ == Changelog ==
46
+
47
+ = 1.0.1 =
48
+ * 16 jul. 2014
49
+ * Fix bug: when a IMG tag or content (widget or post) contains the string "data-no-lazy", all IMG tags were ignored instead of one.
50
+ * Security fix: The preg_replace() could lead to a XSS vuln, thanks to Alexander Concha
51
+ * Code compliance
52
+
53
+ = 1.0 =
54
+ * 01 jan. 2014
55
+ * Initial release.
rocket-lazy-load.php ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' );
3
+
4
+ /*
5
+ Plugin Name: Rocket Lazy Load
6
+ Plugin URI: http://wordpress.org/plugins/rocket-lazy-load/
7
+ Description: The tiny Lazy Load script for WordPress without jQuery or others libraries.
8
+ Version: 1.0.1
9
+ Author: WP Media
10
+ Author URI: http://wp-rocket.me
11
+
12
+ Copyright 2013 WP Media
13
+
14
+ This program is free software; you can redistribute it and/or modify
15
+ it under the terms of the GNU General Public License as published by
16
+ the Free Software Foundation; either version 2 of the License, or
17
+ (at your option) any later version.
18
+
19
+ This program is distributed in the hope that it will be useful,
20
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
+ GNU General Public License for more details.
23
+
24
+ You should have received a copy of the GNU General Public License
25
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
26
+
27
+ */
28
+
29
+ /**
30
+ * Add Lazy Load JavaScript in the header
31
+ * No jQuery or other library is required !!
32
+ *
33
+ * @since 1.0
34
+ */
35
+ add_action( 'wp_head', 'rocket_lazyload_script', PHP_INT_MAX );
36
+ function rocket_lazyload_script() {
37
+ if ( ! apply_filters( 'do_rocket_lazyload', true ) ) {
38
+ return;
39
+ }
40
+
41
+ echo '<script type="text/javascript">(function(a,e){function f(){var d=0;if(e.body&&e.body.offsetWidth){d=e.body.offsetHeight}if(e.compatMode=="CSS1Compat"&&e.documentElement&&e.documentElement.offsetWidth){d=e.documentElement.offsetHeight}if(a.innerWidth&&a.innerHeight){d=a.innerHeight}return d}function b(g){var d=ot=0;if(g.offsetParent){do{d+=g.offsetLeft;ot+=g.offsetTop}while(g=g.offsetParent)}return{left:d,top:ot}}function c(){var l=e.querySelectorAll("[data-lazy-original]");var j=a.pageYOffset||e.documentElement.scrollTop||e.body.scrollTop;var d=f();for(var k=0;k<l.length;k++){var h=l[k];var g=b(h).top;if(g<(d+j)){h.src=h.getAttribute("data-lazy-original");h.removeAttribute("data-lazy-original")}}}if(a.addEventListener){a.addEventListener("DOMContentLoaded",c,false);a.addEventListener("scroll",c,false)}else{a.attachEvent("onload",c);a.attachEvent("onscroll",c)}})(window,document);</script>';
42
+ }
43
+
44
+
45
+
46
+ /**
47
+ * Replace Gravatar, thumbnails, images in post content and in widget text by LazyLoad
48
+ *
49
+ * @since 1.0
50
+ */
51
+ add_filter( 'get_avatar', 'rocket_lazyload_images', PHP_INT_MAX );
52
+ add_filter( 'the_content', 'rocket_lazyload_images', PHP_INT_MAX );
53
+ add_filter( 'widget_text', 'rocket_lazyload_images', PHP_INT_MAX );
54
+ add_filter( 'post_thumbnail_html', 'rocket_lazyload_images', PHP_INT_MAX );
55
+ function rocket_lazyload_images( $html ) {
56
+ // Don't LazyLoad if the thumbnail is in admin, a feed or a post preview
57
+ if( is_admin() || is_feed() || is_preview() || empty( $html ) ) {
58
+ return $html;
59
+ }
60
+
61
+ // You can stop the LalyLoad process with a hook
62
+ if ( ! apply_filters( 'do_rocket_lazyload', true ) ) {
63
+ return $html;
64
+ }
65
+
66
+ $html = preg_replace_callback( '#<img([^>]*) src=("(?:[^"]+)"|\'(?:[^\']+)\'|(?:[^ >]+))([^>]*)>#', '__rocket_lazyload_replace_callback', $html );
67
+
68
+ return $html;
69
+ }
70
+
71
+
72
+ /**
73
+ * Used to check if we have to LazyLoad this or not
74
+ *
75
+ * @since 1.0.1
76
+ */
77
+ function __rocket_lazyload_replace_callback( $matches ) {
78
+ if ( strpos( $matches[1] . $matches[3], 'data-no-lazy=' ) === false && strpos( $matches[1] . $matches[3], 'data-lazy-original=' ) === false ) {
79
+ return sprintf( '<img%1$ssrc="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" data-lazy-original=%2$s%3$s><noscript><img%1$ssrc=%2$s%3$s></noscript>',
80
+ $matches[1], $matches[2], $matches[3] );
81
+ } else {
82
+ return $matches[0];
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Replace WordPress smilies by Lazy Load
88
+ *
89
+ * @since 1.0
90
+ */
91
+ remove_filter( 'the_content', 'convert_smilies' );
92
+ remove_filter( 'the_excerpt', 'convert_smilies' );
93
+ remove_filter( 'comment_text', 'convert_smilies' );
94
+
95
+ add_filter( 'the_content', 'rocket_convert_smilies' );
96
+ add_filter( 'the_excerpt', 'rocket_convert_smilies' );
97
+ add_filter( 'comment_text', 'rocket_convert_smilies' );
98
+
99
+ /**
100
+ * Convert text equivalent of smilies to images.
101
+ *
102
+ * @source convert_smilies() in /wp-includes/formattings.php
103
+ * @since 1.0
104
+ */
105
+ function rocket_convert_smilies( $text ) {
106
+
107
+ global $wp_smiliessearch;
108
+ $output = '';
109
+
110
+ if ( get_option( 'use_smilies' ) && ! empty( $wp_smiliessearch ) ) {
111
+ // HTML loop taken from texturize function, could possible be consolidated
112
+ $textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // capture the tags as well as in between
113
+ $stop = count( $textarr );// loop stuff
114
+
115
+ // Ignore proessing of specific tags
116
+ $tags_to_ignore = 'code|pre|style|script|textarea';
117
+ $ignore_block_element = '';
118
+
119
+ for ( $i = 0; $i < $stop; $i++ ) {
120
+ $content = $textarr[ $i ];
121
+
122
+ // If we're in an ignore block, wait until we find its closing tag
123
+ if ( '' == $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')>/', $content, $matches ) ) {
124
+ $ignore_block_element = $matches[1];
125
+ }
126
+
127
+ // If it's not a tag and not in ignore block
128
+ if ( '' == $ignore_block_element && strlen( $content ) > 0 && '<' != $content[0] ) {
129
+ $content = preg_replace_callback( $wp_smiliessearch, 'rocket_translate_smiley', $content );
130
+ }
131
+
132
+ // did we exit ignore block
133
+ if ( '' != $ignore_block_element && '</' . $ignore_block_element . '>' == $content ) {
134
+ $ignore_block_element = '';
135
+ }
136
+
137
+ $output .= $content;
138
+ }
139
+ } else {
140
+ // return default text.
141
+ $output = $text;
142
+ }
143
+ return $output;
144
+ }
145
+
146
+ /**
147
+ * Convert one smiley code to the icon graphic file equivalent.
148
+ *
149
+ * @source translate_smiley() in /wp-includes/formattings.php
150
+ * @since 1.0
151
+ */
152
+ function rocket_translate_smiley( $matches ) {
153
+
154
+ global $wpsmiliestrans;
155
+
156
+ if ( ! count( $matches ) ) {
157
+ return '';
158
+ }
159
+
160
+ $smiley = trim( reset( $matches ) );
161
+ $img = $wpsmiliestrans[ $smiley ];
162
+
163
+ /**
164
+ * Filter the Smiley image URL before it's used in the image element.
165
+ *
166
+ * @since WP 2.9.0
167
+ *
168
+ * @param string $smiley_url URL for the smiley image.
169
+ * @param string $img Filename for the smiley image.
170
+ * @param string $site_url Site URL, as returned by site_url().
171
+ */
172
+ $src_url = apply_filters( 'smilies_src', includes_url( "images/smilies/$img" ), $img, site_url() );
173
+
174
+ // Don't lazy-load if process is stopped with a hook
175
+ if ( apply_filters( 'do_rocket_lazyload', true ) ) {
176
+ return sprintf( ' <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" data-lazy-original="%s" alt="%s" class="wp-smiley" /> ', esc_url( $src_url ), esc_attr( $smiley ) );
177
+ } else {
178
+ return sprintf( ' <img src="%s" alt="%s" class="wp-smiley" /> ', esc_url( $src_url ), esc_attr( $smiley ) );
179
+ }
180
+
181
+ }