From a501f4f40db8cb6563573d4b22d987457694d3b8 Mon Sep 17 00:00:00 2001 From: Joe Dolson Date: Thu, 2 Feb 2023 23:38:49 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@55199 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post.php | 24 +++++++++++++++---- .../media/wpGenerateAttachmentMetadata.php | 4 +++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 9f436775d7..a7e8ab49d7 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -705,6 +705,8 @@ function create_initial_post_types() { /** * Retrieves attached file path based on attachment ID. * + * Will return intermediate size path if $size param is provided. + * * By default the path will go through the 'get_attached_file' filter, but * passing a true to the $unfiltered argument of get_attached_file() will * return the file path unfiltered. @@ -715,13 +717,27 @@ function create_initial_post_types() { * attached filename through a filter. * * @since 2.0.0 + * @since 6.2 The `$size` parameter was added + * + * @param int $attachment_id Attachment ID. + * @param bool $unfiltered Optional. Whether to apply filters. Default false. + * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array + * of width and height values in pixels (in that order). Default ''. * - * @param int $attachment_id Attachment ID. - * @param bool $unfiltered Optional. Whether to apply filters. Default false. * @return string|false The file path to where the attached file should be, false otherwise. */ -function get_attached_file( $attachment_id, $unfiltered = false ) { - $file = get_post_meta( $attachment_id, '_wp_attached_file', true ); +function get_attached_file( $attachment_id, $unfiltered = false, $size = '' ) { + + // Check for intermediate sizes first, otherwise fallback to original attachment size + if ( ! empty( $size ) ) { + $intermediate_image = image_get_intermediate_size( $attachment_id, $size ); + if ( ! $intermediate_image || ! isset( $intermediate_image['path'] ) ) { + return false; + } + $file = $intermediate_image['path']; + } else { + $file = get_post_meta( $attachment_id, '_wp_attached_file', true ); + } // If the file is relative, prepend upload dir. if ( $file && 0 !== strpos( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) ) { diff --git a/tests/phpunit/tests/media/wpGenerateAttachmentMetadata.php b/tests/phpunit/tests/media/wpGenerateAttachmentMetadata.php index 82fe7dabd6..4afe819c51 100644 --- a/tests/phpunit/tests/media/wpGenerateAttachmentMetadata.php +++ b/tests/phpunit/tests/media/wpGenerateAttachmentMetadata.php @@ -18,6 +18,7 @@ class Tests_Media_wpGenerateAttachmentMetadata extends WP_UnitTestCase { * Tests that filesize meta is generated for JPEGs. * * @ticket 49412 + * @ticket 51780 * * @covers ::wp_create_image_subsizes */ @@ -28,10 +29,11 @@ class Tests_Media_wpGenerateAttachmentMetadata extends WP_UnitTestCase { $this->assertSame( wp_filesize( get_attached_file( $attachment ) ), $metadata['filesize'] ); - foreach ( $metadata['sizes'] as $intermediate_size ) { + foreach ( $metadata['sizes'] as $size => $intermediate_size ) { $this->assertArrayHasKey( 'filesize', $intermediate_size ); $this->assertNotEmpty( $intermediate_size['filesize'] ); $this->assertIsNumeric( $intermediate_size['filesize'] ); + $this->assertStringContainsString( $intermediate_size['file'], get_attached_file( $attachment, false, $size ) ); } }