From 939a47ba483d3ddbdcfdee781b65ed6f5fb01c6c Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Tue, 27 Nov 2012 15:50:59 +0000 Subject: [PATCH] Add a delete link to the media modal. Props merty, nacin, koopersmith fixes #22524 git-svn-id: http://core.svn.wordpress.org/trunk@22869 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/ajax-actions.php | 4 ++-- wp-includes/css/media-views.css | 12 ++++++++++++ wp-includes/js/media-models.js | 21 +++++++++++++++++++-- wp-includes/js/media-views.js | 10 +++++++++- wp-includes/media.php | 10 ++++++++++ wp-includes/script-loader.php | 1 - 6 files changed, 52 insertions(+), 6 deletions(-) diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index f3e2855b9a..082888df07 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -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(); diff --git a/wp-includes/css/media-views.css b/wp-includes/css/media-views.css index 232e24d7ac..23fc5f21cc 100644 --- a/wp-includes/css/media-views.css +++ b/wp-includes/css/media-views.css @@ -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 */ diff --git a/wp-includes/js/media-models.js b/wp-includes/js/media-models.js index cce0dc58de..15129e1cd1 100644 --- a/wp-includes/js/media-models.js +++ b/wp-includes/js/media-models.js @@ -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 ); diff --git a/wp-includes/js/media-views.js b/wp-includes/js/media-views.js index a71ee018b0..5ef57ceba4 100644 --- a/wp-includes/js/media-views.js +++ b/wp-includes/js/media-views.js @@ -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(); } }); diff --git a/wp-includes/media.php b/wp-includes/media.php index 44df7f38dd..d2f09bd216 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -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 ) { #>
{{ data.width }} × {{ data.height }}
<# } #> + <# if ( ! data.uploading ) { #> +
+ +
+ <# } #>
<# if ( data.compat && data.compat.meta ) { #> diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php index 9a82777c34..d9634dfdb0 100644 --- a/wp-includes/script-loader.php +++ b/wp-includes/script-loader.php @@ -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, ),