Clean Image Filenames - Version 1.1

Version Description

  • Added plugin settings to media settings page with option to convert all file types or just image file types.
  • Added shortcut to plugin settings from the plugins list.
Download this release

Release Info

Developer Upperdog
Plugin Icon wp plugin Clean Image Filenames
Version 1.1
Comparing to
See all releases

Code changes from version 1.0 to 1.1

clean-image-filenames.php CHANGED
@@ -1,11 +1,11 @@
1
  <?php
2
  /**
3
  * Plugin Name: Clean Image Filenames
4
- * Description: Say goodbye to bad filenames like Château de Ferrières.jpg and say hello to nice and clean filenames like chateau-de-ferrieres.jpg.
5
- * Version: 1.0
6
- * Author: UPPERDOG
7
  * Author URI: http://upperdog.com
8
- * Author Email: hello@upperdog.se
9
  * License: GPL2
10
  */
11
 
@@ -31,43 +31,252 @@ if(!defined('ABSPATH')) {
31
 
32
  class CleanImageFilenames {
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  function __construct() {
 
 
 
 
 
35
  add_action('wp_handle_upload_prefilter', array($this, 'upload_filter'));
36
  }
37
 
38
- function upload_filter($file) {
39
 
40
- /*
41
- * Only sanitize the filename for files that has one of these mime types.
42
- * The list of mime types can be customized using the clean_image_filenames_mime_types filter.
43
- * For a complete list of mime types, see http://en.wikipedia.org/wiki/Internet_media_type
44
- */
45
- $valid_mime_types = array(
46
- 'image/bmp',
47
- 'image/gif',
48
- 'image/jpeg',
49
- 'image/pjpeg',
50
- 'image/png',
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  );
52
 
 
 
 
 
53
 
54
- /*
55
- * If the current file's mime type is in the array of valid mime types, the filename
56
- * will be sanitized and saved to the original $file array.
57
- */
58
- if (in_array($file['type'], apply_filters('clean_image_filenames_mime_types', $valid_mime_types))) {
59
- $path = pathinfo($file['name']);
60
- $new_filename = preg_replace('/.' . $path['extension'] . '$/', '', $file['name']);
61
- $file['name'] = sanitize_title($new_filename) . '.' . $path['extension'];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  }
 
 
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
- /*
66
- * Return the $file array. If the filename wasn't changed, the input array
67
- * is returned without any modifications.
68
- */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  return $file;
70
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  }
72
 
73
  $clean_image_filenames = new CleanImageFilenames();
1
  <?php
2
  /**
3
  * Plugin Name: Clean Image Filenames
4
+ * Description: Filenames with special characters or language accent characters can sometimes be a problem. This plugin takes care of that by cleaning the filenames.
5
+ * Version: 1.1
6
+ * Author: Upperdog
7
  * Author URI: http://upperdog.com
8
+ * Author Email: hello@upperdog.com
9
  * License: GPL2
10
  */
11
 
31
 
32
  class CleanImageFilenames {
33
 
34
+ /**
35
+ * Plugin settings.
36
+ *
37
+ * @var array Plugin settings for version, default mime types.
38
+ * @since 1.1
39
+ */
40
+
41
+ public $plugin_settings = array(
42
+ 'version' => '1.1',
43
+ 'default_mime_types' => array(
44
+ 'image/gif',
45
+ 'image/jpeg',
46
+ 'image/pjpeg',
47
+ 'image/png',
48
+ 'image/tiff'
49
+ )
50
+ );
51
+
52
+
53
+ /**
54
+ * Sets up hooks, actions and filters that the plugin responds to.
55
+ *
56
+ * @since 1.0
57
+ */
58
+
59
  function __construct() {
60
+
61
+ register_activation_hook(__FILE__, array($this, 'plugin_activation'));
62
+ add_action('plugins_loaded', array($this, 'plugins_loaded'));
63
+ add_action('admin_init', array($this, 'admin_init'));
64
+ add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'add_action_links'));
65
  add_action('wp_handle_upload_prefilter', array($this, 'upload_filter'));
66
  }
67
 
 
68
 
69
+ /**
70
+ * Adds default plugin settings on plugin activation.
71
+ *
72
+ * @since 1.1
73
+ */
74
+
75
+ function plugin_activation() {
76
+ $this->add_default_plugin_settings();
77
+ }
78
+
79
+
80
+ /**
81
+ * Updates plugin version database setting and calls default settings function.
82
+ *
83
+ * Checks current plugin version. If the plugin has been updated, the saved
84
+ * plugin version in the database is updated.
85
+ *
86
+ * Adds default plugin settings if they don't already exist. Default plugin
87
+ * settings didn't exist prior to version 1.1.
88
+ *
89
+ * @since 1.1
90
+ */
91
+
92
+ function plugins_loaded() {
93
+
94
+ if ($this->plugin_settings['version'] !== get_option('clean_image_filenames_plugin_version')) {
95
+ update_option('clean_image_filenames_plugin_version', $this->plugin_settings['version']);
96
+ }
97
+
98
+ $this->add_default_plugin_settings();
99
+ }
100
+
101
+
102
+ /**
103
+ * Adds default plugin settings in the database.
104
+ *
105
+ * This function runs when the plugin is activated and when plugins are loaded
106
+ * using the plugins_loaded hook. The function updates default plugin settings
107
+ * in the database options table.
108
+ *
109
+ * Adds default value for mime types field if it doesn't already exist
110
+ *
111
+ * @since 1.1
112
+ */
113
+
114
+ function add_default_plugin_settings() {
115
+
116
+ if (FALSE === get_option('clean_image_filenames_mime_types')) {
117
+ add_option('clean_image_filenames_mime_types', 'images');
118
+ }
119
+ }
120
+
121
+
122
+ /**
123
+ * Sets up plugin translations and plugin settings fields.
124
+ *
125
+ * @since 1.1
126
+ */
127
+
128
+ function admin_init() {
129
+
130
+ // Load plugin translations
131
+ load_plugin_textdomain('clean_image_filenames', false, dirname(plugin_basename(__FILE__)) . '/languages/');
132
+
133
+ // Add settings section
134
+ add_settings_section('clean_image_filenames_settings_section', 'Clean Image Filenames', array($this, 'clean_image_filenames_settings_section_callback'), 'media');
135
+
136
+ // Add settings field
137
+ add_settings_field(
138
+ 'clean_image_filenames_mime_types',
139
+ __('File types', 'clean_image_filenames'),
140
+ array($this, 'clean_image_filenames_mime_types_callback'),
141
+ 'media',
142
+ 'clean_image_filenames_settings_section',
143
+ array(
144
+ 'alternatives' => array(
145
+ array(
146
+ 'value' => 'all',
147
+ 'label' => __('All file types', 'clean_image_filenames')
148
+ ),
149
+ array(
150
+ 'value' => 'images',
151
+ 'label' => __('Images only', 'clean_image_filenames')
152
+ )
153
+ )
154
+ )
155
  );
156
 
157
+ // Register settings
158
+ register_setting('media', 'clean_image_filenames_mime_types');
159
+ }
160
+
161
 
162
+ /**
163
+ * Add custom action links to the plugin's row in the plugins list.
164
+ *
165
+ * @since 1.1
166
+ * @param array Original action links.
167
+ * @return array Action links with new addition.
168
+ */
169
+
170
+ function add_action_links($links) {
171
+ $plugin_action_links = array('<a href="' . admin_url('options-media.php') . '">' . __('Settings') . '</a>');
172
+ return array_merge($links, $plugin_action_links);
173
+ }
174
+
175
+
176
+ /**
177
+ * Outputs content before the settings fields.
178
+ *
179
+ * @since 1.1
180
+ */
181
+
182
+ function clean_image_filenames_settings_section_callback() {
183
+
184
+ echo '<p>' . __('Choose which file types that Clean Image Filenames shall improve the filenames for when files are uploaded.', 'clean_image_filenames') . '</p>';
185
+ }
186
+
187
+
188
+ /**
189
+ * Outputs the settings fields.
190
+ *
191
+ * If the plugin filter has been used in a plugin or theme, the filter
192
+ * overrides the settings are the settings are therefore disabled.
193
+ *
194
+ * If the plugin filter has not been used, the user selected setting of what
195
+ * file types to clean is used.
196
+ *
197
+ * @since 1.1
198
+ * @param array Field defails.
199
+ */
200
+
201
+ function clean_image_filenames_mime_types_callback($args) {
202
+
203
+ if (apply_filters('clean_image_filenames_mime_types', $this->plugin_settings['default_mime_types']) !== $this->plugin_settings['default_mime_types']) {
204
+
205
+ echo '<input name="clean_image_filenames_mime_types" id="clean_image_filenames_mime_types" type="hidden" value="' . get_option('clean_image_filenames_mime_types') . '">';
206
+ echo '<i>' . __('The setting for what file types should be cleaned is disabled since a plugin or theme has already defined what file types should be cleaned.', 'clean_image_filenames') . '</i>';
207
+
208
+ } else {
209
+
210
+ foreach ($args['alternatives'] as $alternative) {
211
+ echo '<label><input name="clean_image_filenames_mime_types" id="clean_image_filenames_mime_types" type="radio" value="' . $alternative['value'] . '" ' . checked($alternative['value'], get_option('clean_image_filenames_mime_types'), false) . '>' . $alternative['label'] . '</label><br>';
212
+ }
213
  }
214
+ }
215
+
216
 
217
+ /**
218
+ * Checks whether or not the current file should be cleaned.
219
+ *
220
+ * This function runs when files are being uploaded to the WordPress media
221
+ * library. The function checks if the clean_image_filenames_mime_types filter
222
+ * has been used and overrides other settings if it has. Otherwise, the plugin
223
+ * settings are used.
224
+ *
225
+ * If a file shall be cleaned or not is checked by comparing the current file's
226
+ * mime type to the list of mime types to be cleaned.
227
+ *
228
+ * @since 1.1 Added more complex checks and moved the actual cleaning to clean_filename().
229
+ * @since 1.0
230
+ * @param array The file information including the filename in $file['name'].
231
+ * @return array The file information with the cleaned or original filename.
232
+ */
233
 
234
+ function upload_filter($file) {
235
+
236
+ $mime_types_setting = get_option('clean_image_filenames_mime_types');
237
+ $default_mime_types = $this->plugin_settings['default_mime_types'];
238
+ $valid_mime_types = apply_filters('clean_image_filenames_mime_types', $default_mime_types);
239
+
240
+ if ($valid_mime_types !== $default_mime_types) {
241
+
242
+ if (in_array($file['type'], $valid_mime_types)) {
243
+ $file = $this->clean_filename($file);
244
+ }
245
+
246
+ } else {
247
+
248
+ if ('all' == $mime_types_setting) {
249
+ $file = $this->clean_filename($file);
250
+ } elseif ('images' == $mime_types_setting && in_array($file['type'], $default_mime_types)) {
251
+ $file = $this->clean_filename($file);
252
+ }
253
+ }
254
+
255
+ // Return cleaned file or input file if it didn't match
256
  return $file;
257
  }
258
+
259
+
260
+ /**
261
+ * Performs the filename cleaning.
262
+ *
263
+ * This function performs the actual cleaning of the filename. It takes an
264
+ * array with the file information, cleans the filename and sends the file
265
+ * information back to where the function was called from.
266
+ *
267
+ * @since 1.1
268
+ * @param array File details including the filename in $file['name'].
269
+ * @return array The $file array with cleaned filename.
270
+ */
271
+
272
+ function clean_filename($file) {
273
+
274
+ $path = pathinfo($file['name']);
275
+ $new_filename = preg_replace('/.' . $path['extension'] . '$/', '', $file['name']);
276
+ $file['name'] = sanitize_title($new_filename) . '.' . $path['extension'];
277
+
278
+ return $file;
279
+ }
280
  }
