-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbea-sanitize-filename.php
More file actions
154 lines (128 loc) · 7.95 KB
/
bea-sanitize-filename.php
File metadata and controls
154 lines (128 loc) · 7.95 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/*
Plugin Name: BEA - Sanitize Filename
Version: 2.0.6
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 (human@beapi.fr)
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 more special chars.
*
* @param array $special_chars
*
* @return array
*/
function bea_sanitize_file_name_chars( $special_chars = array() ) {
// Special caracters
$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 -
*
* @param string $file_name
*
* @return string
* @since 1.0.1
*/
function bea_sanitize_file_name( $file_name = '' ) {
// Empty filename
if ( empty( $file_name ) ) {
return $file_name;
}
$ext = pathinfo( $file_name, PATHINFO_EXTENSION );
if ( empty( $ext ) ) {
return $file_name;
}
// work only on the filename without extension
$file_name = str_replace( '.' . $ext, '', $file_name );
// let WordPress try to remove as much accents as possible
$file_name = remove_accents( $file_name );
// fix remaining combined accents
// see: https://core.trac.wordpress.org/ticket/24661
$file_name = _ticket_24661_patch( $file_name );
// convert remaining special chars with dashes
$file_name = sanitize_title_with_dashes( $file_name );
// replace undescore(_) with dash(-)
$file_name = str_replace( '_', '-', $file_name );
// remove contiguous dashes
$file_name = preg_replace(
'@-(?=-)@', //select all dashes follow by another dash
'',
$file_name
);
// Return sanitized file name
return $file_name . '.' . $ext;
}
add_filter( 'sanitize_file_name', 'bea_sanitize_file_name', 10, 1 );
if ( ! function_exists( '_ticket_24661_patch' ) ) :
// From: https://core.trac.wordpress.org/attachment/ticket/24661/24661.6.patch
/**
* Removes "Mn" nonspacing combining marks that follow Latin characters.
*
* @param string $string
*
* @return string
*/
function _ticket_24661_patch( $string ) {
if ( _wp_can_use_pcre_ucp() ) { // If UCP available.
$string = preg_replace( '/(?<=\p{Latin})\p{Mn}+/u', '', $string );
} else {
// Generated by "gen_cat_regex_alts.php" from "http://www.unicode.org/Public/9.0.0/ucd/UnicodeData.txt".
static $mn_regex_alts = '\xcc[\x80-\xbf]|\xcd[\x80-\xaf]|\xd2[\x83-\x87]|\xd6[\x91-\xbd\xbf]|\xd7[\x81\x82\x84\x85\x87]|\xd8[\x90-\x9a]|\xd9[\x8b-\x9f\xb0]|\xdb[\x96-\x9c\x9f-\xa4\xa7\xa8\xaa-\xad]|\xdc[\x91\xb0-\xbf]|\xdd[\x80-\x8a]|\xde[\xa6-\xb0]|\xdf[\xab-\xb3]|\xe0(?:\xa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bf]|\x93[\x80\x82\x83]|\x96[\xb2-\xb5\xbc\xbd\xbf]|\x97[\x80\x9c\x9d]|\x98[\xb3-\xba\xbd\xbf]|\x99\x80|\x9a[\xab\xad\xb0-\xb5\xb7]|\x9c[\x9d-\x9f\xa2-\xa5\xa7-\xab]|\xb0[\xb0-\xb6\xb8-\xbd\xbf]|\xb2[\x92-\xa7\xaa-\xb0\xb2\xb3\xb5\xb6])|\x96(?:\xab[\xb0-\xb4]|\xac[\xb0-\xb6]|\xbe[\x8f-\x92])|\x9b\xb2[\x9d\x9e]|\x9d(?:\x85[\xa7-\xa9\xbb-\xbf]|\x86[\x80-\x82\x85-\x8b\xaa-\xad]|\x89[\x82-\x84]|\xa8[\x80-\xb6\xbb-\xbf]|\xa9[\x80-\xac\xb5]|\xaa[\x84\x9b-\x9f\xa1-\xaf])|\x9e(?:\x80[\x80-\x86\x88-\x98\x9b-\xa1\xa3\xa4\xa6-\xaa]|\xa3[\x90-\x96]|\xa5[\x84-\x8a]))|\xf3\xa0(?:[\x84-\x86][\x80-\xbf]|\x87[\x80-\xaf])'; // 1690 code points.
// Generated by "gen_script_regex_alts.php" from "http://www.unicode.org/Public/9.0.0/ucd/Scripts.txt".
static $latin_regex_alts = '[\x41-\x5a\x61-\x7a]|\xc2[\xaa\xba]|\xc3[\x80-\x96\x98-\xb6\xb8-\xbf]|[\xc4-\xc9][\x80-\xbf]|\xca[\x80-\xb8]|\xcb[\xa0-\xa4]|\xe1(?:\xb4[\x80-\xa5\xac-\xbf]|\xb5[\x80-\x9c\xa2-\xa5\xab-\xb7\xb9-\xbf]|\xb6[\x80-\xbe]|[\xb8-\xbb][\x80-\xbf])|\xe2(?:\x81[\xb1\xbf]|\x82[\x90-\x9c]|\x84[\xaa\xab\xb2]|\x85[\x8e\xa0-\xbf]|\x86[\x80-\x88]|\xb1[\xa0-\xbf])|\xea(?:\x9c[\xa2-\xbf]|\x9d[\x80-\xbf]|\x9e[\x80-\x87\x8b-\xae\xb0-\xb7]|\x9f[\xb7-\xbf]|\xac[\xb0-\xbf]|\xad[\x80-\x9a\x9c-\xa4])|\xef(?:\xac[\x80-\x86]|\xbc[\xa1-\xba]|\xbd[\x81-\x9a])'; // 1350 code points.
$string = preg_replace( '/(?<=' . $latin_regex_alts . ')(?:' . $mn_regex_alts . ')+/', '', $string );
}
return $string;
}
endif;
if ( ! function_exists( '_wp_can_use_pcre_ucp' ) ) :
// From: https://core.trac.wordpress.org/attachment/ticket/24661/24661.6.patch
/**
* Returns whether PCRE Unicode character properties (UCP, eg \p{L}) are available for use.
*
* @param bool $set - Used for testing only
* null : default - get PCRE UCP capability
* false : Used for testing - return false for future calls to this function
* 'reset': Used for testing - restore default behavior of this function
*
* @since 4.7.0
* @access private
*
* @staticvar string $ucp_pcre
*
* @ignore
*/
function _wp_can_use_pcre_ucp( $set = null ) {
static $ucp_pcre = 'reset';
if ( null !== $set ) {
$ucp_pcre = $set;
}
if ( 'reset' === $ucp_pcre ) {
$ucp_pcre = false !== @preg_match( '/\p{L}/u', '' );
}
return $ucp_pcre;
}
endif;