Completed
Push — add/tracks ( 83fe2c...85e985 )
by
unknown
102:55 queued 93:40
created

Jetpack_Photon::setup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 18
rs 9.4285
1
<?php
2
3
class Jetpack_Photon {
4
	/**
5
	 * Class variables
6
	 */
7
	// Oh look, a singleton
8
	private static $__instance = null;
9
10
	// Allowed extensions must match http://code.trac.wordpress.org/browser/photon/index.php#L31
11
	protected static $extensions = array(
12
		'gif',
13
		'jpg',
14
		'jpeg',
15
		'png'
16
	);
17
18
	// Don't access this directly. Instead, use self::image_sizes() so it's actually populated with something.
19
	protected static $image_sizes = null;
20
21
	/**
22
	 * Singleton implementation
23
	 *
24
	 * @return object
25
	 */
26
	public static function instance() {
27
		if ( ! is_a( self::$__instance, 'Jetpack_Photon' ) ) {
28
			self::$__instance = new Jetpack_Photon;
29
			self::$__instance->setup();
30
		}
31
32
		return self::$__instance;
33
	}
34
35
	/**
36
	 * Silence is golden.
37
	 */
38
	private function __construct() {}
39
40
	/**
41
	 * Register actions and filters, but only if basic Photon functions are available.
42
	 * The basic functions are found in ./functions.photon.php.
43
	 *
44
	 * @uses add_action, add_filter
45
	 * @return null
46
	 */
47
	private function setup() {
48
		if ( ! function_exists( 'jetpack_photon_url' ) )
49
			return;
50
51
		// Images in post content and galleries
52
		add_filter( 'the_content', array( __CLASS__, 'filter_the_content' ), 999999 );
53
		add_filter( 'get_post_galleries', array( __CLASS__, 'filter_the_galleries' ), 999999 );
54
55
		// Core image retrieval
56
		add_filter( 'image_downsize', array( $this, 'filter_image_downsize' ), 10, 3 );
57
58
		// Responsive image srcset substitution
59
		add_filter( 'wp_calculate_image_srcset', array( $this, 'filter_srcset_array' ), 10, 4 );
60
		add_filter( 'wp_calculate_image_sizes', array( $this, 'filter_sizes' ), 1, 2 ); // Early so themes can still easily filter.
61
62
		// Helpers for maniuplated images
63
		add_action( 'wp_enqueue_scripts', array( $this, 'action_wp_enqueue_scripts' ), 9 );
64
	}
65
66
	/**
67
	 ** IN-CONTENT IMAGE MANIPULATION FUNCTIONS
68
	 **/
69
70
	/**
71
	 * Match all images and any relevant <a> tags in a block of HTML.
72
	 *
73
	 * @param string $content Some HTML.
74
	 * @return array An array of $images matches, where $images[0] is
75
	 *         an array of full matches, and the link_url, img_tag,
76
	 *         and img_url keys are arrays of those matches.
77
	 */
78
	public static function parse_images_from_html( $content ) {
79
		$images = array();
80
81
		if ( preg_match_all( '#(?:<a[^>]+?href=["|\'](?P<link_url>[^\s]+?)["|\'][^>]*?>\s*)?(?P<img_tag><img[^>]*?\s+?src=["|\'](?P<img_url>[^\s]+?)["|\'].*?>){1}(?:\s*</a>)?#is', $content, $images ) ) {
82
			foreach ( $images as $key => $unused ) {
83
				// Simplify the output as much as possible, mostly for confirming test results.
84
				if ( is_numeric( $key ) && $key > 0 )
85
					unset( $images[$key] );
86
			}
87
88
			return $images;
89
		}
90
91
		return array();
92
	}
93
94
	/**
95
	 * Try to determine height and width from strings WP appends to resized image filenames.
96
	 *
97
	 * @param string $src The image URL.
98
	 * @return array An array consisting of width and height.
99
	 */
100
	public static function parse_dimensions_from_filename( $src ) {
101
		$width_height_string = array();
102
103
		if ( preg_match( '#-(\d+)x(\d+)\.(?:' . implode('|', self::$extensions ) . '){1}$#i', $src, $width_height_string ) ) {
104
			$width = (int) $width_height_string[1];
105
			$height = (int) $width_height_string[2];
106
107
			if ( $width && $height )
108
				return array( $width, $height );
109
		}
110
111
		return array( false, false );
112
	}
113
114
	/**
115
	 * Identify images in post content, and if images are local (uploaded to the current site), pass through Photon.
116
	 *
117
	 * @param string $content
118
	 * @uses self::validate_image_url, apply_filters, jetpack_photon_url, esc_url
119
	 * @filter the_content
120
	 * @return string
121
	 */
122
	public static function filter_the_content( $content ) {
123
		$images = Jetpack_Photon::parse_images_from_html( $content );
124
125
		if ( ! empty( $images ) ) {
126
			$content_width = Jetpack::get_content_width();
127
128
			$image_sizes = self::image_sizes();
129
			$upload_dir = wp_upload_dir();
130
131
			foreach ( $images[0] as $index => $tag ) {
132
				// Default to resize, though fit may be used in certain cases where a dimension cannot be ascertained
133
				$transform = 'resize';
134
135
				// Start with a clean attachment ID each time
136
				$attachment_id = false;
137
138
				// Flag if we need to munge a fullsize URL
139
				$fullsize_url = false;
140
141
				// Identify image source
142
				$src = $src_orig = $images['img_url'][ $index ];
143
144
				/**
145
				 * Allow specific images to be skipped by Photon.
146
				 *
147
				 * @module photon
148
				 *
149
				 * @since 2.0.3
150
				 *
151
				 * @param bool false Should Photon ignore this image. Default to false.
152
				 * @param string $src Image URL.
153
				 * @param string $tag Image Tag (Image HTML output).
154
				 */
155
				if ( apply_filters( 'jetpack_photon_skip_image', false, $src, $tag ) )
156
					continue;
157
158
				// Support Automattic's Lazy Load plugin
159
				// Can't modify $tag yet as we need unadulterated version later
160
				if ( preg_match( '#data-lazy-src=["|\'](.+?)["|\']#i', $images['img_tag'][ $index ], $lazy_load_src ) ) {
161
					$placeholder_src = $placeholder_src_orig = $src;
162
					$src = $src_orig = $lazy_load_src[1];
163
				} elseif ( preg_match( '#data-lazy-original=["|\'](.+?)["|\']#i', $images['img_tag'][ $index ], $lazy_load_src ) ) {
164
					$placeholder_src = $placeholder_src_orig = $src;
165
					$src = $src_orig = $lazy_load_src[1];
166
				}
167
168
				// Check if image URL should be used with Photon
169
				if ( self::validate_image_url( $src ) ) {
170
					// Find the width and height attributes
171
					$width = $height = false;
172
173
					// First, check the image tag
174
					if ( preg_match( '#width=["|\']?([\d%]+)["|\']?#i', $images['img_tag'][ $index ], $width_string ) )
175
						$width = $width_string[1];
176
177
					if ( preg_match( '#height=["|\']?([\d%]+)["|\']?#i', $images['img_tag'][ $index ], $height_string ) )
178
						$height = $height_string[1];
179
180
					// Can't pass both a relative width and height, so unset the height in favor of not breaking the horizontal layout.
181
					if ( false !== strpos( $width, '%' ) && false !== strpos( $height, '%' ) )
182
						$width = $height = false;
183
184
					// Detect WP registered image size from HTML class
185
					if ( preg_match( '#class=["|\']?[^"\']*size-([^"\'\s]+)[^"\']*["|\']?#i', $images['img_tag'][ $index ], $size ) ) {
186
						$size = array_pop( $size );
187
188
						if ( false === $width && false === $height && 'full' != $size && array_key_exists( $size, $image_sizes ) ) {
189
							$width = (int) $image_sizes[ $size ]['width'];
190
							$height = (int) $image_sizes[ $size ]['height'];
191
							$transform = $image_sizes[ $size ]['crop'] ? 'resize' : 'fit';
192
						}
193
					} else {
194
						unset( $size );
195
					}
196
197
					// WP Attachment ID, if uploaded to this site
198
					if (
199
						preg_match( '#class=["|\']?[^"\']*wp-image-([\d]+)[^"\']*["|\']?#i', $images['img_tag'][ $index ], $attachment_id ) &&
200
						(
201
							0 === strpos( $src, $upload_dir['baseurl'] ) ||
202
							/**
203
							 * Filter whether an image using an attachment ID in its class has to be uploaded to the local site to go through Photon.
204
							 *
205
							 * @module photon
206
							 *
207
							 * @since 2.0.3
208
							 *
209
							 * @param bool false Was the image uploaded to the local site. Default to false.
210
							 * @param array $args {
211
							 * 	 Array of image details.
212
							 *
213
							 * 	 @type $src Image URL.
214
							 * 	 @type tag Image tag (Image HTML output).
215
							 * 	 @type $images Array of information about the image.
216
							 * 	 @type $index Image index.
217
							 * }
218
							 */
219
							apply_filters( 'jetpack_photon_image_is_local', false, compact( 'src', 'tag', 'images', 'index' ) )
220
						)
221
					) {
222
						$attachment_id = intval( array_pop( $attachment_id ) );
223
224
						if ( $attachment_id ) {
225
							$attachment = get_post( $attachment_id );
226
227
							// Basic check on returned post object
228
							if ( is_object( $attachment ) && ! is_wp_error( $attachment ) && 'attachment' == $attachment->post_type ) {
229
								$src_per_wp = wp_get_attachment_image_src( $attachment_id, isset( $size ) ? $size : 'full' );
230
231
								if ( self::validate_image_url( $src_per_wp[0] ) ) {
232
									$src = $src_per_wp[0];
233
									$fullsize_url = true;
234
235
									// Prevent image distortion if a detected dimension exceeds the image's natural dimensions
236
									if ( ( false !== $width && $width > $src_per_wp[1] ) || ( false !== $height && $height > $src_per_wp[2] ) ) {
237
										$width = false == $width ? false : min( $width, $src_per_wp[1] );
238
										$height = false == $height ? false : min( $height, $src_per_wp[2] );
239
									}
240
241
									// If no width and height are found, max out at source image's natural dimensions
242
									// Otherwise, respect registered image sizes' cropping setting
243
									if ( false == $width && false == $height ) {
244
										$width = $src_per_wp[1];
245
										$height = $src_per_wp[2];
246
										$transform = 'fit';
247
									} elseif ( isset( $size ) && array_key_exists( $size, $image_sizes ) && isset( $image_sizes[ $size ]['crop'] ) ) {
248
										$transform = (bool) $image_sizes[ $size ]['crop'] ? 'resize' : 'fit';
249
									}
250
								}
251
							} else {
252
								unset( $attachment_id );
253
								unset( $attachment );
254
							}
255
						}
256
					}
257
258
					// If image tag lacks width and height arguments, try to determine from strings WP appends to resized image filenames.
259
					if ( false === $width && false === $height ) {
260
						list( $width, $height ) = Jetpack_Photon::parse_dimensions_from_filename( $src );
261
					}
262
263
					// If width is available, constrain to $content_width
264
					if ( false !== $width && false === strpos( $width, '%' ) && is_numeric( $content_width ) ) {
265
						if ( $width > $content_width && false !== $height && false === strpos( $height, '%' ) ) {
266
							$height = round( ( $content_width * $height ) / $width );
267
							$width = $content_width;
268
						} elseif ( $width > $content_width ) {
269
							$width = $content_width;
270
						}
271
					}
272
273
					// Set a width if none is found and $content_width is available
274
					// If width is set in this manner and height is available, use `fit` instead of `resize` to prevent skewing
275
					if ( false === $width && is_numeric( $content_width ) ) {
276
						$width = (int) $content_width;
277
278
						if ( false !== $height )
279
							$transform = 'fit';
280
					}
281
282
					// Detect if image source is for a custom-cropped thumbnail and prevent further URL manipulation.
283
					if ( ! $fullsize_url && preg_match_all( '#-e[a-z0-9]+(-\d+x\d+)?\.(' . implode('|', self::$extensions ) . '){1}$#i', basename( $src ), $filename ) )
284
						$fullsize_url = true;
285
286
					// Build URL, first maybe removing WP's resized string so we pass the original image to Photon
287
					if ( ! $fullsize_url ) {
288
						$src = self::strip_image_dimensions_maybe( $src );
289
					}
290
291
					// Build array of Photon args and expose to filter before passing to Photon URL function
292
					$args = array();
293
294
					if ( false !== $width && false !== $height && false === strpos( $width, '%' ) && false === strpos( $height, '%' ) )
295
						$args[ $transform ] = $width . ',' . $height;
296
					elseif ( false !== $width )
297
						$args['w'] = $width;
298
					elseif ( false !== $height )
299
						$args['h'] = $height;
300
301
					/**
302
					 * Filter the array of Photon arguments added to an image when it goes through Photon.
303
					 * By default, only includes width and height values.
304
					 * @see https://developer.wordpress.com/docs/photon/api/
305
					 *
306
					 * @module photon
307
					 *
308
					 * @since 2.0.0
309
					 *
310
					 * @param array $args Array of Photon Arguments.
311
					 * @param array $args {
312
					 * 	 Array of image details.
313
					 *
314
					 * 	 @type $tag Image tag (Image HTML output).
315
					 * 	 @type $src Image URL.
316
					 * 	 @type $src_orig Original Image URL.
317
					 * 	 @type $width Image width.
318
					 * 	 @type $height Image height.
319
					 * }
320
					 */
321
					$args = apply_filters( 'jetpack_photon_post_image_args', $args, compact( 'tag', 'src', 'src_orig', 'width', 'height' ) );
322
323
					$photon_url = jetpack_photon_url( $src, $args );
324
325
					// Modify image tag if Photon function provides a URL
326
					// Ensure changes are only applied to the current image by copying and modifying the matched tag, then replacing the entire tag with our modified version.
327
					if ( $src != $photon_url ) {
328
						$new_tag = $tag;
329
330
						// If present, replace the link href with a Photoned URL for the full-size image.
331
						if ( ! empty( $images['link_url'][ $index ] ) && self::validate_image_url( $images['link_url'][ $index ] ) )
332
							$new_tag = preg_replace( '#(href=["|\'])' . $images['link_url'][ $index ] . '(["|\'])#i', '\1' . jetpack_photon_url( $images['link_url'][ $index ] ) . '\2', $new_tag, 1 );
333
334
						// Supplant the original source value with our Photon URL
335
						$photon_url = esc_url( $photon_url );
336
						$new_tag = str_replace( $src_orig, $photon_url, $new_tag );
337
338
						// If Lazy Load is in use, pass placeholder image through Photon
339
						if ( isset( $placeholder_src ) && self::validate_image_url( $placeholder_src ) ) {
340
							$placeholder_src = jetpack_photon_url( $placeholder_src );
341
342
							if ( $placeholder_src != $placeholder_src_orig )
343
								$new_tag = str_replace( $placeholder_src_orig, esc_url( $placeholder_src ), $new_tag );
0 ignored issues
show
Bug introduced by
The variable $placeholder_src_orig does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
344
345
							unset( $placeholder_src );
346
						}
347
348
						// Remove the width and height arguments from the tag to prevent distortion
349
						$new_tag = preg_replace( '#(?<=\s)(width|height)=["|\']?[\d%]+["|\']?\s?#i', '', $new_tag );
350
351
						// Tag an image for dimension checking
352
						$new_tag = preg_replace( '#(\s?/)?>(\s*</a>)?$#i', ' data-recalc-dims="1"\1>\2', $new_tag );
353
354
						// Replace original tag with modified version
355
						$content = str_replace( $tag, $new_tag, $content );
356
					}
357
				} elseif ( preg_match( '#^http(s)?://i[\d]{1}.wp.com#', $src ) && ! empty( $images['link_url'][ $index ] ) && self::validate_image_url( $images['link_url'][ $index ] ) ) {
358
					$new_tag = preg_replace( '#(href=["|\'])' . $images['link_url'][ $index ] . '(["|\'])#i', '\1' . jetpack_photon_url( $images['link_url'][ $index ] ) . '\2', $tag, 1 );
359
360
					$content = str_replace( $tag, $new_tag, $content );
361
				}
362
			}
363
		}
364
365
		return $content;
366
	}
367
368
	public static function filter_the_galleries( $galleries ) {
369
		if ( empty( $galleries ) || ! is_array( $galleries ) ) {
370
			return $galleries;
371
		}
372
373
		// Pass by reference, so we can modify them in place.
374
		foreach ( $galleries as &$this_gallery ) {
375
			if ( is_string( $this_gallery ) ) {
376
				$this_gallery = self::filter_the_content( $this_gallery );
377
		// LEAVING COMMENTED OUT as for the moment it doesn't seem
378
		// necessary and I'm not sure how it would propagate through.
379
		//	} elseif ( is_array( $this_gallery )
380
		//	           && ! empty( $this_gallery['src'] )
381
		//	           && ! empty( $this_gallery['type'] )
382
		//	           && in_array( $this_gallery['type'], array( 'rectangle', 'square', 'circle' ) ) ) {
383
		//		$this_gallery['src'] = array_map( 'jetpack_photon_url', $this_gallery['src'] );
384
			}
385
		}
386
		unset( $this_gallery ); // break the reference.
387
388
		return $galleries;
389
	}
390
391
	/**
392
	 ** CORE IMAGE RETRIEVAL
393
	 **/
394
395
	/**
396
	 * Filter post thumbnail image retrieval, passing images through Photon
397
	 *
398
	 * @param string|bool $image
399
	 * @param int $attachment_id
400
	 * @param string|array $size
401
	 * @uses is_admin, apply_filters, wp_get_attachment_url, self::validate_image_url, this::image_sizes, jetpack_photon_url
402
	 * @filter image_downsize
403
	 * @return string|bool
404
	 */
405
	public function filter_image_downsize( $image, $attachment_id, $size ) {
406
		// Don't foul up the admin side of things, and provide plugins a way of preventing Photon from being applied to images.
407
		if (
408
			is_admin() ||
409
			/**
410
			 * Provide plugins a way of preventing Photon from being applied to images retrieved from WordPress Core.
411
			 *
412
			 * @module photon
413
			 *
414
			 * @since 2.0.0
415
			 *
416
			 * @param bool false Stop Photon from being applied to the image. Default to false.
417
			 * @param array $args {
418
			 * 	 Array of image details.
419
			 *
420
			 * 	 @type $image Image URL.
421
			 * 	 @type $attachment_id Attachment ID of the image.
422
			 * 	 @type $size Image size. Can be a string (name of the image size, e.g. full) or an integer.
423
			 * }
424
			 */
425
			apply_filters( 'jetpack_photon_override_image_downsize', false, compact( 'image', 'attachment_id', 'size' ) )
426
		)
427
			return $image;
428
429
		// Get the image URL and proceed with Photon-ification if successful
430
		$image_url = wp_get_attachment_url( $attachment_id );
431
432
		// Set this to true later when we know we have size meta.
433
		$has_size_meta = false;
434
435
		if ( $image_url ) {
436
			// Check if image URL should be used with Photon
437
			if ( ! self::validate_image_url( $image_url ) )
438
				return $image;
439
440
			$intermediate = true; // For the fourth array item returned by the image_downsize filter.
441
442
			// If an image is requested with a size known to WordPress, use that size's settings with Photon
443
			if ( ( is_string( $size ) || is_int( $size ) ) && array_key_exists( $size, self::image_sizes() ) ) {
444
				$image_args = self::image_sizes();
445
				$image_args = $image_args[ $size ];
446
447
				$photon_args = array();
448
449
				$image_meta = image_get_intermediate_size( $attachment_id, $size );
450
451
				// 'full' is a special case: We need consistent data regardless of the requested size.
452
				if ( 'full' == $size ) {
453
					$image_meta = wp_get_attachment_metadata( $attachment_id );
454
					$intermediate = false;
455
				} elseif ( ! $image_meta ) {
456
					// If we still don't have any image meta at this point, it's probably from a custom thumbnail size
457
					// for an image that was uploaded before the custom image was added to the theme.  Try to determine the size manually.
458
					$image_meta = wp_get_attachment_metadata( $attachment_id );
459
460 View Code Duplication
					if ( isset( $image_meta['width'], $image_meta['height'] ) ) {
461
						$image_resized = image_resize_dimensions( $image_meta['width'], $image_meta['height'], $image_args['width'], $image_args['height'], $image_args['crop'] );
462
						if ( $image_resized ) { // This could be false when the requested image size is larger than the full-size image.
463
							$image_meta['width'] = $image_resized[6];
464
							$image_meta['height'] = $image_resized[7];
465
						}
466
					}
467
				}
468
469 View Code Duplication
				if ( isset( $image_meta['width'], $image_meta['height'] ) ) {
470
					$image_args['width']  = $image_meta['width'];
471
					$image_args['height'] = $image_meta['height'];
472
473
					list( $image_args['width'], $image_args['height'] ) = image_constrain_size_for_editor( $image_args['width'], $image_args['height'], $size, 'display' );
474
					$has_size_meta = true;
475
				}
476
477
				// Expose determined arguments to a filter before passing to Photon
478
				$transform = $image_args['crop'] ? 'resize' : 'fit';
479
480
				// Check specified image dimensions and account for possible zero values; photon fails to resize if a dimension is zero.
481
				if ( 0 == $image_args['width'] || 0 == $image_args['height'] ) {
482
					if ( 0 == $image_args['width'] && 0 < $image_args['height'] ) {
483
						$photon_args['h'] = $image_args['height'];
484
					} elseif ( 0 == $image_args['height'] && 0 < $image_args['width'] ) {
485
						$photon_args['w'] = $image_args['width'];
486
					}
487
				} else {
488
					if ( ( 'resize' === $transform ) && $image_meta = wp_get_attachment_metadata( $attachment_id ) ) {
489
						if ( isset( $image_meta['width'], $image_meta['height'] ) ) {
490
							// Lets make sure that we don't upscale images since wp never upscales them as well
491
							$smaller_width  = ( ( $image_meta['width']  < $image_args['width']  ) ? $image_meta['width']  : $image_args['width']  );
492
							$smaller_height = ( ( $image_meta['height'] < $image_args['height'] ) ? $image_meta['height'] : $image_args['height'] );
493
494
							$photon_args[ $transform ] = $smaller_width . ',' . $smaller_height;
495
						}
496
					} else {
497
						$photon_args[ $transform ] = $image_args['width'] . ',' . $image_args['height'];
498
					}
499
500
				}
501
502
503
				/**
504
				 * Filter the Photon Arguments added to an image when going through Photon, when that image size is a string.
505
				 * Image size will be a string (e.g. "full", "medium") when it is known to WordPress.
506
				 *
507
				 * @module photon
508
				 *
509
				 * @since 2.0.0
510
				 *
511
				 * @param array $photon_args Array of Photon arguments.
512
				 * @param array $args {
513
				 * 	 Array of image details.
514
				 *
515
				 * 	 @type $image_args Array of Image arguments (width, height, crop).
516
				 * 	 @type $image_url Image URL.
517
				 * 	 @type $attachment_id Attachment ID of the image.
518
				 * 	 @type $size Image size. Can be a string (name of the image size, e.g. full) or an integer.
519
				 * 	 @type $transform Value can be resize or fit.
520
				 *                    @see https://developer.wordpress.com/docs/photon/api
521
				 * }
522
				 */
523
				$photon_args = apply_filters( 'jetpack_photon_image_downsize_string', $photon_args, compact( 'image_args', 'image_url', 'attachment_id', 'size', 'transform' ) );
524
525
				// Generate Photon URL
526
				$image = array(
527
					jetpack_photon_url( $image_url, $photon_args ),
528
					$has_size_meta ? $image_args['width'] : false,
529
					$has_size_meta ? $image_args['height'] : false,
530
					$intermediate
531
				);
532
			} elseif ( is_array( $size ) ) {
533
				// Pull width and height values from the provided array, if possible
534
				$width = isset( $size[0] ) ? (int) $size[0] : false;
535
				$height = isset( $size[1] ) ? (int) $size[1] : false;
536
537
				// Don't bother if necessary parameters aren't passed.
538
				if ( ! $width || ! $height ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $width of type integer|false is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $height of type integer|false is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
539
					return $image;
540
				}
541
542
				$image_meta = wp_get_attachment_metadata( $attachment_id );
543
				if ( isset( $image_meta['width'], $image_meta['height'] ) ) {
544
					$image_resized = image_resize_dimensions( $image_meta['width'], $image_meta['height'], $width, $height );
545
546
					if ( $image_resized ) { // This could be false when the requested image size is larger than the full-size image.
547
						$width = $image_resized[6];
548
						$height = $image_resized[7];
549
					} else {
550
						$width = $image_meta['width'];
551
						$height = $image_meta['height'];
552
					}
553
554
					$has_size_meta = true;
555
				}
556
557
				list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size );
558
559
				// Expose arguments to a filter before passing to Photon
560
				$photon_args = array(
561
					'fit' => $width . ',' . $height
562
				);
563
564
				/**
565
				 * Filter the Photon Arguments added to an image when going through Photon,
566
				 * when the image size is an array of height and width values.
567
				 *
568
				 * @module photon
569
				 *
570
				 * @since 2.0.0
571
				 *
572
				 * @param array $photon_args Array of Photon arguments.
573
				 * @param array $args {
574
				 * 	 Array of image details.
575
				 *
576
				 * 	 @type $width Image width.
577
				 * 	 @type height Image height.
578
				 * 	 @type $image_url Image URL.
579
				 * 	 @type $attachment_id Attachment ID of the image.
580
				 * }
581
				 */
582
				$photon_args = apply_filters( 'jetpack_photon_image_downsize_array', $photon_args, compact( 'width', 'height', 'image_url', 'attachment_id' ) );
583
584
				// Generate Photon URL
585
				$image = array(
586
					jetpack_photon_url( $image_url, $photon_args ),
587
					$has_size_meta ? $width : false,
588
					$has_size_meta ? $height : false,
589
					$intermediate
590
				);
591
			}
592
		}
593
594
		return $image;
595
	}