281
 
282
  $clean_image_filenames = new CleanImageFilenames();
languages/clean_image_filenames-sv_SE.mo ADDED
Binary file
languages/clean_image_filenames-sv_SE.po ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ msgid ""
2
+ msgstr ""
3
+ "Project-Id-Version: Clean Image Filenames\n"
4
+ "POT-Creation-Date: 2014-12-07 19:37+0100\n"
5
+ "PO-Revision-Date: 2014-12-07 19:37+0100\n"
6
+ "Last-Translator: \n"
7
+ "Language-Team: \n"
8
+ "Language: sv_SE\n"
9
+ "MIME-Version: 1.0\n"
10
+ "Content-Type: text/plain; charset=UTF-8\n"
11
+ "Content-Transfer-Encoding: 8bit\n"
12
+ "X-Generator: Poedit 1.6.3\n"
13
+ "X-Poedit-Basepath: ..\n"
14
+ "X-Poedit-SourceCharset: UTF-8\n"
15
+ "X-Poedit-KeywordsList: __;_e;_n:1,2;_x:1,2c;_ex:1,2c;_nx:4c,1,2;esc_attr__;"
16
+ "esc_attr_e;esc_attr_x:1,2c;esc_html__;esc_html_e;esc_html_x:1,2c;_n_noop:1,2;"
17
+ "_nx_noop:3c,1,2;__ngettext_noop:1,2\n"
18
+ "Plural-Forms: nplurals=2; plural=(n != 1);\n"
19
+ "X-Poedit-SearchPath-0: .\n"
20
+
21
+ #: clean-image-filenames.php:139
22
+ msgid "File types"
23
+ msgstr "Filtyper"
24
+
25
+ #: clean-image-filenames.php:147
26
+ msgid "All file types"
27
+ msgstr "Alla filtyper"
28
+
29
+ #: clean-image-filenames.php:151
30
+ msgid "Images only"
31
+ msgstr "Endast bilder"
32
+
33
+ #: clean-image-filenames.php:171
34
+ msgid "Settings"
35
+ msgstr "Inställningar"
36
+
37
+ #: clean-image-filenames.php:184
38
+ msgid ""
39
+ "Choose which file types that Clean Image Filenames shall improve the "
40
+ "filenames for when files are uploaded."
41
+ msgstr ""
42
+ "Välj vilka filtyper som Clean Image Filenames ska förbättra filnamnen för "
43
+ "när filer laddas upp."
44
+
45
+ #: clean-image-filenames.php:206
46
+ msgid ""
47
+ "The setting for what file types should be cleaned is disabled since a plugin "
48
+ "or theme has already defined what file types should be cleaned."
49
+ msgstr ""
50
+ "Inställningen för vilka filtyper som ska fixas är inaktiverad eftersom ett "
51
+ "plugin eller tema redan har definierat vilka filtyper som ska fixas."
readme.txt CHANGED
@@ -2,29 +2,34 @@
2
  Contributors: Upperdog, Gesen
