Enable Media Replace - Version 3.4.2

Version Description

Release date: August 17th 2020 * New - Also display files size in the replacement windows; * Fix - Replacing image with pdf file would cause noticesi; * Fix - Compensation for faulty WordPress installation that return empty error string on wp_upload_dir and falsely return relative paths and urls on WP core functions; * Fix - When choosing 'replace file and update names' and then uploading a file with identical name as source, it would postfix the duplicate name. This is no longer the case; * Fix - Wording clarification for the situation when the filename is changed, which will lead to broken links from external sites; * Fix - Added hard paths to require_once satements in order to prevent WP-CLI errrors; * Fix - If source image doesn't exist or in error state, replacement would be prevented by Javascript issue.

Download this release

Release Info

Developer petredobrescu
Plugin Icon 128x128 Enable Media Replace
Version 3.4.2
Comparing to
See all releases

Code changes from version 3.4.1 to 3.4.2

build/shortpixel/autoload.php CHANGED
@@ -2,4 +2,3 @@
2
  require_once (dirname(__FILE__) . "/PackageLoader.php");
3
  $loader = new EnableMediaReplace\Build\PackageLoader();
4
  $loader->load(__DIR__);
5
-
2
  require_once (dirname(__FILE__) . "/PackageLoader.php");
3
  $loader = new EnableMediaReplace\Build\PackageLoader();
4
  $loader->load(__DIR__);
 
classes/file.php CHANGED
@@ -66,6 +66,11 @@ class emrFile
66
  @chmod($this->file, $permissions);
67
  }
68
 
 
 
 
 
 
69
  public function getFilePath()
70
  {
71
  return $this->filePath;
66
  @chmod($this->file, $permissions);
67
  }
68
 
69
+ public function getFileSize()
70
+ {
71
+ return filesize($this->file);
72
+ }
73
+
74
  public function getFilePath()
75
  {
76
  return $this->filePath;
classes/replacer.php CHANGED
@@ -49,6 +49,16 @@ class Replacer
49
  else
50
  $source_file = trim(get_attached_file($post_id, apply_filters( 'emr_unfiltered_get_attached_file', true )));
51
 
 
 
 
 
 
 
 
 
 
 
52
  Log::addDebug('SourceFile ' . $source_file);
53
  $this->sourceFile = new File($source_file);
54
 
@@ -92,9 +102,7 @@ class Replacer
92
  public function replaceWith($file, $fileName)
93
  {
94
  global $wpdb;
95
- //$this->targetFile = new File($file);
96
  $this->targetName = $fileName;
97
- //$this->targetFile = new File($file); // this will point to /tmp!
98
 
99
  $targetFile = $this->getTargetFile();
100
 
@@ -105,8 +113,6 @@ class Replacer
105
  // throw new \RuntimeException($ex);
106
  }
107
 
108
-
109
-
110
  $targetFileObj = new File($targetFile);
111
  $result = $targetFileObj->checkAndCreateFolder();
112
  if ($result === false)
@@ -134,8 +140,11 @@ class Replacer
134
  }
135
 
136
  // update the file attached. This is required for wp_get_attachment_url to work.
137
- update_attached_file($this->post_id, $this->targetFile->getFullFilePath() );
138
- $this->target_url = wp_get_attachment_url($this->post_id);
 
 
 
139
 
140
  // Run the filter, so other plugins can hook if needed.
141
  $filtered = apply_filters( 'wp_handle_upload', array(
@@ -156,6 +165,7 @@ class Replacer
156
  wp_update_attachment_metadata( $this->post_id, $metadata );
157
  $this->target_metadata = $metadata;
158
 
 
159
  /** If author is different from replacer, note this */
160
  $author_id = get_post_meta($this->post_id, '_emr_replace_author', true);
161
 
@@ -177,10 +187,11 @@ class Replacer
177
  $update_ar = array('ID' => $this->post_id);
178
  $update_ar['post_title'] = $title;
179
  $update_ar['post_name'] = sanitize_title($title);
180
- // $update_ar['guid'] = wp_get_attachment_url($this->post_id);
181
  $update_ar['post_mime_type'] = $this->targetFile->getFileMime();
182
  $post_id = \wp_update_post($update_ar, true);
183
 
 
184
  // update post doesn't update GUID on updates.
185
  $wpdb->update( $wpdb->posts, array( 'guid' => $this->target_url), array('ID' => $this->post_id) );
186
  //enable-media-replace-upload-done
@@ -293,8 +304,19 @@ class Replacer
293
  }
294
  $path = $this->target_location; // if all went well.
295
  }
296
- $unique = wp_unique_filename($path, $this->targetName);
 
297
 
 
 
 
 
 
 
 
 
 
 
298
  $new_filename = apply_filters( 'emr_unique_filename', $unique, $path, $this->post_id );
299
  $targetFile = trailingslashit($path) . $new_filename;
300
  }
@@ -315,10 +337,37 @@ class Replacer
315
  return null;
