PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.1
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.1
16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 All 504 releases
← All changes | class.jetpack-post-images.php +95 -803 12.5.2 → 16.3-a.1 View file →
@@ -1,12 +1,13 @@
1 1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 2 /**
3 3 * Useful for finding an image to display alongside/in representation of a specific post.
4 4 *
5 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images instead.
5 6 * @package automattic/jetpack
6 7 */
7 8
8 -use Automattic\Jetpack\Image_CDN\Image_CDN_Core;
9 +use Automattic\Jetpack\Post_Media\Images;
9 10
10 11 /**
11 12 * Useful for finding an image to display alongside/in representation of a specific post.
12 13 *
@@ -15,13 +16,19 @@
15 16 * function-bucket. You can also call Jetpack_PostImages::get_image() to cycle through all of the methods until
16 17 * one of them finds something useful.
17 18 *
18 19 * This file is included verbatim in Jetpack
20 + *
21 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images instead.
22 + *
23 + * @see Automattic\Jetpack\Post_Media\Images
19 24 */
20 25 class Jetpack_PostImages {
21 26 /**
22 27 * If a slideshow is embedded within a post, then parse out the images involved and return them
23 28 *
29 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images::from_slideshow() instead.
30 + *
24 31 * @param int $post_id Post ID.
25 32 * @param int $width Image width.
26 33 * @param int $height Image height.
27 34 * @return array Images.
@@ -26,161 +33,38 @@
26 33 * @param int $height Image height.
27 34 * @return array Images.
28 35 */
29 36 public static function from_slideshow( $post_id, $width = 200, $height = 200 ) {
30 - $images = array();
37 + _deprecated_function( __METHOD__, '15.7', 'Automattic\Jetpack\Post_Media\Images::from_slideshow' );
38 + return Images::from_slideshow( $post_id, $width, $height );
39 + }
31 40
32 - $post = get_post( $post_id );
33 -
34 - if ( ! $post ) {
35 - return $images;
36 - }
37 -
38 - if ( ! empty( $post->post_password ) ) {
39 - return $images;
40 - }
41 -
42 - if ( false === has_shortcode( $post->post_content, 'slideshow' ) ) {
43 - return $images; // no slideshow - bail.
44 - }
45 -
46 - $permalink = get_permalink( $post->ID );
47 -
48 - // Mechanic: Somebody set us up the bomb.
49 - $old_post = $GLOBALS['post'];
50 - $GLOBALS['post'] = $post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
51 - $old_shortcodes = $GLOBALS['shortcode_tags'];
52 - $GLOBALS['shortcode_tags'] = array( 'slideshow' => $old_shortcodes['slideshow'] ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
53 -
54 - // Find all the slideshows.
55 - preg_match_all( '/' . get_shortcode_regex() . '/sx', $post->post_content, $slideshow_matches, PREG_SET_ORDER );
56 -
57 - ob_start(); // The slideshow shortcode handler calls wp_print_scripts and wp_print_styles... not too happy about that.
58 -
59 - foreach ( $slideshow_matches as $slideshow_match ) {
60 - $slideshow = do_shortcode_tag( $slideshow_match );
61 - $pos = stripos( $slideshow, 'jetpack-slideshow' );
62 - if ( false === $pos ) { // must be something wrong - or we changed the output format in which case none of the following will work.
63 - continue;
64 - }
65 - $start = strpos( $slideshow, '[', $pos );
66 - $end = strpos( $slideshow, ']', $start );
67 - $post_images = json_decode( wp_specialchars_decode( str_replace( "'", '"', substr( $slideshow, $start, $end - $start + 1 ) ), ENT_QUOTES ) ); // parse via JSON
68 - // If the JSON didn't decode don't try and act on it.
69 - if ( is_array( $post_images ) ) {
70 - foreach ( $post_images as $post_image ) {
71 - $post_image_id = absint( $post_image->id );
72 - if ( ! $post_image_id ) {
73 - continue;
74 - }
75 -
76 - $meta = wp_get_attachment_metadata( $post_image_id );
77 -
78 - // Must be larger than 200x200 (or user-specified).
79 - if ( ! isset( $meta['width'] ) || $meta['width'] < $width ) {
80 - continue;
81 - }
82 - if ( ! isset( $meta['height'] ) || $meta['height'] < $height ) {
83 - continue;
84 - }
85 -
86 - $url = wp_get_attachment_url( $post_image_id );
87 -
88 - $images[] = array(
89 - 'type' => 'image',
90 - 'from' => 'slideshow',
91 - 'src' => $url,
92 - 'src_width' => $meta['width'],
93 - 'src_height' => $meta['height'],
94 - 'href' => $permalink,
95 - );
96 - }
97 - }
98 - }
99 - ob_end_clean();
100 -
101 - // Operator: Main screen turn on.
102 - $GLOBALS['shortcode_tags'] = $old_shortcodes; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
103 - $GLOBALS['post'] = $old_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
104 -
105 - return $images;
41 + /**
42 + * Filtering out images with broken URL from galleries.
43 + *
44 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images::filter_gallery_urls() instead.
45 + *
46 + * @param array $galleries Galleries.
47 + * @return array $filtered_galleries
48 + */
49 + public static function filter_gallery_urls( $galleries ) {
50 + _deprecated_function( __METHOD__, '15.7', 'Automattic\Jetpack\Post_Media\Images::filter_gallery_urls' );
51 + return Images::filter_gallery_urls( $galleries );
106 52 }
107 53
108 54 /**
109 55 * If a gallery is detected, then get all the images from it.
110 56 *
57 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images::from_gallery() instead.
58 + *
111 59 * @param int $post_id Post ID.
60 + * @param int $width Minimum image width to consider.
61 + * @param int $height Minimum image height to consider.
112 62 * @return array Images.
113 63 */
114 - public static function from_gallery( $post_id ) {
115 - $images = array();
116 -
117 - $post = get_post( $post_id );
118 -
119 - if ( ! $post ) {
120 - return $images;
121 - }
122 -
123 - if ( ! empty( $post->post_password ) ) {
124 - return $images;
125 - }
126 -
127 - $permalink = get_permalink( $post->ID );
128 -
129 - /**
130 - * Juggle global post object because the gallery shortcode uses the
131 - * global object.
132 - *
133 - * See core ticket:
134 - * https://core.trac.wordpress.org/ticket/39304
135 - */
136 - // phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited
137 - if ( isset( $GLOBALS['post'] ) ) {
138 - $juggle_post = $GLOBALS['post'];
139 - $GLOBALS['post'] = $post;
140 - $galleries = get_post_galleries( $post->ID, false );
141 - $GLOBALS['post'] = $juggle_post;
142 - } else {
143 - $GLOBALS['post'] = $post;
144 - $galleries = get_post_galleries( $post->ID, false );
145 - unset( $GLOBALS['post'] );
146 - }
147 - // phpcs:enable WordPress.WP.GlobalVariablesOverride.Prohibited
148 -
149 - foreach ( $galleries as $gallery ) {
150 - if ( isset( $gallery['type'] ) && 'slideshow' === $gallery['type'] && ! empty( $gallery['ids'] ) ) {
151 - $image_ids = explode( ',', $gallery['ids'] );
152 - $image_size = isset( $gallery['size'] ) ? $gallery['size'] : 'thumbnail';
153 - foreach ( $image_ids as $image_id ) {
154 - $image = wp_get_attachment_image_src( $image_id, $image_size );
155 - if ( ! empty( $image[0] ) ) {
156 - list( $raw_src ) = explode( '?', $image[0] ); // pull off any Query string (?w=250).
157 - $raw_src = wp_specialchars_decode( $raw_src ); // rawify it.
158 - $raw_src = esc_url_raw( $raw_src ); // clean it.
159 - $images[] = array(
160 - 'type' => 'image',
161 - 'from' => 'gallery',
162 - 'src' => $raw_src,
163 - 'href' => $permalink,
164 - );
165 - }
166 - }
167 - } elseif ( ! empty( $gallery['src'] ) ) {
168 - foreach ( $gallery['src'] as $src ) {
169 - list( $raw_src ) = explode( '?', $src ); // pull off any Query string (?w=250).
170 - $raw_src = wp_specialchars_decode( $raw_src ); // rawify it.
171 - $raw_src = esc_url_raw( $raw_src ); // clean it.
172 - $images[] = array(
173 - 'type' => 'image',
174 - 'from' => 'gallery',
175 - 'src' => $raw_src,
176 - 'href' => $permalink,
177 - );
178 - }
179 - }
180 - }
181 -
182 - return $images;
64 + public static function from_gallery( $post_id, $width = 200, $height = 200 ) {
65 + _deprecated_function( __METHOD__, '15.7', 'Automattic\Jetpack\Post_Media\Images::from_gallery' );
66 + return Images::from_gallery( $post_id, $width, $height );
183 67 }
184 68
185 69 /**
186 70 * Get attachment images for a specified post and return them. Also make sure
@@ -185,8 +69,10 @@
185 69 /**
186 70 * Get attachment images for a specified post and return them. Also make sure
187 71 * their dimensions are at or above a required minimum.
188 72 *
73 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images::from_attachment() instead.
74 + *
189 75 * @param int $post_id The post ID to check.
190 76 * @param int $width Image width.
191 77 * @param int $height Image height.
192 78 * @return array Containing details of the image, or empty array if none.
@@ -191,65 +77,10 @@
191 77 * @param int $height Image height.
192 78 * @return array Containing details of the image, or empty array if none.
193 79 */
194 80 public static function from_attachment( $post_id, $width = 200, $height = 200 ) {
195 - $images = array();
196 -
197 - $post = get_post( $post_id );
198 -
199 - if ( ! empty( $post->post_password ) ) {
200 - return $images;
201 - }
202 -
203 - $post_images = get_posts(
204 - array(
205 - 'post_parent' => $post_id, // Must be children of post.
206 - 'numberposts' => 5, // No more than 5.
207 - 'post_type' => 'attachment', // Must be attachments.
208 - 'post_mime_type' => 'image', // Must be images.
209 - 'suppress_filters' => false,
210 - )
211 - );
212 -
213 - if ( ! $post_images ) {
214 - return $images;
215 - }
216 -
217 - $permalink = get_permalink( $post_id );
218 -
219 - foreach ( $post_images as $post_image ) {
220 - $current_image = self::get_attachment_data( $post_image->ID, $permalink, $width, $height );
221 - if ( false !== $current_image ) {
222 - $images[] = $current_image;
223 - }
224 - }
225 -
226 - /*
227 - * We only want to pass back attached images that were actually inserted.
228 - * We can load up all the images found in the HTML source and then
229 - * compare URLs to see if an image is attached AND inserted.
230 - */
231 - $html_images = self::from_html( $post_id );
232 - $inserted_images = array();
233 -
234 - foreach ( $html_images as $html_image ) {
235 - $src = wp_parse_url( $html_image['src'] );
236 - // strip off any query strings from src.
237 - if ( ! empty( $src['scheme'] ) && ! empty( $src['host'] ) ) {
238 - $inserted_images[] = $src['scheme'] . '://' . $src['host'] . $src['path'];
239 - } elseif ( ! empty( $src['host'] ) ) {
240 - $inserted_images[] = set_url_scheme( 'http://' . $src['host'] . $src['path'] );
241 - } else {
242 - $inserted_images[] = site_url( '/' ) . $src['path'];
243 - }
244 - }
245 - foreach ( $images as $i => $image ) {
246 - if ( ! in_array( $image['src'], $inserted_images, true ) ) {
247 - unset( $images[ $i ] );
248 - }
249 - }
250 -
251 - return $images;
81 + _deprecated_function( __METHOD__, '15.7', 'Automattic\Jetpack\Post_Media\Images::from_attachment' );
82 + return Images::from_attachment( $post_id, $width, $height );
252 83 }
253 84
254 85 /**
255 86 * Check if a Featured Image is set for this post, and return it in a similar
@@ -254,8 +85,10 @@
254 85 /**
255 86 * Check if a Featured Image is set for this post, and return it in a similar
256 87 * format to the other images?_from_*() methods.
257 88 *
89 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images::from_thumbnail() instead.
90 + *
258 91 * @param int $post_id The post ID to check.
259 92 * @param int $width Image width.
260 93 * @param int $height Image height.
261 94 * @return array containing details of the Featured Image, or empty array if none.
@@ -260,110 +93,16 @@
260 93 * @param int $height Image height.
261 94 * @return array containing details of the Featured Image, or empty array if none.
262 95 */
263 96 public static function from_thumbnail( $post_id, $width = 200, $height = 200 ) {
264 - $images = array();
265 -
266 - $post = get_post( $post_id );
267 -
268 - if ( ! empty( $post->post_password ) ) {
269 - return $images;
270 - }
271 -
272 - if ( 'attachment' === get_post_type( $post ) && wp_attachment_is_image( $post ) ) {
273 - $thumb = $post_id;
274 - } else {
275 - $thumb = get_post_thumbnail_id( $post );
276 - }
277 -
278 - if ( $thumb ) {
279 - $meta = wp_get_attachment_metadata( $thumb );
280 - // Must be larger than requested minimums.
281 - if ( ! isset( $meta['width'] ) || $meta['width'] < $width ) {
282 - return $images;
283 - }
284 - if ( ! isset( $meta['height'] ) || $meta['height'] < $height ) {
285 - return $images;
286 - }
287 -
288 - $too_big = ( ( ! empty( $meta['width'] ) && $meta['width'] > 1200 ) || ( ! empty( $meta['height'] ) && $meta['height'] > 1200 ) );
289 -
290 - if (
291 - $too_big &&
292 - (
293 - ( method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'photon' ) ) ||
294 - ( defined( 'IS_WPCOM' ) && IS_WPCOM )
295 - )
296 - ) {
297 - $img_src = wp_get_attachment_image_src( $thumb, array( 1200, 1200 ) );
298 - } else {
299 - $img_src = wp_get_attachment_image_src( $thumb, 'full' );
300 - }
301 - if ( ! is_array( $img_src ) ) {
302 - // If wp_get_attachment_image_src returns false but we know that there should be an image that could be used.
303 - // we try a bit harder and user the data that we have.
304 - $thumb_post_data = get_post( $thumb );
305 - $img_src = array( $thumb_post_data->guid, $meta['width'], $meta['height'] );
306 - }
307 -
308 - // Let's try to use the postmeta if we can, since it seems to be
309 - // more reliable
310 - if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
311 - $featured_image = get_post_meta( $post->ID, '_jetpack_featured_image' );
312 - if ( $featured_image ) {
313 - $url = $featured_image[0];
314 - } else {
315 - $url = $img_src[0];
316 - }
317 - } else {
318 - $url = $img_src[0];
319 - }
320 - $images = array(
321 - array( // Other methods below all return an array of arrays.
322 - 'type' => 'image',
323 - 'from' => 'thumbnail',
324 - 'src' => $url,
325 - 'src_width' => $img_src[1],
326 - 'src_height' => $img_src[2],
327 - 'href' => get_permalink( $thumb ),
328 - 'alt_text' => self::get_alt_text( $thumb ),
329 - ),
330 - );
331 -
332 - }
333 -
334 - if ( empty( $images ) && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
335 - $meta_thumbnail = get_post_meta( $post_id, '_jetpack_post_thumbnail', true );
336 - if ( ! empty( $meta_thumbnail ) ) {
337 - if ( ! isset( $meta_thumbnail['width'] ) || $meta_thumbnail['width'] < $width ) {
338 - return $images;
339 - }
340 -
341 - if ( ! isset( $meta_thumbnail['height'] ) || $meta_thumbnail['height'] < $height ) {
342 - return $images;
343 - }
344 -
345 - $images = array(
346 - array( // Other methods below all return an array of arrays.
347 - 'type' => 'image',
348 - 'from' => 'thumbnail',
349 - 'src' => $meta_thumbnail['URL'],
350 - 'src_width' => $meta_thumbnail['width'],
351 - 'src_height' => $meta_thumbnail['height'],
352 - 'href' => $meta_thumbnail['URL'],
353 - 'alt_text' => self::get_alt_text( $thumb ),
354 - ),
355 - );
356 - }
357 - }
358 -
359 - return $images;
97 + _deprecated_function( __METHOD__, '15.7', 'Automattic\Jetpack\Post_Media\Images::from_thumbnail' );
98 + return Images::from_thumbnail( $post_id, $width, $height );
360 99 }
361 100
362 101 /**
363 102 * Get images from Gutenberg Image blocks.
364 103 *
365 - * @since 6.9.0
104 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images::from_blocks() instead.
366 105 *
367 106 * @param mixed $html_or_id The HTML string to parse for images, or a post id.
368 107 * @param int $width Minimum Image width.
369 108 * @param int $height Minimum Image height.
@@ -368,226 +107,54 @@
368 107 * @param int $width Minimum Image width.
369 108 * @param int $height Minimum Image height.
370 109 */
371 110 public static function from_blocks( $html_or_id, $width = 200, $height = 200 ) {
372 - $images = array();
373 -
374 - $html_info = self::get_post_html( $html_or_id );
375 -
376 - if ( empty( $html_info['html'] ) ) {
377 - return $images;
378 - }
379 -
380 - // Look for block information in the HTML.
381 - $blocks = parse_blocks( $html_info['html'] );
382 - if ( empty( $blocks ) ) {
383 - return $images;
384 - }
385 -
386 - /*
387 - * Let's loop through our blocks.
388 - * Some blocks may include some other blocks. Let's go 2 levels deep to look for blocks
389 - * that we support and that may include images (see get_images_from_block)
390 - *
391 - * @to-do: instead of looping manually (that's a lot of if and loops), search recursively instead.
392 - */
393 - foreach ( $blocks as $block ) {
394 - if ( ! self::is_nested_block( $block ) || 'core/media-text' === $block['blockName'] ) {
395 - $images = self::get_images_from_block( $images, $block, $html_info, $width, $height );
396 - } else {
397 - foreach ( $block['innerBlocks'] as $inner_block ) {
398 - if ( ! self::is_nested_block( $inner_block ) ) {
399 - $images = self::get_images_from_block( $images, $inner_block, $html_info, $width, $height );
400 - } else {
401 - foreach ( $inner_block['innerBlocks'] as $inner_inner_block ) {
402 - $images = self::get_images_from_block( $images, $inner_inner_block, $html_info, $width, $height );
403 - }
404 - }
405 - }
406 - }
407 - }
408 -
409 - /**
410 - * Returning a filtered array because get_attachment_data returns false
411 - * for unsuccessful attempts.
412 - */
413 - return array_filter( $images );
111 + _deprecated_function( __METHOD__, '15.7', 'Automattic\Jetpack\Post_Media\Images::from_blocks' );
112 + return Images::from_blocks( $html_or_id, $width, $height );
414 113 }
415 114
416 115 /**
417 116 * Very raw -- just parse the HTML and pull out any/all img tags and return their src
418 117 *
118 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images::from_html() instead.
119 + *
419 120 * @param mixed $html_or_id The HTML string to parse for images, or a post id.
420 121 * @param int $width Minimum Image width.
421 122 * @param int $height Minimum Image height.
422 123 *
423 - * @uses DOMDocument
424 - *
425 124 * @return array containing images
426 125 */
427 126 public static function from_html( $html_or_id, $width = 200, $height = 200 ) {
428 - $images = array();
429 -
430 - $html_info = self::get_post_html( $html_or_id );
431 -
432 - if ( empty( $html_info['html'] ) ) {
433 - return $images;
434 - }
435 -
436 - // Do not go any further if DOMDocument is disabled on the server.
437 - if ( ! class_exists( 'DOMDocument' ) ) {
438 - return $images;
439 - }
440 -
441 - // Let's grab all image tags from the HTML.
442 - $dom_doc = new DOMDocument();
443 -
444 - // The @ is not enough to suppress errors when dealing with libxml,
445 - // we have to tell it directly how we want to handle errors.
446 - libxml_use_internal_errors( true );
447 - @$dom_doc->loadHTML( $html_info['html'] ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
448 - libxml_use_internal_errors( false );
449 -
450 - $image_tags = $dom_doc->getElementsByTagName( 'img' );
451 -
452 - // For each image Tag, make sure it can be added to the $images array, and add it.
453 - foreach ( $image_tags as $image_tag ) {
454 - $img_src = $image_tag->getAttribute( 'src' );
455 -
456 - if ( empty( $img_src ) ) {
457 - continue;
458 - }
459 -
460 - // Do not grab smiley images that were automatically created by WP when entering text smilies.
461 - if ( stripos( $img_src, '/smilies/' ) ) {
462 - continue;
463 - }
464 -
465 - $meta = array(
466 - 'width' => (int) $image_tag->getAttribute( 'width' ),
467 - 'height' => (int) $image_tag->getAttribute( 'height' ),
468 - 'alt_text' => $image_tag->getAttribute( 'alt' ),
469 - );
470 -
471 - /**
472 - * Filters the switch to ignore minimum image size requirements. Can be used
473 - * to add custom logic to image dimensions, like only enforcing one of the dimensions,
474 - * or disabling it entirely.
475 - *
476 - * @since 6.4.0
477 - *
478 - * @param bool $ignore Should the image dimensions be ignored?
479 - * @param array $meta Array containing image dimensions parsed from the markup.
480 - */
481 - $ignore_dimensions = apply_filters( 'jetpack_postimages_ignore_minimum_dimensions', false, $meta );
482 -
483 - // Must be larger than 200x200 (or user-specified).
484 - if (
485 - ! $ignore_dimensions
486 - && (
487 - empty( $meta['width'] )
488 - || empty( $meta['height'] )
489 - || $meta['width'] < $width
490 - || $meta['height'] < $height
491 - )
492 - ) {
493 - continue;
494 - }
495 -
496 - $images[] = array(
497 - 'type' => 'image',
498 - 'from' => 'html',
499 - 'src' => $img_src,
500 - 'src_width' => $meta['width'],
501 - 'src_height' => $meta['height'],
502 - 'href' => $html_info['post_url'],
503 - 'alt_text' => $meta['alt_text'],
504 - );
505 - }
506 - return $images;
127 + return Images::from_html( $html_or_id, $width, $height );
507 128 }
508 129
509 130 /**
510 131 * Data from blavatar.
511 132 *
133 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images::from_blavatar() instead.
134 + *
512 135 * @param int $post_id The post ID to check.
513 136 * @param int $size Size.
514 137 * @return array containing details of the image, or empty array if none.
515 138 */
516 139 public static function from_blavatar( $post_id, $size = 96 ) {
517 -
518 - $permalink = get_permalink( $post_id );
519 -
520 - if ( function_exists( 'blavatar_domain' ) && function_exists( 'blavatar_exists' ) && function_exists( 'blavatar_url' ) ) {
521 - $domain = blavatar_domain( $permalink );
522 -
523 - if ( ! blavatar_exists( $domain ) ) {
524 - return array();
525 - }
526 -
527 - $url = blavatar_url( $domain, 'img', $size );
528 - } else {
529 - $url = get_site_icon_url( $size );
530 - if ( ! $url ) {
531 - return array();
532 - }
533 - }
534 -
535 - return array(
536 - array(
537 - 'type' => 'image',
538 - 'from' => 'blavatar',
539 - 'src' => $url,
540 - 'src_width' => $size,
541 - 'src_height' => $size,
542 - 'href' => $permalink,
543 - 'alt_text' => '',
544 - ),
545 - );
140 + _deprecated_function( __METHOD__, '15.7', 'Automattic\Jetpack\Post_Media\Images::from_blavatar' );
141 + return Images::from_blavatar( $post_id, $size );
546 142 }
547 143
548 144 /**
549 145 * Gets a post image from the author avatar.
550 146 *
551 - * @param int $post_id The post ID to check.
552 - * @param int $size The size of the avatar to get.
553 - * @param string $default The default image to use.
147 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images::from_gravatar() instead.
148 + *
149 + * @param int $post_id The post ID to check.
150 + * @param int $size The size of the avatar to get.
151 + * @param string|false $default The default image to use.
554 152 * @return array containing details of the image, or empty array if none.
555 153 */
556 154 public static function from_gravatar( $post_id, $size = 96, $default = false ) {
557 - $post = get_post( $post_id );
558 - $permalink = get_permalink( $post_id );
559 -
560 - if ( ! $post instanceof WP_Post ) {
561 - return array();
562 - }
563 -
564 - if ( function_exists( 'wpcom_get_avatar_url' ) ) {
565 - $url = wpcom_get_avatar_url( $post->post_author, $size, $default, true );
566 - if ( $url && is_array( $url ) ) {
567 - $url = $url[0];
568 - }
569 - } else {
570 - $url = get_avatar_url(
571 - $post->post_author,
572 - array(
573 - 'size' => $size,
574 - 'default' => $default,
575 - )
576 - );
577 - }
578 -
579 - return array(
580 - array(
581 - 'type' => 'image',
582 - 'from' => 'gravatar',
583 - 'src' => $url,
584 - 'src_width' => $size,
585 - 'src_height' => $size,
586 - 'href' => $permalink,
587 - 'alt_text' => '',
588 - ),
589 - );
155 + _deprecated_function( __METHOD__, '15.7', 'Automattic\Jetpack\Post_Media\Images::from_gravatar' );
156 + return Images::from_gravatar( $post_id, $size, $default );
590 157 }
591 158
592 159 /**
593 160 * Run through the different methods that we have available to try to find a single good
@@ -592,44 +159,16 @@
592 159 /**
593 160 * Run through the different methods that we have available to try to find a single good
594 161 * display image for this post.
595 162 *
163 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images::get_image() instead.
164 + *
596 165 * @param int $post_id Post ID.
597 166 * @param array $args Other arguments (currently width and height required for images where possible to determine).
598 167 * @return array|null containing details of the best image to be used, or null if no image is found.
599 168 */
600 169 public static function get_image( $post_id, $args = array() ) {
601 - $image = null;
602 -
603 - /**
604 - * Fires before we find a single good image for a specific post.
605 - *
606 - * @since 2.2.0
607 - *
608 - * @param int $post_id Post ID.
609 - */
610 - do_action( 'jetpack_postimages_pre_get_image', $post_id );
611 - $media = self::get_images( $post_id, $args );
612 -
613 - if ( is_array( $media ) ) {
614 - foreach ( $media as $item ) {
615 - if ( 'image' === $item['type'] ) {
616 - $image = $item;
617 - break;
618 - }
619 - }
620 - }
621 -
622 - /**
623 - * Fires after we find a single good image for a specific post.
624 - *
625 - * @since 2.2.0
626 - *
627 - * @param int $post_id Post ID.
628 - */
629 - do_action( 'jetpack_postimages_post_get_image', $post_id );
630 -
631 - return $image;
170 + return Images::get_image( $post_id, $args );
632 171 }
633 172
634 173 /**
635 174 * Get an array containing a collection of possible images for this post, stopping once we hit a method
@@ -634,96 +173,17 @@
634 173 /**
635 174 * Get an array containing a collection of possible images for this post, stopping once we hit a method
636 175 * that returns something useful.
637 176 *
177 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images::get_images() instead.
178 + *
638 179 * @param int $post_id Post ID.
639 180 * @param array $args Optional args, see defaults list for details.
640 181 * @return array containing images that would be good for representing this post
641 182 */
642 183 public static function get_images( $post_id, $args = array() ) {
643 - // Figure out which image to attach to this post.
644 - $media = array();
645 -
646 - /**
647 - * Filters the array of images that would be good for a specific post.
648 - * This filter is applied before options ($args) filter the original array.
649 - *
650 - * @since 2.0.0
651 - *
652 - * @param array $media Array of images that would be good for a specific post.
653 - * @param int $post_id Post ID.
654 - * @param array $args Array of options to get images.
655 - */
656 - $media = apply_filters( 'jetpack_images_pre_get_images', $media, $post_id, $args );
657 - if ( $media ) {
658 - return $media;
659 - }
660 -
661 - $defaults = array(
662 - 'width' => 200, // Required minimum width (if possible to determine).
663 - 'height' => 200, // Required minimum height (if possible to determine).
664 -
665 - 'fallback_to_avatars' => false, // Optionally include Blavatar and Gravatar (in that order) in the image stack.
666 - 'avatar_size' => 96, // Used for both Grav and Blav.
667 - 'gravatar_default' => false, // Default image to use if we end up with no Gravatar.
668 -
669 - 'from_thumbnail' => true, // Use these flags to specify which methods to use to find an image.
670 - 'from_slideshow' => true,
671 - 'from_gallery' => true,
672 - 'from_attachment' => true,
673 - 'from_blocks' => true,
674 - 'from_html' => true,
675 -
676 - 'html_content' => '', // HTML string to pass to from_html().
677 - );
678 - $args = wp_parse_args( $args, $defaults );
679 -
680 - $media = array();
681 - if ( $args['from_thumbnail'] ) {
682 - $media = self::from_thumbnail( $post_id, $args['width'], $args['height'] );
683 - }
684 - if ( ! $media && $args['from_slideshow'] ) {
685 - $media = self::from_slideshow( $post_id, $args['width'], $args['height'] );
686 - }
687 - if ( ! $media && $args['from_gallery'] ) {
688 - $media = self::from_gallery( $post_id );
689 - }
690 - if ( ! $media && $args['from_attachment'] ) {
691 - $media = self::from_attachment( $post_id, $args['width'], $args['height'] );
692 - }
693 - if ( ! $media && $args['from_blocks'] ) {
694 - if ( empty( $args['html_content'] ) ) {
695 - $media = self::from_blocks( $post_id, $args['width'], $args['height'] ); // Use the post_id, which will load the content.
696 - } else {
697 - $media = self::from_blocks( $args['html_content'], $args['width'], $args['height'] ); // If html_content is provided, use that.
698 - }
699 - }
700 - if ( ! $media && $args['from_html'] ) {
701 - if ( empty( $args['html_content'] ) ) {
702 - $media = self::from_html( $post_id, $args['width'], $args['height'] ); // Use the post_id, which will load the content.
703 - } else {
704 - $media = self::from_html( $args['html_content'], $args['width'], $args['height'] ); // If html_content is provided, use that.
705 - }
706 - }
707 -
708 - if ( ! $media && $args['fallback_to_avatars'] ) {
709 - $media = self::from_blavatar( $post_id, $args['avatar_size'] );
710 - if ( ! $media ) {
711 - $media = self::from_gravatar( $post_id, $args['avatar_size'], $args['gravatar_default'] );
712 - }
713 - }
714 -
715 - /**
716 - * Filters the array of images that would be good for a specific post.
717 - * This filter is applied after options ($args) filter the original array.
718 - *
719 - * @since 2.0.0
720 - *
721 - * @param array $media Array of images that would be good for a specific post.
722 - * @param int $post_id Post ID.
723 - * @param array $args Array of options to get images.
724 - */
725 - return apply_filters( 'jetpack_images_get_images', $media, $post_id, $args );
184 + _deprecated_function( __METHOD__, '15.7', 'Automattic\Jetpack\Post_Media\Images::get_images' );
185 + return Images::get_images( $post_id, $args );
726 186 }
727 187
728 188 /**
729 189 * Takes an image and base pixel dimensions and returns a srcset for the
@@ -728,42 +188,19 @@
728 188 /**
729 189 * Takes an image and base pixel dimensions and returns a srcset for the
730 190 * resized and cropped images, based on a fixed set of multipliers.
731 191 *
192 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images::generate_cropped_srcset() instead.
193 + *
732 194 * @param array $image Array containing details of the image.
733 195 * @param int $base_width Base image width (i.e., the width at 1x).
734 196 * @param int $base_height Base image height (i.e., the height at 1x).
197 + * @param bool $use_widths Whether to generate the srcset with widths instead of multipliers.
735 198 * @return string The srcset for the image.
736 199 */
737 - public static function generate_cropped_srcset( $image, $base_width, $base_height ) {
738 - $srcset = '';
739 -
740 - if ( ! is_array( $image ) || empty( $image['src'] ) || empty( $image['src_width'] ) ) {
741 - return $srcset;
742 - }
743 -
744 - $multipliers = array( 1, 1.5, 2, 3, 4 );
745 - $srcset_values = array();
746 - foreach ( $multipliers as $multiplier ) {
747 - $srcset_width = (int) ( $base_width * $multiplier );
748 - $srcset_height = (int) ( $base_height * $multiplier );
749 - if ( $srcset_width < 1 || $srcset_width > $image['src_width'] ) {
750 - break;
751 - }
752 -
753 - $srcset_url = self::fit_image_url(
754 - $image['src'],
755 - $srcset_width,
756 - $srcset_height
757 - );
758 - $srcset_values[] = "{$srcset_url} {$multiplier}x";
759 - }
760 -
761 - if ( count( $srcset_values ) > 1 ) {
762 - $srcset = implode( ', ', $srcset_values );
763 - }
764 -
765 - return $srcset;
200 + public static function generate_cropped_srcset( $image, $base_width, $base_height, $use_widths = false ) {
201 + _deprecated_function( __METHOD__, '15.7', 'Automattic\Jetpack\Post_Media\Images::generate_cropped_srcset' );
202 + return Images::generate_cropped_srcset( $image, $base_width, $base_height, $use_widths );
766 203 }
767 204
768 205 /**
769 206 * Takes an image URL and pixel dimensions then returns a URL for the
@@ -768,8 +205,10 @@
768 205 /**
769 206 * Takes an image URL and pixel dimensions then returns a URL for the
770 207 * resized and cropped image.
771 208 *
209 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images::fit_image_url() instead.
210 + *
772 211 * @param string $src Image URL.
773 212 * @param int $width Image width.
774 213 * @param int $height Image height.
775 214 * @return string Transformed image URL
@@ -774,55 +213,15 @@
774 213 * @param int $height Image height.
775 214 * @return string Transformed image URL
776 215 */
777 216 public static function fit_image_url( $src, $width, $height ) {
778 - $width = (int) $width;
779 - $height = (int) $height;
780 -
781 - if ( $width < 1 || $height < 1 ) {
782 - return $src;
783 - }
784 -
785 - // See if we should bypass WordPress.com SaaS resizing.
786 - if ( has_filter( 'jetpack_images_fit_image_url_override' ) ) {
787 - /**
788 - * Filters the image URL used after dimensions are set by Photon.
789 - *
790 - * @since 3.3.0
791 - *
792 - * @param string $src Image URL.
793 - * @param int $width Image width.
794 - * @param int $width Image height.
795 - */
796 - return apply_filters( 'jetpack_images_fit_image_url_override', $src, $width, $height );
797 - }
798 -
799 - // If WPCOM hosted image use native transformations.
800 - $img_host = wp_parse_url( $src, PHP_URL_HOST );
801 - if ( '.files.wordpress.com' === substr( $img_host, -20 ) ) {
802 - return add_query_arg(
803 - array(
804 - 'w' => $width,
805 - 'h' => $height,
806 - 'crop' => 1,
807 - ),
808 - set_url_scheme( $src )
809 - );
810 - }
811 -
812 - // Use image cdn magic.
813 - if ( class_exists( Image_CDN_Core::class ) && method_exists( Image_CDN_Core::class, 'cdn_url' ) ) {
814 - return Image_CDN_Core::cdn_url( $src, array( 'resize' => "$width,$height" ) );
815 - }
816 -
817 - // Arg... no way to resize image using WordPress.com infrastructure!
818 - return $src;
217 + return Images::fit_image_url( $src, $width, $height );
819 218 }
820 219
821 220 /**
822 221 * Get HTML from given post content.
823 222 *
824 - * @since 6.9.0
223 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images::get_post_html() instead.
825 224 *
826 225 * @param mixed $html_or_id The HTML string to parse for images, or a post id.
827 226 *
828 227 * @return array $html_info {
@@ -830,32 +229,16 @@
830 229 * @type string $post_url Post URL.
831 230 * }
832 231 */
833 232 public static function get_post_html( $html_or_id ) {
834 - if ( is_numeric( $html_or_id ) ) {
835 - $post = get_post( $html_or_id );
836 -
837 - if ( empty( $post ) || ! empty( $post->post_password ) ) {
838 - return '';
839 - }
840 -
841 - $html_info = array(
842 - 'html' => $post->post_content, // DO NOT apply the_content filters here, it will cause loops.
843 - 'post_url' => get_permalink( $post->ID ),
844 - );
845 - } else {
846 - $html_info = array(
847 - 'html' => $html_or_id,
848 - 'post_url' => '',
849 - );
850 - }
851 - return $html_info;
233 + _deprecated_function( __METHOD__, '15.7', 'Automattic\Jetpack\Post_Media\Images::get_post_html' );
234 + return Images::get_post_html( $html_or_id );
852 235 }
853 236
854 237 /**
855 238 * Get info about a WordPress attachment.
856 239 *
857 - * @since 6.9.0
240 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images::get_attachment_data() instead.
858 241 *
859 242 * @param int $attachment_id Attachment ID.
860 243 * @param string $post_url URL of the post, if we have one.
861 244 * @param int $width Minimum Image width.
@@ -862,140 +245,49 @@
862 245 * @param int $height Minimum Image height.
863 246 * @return array|bool Image data or false if unavailable.
864 247 */
865 248 public static function get_attachment_data( $attachment_id, $post_url, $width, $height ) {
866 - if ( empty( $attachment_id ) ) {
867 - return false;
868 - }
869 -
870 - $meta = wp_get_attachment_metadata( $attachment_id );
871 -
872 - if ( empty( $meta ) ) {
873 - return false;
874 - }
875 -
876 - if ( ! empty( $meta['videopress'] ) ) {
877 - // Use poster image for VideoPress videos.
878 - $url = $meta['videopress']['poster'];
879 - $meta_width = $meta['videopress']['width'];
880 - $meta_height = $meta['videopress']['height'];
881 - } elseif ( ! empty( $meta['thumb'] ) ) {
882 - // On WordPress.com, VideoPress videos have a 'thumb' property with the
883 - // poster image filename instead.
884 - $media_url = wp_get_attachment_url( $attachment_id );
885 - $url = str_replace( wp_basename( $media_url ), $meta['thumb'], $media_url );
886 - $meta_width = $meta['width'];
887 - $meta_height = $meta['height'];
888 - } elseif ( wp_attachment_is( 'video', $attachment_id ) ) {
889 - // We don't have thumbnail images for non-VideoPress videos - skip them.
890 - return false;
891 - } else {
892 - if ( ! isset( $meta['width'] ) || ! isset( $meta['height'] ) ) {
893 - return false;
894 - }
895 - $url = wp_get_attachment_url( $attachment_id );
896 - $meta_width = $meta['width'];
897 - $meta_height = $meta['height'];
898 - }
899 -
900 - if ( $meta_width < $width || $meta_height < $height ) {
901 - return false;
902 - }
903 -
904 - return array(
905 - 'type' => 'image',
906 - 'from' => 'attachment',
907 - 'src' => $url,
908 - 'src_width' => $meta_width,
909 - 'src_height' => $meta_height,
910 - 'href' => $post_url,
911 - 'alt_text' => self::get_alt_text( $attachment_id ),
912 - );
249 + _deprecated_function( __METHOD__, '15.7', 'Automattic\Jetpack\Post_Media\Images::get_attachment_data' );
250 + return Images::get_attachment_data( $attachment_id, $post_url, $width, $height );
913 251 }
914 252
915 253 /**
916 254 * Get the alt text for an image or other media from the Media Library.
917 255 *
918 - * @since 7.1
256 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images::get_alt_text() instead.
919 257 *
920 258 * @param int $attachment_id The Post ID of the media.
921 259 * @return string The alt text value or an empty string.
922 260 */
923 261 public static function get_alt_text( $attachment_id ) {
924 - return (string) get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
262 + _deprecated_function( __METHOD__, '15.7', 'Automattic\Jetpack\Post_Media\Images::get_alt_text' );
263 + return Images::get_alt_text( $attachment_id );
925 264 }
926 265
927 266 /**
928 - * Get an image from a block.
267 + * Determine the size to use with Photon for a thumbnail image.
268 + * Images larger than the maximum thumbnail dimension in either dimension are resized to maintain aspect ratio.
929 269 *
930 - * @since 7.8.0
270 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images::determine_thumbnail_size_for_photon() instead.
931 271 *
932 - * @param array $images Images found.
933 - * @param array $block Block and its attributes.
934 - * @param array $html_info Info about the post where the block is found.
935 - * @param int $width Desired image width.
936 - * @param int $height Desired image height.
937 - *
938 - * @return array Array of images found.
272 + * @param int $width Original image width.
273 + * @param int $height Original image height.
274 + * @return array Array containing the width and height to use with Photon (null means auto).
939 275 */
940 - private static function get_images_from_block( $images, $block, $html_info, $width, $height ) {
941 - /**
942 - * Parse content from Core Image blocks.
943 - * If it is an image block for an image hosted on our site, it will have an ID.
944 - * If it does not have an ID, let `from_html` parse that content later,
945 - * and extract an image if it has size parameters.
946 - */
947 - if (
948 - 'core/image' === $block['blockName']
949 - && ! empty( $block['attrs']['id'] )
950 - ) {
951 - $images[] = self::get_attachment_data( $block['attrs']['id'], $html_info['post_url'], $width, $height );
952 - } elseif (
953 - 'core/media-text' === $block['blockName']
954 - && ! empty( $block['attrs']['mediaId'] )
955 - ) {
956 - $images[] = self::get_attachment_data( $block['attrs']['mediaId'], $html_info['post_url'], $width, $height );
957 - } elseif (
958 - /**
959 - * Parse content from Core Gallery blocks as well from Jetpack's Tiled Gallery and Slideshow blocks.
960 - * Gallery blocks include the ID of each one of the images in the gallery.
961 - */
962 - in_array( $block['blockName'], array( 'core/gallery', 'jetpack/tiled-gallery', 'jetpack/slideshow' ), true )
963 - && ! empty( $block['attrs']['ids'] )
964 - ) {
965 - foreach ( $block['attrs']['ids'] as $img_id ) {
966 - $images[] = self::get_attachment_data( $img_id, $html_info['post_url'], $width, $height );
967 - }
968 - } elseif (
969 - /**
970 - * Parse content from Jetpack's Story block.
971 - */
972 - 'jetpack/story' === $block['blockName']
973 - && ! empty( $block['attrs']['mediaFiles'] )
974 - ) {
975 - foreach ( $block['attrs']['mediaFiles'] as $media_file ) {
976 - if ( ! empty( $media_file['id'] ) ) {
977 - $images[] = self::get_attachment_data( $media_file['id'], $html_info['post_url'], $width, $height );
978 - }
979 - }
980 - }
981 -
982 - return $images;
276 + public static function determine_thumbnail_size_for_photon( $width, $height ) {
277 + _deprecated_function( __METHOD__, '15.7', 'Automattic\Jetpack\Post_Media\Images::determine_thumbnail_size_for_photon' );
278 + return Images::determine_thumbnail_size_for_photon( $width, $height );
983 279 }
984 280
985 281 /**
986 - * Check if a block has inner blocks.
282 + * Function to provide the maximum dimension for a thumbnail image.
283 + * Filterable via the `jetpack_post_images_max_dimension` filter.
987 284 *
988 - * @since 7.8.0
285 + * @deprecated 15.7 Use Automattic\Jetpack\Post_Media\Images::get_max_thumbnail_dimension() instead.
989 286 *
990 - * @param array $block Block and its attributes.
991 - *
992 - * @return bool
287 + * @return int The maximum dimension for a thumbnail image.
993 288 */
994 - private static function is_nested_block( $block ) {
995 - if ( ! empty( $block['innerBlocks'] ) ) {
996 - return true;
997 - }
998 -
999 - return false;
289 + public static function get_max_thumbnail_dimension() {
290 + _deprecated_function( __METHOD__, '15.7', 'Automattic\Jetpack\Post_Media\Images::get_max_thumbnail_dimension' );
291 + return Images::get_max_thumbnail_dimension();
1000 292 }
1001 293 }