3
  Tags: upload, images, files, media,
4
  Requires at least: 2.9
5
- Tested up to: 4.0.1
6
- Stable tag: 1.0
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
 
9
 
10
- Say goodbye to bad filenames like Château de Ferrières.jpg and say hello to nice and clean filenames like chateau-de-ferrieres.jpg.
11
 
12
  == Description ==
13
 
14
- Say goodbye to bad image filenames like Château de Ferrières.jpg or Smörgåsbord.png and say hello to nice and clean filenames like chateau-de-ferrieres.jpg and smargasbord.png. This WordPress plugin replaces accent characters and special characters, like Swedish or German umlauts, in the filename of files uploaded to the WordPress media library. The range of file types that the plugin reacts to can be easily extended using a filter in your theme or plugin.
 
 
 
 
15
 
16
  = Features =
17
 
18
- * Can be also used for other file types
19
  * Is multisite compatible
20
  * Works with custom upload_dir setups
21
  * Doesn't alter your database or uploads settings
22
 
23
- = Use for more than images =
24
 
25
- By default, this plugin works with the following file types: GIF, JPEG, PNG. However, you can easily use this plugin for any file type you want by adding your own set of mime types using the `clean_image_filenames_mime_types` filter in your theme or plugin. **When using the filter, the default set of mime types is overridden.** For a complete list of mime types, see http://en.wikipedia.org/wiki/Internet_media_type
26
 
