Enable Media Replace - Version 3.3.4

Version Description

Release date: 23rd July 2019 * compatibility fixes for WP version 4.8 and below * cache killer

Download this release

Release Info

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

Code changes from version 3.3.2 to 3.3.4

build/shortpixel/PackageLoader.php CHANGED
@@ -61,7 +61,7 @@ class PackageLoader
61
  }
62
  $filename = preg_replace("#\\\\#", "/", $classname).".php";
63
  foreach ($classpaths as $classpath) {
64
- $fullpath = $this->dir."/".$classpath."/$filename";
65
  if (file_exists($fullpath)) {
66
  include_once $fullpath;
67
  }
61
  }
62
  $filename = preg_replace("#\\\\#", "/", $classname).".php";
63
  foreach ($classpaths as $classpath) {
64
+ $fullpath = $dir."/".$classpath."/$filename";
65
  if (file_exists($fullpath)) {
66
  include_once $fullpath;
67
  }
build/shortpixel/log/src/DebugItem.php CHANGED
@@ -118,7 +118,12 @@ class DebugItem
118
 
119
  protected function setCaller()
120
  {
121
- $debug=debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,5);
 
 
 
 
 
122
  $i = 4;
123
  if (isset($debug[$i]))
124
  {
118
 
119
  protected function setCaller()
120
  {
121
+ if(PHP_VERSION_ID < 50400) {
122
+ $debug=debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
123
+ } else {
124
+ $debug=debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,5);
125
+ }
126
+
127
  $i = 4;
128
  if (isset($debug[$i]))
129
  {
classes/compat.php ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ // Compatibility functions for old version of WordPress / PHP / Other
4
+
5
+
6
+ /*
7
+ * Introduced in WP 4.9.7 - https://developer.wordpress.org/reference/functions/wp_delete_attachment_files/
8
+ * Compat for previous versions.
9
+ */
10
+ if (! function_exists('wp_delete_attachment_files'))
11
+ {
12
+ function wp_delete_attachment_files($post_id, $meta, $backup_sizes, $file )
13
+ {
14
+ global $wpdb;
15
+ $uploadpath = wp_get_upload_dir();
16
+ $deleted = true;
17
+
18
+ if ( ! empty( $meta['thumb'] ) ) {
19
+ // Don't delete the thumb if another attachment uses it.
20
+ if ( ! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $wpdb->esc_like( $meta['thumb'] ) . '%', $post_id ) ) ) {
21
+ $thumbfile = str_replace( wp_basename( $file ), $meta['thumb'], $file );
22
+ if ( ! empty( $thumbfile ) ) {
23
+ $thumbfile = path_join( $uploadpath['basedir'], $thumbfile );
24
+ $thumbdir = path_join( $uploadpath['basedir'], dirname( $file ) );
25
+
26
+ if ( ! wp_delete_file_from_directory( $thumbfile, $thumbdir ) ) {
27
+ $deleted = false;
28
+ }
29
+ }
30
+ }
31
+ }
32
+
33
+ // Remove intermediate and backup images if there are any.
34
+ if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
35
+ $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
36
+ foreach ( $meta['sizes'] as $size => $sizeinfo ) {
37
+ $intermediate_file = str_replace( wp_basename( $file ), $sizeinfo['file'], $file );
38
+ if ( ! empty( $intermediate_file ) ) {
39
+ $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
40
+
41
+ if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
42
+ $deleted = false;
43
+ }
44
+ }
45
+ }
46
+ }
47
+
48
+ if ( is_array( $backup_sizes ) ) {
49
+ $del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) );
50
+ foreach ( $backup_sizes as $size ) {
51
+ $del_file = path_join( dirname( $meta['file'] ), $size['file'] );
52
+ if ( ! empty( $del_file ) ) {
53
+ $del_file = path_join( $uploadpath['basedir'], $del_file );
54
+
55
+ if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
56
+ $deleted = false;
57
+ }
58
+ }
59
+ }
60
+ }
61
+
62
+ if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
63
+ $deleted = false;
64
+ }
65
+
66
+ return $deleted;
67
+
68
+ }
69
+ } // end function
70
+
71
+
72
+ /*
73
+ * Introduced in WP 4.9.7 - https://developer.wordpress.org/reference/functions/wp_delete_attachment_files/
74
+ * Compat for previous versions.
75
+ */
76
+ if (! function_exists('wp_delete_file_from_directory'))
77
+ {
78
+ function wp_delete_file_from_directory( $file, $directory ) {
79
+ if ( wp_is_stream( $file ) ) {
80
+ $real_file = wp_normalize_path( $file );
81
+ $real_directory = wp_normalize_path( $directory );
82
+ } else {
83
+ $real_file = realpath( wp_normalize_path( $file ) );
84
+ $real_directory = realpath( wp_normalize_path( $directory ) );
85
+ }
86
+
87
+ if ( false === $real_file || false === $real_directory || strpos( $real_file, trailingslashit( $real_directory ) ) !== 0 ) {
88
+ return false;
89
+ }
90
+
91
+ wp_delete_file( $file );
92
+
93
+ return true;
94
+ }
95
+
96
+ } // end function
classes/emr-plugin.php CHANGED
@@ -35,7 +35,6 @@ class EnableMediaReplacePlugin
35
  add_action('admin_enqueue_scripts', array($this,'admin_scripts'));
36
 
37
  // content filters
38
- add_filter('attachment_fields_to_edit', array($this, 'attachment_editor'), 10, 2);
39
  add_filter('media_row_actions', array($this,'add_media_action'), 10, 2);
40
  add_action('attachment_submitbox_misc_actions', array($this,'admin_date_replaced_media_on_edit_media_screen'), 91 );
41
  add_filter('upload_mimes', array($this,'add_mime_types'), 1, 1);
@@ -45,9 +44,20 @@ class EnableMediaReplacePlugin
45
  add_action('network_admin_notices', array($this,'display_network_notices'));
46
  add_action('wp_ajax_emr_dismiss_notices', array($this,'dismiss_notices'));
47
 
 
48
  // shortcode
49
  add_shortcode('file_modified', array($this, 'get_modified_date'));
50
 
 
 
 
 
 
 
 
 
 
 
51
  }
52
 
53
  /**
@@ -128,6 +138,8 @@ class EnableMediaReplacePlugin
128
  wp_register_style('emr_style', plugins_url('css/admin.css', EMR_ROOT_FILE) );
129
  }
130
 
 
 
131
  wp_register_script('emr_admin', plugins_url('js/emr_admin.js', EMR_ROOT_FILE), array('jquery'), false, true );
132
  $emr_options = array(
133
  'dateFormat' => $this->convertdate(get_option( 'date_format' )),
@@ -135,6 +147,7 @@ class EnableMediaReplacePlugin
135
 
136
  );
137
 
 
138
  if (Log::debugIsActive())
139
  $emr_options['is_debug'] = true;
140
 
@@ -179,30 +192,43 @@ class EnableMediaReplacePlugin
179
 
180
  }
181
 
182
- /**
183
- * Add some new fields to the attachment edit panel.
184
- * @param array form fields edit panel
185
- * @return array form fields with enable-media-replace fields added
186
- */
187
- public function attachment_editor($form_fields, $post)
 
 
 
 
 
 
 
 
 
 
 
 
188
  {
189
- $url = $this->getMediaReplaceURL($post->ID);
190
-
191
- $action = "media_replace";
192
- $editurl = wp_nonce_url( $url, $action );
193
-
194
- /* Unneeded - admin_url already checks for force_ssl_admin ( in set_scheme function )
195
- if (FORCE_SSL_ADMIN) {
196
- $editurl = str_replace("http:", "https:", $editurl);
197
- } */
198
- $link = "href=\"$editurl\"";
199
- $form_fields["enable-media-replace"] = array(
200
- "label" => esc_html__("Replace media", "enable-media-replace"),
201
- "input" => "html",
202
- "html" => "<p><a class='button-secondary' $link>" . esc_html__("Upload a new file", "enable-media-replace") . "</a></p>", "helps" => esc_html__("To replace the current file, click the link and upload a replacement.", "enable-media-replace")
203
- );
204
-
205
- return $form_fields;
 
206
  }
207
 
208
  /**
@@ -266,8 +292,15 @@ class EnableMediaReplacePlugin
266
  * @param $post Obj Post Object
267
  */
268
  function admin_date_replaced_media_on_edit_media_screen($post) {
269
- $post_id = $post->ID;
270
 
 
 
 
 
 
 
 
 
271
  if ( $post->post_modified == $post->post_date ) {
272
  return;
273
  }
@@ -281,6 +314,18 @@ class EnableMediaReplacePlugin
281
  <?php
282
  }
283
 
 
 
 
 
 
 
 
 
 
 
 
 
284
  /**
285
  * Shorttag function to show the media file modification date/time.
286
  * @param array shorttag attributes
35
  add_action('admin_enqueue_scripts', array($this,'admin_scripts'));
36
 
37
  // content filters
 
38
  add_filter('media_row_actions', array($this,'add_media_action'), 10, 2);
39
  add_action('attachment_submitbox_misc_actions', array($this,'admin_date_replaced_media_on_edit_media_screen'), 91 );
40
  add_filter('upload_mimes', array($this,'add_mime_types'), 1, 1);
44
  add_action('network_admin_notices', array($this,'display_network_notices'));
45
  add_action('wp_ajax_emr_dismiss_notices', array($this,'dismiss_notices'));
46
 
47
+ add_action( 'add_meta_boxes', function () { add_meta_box('emr-eplace-box', __('Replace Image', 'enable-media-replace'), array($this, 'replace_meta_box'), 'attachment', 'side', 'low'); } );
48
  // shortcode
49
  add_shortcode('file_modified', array($this, 'get_modified_date'));
50
 
51
+ /** Just after an image is replaced, try to browser decache the images */
52
+ if (isset($_GET['emr_replaced']) && intval($_GET['emr_replaced'] == 1))
53
+ {
54
+ add_filter('wp_get_attachment_image_src',array($this, 'attempt_uncache_image'), 10, 4);
55
+
56
+ // adds a metabox to list thumbnails. This is a cache reset hidden as feature.
57
+ add_action( 'add_meta_boxes', function () { add_meta_box('emr-replace-box', __('Replaced Thumbnails Preview', 'enable-media-replace'), array($this, 'show_thumbs_box'), 'attachment', 'side', 'low'); } );
58
+ add_filter('postbox_classes_attachment_emr-replace-box', function($classes) { $classes[] = 'closed'; return $classes; });
59
+ }
60
+
61
  }
62
 
63
  /**
138
  wp_register_style('emr_style', plugins_url('css/admin.css', EMR_ROOT_FILE) );
139
  }
140
 
141
+ wp_register_style('emr_edit-attachment', plugins_url('css/edit_attachment.css', EMR_ROOT_FILE));
142
+
143
  wp_register_script('emr_admin', plugins_url('js/emr_admin.js', EMR_ROOT_FILE), array('jquery'), false, true );
144
  $emr_options = array(
145
  'dateFormat' => $this->convertdate(get_option( 'date_format' )),
147
 
148
  );
149
 
150
+
151
  if (Log::debugIsActive())
152
  $emr_options['is_debug'] = true;
153
 
192
 
193
  }
194
 
195
+ public function replace_meta_box($post)
196
+ {
197
+ $url = $this->getMediaReplaceURL($post->ID);
198
+
199
+ $action = "media_replace";
200
+ $editurl = wp_nonce_url( $url, $action );
201
+
202
+ /* Unneeded - admin_url already checks for force_ssl_admin ( in set_scheme function )
203
+ if (FORCE_SSL_ADMIN) {
204
+ $editurl = str_replace("http:", "https:", $editurl);
205
+ } */
206
+ $link = "href=\"$editurl\"";
207
+
208
+
209
+ echo "<p><a class='button-secondary' $link>" . esc_html__("Upload a new file", "enable-media-replace") . "</a></p><p>" . esc_html__("To replace the current file, click the link and upload a replacement.", "enable-media-replace") . "</p>";
210
+ }
211
+
212
+ public function show_thumbs_box($post)
213
  {
214
+ wp_enqueue_style('emr_edit-attachment');
215
+
216
+ $meta = wp_get_attachment_metadata($post->ID);
217
+
218
+ if (! isset($meta['sizes']) )
219
+ { echo __('Thumbnails were not generated', 'enable-media-replace');
220
+ return false;
221
+ }
222
+
223
+ foreach($meta['sizes'] as $size => $data)
224
+ {
225
+ $display_size = ucfirst(str_replace("_", " ", $size));
226
+ $img = wp_get_attachment_image_src($post->ID, $size);
227
+ echo "<div class='$size previewwrapper'><img src='" . $img[0] . "'><span class='label'>$display_size</span></div>";
228
+ }
229
+
230
+
231
+
232
  }
233
 
234
  /**
292
  * @param $post Obj Post Object
293
  */
294
  function admin_date_replaced_media_on_edit_media_screen($post) {
 
295
 
296
+ // Fallback for before version 4.9, doens't pass post.
297
+ if (! is_object($post))
298
+ global $post;
299
+
300
+ if (! is_object($post)) // try to global, if it doesn't work - return.
301
+ return false;
302
+
303
+ $post_id = $post->ID;
304
  if ( $post->post_modified == $post->post_date ) {
305
  return;
306
  }
314
  <?php
315
  }
316
 
317
+ /** When an image is just replaced, it can stuck in the browser cache making a look like it was not replaced. Try
318
+ * undo that effect by adding a timestamp to the query string */
319
+ public function attempt_uncache_image($image, $attachment_id, $size, $icon)
320
+ {
321
+ if ($image === false)
322
+ return $image;
323
+
324
+ // array with image src on 0
325
+ $image[0] = add_query_arg('time', time(), $image[0]);
326
+ return $image;
327
+ }
328
+
329
  /**
330
  * Shorttag function to show the media file modification date/time.
331
  * @param array shorttag attributes
classes/externals.php CHANGED
@@ -57,8 +57,4 @@ class Externals
57
 
58
  }
59
 
60
-
61
-
62
-
63
-
64
  }
57
 
58
  }
59
 
 
 
 
 
60
  }
classes/replacer.php CHANGED
@@ -300,15 +300,16 @@ class Replacer
300
  }
301
 
302
  /* Search and replace in WP_POSTS */
303
- $posts_sql = $wpdb->remove_placeholder_escape($wpdb->prepare(
 
304
  "SELECT ID, post_content FROM $wpdb->posts WHERE post_status = 'publish' AND post_content LIKE %s;",
305
- '%' . $current_base_url . '%'));
306
 
307
  //INNER JOIN ' . $wpdb->posts . ' on ' . $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id
308
 
309
  $postmeta_sql = 'SELECT meta_id, post_id, meta_value FROM ' . $wpdb->postmeta . '
310
  WHERE post_id in (SELECT ID from '. $wpdb->posts . ' where post_status = "publish") AND meta_value like %s ';
311
- $postmeta_sql = $wpdb->remove_placeholder_escape($wpdb->prepare($postmeta_sql, '%' . $current_base_url . '%'));
312
 
313
  $rsmeta = $wpdb->get_results($postmeta_sql, ARRAY_A);
314
  $rs = $wpdb->get_results( $posts_sql, ARRAY_A );
300
  }
301
 
302
  /* Search and replace in WP_POSTS */
303
+ // Removed $wpdb->remove_placeholder_escape from here, not compatible with WP 4.8
304
+ $posts_sql = $wpdb->prepare(
305
  "SELECT ID, post_content FROM $wpdb->posts WHERE post_status = 'publish' AND post_content LIKE %s;",
306
+ '%' . $current_base_url . '%');
307
 
308
  //INNER JOIN ' . $wpdb->posts . ' on ' . $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id
309
 
310
  $postmeta_sql = 'SELECT meta_id, post_id, meta_value FROM ' . $wpdb->postmeta . '
311
  WHERE post_id in (SELECT ID from '. $wpdb->posts . ' where post_status = "publish") AND meta_value like %s ';
312
+ $postmeta_sql = $wpdb->prepare($postmeta_sql, '%' . $current_base_url . '%');
313
 
314
  $rsmeta = $wpdb->get_results($postmeta_sql, ARRAY_A);
315
  $rs = $wpdb->get_results( $posts_sql, ARRAY_A );
classes/uihelper.php CHANGED
@@ -51,7 +51,7 @@ class UIHelper
51
  public function getSuccesRedirect($post_id)
