Enable Media Replace - Version 3.1.1

Version Description

  • Fixed bug introduced in an earlier version, preventing the updating of URLs on pages/posts if the link did not contain the domain name
Download this release

Release Info

Developer MungoBBQ
Plugin Icon 128x128 Enable Media Replace
Version 3.1.1
Comparing to
See all releases

Code changes from version 3.1 to 3.1.1

assets/banner-1544x500.jpg DELETED
Binary file
assets/banner-772x250.jpg DELETED
Binary file
assets/icon-128x128.png DELETED
Binary file
assets/icon-256x256.png DELETED
Binary file
assets/screenshot-1.png DELETED
Binary file
assets/screenshot-2.png DELETED
Binary file
assets/screenshot-3.png DELETED
Binary file
assets/screenshot-4.png DELETED
Binary file
enable-media-replace.php CHANGED
@@ -3,7 +3,7 @@
3
Plugin Name: Enable Media Replace
4
Plugin URI: http://www.mansjonasson.se/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.1
7
Author: Måns Jonasson
8
Author URI: http://www.mansjonasson.se
9
3
Plugin Name: Enable Media Replace
4
Plugin URI: http://www.mansjonasson.se/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.1.1
7
Author: Måns Jonasson
8
Author URI: http://www.mansjonasson.se
9
readme.txt CHANGED
@@ -37,6 +37,9 @@ If you want more control over the format used to display the time, you can use t
37
38
== Changelog ==
39
40
= 3.1 =
41
* Got rid of some pesky old code, and added some better filtering options, thanks to GitHub users speerface, aaemnnosttv, and ururk
42
* Brand new, shiny code to replace other image sizes in embeded media, thanks to GitHub user ianmjones!
37
38
== Changelog ==
39
40
+ = 3.1.1 =
41
+ * Fixed bug introduced in an earlier version, preventing the updating of URLs on pages/posts if the link did not contain the domain name
42
+
43
= 3.1 =
44
* Got rid of some pesky old code, and added some better filtering options, thanks to GitHub users speerface, aaemnnosttv, and ururk
45
* Brand new, shiny code to replace other image sizes in embeded media, thanks to GitHub user ianmjones!
upload.php CHANGED
@@ -7,7 +7,13 @@ global $wpdb;
7
$table_name = $wpdb->prefix . "posts";
8
$postmeta_table_name = $wpdb->prefix . "postmeta";
9
10
- function emr_delete_current_files($current_file) {
11
// Delete old file
12
13
// Find path of current file
@@ -35,7 +41,10 @@ function emr_delete_current_files($current_file) {
35
if (in_array($suffix, $imgAr)) {
36
// It's a png/gif/jpg based on file name
37
// Get thumbnail filenames from metadata
38
- $metadata = wp_get_attachment_metadata($_POST["ID"]);
39
if (is_array($metadata)) { // Added fix for error messages when there is no metadata (but WHY would there not be? I don't know…)
40
foreach($metadata["sizes"] AS $thissize) {
41
// Get all filenames and do an unlink() on each one;
@@ -58,6 +67,131 @@ function emr_delete_current_files($current_file) {
58
59
}
60
61
62
// Get old guid and filetype from DB
63
$sql = "SELECT guid, post_mime_type FROM $table_name WHERE ID = '" . (int) $_POST["ID"] . "'";
@@ -71,6 +205,7 @@ $current_file = get_attached_file((int) $_POST["ID"], apply_filters( 'emr_unfilt
71
$current_path = substr($current_file, 0, (strrpos($current_file, "/")));
72
$current_file = str_replace("//", "/", $current_file);
73
$current_filename = basename($current_file);
74
75
$replace_type = $_POST["replace_type"];
76
// We have two types: replace / replace_and_search
@@ -96,7 +231,7 @@ if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
96
// Drop-in replace and we don't even care if you uploaded something that is the wrong file-type.
97
// That's your own fault, because we warned you!
98
99
- emr_delete_current_files($current_file);
100
101
// Move new file to old location/name
102
move_uploaded_file($_FILES["userfile"]["tmp_name"], $current_file);
@@ -112,10 +247,11 @@ if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
112
} elseif ( 'replace_and_search' == $replace_type && apply_filters( 'emr_enable_replace_and_search', true ) ) {
113
// Replace file, replace file name, update meta data, replace links pointing to old file name
114
115
- emr_delete_current_files($current_file);
116
117
// Massage new filename to adhere to WordPress standards
118
- $new_filename= wp_unique_filename( $current_path, $new_filename );
119
120
// Move new file to old location, new name
121
$new_file = $current_path . "/" . $new_filename;
@@ -155,50 +291,70 @@ if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
155
$wpdb->query($sql);
156
157
// Make thumb and/or update metadata
158
- wp_update_attachment_metadata( (int) $_POST["ID"], wp_generate_attachment_metadata( (int) $_POST["ID"], $new_file) );
159
160
// Search-and-replace filename in post database
161
$sql = $wpdb->prepare(
162
- "SELECT ID, post_content FROM $table_name WHERE post_content LIKE %s;",
163
- '%' . $current_guid . '%'
164
);
165
-
166
- $rs = $wpdb->get_results($sql, ARRAY_A);
167
168
- foreach($rs AS $rows) {
169
-
170
- // replace old guid with new guid
171
- $post_content = $rows["post_content"];
172
- $post_content = addslashes(str_replace($current_guid, $new_guid, $post_content));
173
174
- $sql = $wpdb->prepare(
175
- "UPDATE $table_name SET post_content = '$post_content' WHERE ID = %d;",
176
- $rows["ID"]
177
- );
178
179
- $wpdb->query($sql);
180
}
181
-
182
- // Trigger possible updates on CDN and other plugins
183
- update_attached_file( (int) $_POST["ID"], $new_file);
184
185
}
186
187
- $returnurl = get_bloginfo("wpurl") . "/wp-admin/post.php?post={$_POST["ID"]}&action=edit&message=1";
188
189
// Execute hook actions - thanks rubious for the suggestion!
190
- if (isset($new_guid)) { do_action("enable-media-replace-upload-done", ($new_guid ? $new_guid : $current_guid)); }
191
192
} else {
193
//TODO Better error handling when no file is selected.
194
//For now just go back to media management
195
- $returnurl = get_bloginfo("wpurl") . "/wp-admin/upload.php";
196
}
197
198
if (FORCE_SSL_ADMIN) {
199
$returnurl = str_replace("http:", "https:", $returnurl);
200
}
201
202
//save redirection
203
wp_redirect($returnurl);
204
?>
7
$table_name = $wpdb->prefix . "posts";
8
$postmeta_table_name = $wpdb->prefix . "postmeta";
9
10
+ /**
11
+ * Delete a media file and its thumbnails.
12
+ *
13
+ * @param string $current_file
14
+ * @param array|null $metadta
15
+ */
16
+ function emr_delete_current_files( $current_file, $metadta = null ) {
17
// Delete old file
18
19
// Find path of current file
41
if (in_array($suffix, $imgAr)) {
42
// It's a png/gif/jpg based on file name
43
// Get thumbnail filenames from metadata
44
+ if ( empty( $metadata ) ) {
45
+ $metadata = wp_get_attachment_metadata( $_POST["ID"] );
46
+ }
47
+
48
if (is_array($metadata)) { // Added fix for error messages when there is no metadata (but WHY would there not be? I don't know…)
49
foreach($metadata["sizes"] AS $thissize) {
50
// Get all filenames and do an unlink() on each one;
67
68
}
69
70
+ /**
71
+ * Maybe remove query string from URL.
72
+ *
73
+ * @param string $url
74
+ *
75
+ * @return string
76
+ */
77
+ function emr_maybe_remove_query_string( $url ) {
78
+ $parts = explode( '?', $url );
79
+
80
+ return reset( $parts );
81
+ }
82
+
83
+ /**
84
+ * Remove scheme from URL.
85
+ *
86
+ * @param string $url
87
+ *
88
+ * @return string
89
+ */
90
+ function emr_remove_scheme( $url ) {
91
+ return preg_replace( '/^(?:http|https):/', '', $url );
92
+ }
93
+
94
+ /**
95
+ * Remove size from filename (image[-100x100].jpeg).
96
+ *
97
+ * @param string $url
98
+ * @param bool $remove_extension
99
+ *
100
+ * @return string
101
+ */
102
+ function emr_remove_size_from_filename( $url, $remove_extension = false ) {
103
+ $url = preg_replace( '/^(\S+)-[0-9]{1,4}x[0-9]{1,4}(\.[a-zA-Z0-9\.]{2,})?/', '$1$2', $url );
104
+
105
+ if ( $remove_extension ) {
106
+ $ext = pathinfo( $url, PATHINFO_EXTENSION );
107
+ $url = str_replace( ".$ext", '', $url );
108
+ }
109
+
110
+ return $url;
111
+ }
112
+
113
+ /**
114
+ * Strip an image URL down to bare minimum for matching.
115
+ *
116
+ * @param string $url
117
+ *
118
+ * @return string
119
+ */
120
+ function emr_get_match_url($url) {
121
+ $url = emr_remove_scheme($url);
122
+ $url = emr_maybe_remove_query_string($url);
123
+ $url = emr_remove_size_from_filename($url, true);
124
+ $url = emr_remove_domain_from_filename($url);
125
+
126
+ return $url;
127
+ }
128
+
129
+
130
+ function emr_remove_domain_from_filename($url) {
131
+ // Holding place for possible future function
132
+ $url = str_replace(emr_remove_scheme(get_bloginfo('url')), '', $url);
133
+ return $url;
134
+ }
135
+
136
+ /**
137
+ * Build an array of search or replace URLs for given attachment GUID and its metadata.
138
+ *
139
+ * @param string $guid
140
+ * @param array $metadata
141
+ *
142
+ * @return array
143
+ */
144
+ function emr_get_file_urls( $guid, $metadata ) {
145
+ $urls = array();
146
+
147
+ $guid = emr_remove_scheme( $guid );
148
+ $guid= emr_remove_domain_from_filename($guid);
149
+
150
+ $urls['guid'] = $guid;
151
+
152
+ if ( empty( $metadata ) ) {
153
+ return $urls;
154
+ }
155
+
156
+ $base_url = dirname( $guid );
157
+
158
+ if ( ! empty( $metadata['file'] ) ) {
159
+ $urls['file'] = trailingslashit( $base_url ) . wp_basename( $metadata['file'] );
160
+ }
161
+
162
+ if ( ! empty( $metadata['sizes'] ) ) {
163
+ foreach ( $metadata['sizes'] as $key => $value ) {
164
+ $urls[ $key ] = trailingslashit( $base_url ) . wp_basename( $value['file'] );
165
+ }
166
+ }
167
+
168
+ return $urls;
169
+ }
170
+
171
+ /**
172
+ * Ensure new search URLs cover known sizes for old attachment.
173
+ * Falls back to full URL if size not covered (srcset or width/height attributes should compensate).
174
+ *
175
+ * @param array $old
176
+ * @param array $new
177
+ *
178
+ * @return array
179
+ */
180
+ function emr_normalize_file_urls( $old, $new ) {
181
+ $result = array();
182
+
183
+ if ( empty( $new['guid'] ) ) {
184
+ return $result;
185
+ }
186
+
187
+ $guid = $new['guid'];
188
+
189
+ foreach ( $old as $key => $value ) {
190
+ $result[ $key ] = empty( $new[ $key ] ) ? $guid : $new[ $key ];
191
+ }
192
+
193
+ return $result;
194
+ }
195
196
// Get old guid and filetype from DB
197
$sql = "SELECT guid, post_mime_type FROM $table_name WHERE ID = '" . (int) $_POST["ID"] . "'";
205
$current_path = substr($current_file, 0, (strrpos($current_file, "/")));
206
$current_file = str_replace("//", "/", $current_file);
207
$current_filename = basename($current_file);
208
+ $current_metadata = wp_get_attachment_metadata( $_POST["ID"] );
209
210
$replace_type = $_POST["replace_type"];
211
// We have two types: replace / replace_and_search
231
// Drop-in replace and we don't even care if you uploaded something that is the wrong file-type.
232
// That's your own fault, because we warned you!
233
234
+ emr_delete_current_files( $current_file, $current_metadata );
235
236
// Move new file to old location/name
237
move_uploaded_file($_FILES["userfile"]["tmp_name"], $current_file);
247
} elseif ( 'replace_and_search' == $replace_type && apply_filters( 'emr_enable_replace_and_search', true ) ) {
248
// Replace file, replace file name, update meta data, replace links pointing to old file name
249
250
+ emr_delete_current_files( $current_file, $current_metadata );
251
252
// Massage new filename to adhere to WordPress standards
253
+ $new_filename = wp_unique_filename( $current_path, $new_filename );
254
+ $new_filename = apply_filters( 'emr_unique_filename', $new_filename, $current_path, (int) $_POST['ID'] );
255
256
// Move new file to old location, new name
257
$new_file = $current_path . "/" . $new_filename;
291
$wpdb->query($sql);
292
293
// Make thumb and/or update metadata
294
+ $new_metadata = wp_generate_attachment_metadata( (int) $_POST["ID"], $new_file );
295
+ wp_update_attachment_metadata( (int) $_POST["ID"], $new_metadata );
296
297
// Search-and-replace filename in post database
298
+ $current_base_url = emr_get_match_url( $current_guid );
299
+
300
$sql = $wpdb->prepare(
301
+ "SELECT ID, post_content FROM $table_name WHERE post_status = 'publish' AND post_content LIKE %s;",
302
+ '%' . $current_base_url . '%'
303
);
304
305
+
306
+ $rs = $wpdb->get_results( $sql, ARRAY_A );
307
308
+ $number_of_updates = 0;
309
+
310
+
311
+ if ( ! empty( $rs ) ) {
312
+ $search_urls = emr_get_file_urls( $current_guid, $current_metadata );
313
+ $replace_urls = emr_get_file_urls( $new_guid, $new_metadata );
314
+ $replace_urls = emr_normalize_file_urls( $search_urls, $replace_urls );
315
316
+
317
+ foreach ( $rs AS $rows ) {
318
+
319
+ $number_of_updates = $number_of_updates + 1;
320
+
321
+ // replace old URLs with new URLs.
322
+ $post_content = $rows["post_content"];
323
+ $post_content = addslashes( str_replace( $search_urls, $replace_urls, $post_content ) );
324
+
325
+ $sql = $wpdb->prepare(
326
+ "UPDATE $table_name SET post_content = '$post_content' WHERE ID = %d;",
327
+ $rows["ID"]
328
+ );
329
+
330
+ $wpdb->query( $sql );
331
+ }
332
}
333
334
+ // Trigger possible updates on CDN and other plugins
335
+ update_attached_file( (int) $_POST["ID"], $new_file );
336
}
337
+
338
+ #echo "Updated: " . $number_of_updates;
339
340
+ $returnurl = admin_url("/post.php?post={$_POST["ID"]}&action=edit&message=1");
341
342
// Execute hook actions - thanks rubious for the suggestion!
343
+ if (isset($new_guid)) { do_action("enable-media-replace-upload-done", $new_guid, $current_guid); }
344
345
} else {
346
//TODO Better error handling when no file is selected.
347
//For now just go back to media management
348
+ $returnurl = admin_url("/wp-admin/upload.php");
349
}
350
351
if (FORCE_SSL_ADMIN) {
352
$returnurl = str_replace("http:", "https:", $returnurl);
353
}
354
355
+ // Allow developers to override $returnurl
356
+ $returnurl = apply_filters('emr_returnurl', $returnurl);
357
+
358
//save redirection
359
wp_redirect($returnurl);
360
?>