596
597
	/**
598
	 * Filters an array of image `srcset` values, replacing each URL with its Photon equivalent.
599
	 *
600
	 * @since 3.8.0
601
	 * @since 4.0.4 Added automatically additional sizes beyond declared image sizes.
602
	 * @param array $sources An array of image urls and widths.
603
	 * @uses self::validate_image_url, jetpack_photon_url, Jetpack_Photon::parse_from_filename
604
	 * @uses Jetpack_Photon::strip_image_dimensions_maybe, Jetpack::get_content_width
605
	 * @return array An array of Photon image urls and widths.
606
	 */
607
	public function filter_srcset_array( $sources, $size_array, $image_src, $image_meta ) {
608
		$upload_dir = wp_upload_dir();
609
610
		foreach ( $sources as $i => $source ) {
611
			if ( ! self::validate_image_url( $source['url'] ) ) {
612
				continue;
613
			}
614
615
			$url = $source['url'];
616
			list( $width, $height ) = Jetpack_Photon::parse_dimensions_from_filename( $url );
617
618
			// It's quicker to get the full size with the data we have already, if available
619
			if ( isset( $image_meta['file'] ) ) {
620
				$url = trailingslashit( $upload_dir['baseurl'] ) . $image_meta['file'];
621
			} else {
622
				$url = Jetpack_Photon::strip_image_dimensions_maybe( $url );
623
			}
624
625
			$args = array();
626
			if ( 'w' === $source['descriptor'] ) {
627
				if ( $height && ( $source['value'] == $width ) ) {
628
					$args['resize'] = $width . ',' . $height;
629
				} else {
630
					$args['w'] = $source['value'];
631
				}
632
633
			}
634
635
			$sources[ $i ]['url'] = jetpack_photon_url( $url, $args );
636
		}
637
638
		/**
639
		 * At this point, $sources is the original srcset with Photonized URLs.
640
		 * Now, we're going to construct additional sizes based on multiples of the content_width.
641
		 * This will reduce the gap between the largest defined size and the original image.
642
		 */
643
644
		/**
645
		 * Filter the multiplier Photon uses to create new srcset items.
646
		 * Return false to short-circuit and bypass auto-generation.
647
		 *
648
		 * @module photon
649
		 *
650
		 * @since 4.0.4
651
		 *
652
		 * @param array|bool $multipliers Array of multipliers to use or false to bypass.
653
		 */
654
		$multipliers = apply_filters( 'jetpack_photon_srcset_multipliers', array( 2, 3 ) );
655
656
		if ( is_array( $multipliers ) // Short-circuit via jetpack_photon_srcset_multipliers filter.
657
			&& isset( $image_meta['width'] ) && isset( $image_meta['height'] ) && isset( $image_meta['file'] ) // Verify basic meta is intact.
658
			&& isset( $size_array[0] ) && isset( $size_array[1] ) // Verify we have the requested width/height.
659
			) {
660
661
			$url = trailingslashit( $upload_dir['baseurl'] ) . $image_meta['file'];
662
			$fullwidth  = $image_meta['width'];
663
			$fullheight = $image_meta['height'];
664
			$reqwidth   = $size_array[0];
665
			$reqheight  = $size_array[1];
666
667
			$constrained_size = wp_constrain_dimensions( $fullwidth, $fullheight, $reqwidth );
668
			$expected_size = array( $reqwidth, $reqheight );
669
670
			if ( abs( $constrained_size[0] - $expected_size[0] ) <= 1 && abs( $constrained_size[1] - $expected_size[1] ) <= 1 ) {
671
				$crop = 'soft';
672
				$base = Jetpack::get_content_width() ? Jetpack::get_content_width() : 1000; // Provide a default width if none set by the theme.
673
			}
674
			else {
675
				$crop = 'hard';
676
				$base = $reqwidth;
677
			}
678
679
680
			$currentwidths = array_keys( $sources );
681
			$newsources = null;
682
683
			foreach ( $multipliers as $multiplier ) {
684
				$newwidth = $base * $multiplier;
685
				foreach ( $currentwidths as $currentwidth ){
686
					// If a new width would be within 100 pixes of an existing one or larger than the full size image, skip.
687
					if ( abs( $currentwidth - $newwidth ) < 50 || ( $newwidth > $fullwidth ) ) {
688
						continue 2; // Back to the foreach ( $multipliers as $multiplier )
689
					}
690
				} // foreach ( $currentwidths as $currentwidth ){
691
692
				if ( 'soft' == $crop ) {
693
					$args = array(
694
						'w' => $newwidth,
695
					);
696
				}
697
				else { // hard crop, e.g. add_image_size( 'example', 200, 200, true );
698
					$args = array(
699
						'zoom'   => $multiplier,
700
						'resize' => $reqwidth . ',' . $reqheight,
701
					);
702
				}
703
704
				$newsources[ $newwidth ] = array(
705
					'url'         => jetpack_photon_url( $url, $args ),
706
					'descriptor'  => 'w',
707
					'value'       => $newwidth,
708
					);
709
			} // foreach ( $multipliers as $multiplier )
710
			if ( is_array( $newsources ) ) {
711
				$sources = array_merge( $sources, $newsources );
712
			}
713
		} // if ( isset( $image_meta['width'] ) && isset( $image_meta['file'] ) )
714
715
		return $sources;
716
	}