52
  {
53
  $url = admin_url('post.php');
54
- $url = add_query_arg(array('action' => 'edit', 'post' => $post_id), $url);
55
 
56
  $url = apply_filters('emr_returnurl', $url);
57
  Log::addDebug('Success URL- ' . $url);
51
  public function getSuccesRedirect($post_id)
52
  {
53
  $url = admin_url('post.php');
54
+ $url = add_query_arg(array('action' => 'edit', 'post' => $post_id, 'emr_replaced' => '1'), $url);
55
 
56
  $url = apply_filters('emr_returnurl', $url);
57
  Log::addDebug('Success URL- ' . $url);
composer.json DELETED
@@ -1,25 +0,0 @@
1
- {
2
- "repositories": [
3
- {
4
- "packagist.org": false,
5
- "type": "path",
6
- "url": "../modules/*",
7
- "options": {
8
- "symlink": true
9
- }
10
- }
11
- ],
12
- "require": {
13
- "shortpixel/notices":"@dev",
14
- "shortpixel/build" : "@dev"
15
- },
16
-
17
- "scripts": {
18
- "post-update-cmd" : "\\ShortPixel\\Build\\Build::BuildIt",
19
- "buildSP" : "\\ShortPixel\\Build\\Build::BuildIt"
20
- },
21
- "extra": {
22
- "targetNamespace" : "EnableMediaReplace"
23
- }
24
-
25
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
css/edit_attachment.css ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Styling for the edit attachment screen */
2
+ #emr-replace-box .previewwrapper {
3
+ display: inline-block;
4
+ position: relative;
5
+ clear: both;
6
+ margin: 3px 0; }
7
+ #emr-replace-box .previewwrapper img {
8
+ max-width: 100%; }
9
+ #emr-replace-box .previewwrapper span.label {
10
+ font-size: 14px;
11
+ color: #fff;
12
+ position: absolute;
13
+ line-height: 16px;
14
+ margin-top: -8px;
15
+ top: 50%;
16
+ left: 0;
17
+ right: 0;
18
+ background: rgba(0, 0, 0, 0.5);
19
+ text-align: center;
20
+ padding: 4px 0; }
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: 3.3.2
7
  Author: ShortPixel
8
  Author URI: https://shortpixel.com
9
  Text Domain: enable-media-replace
@@ -43,6 +43,7 @@ if(!defined("SHORTPIXEL_AFFILIATE_CODE")) {
43
  }
44
 
45
  require_once('build/shortpixel/autoload.php');
 
46
  require_once('classes/replacer.php');
47
  require_once('classes/uihelper.php');
48
  require_once('classes/file.php');
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.3.4
7
  Author: ShortPixel
8
  Author URI: https://shortpixel.com
9
  Text Domain: enable-media-replace
43
  }
44
 
45
  require_once('build/shortpixel/autoload.php');
46
+ require_once('classes/compat.php');
47
  require_once('classes/replacer.php');
48
  require_once('classes/uihelper.php');
49
  require_once('classes/file.php');
readme.txt CHANGED
@@ -47,6 +47,17 @@ If you want more control over the format used to display the time, you can use t
47
 
48
  == Changelog ==
49
 
 
 
 
 
 
 
 
 
 
 
 
50
  = 3.3.2 =
51
 
52
  Release date: 17th July 2019
@@ -306,3 +317,8 @@ Second, if the file really looks unchanged, make sure WordPress has write permis
306
  == Wishlist / Coming attractions ==
307
 
308
  Do you have suggestions? Feel free to contact ShortPixel <a href="https://shortpixel.com/contact" target="_blank">here</a>
 
 
 
 
 
47
 
48
  == Changelog ==
49
 
50
+ = 3.3.4 =
51
+
52
+ Release date: 23rd July 2019
53
+ * compatibility fixes for WP version 4.8 and below
54
+ * cache killer
55
+
56
+ = 3.3.3 =
57
+
58
+ Release date: 19th July 2019
59
+ * Fix error "using $this when not in object context" on some PHP versions
60
+
61
  = 3.3.2 =
62
 
63
  Release date: 17th July 2019
317
  == Wishlist / Coming attractions ==
318
 
319
  Do you have suggestions? Feel free to contact ShortPixel <a href="https://shortpixel.com/contact" target="_blank">here</a>
320
+
321
+
322
+ == Contribute ==
323
+
324
+ Want to help us improve the plugin feel free to submit PRs via GitHub <a href="https://github.com/short-pixel-optimizer/enable-media-replace" target="_blank">here</a>.
scss/edit_attachment.scss ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ /* Styling for the edit attachment screen */
3
+ #emr-replace-box
4
+ {
5
+ .previewwrapper
6
+ {
7
+ display: inline-block;
8
+ position: relative;
9
+ clear: both;
10
+ margin: 3px 0;
11
+
12
+ img { max-width: 100%; }
13
+ span.label
14
+ {
15
+ font-size: 14px;
16
+ color: #fff;
17
+ position: absolute;
18
+ line-height: 16px;
19
+ margin-top: -8px;
20
+ top: 50%;
21
+ left: 0; right: 0;
22
+ background: rgba(0,0,0,0.5);
23
+ text-align: center;
24
+ padding: 4px 0;
25
+
26
+ }
27
+
28
+ }
29
+
30
+ }