Enable Media Replace - Version 3.2.2

Version Description

  • Added ShortPixel links and images, fixed the problem of ShortPixel recommendation not dismissing.
Download this release

Release Info

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

Code changes from version 3.2.1 to 3.2.2

Files changed (7) hide show
  1. enable-media-replace.php +65 -6
  2. img/sp.png +0 -0
  3. js/plugin-install.js +71 -0
  4. notice.php +4 -9
  5. popup.php +43 -1
  6. readme.txt +3 -0
  7. upload.php +34 -37
enable-media-replace.php CHANGED
@@ -3,9 +3,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.2.1
7
- Author: Måns Jonasson
8
- Author URI: http://www.mansjonasson.se
9
 
10
  Dual licensed under the MIT and GPL licenses:
11
  http://www.opensource.org/licenses/mit-license.php
@@ -16,20 +16,27 @@ http://www.gnu.org/licenses/gpl.html
16
  * Main Plugin file
17
  * Set action hooks and add shortcode
18
  *
19
- * @author Måns Jonasson <http://www.mansjonasson.se>
20
- * @copyright Måns Jonasson 13 sep 2010
21
  * @package wordpress
22
  * @subpackage enable-media-replace
23
  *
24
  */
25
-
26
  add_action('admin_init', 'enable_media_replace_init');
27
  add_action('admin_menu', 'emr_menu');
28
  add_filter('attachment_fields_to_edit', 'enable_media_replace', 10, 2);
29
  add_filter('media_row_actions', 'add_media_action', 10, 2);
30
 
 
 
 
 
 
31
  add_shortcode('file_modified', 'emr_get_modified_date');
