Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/wp-includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -3345,13 +3345,15 @@ 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.
*
* @type string $src URL to the source of the audio file. Default empty.
* @type string $loop The 'loop' attribute for the `<audio>` element. Default empty.
* @type string $autoplay The 'autoplay' attribute for the `<audio>` element. Default empty.
* @type string $muted The 'muted' attribute for the `<audio>` element. Default 'false'.
* @type string $preload The 'preload' attribute for the `<audio>` element. Default 'none'.
* @type string $class The 'class' attribute for the `<audio>` element. Default 'wp-audio-shortcode'.
* @type string $style The 'style' attribute for the `<audio>` element. Default 'width: 100%;'.
Expand Down Expand Up @@ -3390,6 +3392,7 @@ function wp_audio_shortcode( $attr, $content = '' ) {
'src' => '',
'loop' => '',
'autoplay' => '',
'muted' => 'false',
'preload' => 'none',
'class' => 'wp-audio-shortcode',
'style' => 'width: 100%;',
Expand Down Expand Up @@ -3469,21 +3472,34 @@ 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 ] );
}
}

$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 a value.
$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 ) );
}
} else {
// For other attributes, include the value.
$attr_strings[] = sprintf( '%s="%s"', esc_attr( $attribute_name ), esc_attr( $attribute_value ) );
}
}

$html = '';
Expand Down
11 changes: 7 additions & 4 deletions tests/phpunit/tests/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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 );
}
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/widgets/wpWidgetMediaAudio.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down
Loading