27
- Example usage:
28
 
29
  <pre><code>function my_clean_image_filenames_mime_types() {
30
  $mime_types = array(
@@ -47,16 +52,27 @@ add_filter('clean_image_filenames_mime_types', 'my_clean_image_filenames_mime_ty
47
 
48
  No matter how hard you try to teach people to name their files in a certain way before uploading, sooner or later you will end up with a media library with filenames like Château de Ferrières.jpg or Smörgåsbord.png. Sometimes browsers have a hard time displaying images with filenames like these and the images end up broken.
49
 
50
- = Can this plugin change the filename of files already in the media library? =
51
 
52
- No, this plugin only changes the filename when the file is uploaded to the WordPress media library for the first time.
53
 
54
- = Why not use the remove_accents() filter? =
55
 
56
- The `remove_accents()` filter converts accent charactes to ASCII characters. While that works great, it doesn't convert periods, commas, and other special characters. You never know what weird characters might end up in a filename, so we thought it was a better idea to use the `sanitize_title()` filter that does everything we need; converts accent characters to ASCII characters and converts whitespaces and special characters to dashes.
57
 
58
  == Changelog ==
59
 
 
 
 
 
 
60
  = 1.0 =
61
 
62
- * Initial release.
 
 
 
 
 
 
2
  Contributors: Upperdog, Gesen
3
  Tags: upload, images, files, media,
4
  Requires at least: 2.9
5
+ Tested up to: 4.1
6
+ Stable tag: 1.1
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
+ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RDLVEE9A2LMUL
10
 
11
+ Filenames with special characters or language accent characters can sometimes be a problem. This plugin takes care of that by cleaning the filenames.
12
 
13
  == Description ==
14
 
15
+ Filenames with special characters or language accent characters, like Château de Ferrières.jpg and smörgåsbord.png, can sometimes be a problem for browsers or servers. This plugin takes care of that and cleans the filenames of files uploaded to the WordPress media library.
16
+
17
+ This plugin cleans the filenames from special characters like exclamation marks, periods, and commas and accent characters like Swedish and German umlauts. Special characters are remove, accent characters are converted to their non-accent equivalent, and blank spaces are converted into dashes.
18
+
19
+ Easily set the plugin to clean the filenames of images only or all files uploaded to the media library. Developers can take advantage of the built in filter to get really specific about what file types to clean the filenames of.
20
 
21
  = Features =
22
 
23
+ * Can be used for all file types, only image file types, or only specific file types
24
  * Is multisite compatible
25
  * Works with custom upload_dir setups
26
  * Doesn't alter your database or uploads settings
27
 
28
+ = Plugin filter for developers =
29
 
30
+ Developers can get really specific about what file types to clean by using the `clean_image_filenames_mime_types` filter in their plugins or themes. **When using this filter, settings saved through the settings page are overridden.** For a complete list of mime types, see [Wikipedia](http://en.wikipedia.org/wiki/Internet_media_type).
31
 
32
+ The following example would make the plugin clean the filenames for PDF, JPEG and PNG files only.
33
 
34
  <pre><code>function my_clean_image_filenames_mime_types() {
35
  $mime_types = array(
52
 
53
  No matter how hard you try to teach people to name their files in a certain way before uploading, sooner or later you will end up with a media library with filenames like Château de Ferrières.jpg or Smörgåsbord.png. Sometimes browsers have a hard time displaying images with filenames like these and the images end up broken.
54
 
55
+ = Can this plugin clean the filenames of existing files in the media library? =
56
 
57
+ No, this plugin only cleans the filenames of files when they are being uploaded to the media library.
58
 
59
+ == Screenshots ==
60
 
61
+ 1. Easily choose between cleaning the filenames of all files or images only.
62
 
63
  == Changelog ==
64
 
65
+ = 1.1 =
66
+
67
+ * Added plugin settings to media settings page with option to convert all file types or just image file types.
68
+ * Added shortcut to plugin settings from the plugins list.
69
+
70
  = 1.0 =
71
 
72
+ * Initial release.
73
+
74
+ == Upgrade Notice ==
75
+
76
+ = 1.1 =
77
+
78
+ This version adds plugin settings to the media settings page which lets you select between cleaning the filenames of all files or images only. The filter from version 1.0 is still available.