316
  }
317
  }
318
-
319
  return $targetFile;
320
  }
321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322
  /** Tries to remove all of the old image, without touching the metadata in database
323
  * This might fail on certain files, but this is not an indication of success ( remove might fail, but overwrite can still work)
324
  */
@@ -328,7 +377,7 @@ class Replacer
328
  $backup_sizes = get_post_meta( $this->post_id, '_wp_attachment_backup_sizes', true );
329
 
330
  // this must be -scaled if that exists, since wp_delete_attachment_files checks for original_files but doesn't recheck if scaled is included since that the one 'that exists' in WP . $this->source_file replaces original image, not the -scaled one.
331
- $file = get_attached_file($this->post_id);
332
  $result = \wp_delete_attachment_files($this->post_id, $meta, $backup_sizes, $file );
333
 
334
  }
@@ -433,6 +482,8 @@ class Replacer
433
  }
434
  }
435
 
 
 
436
  /* If on the other hand, some sizes are available in source, but not in target, try to replace them with something closeby. */
437
  foreach($search_urls as $size => $url)
438
  {
@@ -459,7 +510,7 @@ class Replacer
459
  */
460
  foreach($search_urls as $size => $url)
461
  {
462
- $replace_url = $replace_urls[$size];
463
  if ($url == $replace_url) // if source and target as the same, no need for replacing.
464
  {
465
  unset($search_urls[$size]);
@@ -477,7 +528,7 @@ class Replacer
477
  }
478
 
479
  Log::addDebug('Doing meta search and replace -', array($search_urls, $replace_urls) );
480
- Log::addDebug('Searching with BaseuRL' . $current_base_url);
481
 
482
  /* Search and replace in WP_POSTS */
483
  // Removed $wpdb->remove_placeholder_escape from here, not compatible with WP 4.8
@@ -557,6 +608,7 @@ class Replacer
557
  {
558
  Log::addDebug('Found JSON Content');
559
  $content = json_decode($content);
 
560
  }
561
 
562
  if (is_string($content)) // let's check the normal one first.
@@ -588,7 +640,7 @@ class Replacer
588
  {
589
  Log::addDebug('Value was found to be JSON, encoding');
590
  // wp-slash -> WP does stripslashes_deep which destroys JSON
591
- $content = wp_slash(json_encode($content, JSON_UNESCAPED_SLASHES));
592
  Log::addDebug('Content returning', array($content));
593
  }
594
 
@@ -662,7 +714,12 @@ class Replacer
662
  */
663
  private function findNearestSize($sizeName)
664
  {
 
665
 
 
 
 
 
666
  $old_width = $this->source_metadata['sizes'][$sizeName]['width']; // the width from size not in new image
667
  $new_width = $this->target_metadata['width']; // default check - the width of the main image
668
 
49
  else
50
  $source_file = trim(get_attached_file($post_id, apply_filters( 'emr_unfiltered_get_attached_file', true )));
51
 
52
+ /* It happens that the SourceFile returns relative / incomplete when something messes up get_upload_dir with an error something.
53
+ This case shoudl be detected here and create a non-relative path anyhow..
54
+ */
55
+ if (! file_exists($source_file) && $source_file && 0 !== strpos( $source_file, '/' ) && ! preg_match( '|^.:\\\|', $source_file ) )
56
+ {
57
+ $file = get_post_meta( $post_id, '_wp_attached_file', true );
58
+ $uploads = wp_get_upload_dir();
59
+ $source_file = $uploads['basedir'] . "/$source_file";
60
+ }
61
+
62
  Log::addDebug('SourceFile ' . $source_file);
63
  $this->sourceFile = new File($source_file);
64
 
102
  public function replaceWith($file, $fileName)
103
  {
104
  global $wpdb;
 
105
  $this->targetName = $fileName;
 
106
 
107
  $targetFile = $this->getTargetFile();
108
 
113
  // throw new \RuntimeException($ex);
114
  }
115
 
 
 
116
  $targetFileObj = new File($targetFile);
117
  $result = $targetFileObj->checkAndCreateFolder();
118
  if ($result === false)
140
  }
141
 
142
  // update the file attached. This is required for wp_get_attachment_url to work.
143
+ $updated = update_attached_file($this->post_id, $this->targetFile->getFullFilePath() );
144
+ if (! $updated)
145
+ Log::addError('Update Attached File reports as not updated');
146
+
147
+ $this->target_url = $this->getTargetURL(); //wp_get_attachment_url($this->post_id);
148
 
149
  // Run the filter, so other plugins can hook if needed.
150
  $filtered = apply_filters( 'wp_handle_upload', array(
165
  wp_update_attachment_metadata( $this->post_id, $metadata );
166
  $this->target_metadata = $metadata;
167
 
168
+
169
  /** If author is different from replacer, note this */
170
  $author_id = get_post_meta($this->post_id, '_emr_replace_author', true);
171
 
187
  $update_ar = array('ID' => $this->post_id);
188
  $update_ar['post_title'] = $title;
189
  $update_ar['post_name'] = sanitize_title($title);
190
+ $update_ar['guid'] = $this->target_url; //wp_get_attachment_url($this->post_id);
191
  $update_ar['post_mime_type'] = $this->targetFile->getFileMime();
192
  $post_id = \wp_update_post($update_ar, true);
193
 
194
+
195
  // update post doesn't update GUID on updates.
196
  $wpdb->update( $wpdb->posts, array( 'guid' => $this->target_url), array('ID' => $this->post_id) );
197
  //enable-media-replace-upload-done
304
  }
305
  $path = $this->target_location; // if all went well.
306
  }
307
+ //if ($this->sourceFile->getFileName() == $this->targetName)
308
+ $targetpath = $path . $this->targetName;
309
 
310
+ // If the source and target path AND filename are identical, user has wrong mode, just overwrite the sourceFile.
311
+ if ($targetpath == $this->sourceFile->getFullFilePath())
312
+ {
313
+ $unique = $this->sourceFile->getFileName();
314
+ $this->replaceMode == self::MODE_REPLACE;
315
+ }
316
+ else
317
+ {
318
+ $unique = wp_unique_filename($path, $this->targetName);
319
+ }
320
  $new_filename = apply_filters( 'emr_unique_filename', $unique, $path, $this->post_id );
321
  $targetFile = trailingslashit($path) . $new_filename;
322
  }
337
  return null;
338
  }
339
  }
 
340
  return $targetFile;
341
  }
342
 
343
+ /** Since WP functions also can't be trusted here in certain cases, create the URL by ourselves */
344
+ protected function getTargetURL()
345
+ {
346
+ //$uploads['baseurl']
347
+ $url = wp_get_attachment_url($this->post_id);
348
+ $url_basename = basename($url);
349
+
350
+ // Seems all worked as normal.
351
+ if (strpos($url, '://') >= 0 && $this->targetFile->getFileName() == $url_basename)
352
+ return $url;
353
+
354
+ // Relative path for some reason
355
+ if (strpos($url, '://') === false)
356
+ {
357
+ $uploads = wp_get_upload_dir();
358
+ $url = str_replace($uploads['basedir'], $uploads['baseurl'], $this->targetFile->getFullFilePath());
359
+ }
360
+ // This can happen when WordPress is not taking from attached file, but wrong /old GUID. Try to replace it to the new one.
361
+ elseif ($this->targetFile->getFileName() != $url_basename)
362
+ {
363
+ $url = str_replace($url_basename, $this->targetFile->getFileName(), $url);
364
+ }
365
+
366
+ return $url;
367
+ //$this->targetFile
368
+
369
+ }
370
+
371
  /** Tries to remove all of the old image, without touching the metadata in database
372
  * This might fail on certain files, but this is not an indication of success ( remove might fail, but overwrite can still work)
373
  */
377
  $backup_sizes = get_post_meta( $this->post_id, '_wp_attachment_backup_sizes', true );
378
 
379
  // this must be -scaled if that exists, since wp_delete_attachment_files checks for original_files but doesn't recheck if scaled is included since that the one 'that exists' in WP . $this->source_file replaces original image, not the -scaled one.
380
+ $file = $this->sourceFile->getFullFilePath();
381
  $result = \wp_delete_attachment_files($this->post_id, $meta, $backup_sizes, $file );
382
 
383
  }
482
  }
483
  }
484
 
485
+ Log::addDebug('Source', $this->source_metadata);
486
+ Log::addDebug('Target', $this->target_metadata);
487
  /* If on the other hand, some sizes are available in source, but not in target, try to replace them with something closeby. */
488
  foreach($search_urls as $size => $url)
