Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 86 additions & 70 deletions enable-media-replace.php
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
<?php
/*
Plugin Name: Enable Media Replace
Plugin URI: http://www.mansjonasson.se/enable-media-replace
Description: Enable replacing media files by uploading a new file in the "Edit Media" section of the WordPress Media Library.
Version: 3.2.1
Author: Måns Jonasson
Author URI: http://www.mansjonasson.se

Dual licensed under the MIT and GPL licenses:
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html
*/

/**
* Plugin Name: Enable Media Replace
* Plugin URI: http://www.mansjonasson.se/enable-media-replace
* Description: Enable replacing media files by uploading a new file in the "Edit Media" section of the WordPress Media Library.
* Version: 3.2.1
* Author: Måns Jonasson
* Author URI: http://www.mansjonasson.se
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* Main Plugin file
* Set action hooks and add shortcode
*
* @author Måns Jonasson <http://www.mansjonasson.se>
* @copyright Måns Jonasson 13 sep 2010
* @package wordpress
* @package WordPress
* @subpackage enable-media-replace
*
*/
**/

add_action('admin_init', 'enable_media_replace_init');
add_action('admin_menu', 'emr_menu');
add_filter('attachment_fields_to_edit', 'enable_media_replace', 10, 2);
add_filter('media_row_actions', 'add_media_action', 10, 2);
add_action( 'admin_init', 'enable_media_replace_init' );
add_action( 'admin_menu', 'emr_menu' );
add_filter( 'attachment_fields_to_edit', 'enable_media_replace', 10, 2 );
add_filter( 'media_row_actions', 'add_media_action', 10, 2 );

add_shortcode('file_modified', 'emr_get_modified_date');
add_shortcode( 'file_modified', 'emr_get_modified_date' );

/**
* Register this file in WordPress so we can call it with a ?page= GET var.
* To suppress it in the menu we give it an empty menu title.
*/
function emr_menu() {
add_submenu_page(NULL, __("Replace media", "enable-media-replace"), '','upload_files', 'enable-media-replace/enable-media-replace', 'emr_options');
add_submenu_page( null, esc_html__( 'Replace media', 'enable-media-replace' ), '', 'upload_files', 'enable-media-replace/enable-media-replace', 'emr_options' );
}

