Enable Media Replace - Version 3.3.5

Version Description

Release date: 25th July 2019 * fix Replace button on the MediaLibrary image details popup

Download this release

Release Info

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

Code changes from version 3.3.4 to 3.3.5

classes/emr-plugin.php CHANGED
@@ -34,6 +34,7 @@ class EnableMediaReplacePlugin
34
  add_action('admin_init', array($this,'init'));
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 );
@@ -44,7 +45,10 @@ class EnableMediaReplacePlugin
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
 
@@ -226,9 +230,21 @@ class EnableMediaReplacePlugin
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
  /**
34
  add_action('admin_init', array($this,'init'));
35
  add_action('admin_enqueue_scripts', array($this,'admin_scripts'));
36
 
37
+
38
  // content filters
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 );
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
+ // editors
49
  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'); } );
50
+ add_filter('attachment_fields_to_edit', array($this, 'attachment_editor'), 10, 2);
51
+
52
  // shortcode
53
  add_shortcode('file_modified', array($this, 'get_modified_date'));
54
 
230
  $img = wp_get_attachment_image_src($post->ID, $size);
231
  echo "<div class='$size previewwrapper'><img src='" . $img[0] . "'><span class='label'>$display_size</span></div>";
232
  }
233
+ }
234
 
235
+ public function attachment_editor($form_fields, $post)
236
+ {
237
+ $url = $this->getMediaReplaceURL($post->ID);
238
+ $action = "media_replace";
239
+ $editurl = wp_nonce_url( $url, $action );
240
+
241
+ $link = "href=\"$editurl\"";
242
+ $form_fields["enable-media-replace"] = array(
243
+ "label" => esc_html__("Replace media", "enable-media-replace"),
244
+ "input" => "html",
245
+ "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")
246
+ );
247
+ return $form_fields;
248
  }
249
 
250
  /**
classes/file.php CHANGED
@@ -10,7 +10,7 @@ class emrFile
10
  protected $file; // the full file w/ path.
11
  protected $extension;
12
  protected $fileName;
13
- protected $filePath;
14
  protected $fileURL;
15
  protected $fileMime;
16
  protected $permissions = 0;
@@ -30,13 +30,14 @@ class emrFile
30
  $fileparts = pathinfo($file);
31
 
32
  $this->fileName = isset($fileparts['basename']) ? $fileparts['basename'] : '';
33
- $this->filePath = isset($fileparts['dirname']) ? $fileparts['dirname'] : '';
34
  $this->extension = isset($fileparts['extension']) ? $fileparts['extension'] : '';
35
  if ($this->exists) // doesn't have to be.
36
  $this->permissions = fileperms($file) & 0777;
37
 
38
  $filedata = wp_check_filetype_and_ext($this->file, $this->fileName);
39
  // This will *not* be checked, is not meant for permission of validation!
 
40
  $this->fileMime = (isset($filedata['type'])) ? $filedata['type'] : false;
41
 
42
  }
@@ -89,9 +90,4 @@ class emrFile
89
  {
90
  return $this->exists;
91
  }
92
-
93
-
94
  }
95
-
96
-
97
- ?>
10
  protected $file; // the full file w/ path.
11
  protected $extension;
12
  protected $fileName;
13
+ protected $filePath; // with trailing slash!
14
  protected $fileURL;
15
  protected $fileMime;
16
  protected $permissions = 0;
30
  $fileparts = pathinfo($file);
31
 
32
  $this->fileName = isset($fileparts['basename']) ? $fileparts['basename'] : '';
33
+ $this->filePath = isset($fileparts['dirname']) ? trailingslashit($fileparts['dirname']) : '';
34
  $this->extension = isset($fileparts['extension']) ? $fileparts['extension'] : '';
35
  if ($this->exists) // doesn't have to be.
36
  $this->permissions = fileperms($file) & 0777;
37
 
38
  $filedata = wp_check_filetype_and_ext($this->file, $this->fileName);
39
  // This will *not* be checked, is not meant for permission of validation!
40
+ // Note: this function will work on non-existing file, but not on existing files containing wrong mime in file.
41
  $this->fileMime = (isset($filedata['type'])) ? $filedata['type'] : false;
42
 
43
  }
90
  {
91
  return $this->exists;
92
  }
 
 
93
  }
 
 
 
classes/functions.php ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ namespace EnableMediaReplace;
3
+
4
+ // Legacy functions.
5
+
6
+ /**
7
+ * Maybe remove query string from URL.
8
+ *
9
+ * @param string $url
10
+ *
11
+ * @return string
12
+ */
13
+
14
+ function emr_maybe_remove_query_string( $url ) {
15
+ $parts = explode( '?', $url );
16
+
17
+ return reset( $parts );
18
+ }
19
+
20
+ /**
21
+ * Remove scheme from URL.
22
+ *
23
+ * @param string $url
24
+ *
25
+ * @return string
26
+ */
27
+ function emr_remove_scheme( $url ) {
28
+ return preg_replace( '/^(?:http|https):/', '', $url );
29
+ }
30
+
31
+ /**
32
+ * Remove size from filename (image[-100x100].jpeg).
33
+ *
34
+ * @param string $url
35
+ * @param bool $remove_extension
36
+ *
37
+ * @return string
38
+ */
39
+ function emr_remove_size_from_filename( $url, $remove_extension = false ) {
40
+ $url = preg_replace( '/^(\S+)-[0-9]{1,4}x[0-9]{1,4}(\.[a-zA-Z0-9\.]{2,})?/', '$1$2', $url );
41
+
42
+ if ( $remove_extension ) {
43
+ $ext = pathinfo( $url, PATHINFO_EXTENSION );
44
+ $url = str_replace( ".$ext", '', $url );
45
+ }
46
+
47
+ return $url;
48
+ }
49
+
50
+ /**
51
+ * Strip an image URL down to bare minimum for matching.
52
+ *
53
+ * @param string $url
54
+ *
55
+ * @return string
56
+ */
57
+ function emr_get_match_url($url) {
58
+ $url = emr_remove_scheme($url);
59
+ $url = emr_maybe_remove_query_string($url);
60
+ $url = emr_remove_size_from_filename($url, true);
61
+ $url = emr_remove_domain_from_filename($url);
62
+ return $url;
63
+ }
64
+
65
+
66
+ function emr_remove_domain_from_filename($url) {
67
+ // Holding place for possible future function
68
+ $url = str_replace(emr_remove_scheme(get_bloginfo('url')), '', $url);
69
+ return $url;
70
+ }
71
+
72
+ /**
73
+ * Build an array of search or replace URLs for given attachment GUID and its metadata.
74
+ *
75
+ * @param string $guid
76
+ * @param array $metadata
77
+ *
78
+ * @return array
79
+ */
80
+ function emr_get_file_urls( $guid, $metadata ) {
81
+ $urls = array();
82
+
83
+ $guid = emr_remove_scheme( $guid );
84
+ $guid= emr_remove_domain_from_filename($guid);
85
+
86
+ $urls['guid'] = $guid;
87
+
88
+ if ( empty( $metadata ) ) {
89
+ return $urls;
90
+ }
91
+
92
+ $base_url = dirname( $guid );
93
+
94
+ if ( ! empty( $metadata['file'] ) ) {
95
+ $urls['file'] = trailingslashit( $base_url ) . wp_basename( $metadata['file'] );
96
+ }
97
+
98
+ if ( ! empty( $metadata['sizes'] ) ) {
99
+ foreach ( $metadata['sizes'] as $key => $value ) {
100
+ $urls[ $key ] = trailingslashit( $base_url ) . wp_basename( $value['file'] );
101
+ }
102
+ }
103
+
104
+ return $urls;
105
+ }
106
+
107
+ /**
108
+ * Ensure new search URLs cover known sizes for old attachment.
109
+ * Falls back to full URL if size not covered (srcset or width/height attributes should compensate).
110
+ *
111
+ * @param array $old
112
+ * @param array $new
113
+ *
114
+ * @return array
115
+ */
116
+ function emr_normalize_file_urls( $old, $new ) {
117
+ $result = array();
118
+
119
+ if ( empty( $new['guid'] ) ) {
120
+ return $result;
121
+ }
122
+
123
+ $guid = $new['guid'];
124
+
125
+ foreach ( $old as $key => $value ) {
126
+ $result[ $key ] = empty( $new[ $key ] ) ? $guid : $new[ $key ];
127
+ }
128
+
129
+ return $result;
130
+ }
classes/replacer.php CHANGED
@@ -21,8 +21,8 @@ class Replacer
21
  protected $target_metadata;
