Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack_Photon often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Photon, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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 ) { |
||
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 | } |
||
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 ) { |
||
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 ) { |
||
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 ) { |
||
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 ) { |
||
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 ){ |
||
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() { |
||
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 ) { |
||
1181 | |||
1182 | public function noresize_intermediate_sizes( $sizes ) { |
||
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() { |
||
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 ) { |
||
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 ) { |
||
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() { |
||
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() { |
||
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.