|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Custom functions that handle unicode/non-ASCII characters in filenames |
|
4
|
|
|
* to avoid occasional server-side issues |
|
5
|
|
|
* |
|
6
|
|
|
* @package podium |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
class PodiumCleanImageFilenames |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Plugin settings. |
|
13
|
|
|
* |
|
14
|
|
|
* @var array Plugin settings for version, default mime types. |
|
15
|
|
|
* @since 1.1 |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
public $plugin_settings = [ |
|
19
|
|
|
'version' => '1.1', |
|
20
|
|
|
'default_mime_types' => [ |
|
21
|
|
|
'image/gif', |
|
22
|
|
|
'image/jpeg', |
|
23
|
|
|
'image/pjpeg', |
|
24
|
|
|
'image/png', |
|
25
|
|
|
'image/tiff' |
|
26
|
|
|
] |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Sets up hooks, actions and filters that the plugin responds to. |
|
31
|
|
|
* |
|
32
|
|
|
* @since 1.0 |
|
33
|
|
|
*/ |
|
34
|
|
|
|
|
35
|
|
|
public function __construct() |
|
36
|
|
|
{ |
|
37
|
|
|
add_action('wp_handle_upload_prefilter', [$this, 'upload_filter']); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Performs the filename cleaning. |
|
42
|
|
|
* |
|
43
|
|
|
* This function performs the actual cleaning of the filename. It takes an |
|
44
|
|
|
* array with the file information, cleans the filename and sends the file |
|
45
|
|
|
* information back to where the function was called from. |
|
46
|
|
|
* |
|
47
|
|
|
* @since 1.1 |
|
48
|
|
|
* |
|
49
|
|
|
* @param array File details including the filename in $file['name']. |
|
50
|
|
|
* @return array The $file array with cleaned filename. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function clean_filename($file) |
|
53
|
|
|
{ |
|
54
|
|
|
|
|
55
|
|
|
$path = pathinfo($file['name']); |
|
56
|
|
|
$new_filename = preg_replace('/.' . $path['extension'] . '$/', '', $file['name']); |
|
57
|
|
|
$file['name'] = sanitize_title($new_filename) . '.' . $path['extension']; |
|
58
|
|
|
|
|
59
|
|
|
return $file; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Checks whether or not the current file should be cleaned. |
|
64
|
|
|
* |
|
65
|
|
|
* This function runs when files are being uploaded to the WordPress media |
|
66
|
|
|
* library. The function checks if the clean_image_filenames_mime_types filter |
|
67
|
|
|
* has been used and overrides other settings if it has. Otherwise, the plugin |
|
68
|
|
|
* settings are used. |
|
69
|
|
|
* |
|
70
|
|
|
* If a file shall be cleaned or not is checked by comparing the current file's |
|
71
|
|
|
* mime type to the list of mime types to be cleaned. |
|
72
|
|
|
* |
|
73
|
|
|
* @since 1.1 Added more complex checks and moved the actual cleaning to clean_filename(). |
|
74
|
|
|
* @since 1.0 |
|
75
|
|
|
* |
|
76
|
|
|
* @param array The file information including the filename in $file['name']. |
|
77
|
|
|
* @return array The file information with the cleaned or original filename. |
|
78
|
|
|
*/ |
|
79
|
|
|
public function upload_filter($file) |
|
80
|
|
|
{ |
|
81
|
|
|
|
|
82
|
|
|
$mime_types_setting = 'all'; |
|
83
|
|
|
$default_mime_types = $this->plugin_settings['default_mime_types']; |
|
84
|
|
|
$valid_mime_types = apply_filters('clean_image_filenames_mime_types', $default_mime_types); |
|
85
|
|
|
|
|
86
|
|
|
if ($valid_mime_types !== $default_mime_types) { |
|
87
|
|
|
|
|
88
|
|
|
if (in_array($file['type'], $valid_mime_types)) { |
|
89
|
|
|
$file = $this->clean_filename($file); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
} else { |
|
93
|
|
|
|
|
94
|
|
|
if ('all' == $mime_types_setting) { |
|
95
|
|
|
$file = $this->clean_filename($file); |
|
96
|
|
|
} elseif ('images' == $mime_types_setting && in_array($file['type'], $default_mime_types)) { |
|
97
|
|
|
$file = $this->clean_filename($file); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// Return cleaned file or input file if it didn't match |
|
103
|
|
|
return $file; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// inititnowz! |
|
109
|
|
|
new PodiumCleanImageFilenames; |
|
110
|
|
|
|