Completed
Push — update/sync-users-enqueue-orde... ( d17611...0c48e3 )
by
unknown
29:40
created

Jetpack_Lazy_Images::flatten_kses_hair_data()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_Lazy_Images {
4
	private static $__instance = null;
5
6
	/**
7
	 * Singleton implementation
8
	 *
9
	 * @return object
10
	 */
11
	public static function instance() {
12
		if ( is_null( self::$__instance ) ) {
13
			self::$__instance = new Jetpack_Lazy_Images();
14
		}
15
16
		return self::$__instance;
17
	}
18
19
	/**
20
	 * Registers actions
21
	 */
22
	private function __construct() {
23
		if ( is_admin() ) {
24
			return;
25
		}
26
27
		/**
28
		 * Whether the lazy-images module should load.
29
		 *
30
		 * This filter is not prefixed with jetpack_ to provide a smoother migration
31
		 * process from the WordPress Lazy Load plugin.
32
		 *
33
		 * @module lazy-images
34
		 *
35
		 * @since 5.6.0
36
		 *
37
		 * @param bool true Whether lazy image loading should occur.
38
		 */
39
		if ( ! apply_filters( 'lazyload_is_enabled', true ) ) {
40
			return;
41
		}
42
43
		add_action( 'wp_head', array( $this, 'setup_filters' ), 9999 ); // we don't really want to modify anything in <head> since it's mostly all metadata
44
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
45
46
		// Do not lazy load avatar in admin bar
47
		add_action( 'admin_bar_menu', array( $this, 'remove_filters' ), 0 );
48
	}
49
50
	public function setup_filters() {
51
		add_filter( 'the_content', array( $this, 'add_image_placeholders' ), 99 ); // run this later, so other content filters have run, including image_add_wh on WP.com
52
		add_filter( 'post_thumbnail_html', array( $this, 'add_image_placeholders' ), 11 );
53
		add_filter( 'get_avatar', array( $this, 'add_image_placeholders' ), 11 );
54
	}
55
56
	public function remove_filters() {
57
		remove_filter( 'the_content', array( $this, 'add_image_placeholders' ), 99 );
58
		remove_filter( 'post_thumbnail_html', array( $this, 'add_image_placeholders' ), 11 );
59
		remove_filter( 'get_avatar', array( $this, 'add_image_placeholders' ), 11 );
60
	}
61
62
	public function add_image_placeholders( $content ) {
63
		// Don't lazyload for feeds, previews
64
		if ( is_feed() || is_preview() ) {
65
			return $content;
66
		}
67
68
		// Don't lazy-load if the content has already been run through previously
69
		if ( false !== strpos( $content, 'data-lazy-src' ) ) {
70
			return $content;
71
		}
72
73
		// Don't lazyload for amp-wp content
74
		if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
75
			return $content;
76
		}
77
78
		// This is a pretty simple regex, but it works
79
		$content = preg_replace_callback( '#<(img)([^>]+?)(>(.*?)</\\1>|[\/]?>)#si', array( __CLASS__, 'process_image' ), $content );
80
81
		return $content;
82
	}
83
84
	static function process_image( $matches ) {
85
		$old_attributes_str = $matches[2];
86
		$old_attributes_kses_hair = wp_kses_hair( $old_attributes_str, wp_allowed_protocols() );
87
88
		if ( empty( $old_attributes_kses_hair['src'] ) ) {
89
			return $matches[0];
90
		}
91
92
		$old_attributes = self::flatten_kses_hair_data( $old_attributes_kses_hair );
93
		$new_attributes = $old_attributes;
94
95
		// Set placeholder and lazy-src
96
		$new_attributes['src'] = self::get_placeholder_image();
97
		$new_attributes['data-lazy-src'] = $old_attributes['src'];
98
99
		// Handle `srcset`
100
		if ( ! empty( $new_attributes['srcset'] ) ) {
101
			$new_attributes['data-lazy-srcset'] = $old_attributes['srcset'];
102
			unset( $new_attributes['srcset'] );
103
		}
104
105
		// Handle `sizes`
106
		if ( ! empty( $new_attributes['sizes'] ) ) {
107
			$new_attributes['data-lazy-sizes'] = $old_attributes['sizes'];
108
			unset( $new_attributes['sizes'] );
109
		}
110
111
		/**
112
		 * Allow plugins and themes to override the attributes on the image before the content is updated.
113
		 *
114
		 * One potential use of this filter is for themes that set `height:auto` on the `img` tag.
115
		 * With this filter, the theme could get the width and height attributes from the
116
		 * $new_attributes array and then add a style tag that sets those values as well, which could
117
		 * minimize reflow as images load.
118
		 *
119
		 * @module lazy-images
120
		 *
121
		 * @since 5.6.0
122
		 *
123
		 * @param array An array containing the attributes for the image, where the key is the attribute name
124
		 *              and the value is the attribute value.
125
		 */
126
		$new_attributes = apply_filters( 'jetpack_lazy_images_new_attributes', $new_attributes );
127
		$new_attributes_str = self::build_attributes_string( $new_attributes );
128
129
		return sprintf( '<img %1$s><noscript>%2$s</noscript>', $new_attributes_str, $matches[0] );
130
	}
131
132
	private static function get_placeholder_image() {
133
		/**
134
		 * Allows plugins and themes to modify the placeholder image.
135
		 *
136
		 * This filter is not prefixed with jetpack_ to provide a smoother migration
137
		 * process from the WordPress Lazy Load plugin.
138
		 *
139
		 * @module lazy-images
140
		 *
141
		 * @since 5.6.0
142
		 *
143
		 * @param string The URL to the placeholder image
144
		 */
145
		return apply_filters(
146
			'lazyload_images_placeholder_image',
147
			plugins_url( 'modules/lazy-images/images/1x1.trans.gif', JETPACK__PLUGIN_FILE )
148
		);
149
	}
150
151
	private static function flatten_kses_hair_data( $attributes ) {
152
		$flattened_attributes = array();
153
		foreach ( $attributes as $name => $attribute ) {
154
			$flattened_attributes[ $name ] = $attribute['value'];
155
		}
156
		return $flattened_attributes;
157
	}
158
159
	private static function build_attributes_string( $attributes ) {
160
		$string = array();
161
		foreach ( $attributes as $name => $value ) {
162
			if ( '' === $value ) {
163
				$string[] = sprintf( '%s', $name );
164
			} else {
165
				$string[] = sprintf( '%s="%s"', $name, esc_attr( $value ) );
166
			}
167
		}
168
		return implode( ' ', $string );
169
	}
170
171
	public function enqueue_assets() {
172
		wp_enqueue_script(
173
			'jetpack-lazy-images',
174
			plugins_url( 'modules/lazy-images/js/lazy-images.js', JETPACK__PLUGIN_FILE ),
175
			array( 'jquery' ),
176
			JETPACK__VERSION,
177
			true
178
		);
179
	}
180
}
181