717
718
	/**
719
	 * Filters an array of image `sizes` values, using $content_width instead of image's full size.
720
	 *
721
	 * @since 4.0.4
722
	 * @since 4.1.0 Returns early for images not within the_content.
723
	 * @param array $sizes An array of media query breakpoints.
724
	 * @param array $size  Width and height of the image
725
	 * @uses Jetpack::get_content_width
726
	 * @return array An array of media query breakpoints.
727
	 */
728
	public function filter_sizes( $sizes, $size ) {
729
		if ( ! doing_filter( 'the_content' ) ){
730
			return $sizes;
731
		}
732
		$content_width = Jetpack::get_content_width();
733
		if ( ! $content_width ) {
734
			$content_width = 1000;
735
		}
736
737
		if ( ( is_array( $size ) && $size[0] < $content_width ) ) {
738
			return $sizes;
739
		}
740
741
		return sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $content_width );
742
	}
743
744
	/**
745
	 ** GENERAL FUNCTIONS
746
	 **/
747
748
	/**
749
	 * Ensure image URL is valid for Photon.
750
	 * Though Photon functions address some of the URL issues, we should avoid unnecessary processing if we know early on that the image isn't supported.
751
	 *
752
	 * @param string $url
753
	 * @uses wp_parse_args
754
	 * @return bool
755
	 */
