<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
* Useful for finding an image to display alongside/in representation of a specific post.
* @package automattic/jetpack
use Automattic\Block_Scanner;
use Automattic\Jetpack\Image_CDN\Image_CDN_Core;
* Useful for finding an image to display alongside/in representation of a specific post.
* Includes a few different methods, all of which return a similar-format array containing
* details of any images found. Everything can (should) be called statically, it's just a
* function-bucket. You can also call Jetpack_PostImages::get_image() to cycle through all of the methods until
* one of them finds something useful.
* This file is included verbatim in Jetpack
class Jetpack_PostImages {
* If a slideshow is embedded within a post, then parse out the images involved and return them
* @param int $post_id Post ID.
* @param int $width Image width.
* @param int $height Image height.
public static function from_slideshow( $post_id, $width = 200, $height = 200 ) {
$post = get_post( $post_id );
if ( ! empty( $post->post_password ) ) {
if ( false === has_shortcode( $post->post_content, 'slideshow' ) ) {
return $images; // no slideshow - bail.
$permalink = get_permalink( $post->ID );
// Mechanic: Somebody set us up the bomb.
$old_post = $GLOBALS['post'] ?? null;
$GLOBALS['post'] = $post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$old_shortcodes = $GLOBALS['shortcode_tags'];
$GLOBALS['shortcode_tags'] = array( 'slideshow' => $old_shortcodes['slideshow'] ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
// Find all the slideshows.
preg_match_all( '/' . get_shortcode_regex() . '/sx', $post->post_content, $slideshow_matches, PREG_SET_ORDER );
ob_start(); // The slideshow shortcode handler calls wp_print_scripts and wp_print_styles... not too happy about that.
foreach ( $slideshow_matches as $slideshow_match ) {
$slideshow = do_shortcode_tag( $slideshow_match );
$pos = stripos( $slideshow, 'jetpack-slideshow' );
if ( false === $pos ) { // must be something wrong - or we changed the output format in which case none of the following will work.
$start = strpos( $slideshow, '[', $pos );
$end = strpos( $slideshow, ']', $start );
$post_images = json_decode( wp_specialchars_decode( str_replace( "'", '"', substr( $slideshow, $start, $end - $start + 1 ) ), ENT_QUOTES ) ); // parse via JSON
// If the JSON didn't decode don't try and act on it.
if ( is_array( $post_images ) ) {
foreach ( $post_images as $post_image ) {
$post_image_id = absint( $post_image->id );
if ( ! $post_image_id ) {
$meta = wp_get_attachment_metadata( $post_image_id );
// Must be larger than 200x200 (or user-specified).
if ( ! isset( $meta['width'] ) || $meta['width'] < $width ) {
if ( ! isset( $meta['height'] ) || $meta['height'] < $height ) {
$url = wp_get_attachment_url( $post_image_id );
'src_width' => $meta['width'],
'src_height' => $meta['height'],
// Operator: Main screen turn on.
$GLOBALS['shortcode_tags'] = $old_shortcodes; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$GLOBALS['post'] = $old_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
* Filtering out images with broken URL from galleries.
* @param array $galleries Galleries.
* @return array $filtered_galleries
public static function filter_gallery_urls( $galleries ) {
$filtered_galleries = array();
foreach ( $galleries as $this_gallery ) {
if ( ! isset( $this_gallery['src'] ) ) {
$ids = isset( $this_gallery['ids'] ) ? explode( ',', $this_gallery['ids'] ) : array();
// Make sure 'src' array isn't associative and has no holes.
$this_gallery['src'] = array_values( $this_gallery['src'] );
foreach ( $this_gallery['src'] as $idx => $src_url ) {
if ( filter_var( $src_url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED ) === false ) {
unset( $this_gallery['src'][ $idx ] );
if ( isset( $this_gallery['ids'] ) ) {
$this_gallery['ids'] = implode( ',', $ids );
// Remove any holes we introduced.
$this_gallery['src'] = array_values( $this_gallery['src'] );
$filtered_galleries[] = $this_gallery;
return $filtered_galleries;
* If a gallery is detected, then get all the images from it.
* @param int $post_id Post ID.
* @param int $width Minimum image width to consider.
* @param int $height Minimum image height to consider.
public static function from_gallery( $post_id, $width = 200, $height = 200 ) {
$post = get_post( $post_id );
if ( ! empty( $post->post_password ) ) {
add_filter( 'get_post_galleries', array( __CLASS__, 'filter_gallery_urls' ), 999999 );
$permalink = get_permalink( $post->ID );
* Juggle global post object because the gallery shortcode uses the
* https://core.trac.wordpress.org/ticket/39304
// phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited
if ( isset( $GLOBALS['post'] ) ) {
$juggle_post = $GLOBALS['post'];
$GLOBALS['post'] = $post;
$galleries = get_post_galleries( $post->ID, false );
$GLOBALS['post'] = $juggle_post;
$GLOBALS['post'] = $post;
$galleries = get_post_galleries( $post->ID, false );
unset( $GLOBALS['post'] );
// phpcs:enable WordPress.WP.GlobalVariablesOverride.Prohibited
foreach ( $galleries as $gallery ) {
if ( ! empty( $gallery['ids'] ) ) {
$image_ids = explode( ',', $gallery['ids'] );
$image_size = isset( $gallery['size'] ) ? $gallery['size'] : 'thumbnail';
foreach ( $image_ids as $image_id ) {
$image = wp_get_attachment_image_src( $image_id, $image_size );
$meta = wp_get_attachment_metadata( $image_id );
if ( isset( $gallery['type'] ) && 'slideshow' === $gallery['type'] ) {
// Must be larger than 200x200 (or user-specified).
if ( ! isset( $meta['width'] ) || $meta['width'] < $width ) {
if ( ! isset( $meta['height'] ) || $meta['height'] < $height ) {
if ( ! empty( $image[0] ) ) {
list( $raw_src ) = explode( '?', $image[0] ); // pull off any Query string (?w=250).
$raw_src = wp_specialchars_decode( $raw_src ); // rawify it.
$raw_src = esc_url_raw( $raw_src ); // clean it.
'src_width' => $meta['width'] ?? 0,
'src_height' => $meta['height'] ?? 0,
'alt_text' => self::get_alt_text( $image_id ),
} elseif ( ! empty( $gallery['src'] ) ) {
foreach ( $gallery['src'] as $src ) {
list( $raw_src ) = explode( '?', $src ); // pull off any Query string (?w=250).
$raw_src = wp_specialchars_decode( $raw_src ); // rawify it.
$raw_src = esc_url_raw( $raw_src ); // clean it.
* Get attachment images for a specified post and return them. Also make sure
* their dimensions are at or above a required minimum.
* @param int $post_id The post ID to check.
* @param int $width Image width.
* @param int $height Image height.
* @return array Containing details of the image, or empty array if none.
public static function from_attachment( $post_id, $width = 200, $height = 200 ) {
$post = get_post( $post_id );
if ( ! empty( $post->post_password ) ) {
$post_images = get_posts(
'post_parent' => $post_id, // Must be children of post.
'numberposts' => 5, // No more than 5.
'post_type' => 'attachment', // Must be attachments.
'post_mime_type' => 'image', // Must be images.
'suppress_filters' => false,
$permalink = get_permalink( $post_id );
foreach ( $post_images as $post_image ) {
$current_image = self::get_attachment_data( $post_image->ID, $permalink, $width, $height );
if ( false !== $current_image ) {
$images[] = $current_image;
* We only want to pass back attached images that were actually inserted.
* We can load up all the images found in the HTML source and then
* compare URLs to see if an image is attached AND inserted.
$html_images = self::from_html( $post_id );
$inserted_images = array();
foreach ( $html_images as $html_image ) {
$src = wp_parse_url( $html_image['src'] );
if ( ! $src || empty( $src['path'] ) ) {
// strip off any query strings from src.
if ( ! empty( $src['scheme'] ) && ! empty( $src['host'] ) ) {
$inserted_images[] = $src['scheme'] . '://' . $src['host'] . $src['path'];
} elseif ( ! empty( $src['host'] ) ) {
$inserted_images[] = set_url_scheme( 'http://' . $src['host'] . $src['path'] );
$inserted_images[] = site_url( '/' ) . $src['path'];
foreach ( $images as $i => $image ) {
if ( ! in_array( $image['src'], $inserted_images, true ) ) {
* Check if a Featured Image is set for this post, and return it in a similar
* format to the other images?_from_*() methods.
* @param int $post_id The post ID to check.
* @param int $width Image width.
* @param int $height Image height.
* @return array containing details of the Featured Image, or empty array if none.
public static function from_thumbnail( $post_id, $width = 200, $height = 200 ) {
$post = get_post( $post_id );
if ( ! empty( $post->post_password ) ) {
if ( 'attachment' === get_post_type( $post ) && wp_attachment_is_image( $post ) ) {
$thumb = get_post_thumbnail_id( $post );
$meta = wp_get_attachment_metadata( $thumb );
// Must be larger than requested minimums.
if ( ! isset( $meta['width'] ) || $meta['width'] < $width ) {
if ( ! isset( $meta['height'] ) || $meta['height'] < $height ) {
$max_dimension = self::get_max_thumbnail_dimension();
$too_big = ( ( ! empty( $meta['width'] ) && $meta['width'] > $max_dimension ) || ( ! empty( $meta['height'] ) && $meta['height'] > $max_dimension ) );
( method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'photon' ) ) ||
( defined( 'IS_WPCOM' ) && IS_WPCOM )
$size = self::determine_thumbnail_size_for_photon( $meta['width'], $meta['height'] );
'fit' => $size['width'] . ',' . $size['height'],
$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
$img_src = wp_get_attachment_image_src( $thumb, 'full' );
if ( ! is_array( $img_src ) ) {
// If wp_get_attachment_image_src returns false but we know that there should be an image that could be used.
// we try a bit harder and user the data that we have.
$thumb_post_data = get_post( $thumb );
$img_src = array( $thumb_post_data->guid ?? null, $meta['width'], $meta['height'] );
// Let's try to use the postmeta if we can, since it seems to be
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
$featured_image = get_post_meta( $post->ID, '_jetpack_featured_image' );
$url = $featured_image[0];
array( // Other methods below all return an array of arrays.
'src_width' => $img_src[1],
'src_height' => $img_src[2],
'href' => get_permalink( $thumb ),
'alt_text' => self::get_alt_text( $thumb ),
if ( empty( $images ) && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
$meta_thumbnail = get_post_meta( $post_id, '_jetpack_post_thumbnail', true );
if ( ! empty( $meta_thumbnail ) ) {
if ( ! isset( $meta_thumbnail['width'] ) || $meta_thumbnail['width'] < $width ) {
if ( ! isset( $meta_thumbnail['height'] ) || $meta_thumbnail['height'] < $height ) {
array( // Other methods below all return an array of arrays.
'src' => $meta_thumbnail['URL'],
'src_width' => $meta_thumbnail['width'],
'src_height' => $meta_thumbnail['height'],
'href' => $meta_thumbnail['URL'],
'alt_text' => self::get_alt_text( $thumb ),
* Get images from Gutenberg Image blocks.
* @since 14.8 Updated to use Block_Delimiter for improved performance.
* @since 14.9 Updated to use Block_Scanner for improved performance.
* @param mixed $html_or_id The HTML string to parse for images, or a post id.
* @param int $width Minimum Image width.
* @param int $height Minimum Image height.
public static function from_blocks( $html_or_id, $width = 200, $height = 200 ) {
$html_info = self::get_post_html( $html_or_id );
if ( empty( $html_info['html'] ) ) {
$scanner = Block_Scanner::create( $html_info['html'] );
* Use Block_Scanner to parse our post content HTML,
* and find all the block delimiters for supported blocks,
* whether they're parent or nested blocks.
$supported_blocks = array(
while ( $scanner->next_delimiter() ) {
$type = $scanner->get_delimiter_type();
// Only process opening delimiters for supported block types.
if ( Block_Scanner::OPENER !== $type ) {
$block_type = $scanner->get_block_type();
$is_supported_block = in_array( $block_type, $supported_blocks, true );
if ( ! $is_supported_block ) {
$attributes = $scanner->allocate_and_return_parsed_attributes() ?? array();
$block_images = self::get_images_from_block_attributes( $block_type, $attributes, $html_info, $width, $height );
if ( ! empty( $block_images ) ) {
$images = array_merge( $images, $block_images );
* Returning a filtered array because get_attachment_data returns false
* for unsuccessful attempts.
return array_filter( $images );
* Extract images from block attributes based on block type.
* @param string $block_type Block type name.
* @param array $attributes Block attributes.
* @param array $html_info Info about the post where the block is found.
* @param int $width Desired image width.
* @param int $height Desired image height.
* @return array Array of images found.