Skip to content

Instantly share code, notes, and snippets.

@windyjonas
Created March 28, 2017 13:35
Show Gist options
  • Save windyjonas/af3bbc80c4ef8f857db0ad3478010301 to your computer and use it in GitHub Desktop.
Save windyjonas/af3bbc80c4ef8f857db0ad3478010301 to your computer and use it in GitHub Desktop.
Sanitize decomposed filenames in WordPress
class Sanitize_Filename {
public function __construct() {
add_filter( 'wp_handle_upload_prefilter', [ $this, 'sanitize_decomposed_utf8_file' ] );
}
/**
* Sanitize the filename in a file object
*
* @param array $file In file
* @return array Out file
*/
public function sanitize_decomposed_utf8_file( $file ) {
$file['name'] = $this->sanitize_decomposed_utf8_string( $file['name'] );
return $file;
}
/**
* Sanitize an decomposed UTF8 string (NFD)
*
* @param string $str Input string
* @return string Converted string
*/
public function sanitize_decomposed_utf8_string( $str ) {
$chars = [
chr( 0xCC ) . chr( 0x88 ) => '', // ¨
chr( 0xCC ) . chr( 0x8a ) => '', // °
chr( 0xCC ) . chr( 0x81 ) => '', // ´
];
$new_str = strtr( $str, $chars );
return $new_str;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment