Media File Renamer - Version 4.2.0

Version Description

  • Add: All the actions in the Media Library are now asynchronous. No more page reload!
  • Update: Many changes and little enhancements in the code, for speed, security and code-tidyness.
  • Note: If you like it, please review the plugin here: https://wordpress.org/support/plugin/media-file-renamer/reviews/?rate=5#new-post. It's important for us :) Thank you!
Download this release

Release Info

Developer TigrouMeow
Plugin Icon 128x128 Media File Renamer
Version 4.2.0
Comparing to
See all releases

Code changes from version 4.9.4 to 4.2.0

api.php CHANGED
@@ -2,7 +2,7 @@
2
 
3
  /**
4
  *
5
- * FUNCTIONS
6
  *
7
  */
8
 
@@ -12,6 +12,23 @@ function mfrh_rename( $mediaId ) {
12
  return $mfrh_core->rename( $mediaId );
13
  }
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  /**
16
  *
17
  * ACTIONS AND FILTERS
2
 
3
  /**
4
  *
5
+ * GLOBAL FUNCTIONS
6
  *
7
  */
8
 
12
  return $mfrh_core->rename( $mediaId );
13
  }
14
 
15
+ // Move the media to another folder (relative to /uploads/)
16
+ function mfrh_move( $mediaId, $newPath ) {
17
+ global $mfrh_core;
18
+ return $mfrh_core->move( $mediaId, $newPath );
19
+ }
20
+
21
+ /**
22
+ *
23
+ * TESTS
24
+ *
25
+ */
26
+
27
+ // add_action( 'wp_loaded', 'mfrh_test_move' );
28
+ // function mfrh_test_move() {
29
+ // mfrh_move( 1620, '/2020/01' );
30
+ // }
31
+
32
  /**
33
  *
34
  * ACTIONS AND FILTERS
core.php CHANGED
@@ -17,58 +17,12 @@ class Meow_MFRH_Core {
17
 
18
  // Those actions/filters are only for the admin screens
19
  if ( is_admin() ) {
20
- add_action( 'admin_head', array( $this, 'admin_head' ) );
21
- add_action( 'admin_menu', array( $this, 'admin_menu' ) );
22
- add_action( 'wp_ajax_mfrh_rename_media', array( $this, 'wp_ajax_mfrh_rename_media' ) );
23
- add_action( 'wp_ajax_mfrh_undo_media', array( $this, 'wp_ajax_mfrh_undo_media' ) );
24
- add_filter( 'media_send_to_editor', array( $this, 'media_send_to_editor' ), 20, 3 );
25
- add_action( 'admin_notices', array( $this, 'admin_notices' ) );
26
- //add_action( 'edit_attachment', array( $this, 'edit_attachment' ) );
27
- //add_action( 'add_attachment', array( $this, 'add_attachment' ) );
28
- add_action( 'add_meta_boxes', array( $this, 'add_rename_metabox' ) );
29
  add_filter( 'attachment_fields_to_save', array( $this, 'attachment_fields_to_save' ), 20, 2 );
30
  add_action( 'save_post', array( $this, 'save_post' ) );
31
 
32
  if ( get_option( 'mfrh_on_upload', false ) ) {
33
  add_filter( 'wp_handle_upload_prefilter', array( $this, 'wp_handle_upload_prefilter' ), 10, 2 );
34
  }
35
-
36
- // Column for Media Library
37
- $method = apply_filters( 'mfrh_method', 'media_title' );
38
- if ( $method != 'none' ) {
39
- add_filter( 'manage_media_columns', array( $this, 'add_media_columns' ) );
40
- add_action( 'manage_media_custom_column', array( $this, 'manage_media_custom_column' ), 10, 2 );
41
- }
42
-
43
- // Media Library Bulk Actions
44
- add_filter( 'bulk_actions-upload', array( $this, 'library_bulk_actions' ) );
45
- add_filter( 'handle_bulk_actions-upload', array( $this, 'library_bulk_actions_handler' ), 10, 3 );
46
- }
47
- }
48
-
49
- /**
50
- *
51
- * ERROR/INFO MESSAGE HANDLING
52
- *
53
- */
54
-
55
- function admin_notices() {
56
- $screen = get_current_screen();
57
- if ( ( $screen->base == 'post' && $screen->post_type == 'attachment' ) ||
58
- ( $screen->base == 'media' && isset( $_GET['attachment_id'] ) ) ) {
59
- $id = isset( $_GET['post'] ) ? $_GET['post'] : $_GET['attachment_id'];
60
- if ( $this->check_attachment( get_post( $id, ARRAY_A ), $output ) ) {
61
- if ( $output['desired_filename_exists'] ) {
62
- echo '<div class="error"><p>
63
- The file ' . $output['desired_filename'] . ' already exists. Please give a new title for this media.
64
- </p></div>';
65
- }
66
- }
67
- if ( $this->wpml_media_is_installed() && !$this->is_real_media( $id ) ) {
68
- echo '<div class="error"><p>
69
- This attachment seems to be a virtual copy (or translation). Media File Renamer will not make any modification from here.
70
- </p></div>';
71
- }
72
  }
73
  }
74
 
@@ -111,273 +65,6 @@ class Meow_MFRH_Core {
111
  rmdir( $directory );
112
  }
113
 
