diff --git a/bea-sanitize-filename.php b/bea-sanitize-filename.php index 73bc1f9..0965085 100644 --- a/bea-sanitize-filename.php +++ b/bea-sanitize-filename.php @@ -33,21 +33,21 @@ */ /** - * Add some special chats to espace to WordPress basic list + * Add some special chars to escape to WordPress basic list * * @param array $special_chars * * @return array */ function bea_sanitize_file_name_chars( $special_chars = array() ) { - // Special caracters - $special_chars = array_merge( array( '’', '‘', '”', '“', '«', '»', '‹', '›', '—', '€', '©', '@' ), $special_chars ); + // Special characters + $special_chars = array_merge( array( ‘’’, ‘’’, ‘”’, ‘”’, ‘«’, ‘»’, ‘‹’, ‘›’, ‘—‘, ‘€’, ‘©’, ‘@’ ), $special_chars ); /** - * Accentued caracters + * Accented characters * @see https://github.com/BeAPI/bea-sanitize-filename/issues/8 * @since 2.0.6 */ - $special_chars = array_merge( array( 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'à', 'á', 'â', 'ã', 'ä', 'å', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ò', 'ó', 'ô', 'õ', 'ö', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ' ), $special_chars ); + $special_chars = array_merge( array( ‘À’, ‘Á’, ‘Â’, ‘Ã’, ‘Ä’, ‘Å’, ‘Ç’, ‘È’, ‘É’, ‘Ê’, ‘Ë’, ‘Ì’, ‘Í’, ‘Î’, ‘Ï’, ‘Ò’, ‘Ó’, ‘Ô’, ‘Õ’, ‘Ö’, ‘Ù’, ‘Ú’, ‘Û’, ‘Ü’, ‘Ý’, ‘à’, ‘á’, ‘â’, ‘ã’, ‘ä’, ‘å’, ‘ç’, ‘è’, ‘é’, ‘ê’, ‘ë’, ‘ì’, ‘í’, ‘î’, ‘ï’, ‘ð’, ‘ò’, ‘ó’, ‘ô’, ‘õ’, ‘ö’, ‘ù’, ‘ú’, ‘û’, ‘ü’, ‘ý’, ‘ÿ’ ), $special_chars ); return $special_chars; } @@ -71,19 +71,16 @@ function bea_sanitize_file_name( $file_name = '' ) { return $file_name; } - // get extension - preg_match( '/\.[^\.]+$/i', $file_name, $ext ); + // Separate filename and extension + $ext = pathinfo( $file_name, PATHINFO_EXTENSION ); // No extension, go out ? - if ( ! isset( $ext[0] ) ) { + if ( $ext === '' ) { return $file_name; } - // Get only first part - $ext = $ext[0]; - // work only on the filename without extension - $file_name = str_replace( $ext, '', $file_name ); + $file_name = substr( $file_name, 0, -( strlen( $ext ) + 1 ) ); // only lowercase $file_name = mb_strtolower( $file_name ); @@ -94,7 +91,7 @@ function bea_sanitize_file_name( $file_name = '' ) { $file_name = str_replace( '_', '-', $file_name ); // Return sanitized file name - return $file_name . $ext; + return $file_name . '.' . $ext; } add_filter( 'sanitize_file_name', 'bea_sanitize_file_name', 10, 1 ); diff --git a/phpcs.xml b/phpcs.xml index e929bab..040eb3b 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -44,7 +44,7 @@ - + diff --git a/phpunit.xml.dist b/phpunit.xml.dist index c6df57f..0cec1bd 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -2,9 +2,6 @@ bootstrap="tests/bootstrap.php" backupGlobals="false" colors="true" - convertErrorsToExceptions="true" - convertNoticesToExceptions="true" - convertWarningsToExceptions="true" > diff --git a/tests/wpunit/SanitizeFilenameTest.php b/tests/wpunit/SanitizeFilenameTest.php index db5b458..2d0d2ff 100644 --- a/tests/wpunit/SanitizeFilenameTest.php +++ b/tests/wpunit/SanitizeFilenameTest.php @@ -30,7 +30,7 @@ public function test_convert_underscore_to_dashes() { $this->assertSame( $out, sanitize_file_name( $in ) ); } - public function test_convert_at_sign_to_dashes() { + public function test_remove_at_sign() { $in = 'filename@with@arobase.jpg'; $out = 'filenamewitharobase.jpg'; $this->assertSame( $out, sanitize_file_name( $in ) );