Media File Renamer - Version 4.0.0

Version Description

  • Update: Huge code cleaning and major refactorization. The core was also rewritten.
  • Add: Compatibility with Beaver Builder.
  • Fix: Avoid looking for too much perfection (which is dangerous) when using numbered files.
  • Fix: Works fine now with image sizes in the meta which has the... same size.
Download this release

Release Info

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

Code changes from version 3.7.2 to 4.0.0

Files changed (6) hide show
  1. common/admin.php +16 -3
  2. core.php +342 -308
  3. media-file-renamer.php +4 -3
  4. mfrh_admin.php +2 -2
  5. mfrh_custom.php +57 -7
  6. readme.txt +10 -4
common/admin.php CHANGED
@@ -191,9 +191,13 @@ if ( !class_exists( 'MeowApps_Admin' ) ) {
191
  add_settings_field( 'meowapps_hide_meowapps', "Main Menu",
192
  array( $this, 'meowapps_hide_dashboard_callback' ),
193
  'meowapps_common_settings-menu', 'meowapps_common_settings' );
 
 
 
194
  add_settings_field( 'meowapps_hide_ads', "Ads",
195
  array( $this, 'meowapps_hide_ads_callback' ),
196
  'meowapps_common_settings-menu', 'meowapps_common_settings' );
 
197
  register_setting( 'meowapps_common_settings', 'meowapps_hide_meowapps' );
198
  register_setting( 'meowapps_common_settings', 'meowapps_hide_ads' );
199
  }
@@ -210,7 +214,15 @@ if ( !class_exists( 'MeowApps_Admin' ) ) {
210
  $value = get_option( 'meowapps_hide_meowapps', null );
211
  $html = '<input type="checkbox" id="meowapps_hide_meowapps" name="meowapps_hide_meowapps" value="1" ' .
212
  checked( 1, get_option( 'meowapps_hide_meowapps' ), false ) . '/>';
213
- $html .= __( '<label>Hide <b>Meow Apps</b> Menu</label><br /><small>Hide Meow Apps menu and all its components, for a nicer an faster WordPress admin UI. An option will be added in Settings > General to display it again.</small>', 'meowapps' );
 
 
 
 
 
 
 
 
214
  echo $html;
215
  }
216
 
