- 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
This commit is contained in:
Andrew Ozz 2022-07-07 23:32:11 +00:00
parent b8b2693bdb
commit 8091de83b5
4 changed files with 70 additions and 74 deletions

View File

@ -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;
}

View File

@ -238,22 +238,22 @@ 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 );
if ( $info ) {
$img_url = str_replace( $img_url_basename, wp_basename( $thumbfile ), $img_url );
$width = $info[0];
$height = $info[1];
$is_intermediate = true;
}
}
}
if ( ! $width && ! $height && isset( $meta['width'], $meta['height'] ) ) {
// Any other type: use the real image.

View File

@ -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 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 );
}
/**

View File

@ -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.