Untitled diff

Created Diff never expires
4 removals
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
399 lines
10 additions
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
403 lines
<?php
<?php
if ( ! defined( 'ABSPATH' ) )
if ( ! defined( 'ABSPATH' ) )
exit; // Exit if accessed directly.
exit; // Exit if accessed directly.


if (!current_user_can('upload_files'))
if (!current_user_can('upload_files'))
wp_die( esc_html__('You do not have permission to upload files.', 'enable-media-replace') );
wp_die( esc_html__('You do not have permission to upload files.', 'enable-media-replace') );


// Define DB table names
// Define DB table names
global $wpdb;
global $wpdb;
$table_name = $wpdb->prefix . "posts";
$table_name = $wpdb->prefix . "posts";
$postmeta_table_name = $wpdb->prefix . "postmeta";
$postmeta_table_name = $wpdb->prefix . "postmeta";


/**
/**
* Delete a media file and its thumbnails.
* Delete a media file and its thumbnails.
*
*
* @param string $current_file
* @param string $current_file
* @param array|null $metadta
* @param array|null $metadta
*/
*/
function emr_delete_current_files( $current_file, $metadta = null ) {
function emr_delete_current_files( $current_file, $metadta = null ) {
// Delete old file
// Delete old file


// Find path of current file
// Find path of current file
$current_path = substr($current_file, 0, (strrpos($current_file, "/")));
$current_path = substr($current_file, 0, (strrpos($current_file, "/")));
// Check if old file exists first
// Check if old file exists first
if (file_exists($current_file)) {
if (file_exists($current_file)) {
// Now check for correct file permissions for old file
// Now check for correct file permissions for old file
clearstatcache();
clearstatcache();
if (is_writable($current_file)) {
if (is_writable($current_file)) {
// Everything OK; delete the file
// Everything OK; delete the file
unlink($current_file);
unlink($current_file);
}
}
else {
else {
// File exists, but has wrong permissions. Let the user know.
// File exists, but has wrong permissions. Let the user know.
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);
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);
exit;
exit;
}
}
}
}
// Delete old resized versions if this was an image
// Delete old resized versions if this was an image
$suffix = substr($current_file, (strlen($current_file)-4));
$suffix = substr($current_file, (strlen($current_file)-4));
$prefix = substr($current_file, 0, (strlen($current_file)-4));
$prefix = substr($current_file, 0, (strlen($current_file)-4));


if (strtolower($suffix) === ".pdf") {
if (strtolower($suffix) === ".pdf") {
$prefix .= "-pdf";
$prefix .= "-pdf";
$suffix = ".jpg";
$suffix = ".jpg";
}
}


$imgAr = array(".png", ".gif", ".jpg");
$imgAr = array(".png", ".gif", ".jpg");
if (in_array($suffix, $imgAr)) {
if (in_array($suffix, $imgAr)) {
// It's a png/gif/jpg based on file name
// It's a png/gif/jpg based on file name
// Get thumbnail filenames from metadata
// Get thumbnail filenames from metadata
if ( empty( $metadata ) ) {
if ( empty( $metadata ) ) {
$metadata = wp_get_attachment_metadata( $_POST["ID"] );
$metadata = wp_get_attachment_metadata( $_POST["ID"] );
}
}
// var_dump($metadata);exit;
// var_dump($metadata);exit;


if (is_array($metadata)) { // Added fix for error messages when there is no metadata (but WHY would there not be? I don't know…)
if (is_array($metadata)) { // Added fix for error messages when there is no metadata (but WHY would there not be? I don't know…)
foreach($metadata["sizes"] AS $thissize) {
foreach($metadata["sizes"] AS $thissize) {
// Get all filenames and do an unlink() on each one;
// Get all filenames and do an unlink() on each one;
$thisfile = $thissize["file"];
$thisfile = $thissize["file"];
// Create array with all old sizes for replacing in posts later
// Create array with all old sizes for replacing in posts later
$oldfilesAr[] = $thisfile;
$oldfilesAr[] = $thisfile;
// Look for files and delete them
// Look for files and delete them
if (strlen($thisfile)) {
if (strlen($thisfile)) {
$thisfile = $current_path . "/" . $thissize["file"];
$thisfile = $current_path . "/" . $thissize["file"];
if (file_exists($thisfile)) {
if (file_exists($thisfile)) {
unlink($thisfile);
unlink($thisfile);
}
}
}
}
}
}
}
}
// Old (brutal) method, left here for now
// Old (brutal) method, left here for now
//$mask = $prefix . "-*x*" . $suffix;
//$mask = $prefix . "-*x*" . $suffix;
//array_map( "unlink", glob( $mask ) );
//array_map( "unlink", glob( $mask ) );
}
}
}
}


