Make WordPress Core

Changeset 55199


Ignore:
Timestamp:
02/02/2023 11:38:49 PM (20 months ago)
Author:
joedolson
Message:

Media: Add argument to get_attached_file() for subsizes.

Add a $size argument to get_attached_file() to simplify getting the path to an intermediate image size.

Props paulschreiber, audrasjb, Mista-Flo.
Fixes #51780.

Location:
trunk
Files:
2 edited

Legend:

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

    r55169 r55199  
    706706 * Retrieves attached file path based on attachment ID.
    707707 *
     708 * Will return intermediate size path if $size param is provided.
     709 *
    708710 * By default the path will go through the 'get_attached_file' filter, but
    709711 * passing a true to the $unfiltered argument of get_attached_file() will
     
    716718 *
    717719 * @since 2.0.0
    718  *
    719  * @param int  $attachment_id Attachment ID.
    720  * @param bool $unfiltered    Optional. Whether to apply filters. Default false.
     720 * @since 6.2 The `$size` parameter was added
     721 *
     722 * @param int          $attachment_id Attachment ID.
     723 * @param bool         $unfiltered    Optional. Whether to apply filters. Default false.
     724 * @param string|int[] $size          Optional. Image size. Accepts any registered image size name, or an array
     725 *                                    of width and height values in pixels (in that order). Default ''.
     726 *
    721727 * @return string|false The file path to where the attached file should be, false otherwise.
    722728 */
    723 function get_attached_file( $attachment_id, $unfiltered = false ) {
    724     $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
     729function get_attached_file( $attachment_id, $unfiltered = false, $size = '' ) {
     730
     731    // Check for intermediate sizes first, otherwise fallback to original attachment size
     732    if ( ! empty( $size ) ) {
     733        $intermediate_image = image_get_intermediate_size( $attachment_id, $size );
     734        if ( ! $intermediate_image || ! isset( $intermediate_image['path'] ) ) {
     735            return false;
     736        }
     737        $file = $intermediate_image['path'];
     738    } else {
     739        $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
     740    }
    725741
    726742    // If the file is relative, prepend upload dir.
  • trunk/tests/phpunit/tests/media/wpGenerateAttachmentMetadata.php

    r54863 r55199  
    1919     *
    2020     * @ticket 49412
     21     * @ticket 51780
    2122     *
    2223     * @covers ::wp_create_image_subsizes
     
    2930        $this->assertSame( wp_filesize( get_attached_file( $attachment ) ), $metadata['filesize'] );
    3031
    31         foreach ( $metadata['sizes'] as $intermediate_size ) {
     32        foreach ( $metadata['sizes'] as $size => $intermediate_size ) {
    3233            $this->assertArrayHasKey( 'filesize', $intermediate_size );
    3334            $this->assertNotEmpty( $intermediate_size['filesize'] );
    3435            $this->assertIsNumeric( $intermediate_size['filesize'] );
     36            $this->assertStringContainsString( $intermediate_size['file'], get_attached_file( $attachment, false, $size ) );
    3537        }
    3638    }
Note: See TracChangeset for help on using the changeset viewer.