-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbea-sanitize-filename.php
97 lines (80 loc) · 2.88 KB
/
bea-sanitize-filename.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/*
Plugin Name: BEA - Sanitize Filename
Version: 2.0.7
Plugin URI: https://github.com/BeAPI/bea-sanitize-filename
Description: Remove all punctuation and accents from the filename of uploaded files.
Author: Be API
Author URI: https://beapi.fr
Domain Path: languages
Text Domain: bea-sanitize-filename
Contributors: Amaury Balmer, Maxime Culea
--------
Copyright 2018-2019 Be API Technical team ([email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* Add some special chats to espace 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 );
/**
* Accentued caracters
* @see https://github.com/BeAPI/bea-sanitize-filename/issues/8
* @since 2.0.6
*/
$special_chars = array_merge( array( 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'à', 'á', 'â', 'ã', 'ä', 'å', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ò', 'ó', 'ô', 'õ', 'ö', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ' ), $special_chars );
return $special_chars;
}
add_filter( 'sanitize_file_name_chars', 'bea_sanitize_file_name_chars', 10, 1 );
/**
* Filters the filename by adding more rules :
* - only lowercase
* - replace _ by -
*
* @since 1.0.1
*
* @param string $file_name
*
* @return string
*/
function bea_sanitize_file_name( $file_name = '' ) {
// Empty filename
if ( empty( $file_name ) ) {
return $file_name;
}
// get extension
preg_match( '/\.[^\.]+$/i', $file_name, $ext );
// No extension, go out ?
if ( ! isset( $ext[0] ) ) {
return $file_name;
}
// Get only first part
$ext = $ext[0];
// work only on the filename without extension
$file_name = str_replace( $ext, '', $file_name );
// only lowercase
$file_name = mb_strtolower( $file_name );
// remove accents
$file_name = remove_accents( $file_name );
// replace _ by -
$file_name = str_replace( '_', '-', $file_name );
// Return sanitized file name
return $file_name . $ext;
}
add_filter( 'sanitize_file_name', 'bea_sanitize_file_name', 10, 1 );