1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Automattic\Jetpack\Assets; |
4
|
|
|
|
5
|
|
|
class Jetpack_Photon { |
6
|
|
|
/** |
7
|
|
|
* Class variables |
8
|
|
|
*/ |
9
|
|
|
// Oh look, a singleton |
10
|
|
|
private static $__instance = null; |
11
|
|
|
|
12
|
|
|
// Allowed extensions must match http://code.trac.wordpress.org/browser/photon/index.php#L31 |
13
|
|
|
protected static $extensions = array( |
14
|
|
|
'gif', |
15
|
|
|
'jpg', |
16
|
|
|
'jpeg', |
17
|
|
|
'png' |
18
|
|
|
); |
19
|
|
|
|
20
|
|
|
// Don't access this directly. Instead, use self::image_sizes() so it's actually populated with something. |
21
|
|
|
protected static $image_sizes = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Singleton implementation |
25
|
|
|
* |
26
|
|
|
* @return object |
27
|
|
|
*/ |
28
|
|
|
public static function instance() { |
29
|
|
|
if ( ! is_a( self::$__instance, 'Jetpack_Photon' ) ) { |
30
|
|
|
self::$__instance = new Jetpack_Photon; |
31
|
|
|
self::$__instance->setup(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return self::$__instance; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Silence is golden. |
39
|
|
|
*/ |
40
|
|
|
private function __construct() {} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Register actions and filters, but only if basic Photon functions are available. |
44
|
|
|
* The basic functions are found in ./functions.photon.php. |
45
|
|
|
* |
46
|
|
|
* @uses add_action, add_filter |
47
|
|
|
* @return null |
48
|
|
|
*/ |
49
|
|
|
private function setup() { |
50
|
|
|
if ( ! function_exists( 'jetpack_photon_url' ) ) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Images in post content and galleries |
55
|
|
|
add_filter( 'the_content', array( __CLASS__, 'filter_the_content' ), 999999 ); |
56
|
|
|
add_filter( 'get_post_galleries', array( __CLASS__, 'filter_the_galleries' ), 999999 ); |
57
|
|
|
add_filter( 'widget_media_image_instance', array( __CLASS__, 'filter_the_image_widget' ), 999999 ); |
58
|
|
|
|
59
|
|
|
// Core image retrieval |
60
|
|
|
add_filter( 'image_downsize', array( $this, 'filter_image_downsize' ), 10, 3 ); |
61
|
|
|
add_filter( 'rest_request_before_callbacks', array( $this, 'should_rest_photon_image_downsize' ), 10, 3 ); |
62
|
|
|
add_filter( 'rest_request_after_callbacks', array( $this, 'cleanup_rest_photon_image_downsize' ) ); |
63
|
|
|
|
64
|
|
|
// Responsive image srcset substitution |
65
|
|
|
add_filter( 'wp_calculate_image_srcset', array( $this, 'filter_srcset_array' ), 10, 5 ); |
66
|
|
|
add_filter( 'wp_calculate_image_sizes', array( $this, 'filter_sizes' ), 1, 2 ); // Early so themes can still easily filter. |
67
|
|
|
|
68
|
|
|
// Helpers for maniuplated images |
69
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'action_wp_enqueue_scripts' ), 9 ); |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Allow Photon to disable uploaded images resizing and use its own resize capabilities instead. |
73
|
|
|
* |
74
|
|
|
* @module photon |
75
|
|
|
* |
76
|
|
|
* @since 7.1.0 |
77
|
|
|
* |
78
|
|
|
* @param bool false Should Photon enable noresize mode. Default to false. |
79
|
|
|
*/ |
80
|
|
|
if ( apply_filters( 'jetpack_photon_noresize_mode', false ) ) { |
81
|
|
|
$this->enable_noresize_mode(); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Enables the noresize mode for Photon, allowing to avoid intermediate size files generation. |
87
|
|
|
*/ |
88
|
|
|
private function enable_noresize_mode() { |
89
|
|
|
jetpack_require_lib( 'class.jetpack-photon-image-sizes' ); |
90
|
|
|
|
91
|
|
|
// The main objective of noresize mode is to disable additional resized image versions creation. |
92
|
|
|
// This filter handles removal of additional sizes. |
93
|
|
|
add_filter( 'intermediate_image_sizes_advanced', array( __CLASS__, 'filter_photon_noresize_intermediate_sizes' ) ); |
94
|
|
|
|
95
|
|
|
// Load the noresize srcset solution on priority of 20, allowing other plugins to set sizes earlier. |
96
|
|
|
add_filter( 'wp_get_attachment_metadata', array( __CLASS__, 'filter_photon_norezise_maybe_inject_sizes' ), 20, 2 ); |
97
|
|
|
|
98
|
|
|
// Photonize thumbnail URLs in the API response. |
99
|
|
|
add_filter( 'rest_api_thumbnail_size_urls', array( __CLASS__, 'filter_photon_noresize_thumbnail_urls' ) ); |
100
|
|
|
|
101
|
|
|
// This allows to assign the Photon domain to images that normally use the home URL as base. |
102
|
|
|
add_filter( 'jetpack_photon_domain', array( __CLASS__, 'filter_photon_norezise_domain' ), 10, 2 ); |
103
|
|
|
|
104
|
|
|
add_filter( 'the_content', array( __CLASS__, 'filter_content_add' ), 0 ); |
105
|
|
|
|
106
|
|
|
// Jetpack hooks in at six nines (999999) so this filter does at seven. |
107
|
|
|
add_filter( 'the_content', array( __CLASS__, 'filter_content_remove' ), 9999999 ); |
108
|
|
|
|
109
|
|
|
// Regular Photon operation mode filter doesn't run when is_admin(), so we need an additional filter. |
110
|
|
|
// This is temporary until Jetpack allows more easily running these filters for is_admin(). |
111
|
|
|
if ( is_admin() ) { |
112
|
|
|
add_filter( 'image_downsize', array( $this, 'filter_image_downsize' ), 5, 3 ); |
113
|
|
|
|
114
|
|
|
// Allows any image that gets passed to Photon to be resized via Photon. |
115
|
|
|
add_filter( 'jetpack_photon_admin_allow_image_downsize', '__return_true' ); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* This is our catch-all to strip dimensions from intermediate images in content. |
121
|
|
|
* Since this primarily only impacts post_content we do a little dance to add the filter early |
122
|
|
|
* to `the_content` and then remove it later on in the same hook. |
123
|
|
|
* |
124
|
|
|
* @param String $content the post content. |
125
|
|
|
* @return String the post content unchanged. |
126
|
|
|
*/ |
127
|
|
|
public static function filter_content_add( $content ) { |
128
|
|
|
add_filter( 'jetpack_photon_pre_image_url', array( __CLASS__, 'strip_image_dimensions_maybe' ) ); |
129
|
|
|
return $content; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Removing the content filter that was set previously. |
134
|
|
|
* |
135
|
|
|
* @param String $content the post content. |
136
|
|
|
* @return String the post content unchanged. |
137
|
|
|
*/ |
138
|
|
|
public static function filter_content_remove( $content ) { |
139
|
|
|
remove_filter( 'jetpack_photon_pre_image_url', array( __CLASS__, 'strip_image_dimensions_maybe' ) ); |
140
|
|
|
return $content; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Short circuits the Photon filter to enable Photon processing for any URL. |
145
|
|
|
* |
146
|
|
|
* @param String $photon_url a proposed Photon URL for the media file. |
147
|
|
|
* @param String $image_url the original media URL. |
148
|
|
|
* @return String an URL to be used for the media file. |
149
|
|
|
*/ |
150
|
|
|
public static function filter_photon_norezise_domain( $photon_url, $image_url ) { |
151
|
|
|
return $photon_url; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Disables intermediate sizes to disallow resizing. |
156
|
|
|
* |
157
|
|
|
* @param Array $sizes an array containing image sizes. |
158
|
|
|
* @return Boolean |
159
|
|
|
*/ |
160
|
|
|
public static function filter_photon_noresize_intermediate_sizes( $sizes ) { |
161
|
|
|
return array(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public static function filter_photon_noresize_thumbnail_urls( $sizes ) { |
165
|
|
|
foreach ( $sizes as $size => $url ) { |
166
|
|
|
$parts = explode( '?', $url ); |
167
|
|
|
$arguments = isset( $parts[1] ) ? $parts[1] : array(); |
168
|
|
|
|
169
|
|
|
$sizes[ $size ] = jetpack_photon_url( $url, wp_parse_args( $arguments ) ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $sizes; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Inject image sizes to attachment metadata. |
177
|
|
|
* |
178
|
|
|
* @param array $data Attachment metadata. |
179
|
|
|
* @param int $attachment_id Attachment's post ID. |
180
|
|
|
* |
181
|
|
|
* @return array Attachment metadata. |
182
|
|
|
*/ |
183
|
|
|
public static function filter_photon_norezise_maybe_inject_sizes( $data, $attachment_id ) { |
184
|
|
|
// Can't do much if data is empty. |
185
|
|
|
if ( empty( $data ) ) { |
186
|
|
|
return $data; |
187
|
|
|
} |
188
|
|
|
$sizes_already_exist = ( |
189
|
|
|
true === is_array( $data ) |
190
|
|
|
&& true === array_key_exists( 'sizes', $data ) |
191
|
|
|
&& true === is_array( $data['sizes'] ) |
192
|
|
|
&& false === empty( $data['sizes'] ) |
193
|
|
|
); |
194
|
|
|
if ( $sizes_already_exist ) { |
195
|
|
|
return $data; |
196
|
|
|
} |
197
|
|
|
// Missing some critical data we need to determine sizes, not processing. |
198
|
|
|
if ( ! isset( $data['file'] ) |
199
|
|
|
|| ! isset( $data['width'] ) |
200
|
|
|
|| ! isset( $data['height'] ) |
201
|
|
|
) { |
202
|
|
|
return $data; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$mime_type = get_post_mime_type( $attachment_id ); |
206
|
|
|
$attachment_is_image = preg_match( '!^image/!', $mime_type ); |
207
|
|
|
|
208
|
|
|
if ( 1 === $attachment_is_image ) { |
209
|
|
|
$image_sizes = new Jetpack_Photon_ImageSizes( $attachment_id, $data ); |
210
|
|
|
$data['sizes'] = $image_sizes->generate_sizes_meta(); |
211
|
|
|
} |
212
|
|
|
return $data; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Inject image sizes to Jetpack REST API responses. This wraps the filter_photon_norezise_maybe_inject_sizes function. |
217
|
|
|
* |
218
|
|
|
* @param array $data Attachment sizes data. |
|
|
|
|
219
|
|
|
* @param int $attachment_id Attachment's post ID. |
220
|
|
|
* |
221
|
|
|
* @return array Attachment sizes array. |
222
|
|
|
*/ |
223
|
|
|
public static function filter_photon_norezise_maybe_inject_sizes_api( $sizes, $attachment_id ) { |
224
|
|
|
return self::filter_photon_norezise_maybe_inject_sizes( wp_get_attachment_metadata( $attachment_id ), $attachment_id ); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
** IN-CONTENT IMAGE MANIPULATION FUNCTIONS |
229
|
|
|
**/ |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Match all images and any relevant <a> tags in a block of HTML. |
233
|
|
|
* |
234
|
|
|
* @param string $content Some HTML. |
235
|
|
|
* @return array An array of $images matches, where $images[0] is |
236
|
|
|
* an array of full matches, and the link_url, img_tag, |
237
|
|
|
* and img_url keys are arrays of those matches. |
238
|
|
|
*/ |
239
|
|
|
public static function parse_images_from_html( $content ) { |
240
|
|
|
$images = array(); |
241
|
|
|
|
242
|
|
|
if ( preg_match_all( '#(?:<a[^>]+?href=["|\'](?P<link_url>[^\s]+?)["|\'][^>]*?>\s*)?(?P<img_tag><(?:img|amp-img|amp-anim)[^>]*?\s+?src=["|\'](?P<img_url>[^\s]+?)["|\'].*?>){1}(?:\s*</a>)?#is', $content, $images ) ) { |
243
|
|
|
foreach ( $images as $key => $unused ) { |
244
|
|
|
// Simplify the output as much as possible, mostly for confirming test results. |
245
|
|
|
if ( is_numeric( $key ) && $key > 0 ) |
246
|
|
|
unset( $images[$key] ); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return $images; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return array(); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Try to determine height and width from strings WP appends to resized image filenames. |
257
|
|
|
* |
258
|
|
|
* @param string $src The image URL. |
259
|
|
|
* @return array An array consisting of width and height. |
260
|
|
|
*/ |
261
|
|
|
public static function parse_dimensions_from_filename( $src ) { |
262
|
|
|
$width_height_string = array(); |
263
|
|
|
|
264
|
|
|
if ( preg_match( '#-(\d+)x(\d+)\.(?:' . implode('|', self::$extensions ) . '){1}$#i', $src, $width_height_string ) ) { |
265
|
|
|
$width = (int) $width_height_string[1]; |
266
|
|
|
$height = (int) $width_height_string[2]; |
267
|
|
|
|
268
|
|
|
if ( $width && $height ) |
269
|
|
|
return array( $width, $height ); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return array( false, false ); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Identify images in post content, and if images are local (uploaded to the current site), pass through Photon. |
277
|
|
|
* |
278
|
|
|
* @param string $content |
279
|
|
|
* @uses self::validate_image_url, apply_filters, jetpack_photon_url, esc_url |
280
|
|
|
* @filter the_content |
281
|
|
|
* @return string |
282
|
|
|
*/ |
283
|
|
|
public static function filter_the_content( $content ) { |
284
|
|
|
$images = Jetpack_Photon::parse_images_from_html( $content ); |
285
|
|
|
|
286
|
|
|
if ( ! empty( $images ) ) { |
287
|
|
|
$content_width = Jetpack::get_content_width(); |
288
|
|
|
|
289
|
|
|
$image_sizes = self::image_sizes(); |
290
|
|
|
$upload_dir = wp_get_upload_dir(); |
291
|
|
|
|
292
|
|
|
foreach ( $images[0] as $index => $tag ) { |
293
|
|
|
// Default to resize, though fit may be used in certain cases where a dimension cannot be ascertained |
294
|
|
|
$transform = 'resize'; |
295
|
|
|
|
296
|
|
|
// Start with a clean attachment ID each time |
297
|
|
|
$attachment_id = false; |
298
|
|
|
|
299
|
|
|
// Flag if we need to munge a fullsize URL |
300
|
|
|
$fullsize_url = false; |
301
|
|
|
|
302
|
|
|
// Identify image source |
303
|
|
|
$src = $src_orig = $images['img_url'][ $index ]; |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Allow specific images to be skipped by Photon. |
307
|
|
|
* |
308
|
|
|
* @module photon |
309
|
|
|
* |
310
|
|
|
* @since 2.0.3 |
311
|
|
|
* |
312
|
|
|
* @param bool false Should Photon ignore this image. Default to false. |
313
|
|
|
* @param string $src Image URL. |
314
|
|
|
* @param string $tag Image Tag (Image HTML output). |
315
|
|
|
*/ |
316
|
|
|
if ( apply_filters( 'jetpack_photon_skip_image', false, $src, $tag ) ) |
317
|
|
|
continue; |
318
|
|
|
|
319
|
|
|
// Support Automattic's Lazy Load plugin |
320
|
|
|
// Can't modify $tag yet as we need unadulterated version later |
321
|
|
|
if ( preg_match( '#data-lazy-src=["|\'](.+?)["|\']#i', $images['img_tag'][ $index ], $lazy_load_src ) ) { |
322
|
|
|
$placeholder_src = $placeholder_src_orig = $src; |
323
|
|
|
$src = $src_orig = $lazy_load_src[1]; |
324
|
|
|
} elseif ( preg_match( '#data-lazy-original=["|\'](.+?)["|\']#i', $images['img_tag'][ $index ], $lazy_load_src ) ) { |
325
|
|
|
$placeholder_src = $placeholder_src_orig = $src; |
326
|
|
|
$src = $src_orig = $lazy_load_src[1]; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
// Check if image URL should be used with Photon |
330
|
|
|
if ( self::validate_image_url( $src ) ) { |
331
|
|
|
// Find the width and height attributes |
332
|
|
|
$width = $height = false; |
333
|
|
|
|
334
|
|
|
// First, check the image tag |
335
|
|
|
if ( preg_match( '#width=["|\']?([\d%]+)["|\']?#i', $images['img_tag'][ $index ], $width_string ) ) |
336
|
|
|
$width = $width_string[1]; |
337
|
|
|
|
338
|
|
|
if ( preg_match( '#height=["|\']?([\d%]+)["|\']?#i', $images['img_tag'][ $index ], $height_string ) ) |
339
|
|
|
$height = $height_string[1]; |
340
|
|
|
|
341
|
|
|
// Can't pass both a relative width and height, so unset the height in favor of not breaking the horizontal layout. |
342
|
|
|
if ( false !== strpos( $width, '%' ) && false !== strpos( $height, '%' ) ) |
343
|
|
|
$width = $height = false; |
344
|
|
|
|
345
|
|
|
// Detect WP registered image size from HTML class |
346
|
|
|
if ( preg_match( '#class=["|\']?[^"\']*size-([^"\'\s]+)[^"\']*["|\']?#i', $images['img_tag'][ $index ], $size ) ) { |
347
|
|
|
$size = array_pop( $size ); |
348
|
|
|
|
349
|
|
|
if ( false === $width && false === $height && 'full' != $size && array_key_exists( $size, $image_sizes ) ) { |
350
|
|
|
$width = (int) $image_sizes[ $size ]['width']; |
351
|
|
|
$height = (int) $image_sizes[ $size ]['height']; |
352
|
|
|
$transform = $image_sizes[ $size ]['crop'] ? 'resize' : 'fit'; |
353
|
|
|
} |
354
|
|
|
} else { |
355
|
|
|
unset( $size ); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
// WP Attachment ID, if uploaded to this site |
359
|
|
|
if ( |
360
|
|
|
preg_match( '#class=["|\']?[^"\']*wp-image-([\d]+)[^"\']*["|\']?#i', $images['img_tag'][ $index ], $attachment_id ) && |
361
|
|
|
0 === strpos( $src, $upload_dir['baseurl'] ) && |
362
|
|
|
/** |
363
|
|
|
* Filter whether an image using an attachment ID in its class has to be uploaded to the local site to go through Photon. |
364
|
|
|
* |
365
|
|
|
* @module photon |
366
|
|
|
* |
367
|
|
|
* @since 2.0.3 |
368
|
|
|
* |
369
|
|
|
* @param bool false Was the image uploaded to the local site. Default to false. |
370
|
|
|
* @param array $args { |
371
|
|
|
* Array of image details. |
372
|
|
|
* |
373
|
|
|
* @type $src Image URL. |
374
|
|
|
* @type tag Image tag (Image HTML output). |
375
|
|
|
* @type $images Array of information about the image. |
376
|
|
|
* @type $index Image index. |
377
|
|
|
* } |
378
|
|
|
*/ |
379
|
|
|
apply_filters( 'jetpack_photon_image_is_local', false, compact( 'src', 'tag', 'images', 'index' ) ) |
380
|
|
|
) { |
381
|
|
|
$attachment_id = intval( array_pop( $attachment_id ) ); |
382
|
|
|
|
383
|
|
|
if ( $attachment_id ) { |
384
|
|
|
$attachment = get_post( $attachment_id ); |
385
|
|
|
|
386
|
|
|
// Basic check on returned post object |
387
|
|
|
if ( is_object( $attachment ) && ! is_wp_error( $attachment ) && 'attachment' == $attachment->post_type ) { |
388
|
|
|
$src_per_wp = wp_get_attachment_image_src( $attachment_id, isset( $size ) ? $size : 'full' ); |
389
|
|
|
|
390
|
|
|
if ( self::validate_image_url( $src_per_wp[0] ) ) { |
391
|
|
|
$src = $src_per_wp[0]; |
392
|
|
|
$fullsize_url = true; |
393
|
|
|
|
394
|
|
|
// Prevent image distortion if a detected dimension exceeds the image's natural dimensions |
395
|
|
|
if ( ( false !== $width && $width > $src_per_wp[1] ) || ( false !== $height && $height > $src_per_wp[2] ) ) { |
396
|
|
|
$width = false === $width ? false : min( $width, $src_per_wp[1] ); |
397
|
|
|
$height = false === $height ? false : min( $height, $src_per_wp[2] ); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
// If no width and height are found, max out at source image's natural dimensions |
401
|
|
|
// Otherwise, respect registered image sizes' cropping setting |
402
|
|
|
if ( false === $width && false === $height ) { |
403
|
|
|
$width = $src_per_wp[1]; |
404
|
|
|
$height = $src_per_wp[2]; |
405
|
|
|
$transform = 'fit'; |
406
|
|
|
} elseif ( isset( $size ) && array_key_exists( $size, $image_sizes ) && isset( $image_sizes[ $size ]['crop'] ) ) { |
407
|
|
|
$transform = (bool) $image_sizes[ $size ]['crop'] ? 'resize' : 'fit'; |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
} else { |
411
|
|
|
unset( $attachment_id ); |
412
|
|
|
unset( $attachment ); |
413
|
|
|
} |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
// If image tag lacks width and height arguments, try to determine from strings WP appends to resized image filenames. |
418
|
|
|
if ( false === $width && false === $height ) { |
419
|
|
|
list( $width, $height ) = Jetpack_Photon::parse_dimensions_from_filename( $src ); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
$width_orig = $width; |
423
|
|
|
$height_orig = $height; |
424
|
|
|
$transform_orig = $transform; |
425
|
|
|
|
426
|
|
|
// If width is available, constrain to $content_width |
427
|
|
|
if ( false !== $width && false === strpos( $width, '%' ) && is_numeric( $content_width ) ) { |
428
|
|
|
if ( $width > $content_width && false !== $height && false === strpos( $height, '%' ) ) { |
429
|
|
|
$height = round( ( $content_width * $height ) / $width ); |
430
|
|
|
$width = $content_width; |
431
|
|
|
} elseif ( $width > $content_width ) { |
432
|
|
|
$width = $content_width; |
433
|
|
|
} |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
// Set a width if none is found and $content_width is available |
437
|
|
|
// If width is set in this manner and height is available, use `fit` instead of `resize` to prevent skewing |
438
|
|
|
if ( false === $width && is_numeric( $content_width ) ) { |
439
|
|
|
$width = (int) $content_width; |
440
|
|
|
|
441
|
|
|
if ( false !== $height ) |
442
|
|
|
$transform = 'fit'; |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
// Detect if image source is for a custom-cropped thumbnail and prevent further URL manipulation. |
446
|
|
|
if ( ! $fullsize_url && preg_match_all( '#-e[a-z0-9]+(-\d+x\d+)?\.(' . implode('|', self::$extensions ) . '){1}$#i', basename( $src ), $filename ) ) |
447
|
|
|
$fullsize_url = true; |
448
|
|
|
|
449
|
|
|
// Build URL, first maybe removing WP's resized string so we pass the original image to Photon |
450
|
|
|
if ( ! $fullsize_url ) { |
451
|
|
|
$src = self::strip_image_dimensions_maybe( $src ); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
// Build array of Photon args and expose to filter before passing to Photon URL function |
455
|
|
|
$args = array(); |
456
|
|
|
|
457
|
|
|
if ( false !== $width && false !== $height && false === strpos( $width, '%' ) && false === strpos( $height, '%' ) ) |
458
|
|
|
$args[ $transform ] = $width . ',' . $height; |
459
|
|
|
elseif ( false !== $width ) |
460
|
|
|
$args['w'] = $width; |
461
|
|
|
elseif ( false !== $height ) |
462
|
|
|
$args['h'] = $height; |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Filter the array of Photon arguments added to an image when it goes through Photon. |
466
|
|
|
* By default, only includes width and height values. |
467
|
|
|
* @see https://developer.wordpress.com/docs/photon/api/ |
468
|
|
|
* |
469
|
|
|
* @module photon |
470
|
|
|
* |
471
|
|
|
* @since 2.0.0 |
472
|
|
|
* |
473
|
|
|
* @param array $args Array of Photon Arguments. |
474
|
|
|
* @param array $details { |
475
|
|
|
* Array of image details. |
476
|
|
|
* |
477
|
|
|
* @type string $tag Image tag (Image HTML output). |
478
|
|
|
* @type string $src Image URL. |
479
|
|
|
* @type string $src_orig Original Image URL. |
480
|
|
|
* @type int|false $width Image width. |
481
|
|
|
* @type int|false $height Image height. |
482
|
|
|
* @type int|false $width_orig Original image width before constrained by content_width. |
483
|
|
|
* @type int|false $height_orig Original Image height before constrained by content_width. |
484
|
|
|
* @type string $transform Transform. |
485
|
|
|
* @type string $transform_orig Original transform before constrained by content_width. |
486
|
|
|
* } |
487
|
|
|
*/ |
488
|
|
|
$args = apply_filters( 'jetpack_photon_post_image_args', $args, compact( 'tag', 'src', 'src_orig', 'width', 'height', 'width_orig', 'height_orig', 'transform', 'transform_orig' ) ); |
489
|
|
|
|
490
|
|
|
$photon_url = jetpack_photon_url( $src, $args ); |
491
|
|
|
|
492
|
|
|
// Modify image tag if Photon function provides a URL |
493
|
|
|
// 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. |
494
|
|
|
if ( $src != $photon_url ) { |
495
|
|
|
$new_tag = $tag; |
496
|
|
|
|
497
|
|
|
// If present, replace the link href with a Photoned URL for the full-size image. |
498
|
|
|
if ( ! empty( $images['link_url'][ $index ] ) && self::validate_image_url( $images['link_url'][ $index ] ) ) |
499
|
|
|
$new_tag = preg_replace( '#(href=["|\'])' . $images['link_url'][ $index ] . '(["|\'])#i', '\1' . jetpack_photon_url( $images['link_url'][ $index ] ) . '\2', $new_tag, 1 ); |
500
|
|
|
|
501
|
|
|
// Supplant the original source value with our Photon URL |
502
|
|
|
$photon_url = esc_url( $photon_url ); |
503
|
|
|
$new_tag = str_replace( $src_orig, $photon_url, $new_tag ); |
504
|
|
|
|
505
|
|
|
// If Lazy Load is in use, pass placeholder image through Photon |
506
|
|
|
if ( isset( $placeholder_src ) && self::validate_image_url( $placeholder_src ) ) { |
507
|
|
|
$placeholder_src = jetpack_photon_url( $placeholder_src ); |
508
|
|
|
|
509
|
|
|
if ( $placeholder_src != $placeholder_src_orig ) |
510
|
|
|
$new_tag = str_replace( $placeholder_src_orig, esc_url( $placeholder_src ), $new_tag ); |
|
|
|
|
511
|
|
|
|
512
|
|
|
unset( $placeholder_src ); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
// If we are not transforming the image with resize, fit, or letterbox (lb), then we should remove |
516
|
|
|
// the width and height arguments from the image to prevent distortion. Even if $args['w'] and $args['h'] |
517
|
|
|
// are present, Photon does not crop to those dimensions. Instead, it appears to favor height. |
518
|
|
|
// |
519
|
|
|
// If we are transforming the image via one of those methods, let's update the width and height attributes. |
520
|
|
|
if ( empty( $args['resize'] ) && empty( $args['fit'] ) && empty( $args['lb'] ) ) { |
521
|
|
|
$new_tag = preg_replace( '#(?<=\s)(width|height)=["|\']?[\d%]+["|\']?\s?#i', '', $new_tag ); |
522
|
|
|
} else { |
523
|
|
|
$resize_args = isset( $args['resize'] ) ? $args['resize'] : false; |
524
|
|
View Code Duplication |
if ( false == $resize_args ) { |
525
|
|
|
$resize_args = ( ! $resize_args && isset( $args['fit'] ) ) |
526
|
|
|
? $args['fit'] |
527
|
|
|
: false; |
528
|
|
|
} |
529
|
|
View Code Duplication |
if ( false == $resize_args ) { |
530
|
|
|
$resize_args = ( ! $resize_args && isset( $args['lb'] ) ) |
531
|
|
|
? $args['lb'] |
532
|
|
|
: false; |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
$resize_args = array_map( 'trim', explode( ',', $resize_args ) ); |
536
|
|
|
|
537
|
|
|
// (?<=\s) - Ensure width or height attribute is preceded by a space |
538
|
|
|
// (width=["|\']?) - Matches, and captures, width=, width=", or width=' |
539
|
|
|
// [\d%]+ - Matches 1 or more digits |
540
|
|
|
// (["|\']?) - Matches, and captures, ", ', or empty string |
541
|
|
|
// \s - Ensures there's a space after the attribute |
542
|
|
|
$new_tag = preg_replace( '#(?<=\s)(width=["|\']?)[\d%]+(["|\']?)\s?#i', sprintf( '${1}%d${2} ', $resize_args[0] ), $new_tag ); |
543
|
|
|
$new_tag = preg_replace( '#(?<=\s)(height=["|\']?)[\d%]+(["|\']?)\s?#i', sprintf( '${1}%d${2} ', $resize_args[1] ), $new_tag ); |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
// Tag an image for dimension checking |
547
|
|
|
if ( ! self::is_amp_endpoint() ) { |
548
|
|
|
$new_tag = preg_replace( '#(\s?/)?>(\s*</a>)?$#i', ' data-recalc-dims="1"\1>\2', $new_tag ); |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
// Replace original tag with modified version |
552
|
|
|
$content = str_replace( $tag, $new_tag, $content ); |
553
|
|
|
} |
554
|
|
|
} elseif ( preg_match( '#^http(s)?://i[\d]{1}.wp.com#', $src ) && ! empty( $images['link_url'][ $index ] ) && self::validate_image_url( $images['link_url'][ $index ] ) ) { |
555
|
|
|
$new_tag = preg_replace( '#(href=["|\'])' . $images['link_url'][ $index ] . '(["|\'])#i', '\1' . jetpack_photon_url( $images['link_url'][ $index ] ) . '\2', $tag, 1 ); |
556
|
|
|
|
557
|
|
|
$content = str_replace( $tag, $new_tag, $content ); |
558
|
|
|
} |
559
|
|
|
} |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
return $content; |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
public static function filter_the_galleries( $galleries ) { |
566
|
|
|
if ( empty( $galleries ) || ! is_array( $galleries ) ) { |
567
|
|
|
return $galleries; |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
// Pass by reference, so we can modify them in place. |
571
|
|
|
foreach ( $galleries as &$this_gallery ) { |
572
|
|
|
if ( is_string( $this_gallery ) ) { |
573
|
|
|
$this_gallery = self::filter_the_content( $this_gallery ); |
574
|
|
|
// LEAVING COMMENTED OUT as for the moment it doesn't seem |
575
|
|
|
// necessary and I'm not sure how it would propagate through. |
576
|
|
|
// } elseif ( is_array( $this_gallery ) |
577
|
|
|
// && ! empty( $this_gallery['src'] ) |
578
|
|
|
// && ! empty( $this_gallery['type'] ) |
579
|
|
|
// && in_array( $this_gallery['type'], array( 'rectangle', 'square', 'circle' ) ) ) { |
580
|
|
|
// $this_gallery['src'] = array_map( 'jetpack_photon_url', $this_gallery['src'] ); |
581
|
|
|
} |
582
|
|
|
} |
583
|
|
|
unset( $this_gallery ); // break the reference. |
584
|
|
|
|
585
|
|
|
return $galleries; |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* Runs the image widget through photon. |
591
|
|
|
* |
592
|
|
|
* @param array $instance Image widget instance data. |
593
|
|
|
* @return array |
594
|
|
|
*/ |
595
|
|
|
public static function filter_the_image_widget( $instance ) { |
596
|
|
|
if ( Jetpack::is_module_active( 'photon' ) && ! $instance['attachment_id'] && $instance['url'] ) { |
597
|
|
|
jetpack_photon_url( $instance['url'], array( |
598
|
|
|
'w' => $instance['width'], |
599
|
|
|
'h' => $instance['height'], |
600
|
|
|
) ); |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
return $instance; |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
/** |
607
|
|
|
** CORE IMAGE RETRIEVAL |
608
|
|
|
**/ |
609
|
|
|
|
610
|
|
|
/** |
611
|
|
|
* Filter post thumbnail image retrieval, passing images through Photon |
612
|
|
|
* |
613
|
|
|
* @param string|bool $image |
614
|
|
|
* @param int $attachment_id |
615
|
|
|
* @param string|array $size |
616
|
|
|
* @uses is_admin, apply_filters, wp_get_attachment_url, self::validate_image_url, this::image_sizes, jetpack_photon_url |
617
|
|
|
* @filter image_downsize |
618
|
|
|
* @return string|bool |
619
|
|
|
*/ |
620
|
|
|
public function filter_image_downsize( $image, $attachment_id, $size ) { |
621
|
|
|
// Don't foul up the admin side of things, unless a plugin wants to. |
622
|
|
|
if ( is_admin() && |
623
|
|
|
/** |
624
|
|
|
* Provide plugins a way of running Photon for images in the WordPress Dashboard (wp-admin). |
625
|
|
|
* |
626
|
|
|
* Note: enabling this will result in Photon URLs added to your post content, which could make migrations across domains (and off Photon) a bit more challenging. |
627
|
|
|
* |
628
|
|
|
* @module photon |
629
|
|
|
* |
630
|
|
|
* @since 4.8.0 |
631
|
|
|
* |
632
|
|
|
* @param bool false Stop Photon from being run on the Dashboard. Default to false. |
633
|
|
|
* @param array $args { |
634
|
|
|
* Array of image details. |
635
|
|
|
* |
636
|
|
|
* @type $image Image URL. |
637
|
|
|
* @type $attachment_id Attachment ID of the image. |
638
|
|
|
* @type $size Image size. Can be a string (name of the image size, e.g. full) or an array of width and height. |
639
|
|
|
* } |
640
|
|
|
*/ |
641
|
|
|
false === apply_filters( 'jetpack_photon_admin_allow_image_downsize', false, compact( 'image', 'attachment_id', 'size' ) ) |
642
|
|
|
) { |
643
|
|
|
return $image; |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
/** |
647
|
|
|
* Provide plugins a way of preventing Photon from being applied to images retrieved from WordPress Core. |
648
|
|
|
* |
649
|
|
|
* @module photon |
650
|
|
|
* |
651
|
|
|
* @since 2.0.0 |
652
|
|
|
* |
653
|
|
|
* @param bool false Stop Photon from being applied to the image. Default to false. |
654
|
|
|
* @param array $args { |
655
|
|
|
* Array of image details. |
656
|
|
|
* |
657
|
|
|
* @type $image Image URL. |
658
|
|
|
* @type $attachment_id Attachment ID of the image. |
659
|
|
|
* @type $size Image size. Can be a string (name of the image size, e.g. full) or an array of width and height. |
660
|
|
|
* } |
661
|
|
|
*/ |
662
|
|
|
if ( apply_filters( 'jetpack_photon_override_image_downsize', false, compact( 'image', 'attachment_id', 'size' ) ) ) { |
663
|
|
|
return $image; |
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
// Get the image URL and proceed with Photon-ification if successful |
667
|
|
|
$image_url = wp_get_attachment_url( $attachment_id ); |
668
|
|
|
|
669
|
|
|
// Set this to true later when we know we have size meta. |
670
|
|
|
$has_size_meta = false; |
671
|
|
|
|
672
|
|
|
if ( $image_url ) { |
673
|
|
|
// Check if image URL should be used with Photon |
674
|
|
|
if ( ! self::validate_image_url( $image_url ) ) { |
675
|
|
|
return $image; |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
$intermediate = true; // For the fourth array item returned by the image_downsize filter. |
679
|
|
|
|
680
|
|
|
// If an image is requested with a size known to WordPress, use that size's settings with Photon. |
681
|
|
|
// WP states that `add_image_size()` should use a string for the name, but doesn't enforce that. |
682
|
|
|
// Due to differences in how Core and Photon check for the registered image size, we check both types. |
683
|
|
|
if ( ( is_string( $size ) || is_int( $size ) ) && array_key_exists( $size, self::image_sizes() ) ) { |
684
|
|
|
$image_args = self::image_sizes(); |
685
|
|
|
$image_args = $image_args[ $size ]; |
686
|
|
|
|
687
|
|
|
$photon_args = array(); |
688
|
|
|
|
689
|
|
|
$image_meta = image_get_intermediate_size( $attachment_id, $size ); |
690
|
|
|
|
691
|
|
|
// 'full' is a special case: We need consistent data regardless of the requested size. |
692
|
|
|
if ( 'full' == $size ) { |
693
|
|
|
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
694
|
|
|
$intermediate = false; |
695
|
|
|
} elseif ( ! $image_meta ) { |
696
|
|
|
// If we still don't have any image meta at this point, it's probably from a custom thumbnail size |
697
|
|
|
// for an image that was uploaded before the custom image was added to the theme. Try to determine the size manually. |
698
|
|
|
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
699
|
|
|
|
700
|
|
|
if ( isset( $image_meta['width'], $image_meta['height'] ) ) { |
701
|
|
|
$image_resized = image_resize_dimensions( $image_meta['width'], $image_meta['height'], $image_args['width'], $image_args['height'], $image_args['crop'] ); |
702
|
|
|
if ( $image_resized ) { // This could be false when the requested image size is larger than the full-size image. |
703
|
|
|
$image_meta['width'] = $image_resized[6]; |
704
|
|
|
$image_meta['height'] = $image_resized[7]; |
705
|
|
|
} |
706
|
|
|
} |
707
|
|
|
} |
708
|
|
|
|
709
|
|
|
if ( isset( $image_meta['width'], $image_meta['height'] ) ) { |
710
|
|
|
$image_args['width'] = $image_meta['width']; |
711
|
|
|
$image_args['height'] = $image_meta['height']; |
712
|
|
|
|
713
|
|
|
list( $image_args['width'], $image_args['height'] ) = image_constrain_size_for_editor( $image_args['width'], $image_args['height'], $size, 'display' ); |
714
|
|
|
$has_size_meta = true; |
715
|
|
|
} |
716
|
|
|
|
717
|
|
|
// Expose determined arguments to a filter before passing to Photon |
718
|
|
|
$transform = $image_args['crop'] ? 'resize' : 'fit'; |
719
|
|
|
|
720
|
|
|
// Check specified image dimensions and account for possible zero values; photon fails to resize if a dimension is zero. |
721
|
|
|
if ( 0 == $image_args['width'] || 0 == $image_args['height'] ) { |
722
|
|
|
if ( 0 == $image_args['width'] && 0 < $image_args['height'] ) { |
723
|
|
|
$photon_args['h'] = $image_args['height']; |
724
|
|
|
} elseif ( 0 == $image_args['height'] && 0 < $image_args['width'] ) { |
725
|
|
|
$photon_args['w'] = $image_args['width']; |
726
|
|
|
} |
727
|
|
|
} else { |
728
|
|
|
if ( ( 'resize' === $transform ) && $image_meta = wp_get_attachment_metadata( $attachment_id ) ) { |
729
|
|
|
if ( isset( $image_meta['width'], $image_meta['height'] ) ) { |
730
|
|
|
// Lets make sure that we don't upscale images since wp never upscales them as well |
731
|
|
|
$smaller_width = ( ( $image_meta['width'] < $image_args['width'] ) ? $image_meta['width'] : $image_args['width'] ); |
732
|
|
|
$smaller_height = ( ( $image_meta['height'] < $image_args['height'] ) ? $image_meta['height'] : $image_args['height'] ); |
733
|
|
|
|
734
|
|
|
$photon_args[ $transform ] = $smaller_width . ',' . $smaller_height; |
735
|
|
|
} |
736
|
|
|
} else { |
737
|
|
|
$photon_args[ $transform ] = $image_args['width'] . ',' . $image_args['height']; |
738
|
|
|
} |
739
|
|
|
|
740
|
|
|
} |
741
|
|
|
|
742
|
|
|
|
743
|
|
|
/** |
744
|
|
|
* Filter the Photon Arguments added to an image when going through Photon, when that image size is a string. |
745
|
|
|
* Image size will be a string (e.g. "full", "medium") when it is known to WordPress. |
746
|
|
|
* |
747
|
|
|
* @module photon |
748
|
|
|
* |
749
|
|
|
* @since 2.0.0 |
750
|
|
|
* |
751
|
|
|
* @param array $photon_args Array of Photon arguments. |
752
|
|
|
* @param array $args { |
753
|
|
|
* Array of image details. |
754
|
|
|
* |
755
|
|
|
* @type $image_args Array of Image arguments (width, height, crop). |
756
|
|
|
* @type $image_url Image URL. |
757
|
|
|
* @type $attachment_id Attachment ID of the image. |
758
|
|
|
* @type $size Image size. Can be a string (name of the image size, e.g. full) or an integer. |
759
|
|
|
* @type $transform Value can be resize or fit. |
760
|
|
|
* @see https://developer.wordpress.com/docs/photon/api |
761
|
|
|
* } |
762
|
|
|
*/ |
763
|
|
|
$photon_args = apply_filters( 'jetpack_photon_image_downsize_string', $photon_args, compact( 'image_args', 'image_url', 'attachment_id', 'size', 'transform' ) ); |
764
|
|
|
|
765
|
|
|
// Generate Photon URL |
766
|
|
|
$image = array( |
767
|
|
|
jetpack_photon_url( $image_url, $photon_args ), |
768
|
|
|
$has_size_meta ? $image_args['width'] : false, |
769
|
|
|
$has_size_meta ? $image_args['height'] : false, |
770
|
|
|
$intermediate |
771
|
|
|
); |
772
|
|
|
} elseif ( is_array( $size ) ) { |
773
|
|
|
// Pull width and height values from the provided array, if possible |
774
|
|
|
$width = isset( $size[0] ) ? (int) $size[0] : false; |
775
|
|
|
$height = isset( $size[1] ) ? (int) $size[1] : false; |
776
|
|
|
|
777
|
|
|
// Don't bother if necessary parameters aren't passed. |
778
|
|
|
if ( ! $width || ! $height ) { |
|
|
|
|
779
|
|
|
return $image; |
780
|
|
|
} |
781
|
|
|
|
782
|
|
|
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
783
|
|
|
if ( isset( $image_meta['width'], $image_meta['height'] ) ) { |
784
|
|
|
$image_resized = image_resize_dimensions( $image_meta['width'], $image_meta['height'], $width, $height ); |
785
|
|
|
|
786
|
|
|
if ( $image_resized ) { // This could be false when the requested image size is larger than the full-size image. |
787
|
|
|
$width = $image_resized[6]; |
788
|
|
|
$height = $image_resized[7]; |
789
|
|
|
} else { |
790
|
|
|
$width = $image_meta['width']; |
791
|
|
|
$height = $image_meta['height']; |
792
|
|
|
} |
793
|
|
|
|
794
|
|
|
$has_size_meta = true; |
795
|
|
|
} |
796
|
|
|
|
797
|
|
|
list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size ); |
798
|
|
|
|
799
|
|
|
// Expose arguments to a filter before passing to Photon |
800
|
|
|
$photon_args = array( |
801
|
|
|
'fit' => $width . ',' . $height |
802
|
|
|
); |
803
|
|
|
|
804
|
|
|
/** |
805
|
|
|
* Filter the Photon Arguments added to an image when going through Photon, |
806
|
|
|
* when the image size is an array of height and width values. |
807
|
|
|
* |
808
|
|
|
* @module photon |
809
|
|
|
* |
810
|
|
|
* @since 2.0.0 |
811
|
|
|
* |
812
|
|
|
* @param array $photon_args Array of Photon arguments. |
813
|
|
|
* @param array $args { |
814
|
|
|
* Array of image details. |
815
|
|
|
* |
816
|
|
|
* @type $width Image width. |
817
|
|
|
* @type height Image height. |
818
|
|
|
* @type $image_url Image URL. |
819
|
|
|
* @type $attachment_id Attachment ID of the image. |
820
|
|
|
* } |
821
|
|
|
*/ |
822
|
|
|
$photon_args = apply_filters( 'jetpack_photon_image_downsize_array', $photon_args, compact( 'width', 'height', 'image_url', 'attachment_id' ) ); |
823
|
|
|
|
824
|
|
|
// Generate Photon URL |
825
|
|
|
$image = array( |
826
|
|
|
jetpack_photon_url( $image_url, $photon_args ), |
827
|
|
|
$has_size_meta ? $width : false, |
828
|
|
|
$has_size_meta ? $height : false, |
829
|
|
|
$intermediate |
830
|
|
|
); |
831
|
|
|
} |
832
|
|
|
} |
833
|
|
|
|
834
|
|
|
return $image; |
835
|
|
|
} |
836
|
|
|
|
837
|
|
|
/** |
838
|
|
|
* Filters an array of image `srcset` values, replacing each URL with its Photon equivalent. |
839
|
|
|
* |
840
|
|
|
* @since 3.8.0 |
841
|
|
|
* @since 4.0.4 Added automatically additional sizes beyond declared image sizes. |
842
|
|
|
* @param array $sources An array of image urls and widths. |
843
|
|
|
* @uses self::validate_image_url, jetpack_photon_url, Jetpack_Photon::parse_from_filename |
844
|
|
|
* @uses Jetpack_Photon::strip_image_dimensions_maybe, Jetpack::get_content_width |
845
|
|
|
* @return array An array of Photon image urls and widths. |
846
|
|
|
*/ |
847
|
|
|
public function filter_srcset_array( $sources = array(), $size_array = array(), $image_src = array(), $image_meta = array(), $attachment_id = 0 ) { |
848
|
|
|
if ( ! is_array( $sources ) ) { |
849
|
|
|
return $sources; |
850
|
|
|
} |
851
|
|
|
$upload_dir = wp_get_upload_dir(); |
852
|
|
|
|
853
|
|
|
foreach ( $sources as $i => $source ) { |
854
|
|
|
if ( ! self::validate_image_url( $source['url'] ) ) { |
855
|
|
|
continue; |
856
|
|
|
} |
857
|
|
|
|
858
|
|
|
/** This filter is already documented in class.photon.php */ |
859
|
|
|
if ( apply_filters( 'jetpack_photon_skip_image', false, $source['url'], $source ) ) { |
860
|
|
|
continue; |
861
|
|
|
} |
862
|
|
|
|
863
|
|
|
$url = $source['url']; |
864
|
|
|
list( $width, $height ) = Jetpack_Photon::parse_dimensions_from_filename( $url ); |
865
|
|
|
|
866
|
|
|
// It's quicker to get the full size with the data we have already, if available |
867
|
|
|
if ( ! empty( $attachment_id ) ) { |
868
|
|
|
$url = wp_get_attachment_url( $attachment_id ); |
869
|
|
|
} else { |
870
|
|
|
$url = Jetpack_Photon::strip_image_dimensions_maybe( $url ); |
871
|
|
|
} |
872
|
|
|
|
873
|
|
|
$args = array(); |
874
|
|
|
if ( 'w' === $source['descriptor'] ) { |
875
|
|
|
if ( $height && ( $source['value'] == $width ) ) { |
876
|
|
|
$args['resize'] = $width . ',' . $height; |
877
|
|
|
} else { |
878
|
|
|
$args['w'] = $source['value']; |
879
|
|
|
} |
880
|
|
|
|
881
|
|
|
} |
882
|
|
|
|
883
|
|
|
$sources[ $i ]['url'] = jetpack_photon_url( $url, $args ); |
884
|
|
|
} |
885
|
|
|
|
886
|
|
|
/** |
887
|
|
|
* At this point, $sources is the original srcset with Photonized URLs. |
888
|
|
|
* Now, we're going to construct additional sizes based on multiples of the content_width. |
889
|
|
|
* This will reduce the gap between the largest defined size and the original image. |
890
|
|
|
*/ |
891
|
|
|
|
892
|
|
|
/** |
893
|
|
|
* Filter the multiplier Photon uses to create new srcset items. |
894
|
|
|
* Return false to short-circuit and bypass auto-generation. |
895
|
|
|
* |
896
|
|
|
* @module photon |
897
|
|
|
* |
898
|
|
|
* @since 4.0.4 |
899
|
|
|
* |
900
|
|
|
* @param array|bool $multipliers Array of multipliers to use or false to bypass. |
901
|
|
|
*/ |
902
|
|
|
$multipliers = apply_filters( 'jetpack_photon_srcset_multipliers', array( 2, 3 ) ); |
903
|
|
|
$url = trailingslashit( $upload_dir['baseurl'] ) . $image_meta['file']; |
904
|
|
|
|
905
|
|
|
if ( |
906
|
|
|
/** Short-circuit via jetpack_photon_srcset_multipliers filter. */ |
907
|
|
|
is_array( $multipliers ) |
908
|
|
|
/** This filter is already documented in class.photon.php */ |
909
|
|
|
&& ! apply_filters( 'jetpack_photon_skip_image', false, $url, null ) |
910
|
|
|
/** Verify basic meta is intact. */ |
911
|
|
|
&& isset( $image_meta['width'] ) && isset( $image_meta['height'] ) && isset( $image_meta['file'] ) |
912
|
|
|
/** Verify we have the requested width/height. */ |
913
|
|
|
&& isset( $size_array[0] ) && isset( $size_array[1] ) |
914
|
|
|
) { |
915
|
|
|
|
916
|
|
|
$fullwidth = $image_meta['width']; |
917
|
|
|
$fullheight = $image_meta['height']; |
918
|
|
|
$reqwidth = $size_array[0]; |
919
|
|
|
$reqheight = $size_array[1]; |
920
|
|
|
|
921
|
|
|
$constrained_size = wp_constrain_dimensions( $fullwidth, $fullheight, $reqwidth ); |
922
|
|
|
$expected_size = array( $reqwidth, $reqheight ); |
923
|
|
|
|
924
|
|
|
if ( abs( $constrained_size[0] - $expected_size[0] ) <= 1 && abs( $constrained_size[1] - $expected_size[1] ) <= 1 ) { |
925
|
|
|
$crop = 'soft'; |
926
|
|
|
$base = Jetpack::get_content_width() ? Jetpack::get_content_width() : 1000; // Provide a default width if none set by the theme. |
927
|
|
|
} else { |
928
|
|
|
$crop = 'hard'; |
929
|
|
|
$base = $reqwidth; |
930
|
|
|
} |
931
|
|
|
|
932
|
|
|
|
933
|
|
|
$currentwidths = array_keys( $sources ); |
934
|
|
|
$newsources = null; |
935
|
|
|
|
936
|
|
|
foreach ( $multipliers as $multiplier ) { |
937
|
|
|
|
938
|
|
|
$newwidth = $base * $multiplier; |
939
|
|
|
foreach ( $currentwidths as $currentwidth ){ |
940
|
|
|
// If a new width would be within 100 pixes of an existing one or larger than the full size image, skip. |
941
|
|
|
if ( abs( $currentwidth - $newwidth ) < 50 || ( $newwidth > $fullwidth ) ) { |
942
|
|
|
continue 2; // Back to the foreach ( $multipliers as $multiplier ) |
943
|
|
|
} |
944
|
|
|
} // foreach ( $currentwidths as $currentwidth ){ |
945
|
|
|
|
946
|
|
|
if ( 'soft' == $crop ) { |
947
|
|
|
$args = array( |
948
|
|
|
'w' => $newwidth, |
949
|
|
|
); |
950
|
|
|
} else { // hard crop, e.g. add_image_size( 'example', 200, 200, true ); |
951
|
|
|
$args = array( |
952
|
|
|
'zoom' => $multiplier, |
953
|
|
|
'resize' => $reqwidth . ',' . $reqheight, |
954
|
|
|
); |
955
|
|
|
} |
956
|
|
|
|
957
|
|
|
$newsources[ $newwidth ] = array( |
958
|
|
|
'url' => jetpack_photon_url( $url, $args ), |
959
|
|
|
'descriptor' => 'w', |
960
|
|
|
'value' => $newwidth, |
961
|
|
|
); |
962
|
|
|
} // foreach ( $multipliers as $multiplier ) |
963
|
|
|
if ( is_array( $newsources ) ) { |
964
|
|
|
if ( function_exists( 'array_replace' ) ) { // PHP 5.3+, preferred |
965
|
|
|
// phpcs:disable |
966
|
|
|
$sources = array_replace( $sources, $newsources ); |
967
|
|
|
// phpcs:enable |
968
|
|
|
} else { // For PHP 5.2 using WP shim function |
969
|
|
|
$sources = array_replace_recursive( $sources, $newsources ); |
970
|
|
|
} |
971
|
|
|
} |
972
|
|
|
} // if ( isset( $image_meta['width'] ) && isset( $image_meta['file'] ) ) |
973
|
|
|
|
974
|
|
|
return $sources; |
975
|
|
|
} |
976
|
|
|
|
977
|
|
|
/** |
978
|
|
|
* Filters an array of image `sizes` values, using $content_width instead of image's full size. |
979
|
|
|
* |
980
|
|
|
* @since 4.0.4 |
981
|
|
|
* @since 4.1.0 Returns early for images not within the_content. |
982
|
|
|
* @param array $sizes An array of media query breakpoints. |
983
|
|
|
* @param array $size Width and height of the image |
984
|
|
|
* @uses Jetpack::get_content_width |
985
|
|
|
* @return array An array of media query breakpoints. |
986
|
|
|
*/ |
987
|
|
|
public function filter_sizes( $sizes, $size ) { |
988
|
|
|
if ( ! doing_filter( 'the_content' ) ){ |
989
|
|
|
return $sizes; |
990
|
|
|
} |
991
|
|
|
$content_width = Jetpack::get_content_width(); |
992
|
|
|
if ( ! $content_width ) { |
993
|
|
|
$content_width = 1000; |
994
|
|
|
} |
995
|
|
|
|
996
|
|
|
if ( ( is_array( $size ) && $size[0] < $content_width ) ) { |
997
|
|
|
return $sizes; |
998
|
|
|
} |
999
|
|
|
|
1000
|
|
|
return sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $content_width ); |
1001
|
|
|
} |
1002
|
|
|
|
1003
|
|
|
/** |
1004
|
|
|
** GENERAL FUNCTIONS |
1005
|
|
|
**/ |
1006
|
|
|
|
1007
|
|
|
/** |
1008
|
|
|
* Ensure image URL is valid for Photon. |
1009
|
|
|
* 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. |
1010
|
|
|
* |
1011
|
|
|
* @param string $url |
1012
|
|
|
* @uses wp_parse_args |
1013
|
|
|
* @return bool |
1014
|
|
|
*/ |
1015
|
|
|
protected static function validate_image_url( $url ) { |
1016
|
|
|
$parsed_url = @parse_url( $url ); |
1017
|
|
|
|
1018
|
|
|
if ( ! $parsed_url ) |
1019
|
|
|
return false; |
1020
|
|
|
|
1021
|
|
|
// Parse URL and ensure needed keys exist, since the array returned by `parse_url` only includes the URL components it finds. |
1022
|
|
|
$url_info = wp_parse_args( $parsed_url, array( |
1023
|
|
|
'scheme' => null, |
1024
|
|
|
'host' => null, |
1025
|
|
|
'port' => null, |
1026
|
|
|
'path' => null |
1027
|
|
|
) ); |
1028
|
|
|
|
1029
|
|
|
// Bail if scheme isn't http or port is set that isn't port 80 |
1030
|
|
|
if ( |
1031
|
|
|
( 'http' != $url_info['scheme'] || ! in_array( $url_info['port'], array( 80, null ) ) ) && |
1032
|
|
|
/** |
1033
|
|
|
* Allow Photon to fetch images that are served via HTTPS. |
1034
|
|
|
* |
1035
|
|
|
* @module photon |
1036
|
|
|
* |
1037
|
|
|
* @since 2.4.0 |
1038
|
|
|
* @since 3.9.0 Default to false. |
1039
|
|
|
* |
1040
|
|
|
* @param bool $reject_https Should Photon ignore images using the HTTPS scheme. Default to false. |
1041
|
|
|
*/ |
1042
|
|
|
apply_filters( 'jetpack_photon_reject_https', false ) |
1043
|
|
|
) { |
1044
|
|
|
return false; |
1045
|
|
|
} |
1046
|
|
|
|
1047
|
|
|
// Bail if no host is found |
1048
|
|
|
if ( is_null( $url_info['host'] ) ) |
1049
|
|
|
return false; |
1050
|
|
|
|
1051
|
|
|
// Bail if the image alredy went through Photon |
1052
|
|
|
if ( preg_match( '#^i[\d]{1}.wp.com$#i', $url_info['host'] ) ) |
1053
|
|
|
return false; |
1054
|
|
|
|
1055
|
|
|
// Bail if no path is found |
1056
|
|
|
if ( is_null( $url_info['path'] ) ) |
1057
|
|
|
return false; |
1058
|
|
|
|
1059
|
|
|
// Ensure image extension is acceptable |
1060
|
|
|
if ( ! in_array( strtolower( pathinfo( $url_info['path'], PATHINFO_EXTENSION ) ), self::$extensions ) ) |
1061
|
|
|
return false; |
1062
|
|
|
|
1063
|
|
|
// If we got this far, we should have an acceptable image URL |
1064
|
|
|
// But let folks filter to decline if they prefer. |
1065
|
|
|
/** |
1066
|
|
|
* Overwrite the results of the validation steps an image goes through before to be considered valid to be used by Photon. |
1067
|
|
|
* |
1068
|
|
|
* @module photon |
1069
|
|
|
* |
1070
|
|
|
* @since 3.0.0 |
1071
|
|
|
* |
1072
|
|
|
* @param bool true Is the image URL valid and can it be used by Photon. Default to true. |
1073
|
|
|
* @param string $url Image URL. |
1074
|
|
|
* @param array $parsed_url Array of information about the image. |
1075
|
|
|
*/ |
1076
|
|
|
return apply_filters( 'photon_validate_image_url', true, $url, $parsed_url ); |
1077
|
|
|
} |
1078
|
|
|
|
1079
|
|
|
/** |
1080
|
|
|
* Checks if the file exists before it passes the file to photon |
1081
|
|
|
* |
1082
|
|
|
* @param string $src The image URL |
1083
|
|
|
* @return string |
1084
|
|
|
**/ |
1085
|
|
|
public static function strip_image_dimensions_maybe( $src ){ |
1086
|
|
|
$stripped_src = $src; |
|
|
|
|
1087
|
|
|
|
1088
|
|
|
// Build URL, first removing WP's resized string so we pass the original image to Photon |
1089
|
|
|
if ( preg_match( '#(-\d+x\d+)\.(' . implode('|', self::$extensions ) . '){1}$#i', $src, $src_parts ) ) { |
1090
|
|
|
$stripped_src = str_replace( $src_parts[1], '', $src ); |
1091
|
|
|
$upload_dir = wp_get_upload_dir(); |
1092
|
|
|
|
1093
|
|
|
// Extracts the file path to the image minus the base url |
1094
|
|
|
$file_path = substr( $stripped_src, strlen ( $upload_dir['baseurl'] ) ); |
1095
|
|
|
|
1096
|
|
|
if( file_exists( $upload_dir["basedir"] . $file_path ) ) |
1097
|
|
|
$src = $stripped_src; |
1098
|
|
|
} |
1099
|
|
|
|
1100
|
|
|
return $src; |
1101
|
|
|
} |
1102
|
|
|
|
1103
|
|
|
/** |
1104
|
|
|
* Provide an array of available image sizes and corresponding dimensions. |
1105
|
|
|
* Similar to get_intermediate_image_sizes() except that it includes image sizes' dimensions, not just their names. |
1106
|
|
|
* |
1107
|
|
|
* @global $wp_additional_image_sizes |
1108
|
|
|
* @uses get_option |
1109
|
|
|
* @return array |
1110
|
|
|
*/ |
1111
|
|
|
protected static function image_sizes() { |
1112
|
|
|
if ( null == self::$image_sizes ) { |
1113
|
|
|
global $_wp_additional_image_sizes; |
1114
|
|
|
|
1115
|
|
|
// Populate an array matching the data structure of $_wp_additional_image_sizes so we have a consistent structure for image sizes |
1116
|
|
|
$images = array( |
1117
|
|
|
'thumb' => array( |
1118
|
|
|
'width' => intval( get_option( 'thumbnail_size_w' ) ), |
1119
|
|
|
'height' => intval( get_option( 'thumbnail_size_h' ) ), |
1120
|
|
|
'crop' => (bool) get_option( 'thumbnail_crop' ) |
1121
|
|
|
), |
1122
|
|
|
'medium' => array( |
1123
|
|
|
'width' => intval( get_option( 'medium_size_w' ) ), |
1124
|
|
|
'height' => intval( get_option( 'medium_size_h' ) ), |
1125
|
|
|
'crop' => false |
1126
|
|
|
), |
1127
|
|
|
'large' => array( |
1128
|
|
|
'width' => intval( get_option( 'large_size_w' ) ), |
1129
|
|
|
'height' => intval( get_option( 'large_size_h' ) ), |
1130
|
|
|
'crop' => false |
1131
|
|
|
), |
1132
|
|
|
'full' => array( |
1133
|
|
|
'width' => null, |
1134
|
|
|
'height' => null, |
1135
|
|
|
'crop' => false |
1136
|
|
|
) |
1137
|
|
|
); |
1138
|
|
|
|
1139
|
|
|
// Compatibility mapping as found in wp-includes/media.php |
1140
|
|
|
$images['thumbnail'] = $images['thumb']; |
1141
|
|
|
|
1142
|
|
|
// Update class variable, merging in $_wp_additional_image_sizes if any are set |
1143
|
|
|
if ( is_array( $_wp_additional_image_sizes ) && ! empty( $_wp_additional_image_sizes ) ) |
1144
|
|
|
self::$image_sizes = array_merge( $images, $_wp_additional_image_sizes ); |
1145
|
|
|
else |
1146
|
|
|
self::$image_sizes = $images; |
1147
|
|
|
} |
1148
|
|
|
|
1149
|
|
|
return is_array( self::$image_sizes ) ? self::$image_sizes : array(); |
1150
|
|
|
} |
1151
|
|
|
|
1152
|
|
|
/** |
1153
|
|
|
* Pass og:image URLs through Photon |
1154
|
|
|
* |
1155
|
|
|
* @param array $tags |
1156
|
|
|
* @param array $parameters |
1157
|
|
|
* @uses jetpack_photon_url |
1158
|
|
|
* @return array |
1159
|
|
|
*/ |
1160
|
|
|
function filter_open_graph_tags( $tags, $parameters ) { |
1161
|
|
|
if ( empty( $tags['og:image'] ) ) { |
1162
|
|
|
return $tags; |
1163
|
|
|
} |
1164
|
|
|
|
1165
|
|
|
$photon_args = array( |
1166
|
|
|
'fit' => sprintf( '%d,%d', 2 * $parameters['image_width'], 2 * $parameters['image_height'] ), |
1167
|
|
|
); |
1168
|
|
|
|
1169
|
|
|
if ( is_array( $tags['og:image'] ) ) { |
1170
|
|
|
$images = array(); |
1171
|
|
|
foreach ( $tags['og:image'] as $image ) { |
1172
|
|
|
$images[] = jetpack_photon_url( $image, $photon_args ); |
1173
|
|
|
} |
1174
|
|
|
$tags['og:image'] = $images; |
1175
|
|
|
} else { |
1176
|
|
|
$tags['og:image'] = jetpack_photon_url( $tags['og:image'], $photon_args ); |
1177
|
|
|
} |
1178
|
|
|
|
1179
|
|
|
return $tags; |
1180
|
|
|
} |
1181
|
|
|
|
1182
|
|
|
public function noresize_intermediate_sizes( $sizes ) { |
1183
|
|
|
return __return_empty_array(); |
1184
|
|
|
} |
1185
|
|
|
|
1186
|
|
|
/** |
1187
|
|
|
* Enqueue Photon helper script |
1188
|
|
|
* |
1189
|
|
|
* @uses wp_enqueue_script, plugins_url |
1190
|
|
|
* @action wp_enqueue_script |
1191
|
|
|
* @return null |
1192
|
|
|
*/ |
1193
|
|
|
public function action_wp_enqueue_scripts() { |
1194
|
|
|
if ( self::is_amp_endpoint() ) { |
1195
|
|
|
return; |
1196
|
|
|
} |
1197
|
|
|
wp_enqueue_script( |
1198
|
|
|
'jetpack-photon', |
1199
|
|
|
Assets::get_file_url_for_environment( |
1200
|
|
|
'_inc/build/photon/photon.min.js', |
1201
|
|
|
'modules/photon/photon.js' |
1202
|
|
|
), |
1203
|
|
|
array(), |
1204
|
|
|
20190201, |
1205
|
|
|
true |
1206
|
|
|
); |
1207
|
|
|
} |
1208
|
|
|
|
1209
|
|
|
/** |
1210
|
|
|
* Determine if image_downsize should utilize Photon via REST API. |
1211
|
|
|
* |
1212
|
|
|
* The WordPress Block Editor (Gutenberg) and other REST API consumers using the wp/v2/media endpoint, especially in the "edit" |
1213
|
|
|
* context is more akin to the is_admin usage of Photon (see filter_image_downsize). Since consumers are trying to edit content in posts, |
1214
|
|
|
* Photon should not fire as it will fire later on display. By aborting an attempt to Photonize an image here, we |
1215
|
|
|
* prevents issues like https://github.com/Automattic/jetpack/issues/10580 . |
1216
|
|
|
* |
1217
|
|
|
* To determine if we're using the wp/v2/media endpoint, we hook onto the `rest_request_before_callbacks` filter and |
1218
|
|
|
* if determined we are using it in the edit context, we'll false out the `jetpack_photon_override_image_downsize` filter. |
1219
|
|
|
* |
1220
|
|
|
* @see Jetpack_Photon::filter_image_downsize() |
1221
|
|
|
* |
1222
|
|
|
* @param null|WP_Error $response |
1223
|
|
|
* @param array $endpoint_data |
1224
|
|
|
* @param WP_REST_Request $request Request used to generate the response. |
1225
|
|
|
* |
1226
|
|
|
* @return null|WP_Error The original response object without modification. |
1227
|
|
|
*/ |
1228
|
|
|
public function should_rest_photon_image_downsize( $response, $endpoint_data, $request ) { |
1229
|
|
|
if ( ! is_a( $request , 'WP_REST_Request' ) ) { |
1230
|
|
|
return $response; // Something odd is happening. Do nothing and return the response. |
1231
|
|
|
} |
1232
|
|
|
|
1233
|
|
|
if ( is_wp_error( $response ) ) { |
1234
|
|
|
// If we're going to return an error, we don't need to do anything with Photon. |
1235
|
|
|
return $response; |
1236
|
|
|
} |
1237
|
|
|
|
1238
|
|
|
$route = $request->get_route(); |
1239
|
|
|
|
1240
|
|
|
if ( false !== strpos( $route, 'wp/v2/media' ) && 'edit' === $request['context'] ) { |
1241
|
|
|
// Don't use `__return_true()`: Use something unique. See ::_override_image_downsize_in_rest_edit_context() |
1242
|
|
|
// Late execution to avoid conflict with other plugins as we really don't want to run in this situation. |
1243
|
|
|
add_filter( 'jetpack_photon_override_image_downsize', array( $this, '_override_image_downsize_in_rest_edit_context' ), 999999 ); |
1244
|
|
|
} |
1245
|
|
|
|
1246
|
|
|
return $response; |
1247
|
|
|
|
1248
|
|
|
} |
1249
|
|
|
|
1250
|
|
|
/** |
1251
|
|
|
* Remove the override we may have added in ::should_rest_photon_image_downsize() |
1252
|
|
|
* Since ::_override_image_downsize_in_rest_edit_context() is only |
1253
|
|
|
* every used here, we can always remove it without ever worrying |
1254
|
|
|
* about breaking any other configuration. |
1255
|
|
|
* |
1256
|
|
|
* @param mixed $response |
1257
|
|
|
* @return mixed Unchanged $response |
1258
|
|
|
*/ |
1259
|
|
|
public function cleanup_rest_photon_image_downsize( $response ) { |
1260
|
|
|
remove_filter( 'jetpack_photon_override_image_downsize', array( $this, '_override_image_downsize_in_rest_edit_context' ), 999999 ); |
1261
|
|
|
return $response; |
1262
|
|
|
} |
1263
|
|
|
|
1264
|
|
|
/** |
1265
|
|
|
* Used internally by ::should_rest_photon_image_downsize() to not photonize |
1266
|
|
|
* image URLs in ?context=edit REST requests. |
1267
|
|
|
* MUST NOT be used anywhere else. |
1268
|
|
|
* We use a unique function instead of __return_true so that we can clean up |
1269
|
|
|
* after ourselves without breaking anyone else's filters. |
1270
|
|
|
* |
1271
|
|
|
* @internal |
1272
|
|
|
* @return true |
1273
|
|
|
*/ |
1274
|
|
|
public function _override_image_downsize_in_rest_edit_context() { |
1275
|
|
|
return true; |
1276
|
|
|
} |
1277
|
|
|
|
1278
|
|
|
/** |
1279
|
|
|
* Return whether the current page is AMP. |
1280
|
|
|
* |
1281
|
|
|
* This is only present for the sake of WordPress.com where the Jetpack_AMP_Support |
1282
|
|
|
* class does not yet exist. This mehod may only be called at the wp action or later. |
1283
|
|
|
* |
1284
|
|
|
* @return bool Whether AMP page. |
1285
|
|
|
*/ |
1286
|
|
|
private static function is_amp_endpoint() { |
1287
|
|
|
return class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request(); |
1288
|
|
|
} |
1289
|
|
|
} |
1290
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.