Add a delete link to the media modal.

Props merty, nacin, koopersmith
fixes #22524


git-svn-id: https://develop.svn.wordpress.org/trunk@22869 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2012-11-27 15:50:59 +00:00
parent 1f4eed3141
commit 6a9c14ed5f
6 changed files with 52 additions and 6 deletions

View File

@ -1843,7 +1843,7 @@ function wp_ajax_save_attachment() {
if ( ! $id = absint( $_REQUEST['id'] ) )
wp_send_json_error();
check_ajax_referer( 'save-attachment', 'nonce' );
check_ajax_referer( 'update-post_' . $id, 'nonce' );
if ( ! current_user_can( 'edit_post', $id ) )
wp_send_json_error();
@ -1889,7 +1889,7 @@ function wp_ajax_save_attachment_compat() {
wp_send_json_error();
$attachment_data = $_REQUEST['attachments'][ $id ];
check_ajax_referer( 'save-attachment', 'nonce' );
check_ajax_referer( 'update-post_' . $id, 'nonce' );
if ( ! current_user_can( 'edit_post', $id ) )
wp_send_json_error();

View File

@ -1191,6 +1191,18 @@ a.media-modal-close {
float: left;
}
.attachment-info .delete-attachment a {
color: red;
padding: 2px 4px;
margin: -2px -4px;
text-decoration: none;
}
.attachment-info .delete-attachment a:hover {
color: #fff;
background: red;
}
/**
* Attachment Display Settings
*/

View File

@ -218,6 +218,11 @@ window.wp = window.wp || {};
*/
Attachment = media.model.Attachment = Backbone.Model.extend({
sync: function( method, model, options ) {
// If the attachment does not yet have an `id`, return an instantly
// rejected promise. Otherwise, all of our requests will fail.
if ( _.isUndefined( this.id ) )
return $.Deferred().reject().promise();
// Overload the `read` request so Attachment.fetch() functions correctly.
if ( 'read' === method ) {
options = options || {};
@ -237,7 +242,7 @@ window.wp = window.wp || {};
options.data = _.extend( options.data || {}, {
action: 'save-attachment',
id: this.id,
nonce: media.model.settings.saveAttachmentNonce,
nonce: this.get('nonces').update,
post_id: media.model.settings.postId
});
@ -252,6 +257,18 @@ window.wp = window.wp || {};
}
return media.ajax( options );
// Overload the `delete` request so attachments can be removed.
// This will permanently delete an attachment.
} else if ( 'delete' === method ) {
options = options || {};
options.context = this;
options.data = _.extend( options.data || {}, {
action: 'delete-post',
id: this.id,
_wpnonce: this.get('nonces')['delete']
});
return media.ajax( options );
}
},
@ -270,7 +287,7 @@ window.wp = window.wp || {};
return media.post( 'save-attachment-compat', _.defaults({
id: this.id,
nonce: media.model.settings.saveAttachmentNonce,
nonce: this.get('nonces').update,
post_id: media.model.settings.postId
}, data ) ).done( function( resp, status, xhr ) {
model.set( model.parse( resp, xhr ), options );

View File

@ -3406,7 +3406,15 @@
'change [data-setting]': 'updateSetting',
'change [data-setting] input': 'updateSetting',
'change [data-setting] select': 'updateSetting',
'change [data-setting] textarea': 'updateSetting'
'change [data-setting] textarea': 'updateSetting',
'click .delete-attachment': 'deleteAttachment'
},
deleteAttachment: function(event) {
event.preventDefault();
if ( confirm( l10n.warnDelete ) )
this.model.destroy();
}
});

View File

@ -1327,6 +1327,10 @@ function wp_prepare_attachment_for_js( $attachment ) {
'subtype' => $subtype,
'icon' => wp_mime_type_icon( $attachment->ID ),
'dateFormatted' => mysql2date( get_option('date_format'), $attachment->post_date ),
'nonces' => array(
'update' => wp_create_nonce( 'update-post_' . $attachment->ID ),
'delete' => wp_create_nonce( 'delete-post_' . $attachment->ID ),
),
);
if ( $meta && 'image' === $type ) {
@ -1452,6 +1456,7 @@ function wp_enqueue_media( $args = array() ) {
'allMediaItems' => __( 'All media items' ),
'insertIntoPost' => $hier ? __( 'Insert into page' ) : __( 'Insert into post' ),
'uploadedToThisPost' => $hier ? __( 'Uploaded to this page' ) : __( 'Uploaded to this post' ),
'warnDelete' => __( "You are about to permanently delete this item.\n 'Cancel' to stop, 'OK' to delete." ),
// From URL
'fromUrlTitle' => __( 'From URL' ),
@ -1641,6 +1646,11 @@ function wp_print_media_templates( $attachment ) {
<# if ( 'image' === data.type && ! data.uploading ) { #>
<div class="dimensions">{{ data.width }} &times; {{ data.height }}</div>
<# } #>
<# if ( ! data.uploading ) { #>
<div class="delete-attachment">
<a href="#"><?php _e( 'Delete Permanently' ); ?></a>
</div>
<# } #>
</div>
<div class="compat-meta">
<# if ( data.compat && data.compat.meta ) { #>

View File

@ -322,7 +322,6 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'media-models', "/wp-includes/js/media-models$suffix.js", array( 'backbone', 'jquery' ), false, 1 );
did_action( 'init' ) && $scripts->localize( 'media-models', '_wpMediaModelsL10n', array(
'settings' => array(
'saveAttachmentNonce' => wp_create_nonce( 'save-attachment' ),
'ajaxurl' => admin_url( 'admin-ajax.php', 'relative' ),
'postId' => 0,
),