BJ Lazy Load - Version 0.2.3

Version Description

Improved image replacement

Download this release

Release Info

Developer bjornjohansen
Plugin Icon 128x128 BJ Lazy Load
Version 0.2.3
Comparing to
See all releases

Code changes from version 0.2.2 to 0.2.3

Files changed (2) hide show
  1. bj-lazy-load.php +50 -2
  2. readme.txt +7 -1
bj-lazy-load.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: BJ Lazy Load
4
  Plugin URI: http://wordpress.org/extend/plugins/bj-lazy-load/
5
  Description: Lazy image loading makes your site load faster and saves bandwidth.
6
- Version: 0.2.2
7
  Author: Bjørn Johansen
8
  Author URI: http://twitter.com/bjornjohansen
9
  License: GPL2
@@ -28,7 +28,7 @@ License: GPL2
28
 
29
  class BJLL {
30
 
31
- const version = '0.2.2';
32
  private $_placeholder_url;
33
 
34
  function __construct() {
@@ -140,6 +140,53 @@ var BJLL = {
140
 
141
  protected function _get_placeholder_html ($html) {
142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  $orig_html = $html;
144
 
145
  /**/
@@ -158,6 +205,7 @@ var BJLL = {
158
 
159
 
160
  // http://24ways.org/2011/adaptive-images-for-responsive-designs-again
 
161
  //$html = "<script>document.write('<' + '!--')</script><noscript class=\"lazy-nojs\">" . $orig_html . '<noscript -->';
162
 
163
  return $html;
3
  Plugin Name: BJ Lazy Load
4
  Plugin URI: http://wordpress.org/extend/plugins/bj-lazy-load/
5
  Description: Lazy image loading makes your site load faster and saves bandwidth.
6
+ Version: 0.2.3
7
  Author: Bjørn Johansen
8
  Author URI: http://twitter.com/bjornjohansen
9
  License: GPL2
28
 
29
  class BJLL {
30
 
31
+ const version = '0.2.3';
32
  private $_placeholder_url;
33
 
34
  function __construct() {
140
 
141
  protected function _get_placeholder_html ($html) {
142
 
143
+ if (class_exists('DOMDocument')) {
144
+ $html = $this->_get_placeholder_html_dom($html);
145
+ } else {
146
+ $html = $this->_get_placeholder_html_regexp($html);
147
+ }
148
+
149
+ return $html;
150
+ }
151
+
152
+ protected function _get_placeholder_html_dom ($html) {
153
+
154
+ $doc = DOMDocument::loadHTML($html);
155
+ if (!$doc) {
156
+ return $this->_get_placeholder_html_regexp($html);
157
+ }
158
+
159
+ $images = $doc->getElementsByTagName('img');
160
+
161
+ $img = $images->item(0);
162
+
163
+ //foreach ($images as $img) {
164
+
165
+ $noscriptImg = $img->cloneNode(true);
166
+ $noscript = $doc->createElement('noscript');
167
+ $noscript->appendChild($noscriptImg);
168
+
169
+ $src = $img->getAttribute('src');
170
+ $class = $img->getAttribute('class');
171
+
172
+ $class .= ' lazy lazy-hidden';
173
+
174
+ $img->setAttribute( 'data-href' , $src );
175
+ $img->setAttribute( 'src' , $this->_placeholder_url );
176
+ $img->setAttribute( 'class' , trim($class) );
177
+
178
+ $img->parentNode->appendChild($noscript);
179
+
180
+ //}
181
+
182
+ $rethtml = $doc->saveHTML();
183
+
184
+ $rethtml = substr($rethtml, strpos($rethtml, '<body>') + 6);
185
+ $rethtml = substr($rethtml, 0, strpos($rethtml, '</body>'));
186
+
187
+ return $rethtml;
188
+ }
189
+ protected function _get_placeholder_html_regexp ($html) {
190
  $orig_html = $html;
191
 
192
  /**/
205
 
206
 
207
  // http://24ways.org/2011/adaptive-images-for-responsive-designs-again
208
+ // This is a no-go. <img> within <noscript> within <a> gets parsed horribly wrong
209
  //$html = "<script>document.write('<' + '!--')</script><noscript class=\"lazy-nojs\">" . $orig_html . '<noscript -->';
210
 
211
  return $html;
readme.txt CHANGED
@@ -5,7 +5,7 @@ Tags: images, lazy loading, jquery, javascript, optimize, performance, bandwidth
5
  Author URI: http://twitter.com/bjornjohansen
6
  Requires at least: 3.2
7
  Tested up to: 3.3
8
- Stable tag: 0.2.2
9
 
10
  Lazy image loading makes your site load faster and saves bandwidth. Uses jQuery and degrades gracefully for non-js users.
11
 
@@ -46,6 +46,9 @@ Check your HTML source or see the magic at work in FireBug or similar.
46
 
47
  == Changelog ==
48
 
 
 
 
49
  = Version 0.2.2 =
50
  * Added CSS. No longer need for hiding .no-js .lazy
51
  * Added options whether to include JS and CSS or not
@@ -66,6 +69,9 @@ Check your HTML source or see the magic at work in FireBug or similar.
66
 
67
  == Upgrade Notice ==
68
 
 
 
 
69
  = 0.2.2 =
70
  More options and improved non-JS display.
71
 
5
  Author URI: http://twitter.com/bjornjohansen
6
  Requires at least: 3.2
7
  Tested up to: 3.3
8
+ Stable tag: 0.2.3
9
 
10
  Lazy image loading makes your site load faster and saves bandwidth. Uses jQuery and degrades gracefully for non-js users.
11
 
46
 
47
  == Changelog ==
48
 
49
+ = Version 0.2.3 =
50
+ * Now using DOMDocument for better HTML parsing. Old regexp parsing as fallback if DOMDocument is not available.
51
+
52
  = Version 0.2.2 =
53
  * Added CSS. No longer need for hiding .no-js .lazy
54
  * Added options whether to include JS and CSS or not
69
 
70
  == Upgrade Notice ==
71
 
72
+ = 0.2.3 =
73
+ Improved image replacement
74
+
75
  = 0.2.2 =
76
  More options and improved non-JS display.
77