/**
/**
* Maybe remove query string from URL.
* Maybe remove query string from URL.
*
*
* @param string $url
* @param string $url
*
*
* @return string
* @return string
*/
*/
function emr_maybe_remove_query_string( $url ) {
function emr_maybe_remove_query_string( $url ) {
$parts = explode( '?', $url );
$parts = explode( '?', $url );


return reset( $parts );
return reset( $parts );
}
}


/**
/**
* Remove scheme from URL.
* Remove scheme from URL.
*
*
* @param string $url
* @param string $url
*
*
* @return string
* @return string
*/
*/
function emr_remove_scheme( $url ) {
function emr_remove_scheme( $url ) {
return preg_replace( '/^(?:http|https):/', '', $url );
return preg_replace( '/^(?:http|https):/', '', $url );
}
}


/**
/**
* Remove size from filename (image[-100x100].jpeg).
* Remove size from filename (image[-100x100].jpeg).
*
*
* @param string $url
* @param string $url
* @param bool $remove_extension
* @param bool $remove_extension
*
*
* @return string
* @return string
*/
*/
function emr_remove_size_from_filename( $url, $remove_extension = false ) {
function emr_remove_size_from_filename( $url, $remove_extension = false ) {
$url = preg_replace( '/^(\S+)-[0-9]{1,4}x[0-9]{1,4}(\.[a-zA-Z0-9\.]{2,})?/', '$1$2', $url );
$url = preg_replace( '/^(\S+)-[0-9]{1,4}x[0-9]{1,4}(\.[a-zA-Z0-9\.]{2,})?/', '$1$2', $url );


if ( $remove_extension ) {
if ( $remove_extension ) {
$ext = pathinfo( $url, PATHINFO_EXTENSION );
$ext = pathinfo( $url, PATHINFO_EXTENSION );
$url = str_replace( ".$ext", '', $url );
$url = str_replace( ".$ext", '', $url );
}
}


return $url;
return $url;
}
}


/**
/**
* Strip an image URL down to bare minimum for matching.
* Strip an image URL down to bare minimum for matching.
*
*
* @param string $url
* @param string $url
*
*
* @return string
* @return string
*/
*/
function emr_get_match_url($url) {
function emr_get_match_url($url) {
$url = emr_remove_scheme($url);
$url = emr_remove_scheme($url);
$url = emr_maybe_remove_query_string($url);
$url = emr_maybe_remove_query_string($url);
$url = emr_remove_size_from_filename($url, true);
$url = emr_remove_size_from_filename($url, true);
$url = emr_remove_domain_from_filename($url);
$url = emr_remove_domain_from_filename($url);


return $url;
return $url;
}
}




function emr_remove_domain_from_filename($url) {
function emr_remove_domain_from_filename($url) {
// Holding place for possible future function
// Holding place for possible future function
$url = str_replace(emr_remove_scheme(get_bloginfo('url')), '', $url);
$url = str_replace(emr_remove_scheme(get_bloginfo('url')), '', $url);
return $url;
return $url;
}
}


/**
/**
* Build an array of search or replace URLs for given attachment GUID and its metadata.
* Build an array of search or replace URLs for given attachment GUID and its metadata.
*
*
* @param string $guid
* @param string $guid
* @param array $metadata
* @param array $metadata
*
*
* @return array
* @return array
*/
*/
function emr_get_file_urls( $guid, $metadata ) {
function emr_get_file_urls( $guid, $metadata ) {
$urls = array();
$urls = array();


$guid = emr_remove_scheme( $guid );
$guid = emr_remove_scheme( $guid );
$guid= emr_remove_domain_from_filename($guid);
$guid= emr_remove_domain_from_filename($guid);


$urls['guid'] = $guid;
$urls['guid'] = $guid;


if ( empty( $metadata ) ) {
if ( empty( $metadata ) ) {
return $urls;
return $urls;
}
}


$base_url = dirname( $guid );
$base_url = dirname( $guid );


if ( ! empty( $metadata['file'] ) ) {
if ( ! empty( $metadata['file'] ) ) {
$urls['file'] = trailingslashit( $base_url ) . wp_basename( $metadata['file'] );
$urls['file'] = trailingslashit( $base_url ) . wp_basename( $metadata['file'] );
}
}


if ( ! empty( $metadata['sizes'] ) ) {
if ( ! empty( $metadata['sizes'] ) ) {
foreach ( $metadata['sizes'] as $key => $value ) {
foreach ( $metadata['sizes'] as $key => $value ) {
$urls[ $key ] = trailingslashit( $base_url ) . wp_basename( $value['file'] );
$urls[ $key ] = trailingslashit( $base_url ) . wp_basename( $value['file'] );
}
}
}
}


return $urls;
return $urls;
}
}


/**
/**
* Ensure new search URLs cover known sizes for old attachment.
* Ensure new search URLs cover known sizes for old attachment.
* Falls back to full URL if size not covered (srcset or width/height attributes should compensate).
* Falls back to full URL if size not covered (srcset or width/height attributes should compensate).
*
*
* @param array $old
* @param array $old
* @param array $new
* @param array $new
*
*
* @return array
* @return array
*/
*/
function emr_normalize_file_urls( $old, $new ) {
function emr_normalize_file_urls( $old, $new ) {
$result = array();
$result = array();


if ( empty( $new['guid'] ) ) {
if ( empty( $new['guid'] ) ) {
return $result;
return $result;
}
}


$guid = $new['guid'];
$guid = $new['guid'];


foreach ( $old as $key => $value ) {
foreach ( $old as $key => $value ) {
$result[ $key ] = empty( $new[ $key ] ) ? $guid : $new[ $key ];
$result[ $key ] = empty( $new[ $key ] ) ? $guid : $new[ $key ];
}
}


return $result;
return $result;
}
}


// Get old guid and filetype from DB
// Get old guid and filetype from DB
$sql = "SELECT post_mime_type FROM $table_name WHERE ID = '" . (int) $_POST["ID"] . "'";
$sql = "SELECT post_mime_type FROM $table_name WHERE ID = '" . (int) $_POST["ID"] . "'";
list($current_filetype) = $wpdb->get_row($sql, ARRAY_N);
list($current_filetype) = $wpdb->get_row($sql, ARRAY_N);


// Massage a bunch of vars
// Massage a bunch of vars
$current_guid =wp_get_attachment_url($_POST['ID']);
$current_guid =wp_get_attachment_url($_POST['ID']);


$ID = (int) $_POST["ID"];
$ID = (int) $_POST["ID"];


$current_file = get_attached_file($ID, apply_filters( 'emr_unfiltered_get_attached_file', true ));
$current_file = get_attached_file($ID, apply_filters( 'emr_unfiltered_get_attached_file', true ));
$current_path = substr($current_file, 0, (strrpos($current_file, "/")));
$current_path = substr($current_file, 0, (strrpos($current_file, "/")));
$current_file = preg_replace("|(?<!:)/{2,}|", "/", $current_file);
$current_file = preg_replace("|(?<!:)/{2,}|", "/", $current_file);
$current_filename = wp_basename($current_file);
$current_filename = wp_basename($current_file);
$current_metadata = wp_get_attachment_metadata( $_POST["ID"] );
$current_metadata = wp_get_attachment_metadata( $_POST["ID"] );


$replace_type = $_POST["replace_type"];
$replace_type = $_POST["replace_type"];
// We have two types: replace / replace_and_search
// We have two types: replace / replace_and_search


if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {


// New method for validating that the uploaded file is allowed, using WP:s internal wp_check_filetype_and_ext() function.
// New method for validating that the uploaded file is allowed, using WP:s internal wp_check_filetype_and_ext() function.
$filedata = wp_check_filetype_and_ext($_FILES["userfile"]["tmp_name"], $_FILES["userfile"]["name"]);
$filedata = wp_check_filetype_and_ext($_FILES["userfile"]["tmp_name"], $_FILES["userfile"]["name"]);


if ($filedata["ext"] == "") {
if ($filedata["ext"] == "") {
echo esc_html__("File type does not meet security guidelines. Try another.", 'enable-media-replace');
echo esc_html__("File type does not meet security guidelines. Try another.", 'enable-media-replace');
exit;
exit;
}
}


$new_filename = $_FILES["userfile"]["name"];
$new_filename = $_FILES["userfile"]["name"];
$new_filesize = $_FILES["userfile"]["size"];
$new_filesize = $_FILES["userfile"]["size"];
$new_filetype = $filedata["type"];
$new_filetype = $filedata["type"];


// save original file permissions
// save original file permissions
$original_file_perms = fileperms($current_file) & 0777;
$original_file_perms = fileperms($current_file) & 0777;


if ($replace_type == "replace") {
if ($replace_type == "replace") {
// Drop-in replace and we don't even care if you uploaded something that is the wrong file-type.
// Drop-in replace and we don't even care if you uploaded something that is the wrong file-type.
// That's your own fault, because we warned you!
// That's your own fault, because we warned you!


//call replace action to give a chance to plugins like ShortPixel to delete the metadata, backups and cleanup the cache on server
//call replace action to give a chance to plugins like ShortPixel to delete the metadata, backups and cleanup the cache on server
do_action('wp_handle_replace', array('post_id' => $ID));
do_action('wp_handle_replace', array('post_id' => $ID));


emr_delete_current_files( $current_file, $current_metadata );
emr_delete_current_files( $current_file, $current_metadata );


$new_filename = wp_unique_filename( $current_path, $current_filename );
$new_filename = wp_unique_filename( $current_path, $current_filename );
$new_filename = apply_filters( 'emr_unique_filename', $current_filename, $current_path, $ID );
$new_filename = apply_filters( 'emr_unique_filename', $current_filename, $current_path, $ID );


// Move new file to old location, new name
// Move new file to old location, new name
$new_file = $current_path . "/" . $current_filename;
$new_file = $current_path . "/" . $current_filename;
move_uploaded_file($_FILES["userfile"]["tmp_name"], $new_file);
move_uploaded_file($_FILES["userfile"]["tmp_name"], $new_file);


// Chmod new file to original file permissions
// Chmod new file to original file permissions
@chmod($current_file, $original_file_perms);
@chmod($current_file, $original_file_perms);


apply_filters( 'enable_media_replace_title', basename($new_file) ); // Thanks Jonas Lundman (http://wordpress.org/support/topic/add-filter-hook-suggestion-to)
apply_filters( 'enable_media_replace_title', basename($new_file) ); // Thanks Jonas Lundman (http://wordpress.org/support/topic/add-filter-hook-suggestion-to)


// Update database file timestamp
// Update database file timestamp
$post_date = gmdate( 'Y-m-d H:i:s' );
//$post_date = gmdate( 'Y-m-d H:i:s' );
$post_date = current_time('mysql');
$post_date_gmt = get_gmt_from_date($post_date);
$sql = $wpdb->prepare(
$sql = $wpdb->prepare(
"UPDATE $table_name SET post_date = '$post_date', post_date_gmt = '$post_date' WHERE ID = %d;",
"UPDATE $table_name SET post_date = '$post_date', post_date_gmt = '$post_date_gmt' WHERE ID = %d;",
$ID
$ID
);
);
$wpdb->query($sql);
$wpdb->query($sql);


//call upload action to give a chance to plugins like Resize Image After Upload to handle the new image
//call upload action to give a chance to plugins like Resize Image After Upload to handle the new image
do_action('wp_handle_upload', array('file' => $current_file, 'url' => wp_get_attachment_url($ID), 'type' => $new_filetype));
do_action('wp_handle_upload', array('file' => $current_file, 'url' => wp_get_attachment_url($ID), 'type' => $new_filetype));


// Make thumb and/or update metadata
// Make thumb and/or update metadata
wp_update_attachment_metadata( $ID, wp_generate_attachment_metadata( $ID, $current_file ) );
wp_update_attachment_metadata( $ID, wp_generate_attachment_metadata( $ID, $current_file ) );


// Trigger possible updates on CDN and other plugins
// Trigger possible updates on CDN and other plugins
update_attached_file( $ID, $current_file);
update_attached_file( $ID, $current_file);
} elseif ( 'replace_and_search' == $replace_type && apply_filters( 'emr_enable_replace_and_search', true ) ) {
} elseif ( 'replace_and_search' == $replace_type && apply_filters( 'emr_enable_replace_and_search', true ) ) {
// Replace file, replace file name, update meta data, replace links pointing to old file name
// Replace file, replace file name, update meta data, replace links pointing to old file name


//call replace action to give a chance to plugins like ShortPixel to delete the metadata, backups and cleanup the cache on server
//call replace action to give a chance to plugins like ShortPixel to delete the metadata, backups and cleanup the cache on server
do_action('wp_handle_replace', array('post_id' => $ID));
do_action('wp_handle_replace', array('post_id' => $ID));


emr_delete_current_files( $current_file, $current_metadata );
emr_delete_current_files( $current_file, $current_metadata );


// Massage new filename to adhere to WordPress standards
// Massage new filename to adhere to WordPress standards
$new_filename = wp_unique_filename( $current_path, $new_filename );
$new_filename = wp_unique_filename( $current_path, $new_filename );
$new_filename = apply_filters( 'emr_unique_filename', $new_filename, $current_path, $ID );
$new_filename = apply_filters( 'emr_unique_filename', $new_filename, $current_path, $ID );


// Move new file to old location, new name
// Move new file to old location, new name
$new_file = $current_path . "/" . $new_filename;
$new_file = $current_path . "/" . $new_filename;
move_uploaded_file($_FILES["userfile"]["tmp_name"], $new_file);
move_uploaded_file($_FILES["userfile"]["tmp_name"], $new_file);


// Chmod new file to original file permissions
// Chmod new file to original file permissions
@chmod($current_file, $original_file_perms);
@chmod($current_file, $original_file_perms);


$new_filetitle = preg_replace('/\.[^.]+$/', '', basename($new_file));
$new_filetitle = preg_replace('/\.[^.]+$/', '', basename($new_file));
$new_filetitle = apply_filters( 'enable_media_replace_title', $new_filetitle ); // Thanks Jonas Lundman (http://wordpress.org/support/topic/add-filter-hook-suggestion-to)
$new_filetitle = apply_filters( 'enable_media_replace_title', $new_filetitle ); // Thanks Jonas Lundman (http://wordpress.org/support/topic/add-filter-hook-suggestion-to)
$new_guid = str_replace($current_filename, $new_filename, $current_guid);
$new_guid = str_replace($current_filename, $new_filename, $current_guid);


//call upload action to give a chance to plugins like ShortPixel to handle the new image
//call upload action to give a chance to plugins like ShortPixel to handle the new image
//do_action('wp_handle_upload', array('file' => $new_file, 'url' => $new_guid, 'type' => $new_filetype));
//do_action('wp_handle_upload', array('file' => $new_file, 'url' => $new_guid, 'type' => $new_filetype));


$post_date = gmdate( 'Y-m-d H:i:s' );
//$post_date = gmdate( 'Y-m-d H:i:s' );
$post_date = current_time('mysql');
$post_date_gmt = get_gmt_from_date($post_date);


// Update database file name
// Update database file name
$sql = $wpdb->prepare(
$sql = $wpdb->prepare(
"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;",
"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_gmt' WHERE ID = %d;",
$ID
$ID
);
);
$wpdb->query($sql);
$wpdb->query($sql);


// Update the postmeta file name
// Update the postmeta file name


// Get old postmeta _wp_attached_file
// Get old postmeta _wp_attached_file
$sql = $wpdb->prepare(
$sql = $wpdb->prepare(
"SELECT meta_value FROM $postmeta_table_name WHERE meta_key = '_wp_attached_file' AND post_id = %d;",
"SELECT meta_value FROM $postmeta_table_name WHERE meta_key = '_wp_attached_file' AND post_id = %d;",
$ID
$ID
);
);
$old_meta_name = $wpdb->get_row($sql, ARRAY_A);
$old_meta_name = $wpdb->get_row($sql, ARRAY_A);
$old_meta_name = $old_meta_name["meta_value"];
$old_meta_name = $old_meta_name["meta_value"];


// Make new postmeta _wp_attached_file
// Make new postmeta _wp_attached_file
$new_meta_name = str_replace($current_filename, $new_filename, $old_meta_name);
$new_meta_name = str_replace($current_filename, $new_filename, $old_meta_name);
$sql = $wpdb->prepare(
$sql = $wpdb->prepare(
"UPDATE $postmeta_table_name SET meta_value = '$new_meta_name' WHERE meta_key = '_wp_attached_file' AND post_id = %d;",
"UPDATE $postmeta_table_name SET meta_value = '$new_meta_name' WHERE meta_key = '_wp_attached_file' AND post_id = %d;",
$ID
$ID
);
);
$wpdb->query($sql);
$wpdb->query($sql);


// Make thumb and/or update metadata
// Make thumb and/or update metadata
$new_metadata = wp_generate_attachment_metadata( $ID, $new_file );
$new_metadata = wp_generate_attachment_metadata( $ID, $new_file );
wp_update_attachment_metadata( $ID, $new_metadata );
wp_update_attachment_metadata( $ID, $new_metadata );


// Search-and-replace filename in post database
// Search-and-replace filename in post database
$current_base_url = emr_get_match_url( $current_guid );
$current_base_url = emr_get_match_url( $current_guid );


$sql = $wpdb->prepare(
$sql = $wpdb->prepare(
"SELECT ID, post_content FROM $table_name WHERE post_status = 'publish' AND post_content LIKE %s;",
"SELECT ID, post_content FROM $table_name WHERE post_status = 'publish' AND post_content LIKE %s;",
'%' . $current_base_url . '%'
'%' . $current_base_url . '%'
);
);




$rs = $wpdb->get_results( $sql, ARRAY_A );
$rs = $wpdb->get_results( $sql, ARRAY_A );


$number_of_updates = 0;
$number_of_updates = 0;




if ( ! empty( $rs ) ) {
if ( ! empty( $rs ) ) {
$search_urls = emr_get_file_urls( $current_guid, $current_metadata );
$search_urls = emr_get_file_urls( $current_guid, $current_metadata );
$replace_urls = emr_get_file_urls( $new_guid, $new_metadata );
$replace_urls = emr_get_file_urls( $new_guid, $new_metadata );
$replace_urls = emr_normalize_file_urls( $search_urls, $replace_urls );
$replace_urls = emr_normalize_file_urls( $search_urls, $replace_urls );




foreach ( $rs AS $rows ) {
foreach ( $rs AS $rows ) {


$number_of_updates = $number_of_updates + 1;
$number_of_updates = $number_of_updates + 1;


// replace old URLs with new URLs.
// replace old URLs with new URLs.
$post_content = $rows["post_content"];
$post_content = $rows["post_content"];
$post_content = str_replace( $search_urls, $replace_urls, $post_content );
$post_content = str_replace( $search_urls, $replace_urls, $post_content );


$sql = $wpdb->prepare(
$sql = $wpdb->prepare(
"UPDATE $table_name SET post_content = %s WHERE ID = %d;",
"UPDATE $table_name SET post_content = %s WHERE ID = %d;",
array($post_content, $rows["ID"])
array($post_content, $rows["ID"])
);
);


$wpdb->query( $sql );
$wpdb->query( $sql );
}
}
}
}


// Trigger possible updates on CDN and other plugins
// Trigger possible updates on CDN and other plugins
update_attached_file( $ID, $new_file );
update_attached_file( $ID, $new_file );
}
}


#echo "Updated: " . $number_of_updates;
#echo "Updated: " . $number_of_updates;


$returnurl = admin_url("/post.php?post={$_POST["ID"]}&action=edit&message=1");
$returnurl = admin_url("/post.php?post={$_POST["ID"]}&action=edit&message=1");


// Execute hook actions - thanks rubious for the suggestion!
// Execute hook actions - thanks rubious for the suggestion!
if (isset($new_guid)) { do_action("enable-media-replace-upload-done", $new_guid, $current_guid); }
if (isset($new_guid)) { do_action("enable-media-replace-upload-done", $new_guid, $current_guid); }


} else {
} else {
//TODO Better error handling when no file is selected.
//TODO Better error handling when no file is selected.
//For now just go back to media management
//For now just go back to media management
$returnurl = admin_url("/wp-admin/upload.php");
$returnurl = admin_url("/wp-admin/upload.php");
}
}


if (FORCE_SSL_ADMIN) {
if (FORCE_SSL_ADMIN) {
$returnurl = str_replace("http:", "https:", $returnurl);
$returnurl = str_replace("http:", "https:", $returnurl);
}
}


// Allow developers to override $returnurl
// Allow developers to override $returnurl
$returnurl = apply_filters('emr_returnurl', $returnurl);
$returnurl = apply_filters('emr_returnurl', $returnurl);


//save redirection
//save redirection
wp_redirect($returnurl);
wp_redirect($returnurl);
?>
?>