Media File Renamer - Version 5.2.3

Version Description

(2021/05/29) = * Fix: The 'Move' feature now also works with the original image (in case it has been scaled by WP).

Download this release

Release Info

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

Code changes from version 5.2.1 to 5.2.3

Files changed (5) hide show
  1. classes/api.php +3 -3
  2. classes/core.php +17 -0
  3. classes/updates.php +104 -56
  4. media-file-renamer.php +2 -2
  5. readme.txt +5 -2
classes/api.php CHANGED
@@ -64,15 +64,15 @@ function mfrh_pathinfo( $path, $options = null ) {
64
  return $r;
65
  }
66
  if ( !$path ) return '';
67
- $path = rtrim( $path, DIRECTORY_SEPARATOR );
68
  $normalized_path = wp_normalize_path( $path );
69
  switch ( $options ) {
70
  case PATHINFO_DIRNAME:
71
- $x = mfrh_mb( 'strrpos', $normalized_path, DIRECTORY_SEPARATOR ); // The last occurrence of slash
72
  return is_int($x) ? mfrh_mb( 'substr', $path, 0, $x ) : '.';
73
 
74
  case PATHINFO_BASENAME:
75
- $x = mfrh_mb( 'strrpos', $normalized_path, DIRECTORY_SEPARATOR ); // The last occurrence of slash
76
  return is_int($x) ? mfrh_mb( 'substr', $path, $x + 1 ) : $path;
77
 
78
  case PATHINFO_EXTENSION:
64
  return $r;
65
  }
66
  if ( !$path ) return '';
67
+ $path = rtrim( $path, '/' . DIRECTORY_SEPARATOR );
68
  $normalized_path = wp_normalize_path( $path );
