These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Useful for finding an image to display alongside/in representation of a specific post. |
||
5 | * |
||
6 | * Includes a few different methods, all of which return a similar-format array containing |
||
7 | * details of any images found. Everything can (should) be called statically, it's just a |
||
8 | * function-bucket. You can also call Jetpack_PostImages::get_image() to cycle through all of the methods until |
||
9 | * one of them finds something useful. |
||
10 | * |
||
11 | * This file is included verbatim in Jetpack |
||
12 | */ |
||
13 | class Jetpack_PostImages { |
||
14 | /** |
||
15 | * If a slideshow is embedded within a post, then parse out the images involved and return them |
||
16 | */ |
||
17 | static function from_slideshow( $post_id, $width = 200, $height = 200 ) { |
||
18 | $images = array(); |
||
19 | |||
20 | $post = get_post( $post_id ); |
||
21 | |||
22 | if ( ! $post ) { |
||
23 | return $images; |
||
24 | } |
||
25 | |||
26 | if ( ! empty( $post->post_password ) ) { |
||
27 | return $images; |
||
28 | } |
||
29 | |||
30 | if ( false === has_shortcode( $post->post_content, 'slideshow' ) ) { |
||
31 | return $images; // no slideshow - bail |
||
32 | } |
||
33 | |||
34 | $permalink = get_permalink( $post->ID ); |
||
35 | |||
36 | // Mechanic: Somebody set us up the bomb |
||
37 | $old_post = $GLOBALS['post']; |
||
38 | $GLOBALS['post'] = $post; |
||
39 | $old_shortcodes = $GLOBALS['shortcode_tags']; |
||
40 | $GLOBALS['shortcode_tags'] = array( 'slideshow' => $old_shortcodes['slideshow'] ); |
||
41 | |||
42 | // Find all the slideshows |
||
43 | preg_match_all( '/' . get_shortcode_regex() . '/sx', $post->post_content, $slideshow_matches, PREG_SET_ORDER ); |
||
44 | |||
45 | ob_start(); // The slideshow shortcode handler calls wp_print_scripts and wp_print_styles... not too happy about that |
||
46 | |||
47 | foreach ( $slideshow_matches as $slideshow_match ) { |
||
48 | $slideshow = do_shortcode_tag( $slideshow_match ); |
||
49 | if ( false === $pos = stripos( $slideshow, 'jetpack-slideshow' ) ) // must be something wrong - or we changed the output format in which case none of the following will work |
||
50 | continue; |
||
51 | $start = strpos( $slideshow, '[', $pos ); |
||
52 | $end = strpos( $slideshow, ']', $start ); |
||
53 | $post_images = json_decode( wp_specialchars_decode( str_replace( "'", '"', substr( $slideshow, $start, $end - $start + 1 ) ), ENT_QUOTES ) ); // parse via JSON |
||
54 | foreach ( $post_images as $post_image ) { |
||
55 | if ( !$post_image_id = absint( $post_image->id ) ) |
||
56 | continue; |
||
57 | |||
58 | $meta = wp_get_attachment_metadata( $post_image_id ); |
||
59 | |||
60 | // Must be larger than 200x200 (or user-specified) |
||
61 | if ( !isset( $meta['width'] ) || $meta['width'] < $width ) |
||
62 | continue; |
||
63 | if ( !isset( $meta['height'] ) || $meta['height'] < $height ) |
||
64 | continue; |
||
65 | |||
66 | $url = wp_get_attachment_url( $post_image_id ); |
||
67 | |||
68 | $images[] = array( |
||
69 | 'type' => 'image', |
||
70 | 'from' => 'slideshow', |
||
71 | 'src' => $url, |
||
72 | 'src_width' => $meta['width'], |
||
73 | 'src_height' => $meta['height'], |
||
74 | 'href' => $permalink, |
||
75 | ); |
||
76 | } |
||
77 | } |
||
78 | ob_end_clean(); |
||
79 | |||
80 | // Operator: Main screen turn on |
||
81 | $GLOBALS['shortcode_tags'] = $old_shortcodes; |
||
82 | $GLOBALS['post'] = $old_post; |
||
83 | |||
84 | return $images; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * If a gallery is detected, then get all the images from it. |
||
89 | */ |
||
90 | static function from_gallery( $post_id ) { |
||
91 | $images = array(); |
||
92 | |||
93 | $post = get_post( $post_id ); |
||
94 | |||
95 | if ( ! $post ) { |
||
96 | return $images; |
||
97 | } |
||
98 | |||
99 | if ( ! empty( $post->post_password ) ) { |
||
100 | return $images; |
||
101 | } |
||
102 | |||
103 | $permalink = get_permalink( $post->ID ); |
||
104 | |||
105 | /** |
||
106 | * Juggle global post object because the gallery shortcode uses the |
||
107 | * global object. |
||
108 | * |
||
109 | * See core ticket: |
||
110 | * https://core.trac.wordpress.org/ticket/39304 |
||
111 | */ |
||
112 | if ( isset( $GLOBALS['post'] ) ) { |
||
113 | $juggle_post = $GLOBALS['post']; |
||
114 | $GLOBALS['post'] = $post; |
||
115 | $galleries = get_post_galleries( $post->ID, false ); |
||
116 | $GLOBALS['post'] = $juggle_post; |
||
117 | } else { |
||
118 | $GLOBALS['post'] = $post; |
||
119 | $galleries = get_post_galleries( $post->ID, false ); |
||
120 | unset( $GLOBALS['post'] ); |
||
121 | } |
||
122 | |||
123 | foreach ( $galleries as $gallery ) { |
||
124 | if ( isset( $gallery['type'] ) && 'slideshow' === $gallery['type'] && ! empty( $gallery['ids'] ) ) { |
||
125 | $image_ids = explode( ',', $gallery['ids'] ); |
||
126 | $image_size = isset( $gallery['size'] ) ? $gallery['size'] : 'thumbnail'; |
||
127 | foreach ( $image_ids as $image_id ) { |
||
128 | $image = wp_get_attachment_image_src( $image_id, $image_size ); |
||
129 | View Code Duplication | if ( ! empty( $image[0] ) ) { |
|
130 | list( $raw_src ) = explode( '?', $image[0] ); // pull off any Query string (?w=250) |
||
131 | $raw_src = wp_specialchars_decode( $raw_src ); // rawify it |
||
132 | $raw_src = esc_url_raw( $raw_src ); // clean it |
||
133 | $images[] = array( |
||
134 | 'type' => 'image', |
||
135 | 'from' => 'gallery', |
||
136 | 'src' => $raw_src, |
||
137 | 'href' => $permalink, |
||
138 | ); |
||
139 | } |
||
140 | } |
||
141 | View Code Duplication | } elseif ( ! empty( $gallery['src'] ) ) { |
|
142 | foreach ( $gallery['src'] as $src ) { |
||
143 | list( $raw_src ) = explode( '?', $src ); // pull off any Query string (?w=250) |
||
144 | $raw_src = wp_specialchars_decode( $raw_src ); // rawify it |
||
145 | $raw_src = esc_url_raw( $raw_src ); // clean it |
||
146 | $images[] = array( |
||
147 | 'type' => 'image', |
||
148 | 'from' => 'gallery', |
||
149 | 'src' => $raw_src, |
||
150 | 'href' => $permalink, |
||
151 | ); |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | |||
156 | return $images; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Get attachment images for a specified post and return them. Also make sure |
||
161 | * their dimensions are at or above a required minimum. |
||
162 | */ |
||
163 | static function from_attachment( $post_id, $width = 200, $height = 200 ) { |
||
164 | $images = array(); |
||
165 | |||
166 | $post = get_post( $post_id ); |
||
167 | |||
168 | if ( ! empty( $post->post_password ) ) { |
||
169 | return $images; |
||
170 | } |
||
171 | |||
172 | $post_images = get_posts( array( |
||
173 | 'post_parent' => $post_id, // Must be children of post |
||
174 | 'numberposts' => 5, // No more than 5 |
||
175 | 'post_type' => 'attachment', // Must be attachments |
||
176 | 'post_mime_type' => 'image', // Must be images |
||
177 | ) ); |
||
178 | |||
179 | if ( ! $post_images ) { |
||
180 | return $images; |
||
181 | } |
||
182 | |||
183 | $permalink = get_permalink( $post_id ); |
||
184 | |||
185 | foreach ( $post_images as $post_image ) { |
||
186 | $meta = wp_get_attachment_metadata( $post_image->ID ); |
||
187 | // Must be larger than 200x200 |
||
188 | if ( !isset( $meta['width'] ) || $meta['width'] < $width ) |
||
189 | continue; |
||
190 | if ( !isset( $meta['height'] ) || $meta['height'] < $height ) |
||
191 | continue; |
||
192 | |||
193 | $url = wp_get_attachment_url( $post_image->ID ); |
||
194 | |||
195 | $images[] = array( |
||
196 | 'type' => 'image', |
||
197 | 'from' => 'attachment', |
||
198 | 'src' => $url, |
||
199 | 'src_width' => $meta['width'], |
||
200 | 'src_height' => $meta['height'], |
||
201 | 'href' => $permalink, |
||
202 | ); |
||
203 | } |
||
204 | |||
205 | /* |
||
206 | * We only want to pass back attached images that were actually inserted. |
||
207 | * We can load up all the images found in the HTML source and then |
||
208 | * compare URLs to see if an image is attached AND inserted. |
||
209 | */ |
||
210 | $html_images = self::from_html( $post_id ); |
||
211 | $inserted_images = array(); |
||
212 | |||
213 | foreach( $html_images as $html_image ) { |
||
214 | $src = parse_url( $html_image['src'] ); |
||
215 | // strip off any query strings from src |
||
216 | if( ! empty( $src['scheme'] ) && ! empty( $src['host'] ) ) { |
||
217 | $inserted_images[] = $src['scheme'] . '://' . $src['host'] . $src['path']; |
||
218 | } elseif( ! empty( $src['host'] ) ) { |
||
219 | $inserted_images[] = set_url_scheme( 'http://' . $src['host'] . $src['path'] ); |
||
220 | } else { |
||
221 | $inserted_images[] = site_url( '/' ) . $src['path']; |
||
222 | } |
||
223 | } |
||
224 | foreach( $images as $i => $image ) { |
||
225 | if ( !in_array( $image['src'], $inserted_images ) ) |
||
226 | unset( $images[$i] ); |
||
227 | } |
||
228 | |||
229 | return $images; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Check if a Featured Image is set for this post, and return it in a similar |
||
234 | * format to the other images?_from_*() methods. |
||
235 | * @param int $post_id The post ID to check |
||
236 | * @return Array containing details of the Featured Image, or empty array if none. |
||
237 | */ |
||
238 | static function from_thumbnail( $post_id, $width = 200, $height = 200 ) { |
||
239 | $images = array(); |
||
240 | |||
241 | $post = get_post( $post_id ); |
||
242 | |||
243 | if ( ! empty( $post->post_password ) ) { |
||
244 | return $images; |
||
245 | } |
||
246 | |||
247 | if ( ! function_exists( 'get_post_thumbnail_id' ) ) { |
||
248 | return $images; |
||
249 | } |
||
250 | |||
251 | $thumb = get_post_thumbnail_id( $post_id ); |
||
252 | |||
253 | if ( $thumb ) { |
||
254 | $meta = wp_get_attachment_metadata( $thumb ); |
||
255 | // Must be larger than requested minimums |
||
256 | if ( !isset( $meta['width'] ) || $meta['width'] < $width ) |
||
257 | return $images; |
||
258 | if ( !isset( $meta['height'] ) || $meta['height'] < $height ) |
||
259 | return $images; |
||
260 | |||
261 | $too_big = ( ( ! empty( $meta['width'] ) && $meta['width'] > 1200 ) || ( ! empty( $meta['height'] ) && $meta['height'] > 1200 ) ); |
||
262 | |||
263 | if ( |
||
264 | $too_big && |
||
265 | ( |
||
266 | ( method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'photon' ) ) || |
||
267 | ( defined( 'IS_WPCOM' ) && IS_WPCOM ) |
||
268 | ) |
||
269 | ) { |
||
270 | $img_src = wp_get_attachment_image_src( $thumb, array( 1200, 1200 ) ); |
||
271 | } else { |
||
272 | $img_src = wp_get_attachment_image_src( $thumb, 'full' ); |
||
273 | } |
||
274 | if ( ! is_array( $img_src ) ) { |
||
275 | // If wp_get_attachment_image_src returns false but we know that there should be an image that could be used. |
||
276 | // we try a bit harder and user the data that we have. |
||
277 | $thumb_post_data = get_post( $thumb ); |
||
278 | $img_src = array( $thumb_post_data->guid, $meta['width'], $meta['height'] ); |
||
279 | } |
||
280 | |||
281 | $url = $img_src[0]; |
||
282 | $images = array( array( // Other methods below all return an array of arrays |
||
283 | 'type' => 'image', |
||
284 | 'from' => 'thumbnail', |
||
285 | 'src' => $url, |
||
286 | 'src_width' => $img_src[1], |
||
287 | 'src_height' => $img_src[2], |
||
288 | 'href' => get_permalink( $thumb ), |
||
289 | ) ); |
||
290 | |||
291 | } |
||
292 | |||
293 | if ( empty( $images ) && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { |
||
294 | $meta_thumbnail = get_post_meta( $post_id, '_jetpack_post_thumbnail', true ); |
||
295 | if ( ! empty( $meta_thumbnail ) ) { |
||
296 | if ( ! isset( $meta_thumbnail['width'] ) || $meta_thumbnail['width'] < $width ) { |
||
297 | return $images; |
||
298 | } |
||
299 | |||
300 | if ( ! isset( $meta_thumbnail['height'] ) || $meta_thumbnail['height'] < $height ) { |
||
301 | return $images; |
||
302 | } |
||
303 | |||
304 | $images = array( array( // Other methods below all return an array of arrays |
||
305 | 'type' => 'image', |
||
306 | 'from' => 'thumbnail', |
||
307 | 'src' => $meta_thumbnail['URL'], |
||
308 | 'src_width' => $meta_thumbnail['width'], |
||
309 | 'src_height' => $meta_thumbnail['height'], |
||
310 | 'href' => $meta_thumbnail['URL'], |
||
311 | ) ); |
||
312 | } |
||
313 | } |
||
314 | |||
315 | return $images; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Very raw -- just parse the HTML and pull out any/all img tags and return their src |
||
320 | * |
||
321 | * @param mixed $html_or_id The HTML string to parse for images, or a post id. |
||
322 | * @param int $width Minimum Image width. |
||
323 | * @param int $height Minimum Image height. |
||
324 | * |
||
325 | * @uses DOMDocument |
||
326 | * |
||
327 | * @return Array containing images |
||
328 | */ |
||
329 | static function from_html( $html_or_id, $width = 200, $height = 200 ) { |
||
330 | $images = array(); |
||
331 | |||
332 | if ( is_numeric( $html_or_id ) ) { |
||
333 | $post = get_post( $html_or_id ); |
||
334 | |||
335 | if ( empty( $post ) || ! empty( $post->post_password ) ) { |
||
336 | return $images; |
||
337 | } |
||
338 | |||
339 | $html = $post->post_content; // DO NOT apply the_content filters here, it will cause loops. |
||
340 | } else { |
||
341 | $html = $html_or_id; |
||
342 | } |
||
343 | |||
344 | if ( ! $html ) { |
||
345 | return $images; |
||
346 | } |
||
347 | |||
348 | // Do not go any further if DOMDocument is disabled on the server. |
||
349 | if ( ! class_exists( 'DOMDocument' ) ) { |
||
350 | return $images; |
||
351 | } |
||
352 | |||
353 | // Let's grab all image tags from the HTML. |
||
354 | $dom_doc = new DOMDocument; |
||
355 | |||
356 | // The @ is not enough to suppress errors when dealing with libxml, |
||
357 | // we have to tell it directly how we want to handle errors. |
||
358 | libxml_use_internal_errors( true ); |
||
359 | @$dom_doc->loadHTML( $html ); |
||
0 ignored issues
–
show
|
|||
360 | libxml_use_internal_errors( false ); |
||
361 | |||
362 | $image_tags = $dom_doc->getElementsByTagName( 'img' ); |
||
363 | |||
364 | // For each image Tag, make sure it can be added to the $images array, and add it. |
||
365 | foreach ( $image_tags as $image_tag ) { |
||
366 | $img_src = $image_tag->getAttribute( 'src' ); |
||
367 | |||
368 | if ( empty( $img_src ) ) { |
||
369 | continue; |
||
370 | } |
||
371 | |||
372 | // Do not grab smiley images that were automatically created by WP when entering text smilies. |
||
373 | if ( stripos( $img_src, '/smilies/' ) ) { |
||
374 | continue; |
||
375 | } |
||
376 | |||
377 | $meta = array( |
||
378 | 'width' => (int) $image_tag->getAttribute( 'width' ), |
||
379 | 'height' => (int) $image_tag->getAttribute( 'height' ), |
||
380 | ); |
||
381 | |||
382 | // Must be larger than 200x200 (or user-specified). |
||
383 | if ( empty( $meta['width'] ) || $meta['width'] < $width ) { |
||
384 | continue; |
||
385 | } |
||
386 | if ( empty( $meta['height'] ) || $meta['height'] < $height ) { |
||
387 | continue; |
||
388 | } |
||
389 | |||
390 | $images[] = array( |
||
391 | 'type' => 'image', |
||
392 | 'from' => 'html', |
||
393 | 'src' => $img_src, |
||
394 | 'src_width' => $meta['width'], |
||
395 | 'src_height' => $meta['height'], |
||
396 | 'href' => '', // No link to apply to these. Might potentially parse for that as well, but not for now. |
||
397 | ); |
||
398 | } |
||
399 | return $images; |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * @param int $post_id The post ID to check |
||
404 | * @param int $size |
||
405 | * @return Array containing details of the image, or empty array if none. |
||
406 | */ |
||
407 | static function from_blavatar( $post_id, $size = 96 ) { |
||
408 | |||
409 | $permalink = get_permalink( $post_id ); |
||
410 | |||
411 | if ( function_exists( 'blavatar_domain' ) && function_exists( 'blavatar_exists' ) && function_exists( 'blavatar_url' ) ) { |
||
412 | $domain = blavatar_domain( $permalink ); |
||
413 | |||
414 | if ( ! blavatar_exists( $domain ) ) { |
||
415 | return array(); |
||
416 | } |
||
417 | |||
418 | $url = blavatar_url( $domain, 'img', $size ); |
||
419 | } elseif ( function_exists( 'has_site_icon' ) && has_site_icon() ) { |
||
420 | $url = get_site_icon_url( $size ); |
||
421 | } else { |
||
422 | return array(); |
||
423 | } |
||
424 | |||
425 | return array( array( |
||
426 | 'type' => 'image', |
||
427 | 'from' => 'blavatar', |
||
428 | 'src' => $url, |
||
429 | 'src_width' => $size, |
||
430 | 'src_height' => $size, |
||
431 | 'href' => $permalink, |
||
432 | ) ); |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * Gets a post image from the author avatar. |
||
437 | * |
||
438 | * @param int $post_id The post ID to check. |
||
439 | * @param int $size The size of the avatar to get. |
||
440 | * @param string $default The default image to use. |
||
441 | * @return Array containing details of the image, or empty array if none. |
||
442 | */ |
||
443 | static function from_gravatar( $post_id, $size = 96, $default = false ) { |
||
444 | $post = get_post( $post_id ); |
||
445 | $permalink = get_permalink( $post_id ); |
||
446 | |||
447 | if ( function_exists( 'wpcom_get_avatar_url' ) ) { |
||
448 | $url = wpcom_get_avatar_url( $post->post_author, $size, $default, true ); |
||
449 | if ( $url && is_array( $url ) ) { |
||
450 | $url = $url[0]; |
||
451 | } |
||
452 | } else { |
||
453 | $url = get_avatar_url( $post->post_author, array( |
||
454 | 'size' => $size, |
||
455 | 'default' => $default, |
||
456 | ) ); |
||
457 | } |
||
458 | |||
459 | return array( |
||
460 | array( |
||
461 | 'type' => 'image', |
||
462 | 'from' => 'gravatar', |
||
463 | 'src' => $url, |
||
464 | 'src_width' => $size, |
||
465 | 'src_height' => $size, |
||
466 | 'href' => $permalink, |
||
467 | ), |
||
468 | ); |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * Run through the different methods that we have available to try to find a single good |
||
473 | * display image for this post. |
||
474 | * @param int $post_id |
||
475 | * @param array $args Other arguments (currently width and height required for images where possible to determine) |
||
476 | * @return Array containing details of the best image to be used |
||
477 | */ |
||
478 | static function get_image( $post_id, $args = array() ) { |
||
479 | $image = ''; |
||
480 | |||
481 | /** |
||
482 | * Fires before we find a single good image for a specific post. |
||
483 | * |
||
484 | * @since 2.2.0 |
||
485 | * |
||
486 | * @param int $post_id Post ID. |
||
487 | */ |
||
488 | do_action( 'jetpack_postimages_pre_get_image', $post_id ); |
||
489 | $media = self::get_images( $post_id, $args ); |
||
490 | |||
491 | |||
492 | if ( is_array( $media ) ) { |
||
493 | foreach ( $media as $item ) { |
||
494 | if ( 'image' == $item['type'] ) { |
||
495 | $image = $item; |
||
496 | break; |
||
497 | } |
||
498 | } |
||
499 | } |
||
500 | |||
501 | /** |
||
502 | * Fires after we find a single good image for a specific post. |
||
503 | * |
||
504 | * @since 2.2.0 |
||
505 | * |
||
506 | * @param int $post_id Post ID. |
||
507 | */ |
||
508 | do_action( 'jetpack_postimages_post_get_image', $post_id ); |
||
509 | |||
510 | return $image; |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * Get an array containing a collection of possible images for this post, stopping once we hit a method |
||
515 | * that returns something useful. |
||
516 | * @param int $post_id |
||
517 | * @param array $args Optional args, see defaults list for details |
||
518 | * @return Array containing images that would be good for representing this post |
||
519 | */ |
||
520 | static function get_images( $post_id, $args = array() ) { |
||
521 | // Figure out which image to attach to this post. |
||
522 | $media = false; |
||
523 | |||
524 | /** |
||
525 | * Filters the array of images that would be good for a specific post. |
||
526 | * This filter is applied before options ($args) filter the original array. |
||
527 | * |
||
528 | * @since 2.0.0 |
||
529 | * |
||
530 | * @param array $media Array of images that would be good for a specific post. |
||
531 | * @param int $post_id Post ID. |
||
532 | * @param array $args Array of options to get images. |
||
533 | */ |
||
534 | $media = apply_filters( 'jetpack_images_pre_get_images', $media, $post_id, $args ); |
||
535 | if ( $media ) |
||
536 | return $media; |
||
537 | |||
538 | $defaults = array( |
||
539 | 'width' => 200, // Required minimum width (if possible to determine) |
||
540 | 'height' => 200, // Required minimum height (if possible to determine) |
||
541 | |||
542 | 'fallback_to_avatars' => false, // Optionally include Blavatar and Gravatar (in that order) in the image stack |
||
543 | 'avatar_size' => 96, // Used for both Grav and Blav |
||
544 | 'gravatar_default' => false, // Default image to use if we end up with no Gravatar |
||
545 | |||
546 | 'from_thumbnail' => true, // Use these flags to specify which methods to use to find an image |
||
547 | 'from_slideshow' => true, |
||
548 | 'from_gallery' => true, |
||
549 | 'from_attachment' => true, |
||
550 | 'from_html' => true, |
||
551 | |||
552 | 'html_content' => '' // HTML string to pass to from_html() |
||
553 | ); |
||
554 | $args = wp_parse_args( $args, $defaults ); |
||
555 | |||
556 | $media = false; |
||
557 | if ( $args['from_thumbnail'] ) |
||
558 | $media = self::from_thumbnail( $post_id, $args['width'], $args['height'] ); |
||
559 | View Code Duplication | if ( !$media && $args['from_slideshow'] ) |
|
560 | $media = self::from_slideshow( $post_id, $args['width'], $args['height'] ); |
||
561 | if ( !$media && $args['from_gallery'] ) |
||
562 | $media = self::from_gallery( $post_id ); |
||
563 | View Code Duplication | if ( !$media && $args['from_attachment'] ) |
|
564 | $media = self::from_attachment( $post_id, $args['width'], $args['height'] ); |
||
565 | if ( !$media && $args['from_html'] ) { |
||
566 | if ( empty( $args['html_content'] ) ) |
||
567 | $media = self::from_html( $post_id, $args['width'], $args['height'] ); // Use the post_id, which will load the content |
||
568 | else |
||
569 | $media = self::from_html( $args['html_content'], $args['width'], $args['height'] ); // If html_content is provided, use that |
||
570 | } |
||
571 | |||
572 | if ( !$media && $args['fallback_to_avatars'] ) { |
||
573 | $media = self::from_blavatar( $post_id, $args['avatar_size'] ); |
||
574 | if ( !$media ) |
||
575 | $media = self::from_gravatar( $post_id, $args['avatar_size'], $args['gravatar_default'] ); |
||
576 | } |
||
577 | |||
578 | /** |
||
579 | * Filters the array of images that would be good for a specific post. |
||
580 | * This filter is applied after options ($args) filter the original array. |
||
581 | * |
||
582 | * @since 2.0.0 |
||
583 | * |
||
584 | * @param array $media Array of images that would be good for a specific post. |
||
585 | * @param int $post_id Post ID. |
||
586 | * @param array $args Array of options to get images. |
||
587 | */ |
||
588 | return apply_filters( 'jetpack_images_get_images', $media, $post_id, $args ); |
||
589 | } |
||
590 | |||
591 | /** |
||
592 | * Takes an image URL and pixel dimensions then returns a URL for the |
||
593 | * resized and cropped image. |
||
594 | * |
||
595 | * @param string $src |
||
596 | * @param int $dimension |
||
597 | * @return string Transformed image URL |
||
598 | */ |
||
599 | static function fit_image_url( $src, $width, $height ) { |
||
600 | $width = (int) $width; |
||
601 | $height = (int) $height; |
||
602 | |||
603 | if ( $width < 1 || $height < 1 ) { |
||
604 | return $src; |
||
605 | } |
||
606 | |||
607 | // See if we should bypass WordPress.com SaaS resizing |
||
608 | if ( has_filter( 'jetpack_images_fit_image_url_override' ) ) { |
||
609 | /** |
||
610 | * Filters the image URL used after dimensions are set by Photon. |
||
611 | * |
||
612 | * @since 3.3.0 |
||
613 | * |
||
614 | * @param string $src Image URL. |
||
615 | * @param int $width Image width. |
||
616 | * @param int $width Image height. |
||
617 | */ |
||
618 | return apply_filters( 'jetpack_images_fit_image_url_override', $src, $width, $height ); |
||
619 | } |
||
620 | |||
621 | // If WPCOM hosted image use native transformations |
||
622 | $img_host = parse_url( $src, PHP_URL_HOST ); |
||
623 | if ( '.files.wordpress.com' == substr( $img_host, -20 ) ) { |
||
624 | return add_query_arg( array( 'w' => $width, 'h' => $height, 'crop' => 1 ), set_url_scheme( $src ) ); |
||
625 | } |
||
626 | |||
627 | // Use Photon magic |
||
628 | if( function_exists( 'jetpack_photon_url' ) ) { |
||
629 | return jetpack_photon_url( $src, array( 'resize' => "$width,$height" ) ); |
||
630 | } |
||
631 | |||
632 | // Arg... no way to resize image using WordPress.com infrastructure! |
||
633 | return $src; |
||
634 | } |
||
635 | } |
||
636 |
If you suppress an error, we recommend checking for the error condition explicitly: