Enable Media Replace - Version 4.0.1

Version Description

Release date: November 23, 2022 * Fix: files are now replaced correctly in environments running on Windows/IIC servers; * Fix: the mime type is now correctly detected, so that a warning is no longer displayed when replacing PDFs; * Fix: the plugin no longer tries to load its code for post IDs that are not attachments, like comments; * Fix: some warnings were displayed when using the Members plugin; * Compat: added integration for SiteOrigin Page Builder to correctly replace files in its widgets; * Compat: added emr_after_remove_current action hook (thanks @luistar15); * Tweak: added filter to disable the background replace functionality.

Download this release

Release Info

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

Code changes from version 4.0.0 to 4.0.1

build/shortpixel/filesystem/src/Controller/FileSystemController.php CHANGED
@@ -75,6 +75,18 @@ Class FileSystemController
75
  return $this->getDirectory($abspath);
76
  }
77
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  /** Utility function that tries to convert a file-path to a webURL.
80
  *
@@ -85,8 +97,11 @@ Class FileSystemController
85
  $filepath = $file->getFullPath();
86
  $directory = $file->getFileDir();
87
 
88
- $is_multi_site = $this->env->is_multisite;
89
- $is_main_site = $this->env->is_mainsite;
 
 
 
90
 
91
  // stolen from wp_get_attachment_url
92
  if ( ( $uploads = wp_get_upload_dir() ) && (false === $uploads['error'] || strlen(trim($uploads['error'])) == 0 ) ) {
@@ -113,6 +128,9 @@ Class FileSystemController
113
 
114
  } elseif ( false !== strpos( $filepath, 'wp-content/uploads' ) ) {
115
  // Get the directory name relative to the basedir (back compat for pre-2.7 uploads)
 
 
 
116
  $url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $filepath ) ) . wp_basename( $filepath );
117
  } else {
118
  // It's a newly-uploaded file, therefore $file is relative to the basedir.
@@ -130,7 +148,7 @@ Class FileSystemController
130
  // (2) ** Also a real life fix when a path is /wwwroot/assets/sites/2/ etc, in get site url, the home URL is the site URL, without appending the sites stuff. Fails on original image.
131
  if ($is_multi_site && ! $is_main_site)
132
  {
133
- $wp_home_path = wp_normalize_path(trailingslashit($uploads['basedir']));
134
  $home_url = trailingslashit($uploads['baseurl']);
135
  }
136
  else
75
  return $this->getDirectory($abspath);
76
  }
77
 
78
+ public function getFullPathForWP(FileModel $file)
79
+ {
80
+ $fullpath = $file->getFullPath();
81
+ $abspath = $this->getWPAbsPath();
82
+
83
+ if (! strpos($abspath, $fullpath))
84
+ {
85
+
86
+ }
87
+
88
+ }
89
+
90
 
91
  /** Utility function that tries to convert a file-path to a webURL.
92
  *
97
  $filepath = $file->getFullPath();
98
  $directory = $file->getFileDir();
99
 
100
+ $is_multi_site = (function_exists("is_multisite") && is_multisite()) ? true : false;
101
+ $is_main_site = is_main_site();
102
+
103
+ //$is_multi_site = $this->env->is_multisite;
104
+ //$is_main_site = $this->env->is_mainsite;
105
 
106
  // stolen from wp_get_attachment_url
107
  if ( ( $uploads = wp_get_upload_dir() ) && (false === $uploads['error'] || strlen(trim($uploads['error'])) == 0 ) ) {
128
 
129
  } elseif ( false !== strpos( $filepath, 'wp-content/uploads' ) ) {
130
  // Get the directory name relative to the basedir (back compat for pre-2.7 uploads)
131
+ //$relativePath = $this->getFile(_wp_get_attachment_relative_path( $filepath ) );
132
+ //$basename = wp_basename($relativePath->getFullPath());
133
+
134
  $url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $filepath ) ) . wp_basename( $filepath );
135
  } else {
136
  // It's a newly-uploaded file, therefore $file is relative to the basedir.
148
  // (2) ** Also a real life fix when a path is /wwwroot/assets/sites/2/ etc, in get site url, the home URL is the site URL, without appending the sites stuff. Fails on original image.
149
  if ($is_multi_site && ! $is_main_site)
150
  {
151
+ $wp_home_path = trailingslashit($uploads['basedir']);
152
  $home_url = trailingslashit($uploads['baseurl']);
153
  }
154
  else
build/shortpixel/filesystem/src/Model/File/DirectoryModel.php CHANGED
@@ -32,7 +32,7 @@ class DirectoryModel
32
  */
33
  public function __construct($path)
34
  {
35
- $path = wp_normalize_path($path);
36
  $fs = $this->getFS();
37
 
38
  if ($fs->pathIsUrl($path))
32
  */
33
  public function __construct($path)
34
  {
35
+ // $path = wp_normalize_path($path);
36
  $fs = $this->getFS();
37
 
38
  if ($fs->pathIsUrl($path))
build/shortpixel/filesystem/src/Model/File/FileModel.php CHANGED
@@ -17,6 +17,7 @@ class FileModel
17
 
18
  // File info
19
  protected $fullpath = null;
 
20
  protected $filename = null; // filename + extension
21
  protected $filebase = null; // filename without extension
22
  protected $directory = null;
@@ -42,7 +43,9 @@ class FileModel
42
  /** Creates a file model object. FileModel files don't need to exist on FileSystem */
43
  public function __construct($path)
44
  {
 
45
  $this->fullpath = trim($path);
 
46
  $fs = $this->getFS();
47
  if ($fs->pathIsUrl($path)) // Asap check for URL's to prevent remote wrappers from running.
48
  {
@@ -377,7 +380,7 @@ class FileModel
377
  // So far, testing use for file Filter */
378
  public function getRawFullPath()
379
  {
380
- return $this->fullpath;
381
  }
382
 
383
  public function getFileName()
@@ -440,7 +443,7 @@ class FileModel
440
  if ($path === false) // don't process further
441
  return false;
442
 
443
- $path = wp_normalize_path($path);
444
  $abspath = $fs->getWPAbsPath();
445
 
446
  if ( is_file($path) && ! is_dir($path) ) // if path and file exist, all should be okish.
17
 
18
  // File info
19
  protected $fullpath = null;
20
+ protected $rawfullpath = null;
21
  protected $filename = null; // filename + extension
22
  protected $filebase = null; // filename without extension
23
  protected $directory = null;
43
  /** Creates a file model object. FileModel files don't need to exist on FileSystem */
44
  public function __construct($path)
45
  {
46
+
47
  $this->fullpath = trim($path);
48
+ $this->rawfullpath = $this->fullpath; // path without any doing.
49
  $fs = $this->getFS();
50
  if ($fs->pathIsUrl($path)) // Asap check for URL's to prevent remote wrappers from running.
51
  {
380
  // So far, testing use for file Filter */
381
  public function getRawFullPath()
382
  {
383
+ return $this->rawfullpath;
384
  }
385
 
386
  public function getFileName()
443
  if ($path === false) // don't process further
444
  return false;
445
 
446
+ //$path = wp_normalize_path($path);
447
  $abspath = $fs->getWPAbsPath();
448
 
449
  if ( is_file($path) && ! is_dir($path) ) // if path and file exist, all should be okish.
build/shortpixel/log/src/DebugItem.php CHANGED
@@ -41,13 +41,25 @@ class DebugItem
41
  {
42
  $this->data[] = print_r($data, true);
43
  }
44
- if ($dataType == 2) //array
45
  {
 
 
 
 
 
 
 
 
 
 
 
 
46
  foreach($data as $index => $item)
47
  {
48
  if (is_object($item) || is_array($item))
49
  {
50
- $this->data[] = print_r($item, true);
51
  }
52
  }
53
  }
41
  {
42
  $this->data[] = print_r($data, true);
43
  }
44
+ if ($dataType == 2) //array or object.
45
  {
46
+ $count = false;
47
+ if (gettype($data) == 'array')
48
+ $count = count($data);
49
+ elseif(gettype($data) == 'object')
50
+ $count = count(get_object_vars($data));
51
+
52
+ $firstLine = ucfirst(gettype($data)) . ':';
53
+ if ($count !== false)
54
+ $firstLine .= ' (' . $count . ')';
55
+
56
+ $this->data[] = $firstLine;
57
+
58
  foreach($data as $index => $item)
59
  {
60
  if (is_object($item) || is_array($item))
61
  {
62
+ $this->data[] = print_r($index, true) . ' ( ' . ucfirst(gettype($item)) . ') => ' . print_r($item, true);
63
  }
64
  }
65
  }
build/shortpixel/notices/src/NoticeModel.php CHANGED
@@ -234,9 +234,9 @@ class NoticeModel //extends ShortPixelModel
234
  document.getElementById('button-$id').onclick = function()
235
  {
236
  var el = document.getElementById('$id');
237
- $(el).fadeTo(100,0,function() {
238
- $(el).slideUp(100, 0, function () {
239
- $(el).remove();
240
  })
241
  });
242
  } </script>";
234
  document.getElementById('button-$id').onclick = function()
235
  {
236
  var el = document.getElementById('$id');
237
+ jQuery(el).fadeTo(100,0,function() {
238
+ jQuery(el).slideUp(100, 0, function () {
239
+ jQuery(el).remove();
240
  })
241
  });
242
  } </script>";
classes/emr-plugin.php CHANGED
@@ -16,9 +16,12 @@ class EnableMediaReplacePlugin
16
  private $user_cap = false;
17
  private $general_cap = false;
18
 
 
 
19
  public function __construct()
20
  {
21
- add_action('plugins_loaded', array($this, 'runtime'));
 
22
  }
23
 
24
  public function runtime()
@@ -43,9 +46,17 @@ class EnableMediaReplacePlugin
43
  return;
44
  }
45
 
 
 
46
  $this->plugin_actions(); // init
47
  }
48
 
 
 
 
 
 
 
49
  public function filesystem()
50
  {
51
  return new FileSystem();
@@ -56,6 +67,20 @@ class EnableMediaReplacePlugin
56
  return Uihelper::getInstance();
57
  }
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  public static function get()
60
  {
61
  if (is_null(self::$instance)) {
@@ -106,7 +131,7 @@ class EnableMediaReplacePlugin
106
  add_action('wp_ajax_emr_dismiss_notices', array($this,'dismiss_notices'));
107
 
108
  // editors
109
- add_action('add_meta_boxes', array($this, 'add_meta_boxes'), 10, 2);
110
  add_filter('attachment_fields_to_edit', array($this, 'attachment_editor'), 10, 2);
111
 
112
  /** Just after an image is replaced, try to browser decache the images */
@@ -131,7 +156,6 @@ class EnableMediaReplacePlugin
131
  $title = esc_html__("Replace media", "enable-media-replace");
132
  $title = (isset($_REQUEST['action']) && ($_REQUEST['action'] === 'emr_prepare_remove')) ? esc_html__("Remove background", "enable-media-replace") : $title;
133
  add_submenu_page('upload.php',$title, $title, 'upload_files', 'enable-media-replace/enable-media-replace.php', array($this, 'route'));
134
- /* add_submenu_page(null, esc_html__("Remove background", "enable-media-replace"), esc_html__("Remove the media Background", "enable-media-replace"), 'upload_files', 'emr-remove-background', array($this, 'route')); */
135
 
136
 
137
  }
@@ -195,9 +219,6 @@ class EnableMediaReplacePlugin
195
 
196
  $this->uiHelper()->featureNotice();
197
 
198
-
199
-
200
-
201
  if (! check_admin_referer($action, '_wpnonce')) {
202
  die('Invalid Nonce');
203
  }
@@ -212,7 +233,7 @@ class EnableMediaReplacePlugin
212
  elseif ($action == 'media_replace_upload') {
213
  require_once($this->plugin_path . 'views/upload.php');
214
  }
215
- elseif ('emr_prepare_remove' === $action) {
216
  $attachment_id = intval($_GET['attachment_id']);
217
  $attachment = get_post($attachment_id);
218
  //We're adding a timestamp to the image URL for cache busting
@@ -224,7 +245,10 @@ class EnableMediaReplacePlugin
224
  wp_enqueue_script('emr_upsell');
225
  require_once($this->plugin_path . "views/prepare-remove-background.php");
226
 
227
- } elseif ('do_background_replace' === $action && check_admin_referer($action, '_wpnonce')) {
 
 
 
228
  require_once($this->plugin_path . 'views/do-replace-background.php');
229
  }
230
  else {
@@ -311,8 +335,24 @@ class EnableMediaReplacePlugin
311
  }
312
  }
313
 
314
- public function checkImagePermission($author_id, $post_id)
315
  {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
316
  if ($this->general_cap === false && $this->user_cap === false) {
317
  if (current_user_can('edit_post', $post_id) === true) {
318
  return true;
@@ -354,14 +394,14 @@ class EnableMediaReplacePlugin
354
  return $url;
355
  }
356
 
357
- public function add_meta_boxes($post_type, $post)
358
  {
359
  // Because some plugins don't like to play by the rules.
360
- if (is_null($post_type) || is_null($post)) {
361
  return false;
362
  }
363
 
364
- if (! $this->checkImagePermission($post->post_author, $post->ID)) {
365
  return;
366
  }
367
 
@@ -400,7 +440,7 @@ class EnableMediaReplacePlugin
400
 
401
  public function show_thumbs_box($post)
402
  {
403
- if (! $this->checkImagePermission($post->post_author, $post->ID)) {
404
  return;
405
  }
406
 
@@ -435,7 +475,7 @@ class EnableMediaReplacePlugin
435
  {
436
  $screen = null;
437
 
438
- if (! $this->checkImagePermission($post->post_author, $post->ID)) {
439
  return $form_fields;
440
  }
441
 
@@ -489,7 +529,7 @@ class EnableMediaReplacePlugin
489
  public function add_media_action($actions, $post)
490
  {
491
 
492
- if (! $this->checkImagePermission($post->post_author, $post->ID)) {
493
  return $actions;
494
  }
495
 
16
  private $user_cap = false;
17
  private $general_cap = false;
18
 
19
+ private $features = array();
20
+
21
  public function __construct()
22
  {
23
+ add_action('plugins_loaded', array($this, 'runtime')); //lowInit, before theme setup!
24
+ add_action('admin_init', array($this, 'adminInit')); // adminInit, after functions.php
25
  }
26
 
27
  public function runtime()
46
  return;
47
  }
48
 
49
+
50
+
51
  $this->plugin_actions(); // init
52
  }
53
 
54
+ public function adminInit()
55
+ {
56
+ $this->features['replace'] = true; // does nothing just for completeness
57
+ $this->features['background'] = apply_filters('emr/feature/background', true);
58
+ }
59
+
60
  public function filesystem()
61
  {
62
  return new FileSystem();
67
  return Uihelper::getInstance();
68
  }
69
 
70
+ public function useFeature($name)
71
+ {
72
+ switch($name)
73
+ {
74
+ case 'background':
75
+ $bool = $this->features['background'];
76
+ break;
77
+ default:
78
+ $bool = false;
79
+ break;
80
+ }
81
+ return $bool;
82
+ }
83
+
84
  public static function get()
85
  {
86
  if (is_null(self::$instance)) {
131
  add_action('wp_ajax_emr_dismiss_notices', array($this,'dismiss_notices'));
132
 
133
  // editors
134
+ add_action('add_meta_boxes_attachment', array($this, 'add_meta_boxes'), 10, 2);
135
  add_filter('attachment_fields_to_edit', array($this, 'attachment_editor'), 10, 2);
136
 
137
  /** Just after an image is replaced, try to browser decache the images */
156
  $title = esc_html__("Replace media", "enable-media-replace");
157
  $title = (isset($_REQUEST['action']) && ($_REQUEST['action'] === 'emr_prepare_remove')) ? esc_html__("Remove background", "enable-media-replace") : $title;
158
  add_submenu_page('upload.php',$title, $title, 'upload_files', 'enable-media-replace/enable-media-replace.php', array($this, 'route'));
 
159
 
160
 
161
  }
219
 
220
  $this->uiHelper()->featureNotice();
221
 
 
 
 
222
  if (! check_admin_referer($action, '_wpnonce')) {
223
  die('Invalid Nonce');
224
  }
233
  elseif ($action == 'media_replace_upload') {
234
  require_once($this->plugin_path . 'views/upload.php');
235
  }
236
+ elseif ('emr_prepare_remove' === $action && $this->useFeature('background')) {
237
  $attachment_id = intval($_GET['attachment_id']);
238
  $attachment = get_post($attachment_id);
239
  //We're adding a timestamp to the image URL for cache busting
245
  wp_enqueue_script('emr_upsell');
246
  require_once($this->plugin_path . "views/prepare-remove-background.php");
247
 
248
+ } elseif ('do_background_replace' === $action &&
249
+ check_admin_referer($action, '_wpnonce') &&
250
+ $this->useFeature('background')
251
+ ) {
252
  require_once($this->plugin_path . 'views/do-replace-background.php');
253
  }
254
  else {
335
  }
336
  }
337
 
338
+ public function checkImagePermission($post)
339
  {
340
+ if (! is_object($post))
341
+ {
342
+ return false;
343
+ }
344
+ $post_id = $post->ID;
345
+ $post_type = $post->post_type;
346
+ $author_id = $post->post_author;
347
+
348
+ if ($post_type !== 'attachment')
349
+ return false;
350
+
351
+ if (is_null($post_id) || intval($post_id) >! 0)
352
+ {
353
+ return false;
354
+ }
355
+
356
  if ($this->general_cap === false && $this->user_cap === false) {
357
  if (current_user_can('edit_post', $post_id) === true) {
358
  return true;
394
  return $url;
395
  }
396
 
397
+ public function add_meta_boxes($post)
398
  {
399
  // Because some plugins don't like to play by the rules.
400
+ if (is_null($post) || ! is_object($post) ) {
401
  return false;
402
  }
403
 
404
+ if (! $this->checkImagePermission($post)) {
405
  return;
406
  }
407
 
440
 
441
  public function show_thumbs_box($post)
442
  {
443
+ if (! $this->checkImagePermission($post)) {
444
  return;
445
  }
446
 
475
  {
476
  $screen = null;
477
 
478
+ if (! $this->checkImagePermission($post)) {
479
  return $form_fields;
480
  }
481
 
529
  public function add_media_action($actions, $post)
530
  {
531
 
532
+ if (! $this->checkImagePermission($post)) {
533
  return $actions;
534
  }
535
 
classes/external/siteorigin.php ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ namespace EnableMediaReplace\Externals;
3
+
4
+
5
+ class SiteOrigin
6
+ {
7
+ protected static $instance;
8
+
9
+ public function __construct()
10
+ {
11
+ if (defined('SITEORIGIN_PANELS_VERSION'))
12
+ {
13
+ add_filter('emr/replacer/option_fields', array($this, 'addOption'));
14
+ }
15
+ }
16
+
17
+ public static function getInstance()
18
+ {
19
+ if (is_null(self::$instance))
20
+ {
21
+ self::$instance = new SiteOrigin();
22
+ }
23
+
24
+ return self::$instance;
25
+ }
26
+
27
+ public function addOption($options)
28
+ {
29
+ $options[] = 'widget_siteorigin-panels-builder';
30
+ return $options;
31
+ }
32
+ } // class
classes/externals.php CHANGED
@@ -5,6 +5,8 @@ use EnableMediaReplace\Notices\NoticeController as Notices;
5
 
6
  use EnableMediaReplace\Externals\Elementor as Elementor;
7
  use EnableMediaReplace\Externals\WpBakery as WpBakery;
 
 
8
 
9
  class Externals
10
  {
@@ -25,6 +27,7 @@ class Externals
25
  // integrations
26
  $this->loadElementor();
27
  $this->loadBakery(); // in case of urlencoded issues, this class should be used probably.
 
28
  }
29
 
30
  protected function check() // check if any of the options should be disabled due to conflicts
@@ -70,4 +73,9 @@ class Externals
70
  WpBakery::getInstance();
71
  }
72
 
 
 
 
 
 
73
  } // class
5
 
6
  use EnableMediaReplace\Externals\Elementor as Elementor;
7
  use EnableMediaReplace\Externals\WpBakery as WpBakery;
8
+ use EnableMediaReplace\Externals\SiteOrigin as SiteOrigin;
9
+
10
 
11
  class Externals
12
  {
27
  // integrations
28
  $this->loadElementor();
29
  $this->loadBakery(); // in case of urlencoded issues, this class should be used probably.
30
+ $this->loadSiteOrigins();
31
  }
32
 
33
  protected function check() // check if any of the options should be disabled due to conflicts
73
  WpBakery::getInstance();
74
  }
75
 
76
+ public function loadSiteOrigins()
77
+ {
78
+ SiteOrigin::getInstance();
79
+ }
80
+
81
  } // class
classes/replacer.php CHANGED
@@ -143,8 +143,6 @@ class Replacer
143
  throw new \RuntimeException($ex);
144
  }
145
 
146
-
147
-
148
  // init targetFile.
149
  $this->targetFile = $fs->getFile($targetFile);
150
 
@@ -154,8 +152,10 @@ class Replacer
154
  Log::addWarn('Setting permissions failed');
155
  }
156
 
 
157
  // update the file attached. This is required for wp_get_attachment_url to work.
158
- $updated = update_attached_file($this->post_id, $this->targetFile->getFullPath() );
 
159
  if (! $updated)
160
  Log::addError('Update Attached File reports as not updated or same value');
161
 
@@ -450,7 +450,7 @@ class Replacer
450
  @unlink($attached_file);
451
  }
452
 
453
-
454
  }
455
 
456
  /** Handle new dates for the replacement */
@@ -602,7 +602,7 @@ class Replacer
602
  Log::addDebug('Running additional replace for : '. $component, $run);
603
  $updated += $this->doReplaceQuery($run['base_url'], $run['search_urls'], $run['replace_urls']);
604
  }
605
- //do_action('')
606
 
607
  Log::addDebug("Updated Records : " . $updated);
608
  return $updated;
@@ -655,8 +655,18 @@ class Replacer
655
  {
656
  global $wpdb;
657
 
658
- $meta_options = apply_filters('emr/metadata_tables', array('post', 'comment', 'term', 'user', 'options'));
 
 
 
 
 
 
 
 
 
659
  $number_of_updates = 0;
 
660
 
661
  foreach($meta_options as $type)
662
  {
@@ -670,10 +680,15 @@ class Replacer
670
  $update_sql = ' UPDATE ' . $wpdb->postmeta . ' SET meta_value = %s WHERE meta_id = %d';
671
  break;
672
  case "options": // basked case (for guten widgets).
 
 
 
673
  $sql = 'SELECT option_id as id, option_name, option_value as meta_value FROM ' . $wpdb->options . '
674
- WHERE option_value like %s and option_name = "widget_block" ';
675
  $type = 'option';
676
 
 
 
677
  $update_sql = ' UPDATE ' . $wpdb->options . ' SET option_value = %s WHERE option_id = %d';
678
  break;
679
  default:
@@ -691,7 +706,7 @@ class Replacer
691
  break;
692
  }
693
 
694
- $sql = $wpdb->prepare($sql, '%' . $url . '%');
695
  Log::addTemp('Handle MEta SQL ' . $sql);
696
 
697
  if ($wpdb->last_error)
143
  throw new \RuntimeException($ex);
144
  }
145
 
 
 
146
  // init targetFile.
147
  $this->targetFile = $fs->getFile($targetFile);
148
 
152
  Log::addWarn('Setting permissions failed');
153
  }
154
 
155
+
156
  // update the file attached. This is required for wp_get_attachment_url to work.
157
+ // Using RawFullPath because FullPath does normalize path, which update_attached_file doesn't so in case of windows / strange Apspaths it fails.
158
+ $updated = update_attached_file($this->post_id, $this->targetFile->getRawFullPath() );
159
  if (! $updated)
160
  Log::addError('Update Attached File reports as not updated or same value');
161
 
450
  @unlink($attached_file);
451
  }
452
 
453
+ do_action( 'emr_after_remove_current', $this->post_id, $meta, $backup_sizes, $file );
454
  }
455
 
456
  /** Handle new dates for the replacement */
602
  Log::addDebug('Running additional replace for : '. $component, $run);
603
  $updated += $this->doReplaceQuery($run['base_url'], $run['search_urls'], $run['replace_urls']);
604
  }
605
+
606
 
607
  Log::addDebug("Updated Records : " . $updated);
608
  return $updated;
655
  {
656
  global $wpdb;
657
 
658
+ $options = array('post', 'comment', 'term', 'user', 'options');
659
+ $meta_options = apply_filters('emr/metadata_tables', $options);
660
+
661
+
662
+ // fields in options to look for.
663
+ $option_fields = array('widget_block');
664
+ $option_fields = apply_filters('emr/replacer/option_fields', $option_fields);
665
+
666
+
667
+
668
  $number_of_updates = 0;
669
+ $prepare = array('%' . $url . '%');
670
 
671
  foreach($meta_options as $type)
672
  {
680
  $update_sql = ' UPDATE ' . $wpdb->postmeta . ' SET meta_value = %s WHERE meta_id = %d';
681
  break;
682
  case "options": // basked case (for guten widgets).
683
+ $in_str_arr = array_fill( 0, count( $option_fields ), '%s' );
684
+ $in_str = join( ',', $in_str_arr );
685
+
686
  $sql = 'SELECT option_id as id, option_name, option_value as meta_value FROM ' . $wpdb->options . '
687
+ WHERE option_value like %s and option_name in (' . $in_str . ')';
688
  $type = 'option';
689
 
690
+ $prepare = array_merge($prepare, $option_fields);
691
+
692
  $update_sql = ' UPDATE ' . $wpdb->options . ' SET option_value = %s WHERE option_id = %d';
693
  break;
694
  default:
706
  break;
707
  }
708
 
709
+ $sql = $wpdb->prepare($sql, $prepare);
710
  Log::addTemp('Handle MEta SQL ' . $sql);
711
 
712
  if ($wpdb->last_error)
classes/uihelper.php CHANGED
@@ -308,6 +308,10 @@ class UIHelper
308
  }
309
 
310
  $mime_type = $file->getMime();
 
 
 
 
311
 
312
  $args = array(
313
  'width' => 300,
@@ -413,6 +417,9 @@ class UIHelper
413
  if (false === wp_attachment_is_image($post))
414
  return false;
415
 
 
 
 
416
  $extensions = array('jpg', 'png','jpeg');
417
 
418
  $mime = get_post_mime_type($post);
308
  }
309
 
310
  $mime_type = $file->getMime();
311
+ if (false === $mime_type) // If server is not supporting this, go w/ the post mime type one.
312
+ {
313
+ $mime_type = get_post_mime_type($attach_id);
314
+ }
315
 
316
  $args = array(
317
  'width' => 300,
417
  if (false === wp_attachment_is_image($post))
418
  return false;
419
 
420
+ if (false === emr()->useFeature('background'))
421
+ return false;
422
+
423
  $extensions = array('jpg', 'png','jpeg');
424
 
425
  $mime = get_post_mime_type($post);
enable-media-replace.php CHANGED
@@ -3,7 +3,7 @@
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: 4.0.0
7
  * Author: ShortPixel
8
  * Author URI: https://shortpixel.com
9
  * GitHub Plugin URI: https://github.com/short-pixel-optimizer/enable-media-replace
@@ -27,7 +27,7 @@
27
 
28
  namespace EnableMediaReplace;
29
 
30
- define( 'EMR_VERSION', '4.0.0' );
31
 
32
  if ( ! defined( 'ABSPATH' ) ) {
33
  exit; // Exit if accessed directly.
@@ -80,6 +80,8 @@ require_once( $plugin_path . 'classes/externals.php' );
80
  require_once( $plugin_path . 'classes/external/elementor.php' );
81
  require_once( $plugin_path . 'classes/external/wpbakery.php' );
82
  require_once( $plugin_path . 'classes/external/upsell_installer.php' );
 
 
83
  require_once( $plugin_path . 'thumbnail_updater.php' );
84
 
85
  function emr()
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: 4.0.1
7
  * Author: ShortPixel
8
  * Author URI: https://shortpixel.com
9
  * GitHub Plugin URI: https://github.com/short-pixel-optimizer/enable-media-replace
27
 
28
  namespace EnableMediaReplace;
29
 
30
+ define( 'EMR_VERSION', '4.0.1' );
31
 
32
  if ( ! defined( 'ABSPATH' ) ) {
33
  exit; // Exit if accessed directly.
80
  require_once( $plugin_path . 'classes/external/elementor.php' );
81
  require_once( $plugin_path . 'classes/external/wpbakery.php' );
82
  require_once( $plugin_path . 'classes/external/upsell_installer.php' );
83
+ require_once( $plugin_path . 'classes/external/siteorigin.php' );
84
+
85
  require_once( $plugin_path . 'thumbnail_updater.php' );
86
 
87
  function emr()
js/emr_admin.js CHANGED
@@ -67,18 +67,18 @@
67
  }
68
  },
69
  });
70
- },
71
  this.checkCustomDate = function()
72
  {
73
  if ($('input[name="timestamp_replace"]:checked').val() == 3)
74
  this.showCustomDate();
75
  else
76
  this.hideCustomDate();
77
- },
78
  this.showCustomDate = function()
79
  {
80
  $('.custom_date').css('visibility', 'visible').fadeTo(100, 1);
81
- },
82
  this.hideCustomDate = function()
83
  {
84
  $('.custom_date').fadeTo(100,0,
@@ -113,7 +113,7 @@
113
  this.updatePreview(null);
114
  }
115
  this.checkSubmit();
116
- },
117
  this.updatePreview = function(file)
118
  {
119
  var preview = $('.image_placeholder').last();
@@ -216,7 +216,7 @@
216
  // textlayer.css('margin-left', '-' + (textlayer.width() / 2 ) + 'px');
217
  }
218
 
219
- },
220
  this.updateFileSize = function(preview, file)
221
  {
222
  if (file === null)
@@ -255,12 +255,12 @@
255
  else {
256
  $('input[type="submit"]').prop('disabled', true);
257
  }
258
- },
259
  this.toggleErrors = function(toggle)
260
  {
261
  $('.form-error').fadeOut();
262
  $('.form-warning').fadeOut();
263
- },
264
  this.checkUpload = function(fileItem)
265
  {
266
  var maxsize = emr_options.maxfilesize;
@@ -280,7 +280,7 @@
280
  return false;
281
  }
282
  return true;
283
- },
284
  this.errorFileSize = function(fileItem)
285
  {
286
  $('.form-error.filesize').find('.fn').text(fileItem.name);
67
  }
68
  },
69
  });
70
+ }
71
  this.checkCustomDate = function()
72
  {
73
  if ($('input[name="timestamp_replace"]:checked').val() == 3)
74
  this.showCustomDate();
75
  else
76
  this.hideCustomDate();
77
+ }
78
  this.showCustomDate = function()
79
  {
80
  $('.custom_date').css('visibility', 'visible').fadeTo(100, 1);
81
+ }
82
  this.hideCustomDate = function()
83
  {
84
  $('.custom_date').fadeTo(100,0,
113
  this.updatePreview(null);
114
  }
115
  this.checkSubmit();
116
+ }
117
  this.updatePreview = function(file)
118
  {
119
  var preview = $('.image_placeholder').last();
216
  // textlayer.css('margin-left', '-' + (textlayer.width() / 2 ) + 'px');
217
  }
218
 
219
+ }
220
  this.updateFileSize = function(preview, file)
221
  {
222
  if (file === null)
255
  else {
256
  $('input[type="submit"]').prop('disabled', true);
257
  }
258
+ }
259
  this.toggleErrors = function(toggle)
260
  {
261
  $('.form-error').fadeOut();
262
  $('.form-warning').fadeOut();
263
+ }
264
  this.checkUpload = function(fileItem)
265
  {
266
  var maxsize = emr_options.maxfilesize;
280
  return false;
281
  }
282
  return true;
283
+ }
284
  this.errorFileSize = function(fileItem)
285
  {
286
  $('.form-error.filesize').find('.fn').text(fileItem.name);
readme.txt CHANGED
@@ -3,9 +3,9 @@ Contributors: ShortPixel
3
  Donate link: https://www.paypal.me/resizeImage
4
  Tags: replace, attachment, media, files, replace image, remove background, replace jpg, change media, replace media, image, file
5
  Requires at least: 4.9.7
6
- Tested up to: 6.0
7
  Requires PHP: 5.6
8
- Stable tag: 4.0.0
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
 
@@ -61,6 +61,17 @@ If you want more control over the format in which the time is shown, you can use
61
 
62
  == Changelog ==
63
 
 
 
 
 
 
 
 
 
 
 
 
64
  = 4.0.0 =
65
 
66
  Release date: September 5th, 2022
3
  Donate link: https://www.paypal.me/resizeImage
4
  Tags: replace, attachment, media, files, replace image, remove background, replace jpg, change media, replace media, image, file
5
  Requires at least: 4.9.7
6
+ Tested up to: 6.1
7
  Requires PHP: 5.6
8
+ Stable tag: 4.0.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
 
61
 
62
  == Changelog ==
63
 
64
+ = 4.0.1 =
65
+
66
+ Release date: November 23, 2022
67
+ * Fix: files are now replaced correctly in environments running on Windows/IIC servers;
68
+ * Fix: the mime type is now correctly detected, so that a warning is no longer displayed when replacing PDFs;
69
+ * Fix: the plugin no longer tries to load its code for post IDs that are not attachments, like comments;
70
+ * Fix: some warnings were displayed when using the Members plugin;
71
+ * Compat: added integration for SiteOrigin Page Builder to correctly replace files in its widgets;
72
+ * Compat: added `emr_after_remove_current` action hook (thanks @luistar15);
73
+ * Tweak: added filter to disable the background replace functionality.
74
+
75
  = 4.0.0 =
76
 
77
  Release date: September 5th, 2022
views/do-replace-background.php CHANGED
@@ -29,7 +29,7 @@ if (is_null($post_id)) {
29
 
30
  $attachment = get_post($post_id);
31
 
32
- if (! emr()->checkImagePermission($attachment->post_author, $attachment->ID)) {
33
  wp_die(esc_html__('You do not have permission to upload files for this author.', 'enable-media-replace'));
34
  }
35
 
29
 
30
  $attachment = get_post($post_id);
31
 
32
+ if (! emr()->checkImagePermission($attachment)) {
33
  wp_die(esc_html__('You do not have permission to upload files for this author.', 'enable-media-replace'));
34
  }
35
 
views/popup.php CHANGED
@@ -30,7 +30,7 @@ $table_name = $wpdb->prefix . "posts";
30
  $attachment_id = intval($_GET['attachment_id']);
31
  $attachment = get_post($attachment_id);
32
 
33
- if (! emr()->checkImagePermission($attachment->post_author, $attachment_id))
34
  {
35
  wp_die( esc_html__('You do not have permission to upload files for this author.', 'enable-media-replace') );
36
  }
30
  $attachment_id = intval($_GET['attachment_id']);
31
  $attachment = get_post($attachment_id);
32
 
33
+ if (! emr()->checkImagePermission($attachment))
34
  {
35
  wp_die( esc_html__('You do not have permission to upload files for this author.', 'enable-media-replace') );
36
  }
views/upload.php CHANGED
@@ -29,7 +29,7 @@ if (is_null($post_id)) {
29
  }
30
  $attachment = get_post($post_id);
31
 
32
- if (! emr()->checkImagePermission($attachment->post_author, $attachment->ID)) {
33
  wp_die(esc_html__('You do not have permission to upload files for this author.', 'enable-media-replace'));
34
  }
35
 
@@ -158,7 +158,6 @@ if (is_uploaded_file($_FILES["userfile"]["tmp_name"]) || isset($_POST["remove_bg
158
  var_dump($e->getMessage());
159
  die;
160
  Log::addError($e->getMessage());
161
- // exit($e->getMessage());
162
  }
163
 
164
  if (is_null($result)) {
29
  }
30
  $attachment = get_post($post_id);
31
 
32
+ if (! emr()->checkImagePermission($attachment)) {
33
  wp_die(esc_html__('You do not have permission to upload files for this author.', 'enable-media-replace'));
34
  }
35
 
158
  var_dump($e->getMessage());
159
  die;
160
  Log::addError($e->getMessage());
 
161
  }
162
 
163
  if (is_null($result)) {