114
- /**
115
- *
116
- * MEDIA LIBRARY
117
- *
118
- */
119
-
120
- function library_bulk_actions( $bulk_actions ) {
121
- $bulk_actions['mfrh_lock_all'] = __( 'Lock (Renamer)', 'media-file-renamer');
122
- $bulk_actions['mfrh_unlock_all'] = __( 'Unlock (Renamer)', 'media-file-renamer');
123
- $bulk_actions['mfrh_rename_all'] = __( 'Rename (Renamer)', 'media-file-renamer');
124
- return $bulk_actions;
125
- }
126
-
127
- function library_bulk_actions_handler( $redirect_to, $doaction, $ids ) {
128
- if ( $doaction == 'mfrh_lock_all' ) {
129
- foreach ( $ids as $post_id ) {
130
- add_post_meta( $post_id, '_manual_file_renaming', true, true );
131
- }
132
- }
133
- if ( $doaction == 'mfrh_unlock_all' ) {
134
- foreach ( $ids as $post_id ) {
135
- delete_post_meta( $post_id, '_manual_file_renaming' );
136
- }
137
- }
138
- if ( $doaction == 'mfrh_rename_all' ) {
139
- foreach ( $ids as $post_id ) {
140
- $this->rename( $post_id );
141
- }
142
- }
143
- return $redirect_to;
144
- }
145
-
146
- /**
147
- *
148
- * 'RENAME' LINK
149
- *
150
- */
151
-
152
- function add_media_columns( $columns ) {
153
- $columns['mfrh_column'] = __( 'Rename', 'media-file-renamer' );
154
- return $columns;
155
- }
156
-
157
- function manage_media_custom_column( $column_name, $id ) {
158
- $paged = isset( $_GET['paged'] ) ? ( '&paged=' . $_GET['paged'] ) : "";
159
- if ( $column_name !== 'mfrh_column' )
160
- return;
161
-
162
- // Information for locked media
163
- $locked = get_post_meta( $id, '_manual_file_renaming', true );
164
- if ( $locked ) {
165
- echo "<span title='" . __( 'Manually renamed.', 'media-file-renamer' ) . "' style='font-size: 24px; color: #36B15C;' class='dashicons dashicons-yes'></span>";
166
- $page = isset( $_GET['page'] ) ? ( '&page=' . $_GET['page'] ) : "";
167
- echo "<a title='" . __( 'Locked to manual only. Click to unlock it.', 'media-file-renamer' ) . "' href='?" . $page . "&mfrh_unlock=" . $id . $paged . "'><span style='font-size: 20px; position: relative; top: 0px; color: #36B15C;' class='dashicons dashicons-lock'></span></a>";
168
- return;
169
- }
170
-
171
- // Information for media that needs renaming
172
- $needs_rename = $this->check_attachment( get_post( $id, ARRAY_A ), $output );
173
- if ( $needs_rename ) {
174
- $this->generate_explanation( $output );
175
- return;
176
- }
177
-
178
- // Information for non-locked media
179
- $original_filename = get_post_meta( $id, '_original_filename', true );
180
- echo "<span title='" . __( 'Automatically renamed.', 'media-file-renamer' ) . "'style='font-size: 24px; color: #36B15C;' class='dashicons dashicons-yes'></span>";
181
- $page = isset( $_GET['page'] ) ? ( '&page=' . $_GET['page'] ) : "";
182
- if ( get_option( 'mfrh_undo', false ) && !empty( $original_filename ) ) {
183
- echo "<a title='" . __( 'Rename to original filename: ', 'media-file-renamer' ) . $original_filename . "' href='?" . $page . "&mfrh_undo=" . $id . $paged . "' style='position: relative; top: 4px; font-size: 15px; color: #de4817;' class='dashicons dashicons-undo'></a>";
184
- }
185
- echo "<a title='" . __( 'Click to lock it to manual only.', 'media-file-renamer' ) . "' href='?" . $page . "&mfrh_lock=" . $id . $paged . "'><span style='font-size: 20px;' class='dashicons dashicons-unlock'></span></a>";
186
- }
187
-
188
- function admin_head() {
189
- if ( !empty( $_GET['mfrh_rename'] ) ) {
190
- $mfrh_rename = $_GET['mfrh_rename'];
191
- $this->rename( $mfrh_rename );
192
- $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'mfrh_rename' ), $_SERVER['REQUEST_URI'] );
193
- }
194
- if ( !empty( $_GET['mfrh_unlock'] ) ) {
195
- $mfrh_unlock = $_GET['mfrh_unlock'];
196
- delete_post_meta( $mfrh_unlock, '_manual_file_renaming' );
197
- $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'mfrh_unlock' ), $_SERVER['REQUEST_URI'] );
198
- }
199
- if ( !empty( $_GET['mfrh_undo'] ) ) {
200
- $mfrh_undo = $_GET['mfrh_undo'];
201
- $original_filename = get_post_meta( $mfrh_undo, '_original_filename', true );
202
- $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'mfrh_undo' ), $_SERVER['REQUEST_URI'] );
203
- $this->rename( $mfrh_undo, $original_filename );
204
-
205
- $fp = get_attached_file( $mfrh_undo );
206
- $path_parts = pathinfo( $fp );
207
- $basename = $path_parts['basename'];
208
- if ( $basename == $original_filename )
209
- delete_post_meta( $mfrh_undo, '_original_filename' );
210
- }
211
- if ( !empty( $_GET['mfrh_lock'] ) ) {
212
- $mfrh_lock = $_GET['mfrh_lock'];
213
- add_post_meta( $mfrh_lock, '_manual_file_renaming', true, true );
214
- $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'mfrh_lock' ), $_SERVER['REQUEST_URI'] );
215
- }
216
-
217
- ?>
218
- <script type="text/javascript" >
219
-
220
- var current;
221
- var ids = [];
222
-
223
- function mfrh_process_next() {
224
- var data = { action: 'mfrh_rename_media', subaction: 'renameMediaId', id: ids[current - 1] };
225
- jQuery('#mfrh_progression').text(current + "/" + ids.length);
226
- jQuery.post(ajaxurl, data, function (response) {
227
- if (++current <= ids.length) {
228
- mfrh_process_next();
229
- }
230
- else {
231
- jQuery('#mfrh_progression').html("<?php echo __( "Done. Please <a href='?page=rename_media_files'>refresh</a> this page.", 'media-file-renamer' ); ?>");
232
- }
233
- });
234
- }
235
-
236
- function mfrh_rename_media(all) {
237
- current = 1;
238
- ids = [];
239
- var data = { action: 'mfrh_rename_media', subaction: 'getMediaIds', all: all ? '1' : '0' };
240
- jQuery('#mfrh_progression').text("<?php echo __( "Please wait...", 'media-file-renamer' ); ?>");
241
- jQuery.post(ajaxurl, data, function (response) {
242
- reply = jQuery.parseJSON(response);
243
- ids = reply.ids;
244
- jQuery('#mfrh_progression').html(current + "/" + ids.length);
245
- mfrh_process_next();
246
- });
247
- }
248
-
249
- function mfrh_process_next_undo() {
250
- var data = { action: 'mfrh_undo_media', subaction: 'undoMediaId', id: ids[current - 1] };
251
- jQuery('#mfrh_progression').text(current + "/" + ids.length);
252
- jQuery.post(ajaxurl, data, function (response) {
253
- if (++current <= ids.length) {
254
- mfrh_process_next_undo();
255
- }
256
- else {
257
- jQuery('#mfrh_progression').html("<?php echo __( "Done. Please <a href='?page=rename_media_files'>refresh</a> this page.", 'media-file-renamer' ); ?>");
258
- }
259
- });
260
- }
261
-
262
- function mfrh_undo_media(all) {
263
- current = 1;
264
- ids = [];
265
- var data = { action: 'mfrh_undo_media', subaction: 'getMediaIds', all: all ? '1' : '0' };
266
- jQuery('#mfrh_progression').text("<?php echo __( "Please wait...", 'media-file-renamer' ); ?>");
267
- jQuery.post(ajaxurl, data, function (response) {
268
- reply = jQuery.parseJSON(response);
269
- ids = reply.ids;
270
- jQuery('#mfrh_progression').html(current + "/" + ids.length);
271
- mfrh_process_next_undo();
272
- });
273
- }
274
-
275
- function mfrh_export_table(table) {
276
- var table = jQuery(table);
277
- var data = [];
278
- // Header
279
- table.find('thead tr').each(function(i, tr) {
280
- var row = [];
281
- jQuery(tr).find('th').each(function(i, td) {
282
- var text = jQuery(td).text();
283
- row.push(text);
284
- });
285
- data.push(row);
286
- });
287
- // Body
288
- table.find('tbody tr').each(function(i, tr) {
289
- var row = [];
290
- jQuery(tr).find('td').each(function(i, td) {
291
- var text = jQuery(td).text();
292
- row.push(text);
293
- });
294
- data.push(row);
295
- });
296
- var csvContent = "data:text/csv;charset=utf-8,";
297
- data.forEach(function(infoArray, index){
298
- dataString = infoArray.join(",");
299
- csvContent += index < data.length ? dataString+ "\n" : dataString;
300
- });
301
- var encodedUri = encodeURI(csvContent);
302
- var link = document.createElement("a");
303
- link.setAttribute("href", encodedUri);
304
- link.setAttribute("download", "media-file-renamer.csv");
305
- document.body.appendChild(link);
306
- link.click();
307
- }
308
-
309
- </script>
310
- <?php
311
- }
312
-
313
- /**
314
- *
315
- * BULK MEDIA RENAME PAGE
316
- *
317
- */
318
-
319
- function wp_ajax_mfrh_rename_media() {
320
- $subaction = $_POST['subaction'];
321
- if ( $subaction == 'getMediaIds' ) {
322
- $all = intval( $_POST['all'] );
323
- global $wpdb;
324
- $ids = $wpdb->get_col( "SELECT p.ID FROM $wpdb->posts p WHERE post_status = 'inherit' AND post_type = 'attachment'" );
325
- if ( !$all ) {
326
- $idsToRemove = $wpdb->get_col( "SELECT m.post_id FROM $wpdb->postmeta m
327
- WHERE m.meta_key = '_manual_file_renaming' and m.meta_value = 1" );
328
- $ids = array_values( array_diff( $ids, $idsToRemove ) );
329
- }
330
- else {
331
- // We rename all, so we should unlock everything.
332
- $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_manual_file_renaming'" );
333
- }
334
- $reply = array();
335
- $reply['ids'] = $ids;
336
- $reply['total'] = count( $ids );
337
- echo json_encode( $reply );
338
- die;
339
- }
340
- else if ( $subaction == 'renameMediaId' ) {
341
- $id = intval( $_POST['id'] );
342
- $this->rename( $id );
343
- echo 1;
344
- die();
345
- }
346
- echo 0;
347
- die();
348
- }
349
-
350
- function wp_ajax_mfrh_undo_media() {
351
- $subaction = $_POST['subaction'];
352
- if ( $subaction == 'getMediaIds' ) {
353
- global $wpdb;
354
- $ids = $wpdb->get_col( "
355
- SELECT p.ID FROM $wpdb->posts p
356
- WHERE post_status = 'inherit' AND post_type = 'attachment'" );
357
- $reply = array();
358
- $reply['ids'] = $ids;
359
- $reply['total'] = count( $ids );
360
- echo json_encode( $reply );
361
- die;
362
- }
363
- else if ( $subaction == 'undoMediaId' ) {
364
- $id = intval( $_POST['id'] );
365
- $original_filename = get_post_meta( $id, '_original_filename', true );
366
- $this->rename( $id, $original_filename );
367
- echo 1;
368
- die();
369
- }
370
- echo 0;
371
- die();
372
- }
373
-
374
- function admin_menu() {
375
- $method = apply_filters( 'mfrh_method', 'media_title' );
376
- if ( $method != 'none' ) {
377
- add_media_page( 'Media File Renamer', __( 'Renamer', 'media-file-renamer' ), 'manage_options', 'rename_media_files', array( $this, 'rename_media_files' ) );
378
- }
379
- }
380
-
381
  function wpml_media_is_installed() {
382
  return defined( 'WPML_MEDIA_VERSION' );
383
  }
@@ -606,232 +293,6 @@ class Meow_MFRH_Core {
606
  return $issues;
607
  }
608
 
609
- function generate_explanation( $file ) {
610
-
611
- static $previous = array();
612
-
613
- $smallDiv = '<div style="line-height: 12px; font-size: 10px; margin-top: 5px;">';
614
-
615
- if ( $file['post_title'] == "" ) {
616
- echo " <a class='button-primary' href='post.php?post=" . $file['post_id'] . "&action=edit'>" . __( 'Edit Media', 'media-file-renamer' ) . "</a><br /><small>" . __( 'This title cannot be used for a filename.', 'media-file-renamer' ) . "</small>";
617
- }
618
- else if ( $file['desired_filename_exists'] ) {
619
- echo "<a class='button-primary' href='post.php?post=" . $file['post_id'] . "&action=edit'>" . __( 'Edit Media', 'media-file-renamer' ) . "</a><br />$smallDiv" . __( 'The ideal filename already exists. If you would like to use a count and rename it, enable the <b>Numbered Files</b> option in the plugin settings.', 'media-file-renamer' ) . "</div>";
620
- }
621
- else {
622
- $page = isset( $_GET['page'] ) ? ( '&page=' . $_GET['page'] ) : "";
623
- $mfrh_scancheck = ( isset( $_GET ) && isset( $_GET['mfrh_scancheck'] ) ) ? '&mfrh_scancheck' : '';
624
- $mfrh_to_rename = ( !empty( $_GET['to_rename'] ) && $_GET['to_rename'] == 1 ) ? '&to_rename=1' : '';
625
- $modify_url = "post.php?post=" . $file['post_id'] . "&action=edit";
626
- $page = isset( $_GET['page'] ) ? ( '&page=' . $_GET['page'] ) : "";
627
-
628
- $isNew = true;
629
- if ( in_array( $file['desired_filename'], $previous ) )
630
- $isNew = false;
631
- else
632
- array_push( $previous, $file['desired_filename'] );
633
-
634
- echo "<a class='button-primary' href='?" . $page . $mfrh_scancheck . $mfrh_to_rename . "&mfrh_rename=" . $file['post_id'] . "'>" . __( 'Auto-Rename', 'media-file-renamer' ) . "</a>";
635
- echo "<a title='" . __( 'Click to lock it to manual only.', 'media-file-renamer' ) . "' href='?" . $page . "&mfrh_lock=" . $file['post_id'] . "'><span style='font-size: 16px; margin-top: 5px;' class='dashicons dashicons-unlock'></span></a>";
636
-
637
- if ( $file['case_issue'] ) {
638
- echo '<br />' . $smallDiv .
639
- sprintf( __( 'Rename in lowercase, to %s. You can also <a href="%s">edit this media</a>.', 'media-file-renamer' ),
640
- $file['desired_filename'], $modify_url ) . "</div>";
641
- }
642
- else {
643
- echo '<br />' . $smallDiv .
644
- sprintf( __( 'Rename to %s. You can also <a href="%s">EDIT THIS MEDIA</a>.', 'media-file-renamer' ),
645
- $file['desired_filename'], $modify_url ) . "</div>";
646
- }
647
-
648
- if ( !$isNew ) {
649
- echo $smallDiv . "<i>";
650
- echo __( 'The first media you rename will actually get this filename; the next will be either not renamed or will have a counter appended to it.', 'media-file-renamer' );
651
- echo '</i></div>';
652
- }
653
- }
654
- }
655
-
656
- function rename_media_files() {
657
- $hide_ads = get_option( 'meowapps_hide_ads' );
658
- echo '<div class="wrap">';
659
- echo $this->admin->display_title( "Media File Renamer" );
660
- echo '<p></p>';
661
- global $wpdb;
662
-
663
- if ( isset( $_GET ) && isset( $_GET['mfrh_lockall'] ) ) {
664
- $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_manual_file_renaming'" );
665
- $wpdb->query( "INSERT INTO $wpdb->postmeta (meta_key, meta_value, post_id)
666
- SELECT '_manual_file_renaming', 1, p.ID
667
- FROM $wpdb->posts p WHERE post_status = 'inherit' AND post_type = 'attachment'"
668
- );
669
- echo '<div class="updated"><p>';
670
- echo __( 'All the media files are now locked.', 'media-file-renamer' );
671
- echo '</p></div>';
672
- }
673
-
674
- if ( isset( $_GET ) && isset( $_GET['mfrh_unlockall'] ) ) {
675
- $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_manual_file_renaming'" );
676
- }
677
-
678
- $checkFiles = null;
679
- if ( isset( $_GET ) && isset( $_GET['mfrh_scancheck'] ) )
680
- $checkFiles = $this->check_text();
681
- // FLAGGING
682
- // if ( get_option( 'mfrh_flagging' ) ) {
683
- // $this->file_counter( $flagged, $total, true );
684
- // }
685
- $all_media = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts p WHERE post_status = 'inherit' AND post_type = 'attachment'" );
686
- $manual_media = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->postmeta WHERE meta_key = '_manual_file_renaming' AND meta_value = 1" );
687
- ?>
688
-
689
- <?php
690
- if ( !$this->admin->is_registered() ) {
691
- echo '<div class="updated"><p>';
692
- echo __( '<b>The Pro version</b> of the plugin allows you to <b>rename based on the title of the post</b> (product or whatever else) you media is attached to, <b>rename manually</b>, use <b>numbered files</b> (by adding a counter if the filenames are similar), <b>sync the title with your ALT text</b>, UTF8 support (if you need it), a force rename (to repair a broken install), and, more importantly, <b>supports the developer</b> :) The serial key for the Pro has to be inserted in your Meow Apps > File Renamer > Pro. Thank you :)<br /><br /><a class="button-primary" href="http://meowapps.com/media-file-renamer/" target="_blank">Get the serial key for the Pro</a>', 'media-file-renamer' );
693
- echo '</p></div>';
694
- }
695
- ?>
696
-
697
- <h2>Rename in Bulk</h2>
698
-
699
- <?php if ( get_option( 'mfrh_flagging' ) ): ?>
700
- <p>
701
- <b>There are <span class='mfrh-flagged' style='color: red;'><?php _e( $flagged ); ?></span> media files flagged for auto-renaming out of <?php _e( $total ); ?> in total.</b> Those are the files that couldn't be renamed on the fly when their names were updated. You can now rename those flagged media, or rename all of them (which will unlock them all and force their renaming). <span style='color: red; font-weight: bold;'>Please backup your uploads folder + DB before using this.</span> If you don't know how, give a try to this: <a href='https://updraftplus.com/?afref=460' target='_blank'>UpdraftPlus</a>.
702
- </p>
703
- <?php else: ?>
704
- <p>
705
- You might have noticed that some of your media are locked by the file renamer, others are unlocked. Automatically, the plugin locks the media you renamed manually. By default, they are unlocked. Here, you have the choice of rename all the media in your DB or only the ones which are unlocked (to keep the files you renamed manually). <span style='color: red; font-weight: bold;'>Please backup your uploads folder + DB before using this.</span> If you don't know how, give a try to this: <a href='https://updraftplus.com/?afref=460' target='_blank'>UpdraftPlus</a>.
706
- </p>
707
- <?php endif; ?>
708
-
709
- <div style='margin-top: 12px; background: #FFF; padding: 5px; border-radius: 4px; height: 28px; box-shadow: 0px 0px 6px #C2C2C2;'>
710
-
711
- <a onclick='mfrh_rename_media(false)' id='mfrh_rename_all_images' class='button-primary'
712
- style='margin-right: 0px;'><span class="dashicons dashicons-controls-play" style="position: relative; top: 3px; left: -2px;"></span>
713
- <?php echo sprintf( __( "Rename ALL [%d]", 'media-file-renamer' ), $all_media - $manual_media ); ?>
714
- </a>
715
- <a onclick='mfrh_rename_media(true)' id='mfrh_unlock_rename_all_images' class='button-primary'
716
- style='margin-right: 0px;'><span class="dashicons dashicons-controls-play" style="position: relative; top: 3px; left: -2px;"></span>
717
- <?php echo sprintf( __( "Unlock ALL & Rename [%d]", 'media-file-renamer' ), $all_media ); ?>
718
- </a>
719
- <span style='margin-right: 5px; margin-left: 5px;'>|</span>
720
- <a href="?page=rename_media_files&mfrh_lockall" id='mfrh_lock_all_images' class='button-primary'
721
- style='margin-right: 0px;'><span class="dashicons dashicons-controls-play" style="position: relative; top: 3px; left: -2px;"></span>
722
- <?php echo sprintf( __( "Lock ALL [%d]", 'media-file-renamer' ), $all_media ); ?>
723
- </a>
724
- <a href="?page=rename_media_files&mfrh_unlockall" id='mfrh_unblock_all_images' class='button-primary'
725
- style='margin-right: 0px;'><span class="dashicons dashicons-controls-play" style="position: relative; top: 3px; left: -2px;"></span>
726
- <?php echo sprintf( __( "Unlock ALL [%d]", 'media-file-renamer' ), $all_media ); ?>
727
- </a>
728
- <a onclick='mfrh_undo_media()' id='mfrh_undo_all_images' class='button button-red'
729
- style='margin-right: 0px; float: right;'><span class="dashicons dashicons-undo" style="position: relative; top: 3px; left: -2px;"></span>
730
- <?php echo sprintf( __( "Undo ALL [%d]", 'media-file-renamer' ), $all_media ); ?>
731
- </a>
732
- <span id='mfrh_progression'></span>
733
-
734
- <?php if ( get_option( 'mfrh_flagging' ) ): ?>
735
- <?php if ($flagged > 0): ?>
736
- <a onclick='mfrh_rename_media(false)' id='mfrh_rename_dued_images' class='button-primary'>
737
- <?php echo sprintf( __( "Rename <span class='mfrh-flagged'>%d</span> flagged media", 'media-file-renamer' ), $flagged ); ?>
738
- </a>
739
- <?php else: ?>
740
- <a id='mfrh_rename_dued_images' class='button-primary'>
741
- <?php echo sprintf( __( "Rename <span class='mfrh-flagged'>%d</span> flagged media", 'media-file-renamer' ), $flagged ); ?>
742
- </a>
743
- <?php endif; ?>
744
- <?php endif; ?>
745
-
746
- </div>
747
-
748
- <h2>Rename 1 by 1</h2>
749
- <p>If you want to rename the media this way, I recommend you to do it from the Media Library directly. If you think this "Scan All" is really handy, please tell me that you are using it on the forums. I am currently planning to remove it and moving the "Rename in Bulk" with the settings of File Renamer (to clean the WordPress UI).</p>
750
- <table class='wp-list-table widefat fixed media' style='margin-top: 15px;'>
751
- <thead>
752
- <tr><th><?php _e( 'Title', 'media-file-renamer' ); ?></th><th><?php _e( 'Current Filename', 'media-file-renamer' ); ?></th><th><?php _e( 'Desired Filename', 'media-file-renamer' ); ?></th><th><?php _e( 'Action', 'media-file-renamer' ); ?></th></tr>
753
- </thead>
754
- <tfoot>
755
- <tr><th><?php _e( 'Title', 'media-file-renamer' ); ?></th><th><?php _e( 'Current Filename', 'media-file-renamer' ); ?></th><th><?php _e( 'Desired Filename', 'media-file-renamer' ); ?></th><th><?php _e( 'Action', 'media-file-renamer' ); ?></th></tr>
756
- </tfoot>
757
- <tbody>
758
- <?php
759
- if ( $checkFiles != null ) {
760
- foreach ( $checkFiles as $file ) {
761
- echo "<tr><td><a href='post.php?post=" . $file['post_id'] . "&action=edit'>" . ( $file['post_title'] == "" ? "(no title)" : $file['post_title'] ) . "</a></td>"
762
- . "<td>" . $file['current_filename'] . "</td>"
763
- . "<td>" . $file['desired_filename'] . "</td>";
764
- echo "<td>";
765
- $this->generate_explanation( $file );
766
- echo "</td></tr>";
767
- }
768
- }
769
- else if ( isset( $_GET['mfrh_scancheck'] ) && ( $checkFiles == null || count( $checkFiles ) < 1 ) ) {
770
- ?><tr><td colspan='4'><div style='width: 100%; margin-top: 15px; margin-bottom: 15px; text-align: center;'>
771
- <div style='margin-top: 15px;'><?php _e( 'There are no issues. Cool!<br />Let\'s go visit <a target="_blank" href=\'http://offbeatjapan.org\'>The Offbeat Guide of Japan</a> :)', 'media-file-renamer' ); ?></div>
772
- </div></td><?php
773
- }
774
- else if ( $checkFiles == null ) {
775
- ?><tr><td colspan='4'><div style='width: 100%; text-align: center;'>
776
- <a class='button-primary' href="?page=rename_media_files&mfrh_scancheck" style='margin-top: 15px; margin-bottom: 15px;'><span class="dashicons dashicons-admin-generic" style="position: relative; top: 3px; left: -2px;"></span>
777
- <?php _e( "Scan All & Show Issues", 'media-file-renamer' ); ?>
778
- </a>
779
- </div></td><?php
780
- }
781
- ?>
782
- </tbody>
783
- </table>
784
-
785
- <h2>Before / After</h2>
786
- <p>This is useful if you wish to create redirections from your old filenames to your new ones. The CSV file generated by Media File Renamer is compatible with the import function of the <a href="https://wordpress.org/plugins/redirection/" target="_blank">Redirection</a> plugin. The redirections with slugs are already automatically and natively handled by WordPress.</p>
787
-
788
- <div style='margin-top: 12px; background: #FFF; padding: 5px; border-radius: 4px; height: 28px; box-shadow: 0px 0px 6px #C2C2C2;'>
789
-
790
- <a href="?page=rename_media_files&mfrh_beforeafter_filenames" class='button-primary' style='margin-right: 0px;'>
791
- <span class="dashicons dashicons-media-spreadsheet" style="position: relative; top: 3px; left: -2px;"></span>
792
- <?php echo _e( "Display Filenames", 'media-file-renamer' ); ?>
793
- </a>
794
-
795
- <a onclick="mfrh_export_table('#mfrh-before-after')" class='button-primary' style='margin-right: 0px; float: right;'>
796
- <span class="dashicons dashicons-arrow-down-alt" style="position: relative; top: 3px; left: -2px;"></span>
797
- <?php echo _e( "Export as CSV", 'media-file-renamer' ); ?>
798
- </a>
799
-
800
- </div>
801
-
802
- <table id='mfrh-before-after' class='wp-list-table widefat fixed media' style='margin-top: 15px;'>
803
- <thead>
804
- <tr><th><?php _e( 'Before', 'media-file-renamer' ); ?></th><th><?php _e( 'After', 'media-file-renamer' ); ?></th></tr>
805
- </thead>
806
- <tfoot>
807
- <tr><th><?php _e( 'Before', 'media-file-renamer' ); ?></th><th><?php _e( 'After', 'media-file-renamer' ); ?></th></tr>
808
- </tfoot>
809
- <tbody>
810
- <?php
811
- if ( isset( $_GET['mfrh_beforeafter_filenames'] ) || isset( $_GET['mfrh_beforeafter_slugs'] ) ) {
812
- global $wpdb;
813
- $results = $wpdb->get_results( "
814
- SELECT m.post_id as ID, m.meta_value as original_filename, m2.meta_value as current_filename
815
- FROM {$wpdb->postmeta} m
816
- JOIN {$wpdb->postmeta} m2 on m2.post_id = m.post_id AND m2.meta_key = '_wp_attached_file'
817
- WHERE m.meta_key = '_original_filename'" );
818
- foreach ( $results as $row ) {
819
- $fullsize_path = wp_get_attachment_url( $row->ID );
820
- $parts = pathinfo( $fullsize_path );
821
- $shorten_url = trailingslashit( $parts['dirname'] ) . $row->original_filename;
822
- if ( isset( $_GET['mfrh_beforeafter_filenames'] ) )
823
- echo "<tr><td>{$shorten_url}</td><td>$fullsize_path</td></tr>";
824
- else
825
- echo "<tr><td>{$row->original_slug}</td><td>{$row->current_slug}</td></tr>";
826
- }
827
- }
828
- ?>
829
- </tbody>
830
- </table>
831
-
832
- <?php
833
- }
834
-
835
  /**
836
  *
837
  * RENAME ON SAVE / PUBLISH
@@ -870,25 +331,6 @@ class Meow_MFRH_Core {
870
  return $post;
871
  }
872
 
873
- function media_send_to_editor( $html, $id, $attachment ) {
874
- $this->check_attachment( get_post( $id, ARRAY_A ), $output );
875
- return $html;
876
- }
877
-
878
- function add_rename_metabox() {
879
- add_meta_box( 'mfrh_media', 'Filename', array( $this, 'attachment_fields' ), 'attachment', 'side', 'high' );
880
- }
881
-
882
- function attachment_fields( $post ) {
883
- $info = pathinfo( get_attached_file( $post->ID ) );
884
- $basename = $info['basename'];
885
- $is_manual = apply_filters( 'mfrh_manual', false );
886
- $html = '<input type="text" readonly class="widefat" name="mfrh_new_filename" value="' . $basename. '" />';
887
- $html .= '<p class="description">This feature is for <a target="_blank" href="http://meowapps.com/media-file-renamer/">Pro users</a> only.</p>';
888
- echo apply_filters( "admin_attachment_fields", $html, $post );
889
- return $post;
890
- }
891
-
892
  function log_sql( $data, $antidata ) {
893
  if ( !get_option( 'mfrh_logsql' ) || !$this->admin->is_registered() )
894
  return;
@@ -920,16 +362,16 @@ class Meow_MFRH_Core {
920
  */
921
 
922
  static function replace_special_chars( $str ) {
923
- $special_chars = array(
924
- "å" => "a", "Å" => "a",
925
- "ä" => "ae", "Ä" => "ae",
926
- "ö" => "oe", "Ö" => "oe",
927
- "ü" => "ue", "Ü" => "ue",
928
- "ß" => "ss", "ẞ" => "ss"
929
- );
930
- foreach ( $special_chars as $key => $value )
931
- $str = str_replace( $key, $value, $str );
932
- return $str;
933
  }
934
 
935
  function replace_chars( $str ) {
@@ -980,8 +422,12 @@ class Meow_MFRH_Core {
980
  $utf8_filename = apply_filters( 'mfrh_utf8', false );
981
  if ( $utf8_filename )
982
  $new_filename = sanitize_file_name( $text );
983
- else
984
- $new_filename = str_replace( "%", "-", sanitize_title( Meow_MFRH_Core::replace_special_chars( $text ) ) );
 
 
 
 
985
  }
986
  if ( empty( $new_filename ) )
987
  $new_filename = "empty";
@@ -1038,6 +484,147 @@ class Meow_MFRH_Core {
1038
  return true;
1039
  }
1040
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1041
  function rename( $media, $manual_filename = null, $fromMediaLibrary = true ) {
1042
  $id = null;
1043
  $post = null;
@@ -1253,4 +840,31 @@ class Meow_MFRH_Core {
1253
  do_action( 'mfrh_media_renamed', $post, $old_filepath, $new_filepath );
1254
  return $post;
1255
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1256
  }
17
 
18
  // Those actions/filters are only for the admin screens
19
  if ( is_admin() ) {
 
 
 
 
 
 
 
 
 
20
  add_filter( 'attachment_fields_to_save', array( $this, 'attachment_fields_to_save' ), 20, 2 );
21
  add_action( 'save_post', array( $this, 'save_post' ) );
22
 
23
  if ( get_option( 'mfrh_on_upload', false ) ) {
24
  add_filter( 'wp_handle_upload_prefilter', array( $this, 'wp_handle_upload_prefilter' ), 10, 2 );
25
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  }
27
  }
28
 
65
  rmdir( $directory );
66
  }
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  function wpml_media_is_installed() {
69
  return defined( 'WPML_MEDIA_VERSION' );
70
  }
293
  return $issues;
294
  }
295
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296
  /**
297
  *
298
  * RENAME ON SAVE / PUBLISH
331
  return $post;
332
  }
333
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
334
  function log_sql( $data, $antidata ) {
335
  if ( !get_option( 'mfrh_logsql' ) || !$this->admin->is_registered() )
336
  return;
362
  */
363
 
364
  static function replace_special_chars( $str ) {
365
+ $special_chars = array(
366
+ "å" => "a", "Å" => "a",
367
+ "ä" => "ae", "Ä" => "ae",
368
+ "ö" => "oe", "Ö" => "oe",
369
+ "ü" => "ue", "Ü" => "ue",
370
+ "ß" => "ss", "ẞ" => "ss"
371
+ );
372
+ foreach ( $special_chars as $key => $value )
373
+ $str = str_replace( $key, $value, $str );
374
+ return $str;
375
  }
376
 
377
  function replace_chars( $str ) {
422
  $utf8_filename = apply_filters( 'mfrh_utf8', false );
423
  if ( $utf8_filename )
424
  $new_filename = sanitize_file_name( $text );
425
+ else {
426
+ // Remove non-ASCII characters
427
+ $text = Meow_MFRH_Core::replace_special_chars( $text );
428
+ $text = preg_replace( '/[[:^print:]]/', '', $text );
429
+ $new_filename = str_replace( "%", "-", sanitize_title( $text ) );
430
+ }
431
  }
432
  if ( empty( $new_filename ) )
433
  $new_filename = "empty";
484
  return true;
485
  }
486
 
487
+ function move( $media, $newPath ) {
488
+ $id = null;
489
+ $post = null;
490
+
491
+ // Check the arguments
492
+ if ( is_numeric( $media ) ) {
493
+ $id = $media;
494
+ $post = get_post( $media, ARRAY_A );
495
+ }
496
+ else if ( is_array( $media ) ) {
497
+ $id = $media['ID'];
498
+ $post = $media;
499
+ }
500
+ else {
501
+ die( 'Media File Renamer: move() requires the ID or the array for the media.' );
502
+ }
503
+
504
+ // Prepare the variables
505
+ $old_filepath = get_attached_file( $id );
506
+ $path_parts = pathinfo( $old_filepath );
507
+ $old_ext = $path_parts['extension'];
508
+ $upload_dir = wp_upload_dir();
509
+ $old_directory = trim( str_replace( $upload_dir['basedir'], '', $path_parts['dirname'] ), '/' ); // '2011/01'
510
+ $new_directory = trim( $newPath, '/' );
511
+ $filename = $path_parts['basename']; // 'whatever.jpeg'
512
+ $new_filepath = trailingslashit( trailingslashit( $upload_dir['basedir'] ) . $new_directory ) . $filename;
513
+
514
+ $this->log( "** Move Media: " . $filename );
515
+ $this->log( "The new directory will be: " . dirname( $new_filepath ) );
516
+
517
+ // Create the directory if it does not exist
518
+ if ( !file_exists( dirname( $new_filepath ) ) ) {
519
+ mkdir( dirname( $new_filepath ), 0777, true );
520
+ }
521
+
522
+ // There is no support for UNDO (as the current process of Media File Renamer doesn't keep the path for the undo, only the filename... so the move breaks this - let's deal with this later).
523
+
524
+ // Move the main media file
525
+ if ( !$this->rename_file( $old_filepath, $new_filepath ) ) {
526
+ $this->log( "[!] File\t$old_filepath -> $new_filepath" );
527
+ return false;
528
+ }
529
+ $this->log( "File\t$old_filepath -> $new_filepath" );
530
+ do_action( 'mfrh_path_renamed', $post, $old_filepath, $new_filepath );
531
+
532
+ // Update the attachment meta
533
+ $meta = wp_get_attachment_metadata( $id );
534
+
535
+ if ( $meta ) {
536
+ if ( isset( $meta['file'] ) && !empty( $meta['file'] ) )
537
+ $meta['file'] = $this->str_replace( $old_directory, $new_directory, $meta['file'] );
538
+ if ( isset( $meta['url'] ) && !empty( $meta['url'] ) && count( $meta['url'] ) > 4 )
539
+ $meta['url'] = $this->str_replace( $old_directory, $new_directory, $meta['url'] );
540
+ }
541
+
542
+ // Better to check like this rather than with wp_attachment_is_image
543
+ // PDFs also have thumbnails now, since WP 4.7
544
+ $has_thumbnails = isset( $meta['sizes'] );
545
+
546
+ if ( $has_thumbnails ) {
547
+ $orig_image_urls = array();
548
+ $orig_image_data = wp_get_attachment_image_src( $id, 'full' );
549
+ $orig_image_urls['full'] = $orig_image_data[0];
550
+ foreach ( $meta['sizes'] as $size => $meta_size ) {
551
+ if ( !isset($meta['sizes'][$size]['file'] ) )
552
+ continue;
553
+ $meta_old_filename = $meta['sizes'][$size]['file'];
554
+ $meta_old_filepath = trailingslashit( $upload_dir['basedir'] ) . trailingslashit( $old_directory ) . $meta_old_filename;
555
+ $meta_new_filepath = trailingslashit( $upload_dir['basedir'] ) . trailingslashit( $new_directory ) . $meta_old_filename;
556
+ $orig_image_data = wp_get_attachment_image_src( $id, $size );
557
+ $orig_image_urls[$size] = $orig_image_data[0];
558
+
559
+ // Double check files exist before trying to rename.
560
+ if ( file_exists( $meta_old_filepath )
561
+ && ( ( !file_exists( $meta_new_filepath ) ) || is_writable( $meta_new_filepath ) ) ) {
562
+ // WP Retina 2x is detected, let's rename those files as well
563
+ if ( function_exists( 'wr2x_get_retina' ) ) {
564
+ $wr2x_old_filepath = $this->str_replace( '.' . $old_ext, '@2x.' . $old_ext, $meta_old_filepath );
565
+ $wr2x_new_filepath = $this->str_replace( '.' . $old_ext, '@2x.' . $old_ext, $meta_new_filepath );
566
+ if ( file_exists( $wr2x_old_filepath )
567
+ && ( ( !file_exists( $wr2x_new_filepath ) ) || is_writable( $wr2x_new_filepath ) ) ) {
568
+
569
+ // Rename retina file
570
+ if ( !$this->rename_file( $wr2x_old_filepath, $wr2x_new_filepath, $case_issue ) && !$force_rename ) {
571
+ $this->log( "[!] Retina $wr2x_old_filepath -> $wr2x_new_filepath" );
572
+ return $post;
573
+ }
574
+ $this->log( "Retina\t$wr2x_old_filepath -> $wr2x_new_filepath" );
575
+ do_action( 'mfrh_path_renamed', $post, $wr2x_old_filepath, $wr2x_new_filepath );
576
+ }
577
+ }
578
+
579
+ // Rename meta file
580
+ if ( !$this->rename_file( $meta_old_filepath, $meta_new_filepath ) ) {
581
+ $this->log( "[!] File $meta_old_filepath -> $meta_new_filepath" );
582
+ return false;
583
+ }
584
+
585
+ // Success, call other plugins
586
+ $this->log( "File\t$meta_old_filepath -> $meta_new_filepath" );
587
+ do_action( 'mfrh_path_renamed', $post, $meta_old_filepath, $meta_new_filepath );
588
+
589
+ }
590
+ }
591
+ }
592
+ else {
593
+ $orig_attachment_url = wp_get_attachment_url( $id );
594
+ }
595
+
596
+ // Update metadata
597
+ if ( $meta )
598
+ wp_update_attachment_metadata( $id, $meta );
599
+ update_attached_file( $id, $new_filepath );
600
+
601
+ // I wonder about cleaning the cache for this media. It might have no impact, and will not reset the cache for the posts using this media anyway, and it adds processing time. I keep it for now, but there might be something better to do.
602
+ clean_post_cache( $id );
603
+
604
+ // Call the actions so that the plugin's plugins can update everything else (than the files)
605
+ if ( $has_thumbnails ) {
606
+ $orig_image_url = $orig_image_urls['full'];
607
+ $new_image_data = wp_get_attachment_image_src( $id, 'full' );
608
+ $new_image_url = $new_image_data[0];
609
+ $this->call_hooks_rename_url( $post, $orig_image_url, $new_image_url );
610
+ if ( !empty( $meta['sizes'] ) ) {
611
+ foreach ( $meta['sizes'] as $size => $meta_size ) {
612
+ $orig_image_url = $orig_image_urls[$size];
613
+ $new_image_data = wp_get_attachment_image_src( $id, $size );
614
+ $new_image_url = $new_image_data[0];
615
+ $this->call_hooks_rename_url( $post, $orig_image_url, $new_image_url );
616
+ }
617
+ }
618
+ }
619
+ else {
620
+ $new_attachment_url = wp_get_attachment_url( $id );
621
+ $this->call_hooks_rename_url( $post, $orig_attachment_url, $new_attachment_url );
622
+ }
623
+
624
+ do_action( 'mfrh_media_renamed', $post, $old_filepath, $new_filepath );
625
+ return true;
626
+ }
627
+
628
  function rename( $media, $manual_filename = null, $fromMediaLibrary = true ) {
629
  $id = null;
630
  $post = null;
840
  do_action( 'mfrh_media_renamed', $post, $old_filepath, $new_filepath );
841
  return $post;
842
  }
843
+
844
+ /**
845
+ * Locks a post to be manual-rename only
846
+ * @param int|WP_Post $post The post to lock
847
+ * @return True on success, false on failure
848
+ */
849
+ function lock( $post ) {
850
+ return !!add_post_meta( $post instanceof WP_Post ? $post->ID : $post, '_manual_file_renaming', true, true );
851
+ }
852
+
853
+ /**
854
+ * Unlocks a locked post
855
+ * @param int|WP_Post $post The post to unlock
856
+ * @return True on success, false on failure
857
+ */
858
+ function unlock( $post ) {
859
+ return delete_post_meta( $post instanceof WP_Post ? $post->ID : $post, '_manual_file_renaming' );
860
+ }
861
+
862
+ /**
863
+ * Determines whether a post is locked
864
+ * @param int|WP_Post $post The post to check
865
+ * @return Boolean
866
+ */
867
+ function is_locked( $post ) {
868
+ return get_post_meta( $post instanceof WP_Post ? $post->ID : $post, '_manual_file_renaming', true ) === true;
869
+ }
870
  }
media-file-renamer.php CHANGED
@@ -1,9 +1,9 @@
1
  <?php
2
  /*
3
- Plugin Name: Media File Renamer
4
  Plugin URI: http://meowapps.com
5
  Description: Auto-rename the files when titles are modified and update and the references (links). Manual Rename is a Pro option. Please read the description.
6
- Version: 4.0.4
7
  Author: Jordy Meow
8
  Author URI: http://meowapps.com
9
  Text Domain: media-file-renamer
@@ -29,7 +29,7 @@ if ( class_exists( 'Meow_MFRH_Core' ) ) {
29
  if ( is_admin() ) {
30
 
31
  global $mfrh_version, $mfrh_core;
32
- $mfrh_version = '4.0.4';
33
 
34
  // Admin
35
  require( 'mfrh_admin.php');
1
  <?php
2
  /*
3
+ Plugin Name: Media File Auto Renamer
4
  Plugin URI: http://meowapps.com
5
  Description: Auto-rename the files when titles are modified and update and the references (links). Manual Rename is a Pro option. Please read the description.
6
+ Version: 4.2.0
7
  Author: Jordy Meow
8
  Author URI: http://meowapps.com
9
  Text Domain: media-file-renamer
29
  if ( is_admin() ) {
30
 
31
  global $mfrh_version, $mfrh_core;
32
+ $mfrh_version = '4.2.0';
33
 
34
  // Admin
35
  require( 'mfrh_admin.php');
mfrh_admin.php CHANGED
@@ -253,7 +253,7 @@ class Meow_MFRH_Admin extends MeowApps_Admin {
253
  function admin_manual_rename_callback( $args ) {
254
  $html = '<input ' . disabled( $this->is_registered(), false, false ) . ' type="checkbox" id="mfrh_manual_rename" name="mfrh_manual_rename" value="1" ' .
255
  checked( 1, apply_filters( 'mfrh_manual', false ), false ) . '/>';
256
- $html .= '<label>Enable</label><br /><small>Manual field will be enabled in the Media Edit screen.</small>';
257
  echo $html;
258
  }
259
 
253
  function admin_manual_rename_callback( $args ) {
254
  $html = '<input ' . disabled( $this->is_registered(), false, false ) . ' type="checkbox" id="mfrh_manual_rename" name="mfrh_manual_rename" value="1" ' .
255
  checked( 1, apply_filters( 'mfrh_manual', false ), false ) . '/>';
256
+ $html .= '<label>Enable</label><br /><small>Manual field will be enabled in the Media Library and the Media Edit Screen.</small>';
257
  echo $html;
258
  }
259
 
plugins/custom.php CHANGED
@@ -2,7 +2,7 @@
2
 
3
  // EXAMPLES TO USE ACTIONS AND FILTERS
4
  // For help about this file, check:
5
- // http://meowapps.com/media-file-renamer/faq/
6
 
7
  // HANDLE THE RENAMING
8
  // $new is the proposed filename by Media File Renamer (without extension)
@@ -31,7 +31,8 @@
31
  // function url_of_media_was_modified( $post, $orig_image_url, $new_image_url ) {
32
  // global $wpdb;
33
  // $query = $wpdb->prepare( "UPDATE $wpdb->postmeta SET meta_value = REPLACE(meta_value, '%s', '%s');", $orig_image_url, $new_image_url );
34
- // $query_revert = $wpdb->prepare( "UPDATE $wpdb->postmeta SET meta_value = REPLACE(meta_value, '%s', '%s');", $new_image_url, $orig_image_url );
 
35
  // $wpdb->query( $query );
36
  // $this->log_sql( $query, $query_revert );
37
  // $this->log( "Metadata like $orig_image_url were replaced by $new_image_url." );
@@ -46,4 +47,27 @@
46
  // }
47
  // =============================================================================
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  ?>
2
 
3
  // EXAMPLES TO USE ACTIONS AND FILTERS
4
  // For help about this file, check:
5
+ // https://meowapps.com/media-file-renamer/faq/
6
 
7
  // HANDLE THE RENAMING
8
  // $new is the proposed filename by Media File Renamer (without extension)
31
  // function url_of_media_was_modified( $post, $orig_image_url, $new_image_url ) {
32
  // global $wpdb;
33
  // $query = $wpdb->prepare( "UPDATE $wpdb->postmeta SET meta_value = REPLACE(meta_value, '%s', '%s');", $orig_image_url, $new_image_url );
34
+ // $query_revert = $wpdb->prepare( "UPDATE $wpdb->postmeta
35
+ // SET meta_value = REPLACE(meta_value, '%s', '%s');", $new_image_url, $orig_image_url );
36
  // $wpdb->query( $query );
37
  // $this->log_sql( $query, $query_revert );
38
  // $this->log( "Metadata like $orig_image_url were replaced by $new_image_url." );
47
  // }
48
  // =============================================================================
49
 
50
+ // RENAME THE IMAGES IN A WOOCOMMERCE PRODUCT GALLERY EVERY TIME THE PRODUCT IS MODIFIED
51
+ // =============================================================================
52
+ // add_action( 'woocommerce_update_product', 'woocommerce_product_was_modified', 10, 1 );
53
+
54
+ // function woocommerce_product_was_modified( $productId ) {
55
+ // $wcProduct = new WC_Product( $productId );
56
+ // $images = array();
57
+ // if ( ( $mainImageId = intval( $wcProduct->get_image_id() ) ) !== 0 ) {
58
+ // $images[] = $mainImageId;
59
+ // }
60
+ // $galleryImages = $wcProduct->get_gallery_image_ids();
61
+ // foreach ( $galleryImages as $imageId ) {
62
+ // $images[] = intval( $imageId );
63
+ // }
64
+ // foreach ( $images as $imageId ) {
65
+ // if ( $imageId != 0 ) {
66
+ // mfrh_rename( $imageId );
67
+ // }
68
+ // }
69
+ // return true;
70
+ // }
71
+ // =============================================================================
72
+
73
  ?>
readme.txt CHANGED
@@ -1,15 +1,15 @@
1
- === Media File Renamer ===
2
  Contributors: TigrouMeow
3
  Tags: rename, file, files, media, manager, image, renamer, wpml, optimization, seo, retina, gutenberg
4
  Requires at least: 4.6
5
- Tested up to: 4.9.4
6
- Stable tag: 4.0.4
7
 
8
  Automatically rename files depending on Media titles dynamically + update links. Pro version has many more options. Check the description :)
9
 
10
  == Description ==
11
 
12
- The Media File Renamer is a WordPress plugin that physically renames media files nicely for a cleaner system and for a better SEO. Please read this description.
13
 
14
  **IMPORTANT**. This is originally an *automatic* renamer based on the Media title. This plugin features were all meant to be automatic depending on the title of the Media. Manual Rename (and a few more features) were added two years later, in a Pro version. I add complex features based on requests usually in the Pro to be able to maintain the quality of the plugin and its support.
15
 
@@ -47,6 +47,11 @@ Check the FAQ on the official website, [here](https://meowapps.com/media-file-re
47
 
48
  == Changelog ==
49
 
 
 
 
 
 
50
  = 4.0.4 =
51
  * Fix: Renaming using filters (work in progress).
52
  * Fix: Insensitive-case match was giving the wrong file in some cases (webp for instance).
1
+ === Media File Auto Renamer ===
2
  Contributors: TigrouMeow
3
  Tags: rename, file, files, media, manager, image, renamer, wpml, optimization, seo, retina, gutenberg
4
  Requires at least: 4.6
5
+ Tested up to: 4.9.6
6
+ Stable tag: 4.2.0
7
 
8
  Automatically rename files depending on Media titles dynamically + update links. Pro version has many more options. Check the description :)
9
 
10
  == Description ==
11
 
12
+ The Media File Auto Renamer is a WordPress plugin that physically renames media files nicely for a cleaner system and for a better SEO. Please read this description.
13
 
14
  **IMPORTANT**. This is originally an *automatic* renamer based on the Media title. This plugin features were all meant to be automatic depending on the title of the Media. Manual Rename (and a few more features) were added two years later, in a Pro version. I add complex features based on requests usually in the Pro to be able to maintain the quality of the plugin and its support.
15
 
47
 
48
  == Changelog ==
49
 
50
+ = 4.2.0 =
51
+ * Add: All the actions in the Media Library are now asynchronous. No more page reload!
52
+ * Update: Many changes and little enhancements in the code, for speed, security and code-tidyness.
53
+ * Note: If you like it, please review the plugin here: https://wordpress.org/support/plugin/media-file-renamer/reviews/?rate=5#new-post. It's important for us :) Thank you!
54
+
55
  = 4.0.4 =
56
  * Fix: Renaming using filters (work in progress).
57
  * Fix: Insensitive-case match was giving the wrong file in some cases (webp for instance).
scripts/media-library.js ADDED
@@ -0,0 +1,254 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * The script for Media File Renamer in Media Library
3
+ */
4
+
5
+ /**
6
+ * Quick Renamer
7
+ */
8
+ (function($) {
9
+ var col;
10
+
11
+ /**
12
+ * Initializes the entire part of the UI
13
+ */
14
+ $(document).ready(function($) {
15
+ init('.mfrh_column');
16
+ });
17
+
18
+ /**
19
+ * Initializes the specified columns
20
+ * @param {string|DOMElement|jQuery} cols - Columns to initialize
21
+ */
22
+ function init(cols) {
23
+ cols = $(cols);
24
+ cols.each(function() {
25
+ var col = $(this);
26
+ var id = col.find('input[name="id"]').val();
27
+
28
+ // Quick Renamer
29
+ (function() {
30
+ var wrap = col;
31
+ var button = wrap.find('.button.rename');
32
+ var input = wrap.find('input[name="filename"]');
33
+
34
+ button.on('click', function(ev) {
35
+ ev.preventDefault();
36
+ if (isBusy(col)) return;
37
+ goBusy(col);
38
+ button.addClass('updating-message');
39
+ rename(col, input.val());
40
+ });
41
+ input.on('keydown', function(ev) {
42
+ if (ev.which != 13) return; // 13 = Enter
43
+ ev.preventDefault();
44
+ button.trigger('click');
45
+ });
46
+ input.on('change keyup', function(ev) {
47
+ if (input.val() == input.attr('data-origin')) input.removeClass('changed');
48
+ else input.addClass('changed');
49
+ });
50
+ })();
51
+
52
+ // Quick Auto Renamer
53
+ (function() {
54
+ var wrap = col;
55
+ var button = wrap.find('.button.auto-rename');
56
+ var input = wrap.find('input[name="filename"]');
57
+
58
+ button.on('mouseover', function(ev) {
59
+ if (isBusy(col)) return;
60
+ input.addClass('hidden');
61
+ });
62
+ button.on('mouseout', function(ev) {
63
+ if (isBusy(col)) return;
64
+ input.removeClass('hidden');
65
+ });
66
+ button.on('click', function(ev) {
67
+ ev.preventDefault();
68
+ if (isBusy(col)) return;
69
+ goBusy(col);
70
+ button.addClass('updating-message');
71
+ rename(col, null);
72
+ });
73
+ })();
74
+
75
+ // Quick Undo
76
+ (function() {
77
+ var wrap = col;
78
+ var button = wrap.find('.undo');
79
+
80
+ button.on('click', function(ev) {
81
+ ev.preventDefault();
82
+ if (isBusy(col)) return;
83
+ goBusy(col);
84
+
85
+ $.ajax(ajaxurl, {
86
+ type: 'POST',
87
+ dataType: 'json',
88
+ data: {
89
+ action: 'mfrh_undo_media',
90
+ subaction: 'undoMediaId',
91
+ id: id
92
+ }
93
+
94
+ }).done(function(result) {
95
+ if (result.success === false) { // Rejected
96
+ alert(result.data);
97
+ return;
98
+ }
99
+ update(col, result.data);
100
+ });
101
+ });
102
+ })();
103
+
104
+ // Quick Lock
105
+ (function() {
106
+ var wrap = col;
107
+ var button = wrap.find('.lock');
108
+
109
+ button.on('click', function(ev) {
110
+ ev.preventDefault();
111
+ if (isBusy(col)) return;
112
+ goBusy(col);
113
+
114
+ $.ajax(ajaxurl, {
115
+ type: 'POST',
116
+ dataType: 'json',
117
+ data: {
118
+ action: 'mfrh_lock_media',
119
+ id: id
120
+ }
121
+
122
+ }).done(function(result) {
123
+ if (result.success === false) { // Rejected
124
+ alert(result.data);
125
+ return;
126
+ }
127
+ update(col, null);
128
+ });
129
+ });
130
+ })();
131
+
132
+ // Quick Unlock
133
+ (function() {
134
+ var wrap = col;
135
+ var button = wrap.find('.unlock');
136
+
137
+ button.on('click', function(ev) {
138
+ ev.preventDefault();
139
+ if (isBusy(col)) return;
140
+ goBusy(col);
141
+
142
+ $.ajax(ajaxurl, {
143
+ type: 'POST',
144
+ dataType: 'json',
145
+ data: {
146
+ action: 'mfrh_lock_media',
147
+ subaction: 'unlock',
148
+ id: id
149
+ }
150
+
151
+ }).done(function(result) {
152
+ if (result.success === false) { // Rejected
153
+ alert(result.data);
154
+ return;
155
+ }
156
+ update(col, null);
157
+ });
158
+ });
159
+ })();
160
+ });
161
+
162
+ cols.removeClass('busy');
163
+ }
164
+
165
+ /**
166
+ * Returns whether a column is busy
167
+ * @param {string|DOMElement|jQuery} col - A column to check
168
+ * @return {boolean}
169
+ */
170
+ function isBusy(col) {
171
+ return $(col).hasClass('busy');
172
+ }
173
+
174
+ /**
175
+ * Makes a column busy
176
+ * @param {string|DOMElement|jQuery} col - A column to make busy
177
+ */
178
+ function goBusy(col) {
179
+ col = $(col);
180
+ col.addClass('busy');
181
+ col.find('input[type="text"]').prop('disabled', true);
182
+ }
183
+
184
+ /**
185
+ * Sends a rename request
186
+ * @param {string|DOMElement|jQuery} col - A column to update
187
+ * @param {string|null} newName - The new name of the media. null means auto
188
+ * @return {jQuery.Deffered} - The actual ajax request
189
+ */
190
+ function rename(col, newName) {
191
+ col = $(col);
192
+ var id = col.find('input[name="id"]').val();
193
+
194
+ var data = {
195
+ action: 'mfrh_rename_media',
196
+ subaction: 'renameMediaId',
197
+ id: id
198
+ }
199
+ if (typeof newName == 'string') data.newName = newName;
200
+
201
+ return $.ajax(ajaxurl, {
202
+ type: 'POST',
203
+ dataType: 'json',
204
+ data: data
205
+
206
+ }).done(function(result) {
207
+ if (result.success === false) { // Rejected
208
+ alert(result.data);
209
+ return;
210
+ }
211
+ update(col, result.data);
212
+ });
213
+ }
214
+
215
+ /**
216
+ * Updates the view of the row which affected by asynchronous rename
217
+ * @param {string|DOMElement|jQuery} col - A column to update
218
+ * @param {string|null} newName - The new name of the media
219
+ * @return {jQuery.Deffered} - The actual ajax request
220
+ */
221
+ function update(col, newName) {
222
+ col = $(col);
223
+ var id = col.find('input[name="id"]').val();
224
+
225
+ // Update the filename information in 'File' column
226
+ if (typeof newName == 'string') {
227
+ var filename = col.closest('tr').find('.column-title .filename');
228
+ var children = filename.children().detach();
229
+ filename.text(newName);
230
+ filename.prepend(children);
231
+ }
232
+
233
+ // Re-render the column
234
+ return $.ajax(ajaxurl, {
235
+ type: 'POST',
236
+ dataType: 'json',
237
+ data: {
238
+ action: 'mfrh_render_column',
239
+ id: id
240
+ }
241
+
242
+ }).done(function(result) {
243
+ if (result.success === false) { // Rejected
244
+ // TODO Reload the page
245
+ return;
246
+ }
247
+ // Overwrite the column content with the rendering result
248
+ col.html(result.data);
249
+ // Re-initialize the content
250
+ init(col);
251
+ });
252
+ }
253
+
254
+ })(jQuery);
style.css ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * The common styles for Media File Renamer
3
+ */
4
+
5
+ /**
6
+ * Rename column in Media Library
7
+ */
8
+ .mfrh_column {
9
+ position: relative;
10
+ }
11
+ .mfrh_column.busy a {
12
+ cursor: default;
13
+ pointer-events: none;
14
+ }
15
+
16
+ /* Buttons */
17
+ .mfrh_column .button {
18
+ display: inline-block;
19
+ }
20
+ .mfrh_column .button-primary,
21
+ .mfrh_column .button-primary .dashicons {
22
+ line-height: 26px;
23
+ }
24
+ .mfrh_column .button-primary {
25
+ width: 50px;
26
+ padding-left: 0;
27
+ padding-right: 0;
28
+ text-align: center;
29
+ }
30
+ .mfrh_column .button-primary.updating-message:before {
31
+ margin: 3px 0 0 0;
32
+ }
33
+ .mfrh_column .button-primary.updating-message .label {
34
+ display: none;
35
+ }
36
+ .mfrh_column .button.rename {
37
+ display: none;
38
+ }
39
+
40
+ /* Icons */
41
+ .mfrh_column .icons-wrap {
42
+ display: inline-block;
43
+ }
44
+ .mfrh_column .icons-wrap .dashicons {
45
+ display: inline-block;
46
+ }
47
+ .mfrh_column.busy .icons-wrap a.dashicons {
48
+ opacity: 0.5;
49
+ }
50
+ .mfrh_column .icons-wrap .dashicons + .dashicons {
51
+ margin-left: -2px; /* Shrinks the gaps between icons */
52
+ }
53
+ .mfrh_column .icons-wrap .dashicons-lock,
54
+ .mfrh_column .icons-wrap .dashicons-unlock {
55
+ font-size: 20px;
56
+ }
57
+ .mfrh_column .icons-wrap .dashicons-lock {
58
+ color: #36b15c;
59
+ }
60
+ .mfrh_column .icons-wrap .dashicons-yes {
61
+ font-size: 24px;
62
+ color: #36b15c;
63
+ }
64
+ .mfrh_column .icons-wrap .dashicons-undo {
65
+ font-size: 15px;
66
+ vertical-align: -4px;
67
+ color: #de4817;
68
+ }
69
+
70
+ /* Text Fields */
71
+ .mfrh_column input[type="text"] {
72
+ display: inline-block;
73
+ position: relative;
74
+ width: calc(100% - 128px);
75
+ height: 28px;
76
+ font-size: 11px;
77
+ }
78
+ .mfrh_column input[name="recommended-filename"] {
79
+ display: none;
80
+ background-color: white;
81
+ color: #0073aa;
82
+ }
83
+ .mfrh_column input[name="filename"].hidden {
84
+ display: none;
85
+ }
86
+ .mfrh_column input[name="filename"].hidden ~ input[name="recommended-filename"] {
87
+ display: inline-block;
88
+ }
89
+ .mfrh_column input[name="filename"].changed ~ .button.rename {
90
+ display: inline-block;
91
+ }
92
+ .mfrh_column input[name="filename"].changed ~ .button.auto-rename {
93
+ display: none;
94
+ }
ui.php ADDED
@@ -0,0 +1,421 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Meow_MFRH_UI {
4
+ private $core = null;
5
+ private $admin = null;
6
+
7
+ function __construct( $core, $admin ) {
8
+ $this->core = $core;
9
+ $this->admin = $admin;
10
+ add_action( 'admin_head', array( $this, 'admin_head' ) );
11
+ add_action( 'admin_menu', array( $this, 'admin_menu' ) );
12
+ add_action( 'add_meta_boxes', array( $this, 'add_rename_metabox' ) );
13
+ add_action( 'wp_ajax_mfrh_rename_media', array( $this, 'wp_ajax_mfrh_rename_media' ) );
14
+ add_action( 'wp_ajax_mfrh_undo_media', array( $this, 'wp_ajax_mfrh_undo_media' ) );
15
+ add_action( 'wp_ajax_mfrh_lock_media', array( $this, 'wp_ajax_mfrh_lock_media' ) );
16
+ add_action( 'wp_ajax_mfrh_analyze_media', array( $this, 'wp_ajax_mfrh_analyze_media' ) );
17
+ add_action( 'wp_ajax_mfrh_render_column', array( $this, 'wp_ajax_mfrh_render_column' ) );
18
+ add_action( 'admin_notices', array( $this, 'admin_notices' ) );
19
+ add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
20
+ add_filter( 'media_send_to_editor', array( $this, 'media_send_to_editor' ), 20, 3 );
21
+
22
+ // Column for Media Library
23
+ $method = apply_filters( 'mfrh_method', 'media_title' );
24
+ if ( $method != 'none' ) {
25
+ add_filter( 'manage_media_columns', array( $this, 'add_media_columns' ) );
26
+ add_action( 'manage_media_custom_column', array( $this, 'manage_media_custom_column' ), 10, 2 );
27
+ }
28
+
29
+ // Media Library Bulk Actions
30
+ add_filter( 'bulk_actions-upload', array( $this, 'library_bulk_actions' ) );
31
+ add_filter( 'handle_bulk_actions-upload', array( $this, 'library_bulk_actions_handler' ), 10, 3 );
32
+ }
33
+
34
+ /**
35
+ * Renders a view within the views directory.
36
+ * @param string $view The name of the view to render
37
+ * @param array $data
38
+ * An associative array of variables to bind to the view.
39
+ * Each key turns into a variable name.
40
+ * @return string Rendered view
41
+ */
42
+ function render_view( $view, $data = null ) {
43
+ ob_start();
44
+ if ( is_array( $data ) ) extract( $data );
45
+ include( __DIR__ . "/views/$view.php" );
46
+ return ob_get_clean();
47
+ }
48
+
49
+ /**
50
+ * Loads some scripts & styles for certain pages
51
+ * @param string $page The current page identifier
52
+ */
53
+ function admin_enqueue_scripts( $page ) {
54
+ global $mfrh_version;
55
+ $base = plugin_dir_url( __FILE__ );
56
+ wp_enqueue_style( 'mfrh_style', $base . 'style.css', array(), $mfrh_version );
57
+
58
+ switch ( $page ) {
59
+ case 'upload.php': // Media Library
60
+ wp_enqueue_script( 'mfrh_media-library', $base . 'scripts/media-library.js', array( 'jquery' ), $mfrh_version );
61
+ break;
62
+ }
63
+ }
64
+
65
+ function admin_head() {
66
+ if ( !empty( $_GET['mfrh_rename'] ) ) {
67
+ $mfrh_rename = $_GET['mfrh_rename'];
68
+ $this->core->rename( $mfrh_rename );
69
+ $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'mfrh_rename' ), $_SERVER['REQUEST_URI'] );
70
+ }
71
+ if ( !empty( $_GET['mfrh_unlock'] ) ) {
72
+ $mfrh_unlock = $_GET['mfrh_unlock'];
73
+ delete_post_meta( $mfrh_unlock, '_manual_file_renaming' );
74
+ $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'mfrh_unlock' ), $_SERVER['REQUEST_URI'] );
75
+ }
76
+ if ( !empty( $_GET['mfrh_undo'] ) ) {
77
+ $mfrh_undo = $_GET['mfrh_undo'];
78
+ $original_filename = get_post_meta( $mfrh_undo, '_original_filename', true );
79
+ $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'mfrh_undo' ), $_SERVER['REQUEST_URI'] );
80
+ $this->core->rename( $mfrh_undo, $original_filename );
81
+
82
+ $fp = get_attached_file( $mfrh_undo );
83
+ $path_parts = pathinfo( $fp );
84
+ $basename = $path_parts['basename'];
85
+ if ( $basename == $original_filename )
86
+ delete_post_meta( $mfrh_undo, '_original_filename' );
87
+ }
88
+ if ( !empty( $_GET['mfrh_lock'] ) ) {
89
+ $mfrh_lock = $_GET['mfrh_lock'];
90
+ add_post_meta( $mfrh_lock, '_manual_file_renaming', true, true );
91
+ $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'mfrh_lock' ), $_SERVER['REQUEST_URI'] );
92
+ }
93
+
94
+ echo $this->render_view( 'admin-head' );
95
+ }
96
+
97
+ function admin_menu() {
98
+ $method = apply_filters( 'mfrh_method', 'media_title' );
99
+ if ( $method != 'none' ) {
100
+ add_media_page( 'Media File Renamer', __( 'Rename', 'media-file-renamer' ), 'manage_options', 'rename_media_files', array( $this, 'rename_media_files' ) );
101
+ }
102
+ }
103
+
104
+ function rename_media_files() {
105
+ global $wpdb;
106
+ if ( $locked = ( isset( $_GET ) && isset( $_GET['mfrh_lockall'] ) ) ) {
107
+ $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_manual_file_renaming'" );
108
+ $wpdb->query( "INSERT INTO $wpdb->postmeta (meta_key, meta_value, post_id)
109
+ SELECT '_manual_file_renaming', 1, p.ID
110
+ FROM $wpdb->posts p WHERE post_status = 'inherit' AND post_type = 'attachment'"
111
+ );
112
+ }
113
+
114
+ if ( isset( $_GET ) && isset( $_GET['mfrh_unlockall'] ) ) {
115
+ $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_manual_file_renaming'" );
116
+ }
117
+
118
+ $checkFiles = null;
119
+ if ( isset( $_GET ) && isset( $_GET['mfrh_scancheck'] ) )
120
+ $checkFiles = $this->core->check_text();
121
+ // FLAGGING
122
+ // if ( get_option( 'mfrh_flagging' ) ) {
123
+ // $this->core->file_counter( $flagged, $total, true );
124
+ // }
125
+ $all_media = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts p WHERE post_status = 'inherit' AND post_type = 'attachment'" );
126
+ $manual_media = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->postmeta WHERE meta_key = '_manual_file_renaming' AND meta_value = 1" );
127
+
128
+ echo $this->render_view( 'menu-screen', array(
129
+ 'wpdb' => $wpdb,
130
+ 'ui' => $this,
131
+ 'core' => $this->core,
132
+ 'admin' => $this->admin,
133
+ 'locked' => $locked,
134
+ 'checkFiles' => $checkFiles,
135
+ 'all_media' => $all_media,
136
+ 'manual_media' => $manual_media
137
+ ) );
138
+ }
139
+
140
+ function add_rename_metabox() {
141
+ add_meta_box( 'mfrh_media', 'Filename', array( $this, 'attachment_fields' ), 'attachment', 'side', 'high' );
142
+ }
143
+
144
+ function attachment_fields( $post ) {
145
+ $info = pathinfo( get_attached_file( $post->ID ) );
146
+ $basename = $info['basename'];
147
+ $is_manual = apply_filters( 'mfrh_manual', false );
148
+ $html = '<input type="text" readonly class="widefat" name="mfrh_new_filename" value="' . $basename. '" />';
149
+ $html .= '<p class="description">This feature is for <a target="_blank" href="http://meowapps.com/media-file-renamer/">Pro users</a> only.</p>';
150
+ echo apply_filters( "mfrh_admin_attachment_fields", $html, $post );
151
+ return $post;
152
+ }
153
+
154
+ /**
155
+ *
156
+ * 'RENAME' LINK IN MEDIA LIBRARY
157
+ *
158
+ */
159
+
160
+ function add_media_columns( $columns ) {
161
+ $columns['mfrh_column'] = __( 'Rename', 'media-file-renamer' );
162
+ return $columns;
163
+ }
164
+
165
+ function manage_media_custom_column( $column_name, $id ) {
166
+ if ( $column_name !== 'mfrh_column' ) return;
167
+ echo $this->render_column( $id );
168
+ }
169
+
170
+ /**
171
+ * Renders a custom column content for a specific post
172
+ * @param int $id The post id to render
173
+ * @return string Rendered content
174
+ */
175
+ function render_column( $id ) {
176
+ return $this->render_view( 'column', array(
177
+ 'ui' => $this,
178
+ 'core' => $this->core,
179
+ 'admin' => $this->admin,
180
+ 'id' => $id
181
+ ) );
182
+ }
183
+
184
+ function generate_explanation( $file ) {
185
+
186
+ static $previous = array();
187
+
188
+ $smallDiv = '<div style="line-height: 12px; font-size: 10px; margin-top: 5px;">';
189
+
190
+ if ( $file['post_title'] == "" ) {
191
+ echo " <a class='button-primary' href='post.php?post=" . $file['post_id'] . "&action=edit'>" . __( 'Edit Media', 'media-file-renamer' ) . "</a><br /><small>" . __( 'This title cannot be used for a filename.', 'media-file-renamer' ) . "</small>";
192
+ }
193
+ else if ( $file['desired_filename_exists'] ) {
194
+ echo "<a class='button-primary' href='post.php?post=" . $file['post_id'] . "&action=edit'>" . __( 'Edit Media', 'media-file-renamer' ) . "</a><br />$smallDiv" . __( 'The ideal filename already exists. If you would like to use a count and rename it, enable the <b>Numbered Files</b> option in the plugin settings.', 'media-file-renamer' ) . "</div>";
195
+ }
196
+ else {
197
+ $page = isset( $_GET['page'] ) ? ( '&page=' . $_GET['page'] ) : "";
198
+ $mfrh_scancheck = ( isset( $_GET ) && isset( $_GET['mfrh_scancheck'] ) ) ? '&mfrh_scancheck' : '';
199
+ $mfrh_to_rename = ( !empty( $_GET['to_rename'] ) && $_GET['to_rename'] == 1 ) ? '&to_rename=1' : '';
200
+ $modify_url = "post.php?post=" . $file['post_id'] . "&action=edit";
201
+ $page = isset( $_GET['page'] ) ? ( '&page=' . $_GET['page'] ) : "";
202
+
203
+ $isNew = true;
204
+ if ( in_array( $file['desired_filename'], $previous ) )
205
+ $isNew = false;
206
+ else
207
+ array_push( $previous, $file['desired_filename'] );
208
+
209
+ echo "<a class='button button-primary auto-rename' href='?" . $page . $mfrh_scancheck . $mfrh_to_rename . "&mfrh_rename=" . $file['post_id'] . "'>" . __( 'Auto-Rename', 'media-file-renamer' ) . "</a>";
210
+ echo "<a title='" . __( 'Click to lock it to manual only.', 'media-file-renamer' ) . "' href='?" . $page . "&mfrh_lock=" . $file['post_id'] . "' class='lock'><span style='font-size: 16px; margin-top: 5px;' class='dashicons dashicons-unlock'></span></a>";
211
+
212
+ if ( $file['case_issue'] ) {
213
+ echo '<br />' . $smallDiv .
214
+ sprintf( __( 'Rename in lowercase, to %s. You can also <a href="%s">edit this media</a>.', 'media-file-renamer' ),
215
+ $file['desired_filename'], $modify_url ) . "</div>";
216
+ }
217
+ else {
218
+ echo '<br />' . $smallDiv .
219
+ sprintf( __( 'Rename to %s. You can also <a href="%s">EDIT THIS MEDIA</a>.', 'media-file-renamer' ),
220
+ $file['desired_filename'], $modify_url ) . "</div>";
221
+ }
222
+
223
+ if ( !$isNew ) {
224
+ echo $smallDiv . "<i>";
225
+ echo __( 'The first media you rename will actually get this filename; the next will be either not renamed or will have a counter appended to it.', 'media-file-renamer' );
226
+ echo '</i></div>';
227
+ }
228
+ }
229
+ }
230
+
231
+ /**
232
+ *
233
+ * BULK ACTIONS IN MEDIA LIBRARY
234
+ *
235
+ */
236
+
237
+ function library_bulk_actions( $bulk_actions ) {
238
+ $bulk_actions['mfrh_lock_all'] = __( 'Lock (Renamer)', 'media-file-renamer');
239
+ $bulk_actions['mfrh_unlock_all'] = __( 'Unlock (Renamer)', 'media-file-renamer');
240
+ $bulk_actions['mfrh_rename_all'] = __( 'Rename (Renamer)', 'media-file-renamer');
241
+ return $bulk_actions;
242
+ }
243
+
244
+ function library_bulk_actions_handler( $redirect_to, $doaction, $ids ) {
245
+ if ( $doaction == 'mfrh_lock_all' ) {
246
+ foreach ( $ids as $post_id ) {
247
+ add_post_meta( $post_id, '_manual_file_renaming', true, true );
248
+ }
249
+ }
250
+ if ( $doaction == 'mfrh_unlock_all' ) {
251
+ foreach ( $ids as $post_id ) {
252
+ delete_post_meta( $post_id, '_manual_file_renaming' );
253
+ }
254
+ }
255
+ if ( $doaction == 'mfrh_rename_all' ) {
256
+ foreach ( $ids as $post_id ) {
257
+ $this->core->rename( $post_id );
258
+ }
259
+ }
260
+ return $redirect_to;
261
+ }
262
+
263
+ /**
264
+ *
265
+ * BULK MEDIA RENAME PAGE
266
+ *
267
+ */
268
+
269
+ function wp_ajax_mfrh_rename_media() {
270
+ $subaction = $_POST['subaction'];
271
+ if ( $subaction == 'getMediaIds' ) {
272
+ $all = intval( $_POST['all'] );
273
+ global $wpdb;
274
+ $ids = $wpdb->get_col( "SELECT p.ID FROM $wpdb->posts p WHERE post_status = 'inherit' AND post_type = 'attachment'" );
275
+ if ( !$all ) {
276
+ $idsToRemove = $wpdb->get_col( "SELECT m.post_id FROM $wpdb->postmeta m
277
+ WHERE m.meta_key = '_manual_file_renaming' and m.meta_value = 1" );
278
+ $ids = array_values( array_diff( $ids, $idsToRemove ) );
279
+ }
280
+ else {
281
+ // We rename all, so we should unlock everything.
282
+ $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_manual_file_renaming'" );
283
+ }
284
+ $reply = array();
285
+ $reply['ids'] = $ids;
286
+ $reply['total'] = count( $ids );
287
+ echo json_encode( $reply );
288
+ die;
289
+ }
290
+ else if ( $subaction == 'renameMediaId' ) {
291
+ $id = intval( $_POST['id'] );
292
+ $newName = array_key_exists( 'newName', $_POST ) ? $_POST['newName'] : null;
293
+ if ( isset( $newName ) ) { // Manual Rename
294
+ if ( !$this->admin->is_registered() ) {
295
+ wp_send_json_error( __( 'This feature is for Pro users only', 'media-file-renamer' ) );
296
+ } else if ( !get_option( 'mfrh_manual_rename' ) ) {
297
+ wp_send_json_error( __( 'You need to enable Manual Rename in the plugin settings', 'media-file-renamer' ) );
298
+ }
299
+ }
300
+ $this->core->rename( $id, $newName );
301
+ wp_send_json_success( basename( get_attached_file( $id ) ) );
302
+ }
303
+ echo 0;
304
+ die();
305
+ }
306
+
307
+ function wp_ajax_mfrh_undo_media() {
308
+ $subaction = $_POST['subaction'];
309
+ if ( $subaction == 'getMediaIds' ) {
310
+ global $wpdb;
311
+ $ids = $wpdb->get_col( "
312
+ SELECT p.ID FROM $wpdb->posts p
313
+ WHERE post_status = 'inherit' AND post_type = 'attachment'" );
314
+ $reply = array();
315
+ $reply['ids'] = $ids;
316
+ $reply['total'] = count( $ids );
317
+ echo json_encode( $reply );
318
+ die;
319
+ }
320
+ else if ( $subaction == 'undoMediaId' ) {
321
+ $id = intval( $_POST['id'] );
322
+ $original_filename = get_post_meta( $id, '_original_filename', true );
323
+ $this->core->rename( $id, $original_filename );
324
+ delete_post_meta( $id, '_original_filename' );
325
+ wp_send_json_success( basename( get_attached_file( $id ) ) );
326
+ }
327
+ echo 0;
328
+ die();
329
+ }
330
+
331
+ /**
332
+ * An ajax action to lock a media.
333
+ *
334
+ * Ajax parameters:
335
+ * - id : The post id to lock
336
+ * - subaction : (optional)
337
+ * + 'unlock' : Unlocks the post
338
+ */
339
+ function wp_ajax_mfrh_lock_media() {
340
+ if ( !isset( $_POST['id'] ) )
341
+ wp_send_json_error( __( 'Invalid request', 'media-file-renamer' ) );
342
+
343
+ // Default operation
344
+ if ( !isset( $_POST['subaction'] ) ) {
345
+ if ( !$this->core->lock( (int) $_POST['id'] ) )
346
+ wp_send_json_error( __( 'Failed to lock', 'media-file-renamer' ) );
347
+ wp_send_json_success();
348
+ }
349
+
350
+ // Optional operations
351
+ switch ( $_POST['subaction'] ) {
352
+ case 'unlock':
353
+ if ( !$this->core->unlock( (int) $_POST['id'] ) )
354
+ wp_send_json_error( __( 'Failed to unlock', 'media-file-renamer' ) );
355
+ wp_send_json_success();
356
+ break;
357
+ }
358
+
359
+ wp_send_json_error( __( 'Invalid request', 'media-file-renamer' ) );
360
+ }
361
+
362
+ /**
363
+ * An ajax action that analyzes a media
364
+ *
365
+ * Ajax parameters:
366
+ * - id : The post id to analyze
367
+ */
368
+ function wp_ajax_mfrh_analyze_media() {
369
+ if ( !isset( $_POST['id'] ) )
370
+ wp_send_json_error( __( 'Invalid request', 'media-file-renamer' ) );
371
+
372
+ if ( !$post = get_post( (int) $_POST['id'], ARRAY_A ) )
373
+ wp_send_json_error( __( 'No such post', 'media-file-renamer' ) );
374
+
375
+ $result = array ();
376
+ $this->core->check_attachment( $post, $result );
377
+ wp_send_json_success( $result );
378
+ }
379
+
380
+ /**
381
+ * An ajax action that simply calls render_column() and returns the result.
382
+ *
383
+ * Ajax parameters:
384
+ * - id : The post id to render
385
+ */
386
+ function wp_ajax_mfrh_render_column() {
387
+ if ( !isset( $_POST['id'] ) ) wp_send_json_error();
388
+ wp_send_json_success( $this->render_column( (int) $_POST['id'] ) );
389
+ }
390
+
391
+ /**
392
+ *
393
+ * ERROR/INFO MESSAGE HANDLING
394
+ *
395
+ */
396
+
397
+ function admin_notices() {
398
+ $screen = get_current_screen();
399
+ if ( ( $screen->base == 'post' && $screen->post_type == 'attachment' ) ||
400
+ ( $screen->base == 'media' && isset( $_GET['attachment_id'] ) ) ) {
401
+ $id = isset( $_GET['post'] ) ? $_GET['post'] : $_GET['attachment_id'];
402
+ if ( $this->core->check_attachment( get_post( $id, ARRAY_A ), $output ) ) {
403
+ if ( $output['desired_filename_exists'] ) {
404
+ echo '<div class="error"><p>
405
+ The file ' . $output['desired_filename'] . ' already exists. Please give a new title for this media.
406
+ </p></div>';
407
+ }
408
+ }
409
+ if ( $this->core->wpml_media_is_installed() && !$this->core->is_real_media( $id ) ) {
410
+ echo '<div class="error"><p>
411
+ This attachment seems to be a virtual copy (or translation). Media File Renamer will not make any modification from here.
412
+ </p></div>';
413
+ }
414
+ }
415
+ }
416
+
417
+ function media_send_to_editor( $html, $id, $attachment ) {
418
+ $this->core->check_attachment( get_post( $id, ARRAY_A ), $output );
419
+ return $html;
420
+ }
421
+ }
updates.php CHANGED
@@ -9,6 +9,8 @@ class Meow_MFRH_Updates {
9
  $this->core = $core;
10
  $this->admin = $admin;
11
 
 
 
12
  // Support for WPML
13
  if ( function_exists( 'icl_object_id' ) )
14
  require( 'plugins/wpml.php' );
9
  $this->core = $core;
10
  $this->admin = $admin;
11
 
12
+ $this->init_actions();
13
+
14
  // Support for WPML
15
  if ( function_exists( 'icl_object_id' ) )
16
  require( 'plugins/wpml.php' );
views/admin-head.php ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script type="text/javascript" >
2
+
3
+ var current;
4
+ var ids = [];
5
+
6
+ function mfrh_process_next() {
7
+ var data = { action: 'mfrh_rename_media', subaction: 'renameMediaId', id: ids[current - 1] };
8
+ jQuery('#mfrh_progression').text(current + "/" + ids.length);
9
+ jQuery.post(ajaxurl, data, function (response) {
10
+ if (++current <= ids.length) {
11
+ mfrh_process_next();
12
+ }
13
+ else {
14
+ jQuery('#mfrh_progression').html("<?php echo __( "Done. Please <a href='?page=rename_media_files'>refresh</a> this page.", 'media-file-renamer' ); ?>");
15
+ }
16
+ });
17
+ }
18
+
19
+ function mfrh_rename_media(all) {
20
+ current = 1;
21
+ ids = [];
22
+ var data = { action: 'mfrh_rename_media', subaction: 'getMediaIds', all: all ? '1' : '0' };
23
+ jQuery('#mfrh_progression').text("<?php echo __( "Please wait...", 'media-file-renamer' ); ?>");
24
+ jQuery.post(ajaxurl, data, function (response) {
25
+ reply = jQuery.parseJSON(response);
26
+ ids = reply.ids;
27
+ jQuery('#mfrh_progression').html(current + "/" + ids.length);
28
+ mfrh_process_next();
29
+ });
30
+ }
31
+
32
+ function mfrh_process_next_undo() {
33
+ var data = { action: 'mfrh_undo_media', subaction: 'undoMediaId', id: ids[current - 1] };
34
+ jQuery('#mfrh_progression').text(current + "/" + ids.length);
35
+ jQuery.post(ajaxurl, data, function (response) {
36
+ if (++current <= ids.length) {
37
+ mfrh_process_next_undo();
38
+ }
39
+ else {
40
+ jQuery('#mfrh_progression').html("<?php echo __( "Done. Please <a href='?page=rename_media_files'>refresh</a> this page.", 'media-file-renamer' ); ?>");
41
+ }
42
+ });
43
+ }
44
+
45
+ function mfrh_undo_media(all) {
46
+ current = 1;
47
+ ids = [];
48
+ var data = { action: 'mfrh_undo_media', subaction: 'getMediaIds', all: all ? '1' : '0' };
49
+ jQuery('#mfrh_progression').text("<?php echo __( "Please wait...", 'media-file-renamer' ); ?>");
50
+ jQuery.post(ajaxurl, data, function (response) {
51
+ reply = jQuery.parseJSON(response);
52
+ ids = reply.ids;
53
+ jQuery('#mfrh_progression').html(current + "/" + ids.length);
54
+ mfrh_process_next_undo();
55
+ });
56
+ }
57
+
58
+ function mfrh_export_table(table) {
59
+ var table = jQuery(table);
60
+ var data = [];
61
+ // Header
62
+ table.find('thead tr').each(function(i, tr) {
63
+ var row = [];
64
+ jQuery(tr).find('th').each(function(i, td) {
65
+ var text = jQuery(td).text();
66
+ row.push(text);
67
+ });
68
+ data.push(row);
69
+ });
70
+ // Body
71
+ table.find('tbody tr').each(function(i, tr) {
72
+ var row = [];
73
+ jQuery(tr).find('td').each(function(i, td) {
74
+ var text = jQuery(td).text();
75
+ row.push(text);
76
+ });
77
+ data.push(row);
78
+ });
79
+ var csvContent = "data:text/csv;charset=utf-8,";
80
+ data.forEach(function(infoArray, index){
81
+ dataString = infoArray.join(",");
82
+ csvContent += index < data.length ? dataString+ "\n" : dataString;
83
+ });
84
+ var encodedUri = encodeURI(csvContent);
85
+ var link = document.createElement("a");
86
+ link.setAttribute("href", encodedUri);
87
+ link.setAttribute("download", "media-file-renamer.csv");
88
+ document.body.appendChild(link);
89
+ link.click();
90
+ }
91
+
92
+ </script>
views/column.php ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @param Meow_MFRH_UI $ui
4
+ * @param Meow_MFRH_Core $core
5
+ * @param Meow_MFRH_Admin $admin
6
+ * @param int $id
7
+ */
8
+
9
+ $page = isset( $_GET['page'] ) ? ( '&page=' . $_GET['page'] ) : ""; // What for?
10
+ $paged = isset( $_GET['paged'] ) ? ( '&paged=' . $_GET['paged'] ) : "";
11
+
12
+ $file = array (); // Various stats of the attachment
13
+ $needsRename = $core->check_attachment( get_post( $id, ARRAY_A ), $file );
14
+ ?>
15
+ <input type="hidden" name="id" value="<?php echo $id; ?>">
16
+
17
+ <?php // Quick Renamer ?>
18
+ <?php $filename = basename( get_attached_file( $id ) ); ?>
19
+ <?php $disabled = !( $admin->is_registered() && get_option( 'mfrh_manual_rename' ) ); ?>
20
+ <input type="text" name="filename" value="<?php esc_attr_e( $filename ); ?>" autocomplete="off" data-origin="<?php esc_attr_e( $filename ); ?>"<?php if ($disabled) echo ' readonly'; ?>>
21
+ <?php if ( isset( $file['desired_filename'] ) ): // i ?>
22
+ <input type="text" name="recommended-filename" value="<?php esc_attr_e( $file['desired_filename'] ); ?>" readonly>
23
+ <?php endif; // i ?>
24
+ <a href="#" class="button button-primary rename hidden">
25
+ <span class="label dashicons dashicons-edit"></span>
26
+ </a>
27
+
28
+ <?php // Locked media ?>
29
+ <?php if ( $locked = get_post_meta( $id, '_manual_file_renaming', true ) ): // i ?>
30
+ <div class="icons-wrap">
31
+ <span title="<?php _e( 'Manually renamed.', 'media-file-renamer' ); ?>" class="dashicons dashicons-yes"></span>
32
+ <a title="<?php _e( 'Locked to manual only. Click to unlock it.', 'media-file-renamer' ); ?>" href="?<?php echo $page . '&mfrh_unlock=' . $id . $paged; ?>" class="unlock dashicons dashicons-lock"></a>
33
+ </div>
34
+
35
+ <?php // Media that needs renaming ?>
36
+ <?php elseif ( $needsRename ): // i ?>
37
+ <?php static $previous = array(); // This doesn't work ?>
38
+ <?php if ( $file['post_title'] == "" ): // ii ?>
39
+ <a class="button-primary" href="post.php?post=<?php echo $id . '&action=edit'; ?>"><?php _e( 'Edit Media', 'media-file-renamer' ); ?></a><br />
40
+ <small><?php _e( 'This title cannot be used for a filename.', 'media-file-renamer' ); ?></small>
41
+ <?php elseif ( $file['desired_filename_exists'] ): // ii ?>
42
+ <a class="button-primary" href="post.php?post=<?php echo $id . '&action=edit'; ?>"><?php _e( 'Edit Media', 'media-file-renamer' ); ?></a><br />
43
+ <div style="line-height: 12px; font-size: 10px; margin-top: 5px;">
44
+ <?php _e( 'The ideal filename already exists. If you would like to use a count and rename it, enable the <b>Numbered Files</b> option in the plugin settings.', 'media-file-renamer' ); ?>
45
+ </div>
46
+ <?php else: // ii ?>
47
+ <?php
48
+ $mfrh_scancheck = ( isset( $_GET ) && isset( $_GET['mfrh_scancheck'] ) ) ? '&mfrh_scancheck' : '';
49
+ $mfrh_to_rename = ( !empty( $_GET['to_rename'] ) && $_GET['to_rename'] == 1 ) ? '&to_rename=1' : '';
50
+ $modify_url = "post.php?post=" . $id . "&action=edit";
51
+
52
+ // This doesn't work
53
+ $isNew = true;
54
+ if ( in_array( $file['desired_filename'], $previous ) )
55
+ $isNew = false;
56
+ else
57
+ array_push( $previous, $file['desired_filename'] );
58
+ ?>
59
+ <a class="button button-primary auto-rename" href="?<?php echo $page . $mfrh_scancheck . $mfrh_to_rename . '&mfrh_rename=' . $id; ?>">
60
+ <span class="label"><?php _e( 'Auto', 'media-file-renamer' ); ?></span>
61
+ </a>
62
+ <div class="icons-wrap">
63
+ <a title="<?php _e( 'Click to lock it to manual only.', 'media-file-renamer' ); ?>" href="?<?php echo $page . '&mfrh_lock=' . $id; ?>" class="lock dashicons dashicons-unlock"></a>
64
+ </div>
65
+ <?php if ( $file['case_issue'] ): // iii ?>
66
+ <div style="line-height: 12px; font-size: 10px; margin-top: 5px;">
67
+ <?php printf( __( 'Rename in lowercase, to %s. You can also <a href="%s">edit this media</a>.', 'media-file-renamer' ), $file['desired_filename'], $modify_url ); ?>
68
+ </div>
69
+ <?php else: // iii ?>
70
+ <div style="line-height: 12px; font-size: 10px; margin-top: 5px;">
71
+ <?php printf( __( 'Rename to %s. You can also <a href="%s">EDIT THIS MEDIA</a>.', 'media-file-renamer' ), $file['desired_filename'], $modify_url ); ?>
72
+ </div>
73
+ <?php endif; // iii ?>
74
+ <?php if ( !$isNew ): // iii ?>
75
+ <div style="line-height: 12px; font-size: 10px; margin-top: 5px;">
76
+ <i><?php _e( 'The first media you rename will actually get this filename; the next will be either not renamed or will have a counter appended to it.', 'media-file-renamer' ); ?></i>
77
+ </div>
78
+ <?php endif; // iii ?>
79
+ <?php endif; // ii ?>
80
+
81
+ <?php // Non-locked media ?>
82
+ <?php else: // i ?>
83
+ <div class="icons-wrap">
84
+ <?php $original_filename = get_post_meta( $id, '_original_filename', true ); ?>
85
+ <span title="<?php _e( 'Automatically renamed.', 'media-file-renamer' ); ?>" class="dashicons dashicons-yes"></span>
86
+ <?php if ( get_option( 'mfrh_undo', false ) && !empty( $original_filename ) ): // ii ?>
87
+ <a title="<?php echo __( 'Rename to original filename: ', 'media-file-renamer' ) . $original_filename; ?>" href="?<?php echo $page . "&mfrh_undo=" . $id . $paged; ?>" class="undo dashicons dashicons-undo"></a>
88
+ <?php endif; // ii ?>
89
+ <a title="<?php _e( 'Click to lock it to manual only.', 'media-file-renamer' ); ?>" href="?<?php echo $page . "&mfrh_lock=" . $id . $paged; ?>" class="lock dashicons dashicons-unlock"></a>
90
+ </div>
91
+ <?php endif; // i ?>
views/menu-screen.php ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <div class="wrap">
2
+ <?php $admin->display_title( "Media File Renamer" ); ?>
3
+
4
+ <?php if ( $locked ): ?>
5
+ <div class="updated">
6
+ <p><?php _e( 'All the media files are now locked.', 'media-file-renamer' ); ?></p>
7
+ </div>
8
+ <?php endif; ?>
9
+
10
+ <?php if ( !$admin->is_registered() ): ?>
11
+ <div class="updated">
12
+ <p>
13
+ <?php _e( '<b>The Pro version</b> of the plugin allows you to <b>rename based on the title of the post</b> (product or whatever else) you media is attached to, <b>rename manually</b>, use <b>numbered files</b> (by adding a counter if the filenames are similar), <b>sync the title with your ALT text</b>, UTF8 support (if you need it), a force rename (to repair a broken install), and, more importantly, <b>supports the developer</b> :) Thank you!<br /><br /><a class="button-primary" href="https://store.meowapps.com/" target="_blank">Get the Pro</a>', 'media-file-renamer' ); ?>
14
+ </p>
15
+ </div>
16
+ <?php endif; ?>
17
+
18
+ <h2>Rename in Bulk</h2>
19
+
20
+ <p>
21
+ <?php _e( 'You might have noticed that some of your media are locked by the file renamer, others are unlocked. Automatically, the plugin locks the media you renamed manually. By default, they are unlocked. Here, you have the choice of rename all the media in your DB or only the ones which are unlocked (to keep the files you renamed manually). <span style="color: red; font-weight: bold;">Please backup your uploads folder + DB before using this.</span> If you don\'t know how, give a try to this: <a href="https://updraftplus.com/?afref=460" target="_blank">UpdraftPlus</a>.', 'media-file-renamer' ); ?>
22
+ </p>
23
+
24
+ <div style='margin-top: 12px; background: #FFF; padding: 5px; border-radius: 4px; height: 28px; box-shadow: 0px 0px 6px #C2C2C2;'>
25
+
26
+ <a onclick='mfrh_rename_media(false)' id='mfrh_rename_all_images' class='button-primary'
27
+ style='margin-right: 0px;'><span class="dashicons dashicons-controls-play" style="position: relative; top: 3px; left: -2px;"></span>
28
+ <?php echo sprintf( __( "Rename ALL [%d]", 'media-file-renamer' ), $all_media - $manual_media ); ?>
29
+ </a>
30
+ <a onclick='mfrh_rename_media(true)' id='mfrh_unlock_rename_all_images' class='button-primary'
31
+ style='margin-right: 0px;'><span class="dashicons dashicons-controls-play" style="position: relative; top: 3px; left: -2px;"></span>
32
+ <?php echo sprintf( __( "Unlock ALL & Rename [%d]", 'media-file-renamer' ), $all_media ); ?>
33
+ </a>
34
+ <span style='margin-right: 5px; margin-left: 5px;'>|</span>
35
+ <a href="?page=rename_media_files&mfrh_lockall" id='mfrh_lock_all_images' class='button-primary'
36
+ style='margin-right: 0px;'><span class="dashicons dashicons-controls-play" style="position: relative; top: 3px; left: -2px;"></span>
37
+ <?php echo sprintf( __( "Lock ALL [%d]", 'media-file-renamer' ), $all_media ); ?>
38
+ </a>
39
+ <a href="?page=rename_media_files&mfrh_unlockall" id='mfrh_unblock_all_images' class='button-primary'
40
+ style='margin-right: 0px;'><span class="dashicons dashicons-controls-play" style="position: relative; top: 3px; left: -2px;"></span>
41
+ <?php echo sprintf( __( "Unlock ALL [%d]", 'media-file-renamer' ), $all_media ); ?>
42
+ </a>
43
+ <a onclick='mfrh_undo_media()' id='mfrh_undo_all_images' class='button button-red'
44
+ style='margin-right: 0px; float: right;'><span class="dashicons dashicons-undo" style="position: relative; top: 3px; left: -2px;"></span>
45
+ <?php echo sprintf( __( "Undo ALL [%d]", 'media-file-renamer' ), $all_media ); ?>
46
+ </a>
47
+ <span id='mfrh_progression'></span>
48
+
49
+ <?php if ( get_option( 'mfrh_flagging' ) ): ?>
50
+ <?php if ($flagged > 0): ?>
51
+ <a onclick='mfrh_rename_media(false)' id='mfrh_rename_dued_images' class='button-primary'>
52
+ <?php echo sprintf( __( "Rename <span class='mfrh-flagged'>%d</span> flagged media", 'media-file-renamer' ), $flagged ); ?>
53
+ </a>
54
+ <?php else: ?>
55
+ <a id='mfrh_rename_dued_images' class='button-primary'>
56
+ <?php echo sprintf( __( "Rename <span class='mfrh-flagged'>%d</span> flagged media", 'media-file-renamer' ), $flagged ); ?>
57
+ </a>
58
+ <?php endif; ?>
59
+ <?php endif; ?>
60
+
61
+ </div>
62
+
63
+ <h2>Rename 1 by 1</h2>
64
+ <p><?php _e( 'If you want to rename the media this way, I recommend you to do it from the Media Library directly. If you think this "Scan All" is really handy, please tell me that you are using it on the forums. I am currently planning to remove it and moving the "Rename in Bulk" with the settings of File Renamer (to clean the WordPress UI).', 'media-file-renamer' ); ?></p>
65
+ <table class='wp-list-table widefat fixed media' style='margin-top: 15px;'>
66
+ <thead>
67
+ <tr><th><?php _e( 'Title', 'media-file-renamer' ); ?></th><th><?php _e( 'Current Filename', 'media-file-renamer' ); ?></th><th><?php _e( 'Desired Filename', 'media-file-renamer' ); ?></th><th><?php _e( 'Action', 'media-file-renamer' ); ?></th></tr>
68
+ </thead>
69
+ <tfoot>
70
+ <tr><th><?php _e( 'Title', 'media-file-renamer' ); ?></th><th><?php _e( 'Current Filename', 'media-file-renamer' ); ?></th><th><?php _e( 'Desired Filename', 'media-file-renamer' ); ?></th><th><?php _e( 'Action', 'media-file-renamer' ); ?></th></tr>
71
+ </tfoot>
72
+ <tbody>
73
+ <?php
74
+ if ( $checkFiles != null ) {
75
+ foreach ( $checkFiles as $file ) {
76
+ echo "<tr><td><a href='post.php?post=" . $file['post_id'] . "&action=edit'>" . ( $file['post_title'] == "" ? "(no title)" : $file['post_title'] ) . "</a></td>"
77
+ . "<td>" . $file['current_filename'] . "</td>"
78
+ . "<td>" . $file['desired_filename'] . "</td>";
79
+ echo "<td>";
80
+ $ui->generate_explanation( $file );
81
+ echo "</td></tr>";
82
+ }
83
+ }
84
+ else if ( isset( $_GET['mfrh_scancheck'] ) && ( $checkFiles == null || count( $checkFiles ) < 1 ) ) {
85
+ ?><tr><td colspan='4'><div style='width: 100%; margin-top: 15px; margin-bottom: 15px; text-align: center;'>
86
+ <div style='margin-top: 15px;'><?php _e( 'There are no issues. Cool!<br />Let\'s go visit <a target="_blank" href=\'https://offbeatjapan.org\'>The Offbeat Guide of Japan</a> :)', 'media-file-renamer' ); ?></div>
87
+ </div></td><?php
88
+ }
89
+ else if ( $checkFiles == null ) {
90
+ ?><tr><td colspan='4'><div style='width: 100%; text-align: center;'>
91
+ <a class='button-primary' href="?page=rename_media_files&mfrh_scancheck" style='margin-top: 15px; margin-bottom: 15px;'><span class="dashicons dashicons-admin-generic" style="position: relative; top: 3px; left: -2px;"></span>
92
+ <?php _e( "Scan All & Show Issues", 'media-file-renamer' ); ?>
93
+ </a>
94
+ </div></td><?php
95
+ }
96
+ ?>
97
+ </tbody>
98
+ </table>
99
+
100
+ <h2>Before / After</h2>
101
+ <p><?php _e( 'This is useful if you wish to create redirections from your old filenames to your new ones. The CSV file generated by Media File Renamer is compatible with the import function of the <a href="https://wordpress.org/plugins/redirection/" target="_blank">Redirection</a> plugin. The redirections with slugs are already automatically and natively handled by WordPress.', 'media-file-renamer' ); ?></p>
102
+
103
+ <div style='margin-top: 12px; background: #FFF; padding: 5px; border-radius: 4px; height: 28px; box-shadow: 0px 0px 6px #C2C2C2;'>
104
+
105
+ <a href="?page=rename_media_files&mfrh_beforeafter_filenames" class='button-primary' style='margin-right: 0px;'>
106
+ <span class="dashicons dashicons-media-spreadsheet" style="position: relative; top: 3px; left: -2px;"></span>
107
+ <?php echo _e( "Display Filenames", 'media-file-renamer' ); ?>
108
+ </a>
109
+
110
+ <a onclick="mfrh_export_table('#mfrh-before-after')" class='button-primary' style='margin-right: 0px; float: right;'>
111
+ <span class="dashicons dashicons-arrow-down-alt" style="position: relative; top: 3px; left: -2px;"></span>
112
+ <?php echo _e( "Export as CSV", 'media-file-renamer' ); ?>
113
+ </a>
114
+
115
+ </div>
116
+
117
+ <table id='mfrh-before-after' class='wp-list-table widefat fixed media' style='margin-top: 15px;'>
118
+ <thead>
119
+ <tr><th><?php _e( 'Before', 'media-file-renamer' ); ?></th><th><?php _e( 'After', 'media-file-renamer' ); ?></th></tr>
120
+ </thead>
121
+ <tfoot>
122
+ <tr><th><?php _e( 'Before', 'media-file-renamer' ); ?></th><th><?php _e( 'After', 'media-file-renamer' ); ?></th></tr>
123
+ </tfoot>
124
+ <tbody>
125
+ <?php
126
+ if ( isset( $_GET['mfrh_beforeafter_filenames'] ) || isset( $_GET['mfrh_beforeafter_slugs'] ) ) {
127
+ $results = $wpdb->get_results( "
128
+ SELECT m.post_id as ID, m.meta_value as original_filename, m2.meta_value as current_filename
129
+ FROM {$wpdb->postmeta} m
130
+ JOIN {$wpdb->postmeta} m2 on m2.post_id = m.post_id AND m2.meta_key = '_wp_attached_file'
131
+ WHERE m.meta_key = '_original_filename'" );
132
+ foreach ( $results as $row ) {
133
+ $fullsize_path = wp_get_attachment_url( $row->ID );
134
+ $parts = pathinfo( $fullsize_path );
135
+ $shorten_url = trailingslashit( $parts['dirname'] ) . $row->original_filename;
136
+ if ( isset( $_GET['mfrh_beforeafter_filenames'] ) )
137
+ echo "<tr><td>{$shorten_url}</td><td>$fullsize_path</td></tr>";
138
+ else
139
+ echo "<tr><td>{$row->original_slug}</td><td>{$row->current_slug}</td></tr>";
140
+ }
141
+ }
142
+ ?>
143
+ </tbody>
144
+ </table>
145
+ </div>