32
 
 
 
 
33
  /**
34
  * Register this file in WordPress so we can call it with a ?page= GET var.
35
  * To suppress it in the menu we give it an empty menu title.
@@ -158,3 +165,55 @@ function ua_admin_date_replaced_media_on_edit_media_screen() {
158
  <?php
159
  }
160
  add_action( 'attachment_submitbox_misc_actions', 'ua_admin_date_replaced_media_on_edit_media_screen', 91 );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.2
7
+ Author: ShortPixel
8
+ Author URI: https://shortpixel.com
9
 
10
  Dual licensed under the MIT and GPL licenses:
11
  http://www.opensource.org/licenses/mit-license.php
16
  * Main Plugin file
17
  * Set action hooks and add shortcode
18
  *
19
+ * @author ShortPixel <https://shortpixel.com>
20
+ * @copyright ShortPixel 2018
21
  * @package wordpress
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('wp_ajax_emr_dismiss_notices', 'emr_dismiss_notices');
32
+ add_action('wp_ajax_emr_install_plugin', 'emr_install_plugin');
33
+ add_action( 'admin_enqueue_scripts', 'emr_add_js' );
34
+
35
  add_shortcode('file_modified', 'emr_get_modified_date');
36
 
37
+ if(!defined("SHORTPIXEL_AFFILIATE_CODE")) {
38
+ define("SHORTPIXEL_AFFILIATE_CODE", 'VKG6LYN28044');
39
+ }
40
  /**
41
  * Register this file in WordPress so we can call it with a ?page= GET var.
42
  * To suppress it in the menu we give it an empty menu title.
165
  <?php
166
  }
167
  add_action( 'attachment_submitbox_misc_actions', 'ua_admin_date_replaced_media_on_edit_media_screen', 91 );
168
+
169
+ /*----------------------------------------------------------------------------------------------------------
170
+ Display/dismiss admin notices if needed
171
+ -----------------------------------------------------------------------------------------------------------*/
172
+
173
+ function emr_display_notices() {
174
+ $current_screen = get_current_screen();
175
+
176
+ $crtScreen = function_exists("get_current_screen") ? get_current_screen() : (object)array("base" => false);
177
+ if(current_user_can( 'activate_plugins' ) && !get_option( 'emr_news') && !is_plugin_active('shortpixel-image-optimiser/wp-shortpixel.php')
178
+ && $crtScreen->base != "media_page_enable-media-replace/enable-media-replace") {
179
+ require_once( str_replace("enable-media-replace.php", "notice.php", __FILE__) );
180
+ }
181
+ }
182
+
183
+ function emr_dismiss_notices() {
184
+ update_option( 'emr_news', true);
185
+ exit(json_encode(array("Status" => 0)));
186
+ }
187
+
188
+ function emr_add_js($hook) {
189
+ if("media_page_enable-media-replace/enable-media-replace" == $hook) {
190
+ wp_enqueue_script('emr-plugin-install', plugins_url('/js/plugin-install.js',__FILE__), array( 'jquery', 'updates' ), '1.0.0', 'all' );
191
+ }
192
+ }
193
+
194
+ function emr_install_plugin() {
195
+ $slug = isset($_GET['slug']) ? trim($_GET['slug']) : null;
196
+ if($slug == 'shortpixel-image-optimiser') {
197
+ $response = json_encode(wp_remote_get('https://shortpixel.com/h/af/YXA6CHO28044', array(
198
+ 'method' => 'GET',
199
+ 'timeout' => 15,
200
+ 'redirection' => 3,
201
+ 'sslverify' => false,
202
+ 'httpversion' => '1.0',
203
+ 'blocking' => true,
204
+ 'headers' => array(),
205
+ 'body' => null,
206
+ 'cookies' => array()
207
+ )));
208
+
209
+ setcookie("AffiliateShortPixel", "YXA6CHO28044", time() + 2592000, parse_url(get_admin_url(),PHP_URL_PATH), parse_url(get_admin_url(),PHP_URL_HOST));//30 days
210
+ /*
211
+ $spCookie = isset($response['cookies']['AffiliateShortPixel']) ? $response['cookies']['AffiliateShortPixel'] : false;
212
+ if($spCookie) {
213
+ setcookie("AffiliateShortPixel", $spCookie, time() + 2592000, "/", parse_url(get_site_url(),PHP_URL_HOST));//30 days
214
+ }
215
+ */
216
+
217
+ exit(json_encode(array("Status" => 2)));
218
+ }
219
+ }
img/sp.png CHANGED
Binary file
js/plugin-install.js ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (function( wp, $ ) {
2
+ 'use strict';
3
+
4
+ if ( ! wp ) {
5
+ return;
6
+ }
7
+
8
+ function activatePlugin( url, el ,elInfo) {
9
+ var message = el.data( 'message' );
10
+ var link = el.data( 'add-link' );
11
+ el.removeClass('emr-plugin-button');
12
+ var linkName = el.data( 'add-link-name' );
13
+
14
+ $.ajax( {
15
+ async: true,
16
+ type: 'GET',
17
+ dataType: 'html',
18
+ url: url,
19
+ success: function() {
20
+ el.removeClass( 'emr-updating' );
21
+ el.text( message );
22
+ elInfo.after("<br><br><a href='" + link + "' target='_blank'>" + linkName + "</a>");
23
+ }
24
+ } );
25
+ }
26
+
27
+ function emrInstallPlugin(slug) {
28
+ var data = { action : 'emr_install_plugin', slug: slug};
29
+ jQuery.get(ajaxurl, data, function(response) {
30
+ console.log(response);
31
+ });
32
+ }
33
+
34
+ $( function() {
35
+ $( document ).on( 'click', '.emr-plugin-button', function( event ) {
36
+ var action = $( this ).data( 'action' ),
37
+ url = $( this ).attr( 'href' ),
38
+ slug = $( this ).data( 'slug' );
39
+
40
+ event.preventDefault();
41
+
42
+ if ( 'install' === action ) {
43
+
44
+ $( this ).addClass( 'emr-updating disabled' );
45
+
46
+ wp.updates.installPlugin( {
47
+ slug: slug
48
+ } );
49
+
50
+ } else if ( 'activate' === action ) {
51
+
52
+ $( this ).addClass( 'emr-updating disabled' );
53
+ emrInstallPlugin(slug);
54
+ activatePlugin( url, $( this ), $("#" + slug +"-info"));
55
+
56
+ }
57
+
58
+ } );
59
+
60
+ $( document ).on( 'wp-plugin-install-success', function( response, data ) {
61
+ var el = $( '.emr-plugin-button[data-slug="' + data.slug + '"]' );
62
+ emrInstallPlugin(data.slug);
63
+ activatePlugin( data.activateUrl, el , $("#" + data.slug + "-info"));
64
+ if(typeof event !== 'undefined') {
65
+ event.preventDefault();
66
+ }
67
+ } );
68
+
69
+ } );
70
+ })( window.wp, jQuery );
71
+
notice.php CHANGED
@@ -1,19 +1,14 @@
1
  <div class='notice' id='emr-news' style="padding-top: 7px">
