Media File Renamer - Version 4.9.4

Version Description

Download this release

Release Info

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

Code changes from version 4.0.3 to 4.9.4

api.php CHANGED
@@ -2,7 +2,7 @@
2
 
3
  /**
4
  *
5
- * FUNCTIONS THAT CAN BE USED BY THEMES/PLUGINS DEVELOPERS
6
  *
7
  */
8
 
@@ -12,4 +12,17 @@ function mfrh_rename( $mediaId ) {
12
  return $mfrh_core->rename( $mediaId );
13
  }
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  ?>
2
 
3
  /**
4
  *
5
+ * FUNCTIONS
6
  *
7
  */
8
 
12
  return $mfrh_core->rename( $mediaId );
13
  }
14
 
15
+ /**
16
+ *
17
+ * ACTIONS AND FILTERS
18
+ *
19
+ * Available actions are:
20
+ * mfrh_path_renamed
21
+ * mfrh_url_renamed
22
+ * mfrh_media_renamed
23
+ *
24
+ * Please have a look at the custom.php file for examples.
25
+ *
26
+ */
27
+
28
  ?>
core.php CHANGED
@@ -2,60 +2,48 @@
2
 
3
  class Meow_MFRH_Core {
4
 
5
- private $mfrh_admin = null;
6
 
7
- public function __construct( $mfrh_admin ) {
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() {
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' ) );
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' ) );
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' );
41
- if ( $method != 'none' ) {
42
- add_filter( 'manage_media_columns', array( $this, 'add_media_columns' ) );
43
- add_action( 'manage_media_custom_column', array( $this, 'manage_media_custom_column' ), 10, 2 );
44
- }
45
-
46
- // Media Library Bulk Actions
47
- add_filter( 'bulk_actions-upload', array( $this, 'library_bulk_actions' ) );
48
- add_filter( 'handle_bulk_actions-upload', array( $this, 'library_bulk_actions_handler' ), 10, 3 );
49
- }
50
 
51
- /**
52
- *
53
- * ADDITIONAL PLUGINS
54
- *
55
- */
 
56
 
57
- function wpml_load() {
58
- require( 'plugins/wpml.php' );
 
 
59
  }
60
 
61
  /**
@@ -161,7 +149,7 @@ class Meow_MFRH_Core {
161
  *
162
  */
163
 
164
- function add_media_columns($columns) {
165
  $columns['mfrh_column'] = __( 'Rename', 'media-file-renamer' );
166
  return $columns;
167
  }
@@ -668,7 +656,7 @@ class Meow_MFRH_Core {
668
  function rename_media_files() {
669
  $hide_ads = get_option( 'meowapps_hide_ads' );
670
  echo '<div class="wrap">';
671
- echo $this->mfrh_admin->display_title( "Media File Renamer" );
672
  echo '<p></p>';
673
  global $wpdb;
674
 
@@ -699,7 +687,7 @@ class Meow_MFRH_Core {
699
  ?>
700
 
701
  <?php
702
- if ( !$this->mfrh_admin->is_registered() ) {
703
  echo '<div class="updated"><p>';
704
  echo __( '<b>The Pro version</b> of the plugin allows you to <b>rename based on the title of the post</b> (product or whatever else) you media is attached to, <b>rename manually</b>, use <b>numbered files</b> (by adding a counter if the filenames are similar), <b>sync the title with your ALT text</b>, UTF8 support (if you need it), a force rename (to repair a broken install), and, more importantly, <b>supports the developer</b> :) The serial key for the Pro has to be inserted in your Meow Apps > File Renamer > Pro. Thank you :)<br /><br /><a class="button-primary" href="http://meowapps.com/media-file-renamer/" target="_blank">Get the serial key for the Pro</a>', 'media-file-renamer' );
705
  echo '</p></div>';
@@ -897,12 +885,12 @@ class Meow_MFRH_Core {
897
  $is_manual = apply_filters( 'mfrh_manual', false );
898
  $html = '<input type="text" readonly class="widefat" name="mfrh_new_filename" value="' . $basename. '" />';
899
  $html .= '<p class="description">This feature is for <a target="_blank" href="http://meowapps.com/media-file-renamer/">Pro users</a> only.</p>';
900
- echo apply_filters( "mfrh_admin_attachment_fields", $html, $post );
901
  return $post;
902
  }
903
 
904
  function log_sql( $data, $antidata ) {
905
- if ( !get_option( 'mfrh_logsql' ) || !$this->mfrh_admin->is_registered() )
906
  return;
907
  $fh = fopen( trailingslashit( dirname(__FILE__) ) . 'mfrh_sql.log', 'a' );
908
  $fh_anti = fopen( trailingslashit( dirname(__FILE__) ) . 'mfrh_sql_revert.log', 'a' );
@@ -1031,6 +1019,8 @@ class Meow_MFRH_Core {
1031
  }
1032
 
1033
  function rename_file( $old, $new, $case_issue = false ) {
 
 
1034
  if ( $case_issue ) {
1035
  if ( !rename( $old, $old . md5( $old ) ) ) {
1036
  $this->log( "The file couldn't be renamed (case issue) from $old to " . $old . md5( $old ) . "." );
@@ -1220,6 +1210,8 @@ class Meow_MFRH_Core {
1220
  if ( $meta )
1221
  wp_update_attachment_metadata( $id, $meta );
1222
  update_attached_file( $id, $new_filepath );
 
 
1223
  clean_post_cache( $id );
1224
 
1225
  // Rename slug/permalink
@@ -1261,125 +1253,4 @@ class Meow_MFRH_Core {
1261
  do_action( 'mfrh_media_renamed', $post, $old_filepath, $new_filepath );
1262
  return $post;
1263
  }
1264
-
1265
- /**
1266
- *
1267
- * INTERNAL ACTIONS (HOOKS)
1268
- * Mostly from the Side-Updates
1269
- *
1270
- * Available actions are:
1271
- * mfrh_path_renamed
1272
- * mfrh_url_renamed
1273
- * mfrh_media_renamed
1274
- *
1275
- */
1276
-
1277
- // Register internal actions
1278
- function init_actions() {
1279
- if ( get_option( "mfrh_update_posts", true ) )
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.
1288
- // [TigrouMeow] It the recent version of WordPress, the GUID is not part of the $post (even though it is in database)
1289
- // Explanation: http://pods.io/2013/07/17/dont-use-the-guid-field-ever-ever-ever/
1290
- function action_rename_guid( $post, $old_filepath, $new_filepath ) {
1291
- $meta = wp_get_attachment_metadata( $post['ID'] );
1292
- $old_guid = get_the_guid( $post['ID'] );
1293
- if ( $meta )
1294
- $new_filepath = wp_get_attachment_url( $post['ID'] );
1295
- global $wpdb;
1296
- $query = $wpdb->prepare( "UPDATE $wpdb->posts SET guid = '%s' WHERE ID = '%d'", $new_filepath, $post['ID'] );
1297
- $query_revert = $wpdb->prepare( "UPDATE $wpdb->posts SET guid = '%s' WHERE ID = '%d'", $old_guid, $post['ID'] );
1298
- $this->log_sql( $query, $query_revert );
1299
- $wpdb->query( $query );
1300
- clean_post_cache( $post['ID'] );
1301
- $this->log( "GUID\t$old_guid -> $new_filepath." );
1302
- }
1303
-
1304
- // Mass update of all the meta with the new filenames
1305
- function action_update_postmeta( $post, $orig_image_url, $new_image_url ) {
1306
- global $wpdb;
1307
- $query = $wpdb->prepare( "UPDATE $wpdb->postmeta
1308
- SET meta_value = '%s'
1309
- WHERE meta_key <> '_original_filename'
1310
- AND (TRIM(meta_value) = '%s'
1311
- OR TRIM(meta_value) = '%s'
1312
- );", $new_image_url, $orig_image_url, str_replace( ' ', '%20', $orig_image_url ) );
1313
- $query_revert = $wpdb->prepare( "UPDATE $wpdb->postmeta
1314
- SET meta_value = '%s'
1315
- WHERE meta_key <> '_original_filename'
1316
- AND meta_value = '%s';
1317
- ", $orig_image_url, $new_image_url );
1318
- $wpdb->query( $query );
1319
- $this->log_sql( $query, $query_revert );
1320
- $this->log( "Meta\t$orig_image_url -> $new_image_url" );
1321
- }
1322
-
1323
- // Mass update of all the articles with the new filenames
1324
- function action_update_posts( $post, $orig_image_url, $new_image_url ) {
1325
- global $wpdb;
1326
-
1327
- // Content
1328
- $query = $wpdb->prepare( "UPDATE $wpdb->posts
1329
- SET post_content = REPLACE(post_content, '%s', '%s')
1330
- WHERE post_status != 'inherit'
1331
- AND post_status != 'trash'
1332
- AND post_type != 'attachment'
1333
- AND post_type NOT LIKE '%acf-%'
1334
- AND post_type NOT LIKE '%edd_%'
1335
- AND post_type != 'shop_order'
1336
- AND post_type != 'shop_order_refund'
1337
- AND post_type != 'nav_menu_item'
1338
- AND post_type != 'revision'
1339
- AND post_type != 'auto-draft'", $orig_image_url, $new_image_url );
1340
- $query_revert = $wpdb->prepare( "UPDATE $wpdb->posts
1341
- SET post_content = REPLACE(post_content, '%s', '%s')
1342
- WHERE post_status != 'inherit'
1343
- AND post_status != 'trash'
1344
- AND post_type != 'attachment'
1345
- AND post_type NOT LIKE '%acf-%'
1346
- AND post_type NOT LIKE '%edd_%'
1347
- AND post_type != 'shop_order'
1348
- AND post_type != 'shop_order_refund'
1349
- AND post_type != 'nav_menu_item'
1350
- AND post_type != 'revision'
1351
- AND post_type != 'auto-draft'", $new_image_url, $orig_image_url );
1352
- $wpdb->query( $query );
1353
- $this->log_sql( $query, $query_revert );
1354
- $this->log( "Content\t$orig_image_url -> $new_image_url" );
1355
-
1356
- // Excerpt
1357
- $query = $wpdb->prepare( "UPDATE $wpdb->posts
1358
- SET post_excerpt = REPLACE(post_excerpt, '%s', '%s')
1359
- WHERE post_status != 'inherit'
1360
- AND post_status != 'trash'
1361
- AND post_type != 'attachment'
1362
- AND post_type NOT LIKE '%acf-%'
1363
- AND post_type NOT LIKE '%edd_%'
1364
- AND post_type != 'shop_order'
1365
- AND post_type != 'shop_order_refund'
1366
- AND post_type != 'nav_menu_item'
1367
- AND post_type != 'revision'
1368
- AND post_type != 'auto-draft'", $orig_image_url, $new_image_url );
1369
- $query_revert = $wpdb->prepare( "UPDATE $wpdb->posts
1370
- SET post_excerpt = REPLACE(post_excerpt, '%s', '%s')
1371
- WHERE post_status != 'inherit'
1372
- AND post_status != 'trash'
1373
- AND post_type != 'attachment'
1374
- AND post_type NOT LIKE '%acf-%'
1375
- AND post_type NOT LIKE '%edd_%'
1376
- AND post_type != 'shop_order'
1377
- AND post_type != 'shop_order_refund'
1378
- AND post_type != 'nav_menu_item'
1379
- AND post_type != 'revision'
1380
- AND post_type != 'auto-draft'", $new_image_url, $orig_image_url );
1381
- $wpdb->query( $query );
1382
- $this->log_sql( $query, $query_revert );
1383
- $this->log( "Excerpt\t$orig_image_url -> $new_image_url" );
1384
- }
1385
  }
2
 
3
  class Meow_MFRH_Core {
4
 
5
+ private $admin = null;
6
 
7
+ public function __construct( $admin ) {
8
+ $this->admin = $admin;
9
  add_action( 'plugins_loaded', array( $this, 'init' ) );
 
 
 
 
10
  }
11
 
12
  function init() {
 
13
  include( 'api.php' );
14
+ include( 'updates.php' );
15
+ new Meow_MFRH_Updates( $this, $this->admin );
16
  load_plugin_textdomain( 'media-file-renamer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
17
 
18
+ // Those actions/filters are only for the admin screens
19
+ if ( is_admin() ) {
20
+ add_action( 'admin_head', array( $this, 'admin_head' ) );
21
+ add_action( 'admin_menu', array( $this, 'admin_menu' ) );
22
+ add_action( 'wp_ajax_mfrh_rename_media', array( $this, 'wp_ajax_mfrh_rename_media' ) );
23
+ add_action( 'wp_ajax_mfrh_undo_media', array( $this, 'wp_ajax_mfrh_undo_media' ) );
24
+ add_filter( 'media_send_to_editor', array( $this, 'media_send_to_editor' ), 20, 3 );
25
+ add_action( 'admin_notices', array( $this, 'admin_notices' ) );
26
+ //add_action( 'edit_attachment', array( $this, 'edit_attachment' ) );
27
+ //add_action( 'add_attachment', array( $this, 'add_attachment' ) );
28
+ add_action( 'add_meta_boxes', array( $this, 'add_rename_metabox' ) );
29
+ add_filter( 'attachment_fields_to_save', array( $this, 'attachment_fields_to_save' ), 20, 2 );
30
+ add_action( 'save_post', array( $this, 'save_post' ) );
31
+
32
+ if ( get_option( 'mfrh_on_upload', false ) ) {
33
+ add_filter( 'wp_handle_upload_prefilter', array( $this, 'wp_handle_upload_prefilter' ), 10, 2 );
34
+ }
 
 
 
 
 
 
 
 
 
 
35
 
36
+ // Column for Media Library
37
+ $method = apply_filters( 'mfrh_method', 'media_title' );
38
+ if ( $method != 'none' ) {
39
+ add_filter( 'manage_media_columns', array( $this, 'add_media_columns' ) );
40
+ add_action( 'manage_media_custom_column', array( $this, 'manage_media_custom_column' ), 10, 2 );
41
+ }
42
 
43
+ // Media Library Bulk Actions
44
+ add_filter( 'bulk_actions-upload', array( $this, 'library_bulk_actions' ) );
45
+ add_filter( 'handle_bulk_actions-upload', array( $this, 'library_bulk_actions_handler' ), 10, 3 );
46
+ }
47
  }
48
 
49
  /**
149
  *
150
  */
151
 
152
+ function add_media_columns( $columns ) {
153
  $columns['mfrh_column'] = __( 'Rename', 'media-file-renamer' );
154
  return $columns;
155
  }
656
  function rename_media_files() {
657
  $hide_ads = get_option( 'meowapps_hide_ads' );
658
  echo '<div class="wrap">';
659
+ echo $this->admin->display_title( "Media File Renamer" );
660
  echo '<p></p>';
661
  global $wpdb;
662
 
687
  ?>
688
 
689
  <?php
690
+ if ( !$this->admin->is_registered() ) {
691
  echo '<div class="updated"><p>';
692
  echo __( '<b>The Pro version</b> of the plugin allows you to <b>rename based on the title of the post</b> (product or whatever else) you media is attached to, <b>rename manually</b>, use <b>numbered files</b> (by adding a counter if the filenames are similar), <b>sync the title with your ALT text</b>, UTF8 support (if you need it), a force rename (to repair a broken install), and, more importantly, <b>supports the developer</b> :) The serial key for the Pro has to be inserted in your Meow Apps > File Renamer > Pro. Thank you :)<br /><br /><a class="button-primary" href="http://meowapps.com/media-file-renamer/" target="_blank">Get the serial key for the Pro</a>', 'media-file-renamer' );
693
  echo '</p></div>';
885
  $is_manual = apply_filters( 'mfrh_manual', false );
886
  $html = '<input type="text" readonly class="widefat" name="mfrh_new_filename" value="' . $basename. '" />';
887
  $html .= '<p class="description">This feature is for <a target="_blank" href="http://meowapps.com/media-file-renamer/">Pro users</a> only.</p>';
888
+ echo apply_filters( "admin_attachment_fields", $html, $post );
889
  return $post;
890
  }
891
 
892
  function log_sql( $data, $antidata ) {
893
+ if ( !get_option( 'mfrh_logsql' ) || !$this->admin->is_registered() )
894
  return;
895
  $fh = fopen( trailingslashit( dirname(__FILE__) ) . 'mfrh_sql.log', 'a' );
896
  $fh_anti = fopen( trailingslashit( dirname(__FILE__) ) . 'mfrh_sql_revert.log', 'a' );
1019
  }
1020
 
1021
  function rename_file( $old, $new, $case_issue = false ) {
1022
+ // If there is a case issue, that means the system doesn't make the difference between AA.jpg and aa.jpg even though WordPress does.
1023
+ // In that case it is important to rename the file to a temporary filename in between like: AA.jpg -> TMP.jpg -> aa.jpg.
1024
  if ( $case_issue ) {
1025
  if ( !rename( $old, $old . md5( $old ) ) ) {
1026
  $this->log( "The file couldn't be renamed (case issue) from $old to " . $old . md5( $old ) . "." );
1210
  if ( $meta )
1211
  wp_update_attachment_metadata( $id, $meta );
1212
  update_attached_file( $id, $new_filepath );
1213
+
1214
+ // I wonder about cleaning the cache for this media. It might have no impact, and will not reset the cache for the posts using this media anyway, and it adds processing time. I keep it for now, but there might be something better to do.
1215
  clean_post_cache( $id );
1216
 
1217
  // Rename slug/permalink
1253
  do_action( 'mfrh_media_renamed', $post, $old_filepath, $new_filepath );
1254
  return $post;
1255
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1256
  }
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.3
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.3';
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.4
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.4';
33
 
34
  // Admin
35
  require( 'mfrh_admin.php');
plugins/beaverbuilder.php ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ // Beaver Builder: Update the Metadata, clear the cache
4
+ // https://www.wpbeaverbuilder.com/frequently-asked-questions/
5
+
6
+ add_action( 'mfrh_url_renamed', 'mfrh_beaver_builder', 10, 3 );
7
+
8
+ function mfrh_beaver_builder( $post, $orig_image_url, $new_image_url ) {
9
+ global $wpdb;
10
+ $query = $wpdb->prepare( "UPDATE $wpdb->postmeta
11
+ SET meta_value = REPLACE(meta_value, 's:%d:\"$orig_image_url', 's:%d:\"$new_image_url')
12
+ WHERE meta_key = '_fl_builder_data'
13
+ OR meta_key = '_fl_builder_draft'",
14
+ strlen( $orig_image_url ), strlen( $new_image_url ) );
15
+ $query_revert = $wpdb->prepare( "UPDATE $wpdb->postmeta
16
+ SET meta_value = REPLACE(meta_value, 's:%d:\"$new_image_url', 's:%d:\"$orig_image_url')
17
+ WHERE meta_key = '_fl_builder_data'
18
+ OR meta_key = '_fl_builder_draft'",
19
+ strlen( $new_image_url ), strlen( $orig_image_url ) );
20
+ $wpdb->query( $query );
21
+ global $mfrh_core;
22
+ $mfrh_core->log_sql( $query, $query_revert );
23
+ $mfrh_core->log( "Beaver Metadata like $orig_image_url was replaced by $new_image_url." );
24
+
25
+ // Clear cache
26
+ $uploads = wp_upload_dir();
27
+ $cache = trailingslashit( $uploads['basedir'] ) . 'bb-plugin';
28
+ if ( file_exists( $cache ) )
29
+ Meow_MFRH_Core::rmdir_recursive( $cache );
30
+ else {
31
+ $cache = trailingslashit( $uploads['basedir'] ) . 'fl-builder';
32
+ if ( file_exists( $cache ) )
33
+ Meow_MFRH_Core::rmdir_recursive( $cache );
34
+ }
35
+ }
36
+
37
+ ?>
mfrh_custom.php → plugins/custom.php RENAMED
@@ -4,7 +4,6 @@
4
  // For help about this file, check:
5
  // http://meowapps.com/media-file-renamer/faq/
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)
@@ -47,39 +46,4 @@
47
  // }
48
  // =============================================================================
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
-
56
- function mfrh_beaver_builder( $post, $orig_image_url, $new_image_url ) {
57
- global $wpdb;
58
- $query = $wpdb->prepare( "UPDATE $wpdb->postmeta
59
- SET meta_value = REPLACE(meta_value, 's:%d:\"$orig_image_url', 's:%d:\"$new_image_url')
60
- WHERE meta_key = '_fl_builder_data'
61
- OR meta_key = '_fl_builder_draft'",
62
- strlen( $orig_image_url ), strlen( $new_image_url ) );
63
- $query_revert = $wpdb->prepare( "UPDATE $wpdb->postmeta
64
- SET meta_value = REPLACE(meta_value, 's:%d:\"$new_image_url', 's:%d:\"$orig_image_url')
65
- WHERE meta_key = '_fl_builder_data'
66
- OR meta_key = '_fl_builder_draft'",
67
- strlen( $new_image_url ), strlen( $orig_image_url ) );
68
- $wpdb->query( $query );
69
- global $mfrh_core;
70
- $mfrh_core->log_sql( $query, $query_revert );
71
- $mfrh_core->log( "Beaver Metadata like $orig_image_url was replaced by $new_image_url." );
72
-
73
- // Clear cache
74
- $uploads = wp_upload_dir();
75
- $cache = trailingslashit( $uploads['basedir'] ) . 'bb-plugin';
76
- if ( file_exists( $cache ) )
77
- Meow_MFRH_Core::rmdir_recursive( $cache );
78
- else {
79
- $cache = trailingslashit( $uploads['basedir'] ) . 'fl-builder';
80
- if ( file_exists( $cache ) )
81
- Meow_MFRH_Core::rmdir_recursive( $cache );
82
- }
83
- }
84
-
85
  ?>
4
  // For help about this file, check:
5
  // http://meowapps.com/media-file-renamer/faq/
6
 
 
7
  // HANDLE THE RENAMING
8
  // $new is the proposed filename by Media File Renamer (without extension)
9
  // $old is the current filename (without extension)
46
  // }
47
  // =============================================================================
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  ?>
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.4
6
- Stable tag: 4.0.3
7
 
8
  Automatically rename files depending on Media titles dynamically + update links. Pro version has many more options. Check the description :)
9
 
@@ -33,62 +33,9 @@ Languages: English, French.
33
  2. Activate the plugin through the 'Plugins' menu in WordPress
34
  3. Try it with one file first! :)
35
 
36
- == Upgrade Notice ==
37
-
38
- Simply replace `media-file-renamer.php` by the new one.
39
-
40
  == Frequently Asked Questions ==
41
 
42
- Check the FAQ on the official website, here: https://meowapps.com/media-file-renamer/faq/. The following is to enhance the plugin for your own install and is aim to advanced users and developers. If you want to quickly try to use the following filters and actions, please have a look a the file called mfrh_custom.php in the plugin, uncomment some code and hack it :)
43
-
44
- **Change the way the files are renamed**
45
-
46
- If you are willing to customize the way the file are renamed, please use the mfrh_new_filename filter. The $new is the new filename proposed by the plugin, $old is the previous one and $post contains the information about the current attachment.
47
-
48
- `
49
- add_filter( 'mfrh_new_filename', 'my_filter_filename', 10, 3 );
50
-
51
- function my_filter_filename( $new, $old, $post ) {
52
- return "renamed-" . $new;
53
- }
54
- `
55
-
56
- The mfrh_replace_rules filter is a bit simpler and allows you to customize specific characters or bit of strings, like with the example below.
57
-
58
- `
59
- add_filter( 'mfrh_replace_rules', 'replace_s_by_z', 10, 1 );
60
-
61
- function replace_s_by_z( $rules ) {
62
- $rules['s'] = 'z';
63
- return $rules;
64
- }
65
- `
66
-
67
- **Update References**
68
-
69
- You can also update the references to the files or URLs which are renamed/modified by using the following filters. If you themes or another plugins are storing those references by yourself, then this is a chance to achieve this :)
70
-
71
- The $post is an array containing information about the media (as it is like a post), and you can use the $original_image_url and $new_image_url to do the required renaming:
72
-
73
- `
74
- add_action( 'mfrh_url_renamed', 'my_url_renamed', 10, 3 );
75
-
76
- function my_url_renamed( $post, $orig_image_url, $new_image_url ) {
77
- return "renamed-" . $new;
78
- }
79
- `
80
-
81
- This is the same as above but it is about the physical filepath on your filesystem:
82
-
83
- `
84
- add_action( 'mfrh_media_renamed', 'my_media_renamed', 10, 3 );
85
-
86
- function my_media_renamed( $post, $old_filepath, $new_filepath ) {
87
- return "renamed-" . $new;
88
- }
89
- `
90
-
91
- You are welcome to create plugins using Media File Renamer using special rules for renaming. Please tell me you so if you make one and I will list those plugins here.
92
 
93
  == Screenshots ==
94
 
@@ -100,7 +47,8 @@ You are welcome to create plugins using Media File Renamer using special rules f
100
 
101
  == Changelog ==
102
 
103
- = 4.0.3 =
 
104
  * Fix: Insensitive-case match was giving the wrong file in some cases (webp for instance).
105
 
106
  = 4.0.2 =
3
  Tags: rename, file, files, media, manager, image, renamer, wpml, optimization, seo, retina, gutenberg
4
  Requires at least: 4.6
5
  Tested up to: 4.9.4
6
+ Stable tag: 4.0.4
7
 
8
  Automatically rename files depending on Media titles dynamically + update links. Pro version has many more options. Check the description :)
9
 
33
  2. Activate the plugin through the 'Plugins' menu in WordPress
34
  3. Try it with one file first! :)
35
 
 
 
 
 
36
  == Frequently Asked Questions ==
37
 
38
+ Check the FAQ on the official website, [here](https://meowapps.com/media-file-renamer/faq/).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  == Screenshots ==
41
 
47
 
48
  == Changelog ==
49
 
50
+ = 4.0.4 =
51
+ * Fix: Renaming using filters (work in progress).
52
  * Fix: Insensitive-case match was giving the wrong file in some cases (webp for instance).
53
 
54
  = 4.0.2 =
updates.php ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Meow_MFRH_Updates {
4
+
5
+ private $core = null;
6
+ private $admin = null;
7
+
8
+ public function __construct( $core, $admin ) {
9
+ $this->core = $core;
10
+ $this->admin = $admin;
11
+
12
+ // Support for WPML
13
+ if ( function_exists( 'icl_object_id' ) )
14
+ require( 'plugins/wpml.php' );
15
+ // Support for Beaver Builder
16
+ if ( class_exists( 'FLBuilderModel' ) )
17
+ require( 'plugins/beaverbuilder.php' );
18
+ }
19
+
20
+ function init_actions() {
21
+ if ( get_option( "mfrh_update_posts", true ) )
22
+ add_action( 'mfrh_url_renamed', array( $this, 'action_update_posts' ), 10, 3 );
23
+ if ( get_option( "mfrh_update_postmeta", true ) )
24
+ add_action( 'mfrh_url_renamed', array( $this, 'action_update_postmeta' ), 10, 3 );
25
+ if ( get_option( "mfrh_rename_guid" ) )
26
+ add_action( 'mfrh_media_renamed', array( $this, 'action_rename_guid' ), 10, 3 );
27
+ }
28
+
29
+ // Mass update of all the meta with the new filenames
30
+ function action_update_postmeta( $post, $orig_image_url, $new_image_url ) {
31
+ global $wpdb;
32
+ $query = $wpdb->prepare( "UPDATE $wpdb->postmeta
33
+ SET meta_value = '%s'
34
+ WHERE meta_key <> '_original_filename'
35
+ AND (TRIM(meta_value) = '%s'
36
+ OR TRIM(meta_value) = '%s'
37
+ );", $new_image_url, $orig_image_url, str_replace( ' ', '%20', $orig_image_url ) );
38
+ $query_revert = $wpdb->prepare( "UPDATE $wpdb->postmeta
39
+ SET meta_value = '%s'
40
+ WHERE meta_key <> '_original_filename'
41
+ AND meta_value = '%s';
42
+ ", $orig_image_url, $new_image_url );
43
+ $wpdb->query( $query );
44
+ $this->core->log_sql( $query, $query_revert );
45
+ $this->core->log( "Meta\t$orig_image_url -> $new_image_url" );
46
+ }
47
+
48
+ // Mass update of all the articles with the new filenames
49
+ function action_update_posts( $post, $orig_image_url, $new_image_url ) {
50
+ global $wpdb;
51
+
52
+ // Content
53
+ $query = $wpdb->prepare( "UPDATE $wpdb->posts
54
+ SET post_content = REPLACE(post_content, '%s', '%s')
55
+ WHERE post_status != 'inherit'
56
+ AND post_status != 'trash'
57
+ AND post_type != 'attachment'
58
+ AND post_type NOT LIKE '%acf-%'
59
+ AND post_type NOT LIKE '%edd_%'
60
+ AND post_type != 'shop_order'
61
+ AND post_type != 'shop_order_refund'
62
+ AND post_type != 'nav_menu_item'
63
+ AND post_type != 'revision'
64
+ AND post_type != 'auto-draft'", $orig_image_url, $new_image_url );
65
+ $query_revert = $wpdb->prepare( "UPDATE $wpdb->posts
66
+ SET post_content = REPLACE(post_content, '%s', '%s')
67
+ WHERE post_status != 'inherit'
68
+ AND post_status != 'trash'
69
+ AND post_type != 'attachment'
70
+ AND post_type NOT LIKE '%acf-%'
71
+ AND post_type NOT LIKE '%edd_%'
72
+ AND post_type != 'shop_order'
73
+ AND post_type != 'shop_order_refund'
74
+ AND post_type != 'nav_menu_item'
75
+ AND post_type != 'revision'
76
+ AND post_type != 'auto-draft'", $new_image_url, $orig_image_url );
77
+ $wpdb->query( $query );
78
+ $this->core->log_sql( $query, $query_revert );
79
+ $this->core->log( "Content\t$orig_image_url -> $new_image_url" );
80
+
81
+ // Excerpt
82
+ $query = $wpdb->prepare( "UPDATE $wpdb->posts
83
+ SET post_excerpt = REPLACE(post_excerpt, '%s', '%s')
84
+ WHERE post_status != 'inherit'
85
+ AND post_status != 'trash'
86
+ AND post_type != 'attachment'
87
+ AND post_type NOT LIKE '%acf-%'
88
+ AND post_type NOT LIKE '%edd_%'
89
+ AND post_type != 'shop_order'
90
+ AND post_type != 'shop_order_refund'
91
+ AND post_type != 'nav_menu_item'
92
+ AND post_type != 'revision'
93
+ AND post_type != 'auto-draft'", $orig_image_url, $new_image_url );
94
+ $query_revert = $wpdb->prepare( "UPDATE $wpdb->posts
95
+ SET post_excerpt = REPLACE(post_excerpt, '%s', '%s')
96
+ WHERE post_status != 'inherit'
97
+ AND post_status != 'trash'
98
+ AND post_type != 'attachment'
99
+ AND post_type NOT LIKE '%acf-%'
100
+ AND post_type NOT LIKE '%edd_%'
101
+ AND post_type != 'shop_order'
102
+ AND post_type != 'shop_order_refund'
103
+ AND post_type != 'nav_menu_item'
104
+ AND post_type != 'revision'
105
+ AND post_type != 'auto-draft'", $new_image_url, $orig_image_url );
106
+ $wpdb->query( $query );
107
+ $this->core->log_sql( $query, $query_revert );
108
+ $this->core->log( "Excerpt\t$orig_image_url -> $new_image_url" );
109
+ }
110
+
111
+ // The GUID should never be updated but... this will if the option is checked.
112
+ // [TigrouMeow] It the recent version of WordPress, the GUID is not part of the $post (even though it is in database)
113
+ // Explanation: http://pods.io/2013/07/17/dont-use-the-guid-field-ever-ever-ever/
114
+ function action_rename_guid( $post, $old_filepath, $new_filepath ) {
115
+ $meta = wp_get_attachment_metadata( $post['ID'] );
116
+ $old_guid = get_the_guid( $post['ID'] );
117
+ if ( $meta )
118
+ $new_filepath = wp_get_attachment_url( $post['ID'] );
119
+ global $wpdb;
120
+ $query = $wpdb->prepare( "UPDATE $wpdb->posts SET guid = '%s' WHERE ID = '%d'", $new_filepath, $post['ID'] );
121
+ $query_revert = $wpdb->prepare( "UPDATE $wpdb->posts SET guid = '%s' WHERE ID = '%d'", $old_guid, $post['ID'] );
122
+ $this->core->log_sql( $query, $query_revert );
123
+ $wpdb->query( $query );
124
+ clean_post_cache( $post['ID'] );
125
+ $this->core->log( "GUID\t$old_guid -> $new_filepath." );
126
+ }
127
+
128
+ }