756
	protected static function validate_image_url( $url ) {
757
		$parsed_url = @parse_url( $url );
758
759
		if ( ! $parsed_url )
760
			return false;
761
762
		// Parse URL and ensure needed keys exist, since the array returned by `parse_url` only includes the URL components it finds.
763
		$url_info = wp_parse_args( $parsed_url, array(
764
			'scheme' => null,
765
			'host'   => null,
766
			'port'   => null,
767
			'path'   => null
768
		) );
769
770
		// Bail if scheme isn't http or port is set that isn't port 80
771
		if (
772
			( 'http' != $url_info['scheme'] || ! in_array( $url_info['port'], array( 80, null ) ) ) &&
773
			/**
774
			 * Allow Photon to fetch images that are served via HTTPS.
775
			 *
776
			 * @module photon
777
			 *
778
			 * @since 2.4.0
779
			 * @since 3.9.0 Default to false.
780
			 *
781
			 * @param bool $reject_https Should Photon ignore images using the HTTPS scheme. Default to false.
782
			 */
783
			apply_filters( 'jetpack_photon_reject_https', false )
784
		) {
785
			return false;
786
		}
787
788
		// Bail if no host is found
789
		if ( is_null( $url_info['host'] ) )
790
			return false;
791
792
		// Bail if the image alredy went through Photon
793
		if ( preg_match( '#^i[\d]{1}.wp.com$#i', $url_info['host'] ) )
794
			return false;
795
796
		// Bail if no path is found
797
		if ( is_null( $url_info['path'] ) )
798
			return false;
799
800
		// Ensure image extension is acceptable
801
		if ( ! in_array( strtolower( pathinfo( $url_info['path'], PATHINFO_EXTENSION ) ), self::$extensions ) )
802
			return false;
803
804
		// If we got this far, we should have an acceptable image URL
805
		// But let folks filter to decline if they prefer.
806
		/**
807
		 * Overwrite the results of the validation steps an image goes through before to be considered valid to be used by Photon.
808
		 *
809
		 * @module photon
810
		 *
811
		 * @since 3.0.0
812
		 *
813
		 * @param bool true Is the image URL valid and can it be used by Photon. Default to true.
814
		 * @param string $url Image URL.
815
		 * @param array $parsed_url Array of information about the image.
816
		 */
817
		return apply_filters( 'photon_validate_image_url', true, $url, $parsed_url );
818
	}
