Clean Image Filenames - Version 1.2

Version Description

  • Set original, un-cleaned filename as attachment title.
Download this release

Release Info

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

Code changes from version 1.1.1 to 1.2

Files changed (2) hide show
  1. clean-image-filenames.php +73 -71
  2. readme.txt +6 -2
clean-image-filenames.php CHANGED
@@ -2,7 +2,7 @@
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.1
6
  * Author: Upperdog
7
  * Author URI: http://upperdog.com
8
  * Author Email: hello@upperdog.com
@@ -25,7 +25,7 @@
25
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
  */
27
 
28
- if(!defined('ABSPATH')) {
29
  exit;
30
  }
31
 
@@ -37,9 +37,8 @@ class CleanImageFilenames {
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',
@@ -49,34 +48,29 @@ class CleanImageFilenames {
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
  *
@@ -88,17 +82,15 @@ class CleanImageFilenames {
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
  *
@@ -110,55 +102,51 @@ class CleanImageFilenames {
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
  *
@@ -166,25 +154,20 @@ class CleanImageFilenames {
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
  *
@@ -197,23 +180,21 @@ class CleanImageFilenames {
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
  *
@@ -230,25 +211,26 @@ class CleanImageFilenames {
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
 
@@ -256,7 +238,6 @@ class CleanImageFilenames {
256
  return $file;
257
  }
258
 
259
-
260
  /**
261
  * Performs the filename cleaning.
262
  *
@@ -268,15 +249,36 @@ class CleanImageFilenames {
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();
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.2
6
  * Author: Upperdog
7
  * Author URI: http://upperdog.com
8
  * Author Email: hello@upperdog.com
25
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
  */
27
 
28
+ if ( !defined( 'ABSPATH' ) ) {
29
  exit;
30
  }
31
 
37
  * @var array Plugin settings for version, default mime types.
38
  * @since 1.1
39
  */
 
40
  public $plugin_settings = array(
41
+ 'version' => '1.2',
42
  'default_mime_types' => array(
43
  'image/gif',
44
  'image/jpeg',
48
  )
49
  );
50
 
 
51
  /**
52
  * Sets up hooks, actions and filters that the plugin responds to.
53
  *
54
  * @since 1.0
55
  */
 
56
  function __construct() {
57
+ register_activation_hook( __FILE__, array( $this, 'plugin_activation' ) );
58
+ add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
59
+ add_action( 'admin_init', array( $this, 'admin_init' ) );
60
+ add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_action_links' ) );
61
+ add_action( 'wp_handle_upload_prefilter', array( $this, 'upload_filter' ) );
62
+ add_action( 'add_attachment', array( $this, 'update_attachment_title' ) );
63
  }
64
 
 
65
  /**
66
  * Adds default plugin settings on plugin activation.
67
  *
68
  * @since 1.1
69
  */
 
70
  function plugin_activation() {
71
  $this->add_default_plugin_settings();
72
  }
73
 
 
74
  /**
75
  * Updates plugin version database setting and calls default settings function.
76
  *
82
  *
83
  * @since 1.1
84
  */
 
85
  function plugins_loaded() {
86
 
87
+ if ( $this->plugin_settings[ 'version' ] !== get_option( 'clean_image_filenames_plugin_version' ) ) {
88
+ update_option( 'clean_image_filenames_plugin_version', $this->plugin_settings[ 'version' ] );
89
  }
90
 
91
  $this->add_default_plugin_settings();
92
  }
93
 
 
94
  /**
95
  * Adds default plugin settings in the database.
96
  *
102
  *
103
  * @since 1.1
104
  */
 
105
  function add_default_plugin_settings() {
106
 
107
+ if ( FALSE === get_option( 'clean_image_filenames_mime_types' ) ) {
108
+ add_option( 'clean_image_filenames_mime_types', 'images' );
109
  }
110
  }
111
 
 
112
  /**
113
  * Sets up plugin translations and plugin settings fields.
114
  *
115
  * @since 1.1
116
  */
 
117
  function admin_init() {
118
 
119
  // Load plugin translations
120
+ load_plugin_textdomain( 'clean_image_filenames', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
121
 
122
  // Add settings section
123
+ add_settings_section( 'clean_image_filenames_settings_section', 'Clean Image Filenames', array( $this, 'clean_image_filenames_settings_section_callback' ), 'media' );
124
 
125
  // Add settings field
126
  add_settings_field(
127
  'clean_image_filenames_mime_types',
128
+ __( 'File types', 'clean_image_filenames' ),
129
+ array( $this, 'clean_image_filenames_mime_types_callback' ),
130
  'media',
131
  'clean_image_filenames_settings_section',
132
  array(
133
  'alternatives' => array(
134
  array(
135
  'value' => 'all',
136
+ 'label' => __( 'All file types', 'clean_image_filenames' )
137
  ),
138
  array(
139
  'value' => 'images',
140
+ 'label' => __( 'Images only', 'clean_image_filenames' )
141
  )
142
  )
143
  )
144
  );
145
 
146
  // Register settings
147
+ register_setting( 'media', 'clean_image_filenames_mime_types' );
148
  }
149
 
 
150
  /**
151
  * Add custom action links to the plugin's row in the plugins list.
152
  *
154
  * @param array Original action links.
155
  * @return array Action links with new addition.
156
  */
157
+ function add_action_links( $links ) {
158
+ $plugin_action_links = array( '<a href="' . admin_url( 'options-media.php' ) . '">' . __( 'Settings' ) . '</a>' );
159
+ return array_merge( $links, $plugin_action_links );
 
160
  }
161
 
 
162
  /**
163
  * Outputs content before the settings fields.
164
  *
165
  * @since 1.1
166
  */
 
167
  function clean_image_filenames_settings_section_callback() {
168
+ echo '<p>' . __( 'Choose which file types that Clean Image Filenames shall improve the filenames for when files are uploaded.', 'clean_image_filenames' ) . '</p>';
 
169
  }
170
 
 
171
  /**
172
  * Outputs the settings fields.
173
  *
180
  * @since 1.1
181
  * @param array Field defails.
182
  */
183
+ function clean_image_filenames_mime_types_callback( $args ) {
184
 
185
+ if ( apply_filters( 'clean_image_filenames_mime_types', $this->plugin_settings[ 'default_mime_types' ] ) !== $this->plugin_settings[ 'default_mime_types' ] ) {
 
 
186
 
187
+ echo '<input name="clean_image_filenames_mime_types" id="clean_image_filenames_mime_types" type="hidden" value="' . get_option( 'clean_image_filenames_mime_types' ) . '">';
188
+ 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>';
189
 
190
  } else {
191
 
192
+ foreach ( $args[ 'alternatives' ] as $alternative ) {
193
+ 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>';
194
  }
195
  }
196
  }
197
 
 
198
  /**
199
  * Checks whether or not the current file should be cleaned.
200
  *
211
  * @param array The file information including the filename in $file['name'].
212
  * @return array The file information with the cleaned or original filename.
213
  */
214
+ function upload_filter( $file ) {
215
+
216
+ $original_filename = pathinfo( $file[ 'name' ] );
217
+ set_transient( '_clean_image_filenames_original_filename', $original_filename[ 'filename' ], 60 );
218
+ $mime_types_setting = get_option( 'clean_image_filenames_mime_types' );
219
+ $default_mime_types = $this->plugin_settings[ 'default_mime_types' ];
220
+ $valid_mime_types = apply_filters( 'clean_image_filenames_mime_types', $default_mime_types );
221
+
222
+ if ( $valid_mime_types !== $default_mime_types ) {
223
+
224
+ if ( in_array( $file[ 'type' ], $valid_mime_types ) ) {
225
+ $file = $this->clean_filename( $file );
226
  }
227
 
228
  } else {
229
 
230
+ if ( 'all' == $mime_types_setting ) {
231
+ $file = $this->clean_filename( $file );
232
+ } elseif ( 'images' == $mime_types_setting && in_array( $file[ 'type' ], $default_mime_types ) ) {
233
+ $file = $this->clean_filename( $file );
234
  }
235
  }
236
 
238
  return $file;
239
  }
240
 
 
241
  /**
242
  * Performs the filename cleaning.
243
  *
249
  * @param array File details including the filename in $file['name'].
250
  * @return array The $file array with cleaned filename.
251
  */
252
+ function clean_filename( $file ) {
253
 
254
+ $path = pathinfo( $file[ 'name' ] );
255
+ $new_filename = preg_replace( '/.' . $path[ 'extension' ] . '$/', '', $file[ 'name' ] );
256
+ $file[ 'name' ] = sanitize_title( $new_filename ) . '.' . $path[ 'extension' ];
257
+
 
 
258
  return $file;
259
  }
260
+
261
+ /**
262
+ * Set attachment title to original, un-cleaned filename
263
+ *
264
+ * The original, un-cleaned filename is saved as a transient called
265
+ * _clean_image_filenames_original_filename just before the filename is cleaned
266
+ * and saved. When WordPress adds the attachment to the database, this function
267
+ * picks up the original filename from the transient and saves it as the
268
+ * attachment title.
269
+ *
270
+ * @since 1.2
271
+ * @param int Attachment post ID.
272
+ */
273
+ function update_attachment_title( $attachment_id ) {
274
+
275
+ $original_filename = get_transient( '_clean_image_filenames_original_filename' );
276
+
277
+ if ( $original_filename ) {
278
+ wp_update_post( array( 'ID' => $attachment_id, 'post_title' => $original_filename ) );
279
+ delete_transient( '_clean_image_filenames_original_filename' );
280
+ }
281
+ }
282
  }
283
 
284
  $clean_image_filenames = new CleanImageFilenames();
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: Upperdog, Gesen
3
  Tags: upload, images, files, media,
4
  Requires at least: 2.9
5
- Tested up to: 4.9
6
- Stable tag: 1.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
@@ -62,6 +62,10 @@ No, this plugin only cleans the filenames of files when they are being uploaded
62
 
63
  == Changelog ==
64
 
 
 
 
 
65
  = 1.1.1 =
66
 
67
  * Added uninstall script that deletes plugin settings when the plugin is uninstalled.
2
  Contributors: Upperdog, Gesen
3
  Tags: upload, images, files, media,
4
  Requires at least: 2.9
5
+ Tested up to: 4.9.4
6
+ Stable tag: 1.2
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
62
 
63
  == Changelog ==
64
 
65
+ = 1.2 =
66
+
67
+ * Set original, un-cleaned filename as attachment title.
68
+
69
  = 1.1.1 =
70
 
71
  * Added uninstall script that deletes plugin settings when the plugin is uninstalled.