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 .



git-svn-id: https://develop.svn.wordpress.org/trunk@59954 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jb Audras 2025-03-09 09:14:28 +00:00
parent 903b1fe840
commit f766d2602e
3 changed files with 19 additions and 7 deletions
src/wp-includes
tests/phpunit/tests

@ -3482,8 +3482,20 @@ function wp_audio_shortcode( $attr, $content = '' ) {
$attr_strings = array();
foreach ( $html_atts as $k => $v ) {
$attr_strings[] = $k . '="' . esc_attr( $v ) . '"';
foreach ( $html_atts as $attribute_name => $attribute_value ) {
if ( in_array( $attribute_name, array( 'loop', 'autoplay', 'muted' ), true ) && true === $attribute_value ) {
// Add boolean attributes without their value for true.
$attr_strings[] = esc_attr( $attribute_name );
} elseif ( 'preload' === $attribute_name && ! empty( $attribute_value ) ) {
// Handle the preload attribute with specific allowed values.
$allowed_preload_values = array( 'none', 'metadata', 'auto' );
if ( in_array( $attribute_value, $allowed_preload_values, true ) ) {
$attr_strings[] = sprintf( '%s="%s"', esc_attr( $attribute_name ), esc_attr( $attribute_value ) );
}
} elseif ( ! empty( $attribute_value ) ) {
// For non-boolean attributes, add them with their value.
$attr_strings[] = sprintf( '%s="%s"', esc_attr( $attribute_name ), esc_attr( $attribute_value ) );
}
}
$html = '';

@ -1060,7 +1060,7 @@ VIDEO;
'loop' => true,
'autoplay' => true,
'muted' => true,
'preload' => true,
'preload' => 'metadata',
'width' => 123,
'height' => 456,
'class' => 'foobar',
@ -1069,10 +1069,10 @@ VIDEO;
$this->assertStringContainsString( 'src="https://example.com/foo.mp4', $actual );
$this->assertStringContainsString( 'poster="https://example.com/foo.png', $actual );
$this->assertStringContainsString( 'loop="1"', $actual );
$this->assertStringContainsString( 'autoplay="1"', $actual );
$this->assertStringContainsString( 'loop', $actual );
$this->assertStringContainsString( 'autoplay', $actual );
$this->assertStringContainsString( 'muted', $actual );
$this->assertStringContainsString( 'preload="1"', $actual );
$this->assertStringContainsString( 'preload="metadata"', $actual );
$this->assertStringContainsString( 'width="123"', $actual );
$this->assertStringContainsString( 'height="456"', $actual );
$this->assertStringContainsString( 'class="foobar"', $actual );

@ -277,7 +277,7 @@ class Tests_Widgets_wpWidgetMediaVideo extends WP_UnitTestCase {
// Custom attributes.
$this->assertStringContainsString( 'preload="metadata"', $output );
$this->assertStringContainsString( 'loop="1"', $output );
$this->assertStringContainsString( 'loop', $output );
// Externally hosted video.
ob_start();