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