-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathComponent.php
351 lines (303 loc) · 10.4 KB
/
Component.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
/**
* WP_Rig\WP_Rig\Lazyload\Component class
*
* @package wp_rig
*/
namespace WP_Rig\WP_Rig\Lazyload;
use WP_Rig\WP_Rig\Component_Interface;
use function WP_Rig\WP_Rig\wp_rig;
use WP_Customize_Manager;
use function add_action;
use function add_filter;
use function is_admin;
use function get_theme_mod;
use function apply_filters;
use function wp_enqueue_script;
use function get_theme_file_uri;
use function get_theme_file_path;
use function wp_script_add_data;
use function remove_filter;
use function is_feed;
use function is_preview;
use function wp_kses_hair;
/**
* Class for managing lazy-loading images.
*/
class Component implements Component_Interface {
/**
* Gets the unique identifier for the theme component.
*
* @return string Component slug.
*/
public function get_slug() : string {
return 'lazyload';
}
/**
* Adds the action and filter hooks to integrate with WordPress.
*/
public function initialize() {
add_action( 'wp', [ $this, 'action_lazyload_images' ] );
add_action( 'customize_register', [ $this, 'action_customize_register_lazyload' ] );
}
/**
* Initializes lazy-loading images functionality.
*/
public function action_lazyload_images() {
// If this is the admin page, return early.
if ( is_admin() ) {
return;
}
// If lazy-load is disabled in Customizer, return early.
if ( 'no-lazyload' === get_theme_mod( 'lazy_load_media' ) ) {
return;
}
// If the Jetpack Lazy-Images module is active, return early.
if ( ! apply_filters( 'lazyload_is_enabled', true ) ) {
return;
}
// If the AMP plugin is active, return early.
if ( wp_rig()->is_amp() ) {
return;
}
add_action( 'wp_head', [ $this, 'action_add_lazyload_filters' ], PHP_INT_MAX );
add_action( 'wp_enqueue_scripts', [ $this, 'action_enqueue_lazyload_assets' ] );
// Do not lazy load avatar in admin bar.
add_action( 'admin_bar_menu', [ $this, 'action_remove_lazyload_filters' ], 0 );
add_filter( 'wp_kses_allowed_html', [ $this, 'filter_allow_lazyload_attributes' ] );
}
/**
* Adds a setting and control for lazy loading the Customizer.
*
* @param WP_Customize_Manager $wp_customize Customizer manager instance.
*/
public function action_customize_register_lazyload( WP_Customize_Manager $wp_customize ) {
$lazyload_choices = [
'lazyload' => __( 'Lazy-load on (default)', 'wp-rig' ),
'no-lazyload' => __( 'Lazy-load off', 'wp-rig' ),
];
$wp_customize->add_setting(
'lazy_load_media',
[
'default' => 'lazyload',
'transport' => 'postMessage',
'sanitize_callback' => function( $input ) use ( $lazyload_choices ) : string {
if ( array_key_exists( $input, $lazyload_choices ) ) {
return $input;
}
return '';
},
]
);
$wp_customize->add_control(
'lazy_load_media',
[
'label' => __( 'Lazy-load images', 'wp-rig' ),
'section' => 'theme_options',
'type' => 'radio',
'description' => __( 'Lazy-loading images means images are loaded only when they are in view. Improves performance, but can result in content jumping around on slower connections.', 'wp-rig' ),
'choices' => $lazyload_choices,
]
);
}
/**
* Adds filters to enable lazy-loading of images.
*/
public function action_add_lazyload_filters() {
add_filter( 'the_content', [ $this, 'filter_add_lazyload_placeholders' ], PHP_INT_MAX );
add_filter( 'post_thumbnail_html', [ $this, 'filter_add_lazyload_placeholders' ], PHP_INT_MAX );
add_filter( 'get_avatar', [ $this, 'filter_add_lazyload_placeholders' ], PHP_INT_MAX );
add_filter( 'widget_text', [ $this, 'filter_add_lazyload_placeholders' ], PHP_INT_MAX );
add_filter( 'get_image_tag', [ $this, 'filter_add_lazyload_placeholders' ], PHP_INT_MAX );
add_filter( 'wp_get_attachment_image_attributes', [ $this, 'filter_lazyload_attributes' ], PHP_INT_MAX );
}
/**
* Enqueues and defer lazy-loading JavaScript.
*/
public function action_enqueue_lazyload_assets() {
wp_enqueue_script(
'wp-rig-lazy-load-images',
get_theme_file_uri( '/assets/js/lazyload.min.js' ),
[],
wp_rig()->get_asset_version( get_theme_file_path( '/assets/js/lazyload.min.js' ) ),
false
);
wp_script_add_data( 'wp-rig-lazy-load-images', 'defer', true );
wp_script_add_data( 'wp-rig-lazy-load-images', 'precache', true );
}
/**
* Removes filters for images that should not be lazy-loaded.
*/
public function action_remove_lazyload_filters() {
remove_filter( 'the_content', [ $this, 'filter_add_lazyload_placeholders' ], PHP_INT_MAX );
remove_filter( 'post_thumbnail_html', [ $this, 'filter_add_lazyload_placeholders' ], PHP_INT_MAX );
remove_filter( 'get_avatar', [ $this, 'filter_add_lazyload_placeholders' ], PHP_INT_MAX );
remove_filter( 'widget_text', [ $this, 'filter_add_lazyload_placeholders' ], PHP_INT_MAX );
remove_filter( 'get_image_tag', [ $this, 'filter_add_lazyload_placeholders' ], PHP_INT_MAX );
remove_filter( 'wp_get_attachment_image_attributes', [ $this, 'filter_lazyload_attributes' ], PHP_INT_MAX );
}
/**
* Ensures that lazy-loading image attributes are not filtered out of image tags.
*
* @param array $allowed_tags The allowed tags and their attributes.
* @return array Filtered allowed tags.
*/
public function filter_allow_lazyload_attributes( array $allowed_tags ) : array {
if ( ! isset( $allowed_tags['img'] ) ) {
return $allowed_tags;
}
// But, if images are allowed, ensure that our attributes are allowed!
$allowed_tags['img'] = array_merge(
$allowed_tags['img'],
[
'data-src' => 1,
'data-srcset' => 1,
'data-sizes' => 1,
'class' => 1,
]
);
return $allowed_tags;
}
/**
* Finds image elements that should be lazy-loaded.
*
* @param string $content The content.
* @return string Filtered content.
*/
public function filter_add_lazyload_placeholders( string $content ) : string {
// Don't lazyload for feeds, previews.
if ( is_feed() || is_preview() ) {
return $content;
}
// Don't lazy-load if the content has already been run through previously.
if ( false !== strpos( $content, 'data-src' ) ) {
return $content;
}
// Find all <img> elements via regex, add lazy-load attributes.
$content = preg_replace_callback(
'#<(img)([^>]+?)(>(.*?)</\\1>|[\/]?>)#si',
function( array $matches ) : string {
$old_attributes_str = $matches[2];
$old_attributes_kses_hair = wp_kses_hair( $old_attributes_str, wp_allowed_protocols() );
if ( empty( $old_attributes_kses_hair['src'] ) ) {
return $matches[0];
}
$old_attributes = $this->flatten_kses_hair_data( $old_attributes_kses_hair );
$new_attributes = $this->filter_lazyload_attributes( $old_attributes );
// If we didn't add lazy attributes, just return the original image source.
if ( empty( $new_attributes['data-src'] ) ) {
return $matches[0];
}
$new_attributes_str = $this->build_attributes_string( $new_attributes );
return sprintf( '<img %1$s><noscript>%2$s</noscript>', $new_attributes_str, $matches[0] );
},
$content
);
return $content;
}
/**
* Given an array of image attributes, updates the `src`, `srcset`, and `sizes` attributes so
* that they load lazily.
*
* @param array $attributes Attributes of the current <img> element.
* @return array The updated image attributes array with lazy load attributes.
*/
public function filter_lazyload_attributes( array $attributes ) : array {
if ( empty( $attributes['src'] ) ) {
return $attributes;
}
if ( ! empty( $attributes['class'] ) && $this->should_skip_image_with_blacklisted_class( $attributes['class'] ) ) {
return $attributes;
}
$old_attributes = $attributes;
// Add the lazy class to the img element.
$attributes['class'] = $this->lazyload_class( $attributes );
// Set placeholder and lazy-src.
$attributes['src'] = $this->lazyload_get_placeholder_image();
// Set data-src to the original source uri.
$attributes['data-src'] = $old_attributes['src'];
// Process `srcset` attribute.
if ( ! empty( $attributes['srcset'] ) ) {
$attributes['data-srcset'] = $old_attributes['srcset'];
unset( $attributes['srcset'] );
}
// Process `sizes` attribute.
if ( ! empty( $attributes['sizes'] ) ) {
$attributes['data-sizes'] = $old_attributes['sizes'];
unset( $attributes['sizes'] );
}
return $attributes;
}
/**
* Returns true when a given string of classes contains a class signifying image
* should not be lazy-loaded
*
* @param string $classes A string of space-separated classes.
* @return bool Whether the classes contain a class indicating that lazyloading should be skipped.
*/
protected function should_skip_image_with_blacklisted_class( string $classes ) : bool {
$blacklisted_classes = [
'skip-lazy',
'custom-logo',
];
foreach ( $blacklisted_classes as $class ) {
if ( false !== strpos( $classes, $class ) ) {
return true;
}
}
return false;
}
/**
* Appends a 'lazy' class to <img> elements for lazy-loading.
*
* @param array $attributes <img> element attributes.
* @return string Classes string including a 'lazy' class.
*/
protected function lazyload_class( array $attributes ) : string {
if ( array_key_exists( 'class', $attributes ) ) {
$classes = $attributes['class'];
$classes .= ' lazy';
} else {
$classes = 'lazy';
}
return $classes;
}
/**
* Gets the placeholder image URL.
*
* @return string The URL to the placeholder image.
*/
protected function lazyload_get_placeholder_image() : string {
return get_theme_file_uri( '/assets/images/placeholder.svg' );
}
/**
* Flattens an attribute list into key value pairs.
*
* @param array $attributes Array of attributes.
* @return array Flattened attributes as $attr => $attr_value pairs.
*/
protected function flatten_kses_hair_data( array $attributes ) : array {
$flattened_attributes = [];
foreach ( $attributes as $name => $attribute ) {
$flattened_attributes[ $name ] = $attribute['value'];
}
return $flattened_attributes;
}
/**
* Builds a string of attributes for an HTML element.
*
* @param array $attributes Array of attributes.
* @return string HTML attribute string.
*/
protected function build_attributes_string( array $attributes ) : string {
$string = [];
foreach ( $attributes as $name => $value ) {
if ( '' === $value ) {
$string[] = sprintf( '%s', $name );
} else {
$string[] = sprintf( '%s="%s"', $name, esc_attr( $value ) );
}
}
return implode( ' ', $string );
}
}