Media: Fix wp_audio_shortcode and wp_video_shortcode attributes handling.

Although documented, the `class` and `style` attributes were simply ignored.
Adds unit tests.

Fixes #35367.

git-svn-id: https://develop.svn.wordpress.org/trunk@36240 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler
2016-01-09 14:17:02 +00:00
parent 0e73cc3da4
commit 54134dd49b
2 changed files with 123 additions and 6 deletions

View File

@@ -2163,9 +2163,9 @@ 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 `<audio>` element. Default empty.
* @type string $autoplay The 'autoplay' attribute for the `<audio>` element. Default empty.
* @type string $preload The 'preload' attribute for the `<audio>` element. Default empty.
* @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%'.
* @type string $style The 'style' attribute for the `<audio>` element. Default 'width: 100%; visibility: hidden;'.
* }
* @param string $content Shortcode content.
* @return string|void HTML content to display audio.
@@ -2200,7 +2200,9 @@ function wp_audio_shortcode( $attr, $content = '' ) {
'src' => '',
'loop' => '',
'autoplay' => '',
'preload' => 'none'
'preload' => 'none',
'class' => 'wp-audio-shortcode',
'style' => 'width: 100%; visibility: hidden;'
);
foreach ( $default_types as $type ) {
$defaults_atts[$type] = '';
@@ -2262,13 +2264,15 @@ function wp_audio_shortcode( $attr, $content = '' ) {
*
* @param string $class CSS class or list of space-separated classes.
*/
$atts['class'] = apply_filters( 'wp_audio_shortcode_class', $atts['class'] );
$html_atts = array(
'class' => apply_filters( 'wp_audio_shortcode_class', 'wp-audio-shortcode' ),
'class' => $atts['class'],
'id' => sprintf( 'audio-%d-%d', $post_id, $instance ),
'loop' => wp_validate_boolean( $atts['loop'] ),
'autoplay' => wp_validate_boolean( $atts['autoplay'] ),
'preload' => $atts['preload'],
'style' => 'width: 100%; visibility: hidden;',
'style' => $atts['style'],
);
// These ones should just be omitted altogether if they are blank
@@ -2407,6 +2411,7 @@ function wp_video_shortcode( $attr, $content = '' ) {
'preload' => 'metadata',
'width' => 640,
'height' => 360,
'class' => 'wp-video-shortcode',
);
foreach ( $default_types as $type ) {
@@ -2496,8 +2501,10 @@ function wp_video_shortcode( $attr, $content = '' ) {
*
* @param string $class CSS class or list of space-separated classes.
*/
$atts['class'] = apply_filters( 'wp_video_shortcode_class', $atts['class'] );
$html_atts = array(
'class' => apply_filters( 'wp_video_shortcode_class', 'wp-video-shortcode' ),
'class' => $atts['class'],
'id' => sprintf( 'video-%d-%d', $post_id, $instance ),
'width' => absint( $atts['width'] ),
'height' => absint( $atts['height'] ),

View File

@@ -417,6 +417,116 @@ VIDEO;
$this->assertEquals( array( $video, $audio ), $matches2 );
}
/**
* @ticket 35367
*/
function test_wp_audio_shortcode_with_empty_params() {
$this->assertNull( wp_audio_shortcode( array() ) );
}
/**
* @ticket 35367
*/
function test_wp_audio_shortcode_with_bad_attr() {
$this->assertSame(
'<a class="wp-embedded-audio" href="https://example.com/foo.php">https://example.com/foo.php</a>',
wp_audio_shortcode( array(
'src' => 'https://example.com/foo.php',
) )
);
}
/**
* @ticket 35367
*/
function test_wp_audio_shortcode_attributes() {
$actual = wp_audio_shortcode( array(
'src' => 'https://example.com/foo.mp3',
) );
$this->assertContains( 'src="https://example.com/foo.mp3', $actual );
$this->assertNotContains( 'loop', $actual );
$this->assertNotContains( 'autoplay', $actual );
$this->assertContains( 'preload="none"', $actual );
$this->assertContains( 'class="wp-audio-shortcode"', $actual );
$this->assertContains( 'style="width: 100%; visibility: hidden;"', $actual );
$actual = wp_audio_shortcode( array(
'src' => 'https://example.com/foo.mp3',
'loop' => true,
'autoplay' => true,
'preload' => true,
'class' => 'foobar',
'style' => 'padding:0;',
) );
$this->assertContains( 'src="https://example.com/foo.mp3', $actual );
$this->assertContains( 'loop="1"', $actual );
$this->assertContains( 'autoplay="1"', $actual );
$this->assertContains( 'preload="1"', $actual );
$this->assertContains( 'class="foobar"', $actual );
$this->assertContains( 'style="padding:0;"', $actual );
}
/**
* @ticket 35367
* @depends test_video_shortcode_body
*/
function test_wp_video_shortcode_with_empty_params() {
$this->assertNull( wp_video_shortcode( array() ) );
}
/**
* @ticket 35367
* @depends test_video_shortcode_body
*/
function test_wp_video_shortcode_with_bad_attr() {
$this->assertSame(
'<a class="wp-embedded-video" href="https://example.com/foo.php">https://example.com/foo.php</a>',
wp_video_shortcode( array(
'src' => 'https://example.com/foo.php',
) )
);
}
/**
* @ticket 35367
* @depends test_video_shortcode_body
*/
function test_wp_video_shortcode_attributes() {
$actual = wp_video_shortcode( array(
'src' => 'https://example.com/foo.mp4',
) );
$this->assertContains( 'src="https://example.com/foo.mp4', $actual );
$this->assertNotContains( 'loop', $actual );
$this->assertNotContains( 'autoplay', $actual );
$this->assertContains( 'preload="metadata"', $actual );
$this->assertContains( 'width="640"', $actual );
$this->assertContains( 'height="360"', $actual );
$this->assertContains( 'class="wp-video-shortcode"', $actual );
$actual = wp_video_shortcode( array(
'src' => 'https://example.com/foo.mp4',
'poster' => 'https://example.com/foo.png',
'loop' => true,
'autoplay' => true,
'preload' => true,
'width' => 123,
'height' => 456,
'class' => 'foobar',
) );
$this->assertContains( 'src="https://example.com/foo.mp4', $actual );
$this->assertContains( 'poster="https://example.com/foo.png', $actual );
$this->assertContains( 'loop="1"', $actual );
$this->assertContains( 'autoplay="1"', $actual );
$this->assertContains( 'preload="1"', $actual );
$this->assertContains( 'width="123"', $actual );
$this->assertContains( 'height="456"', $actual );
$this->assertContains( 'class="foobar"', $actual );
}
/**
* Test [video] shortcode processing
*