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