69
  switch ( $options ) {
70
  case PATHINFO_DIRNAME:
71
+ $x = mfrh_mb( 'strrpos', $normalized_path, '/' ); // The last occurrence of slash
72
  return is_int($x) ? mfrh_mb( 'substr', $path, 0, $x ) : '.';
73
 
74
  case PATHINFO_BASENAME:
75
+ $x = mfrh_mb( 'strrpos', $normalized_path, '/' ); // The last occurrence of slash
76
  return is_int($x) ? mfrh_mb( 'substr', $path, $x + 1 ) : $path;
77
 
78
  case PATHINFO_EXTENSION:
classes/core.php CHANGED
@@ -708,6 +708,23 @@ SQL;
708
  $has_thumbnails = isset( $meta['sizes'] );
709
 
710
  if ( $has_thumbnails ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
711
  $orig_image_urls = array();
712
  $orig_image_data = wp_get_attachment_image_src( $id, 'full' );
713
  $orig_image_urls['full'] = $orig_image_data[0];
708
  $has_thumbnails = isset( $meta['sizes'] );
709
 
710
  if ( $has_thumbnails ) {
711
+
712
+ // Support for the original image if it was "-rescaled".
713
+ $is_scaled_image = isset( $meta['original_image'] ) && !empty( $meta['original_image'] );
714
+ if ( $is_scaled_image ) {
715
+ $meta_old_filename = $meta['original_image'];
716
+ $meta_old_filepath = trailingslashit( $upload_dir['basedir'] ) . trailingslashit( $old_directory ) . $meta_old_filename;
717
+ $meta_new_filepath = trailingslashit( $upload_dir['basedir'] ) . trailingslashit( $new_directory ) . $meta_old_filename;
718
+ if ( !$this->rename_file( $meta_old_filepath, $meta_new_filepath ) ) {
719
+ $this->log( "🚫 File $meta_old_filepath ➡️ $meta_new_filepath" );
720
+ }
721
+ else {
722
+ $this->log( "✅ File $meta_old_filepath ➡️ $meta_new_filepath" );
723
+ do_action( 'mfrh_path_renamed', $post, $meta_old_filepath, $meta_new_filepath );
724
+ }
725
+ }
726
+
727
+ // Image Sizes (Thumbnails)
728
  $orig_image_urls = array();
729
  $orig_image_data = wp_get_attachment_image_src( $id, 'full' );
730
  $orig_image_urls['full'] = $orig_image_data[0];
classes/updates.php CHANGED
@@ -1,7 +1,19 @@
1
  <?php
2
 
3
  class Meow_MFRH_Updates {
 
4
  private $core = null;
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  public function __construct( $core ) {
7
  $this->core = $core;
@@ -30,85 +42,121 @@ class Meow_MFRH_Updates {
30
  // Mass update of all the meta with the new filenames
31
  function action_update_postmeta( $post, $orig_image_url, $new_image_url ) {
32
  global $wpdb;
33
- $query = $wpdb->prepare( "UPDATE $wpdb->postmeta
34
- SET meta_value = '%s'
35
- WHERE meta_key <> '_original_filename'
 
36
  AND (TRIM(meta_value) = '%s'
37
  OR TRIM(meta_value) = '%s'
38
- );", $new_image_url, $orig_image_url, str_replace( ' ', '%20', $orig_image_url ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  $query_revert = $wpdb->prepare( "UPDATE $wpdb->postmeta
40
  SET meta_value = '%s'
41
- WHERE meta_key <> '_original_filename'
42
- AND meta_value = '%s';
43
- ", $orig_image_url, $new_image_url );
44
- $wpdb->query( $query );
45
  $this->core->log_sql( $query, $query_revert );
 
 
 
 
46
  $this->core->log( "🚀 Rewrite meta $orig_image_url ➡️ $new_image_url" );
47
  }
48
 
49
  // Mass update of all the articles with the new filenames
50
  function action_update_posts( $post, $orig_image_url, $new_image_url ) {
 
 
 
 
 
 
 
 
 
 
 
 
51
  global $wpdb;
52
 
53
- // Content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  $query = $wpdb->prepare( "UPDATE $wpdb->posts
55
  SET post_content = REPLACE(post_content, '%s', '%s')
56
- WHERE post_status != 'inherit'
57
- AND post_status != 'trash'
58
- AND post_type != 'attachment'
59
- AND post_type NOT LIKE '%acf-%'
60
- AND post_type NOT LIKE '%edd_%'
61
- AND post_type != 'shop_order'
62
- AND post_type != 'shop_order_refund'
63
- AND post_type != 'nav_menu_item'
64
- AND post_type != 'revision'
65
- AND post_type != 'auto-draft'", $orig_image_url, $new_image_url );
66
  $query_revert = $wpdb->prepare( "UPDATE $wpdb->posts
67
  SET post_content = REPLACE(post_content, '%s', '%s')
68
- WHERE post_status != 'inherit'
69
- AND post_status != 'trash'
70
- AND post_type != 'attachment'
71
- AND post_type NOT LIKE '%acf-%'
72
- AND post_type NOT LIKE '%edd_%'
73
- AND post_type != 'shop_order'
74
- AND post_type != 'shop_order_refund'
75
- AND post_type != 'nav_menu_item'
76
- AND post_type != 'revision'
77
- AND post_type != 'auto-draft'", $new_image_url, $orig_image_url );
78
- $wpdb->query( $query );
79
  $this->core->log_sql( $query, $query_revert );
80
- $this->core->log( "🚀 Rewrite content $orig_image_url ➡️ $new_image_url" );
 
 
 
 
 
 
 
 
81
 
82
- // Excerpt
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  $query = $wpdb->prepare( "UPDATE $wpdb->posts
84
  SET post_excerpt = REPLACE(post_excerpt, '%s', '%s')
85
- WHERE post_status != 'inherit'
86
- AND post_status != 'trash'
87
- AND post_type != 'attachment'
88
- AND post_type NOT LIKE '%acf-%'
89
- AND post_type NOT LIKE '%edd_%'
90
- AND post_type != 'shop_order'
91
- AND post_type != 'shop_order_refund'
92
- AND post_type != 'nav_menu_item'
93
- AND post_type != 'revision'
94
- AND post_type != 'auto-draft'", $orig_image_url, $new_image_url );
95
  $query_revert = $wpdb->prepare( "UPDATE $wpdb->posts
96
  SET post_excerpt = REPLACE(post_excerpt, '%s', '%s')
97
- WHERE post_status != 'inherit'
98
- AND post_status != 'trash'
99
- AND post_type != 'attachment'
100
- AND post_type NOT LIKE '%acf-%'
101
- AND post_type NOT LIKE '%edd_%'
102
- AND post_type != 'shop_order'
103
- AND post_type != 'shop_order_refund'
104
- AND post_type != 'nav_menu_item'
105
- AND post_type != 'revision'
106
- AND post_type != 'auto-draft'", $new_image_url, $orig_image_url );
107
- $wpdb->query( $query );
108
  $this->core->log_sql( $query, $query_revert );
109
- $this->core->log( "🚀 Rewrite excerpts $orig_image_url ➡️ $new_image_url" );
110
- }
111
-
 
112
  // The GUID should never be updated but... this will if the option is checked.
113
  // [TigrouMeow] It the recent version of WordPress, the GUID is not part of the $post (even though it is in database)
114
  // Explanation: http://pods.io/2013/07/17/dont-use-the-guid-field-ever-ever-ever/
1
  <?php
2
 
3
  class Meow_MFRH_Updates {
4
+
5
  private $core = null;
6
+ private $useless_types_conditions = array(
7
+ "post_status != 'trash'",
8
+ "post_type != 'attachment'",
9
+ "post_type NOT LIKE '%acf-%'",
10
+ "post_type NOT LIKE '%edd_%'",
11
+ "post_type != 'shop_order'",
12
+ "post_type != 'shop_order_refund'",
13
+ "post_type != 'nav_menu_item'",
14
+ "post_type != 'revision'",
15
+ "post_type != 'auto-draft'"
16
+ );
17
 
18
  public function __construct( $core ) {
19
  $this->core = $core;
42
  // Mass update of all the meta with the new filenames
43
  function action_update_postmeta( $post, $orig_image_url, $new_image_url ) {
44
  global $wpdb;
45
+
46
+ $query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta
47
+ WHERE meta_value LIKE '%s'
48
+ AND meta_key <> '_original_filename'
49
  AND (TRIM(meta_value) = '%s'
50
  OR TRIM(meta_value) = '%s'
51
+ )", $new_image_url, $orig_image_url, str_replace( ' ', '%20', $orig_image_url ) );
52
+ $ids = $wpdb->get_col( $query );
53
+ if ( empty( $ids ) ) {
54
+ return array();
55
+ }
56
+
57
+ // Prepare SQL (WHERE IN)
58
+ $ids_to_update = array_map(function( $id ) { return "'" . esc_sql( $id ) . "'"; }, $ids );
59
+ $ids_to_update = implode(',', $ids_to_update);
60
+
61
+ // Execute updates
62
+ $query = $wpdb->prepare( "UPDATE $wpdb->posts
63
+ SET meta_value = %s
64
+ WHERE ID IN (" . $ids_to_update . ")", $new_image_url );
65
+ $wpdb->query( $query );
66
+
67
+ // Reverse updates & log
68
  $query_revert = $wpdb->prepare( "UPDATE $wpdb->postmeta
69
  SET meta_value = '%s'
70
+ WHERE ID IN (" . $ids_to_update . ")", $new_image_url );
 
 
 
71
  $this->core->log_sql( $query, $query_revert );
72
+
73
+ // Reset meta cache
74
+ update_meta_cache( 'post', $ids );
75
+
76
  $this->core->log( "🚀 Rewrite meta $orig_image_url ➡️ $new_image_url" );
77
  }
78
 
79
  // Mass update of all the articles with the new filenames
80
  function action_update_posts( $post, $orig_image_url, $new_image_url ) {
81
+ $ids = $this->bulk_rename_content( $orig_image_url, $new_image_url );
82
+ $this->core->log( "🚀 Rewrite content $orig_image_url ➡️ $new_image_url" );
83
+ $more_ids = $this->bulk_rename_excerpts( $orig_image_url, $new_image_url );
84
+ $this->core->log( "🚀 Rewrite excerpts $orig_image_url ➡️ $new_image_url" );
85
+
86
+ // Reset post cache
87
+ if ( !empty( $ids ) && !empty( $more_ids ) ) {
88
+ array_walk( array_merge( $ids, $more_ids ), 'clean_post_cache' );
89
+ }
90
+ }
91
+
92
+ function bulk_rename_content( $orig_image_url, $new_image_url ) {
93
  global $wpdb;
94
 
95
+ // Conditions to avoid useless posts (which aren't related to content)
96
+ $sql_conditions = implode( ' AND ', $this->useless_types_conditions );
97
+
98
+ // Get the IDs that require an update
99
+ $query = $wpdb->prepare( "SELECT ID FROM $wpdb->posts
100
+ WHERE post_content LIKE '%s'
101
+ AND $sql_conditions", '%' . $orig_image_url . '%' );
102
+ $ids = $wpdb->get_col( $query );
103
+ if ( empty( $ids ) ) {
104
+ return array();
105
+ }
106
+
107
+ // Prepare SQL (WHERE IN)
108
+ $ids_to_update = array_map(function( $id ) { return "'" . esc_sql( $id ) . "'"; }, $ids );
109
+ $ids_to_update = implode(',', $ids_to_update);
110
+
111
+ // Execute updates
112
  $query = $wpdb->prepare( "UPDATE $wpdb->posts
113
  SET post_content = REPLACE(post_content, '%s', '%s')
114
+ WHERE ID IN (" . $ids_to_update . ")", $orig_image_url, $new_image_url );
115
+ $wpdb->query( $query );
116
+
117
+ // Reverse updates & log
 
 
 
 
 
 
118
  $query_revert = $wpdb->prepare( "UPDATE $wpdb->posts
119
  SET post_content = REPLACE(post_content, '%s', '%s')
120
+ WHERE ID IN (" . $ids_to_update . ")", $orig_image_url, $new_image_url );
 
 
 
 
 
 
 
 
 
 
121
  $this->core->log_sql( $query, $query_revert );
122
+
123
+ return $ids;
124
+ }
125
+
126
+ function bulk_rename_excerpts( $orig_image_url, $new_image_url ) {
127
+ global $wpdb;
128
+
129
+ // Conditions to avoid useless posts (which aren't related to content)
130
+ $sql_conditions = implode( ' AND ', $this->useless_types_conditions );
131
 
132
+ // Get the IDs that require an update
133
+ $query = $wpdb->prepare( "SELECT ID FROM $wpdb->posts
134
+ WHERE post_excerpt LIKE '%s'
135
+ AND $sql_conditions", '%' . $orig_image_url . '%' );
136
+ $ids = $wpdb->get_col( $query );
137
+ if ( empty( $ids ) ) {
138
+ return array();
139
+ }
140
+
141
+ // Prepare SQL (WHERE IN)
142
+ $ids_to_update = array_map(function( $id ) { return "'" . esc_sql( $id ) . "'"; }, $ids );
143
+ $ids_to_update = implode(',', $ids_to_update);
144
+
145
+ // Execute updates
146
  $query = $wpdb->prepare( "UPDATE $wpdb->posts
147
  SET post_excerpt = REPLACE(post_excerpt, '%s', '%s')
148
+ WHERE ID IN (" . $ids_to_update . ")", $orig_image_url, $new_image_url );
149
+ $wpdb->query( $query );
150
+
151
+ // Reverse updates & log
 
 
 
 
 
 
152
  $query_revert = $wpdb->prepare( "UPDATE $wpdb->posts
153
  SET post_excerpt = REPLACE(post_excerpt, '%s', '%s')
154
+ WHERE ID IN (" . $ids_to_update . ")", $orig_image_url, $new_image_url );
 
 
 
 
 
 
 
 
 
 
155
  $this->core->log_sql( $query, $query_revert );
156
+
157
+ return $ids;
158
+ }
159
+
160
  // The GUID should never be updated but... this will if the option is checked.
161
  // [TigrouMeow] It the recent version of WordPress, the GUID is not part of the $post (even though it is in database)
162
  // Explanation: http://pods.io/2013/07/17/dont-use-the-guid-field-ever-ever-ever/
media-file-renamer.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Media File Renamer
4
  Plugin URI: https://meowapps.com
5
  Description: Renames your media files for better SEO and a nicer filesystem (automatically or manually).
6
- Version: 5.2.1
7
  Author: Jordy Meow
8
  Author URI: https://meowapps.com
9
  Text Domain: media-file-renamer
@@ -14,7 +14,7 @@ Originally developed for two of my websites:
14
  - Haikyo (https://haikyo.org)
15
  */
16
 
17
- define( 'MFRH_VERSION', '5.2.1' );
18
  define( 'MFRH_PREFIX', 'mfrh' );
19
  define( 'MFRH_DOMAIN', 'media-file-renamer' );
20
  define( 'MFRH_ENTRY', __FILE__ );
3
  Plugin Name: Media File Renamer
4
  Plugin URI: https://meowapps.com
5
  Description: Renames your media files for better SEO and a nicer filesystem (automatically or manually).
6
+ Version: 5.2.3
7
  Author: Jordy Meow
8
  Author URI: https://meowapps.com
9
  Text Domain: media-file-renamer
14
  - Haikyo (https://haikyo.org)
15
  */
16
 
17
+ define( 'MFRH_VERSION', '5.2.3' );
18
  define( 'MFRH_PREFIX', 'mfrh' );
19
  define( 'MFRH_DOMAIN', 'media-file-renamer' );
20
  define( 'MFRH_ENTRY', __FILE__ );
readme.txt CHANGED
@@ -5,7 +5,7 @@ Donate link: https://commerce.coinbase.com/checkout/d047546a-77a8-41c8-9ea9-4a95
5
  Requires at least: 5.0
6
  Tested up to: 5.7
7
  Requires PHP: 5.6
8
- Stable tag: 5.2.1
9
 
10
  Renames your media files for better SEO and a nicer filesystem (automatically or manually).
11
 
@@ -67,7 +67,10 @@ The plugin can be tweaked in many ways, there are many actions and filters avail
67
 
68
  == Changelog ==
69
 
70
- = 5.2.1 (2021/05/17) =
 
 
 
71
  * Fix: Better Windows support.
72
 
73
  = 5.2.0 (2021/05/15) =
5
  Requires at least: 5.0
6
  Tested up to: 5.7
7
  Requires PHP: 5.6
8
+ Stable tag: 5.2.3
9
 
10
  Renames your media files for better SEO and a nicer filesystem (automatically or manually).
11
 
67
 
68
  == Changelog ==
69
 
70
+ = 5.2.3 (2021/05/29) =
71
+ * Fix: The 'Move' feature now also works with the original image (in case it has been scaled by WP).
72
+
73
+ = 5.2.2 (2021/05/18) =
74
  * Fix: Better Windows support.
75
 
76
  = 5.2.0 (2021/05/15) =