Make WordPress Core

Changeset 55171


Ignore:
Timestamp:
01/31/2023 07:39:51 PM (4 years ago)
Author:
flixos90
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r54802 r55171  
    10241024 *                                  Defaults to 'lazy', depending on wp_lazy_loading_enabled().
    10251025 *     @type string       $decoding The 'decoding' attribute value. Possible values are
    1026  *                                  'async' (default), 'sync', or 'auto'.
     1026 *                                  'async' (default), 'sync', or 'auto'. Passing false or an empty
     1027 *                                  string will result in the attribute being omitted.
    10271028 * }
    10281029 * @return string HTML img element or empty string on failure.
     
    10561057
    10571058                $attr = wp_parse_args( $attr, $default_attr );
     1059
     1060                // Omit the `decoding` attribute if the value is invalid according to the spec.
     1061                if ( empty( $attr['decoding'] ) || ! in_array( $attr['decoding'], array( 'async', 'sync', 'auto' ), true ) ) {
     1062                        unset( $attr['decoding'] );
     1063                }
    10581064
    10591065                // If the default value of `lazy` for the `loading` attribute is overridden
  • trunk/tests/phpunit/tests/media.php

    r54891 r55171  
    32803280
    32813281        /**
     3282         * @ticket 57086
     3283         *
     3284         * @dataProvider data_wp_get_attachment_image_decoding_attr
     3285         *
     3286         * @covers ::wp_get_attachment_image
     3287         */
     3288        public function test_wp_get_attachment_image_decoding_attr( $decoding, $expected ) {
     3289                if ( 'no value' === $decoding ) {
     3290                        $image = wp_get_attachment_image( self::$large_id, 'thumbnail', false, array() );
     3291                } else {
     3292                        $image = wp_get_attachment_image( self::$large_id, 'thumbnail', false, array( 'decoding' => $decoding ) );
     3293                }
     3294
     3295                if ( 'no value' === $expected ) {
     3296                        $this->assertStringNotContainsString( ' decoding=', $image );
     3297                } else {
     3298                        $this->assertStringContainsString( ' decoding="' . esc_attr( $expected ) . '"', $image );
     3299                }
     3300        }
     3301
     3302        /**
     3303         * Data provider for test_wp_get_attachment_image_decoding_attr().
     3304         *
     3305         * @return array[]
     3306         */
     3307        public function data_wp_get_attachment_image_decoding_attr() {
     3308                return array(
     3309                        'default'     => array(
     3310                                'decoding' => 'no value',
     3311                                'expected' => 'async',
     3312                        ),
     3313                        'async'       => array(
     3314                                'decoding' => 'async',
     3315                                'expected' => 'async',
     3316                        ),
     3317                        'sync'        => array(
     3318                                'decoding' => 'sync',
     3319                                'expected' => 'sync',
     3320                        ),
     3321                        'auto'        => array(
     3322                                'decoding' => 'auto',
     3323                                'expected' => 'auto',
     3324                        ),
     3325                        'empty'       => array(
     3326                                'decoding' => '',
     3327                                'expected' => 'no value',
     3328                        ),
     3329                        'false'       => array(
     3330                                'decoding' => false,
     3331                                'expected' => 'no value',
     3332                        ),
     3333                        'null'        => array(
     3334                                'decoding' => null,
     3335                                'expected' => 'no value',
     3336                        ),
     3337                        'zero'        => array(
     3338                                'decoding' => 0,
     3339                                'expected' => 'no value',
     3340                        ),
     3341                        'zero string' => array(
     3342                                'decoding' => '0',
     3343                                'expected' => 'no value',
     3344                        ),
     3345                        'zero float'  => array(
     3346                                'decoding' => 0.0,
     3347                                'expected' => 'no value',
     3348                        ),
     3349                        'invalid'     => array(
     3350                                'decoding' => 'invalid',
     3351                                'expected' => 'no value',
     3352                        ),
     3353                );
     3354        }
     3355
     3356        /**
    32823357         * @ticket 44427
    32833358         * @ticket 50425
Note: See TracChangeset for help on using the changeset viewer.