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