489
  {
510
  */
511
  foreach($search_urls as $size => $url)
512
  {
513
+ $replace_url = isset($replace_urls[$size]) ? $replace_urls[$size] : false;
514
  if ($url == $replace_url) // if source and target as the same, no need for replacing.
515
  {
516
  unset($search_urls[$size]);
528
  }
529
 
530
  Log::addDebug('Doing meta search and replace -', array($search_urls, $replace_urls) );
531
+ Log::addDebug('Searching with BaseuRL ' . $current_base_url);
532
 
533
  /* Search and replace in WP_POSTS */
534
  // Removed $wpdb->remove_placeholder_escape from here, not compatible with WP 4.8
608
  {
609
  Log::addDebug('Found JSON Content');
610
  $content = json_decode($content);
611
+
612
  }
613
 
614
  if (is_string($content)) // let's check the normal one first.
640
  {
641
  Log::addDebug('Value was found to be JSON, encoding');
642
  // wp-slash -> WP does stripslashes_deep which destroys JSON
643
+ $content = (json_encode($content, JSON_UNESCAPED_SLASHES));
644
  Log::addDebug('Content returning', array($content));
645
  }
646
 
714
  */
715
  private function findNearestSize($sizeName)
716
  {
717
+ Log::addDebug('Find Nearest: '. $sizeName);
718
 
719
+ if (! isset($this->source_metadata['sizes'][$sizeName]) || ! isset($this->target_metadata['width'])) // This can happen with non-image files like PDF.
720
+ {
721
+ return false;
722
+ }
723
  $old_width = $this->source_metadata['sizes'][$sizeName]['width']; // the width from size not in new image
724
  $new_width = $this->target_metadata['width']; // default check - the width of the main image
725
 
classes/uihelper.php CHANGED
@@ -98,7 +98,7 @@ class UIHelper
98
  public function setSourceSizes($attach_id)
99
  {
100
  $data = $this->getImageSizes($attach_id, 'full'); // wp_get_attachment_image_src($attach_id, 'full');
101
- $file = get_attached_file($attach_id);
102
 
103
  if (is_array($data))
104
  {
@@ -110,22 +110,29 @@ class UIHelper
110
  }
111
 
112
  // Returns Preview Image HTML Output.
113
- public function getPreviewImage($attach_id)
114
  {
115
  $data = false;
116
 
117
  if ($attach_id > 0)
118
  {
119
  $data = $this->getImageSizes($attach_id, $this->preview_size); //wp_get_attachment_image_src($attach_id, $this->preview_size);
120
- $file = get_attached_file($attach_id);
121
- Log::addDebug('Attached File ' . $file, $data);
122
-
123
 
 
 
 
 
 
 
 
 
 
124
  }
125
 
126
  $mime_type = get_post_mime_type($attach_id);
127
 
128
- if (! is_array($data) || ! file_exists($file) )
129
  {
130
  // if attachid higher than zero ( exists ) but not the image, fail, that's an error state.
131
  $icon = ($attach_id < 0) ? '' : 'dashicons-no';
@@ -137,17 +144,21 @@ class UIHelper
137
  'is_image' => false,
138
  'is_document' => $is_document,
139
  'icon' => $icon,
 
140
  );
141
 
 
 
142
  // failed, it might be this server doens't support PDF thumbnails. Fallback to File preview.
143
  if ($mime_type == 'application/pdf')
144
  {
145
- return $this->getPreviewFile($attach_id);
146
  }
147
 
148
  return $this->getPlaceHolder($args);
149
  }
150
 
 
151
  $url = $data[0];
152
  $width = $data[1];
153
  $height = $data[2];
@@ -171,6 +182,7 @@ class UIHelper
171
  'height' => $height,
172
  'image' => $image,
173
  'mime_type' => $mime_type,
 
174
  );
175
 
176
  $output = $this->getPlaceHolder($args);
@@ -221,18 +233,18 @@ class UIHelper
221
  return $output;
222
  }
223
 
224
- public function getPreviewFile($attach_id)
225
  {
226
  if ($attach_id > 0)
227
  {
228
- $filepath = get_attached_file($attach_id);
229
- $filename = basename($filepath);
230
  }
231
  else {
232
  $filename = false;
233
  }
234
 
235
- $mime_type = get_post_mime_type($attach_id);
236
 
237
  $args = array(
238
  'width' => 300,
@@ -241,6 +253,7 @@ class UIHelper
241
  'is_document' => true,
242
  'layer' => $filename,
243
  'mime_type' => $mime_type,
 
244
  );
245
  $output = $this->getPlaceHolder($args);
246
  return $output;
@@ -279,9 +292,11 @@ class UIHelper
279
  'is_image' => true,
280
  'is_document' => false,
281
  'mime_type' => false,
 
282
  );
283
 
284
  $args = wp_parse_args($args, $defaults);
 
285
  $w = $args['width'];
286
  $h = $args['height'];
287
 
@@ -311,16 +326,26 @@ class UIHelper
311
  $filetype = 'data-filetype="' . $args['mime_type'] . '"';
312
  }
313
 
 
 
314
 
315
  $output = "<div class='image_placeholder $placeholder_class' $filetype style='width:" . $w . "px; height:". $h ."px'> ";
316
  $output .= $args['image'];
317
  $output .= "<div class='dashicons $icon'>&nbsp;</div>";
318
  $output .= "<span class='textlayer'>" . $args['layer'] . "</span>";
 
319
  $output .= "</div>";
320
 
 
 
321
  return $output;
322
  }
323
 
 
 
 
 
 
324
  /**
325
  * Get size information for all currently-registered image sizes.
326
  * Directly stolen from - https://codex.wordpress.org/Function_Reference/get_intermediate_image_sizes
@@ -353,7 +378,7 @@ class UIHelper
353
  {
354
  $uploadDir = wp_upload_dir();
355
  if (isset($uploadDir['subdir']))
356
- return ltrim($uploadDir['subdir'], '/');
357
  else
358
  return false;
359
  }
98
  public function setSourceSizes($attach_id)
99
  {
100
  $data = $this->getImageSizes($attach_id, 'full'); // wp_get_attachment_image_src($attach_id, 'full');
101
+ // $file = get_attached_file($attach_id);
102
 
103
  if (is_array($data))
104
  {
110
  }
111
 
112
  // Returns Preview Image HTML Output.
113
+ public function getPreviewImage($attach_id,$file)
114
  {
115
  $data = false;
116
 
117
  if ($attach_id > 0)
118
  {
119
  $data = $this->getImageSizes($attach_id, $this->preview_size); //wp_get_attachment_image_src($attach_id, $this->preview_size);
120
+ /*$file = get_attached_file($attach_id);
 
 
121
 
122
+ // If the file is relative, prepend upload dir.
123
+ if (! file_exists($file) && $file && 0 !== strpos( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) )
124
+ {
125
+ $file = get_post_meta( $attach_id, '_wp_attached_file', true );
126
+ $uploads = wp_get_upload_dir();
127
+ $file = $uploads['basedir'] . "/$file";
128
+ }
129
+ */
130
+ Log::addDebug('Attached File ' . $file->getFullFilePath(), $data);
131
  }
132
 
133
  $mime_type = get_post_mime_type($attach_id);
134
 
135
+ if (! is_array($data) || ! $file->exists() )
136
  {
137
  // if attachid higher than zero ( exists ) but not the image, fail, that's an error state.
138
  $icon = ($attach_id < 0) ? '' : 'dashicons-no';
144
  'is_image' => false,
145
  'is_document' => $is_document,
146
  'icon' => $icon,
147
+ 'mime_type' => null,
148
  );
149
 
150
+
151
+
152
  // failed, it might be this server doens't support PDF thumbnails. Fallback to File preview.
153
  if ($mime_type == 'application/pdf')
154
  {
155
+ return $this->getPreviewFile($attach_id, $file);
156
  }
157
 
158
  return $this->getPlaceHolder($args);
159
  }
160
 
161
+
162
  $url = $data[0];
163
  $width = $data[1];
164
  $height = $data[2];
182
  'height' => $height,
183
  'image' => $image,
184
  'mime_type' => $mime_type,
185
+ 'file_size' => $file->getFileSize(),
186
  );
187
 
188
  $output = $this->getPlaceHolder($args);
233
  return $output;
234
  }
235
 
236
+ public function getPreviewFile($attach_id, $file)
237
  {
238
  if ($attach_id > 0)
239
  {
240
+ //$filepath = get_attached_file($attach_id);
241
+ $filename = $file->getFileName();
242
  }
243
  else {
244
  $filename = false;
245
  }
246
 
247
+ $mime_type = $file->getFileMime();
248
 
249
  $args = array(
250
  'width' => 300,
253
  'is_document' => true,
254
  'layer' => $filename,
255
  'mime_type' => $mime_type,
256
+ 'file_size' => $file->getFileSize(),
257
  );
258
  $output = $this->getPlaceHolder($args);
259
  return $output;
292
  'is_image' => true,
293
  'is_document' => false,
294
  'mime_type' => false,
295
+ 'file_size' => false,
296
  );
297
 
298
  $args = wp_parse_args($args, $defaults);
299
+
300
  $w = $args['width'];
301
  $h = $args['height'];
302
 
326
  $filetype = 'data-filetype="' . $args['mime_type'] . '"';
327
  }
328
 
329
+ $filesize = ($args['file_size']) ? $args['file_size'] : '';
330
+
331
 
332
  $output = "<div class='image_placeholder $placeholder_class' $filetype style='width:" . $w . "px; height:". $h ."px'> ";
333
  $output .= $args['image'];
334
  $output .= "<div class='dashicons $icon'>&nbsp;</div>";
335
  $output .= "<span class='textlayer'>" . $args['layer'] . "</span>";
336
+ $output .= "<div class='image_size'>" . $this->convertFileSize($filesize). "</div>";
337
  $output .= "</div>";
338
 
339
+
340
+
341
  return $output;
342
  }
343
 
