Media: Allow to omit decoding="async" on tags from wp_get_attachment_image().

When adding `decoding="async"` to images was introduced in [53480], it did not provide the ability to customize that behavior specifically for image tags returned from `wp_get_attachment_image()`.

With this changeset it is now possible to explicitly provide a `decoding` value of e.g. boolean `false` in the `$attr` parameter of the function, to ensure the attribute is omitted.

Props maximej, adamsilverstein, flixos90, costdev, peterwilsoncc, mukesh27.
Fixes #57086.


git-svn-id: https://develop.svn.wordpress.org/trunk@55171 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Felix Arntz
2023-01-31 19:39:51 +00:00
parent a4d42f09d9
commit 6036ee03af
2 changed files with 82 additions and 1 deletions

View File

@@ -3278,6 +3278,81 @@ EOF;
$this->assertStringNotContainsString( ' loading=', $img );
}
/**
* @ticket 57086
*
* @dataProvider data_wp_get_attachment_image_decoding_attr
*
* @covers ::wp_get_attachment_image
*/
public function test_wp_get_attachment_image_decoding_attr( $decoding, $expected ) {
if ( 'no value' === $decoding ) {
$image = wp_get_attachment_image( self::$large_id, 'thumbnail', false, array() );
} else {
$image = wp_get_attachment_image( self::$large_id, 'thumbnail', false, array( 'decoding' => $decoding ) );
}
if ( 'no value' === $expected ) {
$this->assertStringNotContainsString( ' decoding=', $image );
} else {
$this->assertStringContainsString( ' decoding="' . esc_attr( $expected ) . '"', $image );
}
}
/**
* Data provider for test_wp_get_attachment_image_decoding_attr().
*
* @return array[]
*/
public function data_wp_get_attachment_image_decoding_attr() {
return array(
'default' => array(
'decoding' => 'no value',
'expected' => 'async',
),
'async' => array(
'decoding' => 'async',
'expected' => 'async',
),
'sync' => array(
'decoding' => 'sync',
'expected' => 'sync',
),
'auto' => array(
'decoding' => 'auto',
'expected' => 'auto',
),
'empty' => array(
'decoding' => '',
'expected' => 'no value',
),
'false' => array(
'decoding' => false,
'expected' => 'no value',
),
'null' => array(
'decoding' => null,
'expected' => 'no value',
),
'zero' => array(
'decoding' => 0,
'expected' => 'no value',
),
'zero string' => array(
'decoding' => '0',
'expected' => 'no value',
),
'zero float' => array(
'decoding' => 0.0,
'expected' => 'no value',
),
'invalid' => array(
'decoding' => 'invalid',
'expected' => 'no value',
),
);
}
/**
* @ticket 44427
* @ticket 50425