Changeset 47638
- Timestamp:
- 04/29/2020 03:38:43 PM (4 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/formatting.php
r47550 r47638 2006 2006 $filename_raw = $filename; 2007 2007 $special_chars = array( '?', '[', ']', '/', '\\', '=', '<', '>', ':', ';', ',', "'", '"', '&', '$', '#', '*', '(', ')', '|', '~', '`', '!', '{', '}', '%', '+', chr( 0 ) ); 2008 2009 // Check for support for utf8 in the installed PCRE library once and store the result in a static. 2010 static $utf8_pcre = null; 2011 if ( ! isset( $utf8_pcre ) ) { 2012 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged 2013 $utf8_pcre = @preg_match( '/^./u', 'a' ); 2014 } 2015 2016 if ( ! seems_utf8( $filename ) ) { 2017 $_ext = pathinfo( $filename, PATHINFO_EXTENSION ); 2018 $_name = pathinfo( $filename, PATHINFO_FILENAME ); 2019 $filename = sanitize_title_with_dashes( $_name ) . '.' . $_ext; 2020 } 2021 2022 if ( $utf8_pcre ) { 2023 $filename = preg_replace( "#\x{00a0}#siu", ' ', $filename ); 2024 } 2025 2008 2026 /** 2009 2027 * Filters the list of characters to remove from a filename. … … 2015 2033 */ 2016 2034 $special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw ); 2017 $filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );2018 2035 $filename = str_replace( $special_chars, '', $filename ); 2019 2036 $filename = str_replace( array( '%20', '+' ), '-', $filename ); -
trunk/tests/phpunit/tests/formatting/SanitizeFileName.php
r46586 r47638 69 69 $this->assertEquals( 'no-extension', sanitize_file_name( '_.no-extension' ) ); 70 70 } 71 72 /** 73 * @dataProvider data_wp_filenames 74 */ 75 function test_replaces_invalid_utf8_characters( $input, $expected ) { 76 $this->assertEquals( $expected, sanitize_file_name( $input ) ); 77 } 78 79 function data_wp_filenames() { 80 return array( 81 [ urldecode( '%B1myfile.png' ), 'myfile.png' ], 82 [ urldecode( '%B1myfile' ), 'myfile' ], 83 [ 'demo bar.png', 'demo-bar.png' ], 84 [ 'demo' . json_decode( '"\u00a0"' ) . 'bar.png', 'demo-bar.png' ], 85 ); 86 } 71 87 }
Note: See TracChangeset
for help on using the changeset viewer.