819
820
	/**
821
	 * Checks if the file exists before it passes the file to photon
822
	 *
823
	 * @param string $src The image URL
824
	 * @return string
825
	 **/
826
	protected static function strip_image_dimensions_maybe( $src ){
827
		$stripped_src = $src;
0 ignored issues
show
Unused Code introduced by
$stripped_src is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
828
829
		// Build URL, first removing WP's resized string so we pass the original image to Photon
830
		if ( preg_match( '#(-\d+x\d+)\.(' . implode('|', self::$extensions ) . '){1}$#i', $src, $src_parts ) ) {
831
			$stripped_src = str_replace( $src_parts[1], '', $src );
832
			$upload_dir = wp_upload_dir();
833
834
			// Extracts the file path to the image minus the base url
835
			$file_path = substr( $stripped_src, strlen ( $upload_dir['baseurl'] ) );
836
837
			if( file_exists( $upload_dir["basedir"] . $file_path ) )
838
				$src = $stripped_src;
839
		}
840
841
		return $src;
842
	}
843
844
	/**
845
	 * Provide an array of available image sizes and corresponding dimensions.
846
	 * Similar to get_intermediate_image_sizes() except that it includes image sizes' dimensions, not just their names.
847
	 *
848
	 * @global $wp_additional_image_sizes
849
	 * @uses get_option
850
	 * @return array
851
	 */
