Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
44.81% covered (danger)
44.81%
246 / 549
15.79% covered (danger)
15.79%
3 / 19
CRAP
0.00% covered (danger)
0.00%
0 / 1
Images
44.81% covered (danger)
44.81%
246 / 549
15.79% covered (danger)
15.79%
3 / 19
7481.40
0.00% covered (danger)
0.00%
0 / 1
 from_slideshow
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 1
182
 filter_gallery_urls
73.33% covered (warning)
73.33%
11 / 15
0.00% covered (danger)
0.00%
0 / 1
7.93
 from_gallery
61.11% covered (warning)
61.11%
33 / 54
0.00% covered (danger)
0.00%
0 / 1
31.06
 from_attachment
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
210
 from_thumbnail
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 1
992
 from_blocks
92.86% covered (success)
92.86%
26 / 28
0.00% covered (danger)
0.00%
0 / 1
7.02
 get_images_from_block_attributes
94.44% covered (success)
94.44%
34 / 36
0.00% covered (danger)
0.00%
0 / 1
23.09
 from_html
87.88% covered (warning)
87.88%
58 / 66
0.00% covered (danger)
0.00%
0 / 1
25.03
 from_blavatar
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
42
 from_gravatar
88.46% covered (warning)
88.46%
23 / 26
0.00% covered (danger)
0.00%
0 / 1
5.04
 get_image
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 get_images
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
342
 generate_cropped_srcset
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
90
 fit_image_url
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
72
 get_post_html
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
4.01
 get_attachment_data