@@ -233,9 +245,10 @@ if ( !class_exists( 'MeowApps_Admin' ) ) {
233
 
234
  function check_install( $plugin ) {
235
  $pro = false;
236
- $pluginpath = get_home_path() . 'wp-content/plugins/' . $plugin . '-pro';
 
237
  if ( !file_exists( $pluginpath ) ) {
238
- $pluginpath = get_home_path() . 'wp-content/plugins/' . $plugin;
239
  if ( !file_exists( $pluginpath ) ) {
240
  $url = wp_nonce_url( "update.php?action=install-plugin&plugin=$plugin", "install-plugin_$plugin" );
241
  return "<a href='$url'><small><span class='' style='float: right;'>install</span></small></a>";
191
  add_settings_field( 'meowapps_hide_meowapps', "Main Menu",
192
  array( $this, 'meowapps_hide_dashboard_callback' ),
193
  'meowapps_common_settings-menu', 'meowapps_common_settings' );
194
+ add_settings_field( 'meowapps_force_sslverify', "SSL Verify",
195
+ array( $this, 'meowapps_force_sslverify_callback' ),
196
+ 'meowapps_common_settings-menu', 'meowapps_common_settings' );
197
  add_settings_field( 'meowapps_hide_ads', "Ads",
198
  array( $this, 'meowapps_hide_ads_callback' ),
199
  'meowapps_common_settings-menu', 'meowapps_common_settings' );
200
+ register_setting( 'meowapps_common_settings', 'force_sslverify' );
201
  register_setting( 'meowapps_common_settings', 'meowapps_hide_meowapps' );
202
  register_setting( 'meowapps_common_settings', 'meowapps_hide_ads' );
203
  }
214
  $value = get_option( 'meowapps_hide_meowapps', null );
215
  $html = '<input type="checkbox" id="meowapps_hide_meowapps" name="meowapps_hide_meowapps" value="1" ' .
216
  checked( 1, get_option( 'meowapps_hide_meowapps' ), false ) . '/>';
217
+ $html .= __( '<label>Hide <b>Meow Apps</b> Menu</label><br /><small>Hide Meow Apps menu and all its components, for a cleaner admin. This option will be reset if a new Meow Apps plugin is installed. <b>Once activated, an option will be added in your General settings to display it again.</b></small>', 'meowapps' );
218
+ echo $html;
219
+ }
220
+
221
+ function meowapps_force_sslverify_callback() {
222
+ $value = get_option( 'force_sslverify', null );
223
+ $html = '<input type="checkbox" id="force_sslverify" name="force_sslverify" value="1" ' .
224
+ checked( 1, get_option( 'force_sslverify' ), false ) . '/>';
225
+ $html .= __( '<label>Force</label><br /><small>Updates and licenses checks are usually made without checking SSL certificates and it is actually fine this way. But if you are intransigent when it comes to SSL matters, this option will force it.</small>', 'meowapps' );
226
  echo $html;
227
  }
228
 
245
 
246
  function check_install( $plugin ) {
247
  $pro = false;
248
+
249
+ $pluginpath = trailingslashit( plugin_dir_path( __FILE__ ) ) . '../../' . $plugin . '-pro';
250
  if ( !file_exists( $pluginpath ) ) {
251
+ $pluginpath = trailingslashit( plugin_dir_path( __FILE__ ) ) . '../../' . $plugin;
252
  if ( !file_exists( $pluginpath ) ) {
253
  $url = wp_nonce_url( "update.php?action=install-plugin&plugin=$plugin", "install-plugin_$plugin" );
254
  return "<a href='$url'><small><span class='' style='float: right;'>install</span></small></a>";
core.php CHANGED
@@ -14,11 +14,10 @@ class Meow_MFRH_Core {
14
  }
15
 
16
  function init() {
17
-
18
  include( 'mfrh_custom.php' );
19
  include( 'api.php' );
20
 
21
- global $mfrh_version;
22
  load_plugin_textdomain( 'media-file-renamer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
23
 
24
  add_action( 'admin_head', array( $this, 'admin_head' ) );
@@ -27,14 +26,15 @@ class Meow_MFRH_Core {
27
  add_action( 'wp_ajax_mfrh_undo_media', array( $this, 'wp_ajax_mfrh_undo_media' ) );
28
  add_filter( 'media_send_to_editor', array( $this, 'media_send_to_editor' ), 20, 3 );
29
  add_action( 'admin_notices', array( $this, 'admin_notices' ) );
30
- add_action( 'edit_attachment', array( $this, 'edit_attachment' ) );
31
- add_action( 'add_attachment', array( $this, 'edit_attachment' ) );
32
  add_action( 'add_meta_boxes', array( $this, 'add_rename_metabox' ) );
33
- add_filter( 'attachment_fields_to_save', array( $this, 'attachment_save' ), 20, 2 );
34
  add_action( 'save_post', array( $this, 'save_post' ) );
35
 
36
- if ( get_option( 'mfrh_on_upload', false ) )
37
  add_filter( 'wp_handle_upload_prefilter', array( $this, 'wp_handle_upload_prefilter' ), 10, 2 );
 
38
 
39
  // Column for Media Library
40
  $method = apply_filters( 'mfrh_method', 'media_title' );
@@ -68,15 +68,15 @@ class Meow_MFRH_Core {
68
  $screen = get_current_screen();
69
  if ( ( $screen->base == 'post' && $screen->post_type == 'attachment' ) ||
70
  ( $screen->base == 'media' && isset( $_GET['attachment_id'] ) ) ) {
71
- $attachmentId = isset( $_GET['post'] ) ? $_GET['post'] : $_GET['attachment_id'];
72
- if ( $this->check_attachment( $attachmentId, $output ) ) {
73
  if ( $output['desired_filename_exists'] ) {
74
  echo '<div class="error"><p>
75
  The file ' . $output['desired_filename'] . ' already exists. Please give a new title for this media.
76
  </p></div>';
77
  }
78
  }
79
- if ( $this->wpml_media_is_installed() && !$this->is_real_media( $attachmentId ) ) {
80
  echo '<div class="error"><p>
81
  This attachment seems to be a virtual copy (or translation). Media File Renamer will not make any modification from here.
82
  </p></div>';
@@ -84,6 +84,45 @@ class Meow_MFRH_Core {
84
  }
85
  }
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  /**
88
  *
89
  * MEDIA LIBRARY
@@ -91,28 +130,28 @@ class Meow_MFRH_Core {
91
  */
92
 
93
  function library_bulk_actions( $bulk_actions ) {
94
- $bulk_actions['mfrh_lock_all'] = __( 'Lock (Renamer)', 'media-file-renamer');
95
  $bulk_actions['mfrh_unlock_all'] = __( 'Unlock (Renamer)', 'media-file-renamer');
96
  $bulk_actions['mfrh_rename_all'] = __( 'Rename (Renamer)', 'media-file-renamer');
97
- return $bulk_actions;
98
  }
99
 
100
  function library_bulk_actions_handler( $redirect_to, $doaction, $ids ) {
101
  if ( $doaction == 'mfrh_lock_all' ) {
102
  foreach ( $ids as $post_id ) {
103
- add_post_meta( $post_id, '_manual_file_renaming', true, true );
104
- }
105
- }
106
  if ( $doaction == 'mfrh_unlock_all' ) {
107
  foreach ( $ids as $post_id ) {
108
- delete_post_meta( $post_id, '_manual_file_renaming' );
109
- }
110
- }
111
  if ( $doaction == 'mfrh_rename_all' ) {
112
  foreach ( $ids as $post_id ) {
113
- $this->rename_media( get_post( $post_id, ARRAY_A ), null );
114
- }
115
- }
116
  return $redirect_to;
117
  }
118
 
@@ -129,34 +168,39 @@ class Meow_MFRH_Core {
129
 
130
  function manage_media_custom_column( $column_name, $id ) {
131
  $paged = isset( $_GET['paged'] ) ? ( '&paged=' . $_GET['paged'] ) : "";
132
- if ( $column_name == 'mfrh_column' ) {
133
- $check = $this->check_attachment( $id, $output );
134
 
135
- $original_filename = get_post_meta( $id, '_original_filename', true );
 
 
 
 
 
 
 
136
 
137
- if ( $check ) {
138
- $this->generate_explanation( $output );
139
- }
140
- else if ( isset( $output['manual'] ) && $output['manual'] ) {
141
- echo "<span title='" . __( 'Manually renamed.', 'media-file-renamer' ) . "' style='font-size: 24px; color: #36B15C;' class='dashicons dashicons-yes'></span>";
142
- $page = isset( $_GET['page'] ) ? ( '&page=' . $_GET['page'] ) : "";
143
- 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>";
144
- }
145
- else {
146
- echo "<span title='" . __( 'Automatically renamed.', 'media-file-renamer' ) . "'style='font-size: 24px; color: #36B15C;' class='dashicons dashicons-yes'></span>";
147
- $page = isset( $_GET['page'] ) ? ( '&page=' . $_GET['page'] ) : "";
148
- if ( get_option( 'mfrh_undo', false ) && !empty( $original_filename ) ) {
149
- 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>";
150
- }
151
- 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>";
152
- }
153
  }
 
 
 
 
 
 
 
 
 
154
  }
155
 
156
  function admin_head() {
157
  if ( !empty( $_GET['mfrh_rename'] ) ) {
158
  $mfrh_rename = $_GET['mfrh_rename'];
159
- $this->rename_media( get_post( $mfrh_rename, ARRAY_A ), null );
160
  $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'mfrh_rename' ), $_SERVER['REQUEST_URI'] );
161
  }
162
  if ( !empty( $_GET['mfrh_unlock'] ) ) {
@@ -168,7 +212,7 @@ class Meow_MFRH_Core {
168
  $mfrh_undo = $_GET['mfrh_undo'];
169
  $original_filename = get_post_meta( $mfrh_undo, '_original_filename', true );
170
  $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'mfrh_undo' ), $_SERVER['REQUEST_URI'] );
171
- $this->rename_media( get_post( $mfrh_undo, ARRAY_A ), null, false, $original_filename );
172
 
173
  $fp = get_attached_file( $mfrh_undo );
174
  $path_parts = pathinfo( $fp );
@@ -307,7 +351,7 @@ class Meow_MFRH_Core {
307
  }
308
  else if ( $subaction == 'renameMediaId' ) {
309
  $id = intval( $_POST['id'] );
310
- $this->rename_media( get_post( $id, ARRAY_A ), null );
311
  echo 1;
312
  die();
313
  }
@@ -331,7 +375,7 @@ class Meow_MFRH_Core {
331
  else if ( $subaction == 'undoMediaId' ) {
332
  $id = intval( $_POST['id'] );
333
  $original_filename = get_post_meta( $id, '_original_filename', true );
334
- $this->rename_media( get_post( $id, ARRAY_A ), null, false, $original_filename );
335
  echo 1;
336
  die();
337
  }
@@ -402,6 +446,7 @@ class Meow_MFRH_Core {
402
 
403
  function wp_handle_upload_prefilter( $file ) {
404
 
 
405
  $pp = pathinfo( $file['name'] );
406
 
407
  // If everything's fine, renames in based on the Title in the EXIF
@@ -410,6 +455,7 @@ class Meow_MFRH_Core {
410
  $exif = wp_read_image_metadata( $file['tmp_name'] );
411
  if ( !empty( $exif ) && isset( $exif[ 'title' ] ) && !empty( $exif[ 'title' ] ) ) {
412
  $file['name'] = $this->new_filename( null, $exif[ 'title' ] ) . '.' . $pp['extension'];
 
413
  return $file;
414
  }
415
  }
@@ -417,6 +463,7 @@ class Meow_MFRH_Core {
417
  $post = get_post( $_POST['post_id'] );
418
  if ( !empty( $post ) && !empty( $post->post_title ) ) {
419
  $file['name'] = $this->new_filename( null, $post->post_title ) . '.' . $pp['extension'];
 
420
  return $file;
421
  }
422
  }
@@ -445,83 +492,109 @@ class Meow_MFRH_Core {
445
  /****************************************************************************/
446
 
447
  // Return false if everything is fine, otherwise return true with an output.
448
- function check_attachment( $id, &$output = array() ) {
449
- $method = apply_filters( 'mfrh_method', 'media_title' );
450
- if ( $method === 'none') {
 
 
 
 
 
 
 
451
  delete_post_meta( $id, '_require_file_renaming' );
452
  return false;
453
  }
454
- if ( get_post_meta( $id, '_manual_file_renaming', true ) ) {
455
- $output['manual'] = true;
456
- return false;
 
 
 
457
  }
458
 
459
- // Skip header images
460
- if ( $this->is_header_image( $id ) ) {
461
- delete_post_meta( $id, '_require_file_renaming' );
462
- return false;
463
  }
 
 
 
 
 
 
 
 
 
464
 
465
- // Get information
466
- $post = get_post( $id, ARRAY_A );
467
- $base_title = $post['post_title'];
468
- if ( $method == 'post_title' ) {
469
- $attachedpost = $this->get_post_from_media( $post['ID'] );
470
- if ( is_null( $attachedpost ) )
471
  return false;
472
- $base_title = $attachedpost->post_title;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
473
  }
474
- else if ( $method == 'alt_text' ) {
475
- $image_alt = get_post_meta( $post['ID'], '_wp_attachment_image_alt', true );
476
- if ( is_null( $image_alt ) )
 
 
 
477
  return false;
478
- $base_title = $image_alt;
479
- }
480
- $desired_filename = $this->new_filename( $post, $base_title );
481
- $old_filepath = get_attached_file( $post['ID'] );
482
- $path_parts = pathinfo( $old_filepath );
483
-
484
- // Dead file, let's forget it!
485
- if ( !file_exists( $old_filepath ) ) {
486
- delete_post_meta( $id, '_require_file_renaming' );
487
- return false;
488
  }
489
 
490
  // Filename is equal to sanitized title
491
- if ( $desired_filename == $path_parts['basename'] ) {
492
  delete_post_meta( $id, '_require_file_renaming' );
493
  return false;
494
  }
495
 
496
- // Send info to the requester function
497
- $output['post_id'] = $post['ID'];
498
- $output['post_name'] = $post['post_name'];
499
- $output['post_title'] = $post['post_title'];
500
- $output['current_filename'] = $path_parts['basename'];
501
- $output['desired_filename'] = $desired_filename;
502
- $output['desired_filename_exists'] = false;
503
- if ( file_exists( $path_parts['dirname'] . "/" . $desired_filename ) ) {
504
  $is_numbered = apply_filters( 'mfrh_numbered', false );
505
  if ( $is_numbered ) {
506
- $output['desired_filename'] = $this->generate_unique_filename( $path_parts['basename'],
507
- $path_parts['dirname'], $desired_filename );
508
- if ( $output['desired_filename'] == false ) {
509
  delete_post_meta( $id, '_require_file_renaming' );
510
  return false;
511
  }
512
- //TODO: Why was this done...
513
- //add_post_meta( $post['ID'], '_numbered_filename', $output['desired_filename'], true );
514
- }
515
- else {
516
- $output['desired_filename_exists'] = true;
517
- if ( strtolower( $output['current_filename'] ) == strtolower( $output['desired_filename'] ) ) {
518
- // If Windows, let's be careful about the fact that case doesn't affect files
519
- delete_post_meta( $post['ID'], '_require_file_renaming' );
520
- return false;
521
- }
522
  }
523
  }
524
 
 
 
 
 
 
 
 
 
 
 
 
 
 
525
  // It seems it could be renamed :)
526
  if ( !get_post_meta( $post['ID'], '_require_file_renaming' ) ) {
527
  add_post_meta( $post['ID'], '_require_file_renaming', true, true );
@@ -539,7 +612,7 @@ class Meow_MFRH_Core {
539
  AND post_type = 'attachment'
540
  " );
541
  foreach ( $ids as $id )
542
- if ( $this->check_attachment( $id, $output ) )
543
  array_push( $issues, $output );
544
  return $issues;
545
  }
@@ -548,11 +621,13 @@ class Meow_MFRH_Core {
548
 
549
  static $previous = array();
550
 
 
 
551
  if ( $file['post_title'] == "" ) {
552
  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>";
553
  }
554
  else if ( $file['desired_filename_exists'] ) {
555
- echo "<a class='button-primary' href='post.php?post=" . $file['post_id'] . "&action=edit'>" . __( 'Edit Media', 'media-file-renamer' ) . "</a><br /><small>" . __( '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' ) . "</small>";
556
  }
557
  else {
558
  $page = isset( $_GET['page'] ) ? ( '&page=' . $_GET['page'] ) : "";
@@ -570,11 +645,21 @@ class Meow_MFRH_Core {
570
  echo "<a class='button-primary' href='?" . $page . $mfrh_scancheck . $mfrh_to_rename . "&mfrh_rename=" . $file['post_id'] . "'>" . __( 'Auto-Rename', 'media-file-renamer' ) . "</a>";
571
  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>";
572
 
573
- echo "<br /><small style='line-height: 8px;'>" .
574
- sprintf( __( 'Rename to %s. You can also <a href="%s">edit this media</a>.', 'media-file-renamer' ), $file['desired_filename'], $modify_url ) . "</small>";
 
 
 
 
 
 
 
 
575
 
576
  if ( !$isNew ) {
577
- echo __( '<small style=\'line-height: 8px;\'>The first media you rename will actually get this filename; the next will be either not renamed or will have a counter behind it.</small>', 'media-file-renamer' );
 
 
578
  }
579
  }
580
  }
@@ -765,27 +850,23 @@ class Meow_MFRH_Core {
765
  * Added and modified by Jordy Meow
766
  */
767
 
768
- function rename_media_on_publish ( $post_id ) {
769
- $onsave = get_option( "mfrh_rename_on_save" );
770
- $args = array( 'post_type' => 'attachment',
771
- 'numberposts' => -1, 'post_status' =>'any', 'post_parent' => $post_id );
772
- $attachments = get_posts( $args );
773
- if ( $attachments ) {
774
- foreach ( $attachments as $attachment ) {
775
- $attachment = get_post( $attachment, ARRAY_A );
776
- $this->check_attachment( $attachment['ID'] );
777
- if ( $onsave ) {
778
- $this->rename_media( $attachment, $attachment, true );
779
- }
780
- }
781
- }
782
- }
783
-
784
  function save_post( $post_id ) {
785
  $status = get_post_status( $post_id );
786
  if ( !in_array( $status, array( 'publish', 'draft', 'future', 'private' ) ) )
787
  return;
788
- $this->rename_media_on_publish( $post_id );
 
 
 
 
 
 
 
 
 
 
 
 
789
  }
790
 
791
  /**
@@ -794,14 +875,14 @@ class Meow_MFRH_Core {
794
  *
795
  */
796
 
797
- function edit_attachment( $post_ID ) {
798
- $this->log( '[edit_attachment]' );
799
- $this->check_attachment( $post_ID, $output );
800
- // $output['post_name']
801
  }
802
 
803
- function media_send_to_editor( $html, $attachment_id, $attachment ) {
804
- $this->check_attachment( $attachment_id, $output );
805
  return $html;
806
  }
807
 
@@ -819,27 +900,6 @@ class Meow_MFRH_Core {
819
  return $post;
820
  }
821
 
822
- function attachment_save( $post, $attachment ) {
823
- $this->log( '[attachment_save]' );
824
- $method = apply_filters( 'mfrh_method', 'media_title' );
825
- $info = pathinfo( get_attached_file( $post['ID'] ) );
826
- $basename = $info['basename'];
827
- $new = $post['mfrh_new_filename'];
828
-
829
- // The filename is being changed manually, let's force it through $new.
830
- if ( !empty( $new ) && $basename !== $new )
831
- return $this->rename_media( $post, $attachment, false, $new );
832
-
833
- $method = apply_filters( 'mfrh_method', 'media_title' );
834
- if ( $method == 'media_title' ) {
835
- // If the title was not changed, don't do anything.
836
- if ( get_the_title( $post['ID'] ) == $post['post_title'] )
837
- return $post;
838
- return $this->rename_media( $post, $attachment, false, null );
839
- }
840
- return $post;
841
- }
842
-
843
  function log_sql( $data, $antidata ) {
844
  if ( !get_option( 'mfrh_logsql' ) || !$this->mfrh_admin->is_registered() )
845
  return;
@@ -870,8 +930,8 @@ class Meow_MFRH_Core {
870
  *
871
  */
872
 
873
- function replace_special_chars( $str ) {
874
- $special_chars = array(
875
  "å" => "a", "Å" => "a",
876
  "ä" => "ae", "Ä" => "ae",
877
  "ö" => "oe", "Ö" => "oe",
@@ -880,20 +940,20 @@ class Meow_MFRH_Core {
880
  );
881
  foreach ( $special_chars as $key => $value )
882
  $str = str_replace( $key, $value, $str );
883
- return $str;
884
- }
885
 
886
  function replace_chars( $str ) {
887
- $special_chars = array();
888
- $special_chars = apply_filters( 'mfrh_replace_rules', $special_chars );
889
- if ( !empty( $special_chars ) )
890
- foreach ( $special_chars as $key => $value )
891
- $str = str_replace( $key, $value, $str );
892
- return $str;
893
- }
894
 
895
  // NEW MEDIA FILE INFO (depending on the text/filename of the media)
896
- function new_filename( $media, $text, $forceFilename = null ) {
897
 
898
  $old_filename = null;
899
  $new_ext = null;
@@ -913,24 +973,24 @@ class Meow_MFRH_Core {
913
  }
914
 
915
  // Generate the new filename.
916
- if ( !empty( $forceFilename ) ) {
917
  // Filename is forced. Strip the extension. Keeps this extension in $new_ext.
918
- $pp = pathinfo( $forceFilename );
919
- $forceFilename = $pp['filename'];
920
  $new_ext = empty( $pp['extension'] ) ? $new_ext : $pp['extension'];
921
- $new_filename = $forceFilename;
922
  }
923
  else {
924
  // Filename is generated from $text, without an extension.
925
  $text = str_replace( ".jpg", "", $text );
926
  $text = str_replace( ".png", "", $text );
927
  $text = str_replace( "'", "-", $text );
928
- $text = strtolower( $this->replace_chars( $text ) );
929
  $utf8_filename = apply_filters( 'mfrh_utf8', false );
930
  if ( $utf8_filename )
931
  $new_filename = sanitize_file_name( $text );
932
  else
933
- $new_filename = str_replace( "%", "-", sanitize_title( $this->replace_special_chars( $text ) ) );
934
  }
935
  if ( empty( $new_filename ) )
936
  $new_filename = "empty";
@@ -938,7 +998,7 @@ class Meow_MFRH_Core {
938
  // We know have a new filename, let's add an extension.
939
  $new_filename = !empty( $new_ext ) ? ( $new_filename . '.' . $new_ext ) : $new_filename;
940
 
941
- if ( !$forceFilename )
942
  $new_filename = apply_filters( 'mfrh_new_filename', $new_filename, $old_filename, $media );
943
  return $new_filename;
944
  }
@@ -946,9 +1006,8 @@ class Meow_MFRH_Core {
946
  // Only replace the first occurence
947
  function str_replace( $needle, $replace, $haystack ) {
948
  $pos = strpos( $haystack, $needle );
949
- if ( $pos !== false ) {
950
- $haystack = substr_replace( $haystack, $replace, $pos, strlen( $needle ) );
951
- }
952
  return $haystack;
953
  }
954
 
@@ -967,151 +1026,93 @@ class Meow_MFRH_Core {
967
  str_replace( $upload_dir, "", $new_image_url ) );
968
  }
969
 
970
- function rename( $mediaId ) {
971
- return $this->rename_media( get_post( $mediaId, ARRAY_A ) );
972
- }
973
-
974
- function rename_media( $post, $attachment, $disableMediaLibraryMode = false, $forceFilename = null ) {
975
- $force = !empty( $forceFilename );
976
- $manual = get_post_meta( $post['ID'], '_manual_file_renaming', true );
977
 
978
- if ( $manual && !$forceFilename )
979
- return $post;
 
 
 
 
 
 
 
 
 
 
980
 
981
- $require = get_post_meta( $post['ID'], '_require_file_renaming', false );
982
  $method = apply_filters( 'mfrh_method', 'media_title' );
983
 
984
- $numbered_filename = get_post_meta( $post['ID'], '_numbered_filename', true );
985
- if ( !empty( $numbered_filename ) ) {
986
- // Not clear anymore why this is here, but it wouldn't cause any issue anyway.
987
- delete_post_meta( $post['ID'], '_numbered_filename' );
 
988
  }
989
 
990
- // MEDIA TITLE & FILE PARTS
991
- $meta = wp_get_attachment_metadata( $post['ID'] );
992
- $old_filepath = get_attached_file( $post['ID'] ); // '2011/01/whatever.jpeg'
 
 
 
993
  $path_parts = pathinfo( $old_filepath );
994
  $directory = $path_parts['dirname']; // '2011/01'
995
  $old_filename = $path_parts['basename']; // 'whatever.jpeg'
996
- $old_ext = $path_parts['extension'];
997
 
998
  $this->log( "** Rename Media: " . $old_filename );
 
999
 
1000
- // If this is being renamed based on the post the media is attached to.
1001
- $base_new_title = $post['post_title'];
1002
- if ( !$force && $method == 'post_title' ) {
1003
- $linkedpost = $this->get_post_from_media( $post['ID'] );
1004
- if ( empty( $linkedpost ) ) {
1005
- $this->log( "Attachment {$post['ID']} is not linked to a post yet it seems." );
1006
- delete_post_meta( $post['ID'], '_require_file_renaming' );
1007
- return $post;
1008
- }
1009
- $base_new_title = $linkedpost->post_title;
1010
- }
1011
- else if ( !$force && $method == 'alt_text' ) {
1012
- $image_alt = get_post_meta( $post['ID'], '_wp_attachment_image_alt', true );
1013
- if ( empty( $image_alt ) ) {
1014
- $this->log( "Attachment {$post['ID']} has no alternative text it seems." );
1015
- delete_post_meta( $post['ID'], '_require_file_renaming' );
1016
- return $post;
1017
- }
1018
- $base_new_title = $image_alt;
1019
- }
1020
-
1021
- // Empty post title when renaming using title? Let's not go further.
1022
- if ( !$force && empty( $base_new_title ) ) {
1023
- $this->log( "Title is empty, doesn't rename." );
1024
- delete_post_meta( $post['ID'], '_require_file_renaming' );
1025
- return $post;
1026
- }
1027
-
1028
- // Is it a header image? Skip.
1029
- if ( $this->is_header_image( $post['ID'] ) ) {
1030
- $this->log( "Doesn't rename header image." );
1031
- delete_post_meta( $post['ID'], '_require_file_renaming' );
1032
- return $post;
1033
- }
1034
- if ( $manual && !$this->mfrh_admin->is_registered() ) {
1035
- return $post;
1036
- }
1037
-
1038
- delete_post_meta( $post['ID'], '_manual_file_renaming' );
1039
- $sanitized_media_title = $this->new_filename( $post, $base_new_title, $forceFilename );
1040
- $this->log( "New file should be: " . $sanitized_media_title );
1041
-
1042
- // Don't do anything if the media title didn't change or if it would turn to an empty string
1043
- if ( $path_parts['basename'] == $sanitized_media_title ) {
1044
- $this->log( "File seems renamed already." );
1045
- delete_post_meta( $post['ID'], '_require_file_renaming' );
1046
  return $post;
1047
  }
1048
-
1049
- // MEDIA LIBRARY USAGE DETECTION
1050
- // Detects if the user is using the Media Library or 'Add an Image' (while a post edit)
1051
- // If it is not the Media Library, we don't rename, to avoid issues
1052
- $media_library_mode = !isset( $attachment['image-size'] ) || $disableMediaLibraryMode;
1053
- if ( !$media_library_mode ) {
1054
- // This media requires renaming
1055
- if ( !get_post_meta( $post['ID'], '_require_file_renaming' ) )
1056
- add_post_meta( $post['ID'], '_require_file_renaming', true, true );
1057
- $this->log( "Seems like the user is editing a post. Marked the file as to be renamed." );
1058
  return $post;
1059
  }
1060
 
1061
- // NEW DESTINATION FILES ALREADY EXISTS - WE DON'T DO NOTHING
1062
- $force_rename = apply_filters( 'mfrh_force_rename', false );
1063
- $new_filepath = trailingslashit( $directory ) . $sanitized_media_title;
1064
- if ( !$force_rename && file_exists( $directory . "/" . $sanitized_media_title ) ) {
1065
- $desired = false;
1066
- $is_numbered = apply_filters( 'mfrh_numbered', false );
1067
- if ( $is_numbered ) {
1068
- $desired = $this->generate_unique_filename( $old_filename,
1069
- $path_parts['dirname'], $sanitized_media_title );
1070
- }
1071
- if ( $desired != false ) {
1072
- $this->log( "Seems like $sanitized_media_title could be numbered as $desired." );
1073
- $new_filepath = trailingslashit( $directory ) . $desired;
1074
- $sanitized_media_title = $desired;
1075
- }
1076
- else {
1077
- if ( !get_post_meta( $post['ID'], '_require_file_renaming' ) )
1078
- add_post_meta( $post['ID'], '_require_file_renaming', true, true );
1079
- $this->log( "The new file already exists ($new_filepath), it is safer to avoid doing anything." );
1080
- return $post;
1081
- }
1082
- }
1083
-
1084
- // Exact same code as rename-media, it's a good idea to keep track of the original filename.
1085
- $original_filename = get_post_meta( $post['ID'], '_original_filename', true );
1086
  if ( empty( $original_filename ) )
1087
- add_post_meta( $post['ID'], '_original_filename', $old_filename, true );
1088
 
1089
  // Rename the main media file.
1090
  try {
1091
- if ( ( !file_exists( $old_filepath ) || !rename( $old_filepath, $new_filepath ) ) && !$force_rename ) {
 
 
 
 
1092
  $this->log( "The file couldn't be renamed from $old_filepath to $new_filepath." );
1093
  return $post;
1094
  }
1095
  $this->log( "File $old_filepath renamed to $new_filepath." );
1096
  do_action( 'mfrh_path_renamed', $post, $old_filepath, $new_filepath );
1097
  }
1098
- catch (Exception $e) {
 
1099
  return $post;
1100
  }
1101
 
1102
  // The new extension (or maybe it's just the old one)
 
1103
  $new_ext = $old_ext;
1104
- if ( $forceFilename ) {
1105
- $pp = pathinfo( $forceFilename );
1106
  $new_ext = $pp['extension'];
1107
  }
1108
 
1109
  // Filenames without extensions
1110
  $noext_old_filename = $this->str_replace( '.' . $old_ext, '', $old_filename );
1111
- $noext_new_filename = $this->str_replace( '.' . $old_ext, '', $sanitized_media_title );
1112
- $this->log( "Files with no extensions: $noext_old_filename and $noext_new_filename." );
1113
 
1114
  // Update the attachment meta
 
1115
  if ( $meta ) {
1116
  if ( isset( $meta['file'] ) && !empty( $meta['file'] ) )
1117
  $meta['file'] = $this->str_replace( $noext_old_filename, $noext_new_filename, $meta['file'] );
@@ -1122,18 +1123,18 @@ class Meow_MFRH_Core {
1122
  }
1123
 
1124
  // Images
1125
- if ( wp_attachment_is_image( $post['ID'] ) ) {
1126
  // Loop through the different sizes in the case of an image, and rename them.
1127
  $orig_image_urls = array();
1128
- $orig_image_data = wp_get_attachment_image_src( $post['ID'], 'full' );
1129
  $orig_image_urls['full'] = $orig_image_data[0];
1130
  if ( empty( $meta['sizes'] ) ) {
1131
- $this->log( "The WP metadata for attachment " . $post['ID'] . " does not exist.", true );
1132
  }
1133
  else {
1134
  foreach ( $meta['sizes'] as $size => $meta_size ) {
1135
  if ( !isset($meta['sizes'][$size]['file'] ) )
1136
- continue;
1137
  $meta_old_filename = $meta['sizes'][$size]['file'];
1138
  $meta_old_filepath = trailingslashit( $directory ) . $meta_old_filename;
1139
  $meta_new_filename = $this->str_replace( $noext_old_filename, $noext_new_filename, $meta_old_filename );
@@ -1144,63 +1145,101 @@ class Meow_MFRH_Core {
1144
  }
1145
 
1146
  $meta_new_filepath = trailingslashit( $directory ) . $meta_new_filename;
1147
- $orig_image_data = wp_get_attachment_image_src( $post['ID'], $size );
1148
  $orig_image_urls[$size] = $orig_image_data[0];
1149
- // ak: Double check files exist before trying to rename.
1150
- if ( $force_rename || ( file_exists( $meta_old_filepath ) && ( ( !file_exists( $meta_new_filepath ) )
1151
- || is_writable( $meta_new_filepath ) ) ) ) {
 
1152
  // WP Retina 2x is detected, let's rename those files as well
1153
  if ( function_exists( 'wr2x_get_retina' ) ) {
1154
  $wr2x_old_filepath = $this->str_replace( '.' . $old_ext, '@2x.' . $old_ext, $meta_old_filepath );
1155
  $wr2x_new_filepath = $this->str_replace( '.' . $new_ext, '@2x.' . $new_ext, $meta_new_filepath );
1156
  if ( file_exists( $wr2x_old_filepath ) && ( (!file_exists( $wr2x_new_filepath ) ) || is_writable( $wr2x_new_filepath ) ) ) {
1157
- @rename( $wr2x_old_filepath, $wr2x_new_filepath );
1158
- $this->log( "Retina file $wr2x_old_filepath renamed to $wr2x_new_filepath." );
1159
- do_action( 'mfrh_path_renamed', $post, $wr2x_old_filepath, $wr2x_new_filepath );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1160
  }
 
 
 
 
 
 
 
1161
  }
1162
- @rename( $meta_old_filepath, $meta_new_filepath );
1163
- $meta['sizes'][$size]['file'] = $meta_new_filename;
1164
- $this->log( "File $meta_old_filepath renamed to $meta_new_filepath." );
1165
- do_action( 'mfrh_path_renamed', $post, $meta_old_filepath, $meta_new_filepath );
1166
  }
1167
  }
1168
  }
1169
  }
1170
  else {
1171
- $orig_attachment_url = wp_get_attachment_url( $post['ID'] );
1172
  }
1173
 
1174
  // This media doesn't require renaming anymore
1175
- delete_post_meta( $post['ID'], '_require_file_renaming' );
1176
- if ( $force ) {
1177
- add_post_meta( $post['ID'], '_manual_file_renaming', true, true );
1178
- }
 
1179
 
1180
  // Update metadata
1181
  if ( $meta )
1182
- wp_update_attachment_metadata( $post['ID'], $meta );
1183
- update_attached_file( $post['ID'], $new_filepath );
1184
- clean_post_cache( $post['ID'] );
1185
 
1186
  // Call the actions so that the plugin's plugins can update everything else (than the files)
1187
- if ( wp_attachment_is_image( $post['ID'] ) ) {
1188
  $orig_image_url = $orig_image_urls['full'];
1189
- $new_image_data = wp_get_attachment_image_src( $post['ID'], 'full' );
1190
  $new_image_url = $new_image_data[0];
1191
  $this->call_hooks_rename_url( $post, $orig_image_url, $new_image_url );
1192
 
1193
  if ( !empty( $meta['sizes'] ) ) {
1194
  foreach ( $meta['sizes'] as $size => $meta_size ) {
1195
  $orig_image_url = $orig_image_urls[$size];
1196
- $new_image_data = wp_get_attachment_image_src( $post['ID'], $size );
1197
  $new_image_url = $new_image_data[0];
1198
  $this->call_hooks_rename_url( $post, $orig_image_url, $new_image_url );
1199
  }
1200
  }
1201
  }
1202
  else {
1203
- $new_attachment_url = wp_get_attachment_url( $post['ID'] );
1204
  $this->call_hooks_rename_url( $post, $orig_attachment_url, $new_attachment_url );
1205
  }
1206
 
@@ -1210,13 +1249,13 @@ class Meow_MFRH_Core {
1210
  $info = pathinfo( $new_filepath );
1211
  $newslug = preg_replace( '/\\.[^.\\s]{3,4}$/', '', $info['basename'] );
1212
  $post['post_name'] = $newslug;
1213
- $this->log( "Slug $oldslug set to $newslug." );
1214
- wp_update_post( $post );
1215
  }
1216
 
1217
  // HTTP REFERER set to the new media link
1218
  if ( isset( $_REQUEST['_wp_original_http_referer'] ) && strpos( $_REQUEST['_wp_original_http_referer'], '/wp-admin/' ) === false ) {
1219
- $_REQUEST['_wp_original_http_referer'] = get_permalink( $post['ID'] );
1220
  }
1221
 
1222
  do_action( 'mfrh_media_renamed', $post, $old_filepath, $new_filepath );
@@ -1241,10 +1280,8 @@ class Meow_MFRH_Core {
1241
  add_action( 'mfrh_url_renamed', array( $this, 'action_update_posts' ), 10, 3 );
1242
  if ( get_option( "mfrh_update_postmeta", true ) )
1243
  add_action( 'mfrh_url_renamed', array( $this, 'action_update_postmeta' ), 10, 3 );
1244
-
1245
- if ( get_option( "mfrh_rename_guid" ) ) {
1246
  add_action( 'mfrh_media_renamed', array( $this, 'action_rename_guid' ), 10, 3 );
1247
- }
1248
  }
1249
 
1250
  // The GUID should never be updated but... this will if the option is checked.
@@ -1278,9 +1315,6 @@ class Meow_MFRH_Core {
1278
  ", $orig_image_url, $new_image_url );
1279
  $wpdb->query( $query );
1280
  $this->log_sql( $query, $query_revert );
1281
-
1282
- //_wp_attached_file
1283
-
1284
  $this->log( "Metadata exactly like $orig_image_url were replaced by $new_image_url." );
1285
  }
1286
 
14
  }
15
 
16
  function init() {
 
17
  include( 'mfrh_custom.php' );
18
  include( 'api.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' ) );
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' ) );
30
+ //add_action( 'add_attachment', array( $this, 'add_attachment' ) );
31
  add_action( 'add_meta_boxes', array( $this, 'add_rename_metabox' ) );
32
+ add_filter( 'attachment_fields_to_save', array( $this, 'attachment_fields_to_save' ), 20, 2 );
33
  add_action( 'save_post', array( $this, 'save_post' ) );
34
 
35
+ if ( get_option( 'mfrh_on_upload', false ) ) {
36
  add_filter( 'wp_handle_upload_prefilter', array( $this, 'wp_handle_upload_prefilter' ), 10, 2 );
37
+ }
38
 
39
  // Column for Media Library
40
  $method = apply_filters( 'mfrh_method', 'media_title' );
68
  $screen = get_current_screen();
69
  if ( ( $screen->base == 'post' && $screen->post_type == 'attachment' ) ||
70
  ( $screen->base == 'media' && isset( $_GET['attachment_id'] ) ) ) {
71
+ $id = isset( $_GET['post'] ) ? $_GET['post'] : $_GET['attachment_id'];
72
+ if ( $this->check_attachment( get_post( $id, ARRAY_A ), $output ) ) {
73
  if ( $output['desired_filename_exists'] ) {
74
  echo '<div class="error"><p>
75
  The file ' . $output['desired_filename'] . ' already exists. Please give a new title for this media.
76
  </p></div>';
77
  }
78
  }
79
+ if ( $this->wpml_media_is_installed() && !$this->is_real_media( $id ) ) {
80
  echo '<div class="error"><p>
81
  This attachment seems to be a virtual copy (or translation). Media File Renamer will not make any modification from here.
82
  </p></div>';
84
  }
85
  }
86
 
87
+ /**
88
+ *
89
+ * TOOLS / HELPERS
90
+ *
91
+ */
92
+
93
+ // Check if the file exists, if it is, return the real path for it
94
+ // https://stackoverflow.com/questions/3964793/php-case-insensitive-version-of-file-exists
95
+ static function sensitive_file_exists( $filename, $fullpath = true, $caseInsensitive = true ) {
96
+ $output = false;
97
+ $directoryName = dirname( $filename );
98
+ $fileArray = glob( $directoryName . '/*', GLOB_NOSORT );
99
+ $i = ( $caseInsensitive ) ? "i" : "";
100
+
101
+ // Check if \ is in the string
102
+ if ( preg_match( "/\\\|\//", $filename) ) {
103
+ $array = preg_split("/\\\|\//", $filename);
104
+ $filename = $array[count( $array ) -1];
105
+ }
106
+ // Compare filenames
107
+ foreach ( $fileArray as $file ) {
108
+ if ( preg_match( "/{$filename}/{$i}", $file ) ) {
109
+ $output = $file;
110
+ break;
111
+ }
112
+ }
113
+ return $output;
114
+ }
115
+
116
+ static function rmdir_recursive( $directory ) {
117
+ foreach ( glob( "{$directory}/*" ) as $file ) {
118
+ if ( is_dir( $file ) )
119
+ Meow_MFRH_Core::rmdir_recursive( $file );
120
+ else
121
+ unlink( $file );
122
+ }
123
+ rmdir( $directory );
124
+ }
125
+
126
  /**
127
  *
128
  * MEDIA LIBRARY
130
  */
131
 
132
  function library_bulk_actions( $bulk_actions ) {
133
+ $bulk_actions['mfrh_lock_all'] = __( 'Lock (Renamer)', 'media-file-renamer');
134
  $bulk_actions['mfrh_unlock_all'] = __( 'Unlock (Renamer)', 'media-file-renamer');
135
  $bulk_actions['mfrh_rename_all'] = __( 'Rename (Renamer)', 'media-file-renamer');
136
+ return $bulk_actions;
137
  }
138
 
139
  function library_bulk_actions_handler( $redirect_to, $doaction, $ids ) {
140
  if ( $doaction == 'mfrh_lock_all' ) {
141
  foreach ( $ids as $post_id ) {
142
+ add_post_meta( $post_id, '_manual_file_renaming', true, true );
143
+ }
144
+ }
145
  if ( $doaction == 'mfrh_unlock_all' ) {
146
  foreach ( $ids as $post_id ) {
147
+ delete_post_meta( $post_id, '_manual_file_renaming' );
148
+ }
149
+ }
150
  if ( $doaction == 'mfrh_rename_all' ) {
151
  foreach ( $ids as $post_id ) {
152
+ $this->rename( $post_id );
153
+ }
154
+ }
155
  return $redirect_to;
156
  }
157
 
168
 
169
  function manage_media_custom_column( $column_name, $id ) {
170
  $paged = isset( $_GET['paged'] ) ? ( '&paged=' . $_GET['paged'] ) : "";
171
+ if ( $column_name !== 'mfrh_column' )
172
+ return;
173
 
174
+ // Information for locked media
175
+ $locked = get_post_meta( $id, '_manual_file_renaming', true );
176
+ if ( $locked ) {
177
+ echo "<span title='" . __( 'Manually renamed.', 'media-file-renamer' ) . "' style='font-size: 24px; color: #36B15C;' class='dashicons dashicons-yes'></span>";
178
+ $page = isset( $_GET['page'] ) ? ( '&page=' . $_GET['page'] ) : "";
179
+ 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>";
180
+ return;
181
+ }
182
 
183
+ // Information for media that needs renaming
184
+ $needs_rename = $this->check_attachment( get_post( $id, ARRAY_A ), $output );
185
+ if ( $needs_rename ) {
186
+ $this->generate_explanation( $output );
187
+ return;
 
 
 
 
 
 
 
 
 
 
 
188
  }
189
+
190
+ // Information for non-locked media
191
+ $original_filename = get_post_meta( $id, '_original_filename', true );
192
+ echo "<span title='" . __( 'Automatically renamed.', 'media-file-renamer' ) . "'style='font-size: 24px; color: #36B15C;' class='dashicons dashicons-yes'></span>";
193
+ $page = isset( $_GET['page'] ) ? ( '&page=' . $_GET['page'] ) : "";
194
+ if ( get_option( 'mfrh_undo', false ) && !empty( $original_filename ) ) {
195
+ 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>";
196
+ }
197
+ 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>";
198
  }
199
 
200
  function admin_head() {
201
  if ( !empty( $_GET['mfrh_rename'] ) ) {
202
  $mfrh_rename = $_GET['mfrh_rename'];
203
+ $this->rename( $mfrh_rename );
204
  $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'mfrh_rename' ), $_SERVER['REQUEST_URI'] );
205
  }
206
  if ( !empty( $_GET['mfrh_unlock'] ) ) {
212
  $mfrh_undo = $_GET['mfrh_undo'];
213
  $original_filename = get_post_meta( $mfrh_undo, '_original_filename', true );
214
  $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'mfrh_undo' ), $_SERVER['REQUEST_URI'] );
215
+ $this->rename( $mfrh_undo, $original_filename );
216
 
217
  $fp = get_attached_file( $mfrh_undo );
218
  $path_parts = pathinfo( $fp );
351
  }
352
  else if ( $subaction == 'renameMediaId' ) {
353
  $id = intval( $_POST['id'] );
354
+ $this->rename( $id );
355
  echo 1;
356
  die();
357
  }
375
  else if ( $subaction == 'undoMediaId' ) {
376
  $id = intval( $_POST['id'] );
377
  $original_filename = get_post_meta( $id, '_original_filename', true );
378
+ $this->rename( $id, $original_filename );
379
  echo 1;
380
  die();
381
  }
446
 
447
  function wp_handle_upload_prefilter( $file ) {
448
 
449
+ $this->log( "** On Upload: " . $file['name'] );
450
  $pp = pathinfo( $file['name'] );
451
 
452
  // If everything's fine, renames in based on the Title in the EXIF
455
  $exif = wp_read_image_metadata( $file['tmp_name'] );
456
  if ( !empty( $exif ) && isset( $exif[ 'title' ] ) && !empty( $exif[ 'title' ] ) ) {
457
  $file['name'] = $this->new_filename( null, $exif[ 'title' ] ) . '.' . $pp['extension'];
458
+ $this->log( "New file should be: " . $file['name'] );
459
  return $file;
460
  }
461
  }
463
  $post = get_post( $_POST['post_id'] );
464
  if ( !empty( $post ) && !empty( $post->post_title ) ) {
465
  $file['name'] = $this->new_filename( null, $post->post_title ) . '.' . $pp['extension'];
466
+ $this->log( "New file should be: " . $file['name'] );
467
  return $file;
468
  }
469
  }
492
  /****************************************************************************/
493
 
494
  // Return false if everything is fine, otherwise return true with an output.
495
+ function check_attachment( $post, &$output = array(), $manual_filename = null ) {
496
+ $id = $post['ID'];
497
+ $old_filepath = get_attached_file( $id );
498
+ $old_filepath = Meow_MFRH_Core::sensitive_file_exists( $old_filepath );
499
+ $path_parts = pathinfo( $old_filepath );
500
+ $directory = $path_parts['dirname'];
501
+ $old_filename = $path_parts['basename'];
502
+
503
+ // Check if media/file is dead
504
+ if ( !$old_filepath || !file_exists( $old_filepath ) ) {
505
  delete_post_meta( $id, '_require_file_renaming' );
506
  return false;
507
  }
508
+
509
+ // Is it forced/manual
510
+ // Check mfrh_new_filename (coming from manual input) if it is different than previous filename
511
+ if ( empty( $manual_filename ) && isset( $post['mfrh_new_filename'] ) ) {
512
+ if ( strtolower( $post['mfrh_new_filename'] ) != strtolower( $old_filename ) )
513
+ $manual_filename = $post['mfrh_new_filename'];
514
  }
515
 
516
+ if ( !empty( $manual_filename ) ) {
517
+ $new_filename = $manual_filename;
518
+ $output['manual'] = true;
 
519
  }
520
+ else {
521
+ $method = apply_filters( 'mfrh_method', 'media_title' );
522
+ if ( $method === 'none') {
523
+ delete_post_meta( $id, '_require_file_renaming' );
524
+ return false;
525
+ }
526
+ if ( get_post_meta( $id, '_manual_file_renaming', true ) ) {
527
+ return false;
528
+ }
529
 
530
+ // Skip header images
531
+ if ( $this->is_header_image( $id ) ) {
532
+ delete_post_meta( $id, '_require_file_renaming' );
 
 
 
533
  return false;
534
+ }
535
+
536
+ // Get information
537
+ $base_title = $post['post_title'];
538
+ if ( $method == 'post_title' ) {
539
+ $attachedpost = $this->get_post_from_media( $id );
540
+ if ( is_null( $attachedpost ) )
541
+ return false;
542
+ $base_title = $attachedpost->post_title;
543
+ }
544
+ else if ( $method == 'alt_text' ) {
545
+ $image_alt = get_post_meta( $id, '_wp_attachment_image_alt', true );
546
+ if ( is_null( $image_alt ) )
547
+ return false;
548
+ $base_title = $image_alt;
549
+ }
550
+ $new_filename = $this->new_filename( $post, $base_title );
551
+ //$this->log( "New title: $base_title, New filename: $new_filename" );
552
  }
553
+
554
+ // If a filename has a counter, and the ideal is without the counter, let's ignore it
555
+ $ideal = preg_replace( '/-[1-9]{1,10}\./', '$1.', $old_filename );
556
+ if ( !$manual_filename ) {
557
+ if ( $ideal == $new_filename ) {
558
+ delete_post_meta( $id, '_require_file_renaming' );
559
  return false;
560
+ }
 
 
 
 
 
 
 
 
 
561
  }
562
 
563
  // Filename is equal to sanitized title
564
+ if ( $new_filename == $old_filename ) {
565
  delete_post_meta( $id, '_require_file_renaming' );
566
  return false;
567
  }
568
 
569
+ // Check for case issue, numbering
570
+ $new_filepath = trailingslashit( $directory ) . $new_filename;
571
+ $existing_file = Meow_MFRH_Core::sensitive_file_exists( $new_filepath );
572
+ $case_issue = strtolower( $old_filename ) == strtolower( $new_filename );
573
+ if ( $existing_file && !$case_issue ) {
 
 
 
574
  $is_numbered = apply_filters( 'mfrh_numbered', false );
575
  if ( $is_numbered ) {
576
+ $new_filename = $this->generate_unique_filename( $ideal, $directory, $new_filename );
577
+ if ( !$new_filename ) {
 
578
  delete_post_meta( $id, '_require_file_renaming' );
579
  return false;
580
  }
581
+ $new_filepath = trailingslashit( $directory ) . $new_filename;
 
 
 
 
 
 
 
 
 
582
  }
583
  }
584
 
585
+ // Send info to the requester function
586
+ $output['post_id'] = $id;
587
+ $output['post_name'] = $post['post_name'];
588
+ $output['post_title'] = $post['post_title'];
589
+ $output['current_filename'] = $old_filename;
590
+ $output['current_filepath'] = $old_filepath;
591
+ $output['desired_filename'] = $new_filename;
592
+ $output['desired_filepath'] = $new_filepath;
593
+ $output['case_issue'] = $case_issue;
594
+ $output['manual'] = !empty( $manual_filename );
595
+ $output['locked'] = get_post_meta( $id, '_manual_file_renaming', true );
596
+ $output['desired_filename_exists'] = false;
597
+
598
  // It seems it could be renamed :)
599
  if ( !get_post_meta( $post['ID'], '_require_file_renaming' ) ) {
600
  add_post_meta( $post['ID'], '_require_file_renaming', true, true );
612
  AND post_type = 'attachment'
613
  " );
614
  foreach ( $ids as $id )
615
+ if ( $this->check_attachment( get_post( $id, ARRAY_A ), $output ) )
616
  array_push( $issues, $output );
617
  return $issues;
618
  }
621
 
622
  static $previous = array();
623
 
624
+ $smallDiv = '<div style="line-height: 12px; font-size: 10px; margin-top: 5px;">';
625
+
626
  if ( $file['post_title'] == "" ) {
627
  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>";
628
  }
629
  else if ( $file['desired_filename_exists'] ) {
630
+ 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>";
631
  }
632
  else {
633
  $page = isset( $_GET['page'] ) ? ( '&page=' . $_GET['page'] ) : "";
645
  echo "<a class='button-primary' href='?" . $page . $mfrh_scancheck . $mfrh_to_rename . "&mfrh_rename=" . $file['post_id'] . "'>" . __( 'Auto-Rename', 'media-file-renamer' ) . "</a>";
646
  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>";
647
 
648
+ if ( $file['case_issue'] ) {
649
+ echo '<br />' . $smallDiv .
650
+ sprintf( __( 'Rename in lowercase, to %s. You can also <a href="%s">edit this media</a>.', 'media-file-renamer' ),
651
+ $file['desired_filename'], $modify_url ) . "</div>";
652
+ }
653
+ else {
654
+ echo '<br />' . $smallDiv .
655
+ sprintf( __( 'Rename to %s. You can also <a href="%s">EDIT THIS MEDIA</a>.', 'media-file-renamer' ),
656
+ $file['desired_filename'], $modify_url ) . "</div>";
657
+ }
658
 
659
  if ( !$isNew ) {
660
+ echo $smallDiv . "<i>";
661
+ 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' );
662
+ echo '</i></div>';
663
  }
664
  }
665
  }
850
  * Added and modified by Jordy Meow
851
  */
852
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
853
  function save_post( $post_id ) {
854
  $status = get_post_status( $post_id );
855
  if ( !in_array( $status, array( 'publish', 'draft', 'future', 'private' ) ) )
856
  return;
857
+ $onsave = get_option( "mfrh_rename_on_save" );
858
+ if ( !$onsave )
859
+ return;
860
+ $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' =>'any', 'post_parent' => $post_id );
861
+ $medias = get_posts( $args );
862
+ if ( $medias ) {
863
+ $this->log( '[save_post]' );
864
+ foreach ( $medias as $attach ) {
865
+ // In the past, I used this to detect if the Media Library is NOT used:
866
+ // isset( $attachment['image-size'] );
867
+ $this->rename( $attach->ID );
868
+ }
869
+ }
870
  }
871
 
872
  /**
875
  *
876
  */
877
 
878
+ function attachment_fields_to_save( $post, $attachment ) {
879
+ $this->log( '[attachment_fields_to_save]' );
880
+ $post = $this->rename( $post );
881
+ return $post;
882
  }
883
 
884
+ function media_send_to_editor( $html, $id, $attachment ) {
885
+ $this->check_attachment( get_post( $id, ARRAY_A ), $output );
886
  return $html;
887
  }
888
 
900
  return $post;
901
  }
902
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
903
  function log_sql( $data, $antidata ) {
904
  if ( !get_option( 'mfrh_logsql' ) || !$this->mfrh_admin->is_registered() )
905
  return;
930
  *
931
  */
932
 
933
+ static function replace_special_chars( $str ) {
934
+ $special_chars = array(
935
  "å" => "a", "Å" => "a",
936
  "ä" => "ae", "Ä" => "ae",
937
  "ö" => "oe", "Ö" => "oe",
940
  );
941
  foreach ( $special_chars as $key => $value )
942
  $str = str_replace( $key, $value, $str );
943
+ return $str;
944
+ }
945
 
946
  function replace_chars( $str ) {
947
+ $special_chars = array();
948
+ $special_chars = apply_filters( 'mfrh_replace_rules', $special_chars );
949
+ if ( !empty( $special_chars ) )
950
+ foreach ( $special_chars as $key => $value )
951
+ $str = str_replace( $key, $value, $str );
952
+ return $str;
953
+ }
954
 
955
  // NEW MEDIA FILE INFO (depending on the text/filename of the media)
956
+ function new_filename( $media, $text, $manual_filename = null ) {
957
 
958
  $old_filename = null;
959
  $new_ext = null;
973
  }
974
 
975
  // Generate the new filename.
976
+ if ( !empty( $manual_filename ) ) {
977
  // Filename is forced. Strip the extension. Keeps this extension in $new_ext.
978
+ $pp = pathinfo( $manual_filename );
979
+ $manual_filename = $pp['filename'];
980
  $new_ext = empty( $pp['extension'] ) ? $new_ext : $pp['extension'];
981
+ $new_filename = $manual_filename;
982
  }
983
  else {
984
  // Filename is generated from $text, without an extension.
985
  $text = str_replace( ".jpg", "", $text );
986
  $text = str_replace( ".png", "", $text );
987
  $text = str_replace( "'", "-", $text );
988
+ $text = strtolower( Meow_MFRH_Core::replace_chars( $text ) );
989
  $utf8_filename = apply_filters( 'mfrh_utf8', false );
990
  if ( $utf8_filename )
991
  $new_filename = sanitize_file_name( $text );
992
  else
993
+ $new_filename = str_replace( "%", "-", sanitize_title( Meow_MFRH_Core::replace_special_chars( $text ) ) );
994
  }
995
  if ( empty( $new_filename ) )
996
  $new_filename = "empty";
998
  // We know have a new filename, let's add an extension.
999
  $new_filename = !empty( $new_ext ) ? ( $new_filename . '.' . $new_ext ) : $new_filename;
1000
 
1001
+ if ( !$manual_filename )
1002
  $new_filename = apply_filters( 'mfrh_new_filename', $new_filename, $old_filename, $media );
1003
  return $new_filename;
1004
  }
1006
  // Only replace the first occurence
1007
  function str_replace( $needle, $replace, $haystack ) {
1008
  $pos = strpos( $haystack, $needle );
1009
+ if ( $pos !== false )
1010
+ $haystack = substr_replace( $haystack, $replace, $pos, strlen( $needle ) );
 
1011
  return $haystack;
1012
  }
1013
 
1026
  str_replace( $upload_dir, "", $new_image_url ) );
1027
  }
1028
 
1029
+ function rename( $media, $manual_filename = null, $fromMediaLibrary = true ) {
1030
+ $id = null;
1031
+ $post = null;
 
 
 
 
1032
 
1033
+ // Check the arguments
1034
+ if ( is_numeric( $media ) ) {
1035
+ $id = $media;
1036
+ $post = get_post( $media, ARRAY_A );
1037
+ }
1038
+ else if ( is_array( $media ) ) {
1039
+ $id = $media['ID'];
1040
+ $post = $media;
1041
+ }
1042
+ else {
1043
+ die( 'Media File Renamer: rename() requires the ID or the array for the media.' );
1044
+ }
1045
 
1046
+ $force_rename = apply_filters( 'mfrh_force_rename', false );
1047
  $method = apply_filters( 'mfrh_method', 'media_title' );
1048
 
1049
+ // Check attachment
1050
+ $need_rename = $this->check_attachment( $post, $output, $manual_filename );
1051
+ if ( !$need_rename ) {
1052
+ delete_post_meta( $id, '_require_file_renaming' );
1053
+ return $post;
1054
  }
1055
 
1056
+ // Prepare the variables
1057
+ $old_filepath = $output['current_filepath'];
1058
+ $case_issue = $output['case_issue'];
1059
+ $new_filepath = $output['desired_filepath'];
1060
+ $new_filename = $output['desired_filename'];
1061
+ $manual = $output['manual'] || !empty( $manual_filename );
1062
  $path_parts = pathinfo( $old_filepath );
1063
  $directory = $path_parts['dirname']; // '2011/01'
1064
  $old_filename = $path_parts['basename']; // 'whatever.jpeg'
 
1065
 
1066
  $this->log( "** Rename Media: " . $old_filename );
1067
+ $this->log( "New file should be: " . $new_filename );
1068
 
1069
+ // Check for issues with the files
1070
+ if ( !file_exists( $old_filepath ) ) {
1071
+ $this->log( "The original file ($old_filepath) cannot be found." );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1072
  return $post;
1073
  }
1074
+ if ( !$case_issue && !$force_rename && file_exists( $new_filepath ) ) {
1075
+ $this->log( "The new file already exists ($new_filepath). It is not a case issue. Renaming cancelled." );
 
 
 
 
 
 
 
 
1076
  return $post;
1077
  }
1078
 
1079
+ // Keep the original filename
1080
+ $original_filename = get_post_meta( $id, '_original_filename', true );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1081
  if ( empty( $original_filename ) )
1082
+ add_post_meta( $id, '_original_filename', $old_filename, true );
1083
 
1084
  // Rename the main media file.
1085
  try {
1086
+ if ( $case_issue ) {
1087
+ rename( $old_filepath, $new_filepath . md5( $old_filepath ) );
1088
+ rename( $old_filepath . md5( $old_filepath ), $new_filepath );
1089
+ }
1090
+ else if ( ( !rename( $old_filepath, $new_filepath ) ) && !$force_rename ) {
1091
  $this->log( "The file couldn't be renamed from $old_filepath to $new_filepath." );
1092
  return $post;
1093
  }
1094
  $this->log( "File $old_filepath renamed to $new_filepath." );
1095
  do_action( 'mfrh_path_renamed', $post, $old_filepath, $new_filepath );
1096
  }
1097
+ catch ( Exception $e ) {
1098
+ $this->log( "The file couldn't be renamed from $old_filepath to $new_filepath." );
1099
  return $post;
1100
  }
1101
 
1102
  // The new extension (or maybe it's just the old one)
1103
+ $old_ext = $path_parts['extension'];
1104
  $new_ext = $old_ext;
1105
+ if ( $manual_filename ) {
1106
+ $pp = pathinfo( $manual_filename );
1107
  $new_ext = $pp['extension'];
1108
  }
1109
 
1110
  // Filenames without extensions
1111
  $noext_old_filename = $this->str_replace( '.' . $old_ext, '', $old_filename );
1112
+ $noext_new_filename = $this->str_replace( '.' . $old_ext, '', $new_filename );
 
1113
 
1114
  // Update the attachment meta
1115
+ $meta = wp_get_attachment_metadata( $id );
1116
  if ( $meta ) {
1117
  if ( isset( $meta['file'] ) && !empty( $meta['file'] ) )
1118
  $meta['file'] = $this->str_replace( $noext_old_filename, $noext_new_filename, $meta['file'] );
1123
  }
1124
 
1125
  // Images
1126
+ if ( wp_attachment_is_image( $id ) ) {
1127
  // Loop through the different sizes in the case of an image, and rename them.
1128
  $orig_image_urls = array();
1129
+ $orig_image_data = wp_get_attachment_image_src( $id, 'full' );
1130
  $orig_image_urls['full'] = $orig_image_data[0];
1131
  if ( empty( $meta['sizes'] ) ) {
1132
+ $this->log( "The WP metadata for attachment " . $id . " does not exist.", true );
1133
  }
1134
  else {
1135
  foreach ( $meta['sizes'] as $size => $meta_size ) {
1136
  if ( !isset($meta['sizes'][$size]['file'] ) )
1137
+ continue;
1138
  $meta_old_filename = $meta['sizes'][$size]['file'];
1139
  $meta_old_filepath = trailingslashit( $directory ) . $meta_old_filename;
1140
  $meta_new_filename = $this->str_replace( $noext_old_filename, $noext_new_filename, $meta_old_filename );
1145
  }
1146
 
1147
  $meta_new_filepath = trailingslashit( $directory ) . $meta_new_filename;
1148
+ $orig_image_data = wp_get_attachment_image_src( $id, $size );
1149
  $orig_image_urls[$size] = $orig_image_data[0];
1150
+
1151
+ // Double check files exist before trying to rename.
1152
+ if ( $force_rename || ( file_exists( $meta_old_filepath )
1153
+ && ( ( !file_exists( $meta_new_filepath ) ) || is_writable( $meta_new_filepath ) ) ) ) {
1154
  // WP Retina 2x is detected, let's rename those files as well
1155
  if ( function_exists( 'wr2x_get_retina' ) ) {
1156
  $wr2x_old_filepath = $this->str_replace( '.' . $old_ext, '@2x.' . $old_ext, $meta_old_filepath );
1157
  $wr2x_new_filepath = $this->str_replace( '.' . $new_ext, '@2x.' . $new_ext, $meta_new_filepath );
1158
  if ( file_exists( $wr2x_old_filepath ) && ( (!file_exists( $wr2x_new_filepath ) ) || is_writable( $wr2x_new_filepath ) ) ) {
1159
+ $renamed = false;
1160
+ if ( $case_issue ) {
1161
+ if ( @rename( $wr2x_old_filepath, $wr2x_new_filepath . md5( $wr2x_old_filepath ) ) )
1162
+ $renamed = @rename( $wr2x_old_filepath . md5( $wr2x_old_filepath ), $wr2x_new_filepath );
1163
+ }
1164
+ else {
1165
+ $renamed = @rename( $wr2x_old_filepath, $wr2x_new_filepath );
1166
+ }
1167
+ if ( $renamed ) {
1168
+ $this->log( "Retina file $wr2x_old_filepath renamed to $wr2x_new_filepath." );
1169
+ do_action( 'mfrh_path_renamed', $post, $wr2x_old_filepath, $wr2x_new_filepath );
1170
+ }
1171
+ else {
1172
+ $this->log( "Retina file $wr2x_old_filepath COULT NOT BE renamed to $wr2x_new_filepath." );
1173
+ }
1174
+ }
1175
+ }
1176
+ $renamed = false;
1177
+ if ( $case_issue ) {
1178
+ if ( @rename( $meta_old_filepath, $meta_new_filepath . md5( $meta_old_filepath ) ) )
1179
+ $renamed = @rename( $meta_old_filepath . md5( $meta_old_filepath ), $meta_new_filepath );
1180
+ }
1181
+ else {
1182
+ $renamed = @rename( $meta_old_filepath, $meta_new_filepath );
1183
+ }
1184
+ if ( $renamed ) {
1185
+ $meta['sizes'][$size]['file'] = $meta_new_filename;
1186
+
1187
+ // Detect if another size has exactly the same filename
1188
+ foreach ( $meta['sizes'] as $s => $m ) {
1189
+ if ( !isset( $meta['sizes'][$s]['file'] ) )
1190
+ continue;
1191
+ if ( $meta['sizes'][$s]['file'] == $meta_old_filename ) {
1192
+ $this->log( "Updated $s based on $size, as they use the same file (probably same size)." );
1193
+ $meta['sizes'][$s]['file'] = $meta_new_filename;
1194
+ }
1195
  }
1196
+
1197
+ // Success, call other plugins
1198
+ $this->log( "File $meta_old_filepath renamed to $meta_new_filepath." );
1199
+ do_action( 'mfrh_path_renamed', $post, $meta_old_filepath, $meta_new_filepath );
1200
+ }
1201
+ else {
1202
+ $this->log( "File $meta_old_filepath COULD NOT BE renamed to $meta_new_filepath." );
1203
  }
 
 
 
 
1204
  }
1205
  }
1206
  }
1207
  }
1208
  else {
1209
+ $orig_attachment_url = wp_get_attachment_url( $id );
1210
  }
1211
 
1212
  // This media doesn't require renaming anymore
1213
+ delete_post_meta( $id, '_require_file_renaming' );
1214
+
1215
+ // If it was renamed manually (including undo), lock the file
1216
+ if ( $manual )
1217
+ add_post_meta( $id, '_manual_file_renaming', true, true );
1218
 
1219
  // Update metadata
1220
  if ( $meta )
1221
+ wp_update_attachment_metadata( $id, $meta );
1222
+ update_attached_file( $id, $new_filepath );
1223
+ clean_post_cache( $id );
1224
 
1225
  // Call the actions so that the plugin's plugins can update everything else (than the files)
1226
+ if ( wp_attachment_is_image( $id ) ) {
1227
  $orig_image_url = $orig_image_urls['full'];
1228
+ $new_image_data = wp_get_attachment_image_src( $id, 'full' );
1229
  $new_image_url = $new_image_data[0];
1230
  $this->call_hooks_rename_url( $post, $orig_image_url, $new_image_url );
1231
 
1232
  if ( !empty( $meta['sizes'] ) ) {
1233
  foreach ( $meta['sizes'] as $size => $meta_size ) {
1234
  $orig_image_url = $orig_image_urls[$size];
1235
+ $new_image_data = wp_get_attachment_image_src( $id, $size );
1236
  $new_image_url = $new_image_data[0];
1237
  $this->call_hooks_rename_url( $post, $orig_image_url, $new_image_url );
1238
  }
1239
  }
1240
  }
1241
  else {
1242
+ $new_attachment_url = wp_get_attachment_url( $id );
1243
  $this->call_hooks_rename_url( $post, $orig_attachment_url, $new_attachment_url );
1244
  }
1245
 
1249
  $info = pathinfo( $new_filepath );
1250
  $newslug = preg_replace( '/\\.[^.\\s]{3,4}$/', '', $info['basename'] );
1251
  $post['post_name'] = $newslug;
1252
+ if ( wp_update_post( $post ) )
1253
+ $this->log( "Renamed slug from $oldslug to $newslug." );
1254
  }
1255
 
1256
  // HTTP REFERER set to the new media link
1257
  if ( isset( $_REQUEST['_wp_original_http_referer'] ) && strpos( $_REQUEST['_wp_original_http_referer'], '/wp-admin/' ) === false ) {
1258
+ $_REQUEST['_wp_original_http_referer'] = get_permalink( $id );
1259
  }
1260
 
1261
  do_action( 'mfrh_media_renamed', $post, $old_filepath, $new_filepath );
1280
  add_action( 'mfrh_url_renamed', array( $this, 'action_update_posts' ), 10, 3 );
1281
  if ( get_option( "mfrh_update_postmeta", true ) )
1282
  add_action( 'mfrh_url_renamed', array( $this, 'action_update_postmeta' ), 10, 3 );
1283
+ if ( get_option( "mfrh_rename_guid" ) )
 
1284
  add_action( 'mfrh_media_renamed', array( $this, 'action_rename_guid' ), 10, 3 );
 
1285
  }
1286
 
1287
  // The GUID should never be updated but... this will if the option is checked.
1315
  ", $orig_image_url, $new_image_url );
1316
  $wpdb->query( $query );
1317
  $this->log_sql( $query, $query_revert );
 
 
 
1318
  $this->log( "Metadata exactly like $orig_image_url were replaced by $new_image_url." );
1319
  }
1320
 
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.7.2
7
  Author: Jordy Meow
8
  Author URI: http://meowapps.com
9
  Text Domain: media-file-renamer
@@ -29,14 +29,15 @@ if ( class_exists( 'Meow_MFRH_Core' ) ) {
29
  if ( is_admin() ) {
30
 
31
  global $mfrh_version, $mfrh_core;
32
- $mfrh_version = '3.7.2';
33
 
34
  // Admin
35
  require( 'mfrh_admin.php');
36
  $mfrh_admin = new Meow_MFRH_Admin( 'mfrh', __FILE__, 'media-file-renamer' );
37
 
38
  // Core
39
- require( 'core.php' );
 
40
  $mfrh_core = new Meow_MFRH_Core( $mfrh_admin );
41
 
42
  /*******************************************************************************
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.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.0.0';
33
 
34
  // Admin
35
  require( 'mfrh_admin.php');
36
  $mfrh_admin = new Meow_MFRH_Admin( 'mfrh', __FILE__, 'media-file-renamer' );
37
 
38
  // Core
39
+ require( 'core.php' );
40
+ global $mfrh_core;
41
  $mfrh_core = new Meow_MFRH_Core( $mfrh_admin );
42
 
43
  /*******************************************************************************
mfrh_admin.php CHANGED
@@ -280,7 +280,7 @@ class Meow_MFRH_Admin extends MeowApps_Admin {
280
  $what = __( "Title of Media", 'media-file-renamer' );
281
  else if ( $method == "post_title" )
282
  $what = __( "Attached Post Title", 'media-file-renamer' );
283
- $html .= __( "<label>ALT = <b>$what</b></label><br /><small>Keep in mind that the HTML in your posts and pages will be however not modified, that is too dangerous!</small>", 'media-file-renamer' );
284
  echo $html;
285
  }
286
 
@@ -293,7 +293,7 @@ class Meow_MFRH_Admin extends MeowApps_Admin {
293
  $what = __( "Media ALT", 'media-file-renamer' );
294
  else if ( $method == "post_title" )
295
  $what = __( "Attached Post Title", 'media-file-renamer' );
296
- $html .= __( "<label>Media Title = <b>$what</b></label><br /><small>Keep in mind that the HTML in your posts and pages will be however not modified, that is too dangerous!</small>", 'media-file-renamer' );
297
  echo $html;
298
  }
299
 
280
  $what = __( "Title of Media", 'media-file-renamer' );
281
  else if ( $method == "post_title" )
282
  $what = __( "Attached Post Title", 'media-file-renamer' );
283
+ $html .= __( "<label>ALT = <b>$what</b></label><br /><small>Keep in mind that the HTML of your posts and pages WILL NOT be modified, as that is simply too dangerous for a plug-in.</small>", 'media-file-renamer' );
284
  echo $html;
285
  }
286
 
293
  $what = __( "Media ALT", 'media-file-renamer' );
294
  else if ( $method == "post_title" )
295
  $what = __( "Attached Post Title", 'media-file-renamer' );
296
+ $html .= __( "<label>Media Title = <b>$what</b></label><br /><small>Keep in mind that the HTML of your posts and pages WILL NOT be modified, as that is simply too dangerous for a plug-in.</small>", 'media-file-renamer' );
297
  echo $html;
298
  }
299
 
mfrh_custom.php CHANGED
@@ -1,31 +1,81 @@
1
  <?php
2
 
 
3
  // For help about this file, check:
4
  // http://meowapps.com/media-file-renamer/faq/
5
 
 
 
 
6
  // add_filter( 'mfrh_new_filename', 'add_hello_in_front_of_filenames', 10, 3 );
7
  //
8
  // function add_hello_in_front_of_filenames( $new, $old, $post ) {
9
  // return "hello-" . $new;
10
  // }
11
 
 
 
 
 
 
 
 
 
 
 
 
12
  // add_action( 'mfrh_url_renamed', 'url_of_media_was_modified', 10, 3 );
13
  //
14
  // function url_of_media_was_modified( $post, $orig_image_url, $new_image_url ) {
15
- //
 
 
 
 
 
16
  // }
17
 
 
 
18
  // add_action( 'mfrh_media_renamed', 'filepath_of_media_was_modified', 10, 3 );
19
  //
20
  // function filepath_of_media_was_modified( $post, $orig_image_url, $new_image_url ) {
21
  //
22
  // }
 
23
 
24
- // add_filter( 'mfrh_replace_rules', 'replace_s_by_z', 10, 1 );
25
- //
26
- // function replace_s_by_z( $rules ) {
27
- // $rules['s'] = 'z';
28
- // return $rules;
29
- // }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  ?>
1
  <?php
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
+
8
+ // HANDLE THE RENAMING
9
+ // =============================================================================
10
  // add_filter( 'mfrh_new_filename', 'add_hello_in_front_of_filenames', 10, 3 );
11
  //
12
  // function add_hello_in_front_of_filenames( $new, $old, $post ) {
13
  // return "hello-" . $new;
14
  // }
15
 
16
+ // REPLACE CHARACTER/STRING IN THE FILENAMES
17
+ // =============================================================================
18
+ // add_filter( 'mfrh_replace_rules', 'replace_s_by_z', 10, 1 );
19
+ //
20
+ // function replace_s_by_z( $rules ) {
21
+ // $rules['s'] = 'z';
22
+ // return $rules;
23
+ // }
24
+
25
+ // DO SOMETHING (UPDATES FOR INSTANCE) WHEN THE NEW URL IS READY
26
+ // =============================================================================
27
  // add_action( 'mfrh_url_renamed', 'url_of_media_was_modified', 10, 3 );
28
  //
29
  // function url_of_media_was_modified( $post, $orig_image_url, $new_image_url ) {
30
+ // global $wpdb;
31
+ // $query = $wpdb->prepare( "UPDATE $wpdb->postmeta SET meta_value = REPLACE(meta_value, '%s', '%s');", $orig_image_url, $new_image_url );
32
+ // $query_revert = $wpdb->prepare( "UPDATE $wpdb->postmeta SET meta_value = REPLACE(meta_value, '%s', '%s');", $new_image_url, $orig_image_url );
33
+ // $wpdb->query( $query );
34
+ // $this->log_sql( $query, $query_revert );
35
+ // $this->log( "Metadata like $orig_image_url were replaced by $new_image_url." );
36
  // }
37
 
38
+ // DO SOMETHING (UPDATES FOR INSTANCE) WHEN THE FILE IS READY
39
+ // =============================================================================
40
  // add_action( 'mfrh_media_renamed', 'filepath_of_media_was_modified', 10, 3 );
41
  //
42
  // function filepath_of_media_was_modified( $post, $orig_image_url, $new_image_url ) {
43
  //
44
  // }
45
+ // =============================================================================
46
 
47
+ // Beaver Builder: Update the Metadata, clear the cache
48
+ // https://www.wpbeaverbuilder.com/frequently-asked-questions/
49
+ if ( class_exists( 'FLBuilderModel' ) )
50
+ add_action( 'mfrh_url_renamed', 'mfrh_beaver_builder', 10, 3 );
51
+
52
+ function mfrh_beaver_builder( $post, $orig_image_url, $new_image_url ) {
53
+ global $wpdb;
54
+ $query = $wpdb->prepare( "UPDATE $wpdb->postmeta
55
+ SET meta_value = REPLACE(meta_value, 's:%d:\"$orig_image_url', 's:%d:\"$new_image_url')
56
+ WHERE meta_key = '_fl_builder_data'
57
+ OR meta_key = '_fl_builder_draft'",
58
+ strlen( $orig_image_url ), strlen( $new_image_url ) );
59
+ $query_revert = $wpdb->prepare( "UPDATE $wpdb->postmeta
60
+ SET meta_value = REPLACE(meta_value, 's:%d:\"$new_image_url', 's:%d:\"$orig_image_url')
61
+ WHERE meta_key = '_fl_builder_data'
62
+ OR meta_key = '_fl_builder_draft'",
63
+ strlen( $new_image_url ), strlen( $orig_image_url ) );
64
+ $wpdb->query( $query );
65
+ global $mfrh_core;
66
+ $mfrh_core->log_sql( $query, $query_revert );
67
+ $mfrh_core->log( "Beaver Metadata like $orig_image_url was replaced by $new_image_url." );
68
+
69
+ // Clear cache
70
+ $uploads = wp_upload_dir();
71
+ $cache = trailingslashit( $uploads['basedir'] ) . 'bb-plugin';
72
+ if ( file_exists( $cache ) )
73
+ Meow_MFRH_Core::rmdir_recursive( $cache );
74
+ else {
75
+ $cache = trailingslashit( $uploads['basedir'] ) . 'fl-builder';
76
+ if ( file_exists( $cache ) )
77
+ Meow_MFRH_Core::rmdir_recursive( $cache );
78
+ }
79
+ }
80
 
81
  ?>
readme.txt CHANGED
@@ -1,9 +1,9 @@
1
  === Media File Renamer ===
2
  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.7.2
7
 
8
  Automatically rename files depending on Media titles dynamically + update links. Pro version has many more options. Check the description :)
9
 
@@ -100,6 +100,12 @@ You are welcome to create plugins using Media File Renamer using special rules f
100
 
101
  == Changelog ==
102
 
 
 
 
 
 
 
103
  = 3.7.2 =
104
  * Update: Now uploading Media into Post rename the filename accordingly.
105
 
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
6
+ Stable tag: 4.0.0
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
+ = 4.0.0 =
104
+ * Update: Huge code cleaning and major refactorization. The core was also rewritten.
105
+ * Add: Compatibility with Beaver Builder.
106
+ * Fix: Avoid looking for too much perfection (which is dangerous) when using numbered files.
107
+ * Fix: Works fine now with image sizes in the meta which has the... same size.
108
+
109
  = 3.7.2 =
110
  * Update: Now uploading Media into Post rename the filename accordingly.
111