From 081e51d94bdba30189c9053127730da4f41567c3 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sun, 21 Aug 2016 06:14:37 +0000 Subject: [PATCH] Media: use `wp_get_attachment_metadata()` instead of `get_post_meta()` where appropriate. Adds unit test. Props JorritSchippers. Fixes #36246. git-svn-id: https://develop.svn.wordpress.org/trunk@38296 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/media.php | 10 +++++----- tests/phpunit/tests/media.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 4eb2886205..9fac913386 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -861,7 +861,7 @@ function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = fa // Generate 'srcset' and 'sizes' if not already present. if ( empty( $attr['srcset'] ) ) { - $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true ); + $image_meta = wp_get_attachment_metadata( $attachment_id ); if ( is_array( $image_meta ) ) { $size_array = array( absint( $width ), absint( $height ) ); @@ -990,7 +990,7 @@ function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $imag } if ( ! is_array( $image_meta ) ) { - $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true ); + $image_meta = wp_get_attachment_metadata( $attachment_id ); } $image_src = $image[0]; @@ -1206,7 +1206,7 @@ function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image } if ( ! is_array( $image_meta ) ) { - $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true ); + $image_meta = wp_get_attachment_metadata( $attachment_id ); } $image_src = $image[0]; @@ -1239,7 +1239,7 @@ function wp_calculate_image_sizes( $size, $image_src = null, $image_meta = null, $width = absint( $size[0] ); } elseif ( is_string( $size ) ) { if ( ! $image_meta && $attachment_id ) { - $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true ); + $image_meta = wp_get_attachment_metadata( $attachment_id ); } if ( is_array( $image_meta ) ) { @@ -1314,7 +1314,7 @@ function wp_make_content_images_responsive( $content ) { } foreach ( $selected_images as $image => $attachment_id ) { - $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true ); + $image_meta = wp_get_attachment_metadata( $attachment_id ); $content = str_replace( $image, wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ), $content ); } diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 5d8449fbdc..12e30e6eb7 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -1755,6 +1755,38 @@ EOF; $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ) ); } + + /** + * Tests if wp_get_attachment_image() uses wp_get_attachment_metadata(). + * + * In this way, the meta data can be filtered using the filter + * `wp_get_attachment_metadata`. + * + * The test checks if the image size that is added in the filter is + * used in the output of `wp_get_attachment_image()`. + * + * @ticket 36246 + */ + function test_wp_get_attachment_image_should_use_wp_get_attachment_metadata() { + add_filter( 'wp_get_attachment_metadata', array( $this, '_filter_36246' ), 10, 2 ); + + $actual = wp_get_attachment_image( self::$large_id, 'testsize' ); + $expected = 'test-image-large.png'; + + remove_filter( 'wp_get_attachment_metadata', array( $this, '_filter_36246' ) ); + + $this->assertSame( $expected, $actual ); + } + + function _filter_36246( $data, $attachment_id ) { + $data['sizes']['testsize'] = array( + 'file' => 'test-image-testsize-999x999.png', + 'width' => 999, + 'height' => 999, + 'mime-type' => 'image/png', + ); + return $data; + } } /**