Lazy Load by WP Rocket - Version 1.4

Version Description

  • New option: replace Youtube videos by thumbnail. This option can improve your loading time a lot, especially if you have multiple videos on the same page
Download this release

Release Info

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

Code changes from version 1.3.3 to 1.4

Files changed (4) hide show
  1. admin/admin.php +6 -1
  2. assets/img/play.png +0 -0
  3. readme.txt +5 -1
  4. rocket-lazy-load.php +119 -64
admin/admin.php CHANGED
@@ -90,7 +90,12 @@ function rocket_lazyload_options_output() {
90
  <li class="rocket-lazyload-option">
91
  <input type="checkbox" value="1" id="lazyload-iframes" name="rocket_lazyload_options[iframes]" <?php checked( rocket_lazyload_get_option( 'iframes', 0 ), 1 ); ?> aria-describedby="describe-lazyload-iframes">
92
  <label for="lazyload-iframes"><span class="screen-reader-text"><?php _e( 'Iframes & Videos', 'rocket-lazyload' ); ?></span></label>
93
- <span id="describe-lazyload-images" class="rocket-lazyload-label-description"><?php _e( 'Iframes & Videos', 'rocket-lazyload' ); ?></span>
 
 
 
 
 
94
  </li>
95
  </ul>
96
  </fieldset>
90
  <li class="rocket-lazyload-option">
91
  <input type="checkbox" value="1" id="lazyload-iframes" name="rocket_lazyload_options[iframes]" <?php checked( rocket_lazyload_get_option( 'iframes', 0 ), 1 ); ?> aria-describedby="describe-lazyload-iframes">
92
  <label for="lazyload-iframes"><span class="screen-reader-text"><?php _e( 'Iframes & Videos', 'rocket-lazyload' ); ?></span></label>
93
+ <span id="describe-lazyload-iframes" class="rocket-lazyload-label-description"><?php _e( 'Iframes & Videos', 'rocket-lazyload' ); ?></span>
94
+ </li>
95
+ <li class="rocket-lazyload-option">
96
+ <input type="checkbox" value="1" id="lazyload-youtube" name="rocket_lazyload_options[youtube]" <?php checked( rocket_lazyload_get_option( 'youtube', 0 ), 1 ); ?> aria-describedby="describe-lazyload-youtube">
97
+ <label for="lazyload-youtube"><span class="screen-reader-text"><?php _e( 'Replace Youtube videos by thumbnail', 'rocket-lazyload' ); ?></span></label>
98
+ <span id="describe-lazyload-youtube" class="rocket-lazyload-label-description"><?php _e( 'Replace Youtube videos by thumbnail', 'rocket-lazyload' ); ?></span>
99
  </li>
100
  </ul>
101
  </fieldset>
assets/img/play.png ADDED
Binary file
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: creativejuiz, tabrisrp, wp_media
3
  Tags: lazyload, lazy load, images, iframes, thumbnail, thumbnails, smiley, smilies, avatar, gravatar
4
  Requires at least: 3.0
5
  Tested up to: 4.8.1
6
- Stable tag: 1.3.3
7
 
8
  The tiny Lazy Load script for WordPress without jQuery, works for images and iframes.
9
 
@@ -50,6 +50,10 @@ You can also use the filters `rocket_lazyload_excluded_attributes` or `rocket_la
50
  Some plugins are not compatible without lazy loading. Please open a support thread, and we will see how we can solve the issue by excluding lazy loading for this plugin.
51
 
52
  == Changelog ==
 
 
 
 
53
  = 1.3.3 =
54
  * 2017-09-16
55
  * Prevent scripts and styles being removed during html parsing
3
  Tags: lazyload, lazy load, images, iframes, thumbnail, thumbnails, smiley, smilies, avatar, gravatar
4
  Requires at least: 3.0
5
  Tested up to: 4.8.1
6
+ Stable tag: 1.4
7
 
8
  The tiny Lazy Load script for WordPress without jQuery, works for images and iframes.
9
 
50
  Some plugins are not compatible without lazy loading. Please open a support thread, and we will see how we can solve the issue by excluding lazy loading for this plugin.
51
 
52
  == Changelog ==
53
+
54
+ = 1.4 =
55
+ * New option: replace Youtube videos by thumbnail. This option can improve your loading time a lot, especially if you have multiple videos on the same page
56
+
57
  = 1.3.3 =
58
  * 2017-09-16
59
  * Prevent scripts and styles being removed during html parsing
rocket-lazy-load.php CHANGED
@@ -1,11 +1,17 @@
1
  <?php
 
 
 
 
 
 
2
  defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' );
3
 
4
  /**
5
  * Plugin Name: Lazy Load by WP Rocket
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.3.3
9
  * Requires PHP: 5.4
10
  * Author: WP Media
11
  * Author URI: https://wp-rocket.me
@@ -27,7 +33,7 @@ defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' );
27
  * You should have received a copy of the GNU General Public License
28
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
29
  */
30
- define( 'ROCKET_LL_VERSION', '1.3.3' );
31
  define( 'ROCKET_LL_PATH', realpath( plugin_dir_path( __FILE__ ) ) . '/' );
32
  define( 'ROCKET_LL_3RD_PARTY_PATH', ROCKET_LL_PATH . '3rd-party/' );
33
  define( 'ROCKET_LL_ASSETS_URL', plugin_dir_url( __FILE__ ) . 'assets/' );
@@ -44,7 +50,6 @@ define( 'ROCKET_LL_JS_VERSION' , '8.0.3' );
44
  function rocket_lazyload_init() {
45
  load_plugin_textdomain( 'rocket-lazy-load', false, basename( dirname( __FILE__ ) ) . '/languages/' );
46
 
47
- require_once ROCKET_LL_PATH . 'vendor/autoload.php';
48
  require ROCKET_LL_3RD_PARTY_PATH . '3rd-party.php';
49
 
50
  if ( is_admin() ) {
@@ -60,7 +65,7 @@ if ( version_compare( PHP_VERSION, '5.4', '<' ) ) {
60
  */
61
  function rocket_lazyload_php_warning() {
62
  echo '<div class="error"><p>' . __( 'Rocket LazyLoad requires PHP 5.4 to function properly. Please upgrade PHP. The Plugin has been auto-deactivated.', 'rocket-lazy-load' ) . '</p></div>';
63
- if ( isset( $_GET['activate'] ) ) {
64
  unset( $_GET['activate'] );
65
  }
66
  }
@@ -109,8 +114,7 @@ function rocket_lazyload_script() {
109
  $threshold = apply_filters( 'rocket_lazyload_threshold', 300 );
110
 
111
  echo <<<HTML
112
- <script>
113
- window.lazyLoadOptions = {
114
  elements_selector: "img, iframe",
115
  data_src: "lazySrc",
116
  data_srcset: "lazySrcset",
@@ -128,9 +132,14 @@ function rocket_lazyload_script() {
128
  }
129
  }
130
  }
131
- };
132
- </script>
 
 
 
 
133
  HTML;
 
134
  }
135
  add_action( 'wp_footer', 'rocket_lazyload_script', 9 );
136
 
@@ -149,6 +158,14 @@ function rocket_lazyload_enqueue() {
149
  $ll_url = ROCKET_LL_FRONT_JS_URL . 'lazyload-' . ROCKET_LL_JS_VERSION . $suffix . '.js';
150
 
151
  wp_enqueue_script( 'rocket-lazyload', $ll_url, null, null, true );
 
 
 
 
 
 
 
 
152
  }
153
  add_action( 'wp_enqueue_scripts', 'rocket_lazyload_enqueue', PHP_INT_MAX );
154
 
@@ -192,11 +209,12 @@ function rocket_lazyload_images( $html ) {
192
  return $html;
193
  }
194
 
195
- $dom = new PHPHtmlParser\Dom();
196
- $dom->setOptions( [
197
- 'removeScripts' => false,
198
- 'removeStyles' => false,
199
- ] );
 
200
  $dom->load( $html );
201
  $images = $dom->getElementsByTag( 'img' );
202
 
@@ -217,15 +235,15 @@ function rocket_lazyload_images( $html ) {
217
  continue;
218
  }
219
 
220
- $img = new PHPHtmlParser\Dom\Tag( 'img' );
221
 
222
  foreach ( $image_attributes as $key => $value ) {
223
  $img->setAttribute( $key, $value );
224
  }
225
 
226
- $original_image = new PHPHtmlParser\Dom\HtmlNode( $img );
227
- $noscript_tag = new PHPHtmlParser\Dom\Tag( 'noscript' );
228
- $noscript = new PHPHtmlParser\Dom\HtmlNode( $noscript_tag );
229
 
230
  /**
231
  * Filter the LazyLoad placeholder on src attribute
@@ -279,28 +297,34 @@ function rocket_is_excluded_lazyload( $attributes ) {
279
  }
280
  }
281
 
282
- $excluded_attributes = apply_filters( 'rocket_lazyload_excluded_attributes', array(
283
- 'data-no-lazy',
284
- 'data-lazy-original',
285
- 'data-lazy-src',
286
- 'data-lazysrc',
287
- 'data-lazyload',
288
- 'data-bgposition',
289
- 'data-envira-src',
290
- 'fullurl',
291
- 'lazy-slider-img',
292
- 'data-srcset',
293
- ) );
294
-
295
- $excluded_classes = apply_filters( 'rocket_lazyload_excluded_classes', array(
296
- 'ls-l',
297
- 'ls-bg',
298
- ) );
299
-
300
- $excluded_src = apply_filters( 'rocket_lazyload_excluded_src', array(
301
- '/wpcf7_captcha/',
302
- 'timthumb.php?src',
303
- ) );
 
 
 
 
 
 
304
 
305
  $attributes = array_flip( $attributes );
306
 
@@ -452,11 +476,12 @@ function rocket_lazyload_iframes( $html ) {
452
  return $html;
453
  }
454
 
455
- $dom = new PHPHtmlParser\Dom();
456
- $dom->setOptions( [
457
- 'removeScripts' => false,
458
- 'removeStyles' => false,
459
- ] );
 
460
  $dom->load( $html );
461
  $iframes = $dom->getElementsByTag( 'iframe' );
462
 
@@ -477,36 +502,66 @@ function rocket_lazyload_iframes( $html ) {
477
  continue;
478
  }
479
 
480
- $iframe_tag = new PHPHtmlParser\Dom\Tag( 'iframe' );
 
481
 
482
  foreach ( $iframe_attributes as $key => $value ) {
483
  $iframe_tag->setAttribute( $key, $value );
484
  }
485
 
486
- $original_iframe = new PHPHtmlParser\Dom\HtmlNode( $iframe_tag );
487
- $noscript_tag = new PHPHtmlParser\Dom\Tag( 'noscript' );
488
- $noscript = new PHPHtmlParser\Dom\HtmlNode( $noscript_tag );
489
-
490
- /**
491
- * Filter the LazyLoad placeholder on src attribute
492
- *
493
- * @since 1.1
494
- *
495
- * @param string $placeholder placeholder that will be printed.
496
- */
497
- $placeholder = apply_filters( 'rocket_lazyload_placeholder', 'about:blank' );
498
-
499
- $iframe->setAttribute( 'src', $placeholder );
500
- $iframe->setAttribute( 'data-lazy-src', $iframe_attributes['src'] );
501
- $iframe->setAttribute( 'data-rocket-lazyload', 'fitvidscompatible' );
502
-
503
  $noscript->addChild( $original_iframe );
504
 
505
- $parent = $iframe->getParent();
506
- $parent->insertAfter( $noscript, $iframe->id() );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
507
  }
508
 
509
  return $dom;
510
  }
511
  add_filter( 'the_content', 'rocket_lazyload_iframes', PHP_INT_MAX );
512
  add_filter( 'widget_text', 'rocket_lazyload_iframes', PHP_INT_MAX );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  <?php
2
+ require_once realpath( plugin_dir_path( __FILE__ ) ) . '/vendor/autoload.php';
3
+
4
+ use PHPHtmlParser\Dom;
5
+ use PHPHtmlParser\Dom\HtmlNode;
6
+ use PHPHtmlParser\Dom\Tag;
7
+
8
  defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' );
9
 
10
  /**
11
  * Plugin Name: Lazy Load by WP Rocket
12
  * Plugin URI: http://wordpress.org/plugins/rocket-lazy-load/
13
  * Description: The tiny Lazy Load script for WordPress without jQuery or others libraries.
14
+ * Version: 1.4
15
  * Requires PHP: 5.4
16
  * Author: WP Media
17
  * Author URI: https://wp-rocket.me
33
  * You should have received a copy of the GNU General Public License
34
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
35
  */
36
+ define( 'ROCKET_LL_VERSION', '1.4' );
37
  define( 'ROCKET_LL_PATH', realpath( plugin_dir_path( __FILE__ ) ) . '/' );
38
  define( 'ROCKET_LL_3RD_PARTY_PATH', ROCKET_LL_PATH . '3rd-party/' );
39
  define( 'ROCKET_LL_ASSETS_URL', plugin_dir_url( __FILE__ ) . 'assets/' );
50
  function rocket_lazyload_init() {
51
  load_plugin_textdomain( 'rocket-lazy-load', false, basename( dirname( __FILE__ ) ) . '/languages/' );
52
 
 
53
  require ROCKET_LL_3RD_PARTY_PATH . '3rd-party.php';
54
 
55
  if ( is_admin() ) {
65
  */
66
  function rocket_lazyload_php_warning() {
67
  echo '<div class="error"><p>' . __( 'Rocket LazyLoad requires PHP 5.4 to function properly. Please upgrade PHP. The Plugin has been auto-deactivated.', 'rocket-lazy-load' ) . '</p></div>';
68
+ if ( isset( $_GET['activate'] ) ) { // WPCS: CSRF ok.
69
  unset( $_GET['activate'] );
70
  }
71
  }
114
  $threshold = apply_filters( 'rocket_lazyload_threshold', 300 );
115
 
116
  echo <<<HTML
117
+ <script>window.lazyLoadOptions = {
 
118
  elements_selector: "img, iframe",
119
  data_src: "lazySrc",
120
  data_srcset: "lazySrcset",
132
  }
133
  }
134
  }
135
+ };</script>
136
+ HTML;
137
+
138
+ if ( rocket_lazyload_get_option( 'youtube' ) ) {
139
+ echo <<<HTML
140
+ <script>function lazyLoadThumb(e){var t='<img src="https://i.ytimg.com/vi/ID/hqdefault.jpg">',a='<div class="play"></div>';return t.replace("ID",e)+a}function lazyLoadYoutubeIframe(){var e=document.createElement("iframe"),t="https://www.youtube.com/embed/ID?autoplay=1";e.setAttribute("src",t.replace("ID",this.dataset.id)),e.setAttribute("frameborder","0"),e.setAttribute("allowfullscreen","1"),this.parentNode.replaceChild(e,this)}document.addEventListener("DOMContentLoaded",function(){var e,t,a=document.getElementsByClassName("rll-youtube-player");for(t=0;t<a.length;t++)e=document.createElement("div"),e.setAttribute("data-id",a[t].dataset.id),e.innerHTML=lazyLoadThumb(a[t].dataset.id),e.onclick=lazyLoadYoutubeIframe,a[t].appendChild(e)});</script>
141
  HTML;
142
+ }
143
  }
144
  add_action( 'wp_footer', 'rocket_lazyload_script', 9 );
145
 
158
  $ll_url = ROCKET_LL_FRONT_JS_URL . 'lazyload-' . ROCKET_LL_JS_VERSION . $suffix . '.js';
159
 
160
  wp_enqueue_script( 'rocket-lazyload', $ll_url, null, null, true );
161
+
162
+ if ( rocket_lazyload_get_option( 'youtube' ) ) {
163
+ $css = '.rll-youtube-player{position:relative;padding-bottom:56.23%;height:0;overflow:hidden;max-width:100%;background:#000;margin:5px}.rll-youtube-player iframe{position:absolute;top:0;left:0;width:100%;height:100%;z-index:100;background:0 0}.rll-youtube-player img{bottom:0;display:block;left:0;margin:auto;max-width:100%;width:100%;position:absolute;right:0;top:0;border:none;height:auto;cursor:pointer;-webkit-transition:.4s all;-moz-transition:.4s all;transition:.4s all}.rll-youtube-player img:hover{-webkit-filter:brightness(75%)}.rll-youtube-player .play{height:72px;width:72px;left:50%;top:50%;margin-left:-36px;margin-top:-36px;position:absolute;background:url(' . ROCKET_LL_ASSETS_URL . 'img/play.png) no-repeat;cursor:pointer}';
164
+
165
+ wp_register_style( 'rocket-lazyload', false );
166
+ wp_enqueue_style( 'rocket-lazyload' );
167
+ wp_add_inline_style( 'rocket-lazyload', $css );
168
+ }
169
  }
170
  add_action( 'wp_enqueue_scripts', 'rocket_lazyload_enqueue', PHP_INT_MAX );
171
 
209
  return $html;
210
  }
211
 
212
+ $dom = new Dom();
213
+ $dom->setOptions(
214
+ [
215
+ 'cleanupInput' => false,
216
+ ]
217
+ );
218
  $dom->load( $html );
219
  $images = $dom->getElementsByTag( 'img' );
220
 
235
  continue;
236
  }
237
 
238
+ $img = new Tag( 'img' );
239
 
240
  foreach ( $image_attributes as $key => $value ) {
241
  $img->setAttribute( $key, $value );
242
  }
243
 
244
+ $original_image = new HtmlNode( $img );
245
+ $noscript_tag = new Tag( 'noscript' );
246
+ $noscript = new HtmlNode( $noscript_tag );
247
 
248
  /**
249
  * Filter the LazyLoad placeholder on src attribute
297
  }
298
  }
299
 
300
+ $excluded_attributes = apply_filters(
301
+ 'rocket_lazyload_excluded_attributes', array(
302
+ 'data-no-lazy',
303
+ 'data-lazy-original',
304
+ 'data-lazy-src',
305
+ 'data-lazysrc',
306
+ 'data-lazyload',
307
+ 'data-bgposition',
308
+ 'data-envira-src',
309
+ 'fullurl',
310
+ 'lazy-slider-img',
311
+ 'data-srcset',
312
+ )
313
+ );
314
+
315
+ $excluded_classes = apply_filters(
316
+ 'rocket_lazyload_excluded_classes', array(
317
+ 'ls-l',
318
+ 'ls-bg',
319
+ )
320
+ );
321
+
322
+ $excluded_src = apply_filters(
323
+ 'rocket_lazyload_excluded_src', array(
324
+ '/wpcf7_captcha/',
325
+ 'timthumb.php?src',
326
+ )
327
+ );
328
 
329
  $attributes = array_flip( $attributes );
330
 
476
  return $html;
477
  }
478
 
479
+ $dom = new Dom();
480
+ $dom->setOptions(
481
+ [
482
+ 'cleanupInput' => false,
483
+ ]
484
+ );
485
  $dom->load( $html );
486
  $iframes = $dom->getElementsByTag( 'iframe' );
487
 
502
  continue;
503
  }
504
 
505
+ $iframe_tag = new Tag( 'iframe' );
506
+ $parent = $iframe->getParent();
507
 
508
  foreach ( $iframe_attributes as $key => $value ) {
509
  $iframe_tag->setAttribute( $key, $value );
510
  }
511
 
512
+ $original_iframe = new HtmlNode( $iframe_tag );
513
+ $noscript_tag = new Tag( 'noscript' );
514
+ $noscript = new HtmlNode( $noscript_tag );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
515
  $noscript->addChild( $original_iframe );
516
 
517
+ if ( rocket_lazyload_get_option( 'youtube' ) && false !== strpos( $iframe_attributes['src'], 'youtube' ) ) {
518
+ $youtube_id = rocket_lazyload_get_youtube_id_from_url( $iframe_attributes['src'] );
519
+
520
+ $youtube_lazyload_container = new Tag( 'div' );
521
+ $youtube_lazyload_container->setAttribute( 'class', 'rll-youtube-player' );
522
+ $youtube_lazyload_container->setAttribute( 'data-id', $youtube_id );
523
+
524
+ $youtube_lazyload_container = new HtmlNode( $youtube_lazyload_container );
525
+
526
+ $parent->replaceChild( $iframe->id(), $youtube_lazyload_container );
527
+ $parent->insertAfter( $noscript, $youtube_lazyload_container->id() );
528
+ } else {
529
+ /**
530
+ * Filter the LazyLoad placeholder on src attribute
531
+ *
532
+ * @since 1.1
533
+ *
534
+ * @param string $placeholder placeholder that will be printed.
535
+ */
536
+ $placeholder = apply_filters( 'rocket_lazyload_placeholder', 'about:blank' );
537
+
538
+ $iframe->setAttribute( 'src', $placeholder );
539
+ $iframe->setAttribute( 'data-lazy-src', $iframe_attributes['src'] );
540
+ $iframe->setAttribute( 'data-rocket-lazyload', 'fitvidscompatible' );
541
+ $parent->insertAfter( $noscript, $iframe->id() );
542
+ }
543
  }
544
 
545
  return $dom;
546
  }
547
  add_filter( 'the_content', 'rocket_lazyload_iframes', PHP_INT_MAX );
548
  add_filter( 'widget_text', 'rocket_lazyload_iframes', PHP_INT_MAX );
549
+
550
+ /**
551
+ * Gets youtube video ID from URL
552
+ *
553
+ * @author Remy Perona
554
+ * @since 1.4
555
+ *
556
+ * @param string $url URL to parse.
557
+ * @return string Youtube video id or false if none found.
558
+ */
559
+ function rocket_lazyload_get_youtube_id_from_url( $url ) {
560
+ $pattern = '#^(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=))([\w-]{11})#iU';
561
+ $result = preg_match( $pattern, $url, $matches );
562
+
563
+ if ( $result ) {
564
+ return $matches[1];
565
+ }
566
+ return false;
567
+ }