diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
index 7e3f47aba1e9..36e3293d39da 100644
a
|
b
|
function create_initial_post_types() { |
705 | 705 | /** |
706 | 706 | * Retrieves attached file path based on attachment ID. |
707 | 707 | * |
| 708 | * Will return intermediate size path if $size param is provided. |
| 709 | * |
708 | 710 | * By default the path will go through the 'get_attached_file' filter, but |
709 | 711 | * passing a true to the $unfiltered argument of get_attached_file() will |
710 | 712 | * return the file path unfiltered. |
… |
… |
function create_initial_post_types() { |
715 | 717 | * attached filename through a filter. |
716 | 718 | * |
717 | 719 | * @since 2.0.0 |
| 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 ''. |
718 | 726 | * |
719 | | * @param int $attachment_id Attachment ID. |
720 | | * @param bool $unfiltered Optional. Whether to apply filters. Default false. |
721 | 727 | * @return string|false The file path to where the attached file should be, false otherwise. |
722 | 728 | */ |
723 | | function get_attached_file( $attachment_id, $unfiltered = false ) { |
724 | | $file = get_post_meta( $attachment_id, '_wp_attached_file', true ); |
| 729 | function 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 | } |
725 | 741 | |
726 | 742 | // If the file is relative, prepend upload dir. |
727 | 743 | 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 82fe7dabd61b..4afe819c5199 100644
a
|
b
|
public function tear_down() { |
18 | 18 | * Tests that filesize meta is generated for JPEGs. |
19 | 19 | * |
20 | 20 | * @ticket 49412 |
| 21 | * @ticket 51780 |
21 | 22 | * |
22 | 23 | * @covers ::wp_create_image_subsizes |
23 | 24 | */ |
… |
… |
public function test_wp_generate_attachment_metadata_includes_filesize_in_jpg_me |
28 | 29 | |
29 | 30 | $this->assertSame( wp_filesize( get_attached_file( $attachment ) ), $metadata['filesize'] ); |
30 | 31 | |
31 | | foreach ( $metadata['sizes'] as $intermediate_size ) { |
| 32 | foreach ( $metadata['sizes'] as $size => $intermediate_size ) { |
32 | 33 | $this->assertArrayHasKey( 'filesize', $intermediate_size ); |
33 | 34 | $this->assertNotEmpty( $intermediate_size['filesize'] ); |
34 | 35 | $this->assertIsNumeric( $intermediate_size['filesize'] ); |
| 36 | $this->assertStringContainsString( $intermediate_size['file'], get_attached_file( $attachment, false, $size ) ); |
35 | 37 | } |
36 | 38 | } |
37 | 39 | |