Enable Media Replace - Version 3.2.9

Version Description

  • properly replace thumbnails names in the content when the replaced image has a different aspect ratio, thus the new thumbnails have a different height in the name.
Download this release

Release Info

Developer ShortPixel
Plugin Icon 128x128 Enable Media Replace
Version 3.2.9
Comparing to
See all releases

Code changes from version 3.2.8 to 3.2.9

Files changed (4) hide show
  1. enable-media-replace.php +1 -1
  2. readme.txt +4 -1
  3. thumbnail_updater.php +124 -0
  4. upload.php +23 -9
enable-media-replace.php CHANGED
@@ -62,7 +62,7 @@ function dat_mime_types($mime_types) {
62
  * To suppress it in the menu we give it an empty menu title.
63
  */
64
  function emr_menu() {
65
- add_submenu_page(NULL, esc_html__("Replace media", "enable-media-replace"), '','upload_files', 'enable-media-replace/enable-media-replace', 'emr_options');
66
  }
67
 
68
  /**
62
  * To suppress it in the menu we give it an empty menu title.
63
  */
64
  function emr_menu() {
65
+ add_submenu_page('upload.php', esc_html__("Replace media", "enable-media-replace"), '','upload_files', 'enable-media-replace/enable-media-replace', 'emr_options');
66
  }
67
 
68
  /**
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: ShortPixel
3
  Donate link: https://www.paypal.me/resizeImage
4
  Tags: replace, attachment, media, files, replace image, replace jpg, change media, replace media, image, file
5
  Requires at least: 4.0
6
- Tested up to: 5.0
7
  Requires PHP: 5.4
8
  Stable tag: trunk
9
 
@@ -47,6 +47,9 @@ If you want more control over the format used to display the time, you can use t
47
 
48
  == Changelog ==
49
 
 
 
 
50
  = 3.2.8 =
51
  * fix for failures in link updating when replacing file because of addslashes - use prepared query instead
52
  * replace basename with wp_basename because basename doesn't work well with UTF8
3
  Donate link: https://www.paypal.me/resizeImage
4
  Tags: replace, attachment, media, files, replace image, replace jpg, change media, replace media, image, file
5
  Requires at least: 4.0
6
+ Tested up to: 5.1
7
  Requires PHP: 5.4
8
  Stable tag: trunk
9
 
47
 
48
  == Changelog ==
49
 
50
+ = 3.2.9 =
51
+ * properly replace thumbnails names in the content when the replaced image has a different aspect ratio, thus the new thumbnails have a different height in the name.
52
+
53
  = 3.2.8 =
54
  * fix for failures in link updating when replacing file because of addslashes - use prepared query instead
55
  * replace basename with wp_basename because basename doesn't work well with UTF8
thumbnail_updater.php ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ( ! defined( 'ABSPATH' ) )
3
+ exit; // Exit if accessed directly.
4
+
5
+ /* Simple class for updating thumbnails.
6
+ *
7
+ *
8
+ *
9
+ */
10
+ class ThumbnailUpdater
11
+ {
12
+ protected $attach_id;
13
+ protected $oldMeta = array();
14
+ protected $newMeta = array();
15
+
16
+ protected $convertArray = array();
17
+ protected $relPath;
18
+
19
+ protected $post_table;
20
+
21
+ public function __construct($id)
22
+ {
23
+ $this->attach_id = intval($id);
24
+
25
+ global $wpdb;
26
+ $table_name = $wpdb->prefix . "posts";
27
+ // $postmeta_table_name = $wpdb->prefix . "postmeta";
28
+
29
+ $this->post_table = $table_name;
30
+ }
31
+
32
+ public function setOldMetadata($metadata)
33
+ {
34
+ if (isset($metadata['sizes']))
35
+ $this->oldMeta = $metadata;
36
+ }
37
+
38
+ public function setNewMetadata($metadata)
39
+ {
40
+ if (isset($metadata['sizes']))
41
+ $this->newMeta = $metadata;
42
+
43
+ // extract month prefix to prevent overwriting wrong images.
44
+ $file = $metadata['file'];
45
+ $pos = strrpos($metadata['file'], '/');
46
+ $month_path = substr($file, 0, $pos);
47
+ $this->relPath = trailingslashit($month_path);
48
+ }
49
+
50
+ public function updateThumbnails()
51
+ {
52
+ if (count($this->oldMeta) == 0 || count($this->newMeta) == 0)
53
+ return false;
54
+
55
+ $convertArray = array();
56
+ foreach($this->oldMeta['sizes'] as $sizeName => $data)
57
+ {
58
+ if (isset($this->newMeta['sizes'][$sizeName]))
59
+ {
60
+
61
+ $oldFile = $data['file'];
62
+ $newFile = $this->newMeta['sizes'][$sizeName]['file'];
63
+
64
+ // if images are not same size.
65
+ if ($oldFile != $newFile)
66
+ {
67
+ $this->convertArray[] = array('imageFrom' => $this->relPath . $oldFile, 'imageTo' => $this->relPath . $newFile );
68
+ }
69
+
70
+ }
71
+ else {
72
+ $this->findNearestSize($data, $sizeName);
73
+ }
74
+
75
+ }
76
+ $this->updateDatabase();
77
+
78
+ }
79
+
80
+ protected function updateDatabase()
81
+ {
82
+ global $wpdb;
83
+ $sql = "UPDATE " . $this->post_table . " set post_content = REPLACE(post_content, %s, %s)";
84
+
85
+ foreach($this->convertArray as $convert_item)
86
+ {
87
+ $from = $convert_item['imageFrom'];
88
+ $to = $convert_item['imageTo'];
89
+
90
+ $replace_sql = $wpdb->prepare($sql, $from, $to );
91
+ $wpdb->query($replace_sql);
92
+
93
+ }
94
+ }
95
+
96
+ /** FindNearestsize
97
+ * This works on the assumption that when the exact image size name is not available, find the nearest width with the smallest possible difference to impact the site the least.
98
+ */
99
+ protected function findNearestSize($oldData, $sizeName)
100
+ {
101
+ $old_width = $oldData['width']; // the width from size not in new image
102
+ $new_width = $this->newMeta['width']; // default check - new width on image
103
+
104
+ $diff = abs($old_width - $new_width);
105
+ $closest_file = str_replace($this->relPath, '', $this->newMeta['file']);
106
+
107
+ foreach($this->newMeta['sizes'] as $sizeName => $data)
108
+ {
109
+ $thisdiff = abs($old_width - $data['width']);
110
+
111
+ if ( $thisdiff < $diff )
112
+ {
113
+ $diff = $thisdiff;
114
+ $closest_file = $data['file'];
115
+ $found_metasize = true;
116
+ }
117
+ }
118
+
119
+ $this->convertArray[] = array('imageFrom' => $this->relPath . $oldData['file'], 'imageTo' => $this->relPath . $closest_file);
120
+
121
+ }
122
+
123
+
124
+ }
upload.php CHANGED
@@ -10,6 +10,8 @@ global $wpdb;
10
  $table_name = $wpdb->prefix . "posts";
11
  $postmeta_table_name = $wpdb->prefix . "postmeta";
12
 
 
 
13
  /**
14
  * Delete a media file and its thumbnails.
15
  *
@@ -21,7 +23,7 @@ function emr_delete_current_files( $current_file, $metadta = null ) {
21
 
22
  // Find path of current file
23
  $current_path = substr($current_file, 0, (strrpos($current_file, "/")));
24
-
25
  // Check if old file exists first
26
  if (file_exists($current_file)) {
27
  // Now check for correct file permissions for old file
@@ -33,10 +35,10 @@ function emr_delete_current_files( $current_file, $metadta = null ) {
33
  else {
34
  // File exists, but has wrong permissions. Let the user know.
35
  printf( esc_html__('The file %1$s can not be deleted by the web server, most likely because the permissions on the file are wrong.', "enable-media-replace"), $current_file);
36
- exit;
37
  }
38
  }
39
-
40
  // Delete old resized versions if this was an image
41
  $suffix = substr($current_file, (strlen($current_file)-4));
42
  $prefix = substr($current_file, 0, (strlen($current_file)-4));
@@ -53,7 +55,6 @@ function emr_delete_current_files( $current_file, $metadta = null ) {
53
  if ( empty( $metadata ) ) {
54
  $metadata = wp_get_attachment_metadata( $_POST["ID"] );
55
  }
56
- // var_dump($metadata);exit;
57
 
58
  if (is_array($metadata)) { // Added fix for error messages when there is no metadata (but WHY would there not be? I don't know…)
59
  foreach($metadata["sizes"] AS $thissize) {
@@ -230,6 +231,9 @@ if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
230
  exit;
231
  }
232
 
 
 
 
233
  $new_filename = $_FILES["userfile"]["name"];
234
  $new_filesize = $_FILES["userfile"]["size"];
235
  $new_filetype = $filedata["type"];
@@ -270,10 +274,17 @@ if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
270
  do_action('wp_handle_upload', array('file' => $current_file, 'url' => wp_get_attachment_url($ID), 'type' => $new_filetype));
271
 
272
  // Make thumb and/or update metadata
273
- wp_update_attachment_metadata( $ID, wp_generate_attachment_metadata( $ID, $current_file ) );
 
 
 
 
274
 
275
- // Trigger possible updates on CDN and other plugins
 
276
  update_attached_file( $ID, $current_file);
 
 
277
  } elseif ( 'replace_and_search' == $replace_type && apply_filters( 'emr_enable_replace_and_search', true ) ) {
278
  // Replace file, replace file name, update meta data, replace links pointing to old file name
279
 
@@ -316,7 +327,7 @@ if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
316
  "SELECT meta_value FROM $postmeta_table_name WHERE meta_key = '_wp_attached_file' AND post_id = %d;",
317
  $ID
318
  );
319
-
320
  $old_meta_name = $wpdb->get_row($sql, ARRAY_A);
321
  $old_meta_name = $old_meta_name["meta_value"];
322
 
@@ -369,7 +380,10 @@ if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
369
  }
370
  }
371
 
372
- // Trigger possible updates on CDN and other plugins
 
 
 
373
  update_attached_file( $ID, $new_file );
374
  }
375
 
@@ -383,7 +397,7 @@ if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
383
  } else {
384
  //TODO Better error handling when no file is selected.
385
  //For now just go back to media management
386
- $returnurl = admin_url("/wp-admin/upload.php");
387
  }
388
 
389
  if (FORCE_SSL_ADMIN) {
10
  $table_name = $wpdb->prefix . "posts";
11
  $postmeta_table_name = $wpdb->prefix . "postmeta";
12
 
13
+ require_once('thumbnail_updater.php');
14
+
15
  /**
16
  * Delete a media file and its thumbnails.
17
  *
23
 
24
  // Find path of current file
25
  $current_path = substr($current_file, 0, (strrpos($current_file, "/")));
26
+
27
  // Check if old file exists first
28
  if (file_exists($current_file)) {
29
  // Now check for correct file permissions for old file
35
  else {
36
  // File exists, but has wrong permissions. Let the user know.
37
  printf( esc_html__('The file %1$s can not be deleted by the web server, most likely because the permissions on the file are wrong.', "enable-media-replace"), $current_file);
38
+ exit;
39
  }
40
  }
41
+
42
  // Delete old resized versions if this was an image
43
  $suffix = substr($current_file, (strlen($current_file)-4));
44
  $prefix = substr($current_file, 0, (strlen($current_file)-4));
55
  if ( empty( $metadata ) ) {
56
  $metadata = wp_get_attachment_metadata( $_POST["ID"] );
57
  }
 
58
 
59
  if (is_array($metadata)) { // Added fix for error messages when there is no metadata (but WHY would there not be? I don't know…)
60
  foreach($metadata["sizes"] AS $thissize) {
231
  exit;
232
  }
233
 
234
+ $thumbUpdater = new ThumbnailUpdater($ID);
235
+ $thumbUpdater->setOldMetadata($current_metadata);
236
+
237
  $new_filename = $_FILES["userfile"]["name"];
238
  $new_filesize = $_FILES["userfile"]["size"];
239
  $new_filetype = $filedata["type"];
274
  do_action('wp_handle_upload', array('file' => $current_file, 'url' => wp_get_attachment_url($ID), 'type' => $new_filetype));
275
 
276
  // Make thumb and/or update metadata
277
+ $metadata = wp_generate_attachment_metadata( $ID, $current_file );
278
+ wp_update_attachment_metadata( $ID, $metadata );
279
+
280
+ $thumbUpdater->setNewMetadata($metadata);
281
+ $thumbUpdater->updateThumbnails();
282
 
283
+
284
+ // Trigger possible updates on CDN and other plugins
285
  update_attached_file( $ID, $current_file);
286
+
287
+
288
  } elseif ( 'replace_and_search' == $replace_type && apply_filters( 'emr_enable_replace_and_search', true ) ) {
289
  // Replace file, replace file name, update meta data, replace links pointing to old file name
290
 
327
  "SELECT meta_value FROM $postmeta_table_name WHERE meta_key = '_wp_attached_file' AND post_id = %d;",
328
  $ID
329
  );
330
+
331
  $old_meta_name = $wpdb->get_row($sql, ARRAY_A);
332
  $old_meta_name = $old_meta_name["meta_value"];
333
 
380
  }
381
  }
382
 
383
+ $thumbUpdater->setNewMetadata($new_metadata);
384
+ $thumbUpdater->updateThumbnails();
385
+
386
+ // Trigger possible updates on CDN and other plugins
387
  update_attached_file( $ID, $new_file );
388
  }
389
 
397
  } else {
398
  //TODO Better error handling when no file is selected.
399
  //For now just go back to media management
400
+ $returnurl = admin_url("upload.php");
401
  }
402
 
403
  if (FORCE_SSL_ADMIN) {