Skip to content

Commit 2a9b332

Browse files
committed
fixing no extension urls
1 parent 570cc92 commit 2a9b332

2 files changed

Lines changed: 60 additions & 11 deletions

File tree

src/wp-includes/media.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3468,9 +3468,10 @@ function wp_audio_shortcode( $attr, $content = '' ) {
34683468

34693469
$primary = false;
34703470
if ( ! empty( $atts['src'] ) ) {
3471-
$type = wp_check_filetype( $atts['src'], wp_get_mime_types() );
3471+
$type = wp_check_filetype( $atts['src'], wp_get_mime_types() );
3472+
$has_extension = (bool) pathinfo( wp_parse_url( $atts['src'], PHP_URL_PATH ), PATHINFO_EXTENSION );
34723473

3473-
if ( ! in_array( strtolower( $type['ext'] ), $default_types, true ) ) {
3474+
if ( $has_extension && ! in_array( strtolower( $type['ext'] ), $default_types, true ) ) {
34743475
return sprintf( '<a class="wp-embedded-audio" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) );
34753476
}
34763477

@@ -3567,17 +3568,21 @@ function wp_audio_shortcode( $attr, $content = '' ) {
35673568

35683569
$html = sprintf( '<audio %s controls="controls">', implode( ' ', $attr_strings ) );
35693570
$fileurl = '';
3570-
$source = '<source type="%s" src="%s" />';
35713571

35723572
foreach ( $default_types as $fallback ) {
35733573
if ( ! empty( $atts[ $fallback ] ) ) {
35743574
if ( empty( $fileurl ) ) {
35753575
$fileurl = $atts[ $fallback ];
35763576
}
35773577

3578-
$type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() );
3579-
$url = add_query_arg( '_', $instance, $atts[ $fallback ] );
3580-
$html .= sprintf( $source, $type['type'], esc_url( $url ) );
3578+
$type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() );
3579+
$url = add_query_arg( '_', $instance, $atts[ $fallback ] );
3580+
3581+
if ( ! empty( $type['type'] ) ) {
3582+
$html .= sprintf( '<source type="%s" src="%s" />', $type['type'], esc_url( $url ) );
3583+
} else {
3584+
$html .= sprintf( '<source src="%s" />', esc_url( $url ) );
3585+
}
35813586
}
35823587
}
35833588

@@ -3723,9 +3728,10 @@ function wp_video_shortcode( $attr, $content = '' ) {
37233728
$is_youtube = ( preg_match( $yt_pattern, $atts['src'] ) );
37243729

37253730
if ( ! $is_youtube && ! $is_vimeo ) {
3726-
$type = wp_check_filetype( $atts['src'], wp_get_mime_types() );
3731+
$type = wp_check_filetype( $atts['src'], wp_get_mime_types() );
3732+
$has_extension = (bool) pathinfo( wp_parse_url( $atts['src'], PHP_URL_PATH ), PATHINFO_EXTENSION );
37273733

3728-
if ( ! in_array( strtolower( $type['ext'] ), $default_types, true ) ) {
3734+
if ( $has_extension && ! in_array( strtolower( $type['ext'] ), $default_types, true ) ) {
37293735
return sprintf( '<a class="wp-embedded-video" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) );
37303736
}
37313737
}
@@ -3845,7 +3851,6 @@ function wp_video_shortcode( $attr, $content = '' ) {
38453851

38463852
$html = sprintf( '<video %s controls="controls">', implode( ' ', $attr_strings ) );
38473853
$fileurl = '';
3848-
$source = '<source type="%s" src="%s" />';
38493854

38503855
foreach ( $default_types as $fallback ) {
38513856
if ( ! empty( $atts[ $fallback ] ) ) {
@@ -3859,8 +3864,13 @@ function wp_video_shortcode( $attr, $content = '' ) {
38593864
} else {
38603865
$type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() );
38613866
}
3862-
$url = add_query_arg( '_', $instance, $atts[ $fallback ] );
3863-
$html .= sprintf( $source, $type['type'], esc_url( $url ) );
3867+
$url = add_query_arg( '_', $instance, $atts[ $fallback ] );
3868+
3869+
if ( ! empty( $type['type'] ) ) {
3870+
$html .= sprintf( '<source type="%s" src="%s" />', $type['type'], esc_url( $url ) );
3871+
} else {
3872+
$html .= sprintf( '<source src="%s" />', esc_url( $url ) );
3873+
}
38643874
}
38653875
}
38663876

tests/phpunit/tests/media.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,25 @@ public function test_wp_audio_shortcode_with_bad_attr() {
962962
);
963963
}
964964

965+
/**
966+
* Tests that a URL with no file extension (e.g. a 302 redirect) renders
967+
* an audio player instead of a plain link.
968+
*
969+
* @ticket 31689
970+
*/
971+
public function test_wp_audio_shortcode_with_redirect_url() {
972+
$actual = wp_audio_shortcode(
973+
array(
974+
'src' => 'https://onedrive.live.com/download?resid=123',
975+
)
976+
);
977+
978+
$this->assertStringContainsString( '<audio', $actual, 'Should render an audio player, not a link.' );
979+
$this->assertStringNotContainsString( '<a class="wp-embedded-audio"', $actual, 'Should not render a fallback link.' );
980+
$this->assertStringContainsString( 'src="https://onedrive.live.com/download?resid=123', $actual, 'Source URL should be present.' );
981+
$this->assertStringNotContainsString( 'type=""', $actual, 'Source tag should not have an empty type attribute.' );
982+
}
983+
965984
/**
966985
* @ticket 35367
967986
*/
@@ -1068,6 +1087,26 @@ public function test_wp_video_shortcode_with_bad_attr() {
10681087
);
10691088
}
10701089

1090+
/**
1091+
* Tests that a URL with no file extension (e.g. a 302 redirect) renders
1092+
* a video player instead of a plain link.
1093+
*
1094+
* @ticket 31689
1095+
* @depends test_video_shortcode_body
1096+
*/
1097+
public function test_wp_video_shortcode_with_redirect_url() {
1098+
$actual = wp_video_shortcode(
1099+
array(
1100+
'src' => 'https://onedrive.live.com/download?resid=123',
1101+
)
1102+
);
1103+
1104+
$this->assertStringContainsString( '<video', $actual, 'Should render a video player, not a link.' );
1105+
$this->assertStringNotContainsString( '<a class="wp-embedded-video"', $actual, 'Should not render a fallback link.' );
1106+
$this->assertStringContainsString( 'src="https://onedrive.live.com/download?resid=123', $actual, 'Source URL should be present.' );
1107+
$this->assertStringNotContainsString( 'type=""', $actual, 'Source tag should not have an empty type attribute.' );
1108+
}
1109+
10711110
/**
10721111
* @ticket 35367
10731112
* @ticket 54788

0 commit comments

Comments
 (0)