Skip to content

Commit b847599

Browse files
committed
rewrite plugin to support newer jquery_lazyload (1.7.1) which requires HTML changes
1 parent e703d5d commit b847599

File tree

3 files changed

+70
-32
lines changed

3 files changed

+70
-32
lines changed

javascripts/jquery.lazyload.mini.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

jq_img_lazy_load.php

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,65 @@
99
Author URI: http://blog.andrewng.com
1010
*/
1111

12-
function jquery_lazy_load_headers() {
13-
$plugin_path = plugins_url('/', __FILE__);
14-
$lazy_url = $plugin_path . 'javascripts/jquery.lazyload.mini.js';
15-
$jq_url = 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js';
16-
wp_deregister_script('jquery');
17-
wp_enqueue_script('jquery', $jq_url, false, '1.6.1');
18-
wp_enqueue_script('jquerylazyload', $lazy_url, 'jquery', '1.5.0');
19-
}
12+
class jQueryLazyLoad {
13+
var $do_footer = false;
14+
15+
function __construct() {
16+
add_action('wp_enqueue_scripts', array($this, 'action_enqueue_scripts'));
17+
add_filter('the_content', array($this, 'filter_the_content'));
18+
add_filter('wp_get_attachment_link', array($this, 'filter_the_content'));
19+
add_action('wp_footer', array($this, 'action_footer'), 15);
20+
}
21+
22+
function action_enqueue_scripts() {
23+
wp_enqueue_script(
24+
'jquery_lazy_load',
25+
plugins_url('/js/jquery.lazyload.min.js', __FILE__),
26+
array('jquery'),
27+
'1.7.1' // version of the lazyload script from https://github.com/tuupola/jquery_lazyload
28+
);
29+
}
30+
31+
function filter_the_content($content) {
32+
return preg_replace_callback('/(<\s*img[^>]+)(src\s*=\s*"[^"]+")([^>]+>)/i', array($this, 'preg_replace_callback'), $content);
33+
}
34+
35+
function preg_replace_callback($matches) {
36+
// set flag indicating there are images to be replaced
37+
$this->do_footer = true;
2038

21-
function jquery_lazy_load_ready() {
22-
$placeholdergif = plugins_url('images/grey.gif', __FILE__);
23-
echo <<<EOF
39+
// alter original img tag:
40+
// - add empty class attribute if no existing class attribute
41+
// - set src to placeholder image
42+
// - add back original src attribute, but rename it to "data-original"
43+
if (!preg_match('/class\s*=\s*"/i', $matches[0])) {
44+
$class_attr = 'class="" ';
45+
}
46+
$replacement = $matches[1] . $class_attr . 'src="' . plugins_url('/images/grey.gif', __FILE__) . '" data-original' . substr($matches[2], 3) . $matches[3];
47+
48+
// add "lazy" class to existing class attribute
49+
$replacement = preg_replace('/class\s*=\s*"/i', 'class="lazy ', $replacement);
50+
51+
// add noscript fallback with original img tag inside
52+
$replacement .= '<noscript>' . $matches[0] . '</noscript>';
53+
return $replacement;
54+
}
55+
56+
function action_footer() {
57+
if (!$this->do_footer) {
58+
return;
59+
}
60+
61+
echo <<<EOF
2462
<script type="text/javascript">
25-
jQuery(document).ready(function($){
26-
if (navigator.platform == "iPad") return;
27-
jQuery("img").not(".cycle img").lazyload({
28-
effect:"fadeIn",
29-
placeholder: "$placeholdergif"
30-
});
31-
});
63+
(function($){
64+
$("img.lazy").show().lazyload({effect: "fadeIn"});
65+
})(jQuery);
3266
</script>
3367
EOF;
68+
}
3469
}
3570

36-
add_action('wp_head', 'jquery_lazy_load_headers', 5);
37-
add_action('wp_head', 'jquery_lazy_load_ready', 12);
71+
new jQueryLazyLoad();
72+
3873
?>

js/jquery.lazyload.min.js

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)