Clean Image Filenames - Version 1.3

Version Description

  • Rewrite cleaning function to better handle specific characters.
  • Make sure code is compliant with WordPress Coding Standards.
Download this release

Release Info

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

Code changes from version 1.2.1 to 1.3

Files changed (4) hide show
  1. LICENSE +0 -340
  2. clean-image-filenames.php +365 -137
  3. readme.txt +42 -30
  4. uninstall.php +9 -7
LICENSE DELETED
@@ -1,340 +0,0 @@
1
- GNU GENERAL PUBLIC LICENSE
2
- Version 2, June 1991
3
-
4
- Copyright (C) 1989, 1991 Free Software Foundation, Inc., <http://fsf.org/>
5
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6
- Everyone is permitted to copy and distribute verbatim copies
7
- of this license document, but changing it is not allowed.
8
-
9
- Preamble
10
-
11
- The licenses for most software are designed to take away your
12
- freedom to share and change it. By contrast, the GNU General Public
13
- License is intended to guarantee your freedom to share and change free
14
- software--to make sure the software is free for all its users. This
15
- General Public License applies to most of the Free Software
16
- Foundation's software and to any other program whose authors commit to
17
- using it. (Some other Free Software Foundation software is covered by
18
- the GNU Lesser General Public License instead.) You can apply it to
19
- your programs, too.
20
-
21
- When we speak of free software, we are referring to freedom, not
22
- price. Our General Public Licenses are designed to make sure that you
23
- have the freedom to distribute copies of free software (and charge for
24
- this service if you wish), that you receive source code or can get it
25
- if you want it, that you can change the software or use pieces of it
26
- in new free programs; and that you know you can do these things.
27
-
28
- To protect your rights, we need to make restrictions that forbid
29
- anyone to deny you these rights or to ask you to surrender the rights.
30
- These restrictions translate to certain responsibilities for you if you
31
- distribute copies of the software, or if you modify it.
32
-
33
- For example, if you distribute copies of such a program, whether
34
- gratis or for a fee, you must give the recipients all the rights that
35
- you have. You must make sure that they, too, receive or can get the
36
- source code. And you must show them these terms so they know their
37
- rights.
38
-
39
- We protect your rights with two steps: (1) copyright the software, and
40
- (2) offer you this license which gives you legal permission to copy,
41
- distribute and/or modify the software.
42
-
43
- Also, for each author's protection and ours, we want to make certain
44
- that everyone understands that there is no warranty for this free
45
- software. If the software is modified by someone else and passed on, we
46
- want its recipients to know that what they have is not the original, so
47
- that any problems introduced by others will not reflect on the original
48
- authors' reputations.
49
-
50
- Finally, any free program is threatened constantly by software
51
- patents. We wish to avoid the danger that redistributors of a free
52
- program will individually obtain patent licenses, in effect making the
53
- program proprietary. To prevent this, we have made it clear that any
54
- patent must be licensed for everyone's free use or not licensed at all.
55
-
56
- The precise terms and conditions for copying, distribution and
57
- modification follow.
58
-
59
- GNU GENERAL PUBLIC LICENSE
60
- TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61
-
62
- 0. This License applies to any program or other work which contains
63
- a notice placed by the copyright holder saying it may be distributed
64
- under the terms of this General Public License. The "Program", below,
65
- refers to any such program or work, and a "work based on the Program"
66
- means either the Program or any derivative work under copyright law:
67
- that is to say, a work containing the Program or a portion of it,
68
- either verbatim or with modifications and/or translated into another
69
- language. (Hereinafter, translation is included without limitation in
70
- the term "modification".) Each licensee is addressed as "you".
71
-
72
- Activities other than copying, distribution and modification are not
73
- covered by this License; they are outside its scope. The act of
74
- running the Program is not restricted, and the output from the Program
75
- is covered only if its contents constitute a work based on the
76
- Program (independent of having been made by running the Program).
77
- Whether that is true depends on what the Program does.
78
-
79
- 1. You may copy and distribute verbatim copies of the Program's
80
- source code as you receive it, in any medium, provided that you
81
- conspicuously and appropriately publish on each copy an appropriate
82
- copyright notice and disclaimer of warranty; keep intact all the
83
- notices that refer to this License and to the absence of any warranty;
84
- and give any other recipients of the Program a copy of this License
85
- along with the Program.
86
-
87
- You may charge a fee for the physical act of transferring a copy, and
88
- you may at your option offer warranty protection in exchange for a fee.
89
-
90
- 2. You may modify your copy or copies of the Program or any portion
91
- of it, thus forming a work based on the Program, and copy and
92
- distribute such modifications or work under the terms of Section 1
93
- above, provided that you also meet all of these conditions:
94
-
95
- a) You must cause the modified files to carry prominent notices
96
- stating that you changed the files and the date of any change.
97
-
98
- b) You must cause any work that you distribute or publish, that in
99
- whole or in part contains or is derived from the Program or any
100
- part thereof, to be licensed as a whole at no charge to all third
101
- parties under the terms of this License.
102
-
103
- c) If the modified program normally reads commands interactively
104
- when run, you must cause it, when started running for such
105
- interactive use in the most ordinary way, to print or display an
106
- announcement including an appropriate copyright notice and a
107
- notice that there is no warranty (or else, saying that you provide
108
- a warranty) and that users may redistribute the program under
109
- these conditions, and telling the user how to view a copy of this
110
- License. (Exception: if the Program itself is interactive but
111
- does not normally print such an announcement, your work based on
112
- the Program is not required to print an announcement.)
113
-
114
- These requirements apply to the modified work as a whole. If
115
- identifiable sections of that work are not derived from the Program,
116
- and can be reasonably considered independent and separate works in
117
- themselves, then this License, and its terms, do not apply to those
118
- sections when you distribute them as separate works. But when you
119
- distribute the same sections as part of a whole which is a work based
120
- on the Program, the distribution of the whole must be on the terms of
121
- this License, whose permissions for other licensees extend to the
122
- entire whole, and thus to each and every part regardless of who wrote it.
123
-
124
- Thus, it is not the intent of this section to claim rights or contest
125
- your rights to work written entirely by you; rather, the intent is to
126
- exercise the right to control the distribution of derivative or
127
- collective works based on the Program.
128
-
129
- In addition, mere aggregation of another work not based on the Program
130
- with the Program (or with a work based on the Program) on a volume of
131
- a storage or distribution medium does not bring the other work under
132
- the scope of this License.
133
-
134
- 3. You may copy and distribute the Program (or a work based on it,
135
- under Section 2) in object code or executable form under the terms of
136
- Sections 1 and 2 above provided that you also do one of the following:
137
-
138
- a) Accompany it with the complete corresponding machine-readable
139
- source code, which must be distributed under the terms of Sections
140
- 1 and 2 above on a medium customarily used for software interchange; or,
141
-
142
- b) Accompany it with a written offer, valid for at least three
143
- years, to give any third party, for a charge no more than your
144
- cost of physically performing source distribution, a complete
145
- machine-readable copy of the corresponding source code, to be
146
- distributed under the terms of Sections 1 and 2 above on a medium
147
- customarily used for software interchange; or,
148
-
149
- c) Accompany it with the information you received as to the offer
150
- to distribute corresponding source code. (This alternative is
151
- allowed only for noncommercial distribution and only if you
152
- received the program in object code or executable form with such
153
- an offer, in accord with Subsection b above.)
154
-
155
- The source code for a work means the preferred form of the work for
156
- making modifications to it. For an executable work, complete source
157
- code means all the source code for all modules it contains, plus any
158
- associated interface definition files, plus the scripts used to
159
- control compilation and installation of the executable. However, as a
160
- special exception, the source code distributed need not include
161
- anything that is normally distributed (in either source or binary
162
- form) with the major components (compiler, kernel, and so on) of the
163
- operating system on which the executable runs, unless that component
164
- itself accompanies the executable.
165
-
166
- If distribution of executable or object code is made by offering
167
- access to copy from a designated place, then offering equivalent
168
- access to copy the source code from the same place counts as
169
- distribution of the source code, even though third parties are not
170
- compelled to copy the source along with the object code.
171
-
172
- 4. You may not copy, modify, sublicense, or distribute the Program
173
- except as expressly provided under this License. Any attempt
174
- otherwise to copy, modify, sublicense or distribute the Program is
175
- void, and will automatically terminate your rights under this License.
176
- However, parties who have received copies, or rights, from you under
177
- this License will not have their licenses terminated so long as such
178
- parties remain in full compliance.
179
-
180
- 5. You are not required to accept this License, since you have not
181
- signed it. However, nothing else grants you permission to modify or
182
- distribute the Program or its derivative works. These actions are
183
- prohibited by law if you do not accept this License. Therefore, by
184
- modifying or distributing the Program (or any work based on the
185
- Program), you indicate your acceptance of this License to do so, and
186
- all its terms and conditions for copying, distributing or modifying
187
- the Program or works based on it.
188
-
189
- 6. Each time you redistribute the Program (or any work based on the
190
- Program), the recipient automatically receives a license from the
191
- original licensor to copy, distribute or modify the Program subject to
192
- these terms and conditions. You may not impose any further
193
- restrictions on the recipients' exercise of the rights granted herein.
194
- You are not responsible for enforcing compliance by third parties to
195
- this License.
196
-
197
- 7. If, as a consequence of a court judgment or allegation of patent
198
- infringement or for any other reason (not limited to patent issues),
199
- conditions are imposed on you (whether by court order, agreement or
200
- otherwise) that contradict the conditions of this License, they do not
201
- excuse you from the conditions of this License. If you cannot
202
- distribute so as to satisfy simultaneously your obligations under this
203
- License and any other pertinent obligations, then as a consequence you
204
- may not distribute the Program at all. For example, if a patent
205
- license would not permit royalty-free redistribution of the Program by
206
- all those who receive copies directly or indirectly through you, then
207
- the only way you could satisfy both it and this License would be to
208
- refrain entirely from distribution of the Program.
209
-
210
- If any portion of this section is held invalid or unenforceable under
211
- any particular circumstance, the balance of the section is intended to
212
- apply and the section as a whole is intended to apply in other
213
- circumstances.
214
-
215
- It is not the purpose of this section to induce you to infringe any
216
- patents or other property right claims or to contest validity of any
217
- such claims; this section has the sole purpose of protecting the
218
- integrity of the free software distribution system, which is
219
- implemented by public license practices. Many people have made
220
- generous contributions to the wide range of software distributed
221
- through that system in reliance on consistent application of that
222
- system; it is up to the author/donor to decide if he or she is willing
223
- to distribute software through any other system and a licensee cannot
224
- impose that choice.
225
-
226
- This section is intended to make thoroughly clear what is believed to
227
- be a consequence of the rest of this License.
228
-
229
- 8. If the distribution and/or use of the Program is restricted in
230
- certain countries either by patents or by copyrighted interfaces, the
231
- original copyright holder who places the Program under this License
232
- may add an explicit geographical distribution limitation excluding
233
- those countries, so that distribution is permitted only in or among
234
- countries not thus excluded. In such case, this License incorporates
235
- the limitation as if written in the body of this License.
236
-
237
- 9. The Free Software Foundation may publish revised and/or new versions
238
- of the General Public License from time to time. Such new versions will
239
- be similar in spirit to the present version, but may differ in detail to
240
- address new problems or concerns.
241
-
242
- Each version is given a distinguishing version number. If the Program
243
- specifies a version number of this License which applies to it and "any
244
- later version", you have the option of following the terms and conditions
245
- either of that version or of any later version published by the Free
246
- Software Foundation. If the Program does not specify a version number of
247
- this License, you may choose any version ever published by the Free Software
248
- Foundation.
249
-
250
- 10. If you wish to incorporate parts of the Program into other free
251
- programs whose distribution conditions are different, write to the author
252
- to ask for permission. For software which is copyrighted by the Free
253
- Software Foundation, write to the Free Software Foundation; we sometimes
254
- make exceptions for this. Our decision will be guided by the two goals
255
- of preserving the free status of all derivatives of our free software and
256
- of promoting the sharing and reuse of software generally.
257
-
258
- NO WARRANTY
259
-
260
- 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261
- FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262
- OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263
- PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264
- OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265
- MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266
- TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267
- PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268
- REPAIR OR CORRECTION.
269
-
270
- 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271
- WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272
- REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273
- INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274
- OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275
- TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276
- YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277
- PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278
- POSSIBILITY OF SUCH DAMAGES.
279
-
280
- END OF TERMS AND CONDITIONS
281
-
282
- How to Apply These Terms to Your New Programs
283
-
284
- If you develop a new program, and you want it to be of the greatest
285
- possible use to the public, the best way to achieve this is to make it
286
- free software which everyone can redistribute and change under these terms.
287
-
288
- To do so, attach the following notices to the program. It is safest
289
- to attach them to the start of each source file to most effectively
290
- convey the exclusion of warranty; and each file should have at least
291
- the "copyright" line and a pointer to where the full notice is found.
292
-
293
- {description}
294
- Copyright (C) {year} {fullname}
295
-
296
- This program is free software; you can redistribute it and/or modify
297
- it under the terms of the GNU General Public License as published by
298
- the Free Software Foundation; either version 2 of the License, or
299
- (at your option) any later version.
300
-
301
- This program is distributed in the hope that it will be useful,
302
- but WITHOUT ANY WARRANTY; without even the implied warranty of
303
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304
- GNU General Public License for more details.
305
-
306
- You should have received a copy of the GNU General Public License along
307
- with this program; if not, write to the Free Software Foundation, Inc.,
308
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309
-
310
- Also add information on how to contact you by electronic and paper mail.
311
-
312
- If the program is interactive, make it output a short notice like this
313
- when it starts in an interactive mode:
314
-
315
- Gnomovision version 69, Copyright (C) year name of author
316
- Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317
- This is free software, and you are welcome to redistribute it
318
- under certain conditions; type `show c' for details.
319
-
320
- The hypothetical commands `show w' and `show c' should show the appropriate
321
- parts of the General Public License. Of course, the commands you use may
322
- be called something other than `show w' and `show c'; they could even be
323
- mouse-clicks or menu items--whatever suits your program.
324
-
325
- You should also get your employer (if you work as a programmer) or your
326
- school, if any, to sign a "copyright disclaimer" for the program, if
327
- necessary. Here is a sample; alter the names:
328
-
329
- Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330
- `Gnomovision' (which makes passes at compilers) written by James Hacker.
331
-
332
- {signature of Ty Coon}, 1 April 1989
333
- Ty Coon, President of Vice
334
-
335
- This General Public License does not permit incorporating your program into
336
- proprietary programs. If your program is a subroutine library, you may
337
- consider it more useful to permit linking proprietary applications with the
338
- library. If this is what you want to do, use the GNU Lesser General
339
- Public License instead of this License.
340
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
clean-image-filenames.php CHANGED
@@ -1,53 +1,48 @@
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.2.1
6
  * Author: Upperdog
7
  * Author URI: https://upperdog.com
8
  * Author Email: hello@upperdog.com
9
  * Text Domain: clean-image-filenames
10
  * Domain Path: /languages
11
- * License: GPL2
 
 
 
 
 
 
12
  */
13
 
14
- /* Copyright 2014 Upperdog (email : hello@upperdog.se)
15
-
16
- This program is free software; you can redistribute it and/or modify
17
- it under the terms of the GNU General Public License, version 2, as
18
- published by the Free Software Foundation.
19
-
20
- This program is distributed in the hope that it will be useful,
21
- but WITHOUT ANY WARRANTY; without even the implied warranty of
22
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
- GNU General Public License for more details.
24
-
25
- You should have received a copy of the GNU General Public License
26
- along with this program; if not, write to the Free Software
27
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28
- */
29
-
30
- if ( !defined( 'ABSPATH' ) ) {
31
  exit;
32
  }
33
 
 
 
 
34
  class CleanImageFilenames {
35
 
36
  /**
37
  * Plugin settings.
38
- *
39
- * @var array Plugin settings for version, default mime types.
40
  * @since 1.1
 
 
41
  */
42
  public $plugin_settings = array(
43
- 'version' => '1.2',
44
- 'default_mime_types' => array(
45
  'image/gif',
46
  'image/jpeg',
47
  'image/pjpeg',
48
  'image/png',
49
- 'image/tiff'
50
- )
51
  );
52
 
53
  /**
@@ -55,7 +50,7 @@ class CleanImageFilenames {
55
  *
56
  * @since 1.0
57
  */
58
- function __construct() {
59
  register_activation_hook( __FILE__, array( $this, 'plugin_activation' ) );
60
  add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
61
  add_action( 'admin_init', array( $this, 'admin_init' ) );
@@ -69,44 +64,45 @@ class CleanImageFilenames {
69
  *
70
  * @since 1.1
71
  */
72
- function plugin_activation() {
73
  $this->add_default_plugin_settings();
74
  }
75
 
76
  /**
77
- * Updates plugin version database setting and calls default settings function.
78
- *
79
- * Checks current plugin version. If the plugin has been updated, the saved
80
- * plugin version in the database is updated.
81
- *
82
- * Adds default plugin settings if they don't already exist. Default plugin
83
- * settings didn't exist prior to version 1.1.
84
  *
85
  * @since 1.1
86
  */
87
- function plugins_loaded() {
88
 
89
- if ( $this->plugin_settings[ 'version' ] !== get_option( 'clean_image_filenames_plugin_version' ) ) {
90
- update_option( 'clean_image_filenames_plugin_version', $this->plugin_settings[ 'version' ] );
 
91
  }
92
 
 
93
  $this->add_default_plugin_settings();
94
  }
95
 
96
  /**
97
- * Adds default plugin settings in the database.
98
  *
99
- * This function runs when the plugin is activated and when plugins are loaded
100
- * using the plugins_loaded hook. The function updates default plugin settings
101
- * in the database options table.
102
- *
103
- * Adds default value for mime types field if it doesn't already exist
104
  *
105
  * @since 1.1
106
  */
107
- function add_default_plugin_settings() {
108
 
109
- if ( FALSE === get_option( 'clean_image_filenames_mime_types' ) ) {
110
  add_option( 'clean_image_filenames_mime_types', 'images' );
111
  }
112
  }
@@ -116,47 +112,48 @@ class CleanImageFilenames {
116
  *
117
  * @since 1.1
118
  */
119
- function admin_init() {
120
 
121
- // Load plugin translations
122
- load_plugin_textdomain( 'clean-image-filenames', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
123
 
124
- // Add settings section
125
  add_settings_section( 'clean_image_filenames_settings_section', 'Clean Image Filenames', array( $this, 'clean_image_filenames_settings_section_callback' ), 'media' );
126
 
127
- // Add settings field
128
  add_settings_field(
129
  'clean_image_filenames_mime_types',
130
  __( 'File types', 'clean-image-filenames' ),
131
  array( $this, 'clean_image_filenames_mime_types_callback' ),
132
- 'media',
133
- 'clean_image_filenames_settings_section',
134
  array(
135
  'alternatives' => array(
136
  array(
137
- 'value' => 'all',
138
- 'label' => __( 'All file types', 'clean-image-filenames' )
139
- ),
140
  array(
141
- 'value' => 'images',
142
- 'label' => __( 'Images only', 'clean-image-filenames' )
143
- )
144
- )
145
  )
146
  );
147
 
148
- // Register settings
149
  register_setting( 'media', 'clean_image_filenames_mime_types' );
150
  }
151
 
152
  /**
153
  * Add custom action links to the plugin's row in the plugins list.
154
- *
155
  * @since 1.1
156
- * @param array Original action links.
157
- * @return array Action links with new addition.
 
158
  */
159
- function add_action_links( $links ) {
160
  $plugin_action_links = array( '<a href="' . admin_url( 'options-media.php' ) . '">' . __( 'Settings' ) . '</a>' );
161
  return array_merge( $links, $plugin_action_links );
162
  }
@@ -166,33 +163,31 @@ class CleanImageFilenames {
166
  *
167
  * @since 1.1
168
  */
169
- function clean_image_filenames_settings_section_callback() {
170
- echo '<p>' . __( 'Choose which file types that Clean Image Filenames shall improve the filenames for when files are uploaded.', 'clean-image-filenames' ) . '</p>';
171
  }
172
 
173
  /**
174
- * Outputs the settings fields.
175
- *
176
- * If the plugin filter has been used in a plugin or theme, the filter
177
- * overrides the settings are the settings are therefore disabled.
 
 
178
  *
179
- * If the plugin filter has not been used, the user selected setting of what
180
- * file types to clean is used.
181
- *
182
  * @since 1.1
183
- * @param array Field defails.
 
184
  */
185
- function clean_image_filenames_mime_types_callback( $args ) {
186
 
187
- if ( apply_filters( 'clean_image_filenames_mime_types', $this->plugin_settings[ 'default_mime_types' ] ) !== $this->plugin_settings[ 'default_mime_types' ] ) {
188
-
189
  echo '<input name="clean_image_filenames_mime_types" id="clean_image_filenames_mime_types" type="hidden" value="' . get_option( 'clean_image_filenames_mime_types' ) . '">';
190
- 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>';
191
-
192
  } else {
193
 
194
- foreach ( $args[ 'alternatives' ] as $alternative ) {
195
- 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>';
196
  }
197
  }
198
  }
@@ -200,98 +195,331 @@ class CleanImageFilenames {
200
  /**
201
  * Checks whether or not the current file should be cleaned.
202
  *
203
- * This function runs when files are being uploaded to the WordPress media
204
- * library. The function checks if the clean_image_filenames_mime_types filter
205
- * has been used and overrides other settings if it has. Otherwise, the plugin
206
- * settings are used.
207
  *
208
- * If a file shall be cleaned or not is checked by comparing the current file's
209
  * mime type to the list of mime types to be cleaned.
210
  *
211
  * @since 1.1 Added more complex checks and moved the actual cleaning to clean_filename().
212
  * @since 1.0
213
- * @param array The file information including the filename in $file['name'].
214
- * @return array The file information with the cleaned or original filename.
 
215
  */
216
- function upload_filter( $file ) {
217
-
218
- $original_filename = pathinfo( $file[ 'name' ] );
219
- set_transient( '_clean_image_filenames_original_filename', $original_filename[ 'filename' ], 60 );
 
 
 
220
  $mime_types_setting = get_option( 'clean_image_filenames_mime_types' );
221
- $default_mime_types = $this->plugin_settings[ 'default_mime_types' ];
222
- $valid_mime_types = apply_filters( 'clean_image_filenames_mime_types', $default_mime_types );
223
 
224
  if ( $valid_mime_types !== $default_mime_types ) {
225
 
226
- if ( in_array( $file[ 'type' ], $valid_mime_types ) ) {
 
227
  $file = $this->clean_filename( $file );
228
  }
229
-
230
  } else {
231
 
232
- if ( 'all' == $mime_types_setting ) {
 
233
  $file = $this->clean_filename( $file );
234
- } elseif ( 'images' == $mime_types_setting && in_array( $file[ 'type' ], $default_mime_types ) ) {
235
  $file = $this->clean_filename( $file );
236
  }
237
  }
238
 
239
- // Return cleaned file or input file if it didn't match
240
- return $file;
241
  }
242
 
243
  /**
244
- * Performs the filename cleaning.
245
  *
246
- * This function performs the actual cleaning of the filename. It takes an
247
- * array with the file information, cleans the filename and sends the file
248
- * information back to where the function was called from.
249
  *
250
- * @since 1.1
251
- * @param array File details including the filename in $file['name'].
252
- * @return array The $file array with cleaned filename.
 
 
253
  */
254
- function clean_filename( $file ) {
255
-
256
- $input = array(
257
- 'ß',
258
- '·',
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
259
  );
260
-
261
- $output = array(
262
- 'ss',
263
- '.'
264
- );
265
-
266
- $path = pathinfo( $file[ 'name' ] );
267
- $new_filename = preg_replace( '/.' . $path[ 'extension' ] . '$/', '', $file[ 'name' ] );
268
- $new_filename = str_replace( $input, $output, $new_filename );
269
- $file[ 'name' ] = sanitize_title( $new_filename ) . '.' . $path[ 'extension' ];
270
-
 
 
 
 
 
 
 
 
 
 
 
271
  return $file;
272
  }
273
-
274
  /**
275
- * Set attachment title to original, un-cleaned filename
276
  *
277
- * The original, un-cleaned filename is saved as a transient called
278
- * _clean_image_filenames_original_filename just before the filename is cleaned
279
- * and saved. When WordPress adds the attachment to the database, this function
280
- * picks up the original filename from the transient and saves it as the
281
  * attachment title.
282
  *
283
  * @since 1.2
284
- * @param int Attachment post ID.
285
- */
286
- function update_attachment_title( $attachment_id ) {
287
-
 
288
  $original_filename = get_transient( '_clean_image_filenames_original_filename' );
289
-
290
  if ( $original_filename ) {
291
- wp_update_post( array( 'ID' => $attachment_id, 'post_title' => $original_filename ) );
 
 
 
 
 
 
 
 
 
292
  delete_transient( '_clean_image_filenames_original_filename' );
293
  }
294
  }
295
  }
296
 
297
- $clean_image_filenames = new CleanImageFilenames();
1
  <?php
2
  /**
3
  * Plugin Name: Clean Image Filenames
4
+ * Description: This plugin automatically converts language accent characters to non-accent characters in filenames when uploading to the media library.
5
+ * Version: 1.3
6
  * Author: Upperdog
7
  * Author URI: https://upperdog.com
8
  * Author Email: hello@upperdog.com
9
  * Text Domain: clean-image-filenames
10
  * Domain Path: /languages
11
+ * License: GPLv3
12
+ *
13
+ * @package clean-image-filenames
14
+ * @link https://github.com/upperdog/clean-image-filenames
15
+ * @author Upperdog <hello@upperdog.com>
16
+ * @copyright Upperdog
17
+ * @license GPLv3
18
  */
19
 
20
+ // Exit if called incorrectly.
21
+ if ( ! defined( 'ABSPATH' ) ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  exit;
23
  }
24
 
25
+ /**
26
+ * Clean Image Filenames.
27
+ */
28
  class CleanImageFilenames {
29
 
30
  /**
31
  * Plugin settings.
32
+ *
 
33
  * @since 1.1
34
+ *
35
+ * @var array $plugin_settings Plugin settings for version, default mime types.
36
  */
37
  public $plugin_settings = array(
38
+ 'version' => '1.3',
39
+ 'default_mime_types' => array(
40
  'image/gif',
41
  'image/jpeg',
42
  'image/pjpeg',
43
  'image/png',
44
+ 'image/tiff',
45
+ ),
46
  );
47
 
48
  /**
50
  *
51
  * @since 1.0
52
  */
53
+ public function __construct() {
54
  register_activation_hook( __FILE__, array( $this, 'plugin_activation' ) );
55
  add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
56
  add_action( 'admin_init', array( $this, 'admin_init' ) );
64
  *
65
  * @since 1.1
66
  */
67
+ public function plugin_activation() {
68
  $this->add_default_plugin_settings();
69
  }
70
 
71
  /**
72
+ * Plugins loaded hook.
73
+ *
74
+ * Checks current plugin version. If the plugin has been updated, the saved
75
+ * plugin version in the database is updated.
76
+ *
77
+ * Adds default plugin settings if they don't already exist. Default plugin
78
+ * settings didn't exist prior to version 1.1.
79
  *
80
  * @since 1.1
81
  */
82
+ public function plugins_loaded() {
83
 
84
+ // Update plugin version database setting if it's not up to date.
85
+ if ( get_option( 'clean_image_filenames_plugin_version' ) !== $this->plugin_settings['version'] ) {
86
+ update_option( 'clean_image_filenames_plugin_version', $this->plugin_settings['version'] );
87
  }
88
 
89
+ // Add default plugin settings to database.
90
  $this->add_default_plugin_settings();
91
  }
92
 
93
  /**
94
+ * Adds default plugin settings to the database.
95
  *
96
+ * This function runs when the plugin is activated and when plugins are loaded
97
+ * using the plugins_loaded hook. The function updates default plugin settings
98
+ * in the database options table. It adds default value for mime types field
99
+ * if it doesn't already exist
 
100
  *
101
  * @since 1.1
102
  */
103
+ public function add_default_plugin_settings() {
104
 
105
+ if ( false === get_option( 'clean_image_filenames_mime_types' ) ) {
106
  add_option( 'clean_image_filenames_mime_types', 'images' );
107
  }
108
  }
112
  *
113
  * @since 1.1
114
  */
115
+ public function admin_init() {
116
 
117
+ // Load plugin translations.
118
+ load_plugin_textdomain( 'clean-image-filenames', false, dirname( plugin_basename( __FILE__ ) ) . 'languages' );
119
 
120
+ // Add settings section.
121
  add_settings_section( 'clean_image_filenames_settings_section', 'Clean Image Filenames', array( $this, 'clean_image_filenames_settings_section_callback' ), 'media' );
122
 
123
+ // Add settings field.
124
  add_settings_field(
125
  'clean_image_filenames_mime_types',
126
  __( 'File types', 'clean-image-filenames' ),
127
  array( $this, 'clean_image_filenames_mime_types_callback' ),
128
+ 'media',
129
+ 'clean_image_filenames_settings_section',
130
  array(
131
  'alternatives' => array(
132
  array(
133
+ 'value' => 'all',
134
+ 'label' => __( 'All file types', 'clean-image-filenames' ),
135
+ ),
136
  array(
137
+ 'value' => 'images',
138
+ 'label' => __( 'Images only', 'clean-image-filenames' ),
139
+ ),
140
+ ),
141
  )
142
  );
143
 
144
+ // Register settings.
145
  register_setting( 'media', 'clean_image_filenames_mime_types' );
146
  }
147
 
148
  /**
149
  * Add custom action links to the plugin's row in the plugins list.
150
+ *
151
  * @since 1.1
152
+ *
153
+ * @param array $links Default plugin action links.
154
+ * @return array $links Modified plugin action links.
155
  */
156
+ public function add_action_links( $links ) {
157
  $plugin_action_links = array( '<a href="' . admin_url( 'options-media.php' ) . '">' . __( 'Settings' ) . '</a>' );
158
  return array_merge( $links, $plugin_action_links );
159
  }
163
  *
164
  * @since 1.1
165
  */
166
+ public function clean_image_filenames_settings_section_callback() {
167
+ echo '<p>' . esc_html_e( 'Choose which file types that Clean Image Filenames shall improve the filenames for when files are uploaded.', 'clean-image-filenames' ) . '</p>';
168
  }
169
 
170
  /**
171
+ * Output plugin settings fields.
172
+ *
173
+ * If the plugin filter has not been used, the user selected setting of what
174
+ * file types to clean is used. If the plugin filter has been used in a plugin
175
+ * or theme, the filter overrides the settings are the settings are therefore
176
+ * disabled.
177
  *
 
 
 
178
  * @since 1.1
179
+ *
180
+ * @param array $args Field details.
181
  */
182
+ public function clean_image_filenames_mime_types_callback( $args ) {
183
 
184
+ if ( apply_filters( 'clean_image_filenames_mime_types', $this->plugin_settings['default_mime_types'] ) !== $this->plugin_settings['default_mime_types'] ) {
 
185
  echo '<input name="clean_image_filenames_mime_types" id="clean_image_filenames_mime_types" type="hidden" value="' . get_option( 'clean_image_filenames_mime_types' ) . '">';
186
+ echo '<i>' . esc_html_e( '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>';
 
187
  } else {
188
 
189
+ foreach ( $args['alternatives'] as $alternative ) {
190
+ 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>';
191
  }
192
  }
193
  }
195
  /**
196
  * Checks whether or not the current file should be cleaned.
197
  *
198
+ * This function runs when files are being uploaded to the WordPress media
199
+ * library. The function checks if the clean_image_filenames_mime_types filter
200
+ * has been used and overrides other settings if it has. Otherwise, the plugin
201
+ * settings are used.
202
  *
203
+ * If a file shall be cleaned or not is checked by comparing the current file's
204
  * mime type to the list of mime types to be cleaned.
205
  *
206
  * @since 1.1 Added more complex checks and moved the actual cleaning to clean_filename().
207
  * @since 1.0
208
+ *
209
+ * @param array $file The file information including the filename in $file['name'].
210
+ * @return array $file The file information with the cleaned or original filename.
211
  */
212
+ public function upload_filter( $file ) {
213
+
214
+ // Save original filename in transient and add to attachment post later.
215
+ $original_filename = pathinfo( $file['name'] );
216
+ set_transient( '_clean_image_filenames_original_filename', $original_filename['filename'], 60 );
217
+
218
+ // Get mime type settings.
219
  $mime_types_setting = get_option( 'clean_image_filenames_mime_types' );
220
+ $default_mime_types = $this->plugin_settings['default_mime_types'];
221
+ $valid_mime_types = apply_filters( 'clean_image_filenames_mime_types', $default_mime_types );
222
 
223
  if ( $valid_mime_types !== $default_mime_types ) {
224
 
225
+ // Use mime types defined with filter.
226
+ if ( in_array( $file['type'], $valid_mime_types, true ) ) {
227
  $file = $this->clean_filename( $file );
228
  }
 
229
  } else {
230
 
231
+ // Use mime types as defined in settings.
232
+ if ( 'all' === $mime_types_setting ) {
233
  $file = $this->clean_filename( $file );
234
+ } elseif ( 'images' === $mime_types_setting && in_array( $file['type'], $default_mime_types, true ) ) {
235
  $file = $this->clean_filename( $file );
236
  }
237
  }
238
 
239
+ // Return cleaned file, or input file file type is not set to be cleaned.
240
+ return $file;
241
  }
242
 
243
  /**
244
+ * Clean filename.
245
  *
246
+ * Performs the cleaning of filenames. It replaces whitespaces, some specific
247
+ * chacters and uses WordPress' remove_accents() function for the rest. It
248
+ * also converts filenames to lowercase.
249
  *
250
+ * @since 1.3 Rewrote filename cleaning to better handle specific characters.
251
+ * @since 1.1 Added function.
252
+ *
253
+ * @param array $file Uploaded file details.
254
+ * @return array $file Uploaded file details with cleaned filename.
255
  */
256
+ public function clean_filename( $file ) {
257
+
258
+ // Get file details.
259
+ $file_pathinfo = pathinfo( $file['name'] );
260
+
261
+ // Replace whitespaces with dashes.
262
+ $cleaned_filename = str_replace( ' ', '-', $file_pathinfo['filename'] );
263
+
264
+ // Specific replacements not handled at all or not handled well by remove_accents().
265
+ $specific_replacements = array(
266
+ 'А' => 'a',
267
+ 'Ά' => 'a',
268
+ 'Á' => 'a',
269
+ 'Α' => 'a',
270
+ 'Ä' => 'a',
271
+ 'Å' => 'a',
272
+ 'Ã' => 'a',
273
+ 'Â' => 'a',
274
+ 'À' => 'a',
275
+ 'α' => 'a',
276
+ 'Ą' => 'a',
277
+ 'а' => 'a',
278
+ 'ά' => 'a',
279
+ 'Æ' => 'ae',
280
+ 'å' => 'a',
281
+ 'ä' => 'a',
282
+ 'б' => 'b',
283
+ 'Б' => 'b',
284
+ 'Ć' => 'c',
285
+ 'Ç' => 'c',
286
+ 'ц' => 'c',
287
+ 'Ц' => 'c',
288
+ 'Č' => 'c',
289
+ 'Ч' => 'ch',
290
+ 'χ' => 'ch',
291
+ 'ч' => 'ch',
292
+ 'Χ' => 'ch',
293
+ 'д' => 'd',
294
+ 'Ď' => 'd',
295
+ 'Д' => 'd',
296
+ 'δ' => 'd',
297
+ 'Δ' => 'd',
298
+ 'Ð' => 'd',
299
+ 'ε' => 'e',
300
+ 'έ' => 'e',
301
+ 'Έ' => 'e',
302
+ 'Э' => 'e',
303
+ 'Ę' => 'e',
304
+ 'Ε' => 'e',
305
+ 'э' => 'e',
306
+ 'Ê' => 'e',
307
+ 'Ě' => 'e',
308
+ 'É' => 'e',
309
+ 'Е' => 'e',
310
+ 'е' => 'e',
311
+ 'È' => 'e',
312
+ 'Ë' => 'e',
313
+ 'Φ' => 'f',
314
+ 'Ф' => 'f',
315
+ 'φ' => 'f',
316
+ 'ф' => 'f',
317
+ 'Γ' => 'g',
318
+ 'γ' => 'g',
319
+ 'ґ' => 'g',
320
+ 'Г' => 'g',
321
+ 'Ґ' => 'g',
322
+ 'г' => 'g',
323
+ 'Х' => 'h',
324
+ 'х' => 'h',
325
+ 'Ή' => 'i',
326
+ 'Ί' => 'i',
327
+ 'І' => 'i',
328
+ 'і' => 'i',
329
+ 'ΐ' => 'i',
330
+ 'Η' => 'i',
331
+ 'Ι' => 'i',
332
+ 'η' => 'i',
333
+ 'ϊ' => 'i',
334
+ 'ι' => 'i',
335
+ 'Ï' => 'i',
336
+ 'Ì' => 'i',
337
+ 'ή' => 'i',
338
+ 'Í' => 'i',
339
+ 'Î' => 'i',
340
+ 'ί' => 'i',
341
+ 'Ϊ' => 'i',
342
+ 'и' => 'i',
343
+ 'И' => 'i',
344
+ 'й' => 'j',
345
+ 'Й' => 'j',
346
+ 'Я' => 'ja',
347
+ 'я' => 'ja',
348
+ 'ю' => 'ju',
349
+ 'Ю' => 'ju',
350
+ 'Κ' => 'k',
351
+ 'κ' => 'k',
352
+ 'к' => 'k',
353
+ 'К' => 'k',
354
+ 'л' => 'l',
355
+ 'Λ' => 'l',
356
+ 'λ' => 'l',
357
+ 'Ł' => 'l',
358
+ 'Л' => 'l',
359
+ 'Μ' => 'm',
360
+ 'м' => 'm',
361
+ 'М' => 'm',
362
+ 'μ' => 'm',
363
+ 'Ñ' => 'n',
364
+ 'ν' => 'n',
365
+ 'Ν' => 'n',
366
+ 'н' => 'n',
367
+ 'Ň' => 'n',
368
+ 'Ń' => 'n',
369
+ 'Н' => 'n',
370
+ 'ώ' => 'o',
371
+ 'Ò' => 'o',
372
+ 'ό' => 'o',
373
+ 'Ő' => 'o',
374
+ 'Ώ' => 'o',
375
+ 'Õ' => 'o',
376
+ 'Ο' => 'o',
377
+ 'Ø' => 'o',
378
+ 'ο' => 'o',
379
+ 'Ό' => 'o',
380
+ 'Ω' => 'o',
381
+ 'Ó' => 'o',
382
+ 'Ö' => 'o',
383
+ 'ö' => 'o',
384
+ 'О' => 'o',
385
+ 'о' => 'o',
386
+ 'ω' => 'o',
387
+ 'Ô' => 'o',
388
+ 'п' => 'p',
389
+ 'þ' => 'p',
390
+ 'π' => 'p',
391
+ 'Π' => 'p',
392
+ 'П' => 'p',
393
+ 'Þ' => 'p',
394
+ 'Ψ' => 'ps',
395
+ 'ψ' => 'ps',
396
+ 'Р' => 'r',
397
+ 'Ř' => 'r',
398
+ 'Ρ' => 'r',
399
+ 'р' => 'r',
400
+ 'ρ' => 'r',
401
+ 'С' => 's',
402
+ 'σ' => 's',
403
+ 'Ś' => 's',
404
+ 'ς' => 's',
405
+ 'Σ' => 's',
406
+ 'Š' => 's',
407
+ 'с' => 's',
408
+ 'Ш' => 'sh',
409
+ 'ш' => 'sh',
410
+ 'щ' => 'shch',
411
+ 'Щ' => 'shch',
412
+ 'ß' => 'ss',
413
+ 'Τ' => 't',
414
+ 'τ' => 't',
415
+ 'Ť' => 't',
416
+ 'т' => 't',
417
+ 'Т' => 't',
418
+ 'θ' => 'th',
419
+ 'Θ' => 'th',
420
+ 'Ў' => 'u',
421
+ 'ў' => 'u',
422
+ 'Ű' => 'u',
423
+ 'Ú' => 'u',
424
+ 'У' => 'u',
425
+ 'Ù' => 'u',
426
+ 'Û' => 'u',
427
+ 'Ů' => 'u',
428
+ 'у' => 'u',
429
+ 'ü' => 'u',
430
+ 'Ü' => 'u',
431
+ 'в' => 'v',
432
+ 'В' => 'v',
433
+ 'Β' => 'v',
434
+ 'β' => 'v',
435
+ 'Ξ' => 'x',
436
+ '×' => 'x',
437
+ 'ξ' => 'x',
438
+ 'Ϋ' => 'y',
439
+ 'Ÿ' => 'y',
440
+ 'Ý' => 'y',
441
+ 'Υ' => 'y',
442
+ 'υ' => 'y',
443
+ 'Ύ' => 'y',
444
+ 'ύ' => 'y',
445
+ 'ΰ' => 'y',
446
+ 'ϋ' => 'y',
447
+ 'ы' => 'y',
448
+ 'Ы' => 'y',
449
+ 'є' => 'ye',
450
+ 'Є' => 'ye',
451
+ 'ї' => 'yi',
452
+ 'Ї' => 'yi',
453
+ 'ё' => 'yo',
454
+ 'Ё' => 'yo',
455
+ 'з' => 'z',
456
+ 'Ź' => 'z',
457
+ 'З' => 'z',
458
+ 'Ž' => 'z',
459
+ 'ζ' => 'z',
460
+ 'Ζ' => 'z',
461
+ 'Ż' => 'z',
462
+ 'Ж' => 'zh',
463
+ 'ж' => 'zh',
464
+ '_' => '-',
465
+ '%20' => '-',
466
  );
467
+
468
+ // Replace specific characters.
469
+ $cleaned_filename = str_replace( array_keys( $specific_replacements ), array_values( $specific_replacements ), $cleaned_filename );
470
+
471
+ // Convert characters to ASCII equivalents.
472
+ $cleaned_filename = remove_accents( $cleaned_filename );
473
+
474
+ // Convert filename to lowercase.
475
+ $cleaned_filename = strtolower( $cleaned_filename );
476
+
477
+ // Remove characters that are not a-z, 0-9, or - (dash).
478
+ $cleaned_filename = preg_replace( '/[^a-z0-9-]/', '', $cleaned_filename );
479
+
480
+ // Remove multiple dashes in a row.
481
+ $cleaned_filename = preg_replace( '/-+/', '-', $cleaned_filename );
482
+
483
+ // Trim potential leftover dashes at each end of filename.
484
+ $cleaned_filename = trim( $cleaned_filename, '-' );
485
+
486
+ // Replace original filename with cleaned filename.
487
+ $file['name'] = $cleaned_filename . '.' . $file_pathinfo['extension'];
488
+
489
  return $file;
490
  }
491
+
492
  /**
493
+ * Save original filename to attachment title.
494
  *
495
+ * The original, un-cleaned filename is saved as a transient called
496
+ * _clean_image_filenames_original_filename just before the filename is cleaned
497
+ * and saved. When WordPress adds the attachment to the database, this function
498
+ * picks up the original filename from the transient and saves it as the
499
  * attachment title.
500
  *
501
  * @since 1.2
502
+ *
503
+ * @param int $attachment_id Attachment post ID.
504
+ */
505
+ public function update_attachment_title( $attachment_id ) {
506
+
507
  $original_filename = get_transient( '_clean_image_filenames_original_filename' );
508
+
509
  if ( $original_filename ) {
510
+
511
+ // Update attachment post.
512
+ wp_update_post(
513
+ array(
514
+ 'ID' => $attachment_id,
515
+ 'post_title' => $original_filename,
516
+ )
517
+ );
518
+
519
+ // Delete transient.
520
  delete_transient( '_clean_image_filenames_original_filename' );
521
  }
522
  }
523
  }
524
 
525
+ new CleanImageFilenames();
readme.txt CHANGED
@@ -1,68 +1,80 @@
1
  === Clean Image Filenames ===
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.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, not only images
24
- * Multisite compatible
25
- * Works with custom upload_dir setups
26
- * Doesn't alter your database or uploads settings
27
- * Gutenberg ready
28
 
29
- = Plugin filter for developers =
30
 
31
- 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).
32
 
33
- The following example would make the plugin clean the filenames for PDF, JPEG and PNG files only.
 
 
 
 
34
 
35
  <pre><code>function my_clean_image_filenames_mime_types() {
36
  $mime_types = array(
37
- 'application/pdf',
38
- 'image/jpeg',
39
- 'image/png',
40
  );
41
  return $mime_types;
42
  }
43
- add_filter('clean_image_filenames_mime_types', 'my_clean_image_filenames_mime_types');</code></pre>
44
 
45
  == Installation ==
46
 
47
- 1. Upload the `clean-image-filenames` directory to the `/wp-content/plugins/` directory.
48
- 2. Activate the plugin through the 'Plugins' menu in WordPress.
49
 
50
- == Frequently Asked Questions ==
51
 
52
- = Why does this plugin exist? =
 
53
 
54
- 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.
55
 
56
- = Can this plugin clean the filenames of existing files in the media library? =
57
 
58
  No, this plugin only cleans the filenames of files when they are being uploaded to the media library.
59
 
60
  == Screenshots ==
61
 
62
- 1. Easily choose between cleaning the filenames of all files or images only.
63
 
64
  == Changelog ==
65
 
 
 
 
 
 
66
  = 1.2.1 =
67
 
68
  * Enable plugin to be translated/internationalization.
1
  === Clean Image Filenames ===
2
  Contributors: Upperdog, Gesen
3
+ Tags: upload, images, files, media, sanitize,
4
  Requires at least: 2.9
5
+ Tested up to: 6.1
6
+ Stable tag: 1.3
7
+ License: GPLv3
8
+ License URI: https://www.gnu.org/licenses/gpl-3.0.html
9
+ Donate link: https://paypal.me/gesen
10
 
11
+ This plugin automatically converts language accent characters to non-accent characters in filenames when uploading to the media library.
12
 
13
  == Description ==
14
 
15
+ This plugin automatically converts language accent characters in filenames when uploading to the media library. Characters are converted into browser and server friendly, non-accent characters.
16
 
17
+ = Features =
18
 
19
+ * Converts accent characters to non-accent, latin equivalents in Swedish, Danish, German, and more.
20
+ * Removes special characters like exclamation marks, periods, hashtags, and more.
21
+ * Lets you choose if you want to convert only image files, or all file types.
22
+ * Makes site and server migrations easier thanks to non-accent character filenames.
23
+ * Provides filter hook for developers who want to specify which file types to convert.
24
 
25
+ = Examples =
26
 
27
+ * Räksmörgås.jpg raksmorgas.jpg
28
+ * Æblegrød_FTW!.gif → aeblegrod-ftw.gif
29
+ * Château de Ferrières.png chateau-de-ferrieres.png
 
 
30
 
31
+ = Worth noting =
32
 
33
+ The plugin only converts filenames when the files are being uploaded. It can not convert existing files.
34
 
35
+ = Filter for developers =
36
+
37
+ This filter provides developers a way to specify which file types the plugin should convert. This filter overrides the plugin settings on the media settings page. For a complete list of mime types, see [Wikipedia](http://en.wikipedia.org/wiki/Internet_media_type).
38
+
39
+ The following example will convert PDF, JPEG and PNG files only:
40
 
41
  <pre><code>function my_clean_image_filenames_mime_types() {
42
  $mime_types = array(
43
+ 'application/pdf',
44
+ 'image/jpeg',
45
+ 'image/png',
46
  );
47
  return $mime_types;
48
  }
49
+ add_filter( 'clean_image_filenames_mime_types', 'my_clean_image_filenames_mime_types' );</code></pre>
50
 
51
  == Installation ==
52
 
53
+ 1. Search for Clean Image Filenames in the plugins directory.
54
+ 2. Install and activate the plugin.
55
 
56
+ or
57
 
58
+ 1. Download and unzip the plugin and upload the `clean-image-filenames` directory to your `/plugins/` directory.
59
+ 2. Activate the plugin through the 'Plugins' menu in WordPress.
60
 
61
+ == Frequently Asked Questions ==
62
 
63
+ = Can this plugin convert filenames of existing files in the media library? =
64
 
65
  No, this plugin only cleans the filenames of files when they are being uploaded to the media library.
66
 
67
  == Screenshots ==
68
 
69
+ 1. Easily choose between cleaning the filenames of all file types or images only.
70
 
71
  == Changelog ==
72
 
73
+ = 1.3 =
74
+
75
+ * Rewrite cleaning function to better handle specific characters.
76
+ * Make sure code is compliant with WordPress Coding Standards.
77
+
78
  = 1.2.1 =
79
 
80
  * Enable plugin to be translated/internationalization.
uninstall.php CHANGED
@@ -1,17 +1,19 @@
1
  <?php
2
  /**
3
- * Uninstall plugin
4
- *
5
- * This file is executed when the plugin is uninstalled and deletes plugin settings.
 
 
6
  *
7
  * @since 1.1.1
8
  */
9
 
10
- // Exit if uninstall is not called from WordPress
11
- if ( !defined( 'WP_UNINSTALL_PLUGIN' ) )
12
  exit();
 
13
 
14
- // Delete plugin settings
15
  delete_option( 'clean_image_filenames_plugin_version' );
16
  delete_option( 'clean_image_filenames_mime_types' );
17
- ?>
1
  <?php
2
  /**
3
+ * Uninstall plugin.
4
+ *
5
+ * This file is executed when the plugin is uninstalled and deletes plugin settings.
6
+ *
7
+ * @package clean-image-filenames
8
  *
9
  * @since 1.1.1
10
  */
11
 
12
+ // Exit if uninstall is not called from WordPress.
13
+ if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
14
  exit();
15
+ }
16
 
17
+ // Delete plugin settings.
18
  delete_option( 'clean_image_filenames_plugin_version' );
19
  delete_option( 'clean_image_filenames_mime_types' );