BJ Lazy Load - Version 0.2

Version Description

Download this release

Release Info

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

Code changes from version 0.1 to 0.2

Files changed (2) hide show
  1. bj-lazyload.php +120 -23
  2. readme.txt +8 -3
bj-lazyload.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.1
7
  Author: Bjørn Johansen
8
  Author URI: http://twitter.com/bjornjohansen
9
  License: GPL2
@@ -24,11 +24,29 @@ License: GPL2
24
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
 
26
  */
27
-
28
 
29
  class BJLL {
30
 
31
- function enqueue_scripts() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  wp_enqueue_script('JAIL', plugins_url('/js/jail.min.js', __FILE__), array('jquery'), '0.9.7', true);
33
 
34
  wp_enqueue_script( 'BJLL', plugins_url('/js/bjll.js', __FILE__), array( 'jquery', 'JAIL' ), '0.1', true );
@@ -38,14 +56,12 @@ class BJLL {
38
  */
39
  }
40
 
41
- function get_images_json() {
42
  echo json_encode($_POST['attachmentIDs']);
43
  exit;
44
  }
45
 
46
- function filter_post_images($content) {
47
-
48
- $placeholder_url = plugins_url('/img/placeholder.gif', __FILE__);
49
 
50
  $matches = array();
51
  preg_match_all('/<img\s+.*?>/', $content, $matches);
@@ -54,19 +70,8 @@ class BJLL {
54
  $replace = array();
55
 
56
  foreach ($matches[0] as $imgHTML) {
57
- $replaceHTML = $imgHTML;
58
-
59
- // replace the src and add the data-href attribute
60
- $replaceHTML = preg_replace( '/<img(.*?)src=/i', '<img$1src="'.$placeholder_url.'" data-href=', $replaceHTML );
61
-
62
- // add the lazy class to the img element
63
- if (preg_match('/class="/i', $replaceHTML)) {
64
- $replaceHTML = preg_replace('/class="(.*?)"/i', ' class="lazy $1"', $replaceHTML);
65
- } else {
66
- $replaceHTML = preg_replace('/<img/i', '<img class="lazy"', $replaceHTML);
67
- }
68
 
69
- $replaceHTML .= '<noscript>' . $imgHTML . '</noscript>';
70
 
71
  array_push($search, $imgHTML);
72
  array_push($replace, $replaceHTML);
@@ -77,12 +82,104 @@ class BJLL {
77
  return $content;
78
  }
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  }
81
 
82
- add_action('wp_enqueue_scripts', array('BJLL', 'enqueue_scripts'));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
- add_action( 'wp_ajax_BJLL_get_images', array('BJLL', 'get_images_json') );
85
- add_action( 'wp_ajax_nopriv_BJLL_get_images', array('BJLL', 'get_images_json') );
86
 
87
- add_filter('the_content', array('BJLL', 'filter_post_images'), 200);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
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
7
  Author: Bjørn Johansen
8
  Author URI: http://twitter.com/bjornjohansen
9
  License: GPL2
24
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
 
26
  */
27
+
28
 
29
  class BJLL {
30
 
31
+ private $_placeholder_url;
32
+
33
+ function __construct() {
34
+
35
+ $this->_placeholder_url = plugins_url('/img/placeholder.gif', __FILE__);
36
+
37
+ add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
38
+
39
+ add_action( 'wp_ajax_BJLL_get_images', array($this, 'get_images_json') );
40
+ add_action( 'wp_ajax_nopriv_BJLL_get_images', array($this, 'get_images_json') );
41
+
42
+ add_filter('the_content', array($this, 'filter_post_images'), 200);
43
+
44
+ if (intval(get_option('bjll_filter_post_thumbnails', 1))) {
45
+ add_filter( 'post_thumbnail_html', array($this, 'filter_post_thumbnail_html'), 10 );
46
+ }
47
+ }
48
+
49
+ public function enqueue_scripts() {
50
  wp_enqueue_script('JAIL', plugins_url('/js/jail.min.js', __FILE__), array('jquery'), '0.9.7', true);
51
 
52
  wp_enqueue_script( 'BJLL', plugins_url('/js/bjll.js', __FILE__), array( 'jquery', 'JAIL' ), '0.1', true );
56
  */
57
  }
58
 
59
+ public function get_images_json() {
60
  echo json_encode($_POST['attachmentIDs']);
61
  exit;
62
  }
63
 
64
+ public function filter_post_images($content) {
 
 
65
 
66
  $matches = array();
67
  preg_match_all('/<img\s+.*?>/', $content, $matches);
70
  $replace = array();
71
 
72
  foreach ($matches[0] as $imgHTML) {
 
 
 
 
 
 
 
 
 
 
 
73
 
74
+ $replaceHTML = $this->_get_placeholder_html($imgHTML);
75
 
76
  array_push($search, $imgHTML);
77
  array_push($replace, $replaceHTML);
82
  return $content;
83
  }
84
 
85
+ public function filter_post_thumbnail_html( $html ) {
86
+
87
+ $html = $this->_get_placeholder_html($html);
88
+
89
+ return $html;
90
+
91
+ }
92
+
93
+ protected function _get_placeholder_html ($html) {
94
+
95
+ $orig_html = $html;
96
+
97
+ // replace the src and add the data-href attribute
98
+ $html = preg_replace( '/<img(.*?)src=/i', '<img$1src="'.$this->_placeholder_url.'" data-href=', $html );
99
+
100
+ // add the lazy class to the img element
101
+ if (preg_match('/class="/i', $html)) {
102
+ $html = preg_replace('/class="(.*?)"/i', ' class="lazy $1"', $html);
103
+ } else {
104
+ $html = preg_replace('/<img/i', '<img class="lazy"', $html);
105
+ }
106
+
107
+ $html .= '<noscript>' . $orig_html . '</noscript>';
108
+
109
+ return $html;
110
+ }
111
+
112
  }
113
 
114
+ class BJLL_Admin {
115
+
116
+ function __construct () {
117
+ add_action('admin_menu', array($this, 'plugin_menu'));
118
+ add_action( 'admin_init', array($this, 'register_settings'));
119
+ }
120
+
121
+ function plugin_menu() {
122
+ add_options_page('BJ Lazy Load', 'BJ Lazy Load', 'manage_options', 'bjll', array($this, 'plugin_options_page'));
123
+ }
124
+
125
+ function register_settings() {
126
+ //register_setting( $option_group, $option_name, $sanitize_callback );
127
+ register_setting( 'bjll_options', 'bjll_filter_post_thumbnails', 'intval' );
128
+
129
+ //add_settings_section( $id, $title, $callback, $page );
130
+ add_settings_section('bjll_general', __('General'), array('BJLL_Admin','settings_section_general'), 'bjll');
131
+
132
+ //add_settings_field( $id, $title, $callback, $page, $section, $args );
133
+ add_settings_field('bjll_filter_post_thumbnails', __('Lazy load post thumbnails'), array('BJLL_Admin', 'setting_field_filter_post_thumbnails'), 'bjll', 'bjll_general');
134
+
135
+
136
+ }
137
+
138
+ function settings_section_general() {
139
+ }
140
+
141
+ function setting_field_filter_post_thumbnails() {
142
+
143
+ $checked = '';
144
+ if (intval(get_option('bjll_filter_post_thumbnails', 1))) {
145
+ $checked = ' checked="checked"';
146
+ }
147
+
148
+ echo '<input id="bjll_filter_post_thumbnails" name="bjll_filter_post_thumbnails" type="checkbox" value="1" ' . $checked . ' />';
149
 
150
+ }
 
151
 
152
+ function plugin_options_page() {
153
+ if (!current_user_can('manage_options')) {
154
+ wp_die( __('You do not have sufficient permissions to access this page.') );
155
+ }
156
+ ?>
157
+ <div class="wrap">
158
+ <h2>BJ Lazy Load <?php _e('Settings'); ?></h2>
159
+ <p><?php _e('More settings will be available in the near future.'); ?></p>
160
+ <form method="post" action="options.php">
161
+ <?php settings_fields('bjll_options'); ?>
162
+ <?php do_settings_sections('bjll'); ?>
163
+
164
+ <p class="submit">
165
+ <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
166
+ </p>
167
+ </form>
168
+ </div>
169
+ <?php
170
+ }
171
+
172
+ }
173
+
174
+ /*
175
+ is_admin() will return true when trying to make an ajax request
176
+ if (!is_admin() && !is_feed()) {
177
+ */
178
+ if (!is_feed()) {
179
+ new BJLL;
180
+ }
181
+
182
+ if (is_admin()) {
183
+ new BJLL_Admin;
184
+ }
185
 
readme.txt CHANGED
@@ -5,18 +5,18 @@ 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.1
9
 
10
  Lazy image loading makes your site load faster and saves bandwidth. Uses jQuery and degrades gracefully for non-js users.
11
 
12
  == Description ==
13
  Lazy image loading makes your site load faster and saves bandwidth.
14
 
15
- This plugin replaces all your post images with a placeholder and loads images as they enter the browser window.
16
 
17
  Non-javascript visitors gets the original img element in noscript.
18
 
19
- Contains [JqueryAsynchImageLoader Plugin for jQuery by Sebastiano Armeli-Battana](http://www.sebastianoarmelibattana.com/projects/jail)
20
 
21
  = Coming soon =
22
  * More options like defining a threshold, loading effects, custom placeholder etc.
@@ -44,6 +44,11 @@ Check your HTML source or see the magic at work in FireBug or similar.
44
 
45
  == Changelog ==
46
 
 
 
 
 
 
47
  = Version 0.1 =
48
  * Released 2011-12-05
49
  * It works (or at least it does for me)
5
  Author URI: http://twitter.com/bjornjohansen
6
  Requires at least: 3.2
7
  Tested up to: 3.3
8
+ Stable tag: 0.2
9
 
10
  Lazy image loading makes your site load faster and saves bandwidth. Uses jQuery and degrades gracefully for non-js users.
11
 
12
  == Description ==
13
  Lazy image loading makes your site load faster and saves bandwidth.
14
 
15
+ This plugin replaces all your post images and post thumbnails with a placeholder and loads images as they enter the browser window when the visitor scrolls the page.
16
 
17
  Non-javascript visitors gets the original img element in noscript.
18
 
19
+ Includes [JqueryAsynchImageLoader Plugin for jQuery by Sebastiano Armeli-Battana](http://www.sebastianoarmelibattana.com/projects/jail) for the real magic.
20
 
21
  = Coming soon =
22
  * More options like defining a threshold, loading effects, custom placeholder etc.
44
 
45
  == Changelog ==
46
 
47
+ = Version 0.2 =
48
+ * Added options panel in admin
49
+ * Added option to lazy load post thumbnails
50
+ * Skipped the lazy loading in feeds
51
+
52
  = Version 0.1 =
53
  * Released 2011-12-05
54
  * It works (or at least it does for me)