Media File Renamer - Version 4.0.1

Version Description

  • Fix: Issue with the tolowercase feature.
  • Fix: Extension issue with mfrh_new_filename filter.
  • Add: Filter to rewrite Alt Text.
Download this release

Release Info

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

Code changes from version 4.0.0 to 4.0.1

Files changed (4) hide show
  1. core.php +60 -55
  2. media-file-renamer.php +2 -2
  3. mfrh_custom.php +6 -2
  4. readme.txt +6 -1
core.php CHANGED
@@ -956,6 +956,7 @@ class Meow_MFRH_Core {
956
  function new_filename( $media, $text, $manual_filename = null ) {
957
 
958
  $old_filename = null;
 
959
  $new_ext = null;
960
 
961
  if ( !empty( $media ) ) {
@@ -964,6 +965,7 @@ class Meow_MFRH_Core {
964
  $pp = pathinfo( $old_filepath );
965
  $new_ext = empty( $pp['extension'] ) ? "" : $pp['extension'];
966
  $old_filename = $pp['basename'];
 
967
  }
968
  else {
969
  // It's an upload, let's check if the extension is provided in the text
@@ -995,11 +997,12 @@ class Meow_MFRH_Core {
995
  if ( empty( $new_filename ) )
996
  $new_filename = "empty";
997
 
 
 
 
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
  }
1005
 
@@ -1026,6 +1029,24 @@ class Meow_MFRH_Core {
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;
@@ -1082,22 +1103,18 @@ class Meow_MFRH_Core {
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'];
@@ -1155,52 +1172,40 @@ class Meow_MFRH_Core {
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
  }
956
  function new_filename( $media, $text, $manual_filename = null ) {
957
 
958
  $old_filename = null;
959
+ $old_filename_no_ext = null;
960
  $new_ext = null;
961
 
962
  if ( !empty( $media ) ) {
965
  $pp = pathinfo( $old_filepath );
966
  $new_ext = empty( $pp['extension'] ) ? "" : $pp['extension'];
967
  $old_filename = $pp['basename'];
968
+ $old_filename_no_ext = $pp['filename'];
969
  }
970
  else {
971
  // It's an upload, let's check if the extension is provided in the text
997
  if ( empty( $new_filename ) )
998
  $new_filename = "empty";
999
 
1000
+ if ( !$manual_filename )
1001
+ $new_filename = apply_filters( 'mfrh_new_filename', $new_filename, $old_filename_no_ext, $media );
1002
+
1003
  // We know have a new filename, let's add an extension.
1004
  $new_filename = !empty( $new_ext ) ? ( $new_filename . '.' . $new_ext ) : $new_filename;
1005
 
 
 
1006
  return $new_filename;
1007
  }
1008
 
1029
  str_replace( $upload_dir, "", $new_image_url ) );
1030
  }
1031
 
1032
+ function rename_file( $old, $new, $case_issue = false ) {
1033
+ if ( $case_issue ) {
1034
+ if ( !rename( $old, $old . md5( $old ) ) ) {
1035
+ $this->log( "The file couldn't be renamed (case issue) from $old to " . $old . md5( $old ) . "." );
1036
+ return false;
1037
+ }
1038
+ if ( !rename( $old . md5( $old ), $new ) ) {
1039
+ $this->log( "The file couldn't be renamed (case issue) from " . $old . md5( $old ) . " to $new." );
1040
+ return false;
1041
+ }
1042
+ }
1043
+ else if ( ( !rename( $old, $new ) ) ) {
1044
+ $this->log( "The file couldn't be renamed from $old to $new." );
1045
+ return false;
1046
+ }
1047
+ return true;
1048
+ }
1049
+
1050
  function rename( $media, $manual_filename = null, $fromMediaLibrary = true ) {
1051
  $id = null;
1052
  $post = null;
1103
  add_post_meta( $id, '_original_filename', $old_filename, true );
1104
 
1105
  // Rename the main media file.
1106
+ // try {
1107
+ if ( !$this->rename_file( $old_filepath, $new_filepath, $case_issue ) && !$force_rename ) {
 
 
 
 
 
 
 
 
 
 
 
1108
  $this->log( "The file couldn't be renamed from $old_filepath to $new_filepath." );
1109
  return $post;
1110
  }
1111
+ $this->log( "File $old_filepath renamed to $new_filepath." );
1112
+ do_action( 'mfrh_path_renamed', $post, $old_filepath, $new_filepath );
1113
+ // }
1114
+ // catch ( Exception $e ) {
1115
+ // $this->log( "The file couldn't be renamed from $old_filepath to $new_filepath." );
1116
+ // return $post;
1117
+ // }
1118
 
1119
  // The new extension (or maybe it's just the old one)
1120
  $old_ext = $path_parts['extension'];
1172
  if ( function_exists( 'wr2x_get_retina' ) ) {
1173
  $wr2x_old_filepath = $this->str_replace( '.' . $old_ext, '@2x.' . $old_ext, $meta_old_filepath );
1174
  $wr2x_new_filepath = $this->str_replace( '.' . $new_ext, '@2x.' . $new_ext, $meta_new_filepath );
1175
+ if ( file_exists( $wr2x_old_filepath ) && ( ( !file_exists( $wr2x_new_filepath ) ) || is_writable( $wr2x_new_filepath ) ) ) {
1176
+
1177
+ // Rename retina file
1178
+ if ( !$this->rename_file( $wr2x_old_filepath, $wr2x_new_filepath, $case_issue ) && !$force_rename ) {
1179
+ $this->log( "The file couldn't be renamed from $wr2x_old_filepath to $wr2x_new_filepath." );
1180
+ return $post;
 
 
 
 
 
 
 
 
 
1181
  }
1182
+ $this->log( "Retina file $wr2x_old_filepath renamed to $wr2x_new_filepath." );
1183
+ do_action( 'mfrh_path_renamed', $post, $wr2x_old_filepath, $wr2x_new_filepath );
1184
  }
1185
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1186
 
1187
+ // Rename meta file
1188
+ if ( !$this->rename_file( $meta_old_filepath, $meta_new_filepath, $case_issue ) && !$force_rename ) {
1189
+ $this->log( "The file couldn't be renamed from $meta_old_filepath to $meta_new_filepath." );
1190
+ return $post;
1191
  }
1192
+
1193
+ $meta['sizes'][$size]['file'] = $meta_new_filename;
1194
+
1195
+ // Detect if another size has exactly the same filename
1196
+ foreach ( $meta['sizes'] as $s => $m ) {
1197
+ if ( !isset( $meta['sizes'][$s]['file'] ) )
1198
+ continue;
1199
+ if ( $meta['sizes'][$s]['file'] == $meta_old_filename ) {
1200
+ $this->log( "Updated $s based on $size, as they use the same file (probably same size)." );
1201
+ $meta['sizes'][$s]['file'] = $meta_new_filename;
1202
+ }
1203
  }
1204
+
1205
+ // Success, call other plugins
1206
+ $this->log( "File $meta_old_filepath renamed to $meta_new_filepath." );
1207
+ do_action( 'mfrh_path_renamed', $post, $meta_old_filepath, $meta_new_filepath );
1208
+
1209
  }
1210
  }
1211
  }
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: 4.0.0
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.0';
33
 
34
  // Admin
35
  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: 4.0.1
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.1';
33
 
34
  // Admin
35
  require( 'mfrh_admin.php');
mfrh_custom.php CHANGED
@@ -6,11 +6,14 @@
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
@@ -46,6 +49,7 @@
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
 
6
 
7
 
8
  // HANDLE THE RENAMING
9
+ // $new is the proposed filename by Media File Renamer (without extension)
10
+ // $old is the current filename (without extension)
11
+ // $post is the attachment/media
12
+ // return: your ideal filename
13
  // =============================================================================
14
  // add_filter( 'mfrh_new_filename', 'add_hello_in_front_of_filenames', 10, 3 );
 
15
  // function add_hello_in_front_of_filenames( $new, $old, $post ) {
16
+ // return $new . "-offbeat";
17
  // }
18
 
19
  // REPLACE CHARACTER/STRING IN THE FILENAMES
49
 
50
  // Beaver Builder: Update the Metadata, clear the cache
51
  // https://www.wpbeaverbuilder.com/frequently-asked-questions/
52
+
53
  if ( class_exists( 'FLBuilderModel' ) )
54
  add_action( 'mfrh_url_renamed', 'mfrh_beaver_builder', 10, 3 );
55
 
readme.txt CHANGED
@@ -3,7 +3,7 @@ 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,6 +100,11 @@ You are welcome to create plugins using Media File Renamer using special rules f
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.
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.1
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.1 =
104
+ * Fix: Issue with the tolowercase feature.
105
+ * Fix: Extension issue with mfrh_new_filename filter.
106
+ * Add: Filter to rewrite Alt Text.
107
+
108
  = 4.0.0 =
109
  * Update: Huge code cleaning and major refactorization. The core was also rewritten.
110
  * Add: Compatibility with Beaver Builder.