Make WordPress Core

Changeset 59987


Ignore:
Timestamp:
03/16/2025 06:49:39 PM (5 weeks ago)
Author:
joedolson
Message:

Media: Add 'muted' attribute and normalize HTML attributes.

Add the 'muted' attribute to the audio shortcode. Fix boolean attributes to meet HTML5 standards. Replaces instances like attr="1" with attr for loop, autoplay, and muted, and improves handling of the preload attribute to only output valid values.

Props shub07, dmsnell, debarghyabanerjee, audrasjb, narenin, apermo, joedolson.
Fixes #61515.

Location:
trunk
Files:
3 edited

Legend:

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

    r59955 r59987  
    33463346 *
    33473347 * @since 3.6.0
     3348 * @since 6.8.0 Added the 'muted' attribute.
    33483349 *
    33493350 * @param array  $attr {
     
    33533354 *     @type string $loop     The 'loop' attribute for the `<audio>` element. Default empty.
    33543355 *     @type string $autoplay The 'autoplay' attribute for the `<audio>` element. Default empty.
     3356 *     @type string $muted    The 'muted' attribute for the `<audio>` element. Default 'false'.
    33553357 *     @type string $preload  The 'preload' attribute for the `<audio>` element. Default 'none'.
    33563358 *     @type string $class    The 'class' attribute for the `<audio>` element. Default 'wp-audio-shortcode'.
     
    33913393        'loop'     => '',
    33923394        'autoplay' => '',
     3395        'muted'    => 'false',
    33933396        'preload'  => 'none',
    33943397        'class'    => 'wp-audio-shortcode',
     
    34703473        'loop'     => wp_validate_boolean( $atts['loop'] ),
    34713474        'autoplay' => wp_validate_boolean( $atts['autoplay'] ),
     3475        'muted'    => wp_validate_boolean( $atts['muted'] ),
    34723476        'preload'  => $atts['preload'],
    34733477        'style'    => $atts['style'],
     
    34753479
    34763480    // These ones should just be omitted altogether if they are blank.
    3477     foreach ( array( 'loop', 'autoplay', 'preload' ) as $a ) {
     3481    foreach ( array( 'loop', 'autoplay', 'preload', 'muted' ) as $a ) {
    34783482        if ( empty( $html_atts[ $a ] ) ) {
    34793483            unset( $html_atts[ $a ] );
     
    34833487    $attr_strings = array();
    34843488
    3485     foreach ( $html_atts as $k => $v ) {
    3486         $attr_strings[] = $k . '="' . esc_attr( $v ) . '"';
     3489    foreach ( $html_atts as $attribute_name => $attribute_value ) {
     3490        if ( in_array( $attribute_name, array( 'loop', 'autoplay', 'muted' ), true ) && true === $attribute_value ) {
     3491            // Add boolean attributes without a value.
     3492            $attr_strings[] = esc_attr( $attribute_name );
     3493        } elseif ( 'preload' === $attribute_name && ! empty( $attribute_value ) ) {
     3494            // Handle the preload attribute with specific allowed values.
     3495            $allowed_preload_values = array( 'none', 'metadata', 'auto' );
     3496            if ( in_array( $attribute_value, $allowed_preload_values, true ) ) {
     3497                $attr_strings[] = sprintf( '%s="%s"', esc_attr( $attribute_name ), esc_attr( $attribute_value ) );
     3498            }
     3499        } else {
     3500            // For other attributes, include the value.
     3501            $attr_strings[] = sprintf( '%s="%s"', esc_attr( $attribute_name ), esc_attr( $attribute_value ) );
     3502        }
    34873503    }
    34883504
  • trunk/tests/phpunit/tests/media.php

    r59954 r59987  
    942942        $this->assertStringNotContainsString( 'loop', $actual );
    943943        $this->assertStringNotContainsString( 'autoplay', $actual );
     944        $this->assertStringNotContainsString( 'muted', $actual );
    944945        $this->assertStringContainsString( 'preload="none"', $actual );
    945946        $this->assertStringContainsString( 'class="wp-audio-shortcode"', $actual );
     
    951952                'loop'     => true,
    952953                'autoplay' => true,
    953                 'preload'  => true,
     954                'muted'    => true,
     955                'preload'  => 'none',
    954956                'class'    => 'foobar',
    955957                'style'    => 'padding:0;',
     
    958960
    959961        $this->assertStringContainsString( 'src="https://example.com/foo.mp3', $actual );
    960         $this->assertStringContainsString( 'loop="1"', $actual );
    961         $this->assertStringContainsString( 'autoplay="1"', $actual );
    962         $this->assertStringContainsString( 'preload="1"', $actual );
     962        $this->assertStringContainsString( 'loop', $actual );
     963        $this->assertStringContainsString( 'autoplay', $actual );
     964        $this->assertStringContainsString( 'muted', $actual );
     965        $this->assertStringContainsString( 'preload="none"', $actual );
    963966        $this->assertStringContainsString( 'class="foobar"', $actual );
    964967        $this->assertStringContainsString( 'style="padding:0;"', $actual );
  • trunk/tests/phpunit/tests/widgets/wpWidgetMediaAudio.php

    r52248 r59987  
    273273        // Custom attributes.
    274274        $this->assertStringContainsString( 'preload="auto"', $output );
    275         $this->assertStringContainsString( 'loop="1"', $output );
     275        $this->assertStringContainsString( 'loop', $output );
    276276    }
    277277
Note: See TracChangeset for help on using the changeset viewer.