Make WordPress Core

Changeset 53685


Ignore:
Timestamp:
07/07/2022 11:30:21 PM (15 months ago)
Author:
azaozz
Message:

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.

Location:
trunk/src/wp-includes
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/deprecated.php

    r53455 r53685  
    43124312    _deprecated_function( __FUNCTION__, '6.0.0' );
    43134313}
     4314
     4315/**
     4316 * Retrieves thumbnail for an attachment.
     4317 * Note that this works only for the (very) old image metadata style where 'thumb' was set,
     4318 * and the 'sizes' array did not exist. This function returns false for the newer image metadata style
     4319 * despite that 'thumbnail' is present in the 'sizes' array.
     4320 *
     4321 * @since 2.1.0
     4322 * @deprecated 6.1.0
     4323 *
     4324 * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`.
     4325 * @return string|false Thumbnail file path on success, false on failure.
     4326 */
     4327function wp_get_attachment_thumb_file( $post_id = 0 ) {
     4328    _deprecated_function( __FUNCTION__, '6.1.0' );
     4329
     4330    $post_id = (int) $post_id;
     4331    $post    = get_post( $post_id );
     4332
     4333    if ( ! $post ) {
     4334        return false;
     4335    }
     4336
     4337    // Use $post->ID rather than $post_id as get_post() may have used the global $post object.
     4338    $imagedata = wp_get_attachment_metadata( $post->ID );
     4339
     4340    if ( ! is_array( $imagedata ) ) {
     4341        return false;
     4342    }
     4343
     4344    $file = get_attached_file( $post->ID );
     4345
     4346    if ( ! empty( $imagedata['thumb'] ) ) {
     4347        $thumbfile = str_replace( wp_basename( $file ), $imagedata['thumb'], $file );
     4348        if ( file_exists( $thumbfile ) ) {
     4349            /**
     4350             * Filters the attachment thumbnail file path.
     4351             *
     4352             * @since 2.1.0
     4353             *
     4354             * @param string $thumbfile File path to the attachment thumbnail.
     4355             * @param int    $post_id   Attachment ID.
     4356             */
     4357            return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID );
     4358        }
     4359    }
     4360
     4361    return false;
     4362}
  • trunk/src/wp-includes/media.php

    r53481 r53685  
    239239        $height          = $intermediate['height'];
    240240        $is_intermediate = true;
    241     } elseif ( 'thumbnail' === $size ) {
     241    } elseif ( 'thumbnail' === $size && ! empty( $meta['thumb'] ) && is_string( $meta['thumb'] ) ) {
    242242        // Fall back to the old thumbnail.
    243         $thumb_file = wp_get_attachment_thumb_file( $id );
    244         $info       = null;
    245 
    246         if ( $thumb_file ) {
    247             $info = wp_getimagesize( $thumb_file );
    248         }
    249 
    250         if ( $thumb_file && $info ) {
    251             $img_url         = str_replace( $img_url_basename, wp_basename( $thumb_file ), $img_url );
    252             $width           = $info[0];
    253             $height          = $info[1];
    254             $is_intermediate = true;
     243        $imagefile = get_attached_file( $id );
     244        $thumbfile = str_replace( wp_basename( $imagefile ), wp_basename( $meta['thumb'] ), $imagefile );
     245
     246        if ( file_exists( $thumbfile ) ) {
     247            $info = wp_getimagesize( $thumbfile );
     248
     249            if ( $info ) {
     250                $img_url         = str_replace( $img_url_basename, wp_basename( $thumbfile ), $img_url );
     251                $width           = $info[0];
     252                $height          = $info[1];
     253                $is_intermediate = true;
     254            }
    255255        }
    256256    }
  • trunk/src/wp-includes/post.php

    r53559 r53685  
    67036703
    67046704/**
    6705  * Retrieves thumbnail for an attachment.
     6705 * Retrieves URL for an attachment thumbnail.
    67066706 *
    67076707 * @since 2.1.0
    6708  *
    6709  * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`.
    6710  * @return string|false Thumbnail file path on success, false on failure.
    6711  */
    6712 function wp_get_attachment_thumb_file( $post_id = 0 ) {
    6713     $post_id = (int) $post_id;
    6714     $post    = get_post( $post_id );
    6715 
    6716     if ( ! $post ) {
    6717         return false;
    6718     }
    6719 
    6720     $imagedata = wp_get_attachment_metadata( $post->ID );
    6721     if ( ! is_array( $imagedata ) ) {
    6722         return false;
    6723     }
    6724 
    6725     $file = get_attached_file( $post->ID );
    6726 
    6727     if ( ! empty( $imagedata['thumb'] ) ) {
    6728         $thumbfile = str_replace( wp_basename( $file ), $imagedata['thumb'], $file );
    6729         if ( file_exists( $thumbfile ) ) {
    6730             /**
    6731              * Filters the attachment thumbnail file path.
    6732              *
    6733              * @since 2.1.0
    6734              *
    6735              * @param string $thumbfile File path to the attachment thumbnail.
    6736              * @param int    $post_id   Attachment ID.
    6737              */
    6738             return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID );
    6739         }
    6740     }
    6741     return false;
    6742 }
    6743 
    6744 /**
    6745  * Retrieves URL for an attachment thumbnail.
    6746  *
    6747  * @since 2.1.0
     6708 * @since 6.1.0 Changed to use wp_get_attachment_image_url().
    67486709 *
    67496710 * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`.
     
    67526713function wp_get_attachment_thumb_url( $post_id = 0 ) {
    67536714    $post_id = (int) $post_id;
    6754     $post    = get_post( $post_id );
    6755 
    6756     if ( ! $post ) {
     6715
     6716    // This uses image_downsize() which also looks for the (very) old format $image_meta['thumb']
     6717    // when the newer format $image_meta['sizes']['thumbnail'] doesn't exist.
     6718    $thumbnail_url = wp_get_attachment_image_url( $post_id, 'thumbnail' );
     6719
     6720    if ( empty( $thumbnail_url ) ) {
    67576721        return false;
    67586722    }
    6759 
    6760     $url = wp_get_attachment_url( $post->ID );
    6761     if ( ! $url ) {
    6762         return false;
    6763     }
    6764 
    6765     $sized = image_downsize( $post_id, 'thumbnail' );
    6766     if ( $sized ) {
    6767         return $sized[0];
    6768     }
    6769 
    6770     $thumb = wp_get_attachment_thumb_file( $post->ID );
    6771     if ( ! $thumb ) {
    6772         return false;
    6773     }
    6774 
    6775     $url = str_replace( wp_basename( $url ), wp_basename( $thumb ), $url );
    67766723
    67776724    /**
     
    67806727     * @since 2.1.0
    67816728     *
    6782      * @param string $url    URL for the attachment thumbnail.
    6783      * @param int    $post_id Attachment ID.
     6729     * @param string $thumbnail_url URL for the attachment thumbnail.
     6730     * @param int    $post_id       Attachment ID.
    67846731     */
    6785     return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID );
     6732    return apply_filters( 'wp_get_attachment_thumb_url', $thumbnail_url, $post_id );
    67866733}
    67876734
Note: See TracChangeset for help on using the changeset viewer.