2
  <div style="float:right;"><a href="javascript:emrDismissNews()" class="button" style="margin-top:10px;"><?php _e('Dismiss', 'enable-media-replace');?></a></div>
3
- <h3 style="margin:10px;"><?php _e('Enable Media Replace recommends optimizing your images with ShortPixel','enable-media-replace');?></h3>
4
- <p><strong>
5
- <a href="https://shortpixel.com/h/af/VKG6LYN28044" target="_blank">
6
- <?php _e( 'Test your website with ShortPixel for free to see how much you could gain by optimizing your images.', 'enable-media-replace' ); ?>
7
- </a>
8
- </strong></p>
9
- <a href="https://shortpixel.com/h/af/VKG6LYN28044" target="_blank" style="float: left;margin-right: 10px;">
10
  <img src="<?php echo plugins_url('img/sp.png', __FILE__ ); ?>" class="emr-sp"/>
11
  </a>
 
12
  <p style="margin-bottom:0px;">
13
- <?php _e( 'ShortPixel is an easy to use, comprehensive, stable and frequently updated image optimization plugin supported by the friendly team that created it. Using a powerful set of specially tuned algorithms, it squeezes the most of each image striking the best balance between image size and quality. Current images can be all optimized with a single click. Newly added images are automatically resized/rescaled and optimized on the fly, in the background.', 'enable-media-replace' ); ?>
14
  </p>
15
  <p style="text-align: right;margin-top: 0;">
16
- <a href="https://shortpixel.com/h/af/VKG6LYN28044" target="_blank">&gt;&gt; <?php _e( 'More info', 'enable-media-replace' ); ?></a>
17
  </p>
18
  </div>
19
  <script>
1
  <div class='notice' id='emr-news' style="padding-top: 7px">
2
  <div style="float:right;"><a href="javascript:emrDismissNews()" class="button" style="margin-top:10px;"><?php _e('Dismiss', 'enable-media-replace');?></a></div>
3
+ <a href="https://shortpixel.com/wp/af/VKG6LYN28044" target="_blank" style="float: left;margin-right: 10px;">
 
 
 
 
 
 
4
  <img src="<?php echo plugins_url('img/sp.png', __FILE__ ); ?>" class="emr-sp"/>
5
  </a>
6
+ <h3 style="margin:10px;"><?php _e('Enable Media Replace is now compatible with ShortPixel!','enable-media-replace');?></h3>
7
  <p style="margin-bottom:0px;">
8
+ <?php _e( '<a href="https://shortpixel.com/wp/af/VKG6LYN28044" target="_blank">ShortPixel</a> is an image optimization plugin and if you have it activated, upon replacing an image in Enable Media Replace, the image will be also automatically optimized.', 'enable-media-replace' ); ?>
9
  </p>
10
  <p style="text-align: right;margin-top: 0;">
11
+ <a href="https://shortpixel.com/wp/af/VKG6LYN28044" target="_blank">&gt;&gt; <?php _e( 'More info', 'enable-media-replace' ); ?></a>
12
  </p>
13
  </div>
14
  <script>
