diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 5d95b0a188596..ef3610c56f5eb 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -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 ) ) { @@ -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 ); @@ -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( " $value ) { $html .= " $name=" . '"' . $value . '"'; diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index a1e70c2ff1a3c..902c704b39988 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -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'; }