From 2815180607232ac7aac6f241083874ea061d6a4d Mon Sep 17 00:00:00 2001 From: Debarghya Banerjee Date: Tue, 22 Oct 2024 16:17:42 +0530 Subject: [PATCH 1/4] Fix the boolean attributes of wp_audio_shortcode --- src/wp-includes/media.php | 19 +++++++++++++++++-- tests/phpunit/tests/media.php | 11 +++++++---- .../tests/widgets/wpWidgetMediaAudio.php | 2 +- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 56389c5e1450f..5849102b6a097 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -3337,6 +3337,7 @@ function wp_audio_shortcode( $attr, $content = '' ) { 'src' => '', 'loop' => '', 'autoplay' => '', + 'muted' => false, 'preload' => 'none', 'class' => 'wp-audio-shortcode', 'style' => 'width: 100%;', @@ -3416,12 +3417,13 @@ function wp_audio_shortcode( $attr, $content = '' ) { 'id' => sprintf( 'audio-%d-%d', $post_id, $instance ), 'loop' => wp_validate_boolean( $atts['loop'] ), 'autoplay' => wp_validate_boolean( $atts['autoplay'] ), + 'muted' => wp_validate_boolean( $atts['muted'] ), 'preload' => $atts['preload'], 'style' => $atts['style'], ); // These ones should just be omitted altogether if they are blank. - foreach ( array( 'loop', 'autoplay', 'preload' ) as $a ) { + foreach ( array( 'loop', 'autoplay', 'preload', 'muted' ) as $a ) { if ( empty( $html_atts[ $a ] ) ) { unset( $html_atts[ $a ] ); } @@ -3430,7 +3432,20 @@ function wp_audio_shortcode( $attr, $content = '' ) { $attr_strings = array(); foreach ( $html_atts as $k => $v ) { - $attr_strings[] = $k . '="' . esc_attr( $v ) . '"'; + if ( in_array( $k, array( 'loop', 'autoplay', 'muted' ), true ) && true === $v ) { + // Add boolean attributes without a value + $attr_strings[] = esc_attr( $k ); + } elseif ( 'preload' === $k && ! empty( $v ) ) { + // Handle the preload attribute with specific allowed values + $allowed_preload_values = array( 'none', 'metadata', 'auto' ); + if ( in_array( $v, $allowed_preload_values, true ) ) { + $attr_strings[] = sprintf( '%s="%s"', esc_attr( $k ), esc_attr( $v ) ); + } + // If the value is not allowed, you can log a warning or handle it as needed + } else { + // For other attributes, include the value + $attr_strings[] = $k . '="' . esc_attr( $v ) . '"'; + } } $html = ''; diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 0d862bdb51b5c..85b882abcb43a 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -941,6 +941,7 @@ public function test_wp_audio_shortcode_attributes() { $this->assertStringContainsString( 'src="https://example.com/foo.mp3', $actual ); $this->assertStringNotContainsString( 'loop', $actual ); $this->assertStringNotContainsString( 'autoplay', $actual ); + $this->assertStringNotContainsString( 'muted', $actual ); $this->assertStringContainsString( 'preload="none"', $actual ); $this->assertStringContainsString( 'class="wp-audio-shortcode"', $actual ); $this->assertStringContainsString( 'style="width: 100%;"', $actual ); @@ -950,16 +951,18 @@ public function test_wp_audio_shortcode_attributes() { 'src' => 'https://example.com/foo.mp3', 'loop' => true, 'autoplay' => true, - 'preload' => true, + 'muted' => true, + 'preload' => 'none', 'class' => 'foobar', 'style' => 'padding:0;', ) ); $this->assertStringContainsString( 'src="https://example.com/foo.mp3', $actual ); - $this->assertStringContainsString( 'loop="1"', $actual ); - $this->assertStringContainsString( 'autoplay="1"', $actual ); - $this->assertStringContainsString( 'preload="1"', $actual ); + $this->assertStringContainsString( 'loop', $actual ); + $this->assertStringContainsString( 'autoplay', $actual ); + $this->assertStringContainsString( 'muted', $actual ); + $this->assertStringContainsString( 'preload="none"', $actual ); $this->assertStringContainsString( 'class="foobar"', $actual ); $this->assertStringContainsString( 'style="padding:0;"', $actual ); } diff --git a/tests/phpunit/tests/widgets/wpWidgetMediaAudio.php b/tests/phpunit/tests/widgets/wpWidgetMediaAudio.php index a02c1bb042f7b..20aee5f30baf4 100644 --- a/tests/phpunit/tests/widgets/wpWidgetMediaAudio.php +++ b/tests/phpunit/tests/widgets/wpWidgetMediaAudio.php @@ -272,7 +272,7 @@ public function test_render_media() { // Custom attributes. $this->assertStringContainsString( 'preload="auto"', $output ); - $this->assertStringContainsString( 'loop="1"', $output ); + $this->assertStringContainsString( 'loop', $output ); } /** From dc68c29ae3bf5428e6e8b5dd54980df4ea0ba829 Mon Sep 17 00:00:00 2001 From: Debarghya Banerjee Date: Wed, 30 Oct 2024 02:01:54 +0530 Subject: [PATCH 2/4] Update teh name of the variable to make it self explanatory --- src/wp-includes/media.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 6dc6c3c78883e..ea48a7612554c 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -3431,20 +3431,20 @@ function wp_audio_shortcode( $attr, $content = '' ) { $attr_strings = array(); - foreach ( $html_atts as $k => $v ) { - if ( in_array( $k, array( 'loop', 'autoplay', 'muted' ), true ) && true === $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 a value - $attr_strings[] = esc_attr( $k ); - } elseif ( 'preload' === $k && ! empty( $v ) ) { + $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( $v, $allowed_preload_values, true ) ) { - $attr_strings[] = sprintf( '%s="%s"', esc_attr( $k ), esc_attr( $v ) ); + if ( in_array( $attribute_value, $allowed_preload_values, true ) ) { + $attr_strings[] = sprintf( '%s="%s"', esc_attr( $attribute_name ), esc_attr( $attribute_value ) ); } // If the value is not allowed, you can log a warning or handle it as needed } else { // For other attributes, include the value - $attr_strings[] = $k . '="' . esc_attr( $v ) . '"'; + $attr_strings[] = $attribute_name . '="' . esc_attr( $attribute_value ) . '"'; } } From 5bc43db36936a9d80651199a1971450365bd36cb Mon Sep 17 00:00:00 2001 From: Debarghya Banerjee Date: Tue, 28 Jan 2025 13:31:56 +0530 Subject: [PATCH 3/4] Fix the comments and use sprintf --- src/wp-includes/media.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 02174e5b9b04a..7dc7841d43b1c 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -3486,18 +3486,17 @@ function wp_audio_shortcode( $attr, $content = '' ) { 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 a value + // Add boolean attributes without a value. $attr_strings[] = esc_attr( $attribute_name ); } elseif ( 'preload' === $attribute_name && ! empty( $attribute_value ) ) { - // Handle the preload attribute with specific allowed values + // 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 ) ); } - // If the value is not allowed, you can log a warning or handle it as needed } else { - // For other attributes, include the value - $attr_strings[] = $attribute_name . '="' . esc_attr( $attribute_value ) . '"'; + // For other attributes, include the value. + $attr_strings[] = sprintf( '%s="%s"', esc_attr( $attribute_name ), esc_attr( $attribute_value ) ); } } From 9642f5416d05288d2a4e65000facced0432de322 Mon Sep 17 00:00:00 2001 From: Joe Dolson Date: Sun, 16 Mar 2025 13:24:20 -0500 Subject: [PATCH 4/4] Documentat added default attribute; change type to match type passed from shortcodes. --- src/wp-includes/media.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 7fd53621b8577..da46ef90a7dec 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -3345,6 +3345,7 @@ function wp_get_attachment_id3_keys( $attachment, $context = 'display' ) { * WordPress mp3s in a post. * * @since 3.6.0 + * @since 6.8.0 Added the 'muted' attribute. * * @param array $attr { * Attributes of the audio shortcode. @@ -3352,6 +3353,7 @@ function wp_get_attachment_id3_keys( $attachment, $context = 'display' ) { * @type string $src URL to the source of the audio file. Default empty. * @type string $loop The 'loop' attribute for the `