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
This commit is contained in:
parent
c593c6fa95
commit
081e51d94b
|
@ -861,7 +861,7 @@ function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = fa
|
||||||
|
|
||||||
// Generate 'srcset' and 'sizes' if not already present.
|
// Generate 'srcset' and 'sizes' if not already present.
|
||||||
if ( empty( $attr['srcset'] ) ) {
|
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 ) ) {
|
if ( is_array( $image_meta ) ) {
|
||||||
$size_array = array( absint( $width ), absint( $height ) );
|
$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 ) ) {
|
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];
|
$image_src = $image[0];
|
||||||
|
@ -1206,7 +1206,7 @@ function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! is_array( $image_meta ) ) {
|
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];
|
$image_src = $image[0];
|
||||||
|
@ -1239,7 +1239,7 @@ function wp_calculate_image_sizes( $size, $image_src = null, $image_meta = null,
|
||||||
$width = absint( $size[0] );
|
$width = absint( $size[0] );
|
||||||
} elseif ( is_string( $size ) ) {
|
} elseif ( is_string( $size ) ) {
|
||||||
if ( ! $image_meta && $attachment_id ) {
|
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 ) ) {
|
if ( is_array( $image_meta ) ) {
|
||||||
|
@ -1314,7 +1314,7 @@ function wp_make_content_images_responsive( $content ) {
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ( $selected_images as $image => $attachment_id ) {
|
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 );
|
$content = str_replace( $image, wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ), $content );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1755,6 +1755,38 @@ EOF;
|
||||||
|
|
||||||
$this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ) );
|
$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 = '<img width="999" height="999" src="http://example.org/wp-content/uploads/2016/03/test-image-testsize-999x999.png" class="attachment-testsize size-testsize" alt="test-image-large.png" srcset="http://example.org/wp-content/uploads/2016/03/test-image-large-150x150.png 150w, http://example.org/wp-content/uploads/2016/03/test-image-testsize-999x999.png 999w" sizes="(max-width: 999px) 100vw, 999px" />';
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue