PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 15.6.1
Jetpack – WP Security, Backup, Speed, & Growth v15.6.1
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 14.1.1 14.2.2 14.3.1 14.4.2 14.5.1 14.6.1 14.7.1 14.8.1 14.9.2 15.0.3 15.1.2 15.2.1 15.3.2 15.4.1 15.5.1 15.6.1 15.7.2 15.8.1 15.9.2 16.0.2 16.1.3 16.2-a.5 16.2-a.3 16.1.2 16.2-a.1 16.1.1 16.1 16.1-beta 16.1-beta.2 16.1-beta.3 16.1-a.5 16.1-a.3 16.0.1 16.1-a.1 16.0 16.0-beta 16.0-a.7 16.0-a.5 15.9.1 16.0-a.3 16.0-a.1 15.9 15.9-beta 15.9-a.7 15.9-a.5 15.9-a.3 15.9-a.1 15.8 15.8-beta 15.8-a.7 15.8-a.5 5.2.5 5.3.4 5.4.4 5.5.5 5.6.5 5.7.5 5.8.4 5.9.4 6.0.4 6.1 6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 6.2 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.3 6.3.1 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.4 6.4.1 6.4.2 6.4.3 6.4.4 6.4.5 6.4.6 6.5 6.5.1 6.5.2 6.5.3 6.5.4 6.6 6.6.1 6.6.2 6.6.3 6.6.4 6.6.5 6.7 6.7.1 6.7.2 6.7.3 6.7.4 6.8 6.8.1 6.8.2 6.8.3 6.8.4 6.8.5 6.9 6.9.1 6.9.2 6.9.3 6.9.4 7.0 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1 7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.2 7.2.1 7.2.1.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3 7.3.0.1 7.3.1 7.3.1.1 7.3.2 7.3.3 7.3.4 7.3.5 7.4 7.4.1 7.4.2 7.4.3 7.4.4 7.4.5 7.5 7.5.0.1 7.5.1 7.5.2 7.5.3 7.5.4 7.5.5 7.5.6 7.5.7 7.6 7.6.1 7.6.2 7.6.3 7.6.4 7.7 7.7.1 7.7.2 7.7.3 7.7.4 7.7.5 7.7.6 7.8 7.8.1 7.8.2 7.8.3 7.8.4 7.9 7.9.1 7.9.2 7.9.3 7.9.4 8.0 8.0.1 8.0.2 8.0.3 8.1 8.1.1 8.1.2 8.1.3 8.1.4 8.2 8.2.0.1 8.2.1 8.2.2 8.2.3 8.2.4 8.2.5 8.2.6 8.3 8.3.1 8.3.2 8.3.3 8.4 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.5 8.5.1 8.5.2 8.5.3 8.6 8.6.1 8.6.2 8.6.3 8.6.4 8.7 8.7.0.1 8.7.1 8.7.2 8.7.3 8.7.4 8.8 8.8.1 8.8.2 8.8.3 8.8.4 8.8.5 8.9 8.9.1 8.9.2 8.9.3 8.9.4 9.0 9.0.1 9.0.2 9.0.3 9.0.4 9.0.5 9.1 9.1.1 9.1.2 9.1.3 9.2 9.2.1 9.2.2 9.2.3 9.2.4 9.3 9.3.1 9.3.2 9.3.3 9.3.4 9.3.5 9.4 9.4.1 9.4.2 9.4.3 9.4.4 9.5 9.5.1 9.5.2 9.5.3 9.5.4 9.5.5 9.6 9.6.1 9.6.2 9.6.3 9.6.4 9.7 9.7.1 9.7.2 15.7-beta.2 9.7.3 15.7.1 9.8 15.8-a.1 9.8.1 15.8-a.3 9.8.2 2.0.9 9.8.3 2.1.7 9.9 2.2.10 9.9.1 2.3.10 9.9.2 2.4.7 9.9.3 2.5.5 2.6.6 2.7.5 2.8.5 2.9.6 3.0.6 3.1.5 3.2.5 3.3.6 3.4.6 3.5.6 3.6.4 3.7.5 3.8.5 3.9.10 4.0.7 4.1.4 4.2.5 4.3.5 4.4.5 4.5.3 4.6.3 4.7.4 4.8.5 4.9.3 5.0.3 5.1.4 trunk 10.0 10.0.1 10.0.2 10.1 10.1.1 10.1.2 10.2 10.2.1 10.2.2 10.2.3 10.3 10.3.1 10.3.2 10.4 10.4.1 10.4.2 10.5 10.5.1 10.5.2 10.5.3 10.6 10.6.1 10.6.2 10.7 10.7.1 10.7.2 10.8 10.8.1 10.8.2 10.9 10.9.1 10.9.2 10.9.3 11.0 11.0.1 11.0.2 11.1 11.1.1 11.1.2 11.1.3 11.1.4 11.2 11.2.1 11.2.2 11.3 11.3.1 11.3.2 11.3.3 11.3.4 11.4 11.4.1 11.4.2 11.5 11.5.1 11.5.2 11.5.3 11.6 11.6.1 11.6.2 11.7 11.7.1 11.7.2 11.7.3 11.8 11.8.3 11.8.4 11.8.5 11.8.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / class.jetpack-post-images.php
jetpack Last commit date
3rd-party 6 months ago _inc 17 hours ago css 7 months ago extensions 5 months ago images 6 months ago jetpack_vendor 17 hours ago json-endpoints 6 months ago modules 5 months ago sal 6 months ago src 6 months ago vendor 5 months ago views 9 months ago CHANGELOG.md 5 months ago LICENSE.txt 8 months ago SECURITY.md 2 years ago class-jetpack-connection-status.php 2 years ago class-jetpack-gallery-settings.php 9 months ago class-jetpack-newsletter-dashboard-widget.php 9 months ago class-jetpack-pre-connection-jitms.php 2 years ago class-jetpack-stats-dashboard-widget.php 5 months ago class-jetpack-xmlrpc-methods.php 9 months ago class.frame-nonce-preview.php 9 months ago class.jetpack-admin.php 6 months ago class.jetpack-autoupdate.php 9 months ago class.jetpack-cli.php 8 months ago class.jetpack-client-server.php 2 years ago class.jetpack-gutenberg.php 6 months ago class.jetpack-heartbeat.php 6 months ago class.jetpack-modules-list-table.php 9 months ago class.jetpack-network-sites-list-table.php 9 months ago class.jetpack-network.php 9 months ago class.jetpack-plan.php 3 years ago class.jetpack-post-images.php 6 months ago class.jetpack-twitter-cards.php 6 months ago class.jetpack-user-agent.php 2 years ago class.jetpack.php 6 months ago class.json-api-endpoints.php 7 months ago class.json-api.php 8 months ago class.photon.php 3 years ago composer.json 5 months ago enhanced-open-graph.php 6 months ago functions.compat.php 6 months ago functions.cookies.php 2 years ago functions.global.php 9 months ago functions.is-mobile.php 2 years ago functions.opengraph.php 6 months ago functions.photon.php 2 years ago jetpack.php 17 hours ago json-api-config.php 3 years ago json-endpoints.php 2 years ago load-jetpack.php 6 months ago locales.php 9 months ago readme.txt 17 hours ago unauth-file-upload.php 8 months ago uninstall.php 9 months ago wpml-config.xml 4 years ago
class.jetpack-post-images.php
1222 lines
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 /**
3 * Useful for finding an image to display alongside/in representation of a specific post.
4 *
5 * @package automattic/jetpack
6 */
7
8 use Automattic\Block_Scanner;
9 use Automattic\Jetpack\Image_CDN\Image_CDN_Core;
10
11 /**
12 * Useful for finding an image to display alongside/in representation of a specific post.
13 *
14 * Includes a few different methods, all of which return a similar-format array containing
15 * details of any images found. Everything can (should) be called statically, it's just a
16 * function-bucket. You can also call Jetpack_PostImages::get_image() to cycle through all of the methods until
17 * one of them finds something useful.
18 *
19 * This file is included verbatim in Jetpack
20 */
21 class Jetpack_PostImages {
22 /**
23 * If a slideshow is embedded within a post, then parse out the images involved and return them
24 *
25 * @param int $post_id Post ID.
26 * @param int $width Image width.
27 * @param int $height Image height.
28 * @return array Images.
29 */
30 public static function from_slideshow( $post_id, $width = 200, $height = 200 ) {
31 $images = array();
32
33 $post = get_post( $post_id );
34 if ( ! $post instanceof WP_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'] ?? null;
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;
106 }
107
108 /**
109 * Filtering out images with broken URL from galleries.
110 *
111 * @param array $galleries Galleries.
112 * @return array $filtered_galleries
113 */
114 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;
138 }
139
140 /**
141 * If a gallery is detected, then get all the images from it.
142 *
143 * @param int $post_id Post ID.
144 * @param int $width Minimum image width to consider.
145 * @param int $height Minimum image height to consider.
146 * @return array Images.
147 */
148 public static function from_gallery( $post_id, $width = 200, $height = 200 ) {
149 $images = array();
150
151 $post = get_post( $post_id );
152 if ( ! $post instanceof WP_Post ) {
153 return $images;
154 }
155
156 if ( ! empty( $post->post_password ) ) {
157 return $images;
158 }
159 add_filter( 'get_post_galleries', array( __CLASS__, 'filter_gallery_urls' ), 999999 );
160
161 $permalink = get_permalink( $post->ID );
162
163 /**
164 * Juggle global post object because the gallery shortcode uses the
165 * global object.
166 *
167 * See core ticket:
168 * https://core.trac.wordpress.org/ticket/39304
169 */
170 // phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited
171 if ( isset( $GLOBALS['post'] ) ) {
172 $juggle_post = $GLOBALS['post'];
173 $GLOBALS['post'] = $post;
174 $galleries = get_post_galleries( $post->ID, false );
175 $GLOBALS['post'] = $juggle_post;
176 } else {
177 $GLOBALS['post'] = $post;
178 $galleries = get_post_galleries( $post->ID, false );
179 unset( $GLOBALS['post'] );
180 }
181 // phpcs:enable WordPress.WP.GlobalVariablesOverride.Prohibited
182
183 foreach ( $galleries as $gallery ) {
184 if ( ! empty( $gallery['ids'] ) ) {
185 $image_ids = explode( ',', $gallery['ids'] );
186 $image_size = isset( $gallery['size'] ) ? $gallery['size'] : 'thumbnail';
187 foreach ( $image_ids as $image_id ) {
188 $image = wp_get_attachment_image_src( $image_id, $image_size );
189 $meta = wp_get_attachment_metadata( $image_id );
190
191 if ( isset( $gallery['type'] ) && 'slideshow' === $gallery['type'] ) {
192 // Must be larger than 200x200 (or user-specified).
193 if ( ! isset( $meta['width'] ) || $meta['width'] < $width ) {
194 continue;
195 }
196 if ( ! isset( $meta['height'] ) || $meta['height'] < $height ) {
197 continue;
198 }
199 }
200
201 if ( ! empty( $image[0] ) ) {
202 list( $raw_src ) = explode( '?', $image[0] ); // 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 'src_width' => $meta['width'] ?? 0,
210 'src_height' => $meta['height'] ?? 0,
211 'href' => $permalink,
212 'alt_text' => self::get_alt_text( $image_id ),
213 );
214 }
215 }
216 } elseif ( ! empty( $gallery['src'] ) ) {
217 foreach ( $gallery['src'] as $src ) {
218 list( $raw_src ) = explode( '?', $src ); // pull off any Query string (?w=250).
219 $raw_src = wp_specialchars_decode( $raw_src ); // rawify it.
220 $raw_src = esc_url_raw( $raw_src ); // clean it.
221 $images[] = array(
222 'type' => 'image',
223 'from' => 'gallery',
224 'src' => $raw_src,
225 'href' => $permalink,
226 );
227 }
228 }
229 }
230
231 return $images;
232 }
233
234 /**
235 * Get attachment images for a specified post and return them. Also make sure
236 * their dimensions are at or above a required minimum.
237 *
238 * @param int $post_id The post ID to check.
239 * @param int $width Image width.
240 * @param int $height Image height.
241 * @return array Containing details of the image, or empty array if none.
242 */
243 public static function from_attachment( $post_id, $width = 200, $height = 200 ) {
244 $images = array();
245
246 $post = get_post( $post_id );
247 if ( ! $post instanceof WP_Post ) {
248 return $images;
249 }
250
251 if ( ! empty( $post->post_password ) ) {
252 return $images;
253 }
254
255 $post_images = get_posts(
256 array(
257 'post_parent' => $post_id, // Must be children of post.
258 'numberposts' => 5, // No more than 5.
259 'post_type' => 'attachment', // Must be attachments.
260 'post_mime_type' => 'image', // Must be images.
261 'suppress_filters' => false,
262 )
263 );
264
265 if ( ! $post_images ) {
266 return $images;
267 }
268
269 $permalink = get_permalink( $post_id );
270
271 foreach ( $post_images as $post_image ) {
272 $current_image = self::get_attachment_data( $post_image->ID, $permalink, $width, $height );
273 if ( false !== $current_image ) {
274 $images[] = $current_image;
275 }
276 }
277
278 /*
279 * We only want to pass back attached images that were actually inserted.
280 * We can load up all the images found in the HTML source and then
281 * compare URLs to see if an image is attached AND inserted.
282 */
283 $html_images = self::from_html( $post_id );
284 $inserted_images = array();
285
286 foreach ( $html_images as $html_image ) {
287 $src = wp_parse_url( $html_image['src'] );
288 if ( ! $src || empty( $src['path'] ) ) {
289 continue;
290 }
291
292 // strip off any query strings from src.
293 if ( ! empty( $src['scheme'] ) && ! empty( $src['host'] ) ) {
294 $inserted_images[] = $src['scheme'] . '://' . $src['host'] . $src['path'];
295 } elseif ( ! empty( $src['host'] ) ) {
296 $inserted_images[] = set_url_scheme( 'http://' . $src['host'] . $src['path'] );
297 } else {
298 $inserted_images[] = site_url( '/' ) . $src['path'];
299 }
300 }
301 foreach ( $images as $i => $image ) {
302 if ( ! in_array( $image['src'], $inserted_images, true ) ) {
303 unset( $images[ $i ] );
304 }
305 }
306
307 return $images;
308 }
309
310 /**
311 * Check if a Featured Image is set for this post, and return it in a similar
312 * format to the other images?_from_*() methods.
313 *
314 * @param int $post_id The post ID to check.
315 * @param int $width Image width.
316 * @param int $height Image height.
317 * @return array containing details of the Featured Image, or empty array if none.
318 */
319 public static function from_thumbnail( $post_id, $width = 200, $height = 200 ) {
320 $images = array();
321
322 $post = get_post( $post_id );
323 if ( ! $post instanceof WP_Post ) {
324 return $images;
325 }
326
327 if ( ! empty( $post->post_password ) ) {
328 return $images;
329 }
330
331 if ( 'attachment' === get_post_type( $post ) && wp_attachment_is_image( $post ) ) {
332 $thumb = $post_id;
333 } else {
334 $thumb = get_post_thumbnail_id( $post );
335 }
336
337 if ( $thumb ) {
338 $meta = wp_get_attachment_metadata( $thumb );
339 // Must be larger than requested minimums.
340 if ( ! isset( $meta['width'] ) || $meta['width'] < $width ) {
341 return $images;
342 }
343 if ( ! isset( $meta['height'] ) || $meta['height'] < $height ) {
344 return $images;
345 }
346 $max_dimension = self::get_max_thumbnail_dimension();
347 $too_big = ( ( ! empty( $meta['width'] ) && $meta['width'] > $max_dimension ) || ( ! empty( $meta['height'] ) && $meta['height'] > $max_dimension ) );
348
349 if (
350 $too_big &&
351 (
352 ( method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'photon' ) ) ||
353 ( defined( 'IS_WPCOM' ) && IS_WPCOM )
354 )
355 ) {
356 $size = self::determine_thumbnail_size_for_photon( $meta['width'], $meta['height'] );
357 $photon_args = array(
358 'fit' => $size['width'] . ',' . $size['height'],
359 );
360 $img_src = array( Image_CDN_Core::cdn_url( wp_get_attachment_url( $thumb ), $photon_args ), $size['width'], $size['height'], true ); // Match the signature of wp_get_attachment_image_src
361 } else {
362 $img_src = wp_get_attachment_image_src( $thumb, 'full' );
363 }
364 if ( ! is_array( $img_src ) ) {
365 // If wp_get_attachment_image_src returns false but we know that there should be an image that could be used.
366 // we try a bit harder and user the data that we have.
367 $thumb_post_data = get_post( $thumb );
368 $img_src = array( $thumb_post_data->guid ?? null, $meta['width'], $meta['height'] );
369 }
370
371 // Let's try to use the postmeta if we can, since it seems to be
372 // more reliable
373 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
374 $featured_image = get_post_meta( $post->ID, '_jetpack_featured_image', false );
375 if ( $featured_image ) {
376 $url = $featured_image[0];
377 } else {
378 $url = $img_src[0];
379 }
380 } else {
381 $url = $img_src[0];
382 }
383 $images = array(
384 array( // Other methods below all return an array of arrays.
385 'type' => 'image',
386 'from' => 'thumbnail',
387 'src' => $url,
388 'src_width' => $img_src[1],
389 'src_height' => $img_src[2],
390 'href' => get_permalink( $thumb ),
391 'alt_text' => self::get_alt_text( $thumb ),
392 ),
393 );
394
395 }
396
397 if ( empty( $images ) && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
398 $meta_thumbnail = get_post_meta( $post_id, '_jetpack_post_thumbnail', true );
399 if ( ! empty( $meta_thumbnail ) ) {
400 if ( ! isset( $meta_thumbnail['width'] ) || $meta_thumbnail['width'] < $width ) {
401 return $images;
402 }
403
404 if ( ! isset( $meta_thumbnail['height'] ) || $meta_thumbnail['height'] < $height ) {
405 return $images;
406 }
407
408 $images = array(
409 array( // Other methods below all return an array of arrays.
410 'type' => 'image',
411 'from' => 'thumbnail',
412 'src' => $meta_thumbnail['URL'],
413 'src_width' => $meta_thumbnail['width'],
414 'src_height' => $meta_thumbnail['height'],
415 'href' => $meta_thumbnail['URL'],
416 'alt_text' => self::get_alt_text( $thumb ),
417 ),
418 );
419 }
420 }
421
422 return $images;
423 }
424
425 /**
426 * Get images from Gutenberg Image blocks.
427 *
428 * @since 6.9.0
429 * @since 14.8 Updated to use Block_Delimiter for improved performance.
430 * @since 14.9 Updated to use Block_Scanner for improved performance.
431 *
432 * @param mixed $html_or_id The HTML string to parse for images, or a post id.
433 * @param int $width Minimum Image width.
434 * @param int $height Minimum Image height.
435 */
436 public static function from_blocks( $html_or_id, $width = 200, $height = 200 ) {
437 $images = array();
438
439 $html_info = self::get_post_html( $html_or_id );
440
441 if ( empty( $html_info['html'] ) ) {
442 return $images;
443 }
444
445 $scanner = Block_Scanner::create( $html_info['html'] );
446 if ( ! $scanner ) {
447 return $images;
448 }
449
450 /*
451 * Use Block_Scanner to parse our post content HTML,
452 * and find all the block delimiters for supported blocks,
453 * whether they're parent or nested blocks.
454 */
455 $supported_blocks = array(
456 'core/image',
457 'core/media-text',
458 'core/gallery',
459 'jetpack/tiled-gallery',
460 'jetpack/slideshow',
461 'jetpack/story',
462 );
463
464 while ( $scanner->next_delimiter() ) {
465 $type = $scanner->get_delimiter_type();
466 // Only process opening delimiters for supported block types.
467 if ( Block_Scanner::OPENER !== $type ) {
468 continue;
469 }
470
471 $block_type = $scanner->get_block_type();
472 $is_supported_block = in_array( $block_type, $supported_blocks, true );
473 if ( ! $is_supported_block ) {
474 continue;
475 }
476
477 $attributes = $scanner->allocate_and_return_parsed_attributes() ?? array();
478 $block_images = self::get_images_from_block_attributes( $block_type, $attributes, $html_info, $width, $height );
479
480 if ( ! empty( $block_images ) ) {
481 $images = array_merge( $images, $block_images );
482 }
483 }
484
485 /**
486 * Returning a filtered array because get_attachment_data returns false
487 * for unsuccessful attempts.
488 */
489 return array_filter( $images );
490 }
491
492 /**
493 * Extract images from block attributes based on block type.
494 *
495 * @since 14.8
496 *
497 * @param string $block_type Block type name.
498 * @param array $attributes Block attributes.
499 * @param array $html_info Info about the post where the block is found.
500 * @param int $width Desired image width.
501 * @param int $height Desired image height.
502 *
503 * @return array Array of images found.
504 */
505 private static function get_images_from_block_attributes( $block_type, $attributes, $html_info, $width, $height ) {
506 $images = array();
507
508 // Skip blocks marked with the jetpack-ignore-thumbnail CSS class
509 // (set via the block editor's "Advanced > Additional CSS class" panel).
510 $class_name = $attributes['className'] ?? '';
511 if ( str_contains( $class_name, 'jetpack-ignore-thumbnail' ) ) {
512 return $images;
513 }
514
515 switch ( $block_type ) {
516 case 'core/image':
517 case 'core/media-text':
518 $id_key = 'core/image' === $block_type ? 'id' : 'mediaId';
519 if ( ! empty( $attributes[ $id_key ] ) ) {
520 $image = self::get_attachment_data( $attributes[ $id_key ], $html_info['post_url'], $width, $height );
521 if ( false !== $image ) {
522 /** This filter is documented in class.jetpack-post-images.php */
523 if ( apply_filters( 'jetpack_postimages_exclude_image', false, $image ) ) {
524 break;
525 }
526 $images[] = $image;
527 }
528 }
529 break;
530
531 case 'core/gallery':
532 case 'jetpack/tiled-gallery':
533 case 'jetpack/slideshow':
534 if ( ! empty( $attributes['ids'] ) && is_array( $attributes['ids'] ) ) {
535 foreach ( $attributes['ids'] as $img_id ) {
536 $image = self::get_attachment_data( $img_id, $html_info['post_url'], $width, $height );
537 if ( false !== $image ) {
538 /** This filter is documented in class.jetpack-post-images.php */
539 if ( apply_filters( 'jetpack_postimages_exclude_image', false, $image ) ) {
540 continue;
541 }
542 $images[] = $image;
543 }
544 }
545 }
546 break;
547
548 case 'jetpack/story':
549 if ( ! empty( $attributes['mediaFiles'] ) && is_array( $attributes['mediaFiles'] ) ) {
550 foreach ( $attributes['mediaFiles'] as $media_file ) {
551 if ( ! empty( $media_file['id'] ) ) {
552 $image = self::get_attachment_data( $media_file['id'], $html_info['post_url'], $width, $height );
553 if ( false !== $image ) {
554 /** This filter is documented in class.jetpack-post-images.php */
555 if ( apply_filters( 'jetpack_postimages_exclude_image', false, $image ) ) {
556 continue;
557 }
558 $images[] = $image;
559 }
560 }
561 }
562 }
563 break;
564 }
565
566 return $images;
567 }
568
569 /**
570 * Very raw -- just parse the HTML and pull out any/all img tags and return their src
571 *
572 * @param mixed $html_or_id The HTML string to parse for images, or a post id.
573 * @param int $width Minimum Image width.
574 * @param int $height Minimum Image height.
575 *
576 * @uses DOMDocument
577 *
578 * @return array containing images
579 */
580 public static function from_html( $html_or_id, $width = 200, $height = 200 ) {
581 $images = array();
582
583 $html_info = self::get_post_html( $html_or_id );
584
585 if ( empty( $html_info['html'] ) ) {
586 return $images;
587 }
588
589 // Do not go any further if DOMDocument is disabled on the server.
590 if ( ! class_exists( 'DOMDocument' ) ) {
591 return $images;
592 }
593
594 // Let's grab all image tags from the HTML.
595 $dom_doc = new DOMDocument();
596
597 // DOMDocument defaults to ISO-8859 because we're loading only the post content, without head tag.
598 // Fix: Enforce encoding with meta tag.
599 $charset = get_option( 'blog_charset' );
600 if ( empty( $charset ) || ! preg_match( '/^[a-zA-Z0-9_-]+$/', $charset ) ) {
601 $charset = 'UTF-8';
602 }
603 $html_prefix = sprintf( '<meta http-equiv="Content-Type" content="text/html; charset=%s">', esc_attr( $charset ) );
604
605 // The @ is not enough to suppress errors when dealing with libxml,
606 // we have to tell it directly how we want to handle errors.
607 libxml_use_internal_errors( true );
608 @$dom_doc->loadHTML( $html_prefix . $html_info['html'] ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
609 libxml_use_internal_errors( false );
610
611 $image_tags = $dom_doc->getElementsByTagName( 'img' );
612
613 // For each image Tag, make sure it can be added to the $images array, and add it.
614 foreach ( $image_tags as $image_tag ) {
615 $img_src = $image_tag->getAttribute( 'src' );
616
617 if ( empty( $img_src ) ) {
618 continue;
619 }
620
621 // Do not grab smiley images that were automatically created by WP when entering text smilies.
622 if ( stripos( $img_src, '/smilies/' ) ) {
623 continue;
624 }
625
626 // Do not grab Gravatar images.
627 if ( stripos( $img_src, 'gravatar.com' ) ) {
628 continue;
629 }
630
631 // Allow users to exclude images by adding a CSS class to the img tag.
632 if ( str_contains( $image_tag->getAttribute( 'class' ) ?? '', 'jetpack-ignore-thumbnail' ) ) {
633 continue;
634 }
635
636 // First try to get the width and height from the img attributes, but if they are not set, check to see if they are specified in the url. WordPress automatically names files like foo-1024x768.jpg during the upload process
637 $width = (int) $image_tag->getAttribute( 'width' );
638 $height = (int) $image_tag->getAttribute( 'height' );
639 if ( 0 === $width && 0 === $height ) {
640 preg_match( '/-([0-9]{1,5})x([0-9]{1,5})\.(?:jpg|jpeg|png|gif|webp)$/i', $img_src, $matches );
641 if ( ! empty( $matches[1] ) ) {
642 $width = (int) $matches[1];
643 }
644 if ( ! empty( $matches[2] ) ) {
645 $height = (int) $matches[2];
646 }
647 }
648 // If width and height are still 0, try to get the id of the image from the class, e.g. wp-image-1234
649 if ( 0 === $width && 0 === $height ) {
650
651 preg_match( '/wp-image-([0-9]+)/', $image_tag->getAttribute( 'class' ), $matches );
652 if ( ! empty( $matches[1] ) ) {
653 $attachment_id = $matches[1];
654 $meta = wp_get_attachment_metadata( $attachment_id );
655 $height = $meta['height'] ?? 0;
656 $width = $meta['width'] ?? 0;
657 }
658 }
659
660 $meta = array(
661 'width' => $width,
662 'height' => $height,
663 'alt_text' => $image_tag->getAttribute( 'alt' ),
664 );
665
666 /**
667 * Filters the switch to ignore minimum image size requirements. Can be used
668 * to add custom logic to image dimensions, like only enforcing one of the dimensions,
669 * or disabling it entirely.
670 *
671 * @since 6.4.0
672 *
673 * @param bool $ignore Should the image dimensions be ignored?
674 * @param array $meta Array containing image dimensions parsed from the markup.
675 */
676 $ignore_dimensions = apply_filters( 'jetpack_postimages_ignore_minimum_dimensions', false, $meta );
677
678 // Must be larger than 200x200 (or user-specified).
679 if (
680 ! $ignore_dimensions
681 && (
682 empty( $meta['width'] )
683 || empty( $meta['height'] )
684 || $meta['width'] < $width
685 || $meta['height'] < $height
686 )
687 ) {
688 continue;
689 }
690
691 $image = array(
692 'type' => 'image',
693 'from' => 'html',
694 'src' => $img_src,
695 'src_width' => $meta['width'],
696 'src_height' => $meta['height'],
697 'href' => $html_info['post_url'],
698 );
699 if ( ! empty( $meta['alt_text'] ) ) {
700 $image['alt_text'] = $meta['alt_text'];
701 }
702
703 /**
704 * Filters whether to exclude a specific image from post image discovery.
705 *
706 * This filter runs inside Jetpack_PostImages, which powers image selection
707 * for Related Posts, Open Graph tags, and other features. Returning a truthy
708 * value causes the image to be skipped.
709 *
710 * @since 15.6
711 *
712 * @param bool $exclude Whether to exclude the image. Default false.
713 * @param array $image {
714 * Image data.
715 *
716 * @type string $type The type of the image (always 'image').
717 * @type string $from The source method ('html', 'attachment', 'thumbnail', etc.).
718 * @type string $src The image URL.
719 * @type int $src_width The image width in pixels.
720 * @type int $src_height The image height in pixels.
721 * @type string $href The permalink of the post containing the image.
722 * @type string $alt_text The image alt text, if available.
723 * }
724 */
725 if ( apply_filters( 'jetpack_postimages_exclude_image', false, $image ) ) {
726 continue;
727 }
728
729 $images[] = $image;
730 }
731 return $images;
732 }
733
734 /**
735 * Data from blavatar.
736 *
737 * @param int $post_id The post ID to check.
738 * @param int $size Size.
739 * @return array containing details of the image, or empty array if none.
740 */
741 public static function from_blavatar( $post_id, $size = 96 ) {
742
743 $permalink = get_permalink( $post_id );
744
745 if ( function_exists( 'blavatar_domain' ) && function_exists( 'blavatar_exists' ) && function_exists( 'blavatar_url' ) ) {
746 $domain = blavatar_domain( $permalink );
747
748 if ( ! blavatar_exists( $domain ) ) {
749 return array();
750 }
751
752 $url = blavatar_url( $domain, 'img', $size );
753 } else {
754 $url = get_site_icon_url( $size );
755 if ( ! $url ) {
756 return array();
757 }
758 }
759
760 return array(
761 array(
762 'type' => 'image',
763 'from' => 'blavatar',
764 'src' => $url,
765 'src_width' => $size,
766 'src_height' => $size,
767 'href' => $permalink,
768 'alt_text' => '',
769 ),
770 );
771 }
772
773 /**
774 * Gets a post image from the author avatar.
775 *
776 * @param int $post_id The post ID to check.
777 * @param int $size The size of the avatar to get.
778 * @param string $default The default image to use.
779 * @return array containing details of the image, or empty array if none.
780 */
781 public static function from_gravatar( $post_id, $size = 96, $default = false ) {
782 $post = get_post( $post_id );
783 if ( ! $post instanceof WP_Post ) {
784 return array();
785 }
786
787 $permalink = get_permalink( $post_id );
788
789 if ( function_exists( 'wpcom_get_avatar_url' ) ) {
790 $url = wpcom_get_avatar_url( $post->post_author, $size, $default, true );
791 if ( $url && is_array( $url ) ) {
792 $url = $url[0];
793 }
794 } else {
795 $url = get_avatar_url(
796 $post->post_author,
797 array(
798 'size' => $size,
799 'default' => $default,
800 )
801 );
802 }
803
804 return array(
805 array(
806 'type' => 'image',
807 'from' => 'gravatar',
808 'src' => $url,
809 'src_width' => $size,
810 'src_height' => $size,
811 'href' => $permalink,
812 'alt_text' => '',
813 ),
814 );
815 }
816
817 /**
818 * Run through the different methods that we have available to try to find a single good
819 * display image for this post.
820 *
821 * @param int $post_id Post ID.
822 * @param array $args Other arguments (currently width and height required for images where possible to determine).
823 * @return array|null containing details of the best image to be used, or null if no image is found.
824 */
825 public static function get_image( $post_id, $args = array() ) {
826 $image = null;
827
828 /**
829 * Fires before we find a single good image for a specific post.
830 *
831 * @since 2.2.0
832 *
833 * @param int $post_id Post ID.
834 */
835 do_action( 'jetpack_postimages_pre_get_image', $post_id );
836 $media = self::get_images( $post_id, $args );
837
838 if ( is_array( $media ) ) {
839 foreach ( $media as $item ) {
840 if ( 'image' === $item['type'] ) {
841 $image = $item;
842 break;
843 }
844 }
845 }
846
847 /**
848 * Fires after we find a single good image for a specific post.
849 *
850 * @since 2.2.0
851 *
852 * @param int $post_id Post ID.
853 */
854 do_action( 'jetpack_postimages_post_get_image', $post_id );
855
856 return $image;
857 }
858
859 /**
860 * Get an array containing a collection of possible images for this post, stopping once we hit a method
861 * that returns something useful.
862 *
863 * @param int $post_id Post ID.
864 * @param array $args Optional args, see defaults list for details.
865 * @return array containing images that would be good for representing this post
866 */
867 public static function get_images( $post_id, $args = array() ) {
868 // Figure out which image to attach to this post.
869 $media = array();
870
871 /**
872 * Filters the array of images that would be good for a specific post.
873 * This filter is applied before options ($args) filter the original array.
874 *
875 * @since 2.0.0
876 *
877 * @param array $media Array of images that would be good for a specific post.
878 * @param int $post_id Post ID.
879 * @param array $args Array of options to get images.
880 */
881 $media = apply_filters( 'jetpack_images_pre_get_images', $media, $post_id, $args );
882 if ( $media ) {
883 return $media;
884 }
885
886 $defaults = array(
887 'width' => 200, // Required minimum width (if possible to determine).
888 'height' => 200, // Required minimum height (if possible to determine).
889
890 'fallback_to_avatars' => false, // Optionally include Blavatar and Gravatar (in that order) in the image stack.
891 'avatar_size' => 96, // Used for both Grav and Blav.
892 'gravatar_default' => false, // Default image to use if we end up with no Gravatar.
893
894 'from_thumbnail' => true, // Use these flags to specify which methods to use to find an image.
895 'from_slideshow' => true,
896 'from_gallery' => true,
897 'from_attachment' => true,
898 'from_blocks' => true,
899 'from_html' => true,
900
901 'html_content' => '', // HTML string to pass to from_html().
902 );
903 $args = wp_parse_args( $args, $defaults );
904
905 $media = array();
906 if ( $args['from_thumbnail'] ) {
907 $media = self::from_thumbnail( $post_id, $args['width'], $args['height'] );
908 }
909 if ( ! $media && $args['from_slideshow'] ) {
910 $media = self::from_slideshow( $post_id, $args['width'], $args['height'] );
911 }
912 if ( ! $media && $args['from_gallery'] ) {
913 $media = self::from_gallery( $post_id );
914 }
915 if ( ! $media && $args['from_attachment'] ) {
916 $media = self::from_attachment( $post_id, $args['width'], $args['height'] );
917 }
918 if ( ! $media && $args['from_blocks'] ) {
919 if ( empty( $args['html_content'] ) ) {
920 $media = self::from_blocks( $post_id, $args['width'], $args['height'] ); // Use the post_id, which will load the content.
921 } else {
922 $media = self::from_blocks( $args['html_content'], $args['width'], $args['height'] ); // If html_content is provided, use that.
923 }
924 }
925 if ( ! $media && $args['from_html'] ) {
926 if ( empty( $args['html_content'] ) ) {
927 $media = self::from_html( $post_id, $args['width'], $args['height'] ); // Use the post_id, which will load the content.
928 } else {
929 $media = self::from_html( $args['html_content'], $args['width'], $args['height'] ); // If html_content is provided, use that.
930 }
931 }
932
933 if ( ! $media && $args['fallback_to_avatars'] ) {
934 $media = self::from_blavatar( $post_id, $args['avatar_size'] );
935 if ( ! $media ) {
936 $media = self::from_gravatar( $post_id, $args['avatar_size'], $args['gravatar_default'] );
937 }
938 }
939
940 /**
941 * Filters the array of images that would be good for a specific post.
942 * This filter is applied after options ($args) filter the original array.
943 *
944 * @since 2.0.0
945 *
946 * @param array $media Array of images that would be good for a specific post.
947 * @param int $post_id Post ID.
948 * @param array $args Array of options to get images.
949 */
950 return apply_filters( 'jetpack_images_get_images', $media, $post_id, $args );
951 }
952
953 /**
954 * Takes an image and base pixel dimensions and returns a srcset for the
955 * resized and cropped images, based on a fixed set of multipliers.
956 *
957 * @param array $image Array containing details of the image.
958 * @param int $base_width Base image width (i.e., the width at 1x).
959 * @param int $base_height Base image height (i.e., the height at 1x).
960 * @param bool $use_widths Whether to generate the srcset with widths instead of multipliers.
961 * @return string The srcset for the image.
962 */
963 public static function generate_cropped_srcset( $image, $base_width, $base_height, $use_widths = false ) {
964 $srcset = '';
965
966 if ( ! is_array( $image ) || empty( $image['src'] ) || empty( $image['src_width'] ) ) {
967 return $srcset;
968 }
969
970 $multipliers = array( 1, 1.5, 2, 3, 4 );
971 $srcset_values = array();
972 foreach ( $multipliers as $multiplier ) {
973 $srcset_width = (int) ( $base_width * $multiplier );
974 $srcset_height = (int) ( $base_height * $multiplier );
975 if ( $srcset_width < 1 || $srcset_width > $image['src_width'] ) {
976 break;
977 }
978
979 $srcset_url = self::fit_image_url(
980 $image['src'],
981 $srcset_width,
982 $srcset_height
983 );
984
985 if ( $use_widths ) {
986 $srcset_values[] = "{$srcset_url} {$srcset_width}w";
987 } else {
988 $srcset_values[] = "{$srcset_url} {$multiplier}x";
989 }
990 }
991
992 if ( count( $srcset_values ) > 1 ) {
993 $srcset = implode( ', ', $srcset_values );
994 }
995
996 return $srcset;
997 }
998
999 /**
1000 * Takes an image URL and pixel dimensions then returns a URL for the
1001 * resized and cropped image.
1002 *
1003 * @param string $src Image URL.
1004 * @param int $width Image width.
1005 * @param int $height Image height.
1006 * @return string Transformed image URL
1007 */
1008 public static function fit_image_url( $src, $width, $height ) {
1009 $width = (int) $width;
1010 $height = (int) $height;
1011
1012 if ( $width < 1 || $height < 1 ) {
1013 return $src;
1014 }
1015
1016 // See if we should bypass WordPress.com SaaS resizing.
1017 if ( has_filter( 'jetpack_images_fit_image_url_override' ) ) {
1018 /**
1019 * Filters the image URL used after dimensions are set by Photon.
1020 *
1021 * @since 3.3.0
1022 *
1023 * @param string $src Image URL.
1024 * @param int $width Image width.
1025 * @param int $width Image height.
1026 */
1027 return apply_filters( 'jetpack_images_fit_image_url_override', $src, $width, $height );
1028 }
1029
1030 // If WPCOM hosted image use native transformations.
1031 $img_host = wp_parse_url( $src, PHP_URL_HOST );
1032 if ( $img_host && str_ends_with( $img_host, '.files.wordpress.com' ) ) {
1033 return add_query_arg(
1034 array(
1035 'w' => $width,
1036 'h' => $height,
1037 'crop' => 1,
1038 ),
1039 set_url_scheme( $src )
1040 );
1041 }
1042
1043 // Use image cdn magic.
1044 if ( class_exists( Image_CDN_Core::class ) && method_exists( Image_CDN_Core::class, 'cdn_url' ) ) {
1045 return Image_CDN_Core::cdn_url( $src, array( 'resize' => "$width,$height" ) );
1046 }
1047
1048 // Arg... no way to resize image using WordPress.com infrastructure!
1049 return $src;
1050 }
1051
1052 /**
1053 * Get HTML from given post content.
1054 *
1055 * @since 6.9.0
1056 *
1057 * @param mixed $html_or_id The HTML string to parse for images, or a post id.
1058 *
1059 * @return array $html_info {
1060 * @type string $html Post content.
1061 * @type string $post_url Post URL.
1062 * }
1063 */
1064 public static function get_post_html( $html_or_id ) {
1065 if ( is_numeric( $html_or_id ) ) {
1066 $post = get_post( $html_or_id );
1067 if ( ! $post instanceof WP_Post || ! empty( $post->post_password ) ) {
1068 return '';
1069 }
1070
1071 $html_info = array(
1072 'html' => $post->post_content, // DO NOT apply the_content filters here, it will cause loops.
1073 'post_url' => get_permalink( $post->ID ),
1074 );
1075 } else {
1076 $html_info = array(
1077 'html' => $html_or_id,
1078 'post_url' => '',
1079 );
1080 }
1081 return $html_info;
1082 }
1083
1084 /**
1085 * Get info about a WordPress attachment.
1086 *
1087 * @since 6.9.0
1088 *
1089 * @param int $attachment_id Attachment ID.
1090 * @param string $post_url URL of the post, if we have one.
1091 * @param int $width Minimum Image width.
1092 * @param int $height Minimum Image height.
1093 * @return array|bool Image data or false if unavailable.
1094 */
1095 public static function get_attachment_data( $attachment_id, $post_url, $width, $height ) {
1096 if ( empty( $attachment_id ) ) {
1097 return false;
1098 }
1099
1100 $meta = wp_get_attachment_metadata( $attachment_id );
1101
1102 if ( empty( $meta ) ) {
1103 return false;
1104 }
1105
1106 if ( ! empty( $meta['videopress'] ) ) {
1107 // Use poster image for VideoPress videos.
1108 $url = $meta['videopress']['poster'];
1109 $meta_width = $meta['videopress']['width'];
1110 $meta_height = $meta['videopress']['height'];
1111 } elseif ( ! empty( $meta['thumb'] ) ) {
1112 // On WordPress.com, VideoPress videos have a 'thumb' property with the
1113 // poster image filename instead.
1114 $media_url = wp_get_attachment_url( $attachment_id );
1115 $url = str_replace( wp_basename( $media_url ), $meta['thumb'], $media_url );
1116 $meta_width = $meta['width'];
1117 $meta_height = $meta['height'];
1118 } elseif ( wp_attachment_is( 'video', $attachment_id ) ) {
1119 // We don't have thumbnail images for non-VideoPress videos - skip them.
1120 return false;
1121 } else {
1122 if ( ! isset( $meta['width'] ) || ! isset( $meta['height'] ) ) {
1123 return false;
1124 }
1125 $url = wp_get_attachment_url( $attachment_id );
1126 $meta_width = $meta['width'];
1127 $meta_height = $meta['height'];
1128 }
1129
1130 if ( $meta_width < $width || $meta_height < $height ) {
1131 return false;
1132 }
1133
1134 return array(
1135 'type' => 'image',
1136 'from' => 'attachment',
1137 'src' => $url,
1138 'src_width' => $meta_width,
1139 'src_height' => $meta_height,
1140 'href' => $post_url,
1141 'alt_text' => self::get_alt_text( $attachment_id ),
1142 );
1143 }
1144
1145 /**
1146 * Get the alt text for an image or other media from the Media Library.
1147 *
1148 * @since 7.1
1149 *
1150 * @param int $attachment_id The Post ID of the media.
1151 * @return string The alt text value or an empty string.
1152 */
1153 public static function get_alt_text( $attachment_id ) {
1154 return (string) get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
1155 }
1156
1157 /**
1158 * Determine the size to use with Photon for a thumbnail image.
1159 * Images larger than the maximum thumbnail dimension in either dimension are resized to maintain aspect ratio.
1160 *
1161 * @since 14.6
1162 * @see https://github.com/Automattic/jetpack/issues/40349
1163 *
1164 * @param int $width Original image width.
1165 * @param int $height Original image height.
1166 * @return array Array containing the width and height to use with Photon (null means auto).
1167 */
1168 public static function determine_thumbnail_size_for_photon( $width, $height ) {
1169 $max_dimension = self::get_max_thumbnail_dimension();
1170
1171 // If neither dimension exceeds max size, return original dimensions.
1172 if ( $width <= $max_dimension && $height <= $max_dimension ) {
1173 return array(
1174 'width' => $width,
1175 'height' => $height,
1176 );
1177 }
1178
1179 if ( $width >= $height ) {
1180 // For landscape or square images.
1181 $dims = image_resize_dimensions( $width, $height, $max_dimension, 0 ); // Height will be calculated automatically.
1182 } else {
1183 // For portrait images.
1184 $dims = image_resize_dimensions( $width, $height, 0, $max_dimension ); // Width will be calculated automatically.
1185 }
1186
1187 // $dims can be false if the image is virtually the same size as the max dimension, e.g. wp_fuzzy_number_match.
1188 if ( $dims && isset( $dims[4] ) && isset( $dims[5] ) ) {
1189 return array(
1190 'width' => $dims[4],
1191 'height' => $dims[5],
1192 );
1193 }
1194
1195 return array(
1196 'width' => $width,
1197 'height' => $height,
1198 );
1199 }
1200
1201 /**
1202 * Function to provide the maximum dimension for a thumbnail image.
1203 * Filterable via the `jetpack_post_images_max_dimension` filter.
1204 *
1205 * @since 14.6
1206 * @see https://github.com/Automattic/jetpack/issues/40349
1207 *
1208 * @return int The maximum dimension for a thumbnail image.
1209 */
1210 public static function get_max_thumbnail_dimension() {
1211 /**
1212 * Filter the maximum dimension allowed for a thumbnail image.
1213 * The default value is 1200 pixels.
1214 *
1215 * @since 14.6
1216 *
1217 * @param int $max_dimension Maximum dimension in pixels.
1218 */
1219 return (int) apply_filters( 'jetpack_post_images_max_thumbnail_dimension', 1200 );
1220 }
1221 }
1222