852
	protected static function image_sizes() {
853
		if ( null == self::$image_sizes ) {
854
			global $_wp_additional_image_sizes;
855
856
			// Populate an array matching the data structure of $_wp_additional_image_sizes so we have a consistent structure for image sizes
857
			$images = array(
858
				'thumb'  => array(
859
					'width'  => intval( get_option( 'thumbnail_size_w' ) ),
860
					'height' => intval( get_option( 'thumbnail_size_h' ) ),
861
					'crop'   => (bool) get_option( 'thumbnail_crop' )
862
				),
863
				'medium' => array(
864
					'width'  => intval( get_option( 'medium_size_w' ) ),
865
					'height' => intval( get_option( 'medium_size_h' ) ),
866
					'crop'   => false
867
				),
868
				'large'  => array(
869
					'width'  => intval( get_option( 'large_size_w' ) ),
870
					'height' => intval( get_option( 'large_size_h' ) ),
871
					'crop'   => false
872
				),
873
				'full'   => array(
874
					'width'  => null,
875
					'height' => null,
876
					'crop'   => false
877
				)
878
			);
879
880
			// Compatibility mapping as found in wp-includes/media.php
881
			$images['thumbnail'] = $images['thumb'];
882
883
			// Update class variable, merging in $_wp_additional_image_sizes if any are set
884
			if ( is_array( $_wp_additional_image_sizes ) && ! empty( $_wp_additional_image_sizes ) )
885
				self::$image_sizes = array_merge( $images, $_wp_additional_image_sizes );
886
			else
887
				self::$image_sizes = $images;
888
		}
889
890
		return is_array( self::$image_sizes ) ? self::$image_sizes : array();
891
	}
