Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/wp-includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,6 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f
list( $src, $width, $height ) = $image;

$attachment = get_post( $attachment_id );
$hwstring = image_hwstring( $width, $height );
$size_class = $size;

if ( is_array( $size_class ) ) {
Expand All @@ -1090,15 +1089,14 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f
*
* @param string $context The context. Default 'wp_get_attachment_image'.
*/
$context = apply_filters( 'wp_get_attachment_image_context', 'wp_get_attachment_image' );
$attr = wp_parse_args( $attr, $default_attr );
$context = apply_filters( 'wp_get_attachment_image_context', 'wp_get_attachment_image' );
$attr = wp_parse_args( $attr, $default_attr );
$attr['width'] = $width;
$attr['height'] = $height;

$loading_attr = $attr;
$loading_attr['width'] = $width;
$loading_attr['height'] = $height;
$loading_optimization_attr = wp_get_loading_optimization_attributes(
'img',
$loading_attr,
$attr,
$context
);

Expand Down Expand Up @@ -1169,8 +1167,16 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f
*/
$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );

$attr = array_map( 'esc_attr', $attr );
$html = rtrim( "<img $hwstring" );
if ( isset( $attr['height'] ) && is_numeric( $attr['height'] ) ) {
$height = absint( $attr['height'] );
}
if ( isset( $attr['width'] ) && is_numeric( $attr['width'] ) ) {
$width = absint( $attr['width'] );
}
unset( $attr['height'], $attr['width'] );
$attr = array_map( 'esc_attr', $attr );
$hwstring = image_hwstring( $width, $height );
$html = rtrim( "<img $hwstring" );

foreach ( $attr as $name => $value ) {
$html .= " $name=" . '"' . $value . '"';
Expand Down
47 changes: 47 additions & 0 deletions tests/phpunit/tests/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,53 @@ public function test_wp_get_attachment_image_filter_output() {
$this->assertSame( $expected, $output );
}

/**
* @ticket 14110
*/
public function test_wp_get_attachment_image_filter_with_height_width() {
$mock_action = new MockAction();
add_filter( 'wp_get_attachment_image_attributes', array( $mock_action, 'filter' ) );
wp_get_attachment_image( self::$large_id );
$args = $mock_action->get_args();
$this->assertArrayHasKey( '0', $args, 'First argument should be an array.' );
$this->assertArrayHasKey( '0', $args[0], 'First argument should be an array.' );
$this->assertArrayHasKey( 'width', $args[0][0], 'Width should be set.' );
$this->assertArrayHasKey( 'height', $args[0][0], 'Height should be set.' );
}

/**
* @ticket 14110
*/
public function test_wp_get_attachment_image_filter_change_height_width() {
add_filter(
'wp_get_attachment_image_attributes',
static function ( $args ) {
$args['height'] = '999';
$args['width'] = '999';
return $args;
}
);
$output = wp_get_attachment_image( self::$large_id );
$this->assertStringContainsString( 'width="999"', $output, 'Width should be changed.' );
$this->assertStringContainsString( 'height="999"', $output, 'Height should be changed.' );
}

/**
* @ticket 14110
*/
public function test_wp_get_attachment_image_filter_unset_height_width() {
add_filter(
'wp_get_attachment_image_attributes',
static function ( $args ) {
unset( $args['height'], $args['width'] );
return $args;
}
);
$output = wp_get_attachment_image( self::$large_id );
$this->assertStringContainsString( 'width="150"', $output, 'Width should not be changed.' );
$this->assertStringContainsString( 'height="150"', $output, 'Height should not be changed.' );
}

public function filter_wp_get_attachment_image() {
return 'Override wp_get_attachment_image';
}
Expand Down
Loading