22
  protected $target_url;
23
 
24
- protected $replaceMode = null;
25
- protected $timeMode = null;
26
  protected $datetime = null;
27
 
28
  protected $ThumbnailUpdater; // class
@@ -30,9 +30,9 @@ class Replacer
30
  const MODE_REPLACE = 1;
31
  const MODE_SEARCHREPLACE = 2;
32
 
33
- const TIME_UPDATEALL = 1;
34
- const TIME_UPDATEMODIFIED = 2;
35
- const TIME_CUSTOM = 3;
36
 
37
  public function __construct($post_id)
38
  {
21
  protected $target_metadata;
22
  protected $target_url;
23
 
24
+ protected $replaceMode = 1; // replace if nothing is set
25
+ protected $timeMode = 1;
26
  protected $datetime = null;
27
 
28
  protected $ThumbnailUpdater; // class
30
  const MODE_REPLACE = 1;
31
  const MODE_SEARCHREPLACE = 2;
32
 
33
+ const TIME_UPDATEALL = 1; // replace the date
34
+ const TIME_UPDATEMODIFIED = 2; // keep the date, update only modified
35
+ const TIME_CUSTOM = 3; // custom time entry
36
 
37
  public function __construct($post_id)
38
  {
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.4
7
  Author: ShortPixel
8
  Author URI: https://shortpixel.com
9
  Text Domain: enable-media-replace
@@ -44,6 +44,7 @@ if(!defined("SHORTPIXEL_AFFILIATE_CODE")) {
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');
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.5
7
  Author: ShortPixel
8
  Author URI: https://shortpixel.com
9
  Text Domain: enable-media-replace
44
 
45
  require_once('build/shortpixel/autoload.php');
46
  require_once('classes/compat.php');
47
+ require_once('classes/functions.php');
48
  require_once('classes/replacer.php');
49
  require_once('classes/uihelper.php');
50
  require_once('classes/file.php');
readme.txt CHANGED
@@ -47,6 +47,11 @@ If you want more control over the format used to display the time, you can use t
47
 
48
  == Changelog ==
49
 
 
 
 
 
 
50
  = 3.3.4 =
51
 
52
  Release date: 23rd July 2019
47
 
48
  == Changelog ==
49
 
50
+ = 3.3.5 =
51
+
52
+ Release date: 25th July 2019
53
+ * fix Replace button on the MediaLibrary image details popup
54
+
55
  = 3.3.4 =
56
 
57
  Release date: 23rd July 2019
views/popup.php CHANGED
@@ -5,7 +5,6 @@ namespace EnableMediaReplace;
5
  use EnableMediaReplace\ShortPixelLogger\ShortPixelLogger as Log;
6
  use EnableMediaReplace\Notices\NoticeController as Notices;
7
 
8
-
9
  /**
10
  * Uploadscreen for selecting and uploading new media file
11
  *
5
  use EnableMediaReplace\ShortPixelLogger\ShortPixelLogger as Log;
6
  use EnableMediaReplace\Notices\NoticeController as Notices;
7
 
 
8
  /**
9
  * Uploadscreen for selecting and uploading new media file
10
  *
views/upload.php CHANGED
@@ -21,196 +21,6 @@ global $wpdb;
21
  $table_name = $wpdb->prefix . "posts";
22
  $postmeta_table_name = $wpdb->prefix . "postmeta";
23
 
24
- /**
25
- * Delete a media file and its thumbnails.
26
- *
27
- * @param string $current_file
28
- * @param array|null $metadta
29
- */
30
- /* Phased-out, marked for delete.
31
- function emr_delete_current_files( $current_file, $metadta = null ) {
32
- // Delete old file
33
-
34
- // Find path of current file
35
- $current_path = substr($current_file, 0, (strrpos($current_file, "/")));
36
-
37
- // Check if old file exists first
38
- if (file_exists($current_file)) {
39
- // Now check for correct file permissions for old file
40
- clearstatcache();
41
- if (is_writable($current_file)) {
42
- // Everything OK; delete the file
43
- unlink($current_file);
44
- }
45
- else {
46
- // File exists, but has wrong permissions. Let the user know.
47
- 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);
48
- exit;
49
- }
50
- }
51
-
52
- // Delete old resized versions if this was an image
53
- $suffix = substr($current_file, (strlen($current_file)-4));
54
- $prefix = substr($current_file, 0, (strlen($current_file)-4));
55
-
56
- if (strtolower($suffix) === ".pdf") {
57
- $prefix .= "-pdf";
58
- $suffix = ".jpg";
59
- }
60
-
61
- $imgAr = array(".png", ".gif", ".jpg");
62
- if (in_array($suffix, $imgAr)) {
63
- // It's a png/gif/jpg based on file name
64
- // Get thumbnail filenames from metadata
65
- if ( empty( $metadata ) ) {
66
- $metadata = wp_get_attachment_metadata( $_POST["ID"] );
67
- }
68
-
69
- if (is_array($metadata)) { // Added fix for error messages when there is no metadata (but WHY would there not be? I don't know…)
70
- foreach($metadata["sizes"] AS $thissize) {
71
- // Get all filenames and do an unlink() on each one;
72
- $thisfile = $thissize["file"];
73
- // Create array with all old sizes for replacing in posts later
74
- $oldfilesAr[] = $thisfile;
75
- // Look for files and delete them
76
- if (strlen($thisfile)) {
77
- $thisfile = $current_path . "/" . $thissize["file"];
78
- if (file_exists($thisfile)) {
79
- unlink($thisfile);
80
- }
81
- }
82
- }
83
- }
84
- // Old (brutal) method, left here for now
85
- //$mask = $prefix . "-*x*" . $suffix;
86
- //array_map( "unlink", glob( $mask ) );
87
- }
88
- } */
89
-
90
- /**
91
- * Maybe remove query string from URL.
92
- *
93
- * @param string $url
94
- *
95
- * @return string
96
- */
97
- function emr_maybe_remove_query_string( $url ) {
98
- $parts = explode( '?', $url );
99
-
100
- return reset( $parts );
101
- }
102
-
103
- /**
104
- * Remove scheme from URL.
105
- *
106
- * @param string $url
107
- *
108
- * @return string
109
- */
110
- function emr_remove_scheme( $url ) {
111
- return preg_replace( '/^(?:http|https):/', '', $url );
112
- }
113
-
114
- /**
115
- * Remove size from filename (image[-100x100].jpeg).
116
- *
117
- * @param string $url
118
- * @param bool $remove_extension
119
- *
120
- * @return string
121
- */
122
- function emr_remove_size_from_filename( $url, $remove_extension = false ) {
123
- $url = preg_replace( '/^(\S+)-[0-9]{1,4}x[0-9]{1,4}(\.[a-zA-Z0-9\.]{2,})?/', '$1$2', $url );
124
-
125
- if ( $remove_extension ) {
126
- $ext = pathinfo( $url, PATHINFO_EXTENSION );
127
- $url = str_replace( ".$ext", '', $url );
128
- }
129
-
130
- return $url;
131
- }
132
-
133
- /**
134
- * Strip an image URL down to bare minimum for matching.
135
- *
136
- * @param string $url
137
- *
138
- * @return string
139
- */
140
- function emr_get_match_url($url) {
141
- $url = emr_remove_scheme($url);
142
- $url = emr_maybe_remove_query_string($url);
143
- $url = emr_remove_size_from_filename($url, true);
144
- $url = emr_remove_domain_from_filename($url);
145
- return $url;
146
- }
147
-
148
-
149
- function emr_remove_domain_from_filename($url) {
150
- // Holding place for possible future function
151
- $url = str_replace(emr_remove_scheme(get_bloginfo('url')), '', $url);
152
- return $url;
153
- }
154
-
155
- /**
156
- * Build an array of search or replace URLs for given attachment GUID and its metadata.
157
- *
158
- * @param string $guid
159
- * @param array $metadata
160
- *
161
- * @return array
162
- */
163
- function emr_get_file_urls( $guid, $metadata ) {
164
- $urls = array();
165
-
166
- $guid = emr_remove_scheme( $guid );
167
- $guid= emr_remove_domain_from_filename($guid);
168
-
169
- $urls['guid'] = $guid;
170
-
171
- if ( empty( $metadata ) ) {
172
- return $urls;
173
- }
174
-
175
- $base_url = dirname( $guid );
176
-
177
- if ( ! empty( $metadata['file'] ) ) {
178
- $urls['file'] = trailingslashit( $base_url ) . wp_basename( $metadata['file'] );
179
- }
180
-
181
- if ( ! empty( $metadata['sizes'] ) ) {
182
- foreach ( $metadata['sizes'] as $key => $value ) {
183
- $urls[ $key ] = trailingslashit( $base_url ) . wp_basename( $value['file'] );
184
- }
185
- }
186
-
187
- return $urls;
188
- }
189
-
190
- /**
191
- * Ensure new search URLs cover known sizes for old attachment.
192
- * Falls back to full URL if size not covered (srcset or width/height attributes should compensate).
193
- *
194
- * @param array $old
195
- * @param array $new
196
- *
197
- * @return array
198
- */
199
- function emr_normalize_file_urls( $old, $new ) {
200
- $result = array();
201
-
202
- if ( empty( $new['guid'] ) ) {
203
- return $result;
204
- }
205
-
206
- $guid = $new['guid'];
207
-
208
- foreach ( $old as $key => $value ) {
209
- $result[ $key ] = empty( $new[ $key ] ) ? $guid : $new[ $key ];
210
- }
211
-
212
- return $result;
213
- }
214
 
215
  // Starts processing.
216
  $uihelper = new UIHelper();
21
  $table_name = $wpdb->prefix . "posts";
22
  $postmeta_table_name = $wpdb->prefix . "postmeta";
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  // Starts processing.
26
  $uihelper = new UIHelper();