892
893
	/**
894
	 * Pass og:image URLs through Photon
895
	 *
896
	 * @param array $tags
897
	 * @param array $parameters
898
	 * @uses jetpack_photon_url
899
	 * @return array
900
	 */
901
	function filter_open_graph_tags( $tags, $parameters ) {
902
		if ( empty( $tags['og:image'] ) ) {
903
			return $tags;
904
		}
905
906
		$photon_args = array(
907
			'fit' => sprintf( '%d,%d', 2 * $parameters['image_width'], 2 * $parameters['image_height'] ),
908
		);
909
910
		if ( is_array( $tags['og:image'] ) ) {
911
			$images = array();
912
			foreach ( $tags['og:image'] as $image ) {
913
				$images[] = jetpack_photon_url( $image, $photon_args );
914
			}
915
			$tags['og:image'] = $images;
916
		} else {
917
			$tags['og:image'] = jetpack_photon_url( $tags['og:image'], $photon_args );
918
		}
919
920
		return $tags;
921
	}
922
923
	/**
924
	 * Enqueue Photon helper script
925
	 *
926
	 * @uses wp_enqueue_script, plugins_url
927
	 * @action wp_enqueue_script
928
	 * @return null
929
	 */
930
	public function action_wp_enqueue_scripts() {
931
		wp_enqueue_script( 'jetpack-photon', plugins_url( 'modules/photon/photon.js', JETPACK__PLUGIN_FILE ), array( 'jquery' ), 20130122, true );
932
	}
933
}
934