Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions bea-sanitize-filename.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Comment thread
f2cmb marked this conversation as resolved.
/**
* 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 );
Comment thread
f2cmb marked this conversation as resolved.

return $special_chars;
}
Expand All @@ -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 );
Expand All @@ -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 );
2 changes: 1 addition & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<config name="minimum_supported_wp_version" value="4.4"/>

<rule ref="PHPCompatibility"/>
<config name="testVersion" value="5.6-"/>
<config name="testVersion" value="8.0-"/>

<rule ref="WordPress.WP.I18n">
<properties>
Expand Down
3 changes: 0 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite name="unit">
Expand Down
2 changes: 1 addition & 1 deletion tests/wpunit/SanitizeFilenameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
Expand Down