From 8091de83b5d8c35a19ca06defb59d3faf78435b4 Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Thu, 7 Jul 2022 23:32:11 +0000 Subject: [PATCH] Media: - Deprecate `wp_get_attachment_thumb_file()`. - Make `wp_get_attachment_thumb_url()` an alias of `wp_get_attachment_image_url()`. This fixes it to return the proper thumbnail URL and fall back to returning the URL to `image_meta['thumb']` if only that exists. Props: markhowellsmead, mukesh27, csesumonpro, SergeyBiryukov, mikeschroder, killua99, joemcgill, mashukushibiki, mfgmicha, swissspidy, romulodl, nacin, JoshuaAbenazer, wonderboymusic, lonnylot, azaozz. Built from https://develop.svn.wordpress.org/trunk@53685 git-svn-id: http://core.svn.wordpress.org/trunk@53244 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/deprecated.php | 49 ++++++++++++++++++++++++++ wp-includes/media.php | 22 ++++++------ wp-includes/post.php | 71 +++++--------------------------------- wp-includes/version.php | 2 +- 4 files changed, 70 insertions(+), 74 deletions(-) diff --git a/wp-includes/deprecated.php b/wp-includes/deprecated.php index ff6ce82497..af5eb95692 100644 --- a/wp-includes/deprecated.php +++ b/wp-includes/deprecated.php @@ -4311,3 +4311,52 @@ function wp_skip_spacing_serialization( $block_type ) { function wp_add_iframed_editor_assets_html() { _deprecated_function( __FUNCTION__, '6.0.0' ); } + +/** + * Retrieves thumbnail for an attachment. + * Note that this works only for the (very) old image metadata style where 'thumb' was set, + * and the 'sizes' array did not exist. This function returns false for the newer image metadata style + * despite that 'thumbnail' is present in the 'sizes' array. + * + * @since 2.1.0 + * @deprecated 6.1.0 + * + * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`. + * @return string|false Thumbnail file path on success, false on failure. + */ +function wp_get_attachment_thumb_file( $post_id = 0 ) { + _deprecated_function( __FUNCTION__, '6.1.0' ); + + $post_id = (int) $post_id; + $post = get_post( $post_id ); + + if ( ! $post ) { + return false; + } + + // Use $post->ID rather than $post_id as get_post() may have used the global $post object. + $imagedata = wp_get_attachment_metadata( $post->ID ); + + if ( ! is_array( $imagedata ) ) { + return false; + } + + $file = get_attached_file( $post->ID ); + + if ( ! empty( $imagedata['thumb'] ) ) { + $thumbfile = str_replace( wp_basename( $file ), $imagedata['thumb'], $file ); + if ( file_exists( $thumbfile ) ) { + /** + * Filters the attachment thumbnail file path. + * + * @since 2.1.0 + * + * @param string $thumbfile File path to the attachment thumbnail. + * @param int $post_id Attachment ID. + */ + return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID ); + } + } + + return false; +} diff --git a/wp-includes/media.php b/wp-includes/media.php index a383c5b282..bc850cbe6d 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -238,20 +238,20 @@ function image_downsize( $id, $size = 'medium' ) { $width = $intermediate['width']; $height = $intermediate['height']; $is_intermediate = true; - } elseif ( 'thumbnail' === $size ) { + } elseif ( 'thumbnail' === $size && ! empty( $meta['thumb'] ) && is_string( $meta['thumb'] ) ) { // Fall back to the old thumbnail. - $thumb_file = wp_get_attachment_thumb_file( $id ); - $info = null; + $imagefile = get_attached_file( $id ); + $thumbfile = str_replace( wp_basename( $imagefile ), wp_basename( $meta['thumb'] ), $imagefile ); - if ( $thumb_file ) { - $info = wp_getimagesize( $thumb_file ); - } + if ( file_exists( $thumbfile ) ) { + $info = wp_getimagesize( $thumbfile ); - if ( $thumb_file && $info ) { - $img_url = str_replace( $img_url_basename, wp_basename( $thumb_file ), $img_url ); - $width = $info[0]; - $height = $info[1]; - $is_intermediate = true; + if ( $info ) { + $img_url = str_replace( $img_url_basename, wp_basename( $thumbfile ), $img_url ); + $width = $info[0]; + $height = $info[1]; + $is_intermediate = true; + } } } diff --git a/wp-includes/post.php b/wp-includes/post.php index 05b97d699f..4ceb8dd3a6 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -6701,88 +6701,35 @@ function wp_get_attachment_caption( $post_id = 0 ) { return apply_filters( 'wp_get_attachment_caption', $caption, $post->ID ); } -/** - * Retrieves thumbnail for an attachment. - * - * @since 2.1.0 - * - * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`. - * @return string|false Thumbnail file path on success, false on failure. - */ -function wp_get_attachment_thumb_file( $post_id = 0 ) { - $post_id = (int) $post_id; - $post = get_post( $post_id ); - - if ( ! $post ) { - return false; - } - - $imagedata = wp_get_attachment_metadata( $post->ID ); - if ( ! is_array( $imagedata ) ) { - return false; - } - - $file = get_attached_file( $post->ID ); - - if ( ! empty( $imagedata['thumb'] ) ) { - $thumbfile = str_replace( wp_basename( $file ), $imagedata['thumb'], $file ); - if ( file_exists( $thumbfile ) ) { - /** - * Filters the attachment thumbnail file path. - * - * @since 2.1.0 - * - * @param string $thumbfile File path to the attachment thumbnail. - * @param int $post_id Attachment ID. - */ - return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID ); - } - } - return false; -} - /** * Retrieves URL for an attachment thumbnail. * * @since 2.1.0 + * @since 6.1.0 Changed to use wp_get_attachment_image_url(). * * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`. * @return string|false Thumbnail URL on success, false on failure. */ function wp_get_attachment_thumb_url( $post_id = 0 ) { $post_id = (int) $post_id; - $post = get_post( $post_id ); - if ( ! $post ) { + // This uses image_downsize() which also looks for the (very) old format $image_meta['thumb'] + // when the newer format $image_meta['sizes']['thumbnail'] doesn't exist. + $thumbnail_url = wp_get_attachment_image_url( $post_id, 'thumbnail' ); + + if ( empty( $thumbnail_url ) ) { return false; } - $url = wp_get_attachment_url( $post->ID ); - if ( ! $url ) { - return false; - } - - $sized = image_downsize( $post_id, 'thumbnail' ); - if ( $sized ) { - return $sized[0]; - } - - $thumb = wp_get_attachment_thumb_file( $post->ID ); - if ( ! $thumb ) { - return false; - } - - $url = str_replace( wp_basename( $url ), wp_basename( $thumb ), $url ); - /** * Filters the attachment thumbnail URL. * * @since 2.1.0 * - * @param string $url URL for the attachment thumbnail. - * @param int $post_id Attachment ID. + * @param string $thumbnail_url URL for the attachment thumbnail. + * @param int $post_id Attachment ID. */ - return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID ); + return apply_filters( 'wp_get_attachment_thumb_url', $thumbnail_url, $post_id ); } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index 2807f6e0cb..d04a9daba6 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.1-alpha-53684'; +$wp_version = '6.1-alpha-53685'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.