Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack
File: class.jetpack-post-images.php
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
[0] Fix | Delete
/**
[1] Fix | Delete
* Useful for finding an image to display alongside/in representation of a specific post.
[2] Fix | Delete
*
[3] Fix | Delete
* @package automattic/jetpack
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
use Automattic\Block_Scanner;
[7] Fix | Delete
use Automattic\Jetpack\Image_CDN\Image_CDN_Core;
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Useful for finding an image to display alongside/in representation of a specific post.
[11] Fix | Delete
*
[12] Fix | Delete
* Includes a few different methods, all of which return a similar-format array containing
[13] Fix | Delete
* details of any images found. Everything can (should) be called statically, it's just a
[14] Fix | Delete
* function-bucket. You can also call Jetpack_PostImages::get_image() to cycle through all of the methods until
[15] Fix | Delete
* one of them finds something useful.
[16] Fix | Delete
*
[17] Fix | Delete
* This file is included verbatim in Jetpack
[18] Fix | Delete
*/
[19] Fix | Delete
class Jetpack_PostImages {
[20] Fix | Delete
/**
[21] Fix | Delete
* If a slideshow is embedded within a post, then parse out the images involved and return them
[22] Fix | Delete
*
[23] Fix | Delete
* @param int $post_id Post ID.
[24] Fix | Delete
* @param int $width Image width.
[25] Fix | Delete
* @param int $height Image height.
[26] Fix | Delete
* @return array Images.
[27] Fix | Delete
*/
[28] Fix | Delete
public static function from_slideshow( $post_id, $width = 200, $height = 200 ) {
[29] Fix | Delete
$images = array();
[30] Fix | Delete
[31] Fix | Delete
$post = get_post( $post_id );
[32] Fix | Delete
[33] Fix | Delete
if ( ! $post ) {
[34] Fix | Delete
return $images;
[35] Fix | Delete
}
[36] Fix | Delete
[37] Fix | Delete
if ( ! empty( $post->post_password ) ) {
[38] Fix | Delete
return $images;
[39] Fix | Delete
}
[40] Fix | Delete
[41] Fix | Delete
if ( false === has_shortcode( $post->post_content, 'slideshow' ) ) {
[42] Fix | Delete
return $images; // no slideshow - bail.
[43] Fix | Delete
}
[44] Fix | Delete
[45] Fix | Delete
$permalink = get_permalink( $post->ID );
[46] Fix | Delete
[47] Fix | Delete
// Mechanic: Somebody set us up the bomb.
[48] Fix | Delete
$old_post = $GLOBALS['post'] ?? null;
[49] Fix | Delete
$GLOBALS['post'] = $post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
[50] Fix | Delete
$old_shortcodes = $GLOBALS['shortcode_tags'];
[51] Fix | Delete
$GLOBALS['shortcode_tags'] = array( 'slideshow' => $old_shortcodes['slideshow'] ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
[52] Fix | Delete
[53] Fix | Delete
// Find all the slideshows.
[54] Fix | Delete
preg_match_all( '/' . get_shortcode_regex() . '/sx', $post->post_content, $slideshow_matches, PREG_SET_ORDER );
[55] Fix | Delete
[56] Fix | Delete
ob_start(); // The slideshow shortcode handler calls wp_print_scripts and wp_print_styles... not too happy about that.
[57] Fix | Delete
[58] Fix | Delete
foreach ( $slideshow_matches as $slideshow_match ) {
[59] Fix | Delete
$slideshow = do_shortcode_tag( $slideshow_match );
[60] Fix | Delete
$pos = stripos( $slideshow, 'jetpack-slideshow' );
[61] Fix | Delete
if ( false === $pos ) { // must be something wrong - or we changed the output format in which case none of the following will work.
[62] Fix | Delete
continue;
[63] Fix | Delete
}
[64] Fix | Delete
$start = strpos( $slideshow, '[', $pos );
[65] Fix | Delete
$end = strpos( $slideshow, ']', $start );
[66] Fix | Delete
$post_images = json_decode( wp_specialchars_decode( str_replace( "'", '"', substr( $slideshow, $start, $end - $start + 1 ) ), ENT_QUOTES ) ); // parse via JSON
[67] Fix | Delete
// If the JSON didn't decode don't try and act on it.
[68] Fix | Delete
if ( is_array( $post_images ) ) {
[69] Fix | Delete
foreach ( $post_images as $post_image ) {
[70] Fix | Delete
$post_image_id = absint( $post_image->id );
[71] Fix | Delete
if ( ! $post_image_id ) {
[72] Fix | Delete
continue;
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
$meta = wp_get_attachment_metadata( $post_image_id );
[76] Fix | Delete
[77] Fix | Delete
// Must be larger than 200x200 (or user-specified).
[78] Fix | Delete
if ( ! isset( $meta['width'] ) || $meta['width'] < $width ) {
[79] Fix | Delete
continue;
[80] Fix | Delete
}
[81] Fix | Delete
if ( ! isset( $meta['height'] ) || $meta['height'] < $height ) {
[82] Fix | Delete
continue;
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
$url = wp_get_attachment_url( $post_image_id );
[86] Fix | Delete
[87] Fix | Delete
$images[] = array(
[88] Fix | Delete
'type' => 'image',
[89] Fix | Delete
'from' => 'slideshow',
[90] Fix | Delete
'src' => $url,
[91] Fix | Delete
'src_width' => $meta['width'],
[92] Fix | Delete
'src_height' => $meta['height'],
[93] Fix | Delete
'href' => $permalink,
[94] Fix | Delete
);
[95] Fix | Delete
}
[96] Fix | Delete
}
[97] Fix | Delete
}
[98] Fix | Delete
ob_end_clean();
[99] Fix | Delete
[100] Fix | Delete
// Operator: Main screen turn on.
[101] Fix | Delete
$GLOBALS['shortcode_tags'] = $old_shortcodes; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
[102] Fix | Delete
$GLOBALS['post'] = $old_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
[103] Fix | Delete
[104] Fix | Delete
return $images;
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
/**
[108] Fix | Delete
* Filtering out images with broken URL from galleries.
[109] Fix | Delete
*
[110] Fix | Delete
* @param array $galleries Galleries.
[111] Fix | Delete
* @return array $filtered_galleries
[112] Fix | Delete
*/
[113] Fix | Delete
public static function filter_gallery_urls( $galleries ) {
[114] Fix | Delete
$filtered_galleries = array();
[115] Fix | Delete
foreach ( $galleries as $this_gallery ) {
[116] Fix | Delete
if ( ! isset( $this_gallery['src'] ) ) {
[117] Fix | Delete
continue;
[118] Fix | Delete
}
[119] Fix | Delete
$ids = isset( $this_gallery['ids'] ) ? explode( ',', $this_gallery['ids'] ) : array();
[120] Fix | Delete
// Make sure 'src' array isn't associative and has no holes.
[121] Fix | Delete
$this_gallery['src'] = array_values( $this_gallery['src'] );
[122] Fix | Delete
foreach ( $this_gallery['src'] as $idx => $src_url ) {
[123] Fix | Delete
if ( filter_var( $src_url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED ) === false ) {
[124] Fix | Delete
unset( $this_gallery['src'][ $idx ] );
[125] Fix | Delete
unset( $ids[ $idx ] );
[126] Fix | Delete
}
[127] Fix | Delete
}
[128] Fix | Delete
if ( isset( $this_gallery['ids'] ) ) {
[129] Fix | Delete
$this_gallery['ids'] = implode( ',', $ids );
[130] Fix | Delete
}
[131] Fix | Delete
// Remove any holes we introduced.
[132] Fix | Delete
$this_gallery['src'] = array_values( $this_gallery['src'] );
[133] Fix | Delete
$filtered_galleries[] = $this_gallery;
[134] Fix | Delete
}
[135] Fix | Delete
[136] Fix | Delete
return $filtered_galleries;
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
/**
[140] Fix | Delete
* If a gallery is detected, then get all the images from it.
[141] Fix | Delete
*
[142] Fix | Delete
* @param int $post_id Post ID.
[143] Fix | Delete
* @param int $width Minimum image width to consider.
[144] Fix | Delete
* @param int $height Minimum image height to consider.
[145] Fix | Delete
* @return array Images.
[146] Fix | Delete
*/
[147] Fix | Delete
public static function from_gallery( $post_id, $width = 200, $height = 200 ) {
[148] Fix | Delete
$images = array();
[149] Fix | Delete
[150] Fix | Delete
$post = get_post( $post_id );
[151] Fix | Delete
[152] Fix | Delete
if ( ! $post ) {
[153] Fix | Delete
return $images;
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
if ( ! empty( $post->post_password ) ) {
[157] Fix | Delete
return $images;
[158] Fix | Delete
}
[159] Fix | Delete
add_filter( 'get_post_galleries', array( __CLASS__, 'filter_gallery_urls' ), 999999 );
[160] Fix | Delete
[161] Fix | Delete
$permalink = get_permalink( $post->ID );
[162] Fix | Delete
[163] Fix | Delete
/**
[164] Fix | Delete
* Juggle global post object because the gallery shortcode uses the
[165] Fix | Delete
* global object.
[166] Fix | Delete
*
[167] Fix | Delete
* See core ticket:
[168] Fix | Delete
* https://core.trac.wordpress.org/ticket/39304
[169] Fix | Delete
*/
[170] Fix | Delete
// phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited
[171] Fix | Delete
if ( isset( $GLOBALS['post'] ) ) {
[172] Fix | Delete
$juggle_post = $GLOBALS['post'];
[173] Fix | Delete
$GLOBALS['post'] = $post;
[174] Fix | Delete
$galleries = get_post_galleries( $post->ID, false );
[175] Fix | Delete
$GLOBALS['post'] = $juggle_post;
[176] Fix | Delete
} else {
[177] Fix | Delete
$GLOBALS['post'] = $post;
[178] Fix | Delete
$galleries = get_post_galleries( $post->ID, false );
[179] Fix | Delete
unset( $GLOBALS['post'] );
[180] Fix | Delete
}
[181] Fix | Delete
// phpcs:enable WordPress.WP.GlobalVariablesOverride.Prohibited
[182] Fix | Delete
[183] Fix | Delete
foreach ( $galleries as $gallery ) {
[184] Fix | Delete
if ( ! empty( $gallery['ids'] ) ) {
[185] Fix | Delete
$image_ids = explode( ',', $gallery['ids'] );
[186] Fix | Delete
$image_size = isset( $gallery['size'] ) ? $gallery['size'] : 'thumbnail';
[187] Fix | Delete
foreach ( $image_ids as $image_id ) {
[188] Fix | Delete
$image = wp_get_attachment_image_src( $image_id, $image_size );
[189] Fix | Delete
$meta = wp_get_attachment_metadata( $image_id );
[190] Fix | Delete
[191] Fix | Delete
if ( isset( $gallery['type'] ) && 'slideshow' === $gallery['type'] ) {
[192] Fix | Delete
// Must be larger than 200x200 (or user-specified).
[193] Fix | Delete
if ( ! isset( $meta['width'] ) || $meta['width'] < $width ) {
[194] Fix | Delete
continue;
[195] Fix | Delete
}
[196] Fix | Delete
if ( ! isset( $meta['height'] ) || $meta['height'] < $height ) {
[197] Fix | Delete
continue;
[198] Fix | Delete
}
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
if ( ! empty( $image[0] ) ) {
[202] Fix | Delete
list( $raw_src ) = explode( '?', $image[0] ); // pull off any Query string (?w=250).
[203] Fix | Delete
$raw_src = wp_specialchars_decode( $raw_src ); // rawify it.
[204] Fix | Delete
$raw_src = esc_url_raw( $raw_src ); // clean it.
[205] Fix | Delete
$images[] = array(
[206] Fix | Delete
'type' => 'image',
[207] Fix | Delete
'from' => 'gallery',
[208] Fix | Delete
'src' => $raw_src,
[209] Fix | Delete
'src_width' => $meta['width'] ?? 0,
[210] Fix | Delete
'src_height' => $meta['height'] ?? 0,
[211] Fix | Delete
'href' => $permalink,
[212] Fix | Delete
'alt_text' => self::get_alt_text( $image_id ),
[213] Fix | Delete
);
[214] Fix | Delete
}
[215] Fix | Delete
}
[216] Fix | Delete
} elseif ( ! empty( $gallery['src'] ) ) {
[217] Fix | Delete
foreach ( $gallery['src'] as $src ) {
[218] Fix | Delete
list( $raw_src ) = explode( '?', $src ); // pull off any Query string (?w=250).
[219] Fix | Delete
$raw_src = wp_specialchars_decode( $raw_src ); // rawify it.
[220] Fix | Delete
$raw_src = esc_url_raw( $raw_src ); // clean it.
[221] Fix | Delete
$images[] = array(
[222] Fix | Delete
'type' => 'image',
[223] Fix | Delete
'from' => 'gallery',
[224] Fix | Delete
'src' => $raw_src,
[225] Fix | Delete
'href' => $permalink,
[226] Fix | Delete
);
[227] Fix | Delete
}
[228] Fix | Delete
}
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
return $images;
[232] Fix | Delete
}
[233] Fix | Delete
[234] Fix | Delete
/**
[235] Fix | Delete
* Get attachment images for a specified post and return them. Also make sure
[236] Fix | Delete
* their dimensions are at or above a required minimum.
[237] Fix | Delete
*
[238] Fix | Delete
* @param int $post_id The post ID to check.
[239] Fix | Delete
* @param int $width Image width.
[240] Fix | Delete
* @param int $height Image height.
[241] Fix | Delete
* @return array Containing details of the image, or empty array if none.
[242] Fix | Delete
*/
[243] Fix | Delete
public static function from_attachment( $post_id, $width = 200, $height = 200 ) {
[244] Fix | Delete
$images = array();
[245] Fix | Delete
[246] Fix | Delete
$post = get_post( $post_id );
[247] Fix | Delete
[248] Fix | Delete
if ( ! empty( $post->post_password ) ) {
[249] Fix | Delete
return $images;
[250] Fix | Delete
}
[251] Fix | Delete
[252] Fix | Delete
$post_images = get_posts(
[253] Fix | Delete
array(
[254] Fix | Delete
'post_parent' => $post_id, // Must be children of post.
[255] Fix | Delete
'numberposts' => 5, // No more than 5.
[256] Fix | Delete
'post_type' => 'attachment', // Must be attachments.
[257] Fix | Delete
'post_mime_type' => 'image', // Must be images.
[258] Fix | Delete
'suppress_filters' => false,
[259] Fix | Delete
)
[260] Fix | Delete
);
[261] Fix | Delete
[262] Fix | Delete
if ( ! $post_images ) {
[263] Fix | Delete
return $images;
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
$permalink = get_permalink( $post_id );
[267] Fix | Delete
[268] Fix | Delete
foreach ( $post_images as $post_image ) {
[269] Fix | Delete
$current_image = self::get_attachment_data( $post_image->ID, $permalink, $width, $height );
[270] Fix | Delete
if ( false !== $current_image ) {
[271] Fix | Delete
$images[] = $current_image;
[272] Fix | Delete
}
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
/*
[276] Fix | Delete
* We only want to pass back attached images that were actually inserted.
[277] Fix | Delete
* We can load up all the images found in the HTML source and then
[278] Fix | Delete
* compare URLs to see if an image is attached AND inserted.
[279] Fix | Delete
*/
[280] Fix | Delete
$html_images = self::from_html( $post_id );
[281] Fix | Delete
$inserted_images = array();
[282] Fix | Delete
[283] Fix | Delete
foreach ( $html_images as $html_image ) {
[284] Fix | Delete
$src = wp_parse_url( $html_image['src'] );
[285] Fix | Delete
if ( ! $src || empty( $src['path'] ) ) {
[286] Fix | Delete
continue;
[287] Fix | Delete
}
[288] Fix | Delete
[289] Fix | Delete
// strip off any query strings from src.
[290] Fix | Delete
if ( ! empty( $src['scheme'] ) && ! empty( $src['host'] ) ) {
[291] Fix | Delete
$inserted_images[] = $src['scheme'] . '://' . $src['host'] . $src['path'];
[292] Fix | Delete
} elseif ( ! empty( $src['host'] ) ) {
[293] Fix | Delete
$inserted_images[] = set_url_scheme( 'http://' . $src['host'] . $src['path'] );
[294] Fix | Delete
} else {
[295] Fix | Delete
$inserted_images[] = site_url( '/' ) . $src['path'];
[296] Fix | Delete
}
[297] Fix | Delete
}
[298] Fix | Delete
foreach ( $images as $i => $image ) {
[299] Fix | Delete
if ( ! in_array( $image['src'], $inserted_images, true ) ) {
[300] Fix | Delete
unset( $images[ $i ] );
[301] Fix | Delete
}
[302] Fix | Delete
}
[303] Fix | Delete
[304] Fix | Delete
return $images;
[305] Fix | Delete
}
[306] Fix | Delete
[307] Fix | Delete
/**
[308] Fix | Delete
* Check if a Featured Image is set for this post, and return it in a similar
[309] Fix | Delete
* format to the other images?_from_*() methods.
[310] Fix | Delete
*
[311] Fix | Delete
* @param int $post_id The post ID to check.
[312] Fix | Delete
* @param int $width Image width.
[313] Fix | Delete
* @param int $height Image height.
[314] Fix | Delete
* @return array containing details of the Featured Image, or empty array if none.
[315] Fix | Delete
*/
[316] Fix | Delete
public static function from_thumbnail( $post_id, $width = 200, $height = 200 ) {
[317] Fix | Delete
$images = array();
[318] Fix | Delete
[319] Fix | Delete
$post = get_post( $post_id );
[320] Fix | Delete
[321] Fix | Delete
if ( ! empty( $post->post_password ) ) {
[322] Fix | Delete
return $images;
[323] Fix | Delete
}
[324] Fix | Delete
[325] Fix | Delete
if ( 'attachment' === get_post_type( $post ) && wp_attachment_is_image( $post ) ) {
[326] Fix | Delete
$thumb = $post_id;
[327] Fix | Delete
} else {
[328] Fix | Delete
$thumb = get_post_thumbnail_id( $post );
[329] Fix | Delete
}
[330] Fix | Delete
[331] Fix | Delete
if ( $thumb ) {
[332] Fix | Delete
$meta = wp_get_attachment_metadata( $thumb );
[333] Fix | Delete
// Must be larger than requested minimums.
[334] Fix | Delete
if ( ! isset( $meta['width'] ) || $meta['width'] < $width ) {
[335] Fix | Delete
return $images;
[336] Fix | Delete
}
[337] Fix | Delete
if ( ! isset( $meta['height'] ) || $meta['height'] < $height ) {
[338] Fix | Delete
return $images;
[339] Fix | Delete
}
[340] Fix | Delete
$max_dimension = self::get_max_thumbnail_dimension();
[341] Fix | Delete
$too_big = ( ( ! empty( $meta['width'] ) && $meta['width'] > $max_dimension ) || ( ! empty( $meta['height'] ) && $meta['height'] > $max_dimension ) );
[342] Fix | Delete
[343] Fix | Delete
if (
[344] Fix | Delete
$too_big &&
[345] Fix | Delete
(
[346] Fix | Delete
( method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'photon' ) ) ||
[347] Fix | Delete
( defined( 'IS_WPCOM' ) && IS_WPCOM )
[348] Fix | Delete
)
[349] Fix | Delete
) {
[350] Fix | Delete
$size = self::determine_thumbnail_size_for_photon( $meta['width'], $meta['height'] );
[351] Fix | Delete
$photon_args = array(
[352] Fix | Delete
'fit' => $size['width'] . ',' . $size['height'],
[353] Fix | Delete
);
[354] Fix | Delete
$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
[355] Fix | Delete
} else {
[356] Fix | Delete
$img_src = wp_get_attachment_image_src( $thumb, 'full' );
[357] Fix | Delete
}
[358] Fix | Delete
if ( ! is_array( $img_src ) ) {
[359] Fix | Delete
// If wp_get_attachment_image_src returns false but we know that there should be an image that could be used.
[360] Fix | Delete
// we try a bit harder and user the data that we have.
[361] Fix | Delete
$thumb_post_data = get_post( $thumb );
[362] Fix | Delete
$img_src = array( $thumb_post_data->guid ?? null, $meta['width'], $meta['height'] );
[363] Fix | Delete
}
[364] Fix | Delete
[365] Fix | Delete
// Let's try to use the postmeta if we can, since it seems to be
[366] Fix | Delete
// more reliable
[367] Fix | Delete
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
[368] Fix | Delete
$featured_image = get_post_meta( $post->ID, '_jetpack_featured_image' );
[369] Fix | Delete
if ( $featured_image ) {
[370] Fix | Delete
$url = $featured_image[0];
[371] Fix | Delete
} else {
[372] Fix | Delete
$url = $img_src[0];
[373] Fix | Delete
}
[374] Fix | Delete
} else {
[375] Fix | Delete
$url = $img_src[0];
[376] Fix | Delete
}
[377] Fix | Delete
$images = array(
[378] Fix | Delete
array( // Other methods below all return an array of arrays.
[379] Fix | Delete
'type' => 'image',
[380] Fix | Delete
'from' => 'thumbnail',
[381] Fix | Delete
'src' => $url,
[382] Fix | Delete
'src_width' => $img_src[1],
[383] Fix | Delete
'src_height' => $img_src[2],
[384] Fix | Delete
'href' => get_permalink( $thumb ),
[385] Fix | Delete
'alt_text' => self::get_alt_text( $thumb ),
[386] Fix | Delete
),
[387] Fix | Delete
);
[388] Fix | Delete
[389] Fix | Delete
}
[390] Fix | Delete
[391] Fix | Delete
if ( empty( $images ) && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
[392] Fix | Delete
$meta_thumbnail = get_post_meta( $post_id, '_jetpack_post_thumbnail', true );
[393] Fix | Delete
if ( ! empty( $meta_thumbnail ) ) {
[394] Fix | Delete
if ( ! isset( $meta_thumbnail['width'] ) || $meta_thumbnail['width'] < $width ) {
[395] Fix | Delete
return $images;
[396] Fix | Delete
}
[397] Fix | Delete
[398] Fix | Delete
if ( ! isset( $meta_thumbnail['height'] ) || $meta_thumbnail['height'] < $height ) {
[399] Fix | Delete
return $images;
[400] Fix | Delete
}
[401] Fix | Delete
[402] Fix | Delete
$images = array(
[403] Fix | Delete
array( // Other methods below all return an array of arrays.
[404] Fix | Delete
'type' => 'image',
[405] Fix | Delete
'from' => 'thumbnail',
[406] Fix | Delete
'src' => $meta_thumbnail['URL'],
[407] Fix | Delete
'src_width' => $meta_thumbnail['width'],
[408] Fix | Delete
'src_height' => $meta_thumbnail['height'],
[409] Fix | Delete
'href' => $meta_thumbnail['URL'],
[410] Fix | Delete
'alt_text' => self::get_alt_text( $thumb ),
[411] Fix | Delete
),
[412] Fix | Delete
);
[413] Fix | Delete
}
[414] Fix | Delete
}
[415] Fix | Delete
[416] Fix | Delete
return $images;
[417] Fix | Delete
}
[418] Fix | Delete
[419] Fix | Delete
/**
[420] Fix | Delete
* Get images from Gutenberg Image blocks.
[421] Fix | Delete
*
[422] Fix | Delete
* @since 6.9.0
[423] Fix | Delete
* @since 14.8 Updated to use Block_Delimiter for improved performance.
[424] Fix | Delete
* @since 14.9 Updated to use Block_Scanner for improved performance.
[425] Fix | Delete
*
[426] Fix | Delete
* @param mixed $html_or_id The HTML string to parse for images, or a post id.
[427] Fix | Delete
* @param int $width Minimum Image width.
[428] Fix | Delete
* @param int $height Minimum Image height.
[429] Fix | Delete
*/
[430] Fix | Delete
public static function from_blocks( $html_or_id, $width = 200, $height = 200 ) {
[431] Fix | Delete
$images = array();
[432] Fix | Delete
[433] Fix | Delete
$html_info = self::get_post_html( $html_or_id );
[434] Fix | Delete
[435] Fix | Delete
if ( empty( $html_info['html'] ) ) {
[436] Fix | Delete
return $images;
[437] Fix | Delete
}
[438] Fix | Delete
[439] Fix | Delete
$scanner = Block_Scanner::create( $html_info['html'] );
[440] Fix | Delete
if ( ! $scanner ) {
[441] Fix | Delete
return $images;
[442] Fix | Delete
}
[443] Fix | Delete
[444] Fix | Delete
/*
[445] Fix | Delete
* Use Block_Scanner to parse our post content HTML,
[446] Fix | Delete
* and find all the block delimiters for supported blocks,
[447] Fix | Delete
* whether they're parent or nested blocks.
[448] Fix | Delete
*/
[449] Fix | Delete
$supported_blocks = array(
[450] Fix | Delete
'core/image',
[451] Fix | Delete
'core/media-text',
[452] Fix | Delete
'core/gallery',
[453] Fix | Delete
'jetpack/tiled-gallery',
[454] Fix | Delete
'jetpack/slideshow',
[455] Fix | Delete
'jetpack/story',
[456] Fix | Delete
);
[457] Fix | Delete
[458] Fix | Delete
while ( $scanner->next_delimiter() ) {
[459] Fix | Delete
$type = $scanner->get_delimiter_type();
[460] Fix | Delete
// Only process opening delimiters for supported block types.
[461] Fix | Delete
if ( Block_Scanner::OPENER !== $type ) {
[462] Fix | Delete
continue;
[463] Fix | Delete
}
[464] Fix | Delete
[465] Fix | Delete
$block_type = $scanner->get_block_type();
[466] Fix | Delete
$is_supported_block = in_array( $block_type, $supported_blocks, true );
[467] Fix | Delete
if ( ! $is_supported_block ) {
[468] Fix | Delete
continue;
[469] Fix | Delete
}
[470] Fix | Delete
[471] Fix | Delete
$attributes = $scanner->allocate_and_return_parsed_attributes() ?? array();
[472] Fix | Delete
$block_images = self::get_images_from_block_attributes( $block_type, $attributes, $html_info, $width, $height );
[473] Fix | Delete
[474] Fix | Delete
if ( ! empty( $block_images ) ) {
[475] Fix | Delete
$images = array_merge( $images, $block_images );
[476] Fix | Delete
}
[477] Fix | Delete
}
[478] Fix | Delete
[479] Fix | Delete
/**
[480] Fix | Delete
* Returning a filtered array because get_attachment_data returns false
[481] Fix | Delete
* for unsuccessful attempts.
[482] Fix | Delete
*/
[483] Fix | Delete
return array_filter( $images );
[484] Fix | Delete
}
[485] Fix | Delete
[486] Fix | Delete
/**
[487] Fix | Delete
* Extract images from block attributes based on block type.
[488] Fix | Delete
*
[489] Fix | Delete
* @since 14.8
[490] Fix | Delete
*
[491] Fix | Delete
* @param string $block_type Block type name.
[492] Fix | Delete
* @param array $attributes Block attributes.
[493] Fix | Delete
* @param array $html_info Info about the post where the block is found.
[494] Fix | Delete
* @param int $width Desired image width.
[495] Fix | Delete
* @param int $height Desired image height.
[496] Fix | Delete
*
[497] Fix | Delete
* @return array Array of images found.
[498] Fix | Delete
*/
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function