/**
Expand All @@ -48,20 +45,27 @@ function enable_media_replace_init() {

/**
* Add some new fields to the attachment edit panel.
* @param array form fields edit panel
* @return array form fields with enable-media-replace fields added
*
* @param array $form_fields form fields edit panel.
* @param object $post post object.
* @return array $form_fields form fields with enable-media-replace fields added.
*/
function enable_media_replace( $form_fields, $post ) {

$url = admin_url( "upload.php?page=enable-media-replace/enable-media-replace.php&action=media_replace&attachment_id=" . $post->ID);
$action = "media_replace";
$editurl = wp_nonce_url( $url, $action );
$url = admin_url( 'upload.php?page=enable-media-replace/enable-media-replace.php&action=media_replace&attachment_id=' . $post->ID );
$action = 'media_replace';
$editurl = wp_nonce_url( $url, $action );

if (FORCE_SSL_ADMIN) {
$editurl = str_replace("http:", "https:", $editurl);
if ( FORCE_SSL_ADMIN ) {
$editurl = str_replace( 'http:', 'https:', $editurl );
}
$link = "href=\"$editurl\"";
$form_fields["enable-media-replace"] = array("label" => __("Replace media", "enable-media-replace"), "input" => "html", "html" => "<p><a class='button-secondary'$link>" . __("Upload a new file", "enable-media-replace") . "</a></p>", "helps" => __("To replace the current file, click the link and upload a replacement.", "enable-media-replace"));
$link = 'href="' . $editurl . '"';
$form_fields['enable-media-replace'] = array(
'label' => esc_html__( 'Replace media', 'enable-media-replace' ),
'input' => 'html',
'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' ),
);

return $form_fields;
}
Expand All @@ -73,87 +77,99 @@ function enable_media_replace( $form_fields, $post ) {
*/
function emr_options() {

if ( isset( $_GET['action'] ) && $_GET['action'] == 'media_replace' ) {
check_admin_referer( 'media_replace' ); // die if invalid or missing nonce
if ( array_key_exists("attachment_id", $_GET) && (int) $_GET["attachment_id"] > 0) {
include("popup.php");
if ( isset( $_GET['action'] ) && $_GET['action'] === 'media_replace' ) {
check_admin_referer( 'media_replace' ); // die if invalid or missing nonce.
if ( array_key_exists( 'attachment_id', $_GET ) && (int) $_GET['attachment_id'] > 0 ) {
include 'popup.php';
}
}

if ( isset( $_GET['action'] ) && $_GET['action'] == 'media_replace_upload' ) {
$plugin_url = str_replace("enable-media-replace.php", "", __FILE__);
check_admin_referer( 'media_replace_upload' ); // die if invalid or missing nonce
require_once($plugin_url . "upload.php");
if ( isset( $_GET['action'] ) && $_GET['action'] === 'media_replace_upload' ) {
$plugin_url = str_replace( 'enable-media-replace.php', '', __FILE__ );
check_admin_referer( 'media_replace_upload' ); // die if invalid or missing nonce.
require_once $plugin_url . 'upload.php';
}

}

/**
* Function called by filter 'media_row_actions'
* Enables linking to EMR straight from the media library
*/
function add_media_action( $actions, $post) {
$url = admin_url( "upload.php?page=enable-media-replace/enable-media-replace.php&action=media_replace&attachment_id=" . $post->ID);
$action = "media_replace";
$editurl = wp_nonce_url( $url, $action );

if (FORCE_SSL_ADMIN) {
$editurl = str_replace("http:", "https:", $editurl);
*
* @param array $actions actions.
* @param object $post post object.
* @return array $actions All actions.
*/
function add_media_action( $actions, $post ) {
$url = admin_url( 'upload.php?page=enable-media-replace/enable-media-replace.php&action=media_replace&attachment_id=' . $post->ID );
$action = 'media_replace';
$editurl = wp_nonce_url( $url, $action );

if ( FORCE_SSL_ADMIN ) {
$editurl = str_replace( 'http:', 'https:', $editurl );
}
$link = "href=\"$editurl\"";
$link = 'href="' . $editurl . '"';

$newaction['adddata'] = '<a ' . $link . ' aria-label="' . __("Replace media", "enable-media-replace") . '" rel="permalink">' . __("Replace media", "enable-media-replace") . '</a>';
return array_merge($actions,$newaction);
$newaction['adddata'] = '<a ' . $link . ' aria-label="' . esc_attr__( 'Replace media', 'enable-media-replace' ) . '" rel="permalink">' . esc_html__( 'Replace media', 'enable-media-replace' ) . '</a>';
return array_merge( $actions, $newaction );
}

/**
* Shorttag function to show the media file modification date/time.
* @param array shorttag attributes
*
* @param array $atts shorttag attributes.
* @return string content / replacement shorttag
*/
function emr_get_modified_date($atts) {
$id=0;
$format= '';
function emr_get_modified_date( $atts ) {
$id = 0;
$format = '';

extract(shortcode_atts(array(
'id' => '',
'format' => get_option('date_format') . " " . get_option('time_format'),
), $atts));
extract( shortcode_atts( array(
'id' => '',
'format' => get_option( 'date_format' ) . ' ' . get_option( 'time_format' ),
), $atts ) );

if ($id == '') return false;
if ( $id === '' ) {
return false;
}

// Get path to file
$current_file = get_attached_file($id);
// Get path to file.
$current_file = get_attached_file( $id );

if ( ! file_exists( $current_file ) ) {
return false;
}

// Get file modification time
$filetime = filemtime($current_file);
// Get file modification time.
$filetime = filemtime( $current_file );

if ( false !== $filetime ) {
// do date conversion
// Do date conversion.
return date( $format, $filetime );
}

return false;
}

// Add Last replaced by EMR plugin in the media edit screen metabox - Thanks Jonas Lundman (http://wordpress.org/support/topic/add-filter-hook-suggestion-to)

/**
* Add Last replaced by EMR plugin in the media edit screen metabox - Thanks Jonas Lundman (http://wordpress.org/support/topic/add-filter-hook-suggestion-to).
*/
function ua_admin_date_replaced_media_on_edit_media_screen() {
if( !function_exists( 'enable_media_replace' ) ) return;
if ( ! function_exists( 'enable_media_replace' ) ) {
return;
}
global $post;
$id = $post->ID;
$shortcode = "[file_modified id=$id]";
$id = $post->ID;
$shortcode = '[file_modified id=' . $id . ']';

$file_modified_time = do_shortcode($shortcode);
$file_modified_time = do_shortcode( $shortcode );
if ( ! $file_modified_time ) {
return;
}
?>
<div class="misc-pub-section curtime">
<span id="timestamp"><?php _e( 'Revised', 'enable-media-replace' ); ?>: <b><?php echo $file_modified_time; ?></b></span>
<span id="timestamp"><?php esc_html_e( 'Revised', 'enable-media-replace' ); ?>: <b><?php echo esc_html( $file_modified_time ); ?></b></span>
</div>
<?php
}
Expand Down
73 changes: 45 additions & 28 deletions popup.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,82 @@
* @author Måns Jonasson <http://www.mansjonasson.se>
* @copyright Måns Jonasson 13 sep 2010
* @version $Revision: 2303 $ | $Date: 2010-09-13 11:12:35 +0200 (ma, 13 sep 2010) $
* @package wordpress
* @package WordPress
* @subpackage enable-media-replace
*
*/

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

global $wpdb;

$table_name = $wpdb->prefix . "posts";
$table_name = $wpdb->prefix . 'posts';

$sql = "SELECT guid, post_mime_type FROM $table_name WHERE ID = " . (int) $_GET["attachment_id"];
$sql = "SELECT guid, post_mime_type FROM $table_name WHERE ID = " . (int) $_GET['attachment_id'];

list($current_filename, $current_filetype) = $wpdb->get_row($sql, ARRAY_N);
list( $current_filename, $current_filetype ) = $wpdb->get_row( $sql, ARRAY_N );

$current_filename = substr($current_filename, (strrpos($current_filename, "/") + 1));
$current_filename = substr( $current_filename, ( strrpos( $current_filename, '/' ) + 1 ) );


?>
<div class="wrap">
<h1><?php echo __("Replace Media Upload", "enable-media-replace"); ?></h1>
<h1><?php esc_html_e( 'Replace Media Upload', 'enable-media-replace' ); ?></h1>

<?php
$url = admin_url( "upload.php?page=enable-media-replace/enable-media-replace.php&noheader=true&action=media_replace_upload&attachment_id=" . (int) $_GET["attachment_id"]);
$action = "media_replace_upload";
$formurl = wp_nonce_url( $url, $action );
if (FORCE_SSL_ADMIN) {
$formurl = str_replace("http:", "https:", $formurl);
}
$url = admin_url( 'upload.php?page=enable-media-replace/enable-media-replace.php&noheader=true&action=media_replace_upload&attachment_id=' . (int) $_GET['attachment_id'] );
$action = 'media_replace_upload';
$formurl = wp_nonce_url( $url, $action );
if ( FORCE_SSL_ADMIN ) {
$formurl = str_replace( 'http:', 'https:', $formurl );
}
?>

<form enctype="multipart/form-data" method="post" action="<?php echo $formurl; ?>">
<form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $formurl ); ?>">
<?php
#wp_nonce_field('enable-media-replace');
// Save this wp_nonce_field('enable-media-replace');.
?>
<input type="hidden" name="ID" value="<?php echo (int) $_GET["attachment_id"]; ?>" />
<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>

<p><?php echo __("Choose a file to upload from your computer", "enable-media-replace"); ?></p>
<input type="hidden" name="ID" value="<?php echo esc_attr( (int) $_GET['attachment_id'] ); ?>" />
<div id="message" class="updated notice notice-success is-dismissible">
<p>
<?php
// translators: The placeholder is for the cuurent file name.
printf( esc_html__( 'NOTE: You are about to replace the media file "%s". There is no undo. Think about it!', 'enable-media-replace' ), esc_html( $current_filename ) );
?>
</p>
</div>

<p><?php esc_html_e( 'Choose a file to upload from your computer', 'enable-media-replace' ); ?></p>

<input type="file" name="userfile" />

<?php do_action( 'emr_before_replace_type_options' ); ?>

<?php if ( apply_filters( 'emr_display_replace_type_options', true ) ) : ?>
<p><?php echo __("Select media replacement type:", "enable-media-replace"); ?></p>
<p><?php esc_html_e( 'Select media replacement type:', 'enable-media-replace' ); ?></p>

<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>
<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>
<label for="replace_type_1"><input CHECKED id="replace_type_1" type="radio" name="replace_type" value="replace"> <?php esc_html_e( 'Just replace the file', 'enable-media-replace' ); ?></label>
<p class="howto">
<?php
// translators: The placeholders are for the cuurent file type and name.
printf( esc_html__( 'Note: This option requires you to upload a file of the same type (%1$s) as the one you are replacing. The name of the attachment will stay the same (%2$s) no matter what the file you upload is called.', 'enable-media-replace' ), esc_html( $current_filetype ), esc_html( $current_filename ) );
?>
</p>

<?php if ( apply_filters( 'emr_enable_replace_and_search', true ) ) : ?>
<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>
<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>
<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>
<label for="replace_type_2"><input id="replace_type_2" type="radio" name="replace_type" value="replace_and_search"> <?php esc_html_e( 'Replace the file, use new file name and update all links', 'enable-media-replace' ); ?></label>
<p class="howto">
<?php
// translators: The placeholder is for the cuurent file name.
printf( esc_html__( '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' ), esc_html( $current_filename ) );
?>
</p>
<p class="howto"><?php esc_html_e( '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>
<?php endif; ?>
<?php else : ?>
<input type="hidden" name="replace_type" value="replace" />
<?php endif; ?>
<input type="submit" class="button" value="<?php echo __("Upload", "enable-media-replace"); ?>" /> <a href="#" onclick="history.back();"><?php echo __("Cancel", "enable-media-replace"); ?></a>
<input type="submit" class="button" value="<?php esc_html_e( 'Upload', 'enable-media-replace' ); ?>" /> <a href="#" onclick="history.back();"><?php esc_html_e( 'Cancel', 'enable-media-replace' ); ?></a>
</form>
</div>
Loading