Enable Media Replace - Version 3.2.6

Version Description

  • no more 404 error if no image was selected when trying to replace it
  • added preview so you can check the image being replaced and also the image that's being replaced with
  • .dat files can be replaced (functionality accidetanly removed in the previous version)
  • added compatibility with S3 upload plugin
  • when an image is replaced the date is also updated
Download this release

Release Info

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

Code changes from version 3.2.5 to 3.2.6

Files changed (4) hide show
  1. enable-media-replace.php +19 -10
  2. popup.php +49 -4
  3. readme.txt +19 -8
  4. upload.php +32 -9
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.2.5
7
Author: ShortPixel
8
Author URI: https://shortpixel.com
9
@@ -22,22 +22,37 @@ http://www.gnu.org/licenses/gpl.html
22
* @subpackage enable-media-replace
23
*
24
*/
25
add_action('admin_init', 'enable_media_replace_init');
26
add_action('admin_menu', 'emr_menu');
27
add_filter('attachment_fields_to_edit', 'enable_media_replace', 10, 2);
28
add_filter('media_row_actions', 'add_media_action', 10, 2);
29
30
add_action('admin_notices', 'emr_display_notices');
31
add_action('network_admin_notices', 'emr_display_network_notices');
32
add_action('wp_ajax_emr_dismiss_notices', 'emr_dismiss_notices');
33
- //add_action('wp_ajax_emr_install_plugin', 'emr_install_plugin');
34
- //add_action( 'admin_enqueue_scripts', 'emr_add_js' );
35
36
add_shortcode('file_modified', 'emr_get_modified_date');
37
38
if(!defined("SHORTPIXEL_AFFILIATE_CODE")) {
39
define("SHORTPIXEL_AFFILIATE_CODE", 'VKG6LYN28044');
40
}
41
/**
42
* Register this file in WordPress so we can call it with a ?page= GET var.
43
* To suppress it in the menu we give it an empty menu title.
@@ -178,7 +193,7 @@ function emr_display_notices() {
178
179
if(current_user_can( 'activate_plugins' ) && !get_option( 'emr_news') && !is_plugin_active('shortpixel-image-optimiser/wp-shortpixel.php')
180
&& ($crtScreen->base == "upload" || $crtScreen->base == "plugins")
181
- //for network installed plugins, don't display the message on subsites.
182
&& !(function_exists('is_multisite') && is_multisite() && is_plugin_active_for_network('enable-media-replace/enable-media-replace.php') && !is_main_site()))
183
{
184
require_once( str_replace("enable-media-replace.php", "notice.php", __FILE__) );
@@ -195,9 +210,3 @@ function emr_dismiss_notices() {
195
update_option( 'emr_news', true);
196
exit(json_encode(array("Status" => 0)));
197
}
198
-
199
- /*function emr_add_js($hook) {
200
- if("media_page_enable-media-replace/enable-media-replace" == $hook) {
201
- wp_enqueue_script('emr-plugin-install', plugins_url('/js/plugin-install.js',__FILE__), array( 'jquery', 'updates' ), '1.0.0', 'all' );
202
- }
203
- }*/
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.2.6
7
Author: ShortPixel
8
Author URI: https://shortpixel.com
9
22
* @subpackage enable-media-replace
23
*
24
*/
25
+
26
+ if(!defined("S3_UPLOADS_AUTOENABLE")) {
27
+ define('S3_UPLOADS_AUTOENABLE', true);
28
+ }
29
+
30
add_action('admin_init', 'enable_media_replace_init');
31
add_action('admin_menu', 'emr_menu');
32
add_filter('attachment_fields_to_edit', 'enable_media_replace', 10, 2);
33
add_filter('media_row_actions', 'add_media_action', 10, 2);
34
+ add_filter('upload_mimes', 'dat_mime_types', 1, 1);
35
36
add_action('admin_notices', 'emr_display_notices');
37
add_action('network_admin_notices', 'emr_display_network_notices');
38
add_action('wp_ajax_emr_dismiss_notices', 'emr_dismiss_notices');
39
40
add_shortcode('file_modified', 'emr_get_modified_date');
41
42
if(!defined("SHORTPIXEL_AFFILIATE_CODE")) {
43
define("SHORTPIXEL_AFFILIATE_CODE", 'VKG6LYN28044');
44
}
45
+
46
+ /**
47
+ * @param array $mime_types
48
+ * @return array
49
+ */
50
+ function dat_mime_types($mime_types) {
51
+ $mime_types['dat'] = 'text/plain'; // Adding .dat extension
52
+
53
+ return $mime_types;
54
+ }
55
+
56
/**
57
* Register this file in WordPress so we can call it with a ?page= GET var.
58
* To suppress it in the menu we give it an empty menu title.
193
194
if(current_user_can( 'activate_plugins' ) && !get_option( 'emr_news') && !is_plugin_active('shortpixel-image-optimiser/wp-shortpixel.php')
195
&& ($crtScreen->base == "upload" || $crtScreen->base == "plugins")
196
+ //for network installed plugins, don't display the message on subsites.
197
&& !(function_exists('is_multisite') && is_multisite() && is_plugin_active_for_network('enable-media-replace/enable-media-replace.php') && !is_main_site()))
198
{
199
require_once( str_replace("enable-media-replace.php", "notice.php", __FILE__) );
210
update_option( 'emr_news', true);
211
exit(json_encode(array("Status" => 0)));
212
}
popup.php CHANGED
@@ -21,6 +21,7 @@ $sql = "SELECT guid, post_mime_type FROM $table_name WHERE ID = " . (int) $_GET[
21
22
list($current_filename, $current_filetype) = $wpdb->get_row($sql, ARRAY_N);
23
24
$current_filename = substr($current_filename, (strrpos($current_filename, "/") + 1));
25
26
@@ -84,25 +85,69 @@ $current_filename = substr($current_filename, (strrpos($current_filename, "/") +
84
85
<p><?php echo __("Choose a file to upload from your computer", "enable-media-replace"); ?></p>
86
87
- <input type="file" name="userfile" />
88
89
<?php do_action( 'emr_before_replace_type_options' ); ?>
90
91
<?php if ( apply_filters( 'emr_display_replace_type_options', true ) ) : ?>
92
<p><?php echo __("Select media replacement type:", "enable-media-replace"); ?></p>
93
94
<label for="replace_type_1"><input CHECKED id="replace_type_1" type="radio" name="replace_type" value="replace"> <?php echo __("Just replace the file", "enable-media-replace"); ?></label>
95
<p class="howto"><?php printf( __("Note: This option requires you to upload a file of the same type (%s) as the one you are replacing. The name of the attachment will stay the same (%s) no matter what the file you upload is called.", "enable-media-replace"), $current_filetype, $current_filename ); ?></p>
96
97
<?php if ( apply_filters( 'emr_enable_replace_and_search', true ) ) : ?>
98
- <label for="replace_type_2"><input id="replace_type_2" type="radio" name="replace_type" value="replace_and_search"> <?php echo __("Replace the file, use new file name and update all links", "enable-media-replace"); ?></label>
99
<p class="howto"><?php printf( __("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"), $current_filename ); ?></p>
100
<p class="howto"><?php echo __("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>
101
<?php endif; ?>
102
<?php else : ?>
103
- <input type="hidden" name="replace_type" value="replace" />
104
<?php endif; ?>
105
- <input type="submit" class="button button-primary" value="<?php echo __("Upload", "enable-media-replace"); ?>" />
106
<a href="#" class="button" onclick="history.back();"><?php echo __("Cancel", "enable-media-replace"); ?></a>
107
</form>
108
</div>
21
22
list($current_filename, $current_filetype) = $wpdb->get_row($sql, ARRAY_N);
23
24
+ $current_fullpath = $current_filename;
25
$current_filename = substr($current_filename, (strrpos($current_filename, "/") + 1));
26
27
85
86
<p><?php echo __("Choose a file to upload from your computer", "enable-media-replace"); ?></p>
87
88
+ <input type="file" name="userfile" id="userfile" onchange="imageHandle(event);" />
89
+ <div>
90
+ <img src="<?= $current_fullpath ?>" width="150px" height="150px" style="object-fit: cover"/>
91
+ <img id="previewImage" src="http://via.placeholder.com/150x150" width="150px" height="150px"/>
92
+ </div>
93
94
<?php do_action( 'emr_before_replace_type_options' ); ?>
95
96
+ <?php $s3pluginExist = class_exists('S3_Uploads'); ?>
97
<?php if ( apply_filters( 'emr_display_replace_type_options', true ) ) : ?>
98
+ <?php if ( ! $s3pluginExist) : ?>
99
<p><?php echo __("Select media replacement type:", "enable-media-replace"); ?></p>
100
101
<label for="replace_type_1"><input CHECKED id="replace_type_1" type="radio" name="replace_type" value="replace"> <?php echo __("Just replace the file", "enable-media-replace"); ?></label>
102
<p class="howto"><?php printf( __("Note: This option requires you to upload a file of the same type (%s) as the one you are replacing. The name of the attachment will stay the same (%s) no matter what the file you upload is called.", "enable-media-replace"), $current_filetype, $current_filename ); ?></p>
103
104
+ <?php endif; ?>
105
<?php if ( apply_filters( 'emr_enable_replace_and_search', true ) ) : ?>
106
+ <label for="replace_type_2"><input <?= $s3pluginExist ? 'CHECKED' : '' ?> id="replace_type_2" type="radio" name="replace_type" value="replace_and_search"> <?php echo __("Replace the file, use new file name and update all links", "enable-media-replace"); ?></label>
107
<p class="howto"><?php printf( __("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"), $current_filename ); ?></p>
108
<p class="howto"><?php echo __("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>
109
<?php endif; ?>
110
<?php else : ?>
111
+ <?php if ( ! $s3pluginExist) : ?>
112
+ <input type="hidden" name="replace_type" value="replace" />
113
+ <?php else : ?>
114
+ <input type="hidden" name="replace_type" value="replace_and_search" />
115
+ <?php endif; ?>
116
<?php endif; ?>
117
+ <input id="submit" type="submit" class="button button-primary" disabled="disabled" value="<?php echo __("Upload", "enable-media-replace"); ?>" />
118
<a href="#" class="button" onclick="history.back();"><?php echo __("Cancel", "enable-media-replace"); ?></a>
119
</form>
120
</div>
121
+ <script>
122
+ function imageHandle(event) {
123
+ var file = document.getElementById("userfile");
124
+ var submit = document.getElementById("submit");
125
+ var preview = document.getElementById("previewImage");
126
+
127
+ appendPreview(file, preview, event);
128
+ enableSubmitButton(file, submit);
129
+ }
130
+
131
+ function appendPreview(fileSource, preview, event) {
132
+ if (fileSource.value) {
133
+ var file = fileSource.files[0];
134
+ if (file.type.match("image/*")) {
135
+ preview.setAttribute("src", window.URL.createObjectURL(file));
136
+ preview.setAttribute("style", "object-fit: cover");
137
+ } else {
138
+ preview.setAttribute("src", "https://dummyimage.com/150x150/ccc/969696.gif&text=File");
139
+ preview.removeAttribute("style");
140
+ }
141
+ } else {
142
+ preview.setAttribute("src", "http://via.placeholder.com/150x150");
143
+ }
144
+ }
145
+ function enableSubmitButton(file, submit)
146
+ {
147
+ if (file.value) {
148
+ submit.removeAttribute("disabled");
149
+ } else {
150
+ submit.setAttribute("disabled", true);
151
+ }
152
+ }
153
+ </script>
readme.txt CHANGED
@@ -1,12 +1,12 @@
1
=== Enable Media Replace ===
2
Contributors: ShortPixel
3
Donate link: https://www.paypal.me/resizeImage
4
- Tags: admin, replace, attachment, media, files, replace image, replace jpg, change media, replace media
5
Requires at least: 4.0
6
- Tested up to: 4.9.4
7
Stable tag: trunk
8
9
- Easily replace any attached image/file by simply uploading a new file in the Media Library edit view.
10
11
== Description ==
12
@@ -26,10 +26,7 @@ Now you'll be able to replace any uploaded file from the media "edit" view, wher
26
1. Just replace the file. This option requires you to upload a file of the same type as the one you are replacing. The name of the attachment will stay the same no matter what the file you upload is called.
27
1. Replace the file, use new file name and update all links. 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 will be updated to point to the new file name.
28
29
- This plugin is very powerful and a must-have for any larger sites built with WordPress.
30
-
31
- Enable Media Replace is fully compatible with the <a href="https://wordpress.org/plugins/shortpixel-image-optimiser/" target="_blank">ShortPixel Image Optimizer plugin</a>, which, once enabled, will optimize the images you replace automatically.
32
- It's also compatible with the <a href="https://wordpress.org/plugins/resize-image-after-upload/" target="_blank">Resize Image After Upload plugin</a> which can do a sanity check on the size in pixels of the images uploaded.
33
34
#### Display file modification time
35
@@ -40,8 +37,22 @@ So `[file_modified id=870]` would display the last time the file with ID 870 was
40
41
If you want more control over the format used to display the time, you can use the format option, so `[file_modified id=870 format=Y-m-d]` would display the file modification date but not the time. The format string uses [standard PHP date() formatting tags](http://php.net/manual/en/function.date.php).
42
43
== Changelog ==
44
45
= 3.2.5 =
46
* remove the leftover setcookie and the plugins recommendations.
47
@@ -246,4 +257,4 @@ Second, if the file really looks unchanged, make sure WordPress has write permis
246
247
== Wishlist / Coming attractons ==
248
249
- Do you have suggestions? Feel free to contact contact ShortPixel <a href="https://shortpixel.com/contact" target="_blank">here</a>
1
=== Enable Media Replace ===
2
Contributors: ShortPixel
3
Donate link: https://www.paypal.me/resizeImage
4
+ Tags: replace, attachment, media, files, replace image, replace jpg, change media, replace media, image, file
5
Requires at least: 4.0
6
+ Tested up to: 4.9
7
Stable tag: trunk
8
9
+ Easily replace any attached image/file by simply uploading a new file in the Media Library edit view - a real time saver!
10
11
== Description ==
12
26
1. Just replace the file. This option requires you to upload a file of the same type as the one you are replacing. The name of the attachment will stay the same no matter what the file you upload is called.
27
1. Replace the file, use new file name and update all links. 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 will be updated to point to the new file name.
28
29
+ This plugin is very powerful and a must-have for any larger sites built with WordPress. It now also comes with preview of the replaced image!
30
31
#### Display file modification time
32
37
38
If you want more control over the format used to display the time, you can use the format option, so `[file_modified id=870 format=Y-m-d]` would display the file modification date but not the time. The format string uses [standard PHP date() formatting tags](http://php.net/manual/en/function.date.php).
39
40
+
41
+ #### Compatible and recommended Plugins =
42
+
43
+ * [ShortPixel Image Optimization](https://wordpress.org/plugins/shortpixel-image-optimiser/) - Enable Media Replace is fully compatible with this plugin. Once enabled, ShortPixel will automatically optimize the images you replace using Enable Media Replace.
44
+ * [Resize Image After Upload plugin](https://wordpress.org/plugins/resize-image-after-upload/) - automatically resize images upon upload to save traffic & disk space. Good for SEO and compatible with EMR.
45
+ * [Regenerate Thumbnails Advanced](https://wordpress.org/plugins/regenerate-thumbnails-advanced/) - Fast, free and simple to use plugin to regenerate the thumbnails for your site after changing a theme (for example). Supported & maintained by [ShortPixel](https://ShortPixel.com)
46
+
47
== Changelog ==
48
49
+ = 3.2.6 =
50
+ * no more 404 error if no image was selected when trying to replace it
51
+ * added preview so you can check the image being replaced and also the image that's being replaced with
52
+ * .dat files can be replaced (functionality accidetanly removed in the previous version)
53
+ * added compatibility with S3 upload plugin
54
+ * when an image is replaced the date is also updated
55
+
56
= 3.2.5 =
57
* remove the leftover setcookie and the plugins recommendations.
58
257
258
== Wishlist / Coming attractons ==
259
260
+ Do you have suggestions? Feel free to contact ShortPixel <a href="https://shortpixel.com/contact" target="_blank">here</a>
upload.php CHANGED
@@ -37,13 +37,20 @@ function emr_delete_current_files( $current_file, $metadta = null ) {
37
// Delete old resized versions if this was an image
38
$suffix = substr($current_file, (strlen($current_file)-4));
39
$prefix = substr($current_file, 0, (strlen($current_file)-4));
40
$imgAr = array(".png", ".gif", ".jpg");
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) {
@@ -64,7 +71,6 @@ function emr_delete_current_files( $current_file, $metadta = null ) {
64
//$mask = $prefix . "-*x*" . $suffix;
65
//array_map( "unlink", glob( $mask ) );
66
}
67
-
68
}
69
70
/**
@@ -194,8 +200,9 @@ function emr_normalize_file_urls( $old, $new ) {
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"] . "'";
198
- list($current_filename, $current_filetype) = $wpdb->get_row($sql, ARRAY_N);
199
200
// Massage a bunch of vars
201
$current_guid = $current_filename;
@@ -205,7 +212,7 @@ $ID = (int) $_POST["ID"];
205
206
$current_file = get_attached_file($ID, apply_filters( 'emr_unfiltered_get_attached_file', true ));
207
$current_path = substr($current_file, 0, (strrpos($current_file, "/")));
208
- $current_file = str_replace("//", "/", $current_file);
209
$current_filename = basename($current_file);
210
$current_metadata = wp_get_attachment_metadata( $_POST["ID"] );
211
@@ -238,12 +245,26 @@ if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
238
239
emr_delete_current_files( $current_file, $current_metadata );
240
241
- // Move new file to old location/name
242
- move_uploaded_file($_FILES["userfile"]["tmp_name"], $current_file);
243
244
// Chmod new file to original file permissions
245
@chmod($current_file, $original_file_perms);
246
247
//call upload action to give a chance to plugins like Resize Image After Upload to handle the new image
248
do_action('wp_handle_upload', array('file' => $current_file, 'url' => wp_get_attachment_url($ID), 'type' => $new_filetype));
249
@@ -278,9 +299,11 @@ if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
278
//call upload action to give a chance to plugins like ShortPixel to handle the new image
279
//do_action('wp_handle_upload', array('file' => $new_file, 'url' => $new_guid, 'type' => $new_filetype));
280
281
- // Update database file name
282
$sql = $wpdb->prepare(
283
- "UPDATE $table_name SET post_title = '$new_filetitle', post_name = '$new_filetitle', guid = '$new_guid', post_mime_type = '$new_filetype' WHERE ID = %d;",
284
$ID
285
);
286
$wpdb->query($sql);
37
// Delete old resized versions if this was an image
38
$suffix = substr($current_file, (strlen($current_file)-4));
39
$prefix = substr($current_file, 0, (strlen($current_file)-4));
40
+
41
+ if (strtolower($suffix) === ".pdf") {
42
+ $prefix .= "-pdf";
43
+ $suffix = ".jpg";
44
+ }
45
+
46
$imgAr = array(".png", ".gif", ".jpg");
47
+ if (in_array($suffix, $imgAr)) {
48
// It's a png/gif/jpg based on file name
49
// Get thumbnail filenames from metadata
50
if ( empty( $metadata ) ) {
51
$metadata = wp_get_attachment_metadata( $_POST["ID"] );
52
}
53
+ // var_dump($metadata);exit;
54
55
if (is_array($metadata)) { // Added fix for error messages when there is no metadata (but WHY would there not be? I don't know…)
56
foreach($metadata["sizes"] AS $thissize) {
71
//$mask = $prefix . "-*x*" . $suffix;
72
//array_map( "unlink", glob( $mask ) );
73
}
74
}
75
76
/**
200
}
201
202
// Get old guid and filetype from DB
203
+ $sql = "SELECT post_mime_type FROM $table_name WHERE ID = '" . (int) $_POST["ID"] . "'";
204
+ list($current_filetype) = $wpdb->get_row($sql, ARRAY_N);
205
+ $current_filename = wp_get_attachment_url($_POST['ID']);
206
207
// Massage a bunch of vars
208
$current_guid = $current_filename;
212
213
$current_file = get_attached_file($ID, apply_filters( 'emr_unfiltered_get_attached_file', true ));
214
$current_path = substr($current_file, 0, (strrpos($current_file, "/")));
215
+ $current_file = preg_replace("|(?<!:)/{2,}|", "/", $current_file);
216
$current_filename = basename($current_file);
217
$current_metadata = wp_get_attachment_metadata( $_POST["ID"] );
218
245
246
emr_delete_current_files( $current_file, $current_metadata );
247
248
+ $new_filename = wp_unique_filename( $current_path, $current_filename );
249
+ $new_filename = apply_filters( 'emr_unique_filename', $current_filename, $current_path, $ID );
250
+
251
+ // Move new file to old location, new name
252
+ $new_file = $current_path . "/" . $current_filename;
253
+ move_uploaded_file($_FILES["userfile"]["tmp_name"], $new_file);
254
255
// Chmod new file to original file permissions
256
@chmod($current_file, $original_file_perms);
257
258
+ apply_filters( 'enable_media_replace_title', basename($new_file) ); // Thanks Jonas Lundman (http://wordpress.org/support/topic/add-filter-hook-suggestion-to)
259
+
260
+ // Update database file timestamp
261
+ $post_date = gmdate( 'Y-m-d H:i:s' );
262
+ $sql = $wpdb->prepare(
263
+ "UPDATE $table_name SET post_date = '$post_date', post_date_gmt = '$post_date' WHERE ID = %d;",
264
+ $ID
265
+ );
266
+ $wpdb->query($sql);
267
+
268
//call upload action to give a chance to plugins like Resize Image After Upload to handle the new image
269
do_action('wp_handle_upload', array('file' => $current_file, 'url' => wp_get_attachment_url($ID), 'type' => $new_filetype));
270
299
//call upload action to give a chance to plugins like ShortPixel to handle the new image
300
//do_action('wp_handle_upload', array('file' => $new_file, 'url' => $new_guid, 'type' => $new_filetype));
301
302
+ $post_date = gmdate( 'Y-m-d H:i:s' );
303
+
304
+ // Update database file name
305
$sql = $wpdb->prepare(
306
+ "UPDATE $table_name SET post_title = '$new_filetitle', post_name = '$new_filetitle', guid = '$new_guid', post_mime_type = '$new_filetype', post_date = '$post_date', post_date_gmt = '$post_date' WHERE ID = %d;",
307
$ID
308
);
309
$wpdb->query($sql);