Media: Remove accents in sanitize_file_name().

This brings some consistency with `sanitize_title()` and `sanitize_user()`.

Props tar.gz, NumidWasNotAvailable, juliobox, p_enrique, cristovaov, zodiac1978, mikeschroder, markoheijnen, chriscct7, swissspidy, DrProtocols, pento, gitlost, joemcgill, dustinbolton, programmin, Vayu, MaximeCulea, lucasbustamante, nilovelez, RavanH, audrasjb, SergeyBiryukov.
See #22363.

git-svn-id: https://develop.svn.wordpress.org/trunk@48603 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2020-07-24 18:01:48 +00:00
parent 2f98829e41
commit c7783dc49a
2 changed files with 22 additions and 8 deletions

View File

@ -1985,6 +1985,8 @@ function remove_accents( $string ) {
*/ */
function sanitize_file_name( $filename ) { function sanitize_file_name( $filename ) {
$filename_raw = $filename; $filename_raw = $filename;
$filename = remove_accents( $filename );
$special_chars = array( '?', '[', ']', '/', '\\', '=', '<', '>', ':', ';', ',', "'", '"', '&', '$', '#', '*', '(', ')', '|', '~', '`', '!', '{', '}', '%', '+', '', '«', '»', '”', '“', chr( 0 ) ); $special_chars = array( '?', '[', ']', '/', '\\', '=', '<', '>', ':', ';', ',', "'", '"', '&', '$', '#', '*', '(', ')', '|', '~', '`', '!', '{', '}', '%', '+', '', '«', '»', '”', '“', chr( 0 ) );
// Check for support for utf8 in the installed PCRE library once and store the result in a static. // Check for support for utf8 in the installed PCRE library once and store the result in a static.
@ -2013,6 +2015,7 @@ function sanitize_file_name( $filename ) {
* @param string $filename_raw The original filename to be sanitized. * @param string $filename_raw The original filename to be sanitized.
*/ */
$special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw ); $special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw );
$filename = str_replace( $special_chars, '', $filename ); $filename = str_replace( $special_chars, '', $filename );
$filename = str_replace( array( '%20', '+' ), '-', $filename ); $filename = str_replace( array( '%20', '+' ), '-', $filename );
$filename = preg_replace( '/[\r\n\t -]+/', '-', $filename ); $filename = preg_replace( '/[\r\n\t -]+/', '-', $filename );
@ -2068,7 +2071,9 @@ function sanitize_file_name( $filename ) {
} }
} }
} }
$filename .= '.' . $extension; $filename .= '.' . $extension;
/** This filter is documented in wp-includes/formatting.php */ /** This filter is documented in wp-includes/formatting.php */
return apply_filters( 'sanitize_file_name', $filename, $filename_raw ); return apply_filters( 'sanitize_file_name', $filename, $filename_raw );
} }

View File

@ -20,12 +20,21 @@ class Tests_Formatting_SanitizeFileName extends WP_UnitTestCase {
$this->assertEquals( 'testtest', sanitize_file_name( $string ) ); $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. * Test that spaces are correctly replaced with dashes.
* *
* @ticket 16330 * @ticket 16330
*/ */
function test_replace_spaces() { function test_replaces_spaces() {
$urls = array( $urls = array(
'unencoded space.png' => 'unencoded-space.png', 'unencoded space.png' => 'unencoded-space.png',
'encoded-space.jpg' => 'encoded-space.jpg', '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' ) ); $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. // Test filenames with both supported and unsupported extensions.
$this->assertEquals( 'unnamed-file.exe', sanitize_file_name( '_.exe' ) ); $this->assertEquals( 'unnamed-file.exe', sanitize_file_name( '_.exe' ) );
$this->assertEquals( 'unnamed-file.jpg', sanitize_file_name( '_.jpg' ) ); $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. // Test a filenames that becomes extensionless.
$this->assertEquals( 'no-extension', sanitize_file_name( '_.no-extension' ) ); $this->assertEquals( 'no-extension', sanitize_file_name( '_.no-extension' ) );
} }