Completed
Push — add/asset-cdn ( 96dc37...2eecce )
by
unknown
11:00 queued 02:17
created

Jetpack_Lazy_Images::process_image()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 5
nop 1
dl 0
loc 31
rs 8.5806
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
		$new_attributes_str = self::build_attributes_string( $new_attributes );
112
113
		return sprintf( '<img %1$s><noscript>%2$s</noscript>', $new_attributes_str, $matches[0] );
114
	}
115
116
	private static function get_placeholder_image() {
117
		/**
118
		 * Allows plugins and themes to modify the placeholder image.
119
		 *
120
		 * This filter is not prefixed with jetpack_ to provide a smoother migration
121
		 * process from the WordPress Lazy Load plugin.
122
		 *
123
		 * @module lazy-images
124
		 *
125
		 * @since 5.6.0
126
		 *
127
		 * @param string The URL to the placeholder image
128
		 */
129
		return apply_filters(
130
			'lazyload_images_placeholder_image',
131
			plugins_url( 'modules/lazy-images/images/1x1.trans.gif', JETPACK__PLUGIN_FILE )
132
		);
133
	}
134
135
	private static function flatten_kses_hair_data( $attributes ) {
136
		$flattened_attributes = array();
137
		foreach ( $attributes as $name => $attribute ) {
138
			$flattened_attributes[ $name ] = $attribute['value'];
139
		}
140
		return $flattened_attributes;
141
	}
142
143
	private static function build_attributes_string( $attributes ) {
144
		$string = array();
145
		foreach ( $attributes as $name => $value ) {
146
			if ( '' === $value ) {
147
				$string[] = sprintf( '%s', $name );
148
			} else {
149
				$string[] = sprintf( '%s="%s"', $name, esc_attr( $value ) );
150
			}
151
		}
152
		return implode( ' ', $string );
153
	}
154
155
	public function enqueue_assets() {
156
		wp_enqueue_script(
157
			'jetpack-lazy-images',
158
			plugins_url( 'modules/lazy-images/js/lazy-images.js', JETPACK__PLUGIN_FILE ),
159
			array( 'jquery' ),
160
			JETPACK__VERSION,
161
			true
162
		);
163
	}
164
}
165