344
+ private function convertFileSize($filesize)
345
+ {
346
+ return size_format($filesize);
347
+ }
348
+
349
  /**
350
  * Get size information for all currently-registered image sizes.
351
  * Directly stolen from - https://codex.wordpress.org/Function_Reference/get_intermediate_image_sizes
378
  {
379
  $uploadDir = wp_upload_dir();
380
  if (isset($uploadDir['subdir']))
381
+ return ltrim($uploadDir['subdir'], '/');
382
  else
383
  return false;
384
  }
css/admin.css CHANGED
@@ -836,6 +836,11 @@
836
  left: 50%;
837
  margin-left: -30px;
838
  opacity: 0.5; }
 
 
 
 
 
839
  .emr_upload_form .image_chooser.wrapper .image_previews .image_placeholder.is_image .dashicons::before, .emr_upload_form .image_chooser.wrapper .image_previews .image_placeholder.is_image .dashicons {
840
  display: none; }
841
  .emr_upload_form .image_chooser.wrapper .image_previews .image_placeholder.not_image img {
836
  left: 50%;
837
  margin-left: -30px;
838
  opacity: 0.5; }
839
+ .emr_upload_form .image_chooser.wrapper .image_previews .image_placeholder .image_size {
840
+ text-align: center;
841
+ position: absolute;
842
+ bottom: -25px;
843
+ width: 100%; }
844
  .emr_upload_form .image_chooser.wrapper .image_previews .image_placeholder.is_image .dashicons::before, .emr_upload_form .image_chooser.wrapper .image_previews .image_placeholder.is_image .dashicons {
845
  display: none; }
846
  .emr_upload_form .image_chooser.wrapper .image_previews .image_placeholder.not_image img {
enable-media-replace.php CHANGED
@@ -2,8 +2,8 @@
2
  /*
3
  Plugin Name: Enable Media Replace
4
  Plugin URI: https://wordpress.org/plugins/enable-media-replace/
5
- Description: Enable replacing media files by uploading a new file in the "Edit Media" section of the WordPress Media Library.
6
- Version: 3.4.1
7
  Author: ShortPixel
8
  Author URI: https://shortpixel.com
9
  Text Domain: enable-media-replace
@@ -26,7 +26,7 @@ http://www.gnu.org/licenses/gpl.html
26
 
27
  namespace EnableMediaReplace;
28
 
29
- define('EMR_VERSION', '3.4.1');
30
 
31
  if ( ! defined( 'ABSPATH' ) ) {
32
  exit; // Exit if accessed directly.
@@ -45,15 +45,17 @@ if(!defined("SHORTPIXEL_AFFILIATE_CODE")) {
45
  define("SHORTPIXEL_AFFILIATE_CODE", 'VKG6LYN28044');
46
  }
47
 
48
- require_once('build/shortpixel/autoload.php');
49
- require_once('classes/compat.php');
50
- require_once('classes/functions.php');
51
- require_once('classes/replacer.php');
52
- require_once('classes/uihelper.php');
53
- require_once('classes/file.php');
54
- require_once('classes/cache.php');
55
- require_once('classes/emr-plugin.php');
56
- require_once('classes/externals.php');
57
- require_once('thumbnail_updater.php');
 
 
58
 
59
  $emr_plugin = EnableMediaReplacePlugin::get();
2
  /*
3
  Plugin Name: Enable Media Replace
4
  Plugin URI: https://wordpress.org/plugins/enable-media-replace/
5
+ Description: Enable replacing media files by uploading a new file in the "Edit Media" section of the WordPress Media Library
6
+ Version: 3.4.2
7
  Author: ShortPixel
8
  Author URI: https://shortpixel.com
9
  Text Domain: enable-media-replace
26
 
27
  namespace EnableMediaReplace;
28
 
29
+ define('EMR_VERSION', '3.4.2');
30
 
31
  if ( ! defined( 'ABSPATH' ) ) {
32
  exit; // Exit if accessed directly.
45
  define("SHORTPIXEL_AFFILIATE_CODE", 'VKG6LYN28044');
46
  }
47
 
48
+ $plugin_path = plugin_dir_path(EMR_ROOT_FILE);
49
+
50
+ require_once($plugin_path . 'build/shortpixel/autoload.php');
51
+ require_once($plugin_path . 'classes/compat.php');
52
+ require_once($plugin_path . 'classes/functions.php');
53
+ require_once($plugin_path . 'classes/replacer.php');
54
+ require_once($plugin_path . 'classes/uihelper.php');
55
+ require_once($plugin_path . 'classes/file.php');
56
+ require_once($plugin_path . 'classes/cache.php');
57
+ require_once($plugin_path . 'classes/emr-plugin.php');
58
+ require_once($plugin_path . 'classes/externals.php');
59
+ require_once($plugin_path . 'thumbnail_updater.php');
60
 
61
  $emr_plugin = EnableMediaReplacePlugin::get();
js/emr_admin.js CHANGED
@@ -38,6 +38,9 @@
38
  source_type = $(source).data('filetype').trim();
39
  this.debug('detected type - ' + source_type);
40
  }
 
 
 
41
  if (source.hasClass('is_image'))
42
  {
43
  source_is_image = true;
@@ -98,6 +101,7 @@
98
 
99
  var status = this.checkUpload(file);
100
  this.debug('check upload status ' + status);
 
101
 
102
  if (status)
103
  {
@@ -143,6 +147,7 @@
143
  height = img.height;
144
  // $(preview).find('.textlayer').text(img.naturalWidth + ' x ' + img.naturalHeight );
145
  self.updateTextLayer(preview, width + ' x ' + height);
 
146
  });
147
 
148
  $(preview).prepend(img);
@@ -154,6 +159,7 @@
154
  $(preview).find('.dashicons').removeClass().addClass('dashicons dashicons-no');
155
  //$(preview).find('.textlayer').text('');
156
  this.updateTextLayer(preview, '');
 
157
  this.debug('File is null');
158
  }
159
  else { // not an image
@@ -161,6 +167,7 @@
161
  $(preview).find('.dashicons').removeClass().addClass('dashicons dashicons-media-document');
162
  //$(preview).find('.textlayer').text(file.name);
163
  this.updateTextLayer(preview, file.name);
 
164
  this.debug('Not image, media document');
165
  }
166
 
@@ -208,6 +215,20 @@
208
  }
209
 
210
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
  this.checkSubmit = function()
212
  {
213
  var check = ($('input[name="userfile"]').val().length > 0) ? true : false;
38
  source_type = $(source).data('filetype').trim();
39
  this.debug('detected type - ' + source_type);
40
  }
41
+ else
42
+ source_type = ''; // error state
43
+
44
  if (source.hasClass('is_image'))
45
  {
46
  source_is_image = true;
101
 
102
  var status = this.checkUpload(file);
103
  this.debug('check upload status ' + status);
104
+ this.debug(file.size);
105
 
106
  if (status)
107
  {
147
  height = img.height;
148
  // $(preview).find('.textlayer').text(img.naturalWidth + ' x ' + img.naturalHeight );
149
  self.updateTextLayer(preview, width + ' x ' + height);
150
+ self.updateFileSize(preview, file);
151
  });
152
 
153
  $(preview).prepend(img);
159
  $(preview).find('.dashicons').removeClass().addClass('dashicons dashicons-no');
160
  //$(preview).find('.textlayer').text('');
161
  this.updateTextLayer(preview, '');
162
+ this.updateFileSize(preview, null);
163
  this.debug('File is null');
164
  }
165
  else { // not an image
167
  $(preview).find('.dashicons').removeClass().addClass('dashicons dashicons-media-document');
168
  //$(preview).find('.textlayer').text(file.name);
169
  this.updateTextLayer(preview, file.name);
170
+ this.updateFileSize(preview, file);
171
  this.debug('Not image, media document');
172
  }
173
 
215
  }
216
 
217
  },
218
+ this.updateFileSize = function(preview, file)
219
+ {
220
+ if (file === null)
221
+ {
222
+ $(preview).find('.image_size').text('');
223
+ return;
224
+ }
225
+ var bytes = file.size;
226
+ if (bytes == 0) { return "0.00 B"; }
227
+ var e = Math.floor(Math.log(bytes) / Math.log(1024));
228
+ var size = (bytes/Math.pow(1024, e)).toFixed(2)+' '+' KMGTP'.charAt(e)+'B';
229
+
230
+ $(preview).find('.image_size').text(size);
231
+ }
232
  this.checkSubmit = function()
233
  {
234
  var check = ($('input[name="userfile"]').val().length > 0) ? true : false;
readme.txt CHANGED
@@ -5,7 +5,7 @@ Tags: replace, attachment, media, files, replace image, replace jpg, change medi
5
  Requires at least: 4.9.7
6
  Tested up to: 5.5
7
  Requires PHP: 5.6
8
- Stable tag: 3.4.1
9
 
10
  Easily replace any attached image/file by simply uploading a new file in the Media Library edit view - a real time saver!
11
 
@@ -47,6 +47,18 @@ If you want more control over the format used to display the time, you can use t
47
 
48
  == Changelog ==
49
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  = 3.4.1 =
51
 
52
  Release date: June 18th 2020
@@ -55,7 +67,7 @@ Release date: June 18th 2020
55
  = 3.4.0 =
56
 
57
  Release date: June 9th 2020
58
- * New - In edit media screen EMR notes user who replaced file if this was not uploader;
59
  * New - Added Drag and Drop area for files;
60
  * New - When replacing with new file name, now also possible to update upload path;
61
  * Fix - Various minor CSS tweaks and fixes;
5
  Requires at least: 4.9.7
6
  Tested up to: 5.5
7
  Requires PHP: 5.6
8
+ Stable tag: 3.4.2
9
 
10
  Easily replace any attached image/file by simply uploading a new file in the Media Library edit view - a real time saver!
11
 
47
 
48
  == Changelog ==
49
 
50
+
51
+ = 3.4.2 =
52
+
53
+ Release date: August 17th 2020
54
+ * New - Also display files size in the replacement windows;
55
+ * Fix - Replacing image with pdf file would cause noticesi;
56
+ * Fix - Compensation for faulty WordPress installation that return empty error string on wp_upload_dir and falsely return relative paths and urls on WP core functions;
57
+ * Fix - When choosing 'replace file and update names' and then uploading a file with identical name as source, it would postfix the duplicate name. This is no longer the case;
58
+ * Fix - Wording clarification for the situation when the filename is changed, which will lead to broken links from external sites;
59
+ * Fix - Added hard paths to require_once satements in order to prevent WP-CLI errrors;
60
+ * Fix - If source image doesn't exist or in error state, replacement would be prevented by Javascript issue.
61
+
62
  = 3.4.1 =
63
 
64
  Release date: June 18th 2020
67
  = 3.4.0 =
68
 
69
  Release date: June 9th 2020
70
+ * New - In edit media screen EMR notes user who replaced file if this was not uploader;
71
  * New - Added Drag and Drop area for files;
72
  * New - When replacing with new file name, now also possible to update upload path;
73
  * Fix - Various minor CSS tweaks and fixes;
scss/admin.scss CHANGED
@@ -111,6 +111,13 @@
111
  opacity: 0.5;
112
 
113
  }
 
 
 
 
 
 
 
114
  &.is_image
115
  {
116
  .dashicons::before, .dashicons { display: none }
111
  opacity: 0.5;
112
 
113
  }
114
+ .image_size
115
+ {
116
+ text-align: center;
117
+ position: absolute;
118
+ bottom: -25px;
119
+ width: 100%;
120
+ }
121
  &.is_image
122
  {
123
  .dashicons::before, .dashicons { display: none }
views/popup.php CHANGED
@@ -88,18 +88,19 @@ $url = $uiHelper->getFormUrl($attachment_id);
88
  <div class='image_previews'>
89
  <?php if (wp_attachment_is('image', $attachment_id) || $source_mime == 'application/pdf')
90
  {
91
- echo $uiHelper->getPreviewImage($attachment_id);
92
- echo $uiHelper->getPreviewImage(-1);
93
  }
94
  else {
 
95
  if (strlen($filepath) == 0) // check if image in error state.
96
  {
97
  echo $uiHelper->getPreviewError(-1);
98
- echo $uiHelper->getPreviewImage(-1);
99
  }
100
  else {
101
- echo $uiHelper->getPreviewFile($attachment_id);
102
- echo $uiHelper->getPreviewFile(-1);
103
  }
104
 
105
  }
@@ -139,7 +140,7 @@ $url = $uiHelper->getFormUrl($attachment_id);
139
  <label for="replace_type_2"><input id="replace_type_2" type="radio" name="replace_type" value="replace_and_search" <?php echo $searchreplace_disabled ?> > <?php echo __("Replace the file, use new file name and update all links", "enable-media-replace"); ?>
140
  </label>
141
 
142
- <p class="howto"><?php printf( esc_html__("Note: If you check this option, the name and type of the file you are about to upload will replace the old file. All links pointing to the current file (%s) will be updated to point to the new file name.", "enable-media-replace"), $filename ); ?></p>
143
 
144
  <!-- <p class="howto"><?php echo esc_html__("Please note that if you upload a new image, only embeds/links of the original size image will be replaced in your posts.", "enable-media-replace"); ?></p> -->
145
 
88
  <div class='image_previews'>
89
  <?php if (wp_attachment_is('image', $attachment_id) || $source_mime == 'application/pdf')
90
  {
91
+ echo $uiHelper->getPreviewImage($attachment_id, $file);
92
+ echo $uiHelper->getPreviewImage(-1, $file);
93
  }
94
  else {
95
+
96
  if (strlen($filepath) == 0) // check if image in error state.
97
  {
98
  echo $uiHelper->getPreviewError(-1);
99
+ echo $uiHelper->getPreviewImage(-1, $file);
100
  }
101
  else {
102
+ echo $uiHelper->getPreviewFile($attachment_id, $file);
103
+ echo $uiHelper->getPreviewFile(-1, $file);
104
  }
105
 
106
  }
140
  <label for="replace_type_2"><input id="replace_type_2" type="radio" name="replace_type" value="replace_and_search" <?php echo $searchreplace_disabled ?> > <?php echo __("Replace the file, use new file name and update all links", "enable-media-replace"); ?>
141
  </label>
142
 
143
+ <p class="howto"><?php printf( esc_html__("Note: If you check this option, the name and type of the file you are about to upload will replace the old file. All links pointing to the current file (%s) will be updated to point to the new file name. (If any other websites link to the file directly, those links will no longer work. Be careful.)", "enable-media-replace"), $filename ); ?></p>
144
 
145
  <!-- <p class="howto"><?php echo esc_html__("Please note that if you upload a new image, only embeds/links of the original size image will be replaced in your posts.", "enable-media-replace"); ?></p> -->
146
 
views/upload.php CHANGED
@@ -10,8 +10,6 @@ use EnableMediaReplace\Notices\NoticeController as Notices;
10
  if (!current_user_can('upload_files'))
11
  wp_die( esc_html__('You do not have permission to upload files.', 'enable-media-replace') );
12
 
13
-
14
-
15
  /*require_once('classes/replacer.php');
16
  require_once('classes/file.php'); */
17
 
10
  if (!current_user_can('upload_files'))
11
  wp_die( esc_html__('You do not have permission to upload files.', 'enable-media-replace') );
12
 
 
 
13
  /*require_once('classes/replacer.php');
14
  require_once('classes/file.php'); */
15