diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 9dba689b32..28b65135a0 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -1984,7 +1984,9 @@ function remove_accents( $string ) { * @return string The sanitized filename. */ function sanitize_file_name( $filename ) { - $filename_raw = $filename; + $filename_raw = $filename; + $filename = remove_accents( $filename ); + $special_chars = array( '?', '[', ']', '/', '\\', '=', '<', '>', ':', ';', ',', "'", '"', '&', '$', '#', '*', '(', ')', '|', '~', '`', '!', '{', '}', '%', '+', '’', '«', '»', '”', '“', chr( 0 ) ); // Check for support for utf8 in the installed PCRE library once and store the result in a static. @@ -2013,10 +2015,11 @@ function sanitize_file_name( $filename ) { * @param string $filename_raw The original filename to be sanitized. */ $special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw ); - $filename = str_replace( $special_chars, '', $filename ); - $filename = str_replace( array( '%20', '+' ), '-', $filename ); - $filename = preg_replace( '/[\r\n\t -]+/', '-', $filename ); - $filename = trim( $filename, '.-_' ); + + $filename = str_replace( $special_chars, '', $filename ); + $filename = str_replace( array( '%20', '+' ), '-', $filename ); + $filename = preg_replace( '/[\r\n\t -]+/', '-', $filename ); + $filename = trim( $filename, '.-_' ); if ( false === strpos( $filename, '.' ) ) { $mime_types = wp_get_mime_types(); @@ -2068,7 +2071,9 @@ function sanitize_file_name( $filename ) { } } } + $filename .= '.' . $extension; + /** This filter is documented in wp-includes/formatting.php */ return apply_filters( 'sanitize_file_name', $filename, $filename_raw ); } diff --git a/tests/phpunit/tests/formatting/SanitizeFileName.php b/tests/phpunit/tests/formatting/SanitizeFileName.php index 76c0d9705c..3e01469329 100644 --- a/tests/phpunit/tests/formatting/SanitizeFileName.php +++ b/tests/phpunit/tests/formatting/SanitizeFileName.php @@ -20,12 +20,21 @@ class Tests_Formatting_SanitizeFileName extends WP_UnitTestCase { $this->assertEquals( 'testtest', sanitize_file_name( $string ) ); } + /** + * @ticket 22363 + */ + function test_removes_accents() { + $in = 'àáâãäåæçèéêëìíîïñòóôõöøùúûüýÿ'; + $out = 'aaaaaaaeceeeeiiiinoooooouuuuyy'; + $this->assertEquals( $out, sanitize_file_name( $in ) ); + } + /** * Test that spaces are correctly replaced with dashes. * * @ticket 16330 */ - function test_replace_spaces() { + function test_replaces_spaces() { $urls = array( 'unencoded space.png' => 'unencoded-space.png', 'encoded-space.jpg' => 'encoded-space.jpg', @@ -58,13 +67,13 @@ class Tests_Formatting_SanitizeFileName extends WP_UnitTestCase { $this->assertEquals( 'a22b.jpg', sanitize_file_name( 'a%22b.jpg' ) ); } - function test_replaces_unnammed_file_extensions() { + function test_replaces_unnamed_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() { + function test_replaces_unnamed_file_extensionless() { // Test a filenames that becomes extensionless. $this->assertEquals( 'no-extension', sanitize_file_name( '_.no-extension' ) ); }