Media File Renamer - Version 3.6.4

Version Description

  • Add: Button "Undo All" to restore all the original filenames.
  • Fix: Avoid the Numbered Files and Force Renamed options to be activated at the same time.
Download this release

Release Info

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

Code changes from version 3.6.0 to 3.6.4

Files changed (5) hide show
  1. core.php +91 -11
  2. media-file-renamer.php +2 -2
  3. mfrh_admin.php +14 -2
  4. plugins/wpml.php +5 -4
  5. readme.txt +6 -2
core.php CHANGED
@@ -8,6 +8,9 @@ class Meow_MFRH_Core {
8
  $this->mfrh_admin = $mfrh_admin;
9
  add_action( 'plugins_loaded', array( $this, 'init' ) );
10
  add_action( 'plugins_loaded', array( $this, 'init_actions' ) );
 
 
 
11
  }
12
 
13
  function init() {
@@ -15,11 +18,12 @@ class Meow_MFRH_Core {
15
  include( 'mfrh_custom.php' );
16
 
17
  global $mfrh_version;
18
- //load_plugin_textdomain( 'media-file-renamer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
19
 
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_filter( 'media_send_to_editor', array( $this, 'media_send_to_editor' ), 20, 3 );
24
  add_action( 'admin_notices', array( $this, 'admin_notices' ) );
25
  add_action( 'edit_attachment', array( $this, 'edit_attachment' ) );
@@ -38,8 +42,9 @@ class Meow_MFRH_Core {
38
  add_action( 'manage_media_custom_column', array( $this, 'manage_media_custom_column' ), 10, 2 );
39
  }
40
 
41
- // Support for additional plugins
42
- add_action( 'wpml_loaded', array( $this, 'wpml_load' ) );
 
43
  }
44
 
45
  /**
@@ -78,6 +83,32 @@ class Meow_MFRH_Core {
78
  }
79
  }
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  /**
82
  *
83
  * 'RENAME' LINK
@@ -175,6 +206,33 @@ class Meow_MFRH_Core {
175
  mfrh_process_next();
176
  });
177
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
  </script>
179
  <?php
180
  }
@@ -192,7 +250,8 @@ class Meow_MFRH_Core {
192
  global $wpdb;
193
  $ids = $wpdb->get_col( "SELECT p.ID FROM $wpdb->posts p WHERE post_status = 'inherit' AND post_type = 'attachment'" );
194
  if ( !$all ) {
195
- $idsToRemove = $wpdb->get_col( "SELECT m.post_id FROM wp_postmeta m WHERE m.meta_key = '_manual_file_renaming' and m.meta_value = 1" );
 
196
  $ids = array_values( array_diff( $ids, $idsToRemove ) );
197
  }
198
  else {
@@ -215,6 +274,30 @@ class Meow_MFRH_Core {
215
  die();
216
  }
217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
  function admin_menu() {
219
  $method = apply_filters( 'mfrh_method', 'media_title' );
220
  if ( $method != 'none' ) {
@@ -483,6 +566,10 @@ class Meow_MFRH_Core {
483
  style='margin-right: 10px;'><span class="dashicons dashicons-controls-play" style="position: relative; top: 3px; left: -2px;"></span>
484
  <?php echo sprintf( __( "Lock ALL [%d]", 'media-file-renamer' ), $all_media ); ?>
485
  </a>
 
 
 
 
486
  <span id='mfrh_progression'></span>
487
 
488
  <?php if ( get_option( 'mfrh_flagging' ) ): ?>
@@ -786,13 +873,6 @@ class Meow_MFRH_Core {
786
 
787
  $this->log( "** Rename Media: " . $old_filename );
788
 
789
- // Was renamed manually? Avoid renaming when title has been changed.
790
- if ( !$this->is_real_media( $post['ID'] ) ) {
791
- $this->log( "Attachment {$post['ID']} looks like a translation, better not to continue." );
792
- delete_post_meta( $post['ID'], '_require_file_renaming' );
793
- return $post;
794
- }
795
-
796
  // If this is being renamed based on the post the media is attached to.
797
  $base_new_title = $post['post_title'];
798
  if ( !$force && $method == 'post_title' ) {
8
  $this->mfrh_admin = $mfrh_admin;
9
  add_action( 'plugins_loaded', array( $this, 'init' ) );
10
  add_action( 'plugins_loaded', array( $this, 'init_actions' ) );
11
+
12
+ // Support for additional plugins
13
+ add_action( 'wpml_loaded', array( $this, 'wpml_load' ) );
14
  }
15
 
16
  function init() {
18
  include( 'mfrh_custom.php' );
19
 
20
  global $mfrh_version;
21
+ load_plugin_textdomain( 'media-file-renamer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
22
 
23
  add_action( 'admin_head', array( $this, 'admin_head' ) );
24
  add_action( 'admin_menu', array( $this, 'admin_menu' ) );
25
  add_action( 'wp_ajax_mfrh_rename_media', array( $this, 'wp_ajax_mfrh_rename_media' ) );
26
+ add_action( 'wp_ajax_mfrh_undo_media', array( $this, 'wp_ajax_mfrh_undo_media' ) );
27
  add_filter( 'media_send_to_editor', array( $this, 'media_send_to_editor' ), 20, 3 );
28
  add_action( 'admin_notices', array( $this, 'admin_notices' ) );
29
  add_action( 'edit_attachment', array( $this, 'edit_attachment' ) );
42
  add_action( 'manage_media_custom_column', array( $this, 'manage_media_custom_column' ), 10, 2 );
43
  }
44
 
45
+ // Media Library Bulk Actions
46
+ add_filter( 'bulk_actions-upload', array( $this, 'library_bulk_actions' ) );
47
+ add_filter( 'handle_bulk_actions-upload', array( $this, 'library_bulk_actions_handler' ), 10, 3 );
48
  }
49
 
50
  /**
83
  }
84
  }
85
 
86
+ /**
87
+ *
88
+ * MEDIA LIBRARY
89
+ *
90
+ */
91
+
92
+ function library_bulk_actions( $bulk_actions ) {
93
+ $bulk_actions['mfrh_lock_all'] = __( 'Lock All (Renamer)', 'media-file-renamer');
94
+ $bulk_actions['mfrh_unlock_all'] = __( 'Unlock All (Renamer)', 'media-file-renamer');
95
+ return $bulk_actions;
96
+ }
97
+
98
+ function library_bulk_actions_handler( $redirect_to, $doaction, $ids ) {
99
+ if ( $doaction == 'mfrh_lock_all' ) {
100
+ foreach ( $ids as $post_id ) {
101
+ add_post_meta( $post_id, '_manual_file_renaming', true, true );
102
+ }
103
+ }
104
+ if ( $doaction == 'mfrh_unlock_all' ) {
105
+ foreach ( $ids as $post_id ) {
106
+ delete_post_meta( $post_id, '_manual_file_renaming' );
107
+ }
108
+ }
109
+ return $redirect_to;
110
+ }
111
+
112
  /**
113
  *
114
  * 'RENAME' LINK
206
  mfrh_process_next();
207
  });
208
  }
209
+
210
+ function mfrh_process_next_undo() {
211
+ var data = { action: 'mfrh_undo_media', subaction: 'undoMediaId', id: ids[current - 1] };
212
+ jQuery('#mfrh_progression').text(current + "/" + ids.length);
213
+ jQuery.post(ajaxurl, data, function (response) {
214
+ if (++current <= ids.length) {
215
+ mfrh_process_next_undo();
216
+ }
217
+ else {
218
+ jQuery('#mfrh_progression').html("<?php echo __( "Done. Please <a href='?page=rename_media_files'>refresh</a> this page.", 'media-file-renamer' ); ?>");
219
+ }
220
+ });
221
+ }
222
+
223
+ function mfrh_undo_media(all) {
224
+ current = 1;
225
+ ids = [];
226
+ var data = { action: 'mfrh_undo_media', subaction: 'getMediaIds', all: all ? '1' : '0' };
227
+ jQuery('#mfrh_progression').text("<?php echo __( "Please wait...", 'media-file-renamer' ); ?>");
228
+ jQuery.post(ajaxurl, data, function (response) {
229
+ reply = jQuery.parseJSON(response);
230
+ ids = reply.ids;
231
+ jQuery('#mfrh_progression').html(current + "/" + ids.length);
232
+ mfrh_process_next_undo();
233
+ });
234
+ }
235
+
236
  </script>
237
  <?php
238
  }
250
  global $wpdb;
251
  $ids = $wpdb->get_col( "SELECT p.ID FROM $wpdb->posts p WHERE post_status = 'inherit' AND post_type = 'attachment'" );
252
  if ( !$all ) {
253
+ $idsToRemove = $wpdb->get_col( "SELECT m.post_id FROM $wpdb->postmeta m
254
+ WHERE m.meta_key = '_manual_file_renaming' and m.meta_value = 1" );
255
  $ids = array_values( array_diff( $ids, $idsToRemove ) );
256
  }
257
  else {
274
  die();
275
  }
276
 
277
+ function wp_ajax_mfrh_undo_media() {
278
+ $subaction = $_POST['subaction'];
279
+ if ( $subaction == 'getMediaIds' ) {
280
+ global $wpdb;
281
+ $ids = $wpdb->get_col( "
282
+ SELECT p.ID FROM $wpdb->posts p
283
+ WHERE post_status = 'inherit' AND post_type = 'attachment'" );
284
+ $reply = array();
285
+ $reply['ids'] = $ids;
286
+ $reply['total'] = count( $ids );
287
+ echo json_encode( $reply );
288
+ die;
289
+ }
290
+ else if ( $subaction == 'undoMediaId' ) {
291
+ $id = intval( $_POST['id'] );
292
+ $original_filename = get_post_meta( $id, '_original_filename', true );
293
+ $this->rename_media( get_post( $id, ARRAY_A ), null, false, $original_filename );
294
+ echo 1;
295
+ die();
296
+ }
297
+ echo 0;
298
+ die();
299
+ }
300
+
301
  function admin_menu() {
302
  $method = apply_filters( 'mfrh_method', 'media_title' );
303
  if ( $method != 'none' ) {
566
  style='margin-right: 10px;'><span class="dashicons dashicons-controls-play" style="position: relative; top: 3px; left: -2px;"></span>
567
  <?php echo sprintf( __( "Lock ALL [%d]", 'media-file-renamer' ), $all_media ); ?>
568
  </a>
569
+ <a onclick='mfrh_undo_media()' id='mfrh_undo_all_images' class='button button-red'
570
+ style='margin-right: 0px; float: right;'><span class="dashicons dashicons-undo" style="position: relative; top: 3px; left: -2px;"></span>
571
+ <?php echo sprintf( __( "Undo ALL [%d]", 'media-file-renamer' ), $all_media ); ?>
572
+ </a>
573
  <span id='mfrh_progression'></span>
574
 
575
  <?php if ( get_option( 'mfrh_flagging' ) ): ?>
873
 
874
  $this->log( "** Rename Media: " . $old_filename );
875
 
 
 
 
 
 
 
 
876
  // If this is being renamed based on the post the media is attached to.
877
  $base_new_title = $post['post_title'];
878
  if ( !$force && $method == 'post_title' ) {
media-file-renamer.php CHANGED
@@ -3,7 +3,7 @@
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: 3.6.0
7
  Author: Jordy Meow
8
  Author URI: http://meowapps.com
9
  Text Domain: media-file-renamer
@@ -21,7 +21,7 @@ Originally developed for two of my websites:
21
  if ( is_admin() ) {
22
 
23
  global $mfrh_version;
24
- $mfrh_version = '3.6.0';
25
 
26
  // Admin
27
  require( 'mfrh_admin.php');
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: 3.6.4
7
  Author: Jordy Meow
8
  Author URI: http://meowapps.com
9
  Text Domain: media-file-renamer
21
  if ( is_admin() ) {
22
 
23
  global $mfrh_version;
24
+ $mfrh_version = '3.6.4';
25
 
26
  // Admin
27
  require( 'mfrh_admin.php');
mfrh_admin.php CHANGED
@@ -25,11 +25,23 @@ class Meow_MFRH_Admin extends MeowApps_Admin {
25
  $sync_alt = get_option( 'mfrh_sync_alt' );
26
  $sync_meta_title = get_option( 'mfrh_sync_media_title' );
27
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  if ( $sync_alt && $method == 'alt_text' ) {
29
  update_option( 'mfrh_sync_alt', false, false );
30
  ?>
31
  <div class="notice notice-warning is-dismissible">
32
- <p><?php _e( 'The option Sync ALT was turned off since it does not make sense to have it with this Auto-Rename mode.', 'media-file-renamer' ); ?></p>
33
  </div>
34
  <?php
35
  }
@@ -342,7 +354,7 @@ class Meow_MFRH_Admin extends MeowApps_Admin {
342
  }
343
 
344
  function admin_force_rename_callback( $args ) {
345
- $value = get_option( 'mfrh_force_rename', null );
346
  $html = '<input ' . disabled( $this->is_registered(), false, false ) . ' ' . disabled( $this->is_registered(), false, false ) . ' type="checkbox" id="mfrh_force_rename" name="mfrh_force_rename" value="1" ' .
347
  checked( 1, get_option( 'mfrh_force_rename' ), false ) . '/>';
348
  $html .= __( '<label>Enabled</label><br/><small>Update the references to the file even if the file renaming itself was not successful. You might want to use that option if your install is broken and you are trying to link your Media to files for which the filenames has been altered (after a migration for exemple)</small>', 'media-file-renamer' );
25
  $sync_alt = get_option( 'mfrh_sync_alt' );
26
  $sync_meta_title = get_option( 'mfrh_sync_media_title' );
27
 
28
+ $force_rename = get_option( 'mfrh_force_rename', false );
29
+ $numbered_files = get_option( 'mfrh_numbered_files', false );
30
+
31
+ if ( $force_rename && $numbered_files ) {
32
+ update_option( 'mfrh_force_rename', false, false );
33
+ ?>
34
+ <div class="notice notice-warning is-dismissible">
35
+ <p><?php _e( 'Force Rename and Numbered Files cannot be used at the same time. Please use Force Rename only when you are trying to repair a broken install. For now, Force Rename has been disabled.', 'media-file-renamer' ); ?></p>
36
+ </div>
37
+ <?php
38
+ }
39
+
40
  if ( $sync_alt && $method == 'alt_text' ) {
41
  update_option( 'mfrh_sync_alt', false, false );
42
  ?>
43
  <div class="notice notice-warning is-dismissible">
44
+ <p><?php _e( 'The option Sync ALT was turned off since it does not make sense to have it with this Auto-Rename mode.', 'media-file-renamer' ); ?></p>
45
  </div>
46
  <?php
47
  }
354
  }
355
 
356
  function admin_force_rename_callback( $args ) {
357
+ $value = get_option( 'mfrh_force_rename', false );
358
  $html = '<input ' . disabled( $this->is_registered(), false, false ) . ' ' . disabled( $this->is_registered(), false, false ) . ' type="checkbox" id="mfrh_force_rename" name="mfrh_force_rename" value="1" ' .
359
  checked( 1, get_option( 'mfrh_force_rename' ), false ) . '/>';
360
  $html .= __( '<label>Enabled</label><br/><small>Update the references to the file even if the file renaming itself was not successful. You might want to use that option if your install is broken and you are trying to link your Media to files for which the filenames has been altered (after a migration for exemple)</small>', 'media-file-renamer' );
plugins/wpml.php CHANGED
@@ -2,7 +2,7 @@
2
 
3
  // Thanks to David García from WPML
4
 
5
- add_action( 'mfrh_path_renamed', 'mfrh_wpml_update_translations', 10, 3 );
6
 
7
  function mfrh_wpml_update_translations( $post, $old_filepath, $new_filepath ) {
8
  $args = array('element_id' => $post['ID'], 'element_type' => 'attachment' );
@@ -11,10 +11,11 @@
11
  $translations = apply_filters( 'wpml_get_element_translations', NULL, $info->trid, 'post_attachment' );
12
  foreach ( $translations as $translation ) {
13
  if ( $post['ID'] != $translation->element_id ) {
14
- update_post_meta( $translation->element_id, '_wp_attached_file', $new_filepath );
 
 
 
15
  }
16
  }
17
  }
18
  }
19
-
20
- ?>
2
 
3
  // Thanks to David García from WPML
4
 
5
+ add_action( 'mfrh_media_renamed', 'mfrh_wpml_update_translations', 10, 3 );
6
 
7
  function mfrh_wpml_update_translations( $post, $old_filepath, $new_filepath ) {
8
  $args = array('element_id' => $post['ID'], 'element_type' => 'attachment' );
11
  $translations = apply_filters( 'wpml_get_element_translations', NULL, $info->trid, 'post_attachment' );
12
  foreach ( $translations as $translation ) {
13
  if ( $post['ID'] != $translation->element_id ) {
14
+ update_post_meta( $translation->element_id, '_wp_attached_file', get_post_meta( $post['ID'],
15
+ '_wp_attached_file', true ) );
16
+ update_post_meta( $translation->element_id, '_wp_attachment_metadata', get_post_meta( $post['ID'],
17
+ '_wp_attachment_metadata', true ) );
18
  }
19
  }
20
  }
21
  }
 
 
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: TigrouMeow
3
  Tags: rename, file, media, management, image, renamer, wpml, wp-retina-2x
4
  Requires at least: 4.2
5
  Tested up to: 4.8
6
- Stable tag: 3.6.0
7
 
8
  Automatically rename files depending on Media titles dynamically + update links. Pro version has many more options. Check the description :)
9
 
@@ -100,11 +100,15 @@ You are welcome to create plugins using Media File Renamer using special rules f
100
 
101
  == Changelog ==
102
 
 
 
 
 
103
  = 3.6.0 =
104
  * Fix: Compatibility with WPML.
105
  * Fix: There was a compatibility issue with retina.
106
 
107
- = 3.5.8 =s
108
  * Add: New button "Lock All"
109
  * Fix: Button "Unlock All and Rename" was not really unlocking everything.
110
 
3
  Tags: rename, file, media, management, image, renamer, wpml, wp-retina-2x
4
  Requires at least: 4.2
5
  Tested up to: 4.8
6
+ Stable tag: 3.6.4
7
 
8
  Automatically rename files depending on Media titles dynamically + update links. Pro version has many more options. Check the description :)
9
 
100
 
101
  == Changelog ==
102
 
103
+ = 3.6.4 =
104
+ * Add: Button "Undo All" to restore all the original filenames.
105
+ * Fix: Avoid the Numbered Files and Force Renamed options to be activated at the same time.
106
+
107
  = 3.6.0 =
108
  * Fix: Compatibility with WPML.
109
  * Fix: There was a compatibility issue with retina.
110
 
111
+ = 3.5.8 =
112
  * Add: New button "Lock All"
113
  * Fix: Button "Unlock All and Rename" was not really unlocking everything.
114