popup.php CHANGED
@@ -25,6 +25,17 @@ $current_filename = substr($current_filename, (strrpos($current_filename, "/") +
25
 
26
 
27
  ?>
 
 
 
 
 
 
 
 
 
 
 
28
  <div class="wrap">
29
  <h1><?php echo __("Replace Media Upload", "enable-media-replace"); ?></h1>
30
 
@@ -40,10 +51,40 @@ $current_filename = substr($current_filename, (strrpos($current_filename, "/") +
40
  <form enctype="multipart/form-data" method="post" action="<?php echo $formurl; ?>">
41
  <?php
42
  #wp_nonce_field('enable-media-replace');
 
 
 
43
  ?>
44
  <input type="hidden" name="ID" value="<?php echo (int) $_GET["attachment_id"]; ?>" />
45
  <div id="message" class="updated notice notice-success is-dismissible"><p><?php printf( __('NOTE: You are about to replace the media file "%s". There is no undo. Think about it!', "enable-media-replace"), $current_filename ); ?></p></div>
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  <p><?php echo __("Choose a file to upload from your computer", "enable-media-replace"); ?></p>
48
 
49
  <input type="file" name="userfile" />
@@ -64,6 +105,7 @@ $current_filename = substr($current_filename, (strrpos($current_filename, "/") +
64
  <?php else : ?>
65
  <input type="hidden" name="replace_type" value="replace" />
66
  <?php endif; ?>
67
- <input type="submit" class="button" value="<?php echo __("Upload", "enable-media-replace"); ?>" /> <a href="#" onclick="history.back();"><?php echo __("Cancel", "enable-media-replace"); ?></a>
 
68
  </form>
69
  </div>
25
 
26
 
27
  ?>
28
+ <style>
29
+ .emr-plugin-button.emr-updating:before {
30
+ font: 400 20px/1 dashicons;
31
+ display: inline-block;
32
+ content: "\f463";
33
+ -webkit-animation: rotation 2s infinite linear;
34
+ animation: rotation 2s infinite linear;
35
+ margin: 3px 5px 0 -2px;
36
+ vertical-align: top
37
+ }
38
+ </style>
39
  <div class="wrap">
40
  <h1><?php echo __("Replace Media Upload", "enable-media-replace"); ?></h1>
41
 
51
  <form enctype="multipart/form-data" method="post" action="<?php echo $formurl; ?>">
52
  <?php
53
  #wp_nonce_field('enable-media-replace');
54
+ $plugins = get_plugins();
55
+ $spInstalled = isset($plugins['shortpixel-image-optimiser/wp-shortpixel.php']);
56
+ $spActive = is_plugin_active('shortpixel-image-optimiser/wp-shortpixel.php');
57
  ?>
58
  <input type="hidden" name="ID" value="<?php echo (int) $_GET["attachment_id"]; ?>" />
59
  <div id="message" class="updated notice notice-success is-dismissible"><p><?php printf( __('NOTE: You are about to replace the media file "%s". There is no undo. Think about it!', "enable-media-replace"), $current_filename ); ?></p></div>
60
 
61
+ <?php if(!$spInstalled) {?>
62
+ <div style="background: #fff;width: 250px;min-height: 270px;border: 1px solid #ccc;float: right;padding: 15px;position: relative;margin: 0 0 10px 10px;">
63
+ <h3 class="" style="margin-top: 0;text-align: center;">
64
+ <a href="https://shortpixel.com/wp/af/VKG6LYN28044" target="_blank">
65
+ <?php _e("Optimize your images with ShortPixel, get +50% credits!", "enable-media-replace"); ?>
66
+ </a>
67
+ </h3>
68
+ <div class="" style="text-align: center;">
69
+ <a href="https://shortpixel.com/wp/af/VKG6LYN28044" target="_blank">
70
+ <img src="https://optimizingmattersblog.files.wordpress.com/2016/10/shortpixel.png">
71
+ </a>
72
+ </div>
73
+ <div class="" style="margin-bottom: 10px;">
74
+ <?php _e("Get more Google love by compressing your site's images! Check out how much ShortPixel can save your site and get +50% credits when signing up as an Enable Media Replace user! Forever!", "enable-media-replace"); ?>
75
+ </div>
76
+ <div class=""><div style="text-align: right;">
77
+ <a href="#" data-action="install" data-slug="shortpixel-image-optimiser" data-message="Activated" data-add-link="options-general.php?page=wp-shortpixel" data-add-link-name="ShortPixel options" class="button-primary emr-plugin-button ">
78
+ <?php _e("Install &amp; Activate", "enable-media-replace"); ?>
79
+ </a>
80
+ <a class="button button-secondary" id="shortpixel-image-optimiser-info" href="https://shortpixel.com/wp/af/VKG6LYN28044" target="_blank">
81
+ <?php _e("More info", "enable-media-replace"); ?></p>
82
+ </a>
83
+ </div>
84
+ </div>
85
+ </div>
86
+ <?php } ?>
87
+
88
  <p><?php echo __("Choose a file to upload from your computer", "enable-media-replace"); ?></p>
89
 
90
  <input type="file" name="userfile" />
105
  <?php else : ?>
106
  <input type="hidden" name="replace_type" value="replace" />
107
  <?php endif; ?>
108
+ <input type="submit" class="button button-primary" value="<?php echo __("Upload", "enable-media-replace"); ?>" />
109
+ <a href="#" class="button" onclick="history.back();"><?php echo __("Cancel", "enable-media-replace"); ?></a>
110
  </form>
111
  </div>
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.2.1 =
41
  * Bugfix, typo made metadata changes (thanks GitHub user icecandy!)
42
  * Removed Shortpixel links and images
37
 
38
  == Changelog ==
39
 
40
+ = 3.2.2 =
41
+ * Added ShortPixel links and images, fixed the problem of ShortPixel recommendation not dismissing.
42
+
43
  = 3.2.1 =
44
  * Bugfix, typo made metadata changes (thanks GitHub user icecandy!)
45
  * Removed Shortpixel links and images
upload.php CHANGED
@@ -13,32 +13,32 @@ $postmeta_table_name = $wpdb->prefix . "postmeta";
13
  * @param string $current_file
14
  * @param array|null $metadta
15
  */
16
- function emr_delete_current_files( $current_file, $metadata = null ) {
17
  // Delete old file
18
 
19
  // Find path of current file
20
  $current_path = substr($current_file, 0, (strrpos($current_file, "/")));
21
-
22
  // Check if old file exists first
23
  if (file_exists($current_file)) {
24
  // Now check for correct file permissions for old file
25
  clearstatcache();
26
- if (is_writable(dirname($current_file))) {
27
  // Everything OK; delete the file
28
  unlink($current_file);
29
  }
30
  else {
31
  // File exists, but has wrong permissions. Let the user know.
32
  printf(__('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);
33
- exit;
34
  }
35
  }
36
-
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 ) ) {
@@ -193,17 +193,6 @@ function emr_normalize_file_urls( $old, $new ) {
193
  return $result;
194
  }
195
 
196
- function emr_update_modified_dates( $attachment_id ) {
197
- $attachment = array(
198
- 'ID' => $attachment_id,
199
- 'post_modified_gmt' => date("Y-m-d h:m:i"),
200
- 'post_modified' => current_time('mysql'),
201
- );
202
-
203
- // Update the post modified dates into the database
204
- wp_update_post( $attachment );
205
- }
206
-
207
  // Get old guid and filetype from DB
208
  $sql = "SELECT guid, post_mime_type FROM $table_name WHERE ID = '" . (int) $_POST["ID"] . "'";
209
  list($current_filename, $current_filetype) = $wpdb->get_row($sql, ARRAY_N);
@@ -212,7 +201,9 @@ list($current_filename, $current_filetype) = $wpdb->get_row($sql, ARRAY_N);
212
  $current_guid = $current_filename;
213
  $current_filename = substr($current_filename, (strrpos($current_filename, "/") + 1));
214
 
215
- $current_file = get_attached_file((int) $_POST["ID"], apply_filters( 'emr_unfiltered_get_attached_file', true ));
 
 
216
  $current_path = substr($current_file, 0, (strrpos($current_file, "/")));
217
  $current_file = str_replace("//", "/", $current_file);
218
  $current_filename = basename($current_file);
@@ -242,7 +233,10 @@ if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
242
  // Drop-in replace and we don't even care if you uploaded something that is the wrong file-type.
243
  // That's your own fault, because we warned you!
244
 
245
- emr_delete_current_files( $current_file, $current_metadata );
 
 
 
246
 
247
  // Move new file to old location/name
248
  move_uploaded_file($_FILES["userfile"]["tmp_name"], $current_file);
@@ -250,22 +244,25 @@ if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
250
  // Chmod new file to original file permissions
251
  @chmod($current_file, $original_file_perms);
252
 
253
- // Make thumb and/or update metadata
254
- wp_update_attachment_metadata( (int) $_POST["ID"], wp_generate_attachment_metadata( (int) $_POST["ID"], $current_file ) );
255
 
256
- // Update the attachment modified dates
257
- emr_update_modified_dates( (int) $_POST["ID"] );
258
 
259
- // Trigger possible updates on CDN and other plugins
260
- update_attached_file( (int) $_POST["ID"], $current_file);
261
  } elseif ( 'replace_and_search' == $replace_type && apply_filters( 'emr_enable_replace_and_search', true ) ) {
262
  // Replace file, replace file name, update meta data, replace links pointing to old file name
263
 
 
 
 
264
  emr_delete_current_files( $current_file, $current_metadata );
265
 
266
  // Massage new filename to adhere to WordPress standards
267
  $new_filename = wp_unique_filename( $current_path, $new_filename );
268
- $new_filename = apply_filters( 'emr_unique_filename', $new_filename, $current_path, (int) $_POST['ID'] );
269
 
270
  // Move new file to old location, new name
271
  $new_file = $current_path . "/" . $new_filename;
@@ -278,10 +275,13 @@ if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
278
  $new_filetitle = apply_filters( 'enable_media_replace_title', $new_filetitle ); // Thanks Jonas Lundman (http://wordpress.org/support/topic/add-filter-hook-suggestion-to)
279
  $new_guid = str_replace($current_filename, $new_filename, $current_guid);
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
- (int) $_POST["ID"]
285
  );
286
  $wpdb->query($sql);
287
 
@@ -290,9 +290,9 @@ if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
290
  // Get old postmeta _wp_attached_file
291
  $sql = $wpdb->prepare(
292
  "SELECT meta_value FROM $postmeta_table_name WHERE meta_key = '_wp_attached_file' AND post_id = %d;",
293
- (int) $_POST["ID"]
294
  );
295
-
296
  $old_meta_name = $wpdb->get_row($sql, ARRAY_A);
297
  $old_meta_name = $old_meta_name["meta_value"];
298
 
@@ -300,16 +300,13 @@ if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
300
  $new_meta_name = str_replace($current_filename, $new_filename, $old_meta_name);
301
  $sql = $wpdb->prepare(
302
  "UPDATE $postmeta_table_name SET meta_value = '$new_meta_name' WHERE meta_key = '_wp_attached_file' AND post_id = %d;",
303
- (int) $_POST["ID"]
304
  );
305
  $wpdb->query($sql);
306
 
307
  // Make thumb and/or update metadata
308
- $new_metadata = wp_generate_attachment_metadata( (int) $_POST["ID"], $new_file );
309
- wp_update_attachment_metadata( (int) $_POST["ID"], $new_metadata );
310
-
311
- // Update the attachment modified dates
312
- emr_update_modified_dates( (int) $_POST["ID"] );
313
 
314
  // Search-and-replace filename in post database
315
  $current_base_url = emr_get_match_url( $current_guid );
@@ -348,8 +345,8 @@ if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
348
  }
349
  }
350
 
351
- // Trigger possible updates on CDN and other plugins
352
- update_attached_file( (int) $_POST["ID"], $new_file );
353
  }
354
 
355
  #echo "Updated: " . $number_of_updates;
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
20
  $current_path = substr($current_file, 0, (strrpos($current_file, "/")));
21
+
22
  // Check if old file exists first
23
  if (file_exists($current_file)) {
24
  // Now check for correct file permissions for old file
25
  clearstatcache();
26
+ if (is_writable($current_file)) {
27
  // Everything OK; delete the file
28
  unlink($current_file);
29
  }
30
  else {
31
  // File exists, but has wrong permissions. Let the user know.
32
  printf(__('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);
33
+ exit;
34
  }
35
  }
36
+
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 ) ) {
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"] . "'";
198
  list($current_filename, $current_filetype) = $wpdb->get_row($sql, ARRAY_N);
201
  $current_guid = $current_filename;
202
  $current_filename = substr($current_filename, (strrpos($current_filename, "/") + 1));
203
 
204
+ $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);
233
  // Drop-in replace and we don't even care if you uploaded something that is the wrong file-type.
234
  // That's your own fault, because we warned you!
235
 
236
+ //call replace action to give a chance to plugins like ShortPixel to delete the metadata, backups and cleanup the cache on server
237
+ do_action('wp_handle_replace', array('post_id' => $ID));
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);
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
 
250
+ // Make thumb and/or update metadata
251
+ wp_update_attachment_metadata( $ID, wp_generate_attachment_metadata( $ID, $current_file ) );
252
 
253
+ // Trigger possible updates on CDN and other plugins
254
+ update_attached_file( $ID, $current_file);
255
  } elseif ( 'replace_and_search' == $replace_type && apply_filters( 'emr_enable_replace_and_search', true ) ) {
256
  // Replace file, replace file name, update meta data, replace links pointing to old file name
257
 
258
+ //call replace action to give a chance to plugins like ShortPixel to delete the metadata, backups and cleanup the cache on server
259
+ do_action('wp_handle_replace', array('post_id' => $ID));
260
+
261
  emr_delete_current_files( $current_file, $current_metadata );
262
 
263
  // Massage new filename to adhere to WordPress standards
264
  $new_filename = wp_unique_filename( $current_path, $new_filename );
265
+ $new_filename = apply_filters( 'emr_unique_filename', $new_filename, $current_path, $ID );
266
 
267
  // Move new file to old location, new name
268
  $new_file = $current_path . "/" . $new_filename;
275
  $new_filetitle = apply_filters( 'enable_media_replace_title', $new_filetitle ); // Thanks Jonas Lundman (http://wordpress.org/support/topic/add-filter-hook-suggestion-to)
276
  $new_guid = str_replace($current_filename, $new_filename, $current_guid);
277
 
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);
287
 
290
  // Get old postmeta _wp_attached_file
291
  $sql = $wpdb->prepare(
292
  "SELECT meta_value FROM $postmeta_table_name WHERE meta_key = '_wp_attached_file' AND post_id = %d;",
293
+ $ID
294
  );
295
+
296
  $old_meta_name = $wpdb->get_row($sql, ARRAY_A);
297
  $old_meta_name = $old_meta_name["meta_value"];
298
 
300
  $new_meta_name = str_replace($current_filename, $new_filename, $old_meta_name);
301
  $sql = $wpdb->prepare(
302
  "UPDATE $postmeta_table_name SET meta_value = '$new_meta_name' WHERE meta_key = '_wp_attached_file' AND post_id = %d;",
303
+ $ID
304
  );
305
  $wpdb->query($sql);
306
 
307
  // Make thumb and/or update metadata
308
+ $new_metadata = wp_generate_attachment_metadata( $ID, $new_file );
309
+ wp_update_attachment_metadata( $ID, $new_metadata );
 
 
 
310
 
311
  // Search-and-replace filename in post database
312
  $current_base_url = emr_get_match_url( $current_guid );
345
  }
346
  }
347
 
348
+ // Trigger possible updates on CDN and other plugins
349
+ update_attached_file( $ID, $new_file );
350
  }
351
 
352
  #echo "Updated: " . $number_of_updates;