90.62% covered (success)
90.62%
29 / 32
0.00% covered (danger)
0.00%
0 / 1
10.08
 get_alt_text
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 determine_thumbnail_size_for_photon
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
7
 get_max_thumbnail_dimension
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Useful for finding an image to display alongside/in representation of a specific post.
4 *
5 * @package automattic/jetpack-post-media
6 */
7
8namespace Automattic\Jetpack\Post_Media;
9
10use Automattic\Block_Scanner;
11use Automattic\Jetpack\Image_CDN\Image_CDN_Core;
12
13/**
14 * Useful for finding an image to display alongside/in representation of a specific post.
15 *
16 * Includes a few different methods, all of which return a similar-format array containing
17 * details of any images found. Everything can (should) be called statically, it's just a
18 * function-bucket. You can also call Images::get_image() to cycle through all of the methods until
19 * one of them finds something useful.
20 */
21class Images {
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 = $gallery['size'] ?? 'thumbnail';
187                foreach ( $image_ids as $image_id ) {
188                    $image_id = (int) $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        if ( ! $post instanceof \WP_Post ) {
249            return $images;
250        }
251
252        if ( ! empty( $post->post_password ) ) {
253            return $images;
254        }
255
256        $post_images = get_posts(
257            array(
258                'post_parent'      => $post_id,   // Must be children of post.
259                'numberposts'      => 5,          // No more than 5.
260                'post_type'        => 'attachment', // Must be attachments.
261                'post_mime_type'   => 'image', // Must be images.
262                'suppress_filters' => false,
263            )
264        );
265
266        if ( ! $post_images ) {
267            return $images;
268        }
269
270        $permalink = get_permalink( $post_id );
271
272        foreach ( $post_images as $post_image ) {
273            $current_image = self::get_attachment_data( $post_image->ID, $permalink, $width, $height );
274            if ( false !== $current_image ) {
275                $images[] = $current_image;
276            }
277        }
278
279        /*
280        * We only want to pass back attached images that were actually inserted.
281        * We can load up all the images found in the HTML source and then
282        * compare URLs to see if an image is attached AND inserted.
283        */
284        $html_images     = self::from_html( $post_id );
285        $inserted_images = array();
286
287        foreach ( $html_images as $html_image ) {
288            $src = wp_parse_url( $html_image['src'] );
289            if ( ! $src || empty( $src['path'] ) ) {
290                continue;
291            }
292
293            // strip off any query strings from src.
294            if ( ! empty( $src['scheme'] ) && ! empty( $src['host'] ) ) {
295                $inserted_images[] = $src['scheme'] . '://' . $src['host'] . $src['path'];
296            } elseif ( ! empty( $src['host'] ) ) {
297                $inserted_images[] = set_url_scheme( 'http://' . $src['host'] . $src['path'] );
298            } else {
299                $inserted_images[] = site_url( '/' ) . $src['path'];
300            }
301        }
302        foreach ( $images as $i => $image ) {
303            if ( ! in_array( $image['src'], $inserted_images, true ) ) {
304                unset( $images[ $i ] );
305            }
306        }
307
308        return $images;
309    }
310
311    /**
312     * Check if a Featured Image is set for this post, and return it in a similar
313     * format to the other images?_from_*() methods.
314     *
315     * @param  int $post_id The post ID to check.
316     * @param  int $width Image width.
317     * @param  int $height Image height.
318     * @return array containing details of the Featured Image, or empty array if none.
319     */
320    public static function from_thumbnail( $post_id, $width = 200, $height = 200 ) {
321        $images = array();
322
323        $post = get_post( $post_id );
324        if ( ! $post instanceof \WP_Post ) {
325            return $images;
326        }
327
328        if ( ! empty( $post->post_password ) ) {
329            return $images;
330        }
331
332        if ( 'attachment' === get_post_type( $post ) && wp_attachment_is_image( $post ) ) {
333            $thumb = $post_id;
334        } else {
335            $thumb = get_post_thumbnail_id( $post );
336        }
337
338        if ( $thumb ) {
339            $meta = wp_get_attachment_metadata( $thumb );
340            // Must be larger than requested minimums.
341            if ( ! isset( $meta['width'] ) || $meta['width'] < $width ) {
342                return $images;
343            }
344            if ( ! isset( $meta['height'] ) || $meta['height'] < $height ) {
345                return $images;
346            }
347            $max_dimension = self::get_max_thumbnail_dimension();
348            $too_big       = ( ( ! empty( $meta['width'] ) && $meta['width'] > $max_dimension ) || ( ! empty( $meta['height'] ) && $meta['height'] > $max_dimension ) );
349
350            if (
351                $too_big &&
352                (
353                    // @phan-suppress-next-line PhanUndeclaredClassMethod, PhanUndeclaredClassReference -- Checked with method_exists().
354                    ( method_exists( 'Jetpack', 'is_module_active' ) && \Jetpack::is_module_active( 'photon' ) ) ||
355                    ( defined( 'IS_WPCOM' ) && IS_WPCOM )
356                )
357            ) {
358                $size        = self::determine_thumbnail_size_for_photon( $meta['width'], $meta['height'] );
359                $photon_args = array(
360                    'fit' => $size['width'] . ',' . $size['height'],
361                );
362                $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
363            } else {
364                $img_src = wp_get_attachment_image_src( $thumb, 'full' );
365            }
366            if ( ! is_array( $img_src ) ) {
367                // If wp_get_attachment_image_src returns false but we know that there should be an image that could be used.
368                // we try a bit harder and user the data that we have.
369                $thumb_post_data = get_post( $thumb );
370                $img_src         = array( $thumb_post_data->guid ?? null, $meta['width'], $meta['height'] );
371            }
372
373            // Let's try to use the postmeta if we can, since it seems to be
374            // more reliable
375            if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
376                $featured_image = get_post_meta( $post->ID, '_jetpack_featured_image', false );
377                if ( $featured_image ) {
378                    $url = $featured_image[0];
379                } else {
380                    $url = $img_src[0];
381                }
382            } else {
383                $url = $img_src[0];
384            }
385            $images = array(
386                array( // Other methods below all return an array of arrays.
387                    'type'       => 'image',
388                    'from'       => 'thumbnail',
389                    'src'        => $url,
390                    'src_width'  => $img_src[1],
391                    'src_height' => $img_src[2],
392                    'href'       => get_permalink( $thumb ),
393                    'alt_text'   => self::get_alt_text( $thumb ),
394                ),
395            );
396
397        }
398
399        if ( empty( $images ) && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
400            $meta_thumbnail = get_post_meta( $post_id, '_jetpack_post_thumbnail', true );
401            if ( ! empty( $meta_thumbnail ) ) {
402                if ( ! isset( $meta_thumbnail['width'] ) || $meta_thumbnail['width'] < $width ) {
403                    return $images;
404                }
405
406                if ( ! isset( $meta_thumbnail['height'] ) || $meta_thumbnail['height'] < $height ) {
407                    return $images;
408                }
409
410                $images = array(
411                    array( // Other methods below all return an array of arrays.
412                        'type'       => 'image',
413                        'from'       => 'thumbnail',
414                        'src'        => $meta_thumbnail['URL'],
415                        'src_width'  => $meta_thumbnail['width'],
416                        'src_height' => $meta_thumbnail['height'],
417                        'href'       => $meta_thumbnail['URL'],
418                        'alt_text'   => $thumb ? self::get_alt_text( $thumb ) : '',
419                    ),
420                );
421            }
422        }
423
424        return $images;
425    }
426
427    /**
428     * Get images from Gutenberg Image blocks.
429     *
430     * @since jetpack-6.9.0
431     * @since jetpack-14.8 Updated to use Block_Delimiter for improved performance.
432     * @since jetpack-14.9 Updated to use Block_Scanner for improved performance.
433     *
434     * @param mixed $html_or_id The HTML string to parse for images, or a post id.
435     * @param int   $width      Minimum Image width.
436     * @param int   $height     Minimum Image height.
437     */
438    public static function from_blocks( $html_or_id, $width = 200, $height = 200 ) {
439        $images = array();
440
441        $html_info = self::get_post_html( $html_or_id );
442
443        if ( empty( $html_info['html'] ) ) {
444            return $images;
445        }
446
447        $scanner = Block_Scanner::create( $html_info['html'] );
448        if ( ! $scanner ) {
449            return $images;
450        }
451
452        /*
453         * Use Block_Scanner to parse our post content HTML,
454         * and find all the block delimiters for supported blocks,
455         * whether they're parent or nested blocks.
456         */
457        $supported_blocks = array(
458            'core/image',
459            'core/media-text',
460            'core/gallery',
461            'jetpack/tiled-gallery',
462            'jetpack/slideshow',
463            'jetpack/story',
464        );
465
466        while ( $scanner->next_delimiter() ) {
467            $type = $scanner->get_delimiter_type();
468            // Only process opening delimiters for supported block types.
469            if ( Block_Scanner::OPENER !== $type ) {
470                continue;
471            }
472
473            $block_type         = $scanner->get_block_type();
474            $is_supported_block = in_array( $block_type, $supported_blocks, true );
475            if ( ! $is_supported_block ) {
476                continue;
477            }
478
479            $attributes   = $scanner->allocate_and_return_parsed_attributes() ?? array();
480            $block_images = self::get_images_from_block_attributes( $block_type, $attributes, $html_info, $width, $height );
481
482            if ( ! empty( $block_images ) ) {
483                $images = array_merge( $images, $block_images );
484            }
485        }
486
487        /**
488         * Returning a filtered array because get_attachment_data returns false
489         * for unsuccessful attempts.
490         */
491        return array_filter( $images );
492    }
493
494    /**
495     * Extract images from block attributes based on block type.
496     *
497     * @since jetpack-14.8
498     *
499     * @param string $block_type Block type name.
500     * @param array  $attributes Block attributes.
501     * @param array  $html_info  Info about the post where the block is found.
502     * @param int    $width      Desired image width.
503     * @param int    $height     Desired image height.
504     *
505     * @return array Array of images found.
506     */
507    private static function get_images_from_block_attributes( $block_type, $attributes, $html_info, $width, $height ) {
508        $images = array();
509
510        // Skip blocks marked with the jetpack-ignore-thumbnail CSS class
511        // (set via the block editor's "Advanced > Additional CSS class" panel).
512        $class_name = $attributes['className'] ?? '';
513        if ( str_contains( $class_name, 'jetpack-ignore-thumbnail' ) ) {
514            return $images;
515        }
516
517        switch ( $block_type ) {
518            case 'core/image':
519            case 'core/media-text':
520                $id_key = 'core/image' === $block_type ? 'id' : 'mediaId';
521                if ( ! empty( $attributes[ $id_key ] ) ) {
522                    $image = self::get_attachment_data( $attributes[ $id_key ], $html_info['post_url'], $width, $height );
523                    if ( false !== $image ) {
524                        /** This filter is documented in class-images.php */
525                        if ( apply_filters( 'jetpack_postimages_exclude_image', false, $image ) ) {
526                            break;
527                        }
528                        $images[] = $image;
529                    }
530                }
531                break;
532
533            case 'core/gallery':
534            case 'jetpack/tiled-gallery':
535            case 'jetpack/slideshow':
536                if ( ! empty( $attributes['ids'] ) && is_array( $attributes['ids'] ) ) {
537                    foreach ( $attributes['ids'] as $img_id ) {
538                        $image = self::get_attachment_data( $img_id, $html_info['post_url'], $width, $height );
539                        if ( false !== $image ) {
540                            /** This filter is documented in class-images.php */
541                            if ( apply_filters( 'jetpack_postimages_exclude_image', false, $image ) ) {
542                                continue;
543                            }
544                            $images[] = $image;
545                        }
546                    }
547                }
548                break;
549
550            case 'jetpack/story':
551                if ( ! empty( $attributes['mediaFiles'] ) && is_array( $attributes['mediaFiles'] ) ) {
552                    foreach ( $attributes['mediaFiles'] as $media_file ) {
553                        if ( ! empty( $media_file['id'] ) ) {
554                            $image = self::get_attachment_data( $media_file['id'], $html_info['post_url'], $width, $height );
555                            if ( false !== $image ) {
556                                /** This filter is documented in class-images.php */
557                                if ( apply_filters( 'jetpack_postimages_exclude_image', false, $image ) ) {
558                                    continue;
559                                }
560                                $images[] = $image;
561                            }
562                        }
563                    }
564                }
565                break;
566        }
567
568        return $images;
569    }
570
571    /**
572     * Very raw -- just parse the HTML and pull out any/all img tags and return their src
573     *
574     * @param mixed $html_or_id The HTML string to parse for images, or a post id.
575     * @param int   $width      Minimum Image width.
576     * @param int   $height     Minimum Image height.
577     *
578     * @uses DOMDocument
579     *
580     * @return array containing images
581     */
582    public static function from_html( $html_or_id, $width = 200, $height = 200 ) {
583        $images = array();
584
585        $html_info = self::get_post_html( $html_or_id );
586
587        if ( empty( $html_info['html'] ) ) {
588            return $images;
589        }
590
591        // Do not go any further if DOMDocument is disabled on the server.
592        if ( ! class_exists( 'DOMDocument' ) ) {
593            return $images;
594        }
595
596        // Let's grab all image tags from the HTML.
597        $dom_doc = new \DOMDocument();
598
599        // DOMDocument defaults to ISO-8859 because we're loading only the post content, without head tag.
600        // Fix: Enforce encoding with meta tag.
601        $charset = get_option( 'blog_charset' );
602        if ( empty( $charset ) || ! preg_match( '/^[a-zA-Z0-9_-]+$/', $charset ) ) {
603            $charset = 'UTF-8';
604        }
605        $html_prefix = sprintf( '<meta http-equiv="Content-Type" content="text/html; charset=%s">', esc_attr( $charset ) );
606
607        // The @ is not enough to suppress errors when dealing with libxml,
608        // we have to tell it directly how we want to handle errors.
609        libxml_use_internal_errors( true );
610        @$dom_doc->loadHTML( $html_prefix . $html_info['html'] ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
611        libxml_use_internal_errors( false );
612
613        $image_tags = $dom_doc->getElementsByTagName( 'img' );
614
615        // For each image Tag, make sure it can be added to the $images array, and add it.
616        foreach ( $image_tags as $image_tag ) {
617            $img_src = $image_tag->getAttribute( 'src' );
618
619            if ( empty( $img_src ) ) {
620                continue;
621            }
622
623            // Do not grab smiley images that were automatically created by WP when entering text smilies.
624            if ( stripos( $img_src, '/smilies/' ) ) {
625                continue;
626            }
627
628            // Do not grab Gravatar images.
629            if ( stripos( $img_src, 'gravatar.com' ) ) {
630                continue;
631            }
632
633            // Allow users to exclude images by adding a CSS class to the img tag.
634            if ( str_contains( $image_tag->getAttribute( 'class' ) ?? '', 'jetpack-ignore-thumbnail' ) ) {
635                continue;
636            }
637
638            // 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
639            $width  = (int) $image_tag->getAttribute( 'width' );
640            $height = (int) $image_tag->getAttribute( 'height' );
641            if ( 0 === $width && 0 === $height ) {
642                preg_match( '/-([0-9]{1,5})x([0-9]{1,5})\.(?:jpg|jpeg|png|gif|webp)$/i', $img_src, $matches );
643                if ( ! empty( $matches[1] ) ) {
644                    $width = (int) $matches[1];
645                }
646                if ( ! empty( $matches[2] ) ) {
647                    $height = (int) $matches[2];
648                }
649            }
650            // If width and height are still 0, try to get the id of the image from the class, e.g. wp-image-1234
651            if ( 0 === $width && 0 === $height ) {
652
653                preg_match( '/wp-image-([0-9]+)/', $image_tag->getAttribute( 'class' ), $matches );
654                if ( ! empty( $matches[1] ) ) {
655                    $attachment_id = (int) $matches[1];
656                    $meta          = wp_get_attachment_metadata( $attachment_id );
657                    $height        = $meta['height'] ?? 0;
658                    $width         = $meta['width'] ?? 0;
659                }
660            }
661
662            $meta = array(
663                'width'    => $width,
664                'height'   => $height,
665                'alt_text' => $image_tag->getAttribute( 'alt' ),
666            );
667
668            /**
669             * Filters the switch to ignore minimum image size requirements. Can be used
670             * to add custom logic to image dimensions, like only enforcing one of the dimensions,
671             * or disabling it entirely.
672             *
673             * @since jetpack-6.4.0
674             *
675             * @param bool $ignore Should the image dimensions be ignored?
676             * @param array $meta Array containing image dimensions parsed from the markup.
677             */
678            $ignore_dimensions = apply_filters( 'jetpack_postimages_ignore_minimum_dimensions', false, $meta );
679
680            // Must be larger than 200x200 (or user-specified).
681            if (
682                ! $ignore_dimensions
683                && (
684                    empty( $meta['width'] )
685                    || empty( $meta['height'] )
686                    || $meta['width'] < $width
687                    || $meta['height'] < $height
688                )
689            ) {
690                continue;
691            }
692
693            $image = array(
694                'type'       => 'image',
695                'from'       => 'html',
696                'src'        => $img_src,
697                'src_width'  => $meta['width'],
698                'src_height' => $meta['height'],
699                'href'       => $html_info['post_url'],
700            );
701            if ( ! empty( $meta['alt_text'] ) ) {
702                $image['alt_text'] = $meta['alt_text'];
703            }
704
705            /**
706             * Filters whether to exclude a specific image from post image discovery.
707             *
708             * This filter runs inside the Images class, which powers image selection
709             * for Related Posts, Open Graph tags, and other features. Returning a truthy
710             * value causes the image to be skipped.
711             *
712             * @since 0.1.0
713             *
714             * @param bool  $exclude Whether to exclude the image. Default false.
715             * @param array $image   {
716             *     Image data.
717             *
718             *     @type string $type       The type of the image (always 'image').
719             *     @type string $from       The source method ('html', 'attachment', 'thumbnail', etc.).
720             *     @type string $src        The image URL.
721             *     @type int    $src_width  The image width in pixels.
722             *     @type int    $src_height The image height in pixels.
723             *     @type string $href       The permalink of the post containing the image.
724             *     @type string $alt_text   The image alt text, if available.
725             * }
726             */
727            if ( apply_filters( 'jetpack_postimages_exclude_image', false, $image ) ) {
728                continue;
729            }
730
731            $images[] = $image;
732        }
733        return $images;
734    }
735
736    /**
737     * Data from blavatar.
738     *
739     * @param    int $post_id The post ID to check.
740     * @param    int $size Size.
741     * @return array containing details of the image, or empty array if none.
742     */
743    public static function from_blavatar( $post_id, $size = 96 ) {
744
745        $permalink = get_permalink( $post_id );
746
747        if ( function_exists( 'blavatar_domain' ) && function_exists( 'blavatar_exists' ) && function_exists( 'blavatar_url' ) ) {
748            $domain = blavatar_domain( $permalink ); // @phan-suppress-current-line PhanUndeclaredFunction -- Checked with function_exists().
749
750            if ( ! blavatar_exists( $domain ) ) { // @phan-suppress-current-line PhanUndeclaredFunction -- Checked with function_exists().
751                return array();
752            }
753
754            $url = blavatar_url( $domain, 'img', $size ); // @phan-suppress-current-line PhanUndeclaredFunction -- Checked with function_exists().
755        } else {
756            $url = get_site_icon_url( $size );
757            if ( ! $url ) {
758                return array();
759            }
760        }
761
762        return array(
763            array(
764                'type'       => 'image',
765                'from'       => 'blavatar',
766                'src'        => $url,
767                'src_width'  => $size,
768                'src_height' => $size,
769                'href'       => $permalink,
770                'alt_text'   => '',
771            ),
772        );
773    }
774
775    /**
776     * Gets a post image from the author avatar.
777     *
778     * @param int          $post_id The post ID to check.
779     * @param int          $size The size of the avatar to get.
780     * @param string|false $default The default image to use.
781     * @return array containing details of the image, or empty array if none.
782     */
783    public static function from_gravatar( $post_id, $size = 96, $default = false ) {
784        $post = get_post( $post_id );
785        if ( ! $post instanceof \WP_Post ) {
786            return array();
787        }
788
789        $permalink = get_permalink( $post_id );
790
791        if ( function_exists( 'wpcom_get_avatar_url' ) ) {
792            $url = wpcom_get_avatar_url( $post->post_author, $size, $default, true ); // @phan-suppress-current-line PhanUndeclaredFunction -- Checked with function_exists().
793            if ( $url && is_array( $url ) ) {
794                $url = $url[0];
795            }
796        } else {
797            $url = get_avatar_url(
798                $post->post_author,
799                array(
800                    'size'    => $size,
801                    'default' => $default,
802                )
803            );
804        }
805
806        return array(
807            array(
808                'type'       => 'image',
809                'from'       => 'gravatar',
810                'src'        => $url,
811                'src_width'  => $size,
812                'src_height' => $size,
813                'href'       => $permalink,
814                'alt_text'   => '',
815            ),
816        );
817    }
818
819    /**
820     * Run through the different methods that we have available to try to find a single good
821     * display image for this post.
822     *
823     * @param int   $post_id Post ID.
824     * @param array $args Other arguments (currently width and height required for images where possible to determine).
825     * @return array|null containing details of the best image to be used, or null if no image is found.
826     */
827    public static function get_image( $post_id, $args = array() ) {
828        $image = null;
829
830        /**
831         * Fires before we find a single good image for a specific post.
832         *
833         * @since jetpack-2.2.0
834         *
835         * @param int $post_id Post ID.
836         */
837        do_action( 'jetpack_postimages_pre_get_image', $post_id );
838        $media = self::get_images( $post_id, $args );
839
840        if ( is_array( $media ) ) {
841            foreach ( $media as $item ) {
842                if ( 'image' === $item['type'] ) {
843                    $image = $item;
844                    break;
845                }
846            }
847        }
848
849        /**
850         * Fires after we find a single good image for a specific post.
851         *
852         * @since jetpack-2.2.0
853         *
854         * @param int $post_id Post ID.
855         */
856        do_action( 'jetpack_postimages_post_get_image', $post_id );
857
858        return $image;
859    }
860
861    /**
862     * Get an array containing a collection of possible images for this post, stopping once we hit a method
863     * that returns something useful.
864     *
865     * @param  int   $post_id Post ID.
866     * @param  array $args Optional args, see defaults list for details.
867     * @return array containing images that would be good for representing this post
868     */
869    public static function get_images( $post_id, $args = array() ) {
870        // Figure out which image to attach to this post.
871        $media = array();
872
873        /**
874         * Filters the array of images that would be good for a specific post.
875         * This filter is applied before options ($args) filter the original array.
876         *
877         * @since jetpack-2.0.0
878         *
879         * @param array $media Array of images that would be good for a specific post.
880         * @param int $post_id Post ID.
881         * @param array $args Array of options to get images.
882         */
883        $media = apply_filters( 'jetpack_images_pre_get_images', $media, $post_id, $args );
884        if ( $media ) {
885            return $media;
886        }
887
888        $defaults = array(
889            'width'               => 200, // Required minimum width (if possible to determine).
890            'height'              => 200, // Required minimum height (if possible to determine).
891
892            'fallback_to_avatars' => false, // Optionally include Blavatar and Gravatar (in that order) in the image stack.
893            'avatar_size'         => 96, // Used for both Grav and Blav.
894            'gravatar_default'    => false, // Default image to use if we end up with no Gravatar.
895
896            'from_thumbnail'      => true, // Use these flags to specify which methods to use to find an image.
897            'from_slideshow'      => true,
898            'from_gallery'        => true,
899            'from_attachment'     => true,
900            'from_blocks'         => true,
901            'from_html'           => true,
902
903            'html_content'        => '', // HTML string to pass to from_html().
904        );
905        $args     = wp_parse_args( $args, $defaults );
906
907        $media = array();
908        if ( $args['from_thumbnail'] ) {
909            $media = self::from_thumbnail( $post_id, $args['width'], $args['height'] );
910        }
911        if ( ! $media && $args['from_slideshow'] ) {
912            $media = self::from_slideshow( $post_id, $args['width'], $args['height'] );
913        }
914        if ( ! $media && $args['from_gallery'] ) {
915            $media = self::from_gallery( $post_id );
916        }
917        if ( ! $media && $args['from_attachment'] ) {
918            $media = self::from_attachment( $post_id, $args['width'], $args['height'] );
919        }
920        if ( ! $media && $args['from_blocks'] ) {
921            if ( empty( $args['html_content'] ) ) {
922                $media = self::from_blocks( $post_id, $args['width'], $args['height'] ); // Use the post_id, which will load the content.
923            } else {
924                $media = self::from_blocks( $args['html_content'], $args['width'], $args['height'] ); // If html_content is provided, use that.
925            }
926        }
927        if ( ! $media && $args['from_html'] ) {
928            if ( empty( $args['html_content'] ) ) {
929                $media = self::from_html( $post_id, $args['width'], $args['height'] ); // Use the post_id, which will load the content.
930            } else {
931                $media = self::from_html( $args['html_content'], $args['width'], $args['height'] ); // If html_content is provided, use that.
932            }
933        }
934
935        if ( ! $media && $args['fallback_to_avatars'] ) {
936            $media = self::from_blavatar( $post_id, $args['avatar_size'] );
937            if ( ! $media ) {
938                $media = self::from_gravatar( $post_id, $args['avatar_size'], $args['gravatar_default'] );
939            }
940        }
941
942        /**
943         * Filters the array of images that would be good for a specific post.
944         * This filter is applied after options ($args) filter the original array.
945         *
946         * @since jetpack-2.0.0
947         *
948         * @param array $media Array of images that would be good for a specific post.
949         * @param int $post_id Post ID.
950         * @param array $args Array of options to get images.
951         */
952        return apply_filters( 'jetpack_images_get_images', $media, $post_id, $args );
953    }
954
955    /**
956     * Takes an image and base pixel dimensions and returns a srcset for the
957     * resized and cropped images, based on a fixed set of multipliers.
958     *
959     * @param  array $image Array containing details of the image.
960     * @param  int   $base_width Base image width (i.e., the width at 1x).
961     * @param  int   $base_height Base image height (i.e., the height at 1x).
962     * @param  bool  $use_widths Whether to generate the srcset with widths instead of multipliers.
963     * @return string The srcset for the image.
964     */
965    public static function generate_cropped_srcset( $image, $base_width, $base_height, $use_widths = false ) {
966        $srcset = '';
967
968        if ( ! is_array( $image ) || empty( $image['src'] ) || empty( $image['src_width'] ) ) {
969            return $srcset;
970        }
971
972        $multipliers   = array( 1, 1.5, 2, 3, 4 );
973        $srcset_values = array();
974        foreach ( $multipliers as $multiplier ) {
975            $srcset_width  = (int) ( $base_width * $multiplier );
976            $srcset_height = (int) ( $base_height * $multiplier );
977            if ( $srcset_width < 1 || $srcset_width > $image['src_width'] ) {
978                break;
979            }
980
981            $srcset_url = self::fit_image_url(
982                $image['src'],
983                $srcset_width,
984                $srcset_height
985            );
986
987            if ( $use_widths ) {
988                $srcset_values[] = "{$srcset_url} {$srcset_width}w";
989            } else {
990                $srcset_values[] = "{$srcset_url} {$multiplier}x";
991            }
992        }
993
994        if ( count( $srcset_values ) > 1 ) {
995            $srcset = implode( ', ', $srcset_values );
996        }
997
998        return $srcset;
999    }
1000
1001    /**
1002     * Takes an image URL and pixel dimensions then returns a URL for the
1003     * resized and cropped image.
1004     *
1005     * @param  string $src Image URL.
1006     * @param  int    $width Image width.
1007     * @param  int    $height Image height.
1008     * @return string Transformed image URL
1009     */
1010    public static function fit_image_url( $src, $width, $height ) {
1011        $width  = (int) $width;
1012        $height = (int) $height;
1013
1014        if ( $width < 1 || $height < 1 ) {
1015            return $src;
1016        }
1017
1018        // See if we should bypass WordPress.com SaaS resizing.
1019        if ( has_filter( 'jetpack_images_fit_image_url_override' ) ) {
1020            /**
1021             * Filters the image URL used after dimensions are set by Photon.
1022             *
1023             * @since jetpack-3.3.0
1024             *
1025             * @param string $src Image URL.
1026             * @param int $width Image width.
1027             * @param int $width Image height.
1028             */
1029            return apply_filters( 'jetpack_images_fit_image_url_override', $src, $width, $height );
1030        }
1031
1032        // If WPCOM hosted image use native transformations.
1033        $img_host = wp_parse_url( $src, PHP_URL_HOST );
1034        if ( $img_host && str_ends_with( $img_host, '.files.wordpress.com' ) ) {
1035            return add_query_arg(
1036                array(
1037                    'w'    => $width,
1038                    'h'    => $height,
1039                    'crop' => 1,
1040                ),
1041                set_url_scheme( $src )
1042            );
1043        }
1044
1045        // Use image cdn magic.
1046        if ( class_exists( Image_CDN_Core::class ) && method_exists( Image_CDN_Core::class, 'cdn_url' ) ) {
1047            return Image_CDN_Core::cdn_url( $src, array( 'resize' => "$width,$height" ) );
1048        }
1049
1050        // Arg... no way to resize image using WordPress.com infrastructure!
1051        return $src;
1052    }
1053
1054    /**
1055     * Get HTML from given post content.
1056     *
1057     * @since jetpack-6.9.0
1058     *
1059     * @param mixed $html_or_id The HTML string to parse for images, or a post id.
1060     *
1061     * @return array $html_info {
1062     * @type string $html     Post content.
1063     * @type string $post_url Post URL.
1064     * }
1065     */
1066    public static function get_post_html( $html_or_id ) {
1067        if ( is_numeric( $html_or_id ) ) {
1068            $post = get_post( $html_or_id );
1069            if ( ! $post instanceof \WP_Post || ! empty( $post->post_password ) ) {
1070                return array();
1071            }
1072
1073            $html_info = array(
1074                'html'     => $post->post_content, // DO NOT apply the_content filters here, it will cause loops.
1075                'post_url' => get_permalink( $post->ID ),
1076            );
1077        } else {
1078            $html_info = array(
1079                'html'     => $html_or_id,
1080                'post_url' => '',
1081            );
1082        }
1083        return $html_info;
1084    }
1085
1086    /**
1087     * Get info about a WordPress attachment.
1088     *
1089     * @since jetpack-6.9.0
1090     *
1091     * @param int    $attachment_id Attachment ID.
1092     * @param string $post_url      URL of the post, if we have one.
1093     * @param int    $width         Minimum Image width.
1094     * @param int    $height        Minimum Image height.
1095     * @return array|bool           Image data or false if unavailable.
1096     */
1097    public static function get_attachment_data( $attachment_id, $post_url, $width, $height ) {
1098        if ( empty( $attachment_id ) ) {
1099            return false;
1100        }
1101
1102        $meta = wp_get_attachment_metadata( $attachment_id );
1103
1104        if ( empty( $meta ) ) {
1105            return false;
1106        }
1107
1108        if ( ! empty( $meta['videopress'] ) ) {
1109            // Use poster image for VideoPress videos.
1110            $url         = $meta['videopress']['poster'];
1111            $meta_width  = $meta['videopress']['width'];
1112            $meta_height = $meta['videopress']['height'];
1113        } elseif ( ! empty( $meta['thumb'] ) ) {
1114            // On WordPress.com, VideoPress videos have a 'thumb' property with the
1115            // poster image filename instead.
1116            $media_url   = wp_get_attachment_url( $attachment_id );
1117            $url         = str_replace( wp_basename( $media_url ), $meta['thumb'], $media_url );
1118            $meta_width  = $meta['width'];
1119            $meta_height = $meta['height'];
1120        } elseif ( wp_attachment_is( 'video', $attachment_id ) ) {
1121            // We don't have thumbnail images for non-VideoPress videos - skip them.
1122            return false;
1123        } else {
1124            if ( ! isset( $meta['width'] ) || ! isset( $meta['height'] ) ) {
1125                return false;
1126            }
1127            $url         = wp_get_attachment_url( $attachment_id );
1128            $meta_width  = $meta['width'];
1129            $meta_height = $meta['height'];
1130        }
1131
1132        if ( $meta_width < $width || $meta_height < $height ) {
1133            return false;
1134        }
1135
1136        return array(
1137            'type'       => 'image',
1138            'from'       => 'attachment',
1139            'src'        => $url,
1140            'src_width'  => $meta_width,
1141            'src_height' => $meta_height,
1142            'href'       => $post_url,
1143            'alt_text'   => self::get_alt_text( $attachment_id ),
1144        );
1145    }
1146
1147    /**
1148     * Get the alt text for an image or other media from the Media Library.
1149     *
1150     * @since jetpack-7.1
1151     *
1152     * @param int $attachment_id The Post ID of the media.
1153     * @return string The alt text value or an empty string.
1154     */
1155    public static function get_alt_text( $attachment_id ) {
1156        return (string) get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
1157    }
1158
1159    /**
1160     * Determine the size to use with Photon for a thumbnail image.
1161     * Images larger than the maximum thumbnail dimension in either dimension are resized to maintain aspect ratio.
1162     *
1163     * @since jetpack-14.6
1164     * @see https://github.com/Automattic/jetpack/issues/40349
1165     *
1166     * @param int $width Original image width.
1167     * @param int $height Original image height.
1168     * @return array Array containing the width and height to use with Photon (null means auto).
1169     */
1170    public static function determine_thumbnail_size_for_photon( $width, $height ) {
1171        $max_dimension = self::get_max_thumbnail_dimension();
1172
1173        // If neither dimension exceeds max size, return original dimensions.
1174        if ( $width <= $max_dimension && $height <= $max_dimension ) {
1175            return array(
1176                'width'  => $width,
1177                'height' => $height,
1178            );
1179        }
1180
1181        if ( $width >= $height ) {
1182            // For landscape or square images.
1183            $dims = image_resize_dimensions( $width, $height, $max_dimension, 0 ); // Height will be calculated automatically.
1184        } else {
1185            // For portrait images.
1186            $dims = image_resize_dimensions( $width, $height, 0, $max_dimension ); // Width will be calculated automatically.
1187        }
1188
1189        // $dims can be false if the image is virtually the same size as the max dimension, e.g. wp_fuzzy_number_match.
1190        if ( $dims && isset( $dims[4] ) && isset( $dims[5] ) ) {
1191            return array(
1192                'width'  => $dims[4],
1193                'height' => $dims[5],
1194            );
1195        }
1196
1197        return array(
1198            'width'  => $width,
1199            'height' => $height,
1200        );
1201    }
1202
1203    /**
1204     * Function to provide the maximum dimension for a thumbnail image.
1205     * Filterable via the `jetpack_post_images_max_dimension` filter.
1206     *
1207     * @since jetpack-14.6
1208     * @see https://github.com/Automattic/jetpack/issues/40349
1209     *
1210     * @return int The maximum dimension for a thumbnail image.
1211     */
1212    public static function get_max_thumbnail_dimension() {
1213        /**
1214         * Filter the maximum dimension allowed for a thumbnail image.
1215         * The default value is 1200 pixels.
1216         *
1217         * @since jetpack-14.6
1218         *
1219         * @param int $max_dimension Maximum dimension in pixels.
1220         */
1221        return (int) apply_filters( 'jetpack_post_images_max_thumbnail_dimension', 1200 );
1222    }
1223}