Make WordPress Core

Changeset 59954


Ignore:
Timestamp:
03/09/2025 09:14:28 AM (7 weeks ago)
Author:
audrasjb
Message:

Media: Improve HTML5 compliance of wp_video_shortcode() boolean attributes.

This changeset updates wp_video_shortcode() to improve boolean attributes handling in accordance with HTML5 standards. Technically, it replaces attr="1" with attr for the loop, autoplay and muted attributes. The preload attribute is also updated to accept only allowed values: none, metadata, and auto. If a value outside of this list is provided, it will be ignored, preventing invalid attribute outputs.

Props jongycastillo, sabernhardt, joedolson, audrasjb, shub07, debarghyabanerjee.
Fixes #60178.

Location:
trunk
Files:
3 edited

Legend:

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

    r59865 r59954  
    34833483    $attr_strings = array();
    34843484
    3485     foreach ( $html_atts as $k => $v ) {
    3486         $attr_strings[] = $k . '="' . esc_attr( $v ) . '"';
     3485    foreach ( $html_atts as $attribute_name => $attribute_value ) {
     3486        if ( in_array( $attribute_name, array( 'loop', 'autoplay', 'muted' ), true ) && true === $attribute_value ) {
     3487            // Add boolean attributes without their value for true.
     3488            $attr_strings[] = esc_attr( $attribute_name );
     3489        } elseif ( 'preload' === $attribute_name && ! empty( $attribute_value ) ) {
     3490            // Handle the preload attribute with specific allowed values.
     3491            $allowed_preload_values = array( 'none', 'metadata', 'auto' );
     3492            if ( in_array( $attribute_value, $allowed_preload_values, true ) ) {
     3493                $attr_strings[] = sprintf( '%s="%s"', esc_attr( $attribute_name ), esc_attr( $attribute_value ) );
     3494            }
     3495        } elseif ( ! empty( $attribute_value ) ) {
     3496            // For non-boolean attributes, add them with their value.
     3497            $attr_strings[] = sprintf( '%s="%s"', esc_attr( $attribute_name ), esc_attr( $attribute_value ) );
     3498        }
    34873499    }
    34883500
  • trunk/tests/phpunit/tests/media.php

    r59473 r59954  
    10611061                'autoplay' => true,
    10621062                'muted'    => true,
    1063                 'preload'  => true,
     1063                'preload'  => 'metadata',
    10641064                'width'    => 123,
    10651065                'height'   => 456,
     
    10701070        $this->assertStringContainsString( 'src="https://example.com/foo.mp4', $actual );
    10711071        $this->assertStringContainsString( 'poster="https://example.com/foo.png', $actual );
    1072         $this->assertStringContainsString( 'loop="1"', $actual );
    1073         $this->assertStringContainsString( 'autoplay="1"', $actual );
     1072        $this->assertStringContainsString( 'loop', $actual );
     1073        $this->assertStringContainsString( 'autoplay', $actual );
    10741074        $this->assertStringContainsString( 'muted', $actual );
    1075         $this->assertStringContainsString( 'preload="1"', $actual );
     1075        $this->assertStringContainsString( 'preload="metadata"', $actual );
    10761076        $this->assertStringContainsString( 'width="123"', $actual );
    10771077        $this->assertStringContainsString( 'height="456"', $actual );
  • trunk/tests/phpunit/tests/widgets/wpWidgetMediaVideo.php

    r52248 r59954  
    278278        // Custom attributes.
    279279        $this->assertStringContainsString( 'preload="metadata"', $output );
    280         $this->assertStringContainsString( 'loop="1"', $output );
     280        $this->assertStringContainsString( 'loop', $output );
    281281
    282282        // Externally hosted video.
Note: See TracChangeset for help on using the changeset viewer.