Skip to content

Commit 176afff

Browse files
committed
Media: Allow wp_check_filetype() to support query strings in URLs.
This changeset adjusts the regex in `wp_check_filetype()` to support query strings in URLs. Follow-up to [30640], [32172]. Props voldemortensen, johnbillion, layotte, dd32, atomicjack, supercleanse, spencercameron, ianmjones, audrasjb. Fixes #30377. git-svn-id: https://develop.svn.wordpress.org/trunk@52829 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 4cee32d commit 176afff

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

src/wp-includes/functions.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2981,8 +2981,9 @@ function wp_get_default_extension_for_mime_type( $mime_type ) {
29812981
* You can optionally define the mime array, if needed.
29822982
*
29832983
* @since 2.0.4
2984+
* @since 6.0.0 URLs are now supported.
29842985
*
2985-
* @param string $filename File name or path.
2986+
* @param string $filename File name, path, or URL.
29862987
* @param string[] $mimes Optional. Array of allowed mime types keyed by their file extension regex.
29872988
* @return array {
29882989
* Values for the extension and mime type.
@@ -2998,6 +2999,15 @@ function wp_check_filetype( $filename, $mimes = null ) {
29982999
$type = false;
29993000
$ext = false;
30003001

3002+
// Strip query args and fragment from filename to reveal extension.
3003+
$query_pos = strpos( $filename, '?' );
3004+
3005+
if ( false !== $query_pos ) {
3006+
$filename = substr_replace( $filename, '', $query_pos );
3007+
}
3008+
3009+
$filename = strip_fragment_from_url( $filename );
3010+
30013011
foreach ( $mimes as $ext_preg => $mime_match ) {
30023012
$ext_preg = '!\.(' . $ext_preg . ')$!i';
30033013
if ( preg_match( $ext_preg, $filename, $ext_matches ) ) {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/**
4+
* Tests for wp_check_filetype()
5+
*
6+
* @group functions.php
7+
* @covers ::wp_check_filetype
8+
*/
9+
class Tests_Functions_wpCheckFiletype extends WP_UnitTestCase {
10+
11+
public function data_url_filetypes() {
12+
return array(
13+
// Invalid or empty data:
14+
array( null, false ),
15+
array( '', false ),
16+
array( ' ', false ),
17+
18+
// Paths:
19+
array( 'file.jpg', 'jpg' ),
20+
array( 'C:\path\to\file.mp3', 'mp3' ),
21+
array( 'C:\path\to\file.mp3?file.jpg', 'mp3' ),
22+
array( 'C:\path\to\file.exe?file.jpg', false ),
23+
array( '/file.jpg', 'jpg' ),
24+
array( '/path/to/file.jpg', 'jpg' ),
25+
array( '/path/to/file.jpg', 'jpg' ),
26+
array( '/file.exe?file.jpg', false ),
27+
28+
// Absolute URLs:
29+
array( 'http://example.com', false ),
30+
array( 'http://example.com/', false ),
31+
array( 'http://example.com/wibble', false ),
32+
array( 'http://example.com/wibble/', false ),
33+
array( 'http://example.com/wibble.wobble', false ),
34+
array( 'http://example.com/wibble.mp3', 'mp3' ),
35+
array( 'http://example.com/wibble.mp3#wobble', 'mp3' ),
36+
array( 'http://example.com/wibble.mp3?wobble=true', 'mp3' ),
37+
array( 'http://example.com/wibble.mp3?wobble=true#wobble', 'mp3' ),
38+
array( 'http://example.mp3/', false ),
39+
array( 'http://example.com/file.mp3#file.jpg', 'mp3' ),
40+
array( 'http://example.com/file.mp3?file.jpg', 'mp3' ),
41+
array( 'http://example.com/file.exe#file.jpg', false ),
42+
array( 'http://example.com/file.exe?file.jpg', false ),
43+
array( 'http://example.com/file.mp3?foo=bar#?file=file.jpg', 'mp3' ),
44+
array( 'http://example.com?file.jpg', false ),
45+
);
46+
}
47+
48+
/**
49+
* @dataProvider data_url_filetypes
50+
*
51+
* @param string $url
52+
* @param string|false $expected
53+
*/
54+
public function test_url_ext( $url, $expected ) {
55+
$filetype = wp_check_filetype( $url );
56+
$this->assertSame( $expected, $filetype['ext'] );
57+
}
58+
}

0 commit comments

Comments
 (0)