mirror of
git://develop.git.wordpress.org/
synced 2025-01-17 12:58:25 +01:00
Media: Improve handling of extensionless filenames.
Merge of [37756] to the 4.4 branch. See #37111. git-svn-id: https://develop.svn.wordpress.org/branches/4.4@37810 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
3a736df922
commit
ccd5d3e540
@ -1364,7 +1364,8 @@ function remove_accents( $string ) {
|
||||
* operating systems and special characters requiring special escaping
|
||||
* to manipulate at the command line. Replaces spaces and consecutive
|
||||
* dashes with a single dash. Trims period, dash and underscore from beginning
|
||||
* and end of filename.
|
||||
* and end of filename. It is not guaranteed that this function will return a
|
||||
* filename that is allowed to be uploaded.
|
||||
*
|
||||
* @since 2.1.0
|
||||
*
|
||||
@ -1389,6 +1390,14 @@ function sanitize_file_name( $filename ) {
|
||||
$filename = preg_replace( '/[\r\n\t -]+/', '-', $filename );
|
||||
$filename = trim( $filename, '.-_' );
|
||||
|
||||
if ( false === strpos( $filename, '.' ) ) {
|
||||
$mime_types = wp_get_mime_types();
|
||||
$filetype = wp_check_filetype( 'test.' . $filename, $mime_types );
|
||||
if ( $filetype['ext'] === $filename ) {
|
||||
$filename = 'unnamed-file.' . $filetype['ext'];
|
||||
}
|
||||
}
|
||||
|
||||
// Split the filename into a base and extension[s]
|
||||
$parts = explode('.', $filename);
|
||||
|
||||
|
@ -56,4 +56,15 @@ class Tests_Formatting_SanitizeFileName extends WP_UnitTestCase {
|
||||
function test_replaces_percent_sign() {
|
||||
$this->assertEquals( 'a22b.jpg', sanitize_file_name( 'a%22b.jpg' ) );
|
||||
}
|
||||
|
||||
function test_replaces_unnammed_file_extensions() {
|
||||
// Test filenames with both supported and unsupported extensions.
|
||||
$this->assertEquals( 'unnamed-file.exe', sanitize_file_name( '_.exe' ) );
|
||||
$this->assertEquals( 'unnamed-file.jpg', sanitize_file_name( '_.jpg' ) );
|
||||
}
|
||||
|
||||
function test_replaces_unnammed_file_extensionless() {
|
||||
// Test a filenames that becomes extensionless.
|
||||
$this->assertEquals( 'no-extension', sanitize_file_name( '_.no-extension' ) );
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user