<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[23032] trunk: Only show Delete in media modal if the user can delete.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/23032">23032</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2012-12-04 18:33:51 +0000 (Tue, 04 Dec 2012)</dd>
</dl>
<h3>Log Message</h3>
<pre>Only show Delete in media modal if the user can delete.
Props nacin, koopersmith
fixes <a href="http://core.trac.wordpress.org/ticket/22711">#22711</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminincludesajaxactionsphp">trunk/wp-admin/includes/ajax-actions.php</a></li>
<li><a href="#trunkwpincludescssmediaviewscss">trunk/wp-includes/css/media-views.css</a></li>
<li><a href="#trunkwpincludesjsmediamodelsjs">trunk/wp-includes/js/media-models.js</a></li>
<li><a href="#trunkwpincludesjsmediaviewsjs">trunk/wp-includes/js/media-views.js</a></li>
<li><a href="#trunkwpincludesmediaphp">trunk/wp-includes/media.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminincludesajaxactionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/ajax-actions.php (23031 => 23032)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/ajax-actions.php        2012-12-04 18:23:47 UTC (rev 23031)
+++ trunk/wp-admin/includes/ajax-actions.php        2012-12-04 18:33:51 UTC (rev 23032)
</span><span class="lines">@@ -1812,9 +1812,15 @@
</span><span class="cx">         if ( ! $id = absint( $_REQUEST['id'] ) )
</span><span class="cx">                 wp_send_json_error();
</span><span class="cx">
</span><del>-        if ( ! current_user_can( 'read_post', $id ) )
</del><ins>+        if ( ! $post = get_post( $id ) )
</ins><span class="cx">                 wp_send_json_error();
</span><span class="cx">
</span><ins>+        if ( 'attachment' != $post->post_type )
+                wp_send_json_error();
+
+        if ( ! current_user_can( 'upload_files' ) )
+                wp_send_json_error();
+
</ins><span class="cx">         if ( ! $attachment = wp_prepare_attachment_for_js( $id ) )
</span><span class="cx">                 wp_send_json_error();
</span><span class="cx">
</span><span class="lines">@@ -1827,6 +1833,9 @@
</span><span class="cx"> * @since 3.5.0
</span><span class="cx"> */
</span><span class="cx"> function wp_ajax_query_attachments() {
</span><ins>+        if ( ! current_user_can( 'upload_files' ) )
+                wp_send_json_error();
+
</ins><span class="cx">         $query = isset( $_REQUEST['query'] ) ? (array) $_REQUEST['query'] : array();
</span><span class="cx">         $query = array_intersect_key( $query, array_flip( array(
</span><span class="cx">                 's', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type',
</span><span class="lines">@@ -1988,15 +1997,14 @@
</span><span class="cx">         if ( ! $post = get_post( $id ) )
</span><span class="cx">                 wp_send_json_error();
</span><span class="cx">
</span><del>-        if ( ! current_user_can( 'edit_post', $id ) )
-                wp_send_json_error();
-
</del><span class="cx">         if ( 'attachment' != $post->post_type )
</span><span class="cx">                 wp_send_json_error();
</span><span class="cx">
</span><del>-        // If this attachment is unattached, attach it. Primarily a back compat thing.
-        if ( 0 == $post->post_parent && $insert_into_post_id = intval( $_POST['post_id'] ) ) {
-                wp_update_post( array( 'ID' => $id, 'post_parent' => $insert_into_post_id ) );
</del><ins>+        if ( current_user_can( 'edit_post', $id ) ) {
+                // If this attachment is unattached, attach it. Primarily a back compat thing.
+                if ( 0 == $post->post_parent && $insert_into_post_id = intval( $_POST['post_id'] ) ) {
+                        wp_update_post( array( 'ID' => $id, 'post_parent' => $insert_into_post_id ) );
+                }
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         $rel = $url = '';
</span></span></pre></div>
<a id="trunkwpincludescssmediaviewscss"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/css/media-views.css (23031 => 23032)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/css/media-views.css        2012-12-04 18:23:47 UTC (rev 23031)
+++ trunk/wp-includes/css/media-views.css        2012-12-04 18:33:51 UTC (rev 23032)
</span><span class="lines">@@ -61,6 +61,13 @@
</span><span class="cx">         border-color: #dfdfdf;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+.media-frame input:disabled,
+.media-frame textarea:disabled,
+.media-frame input[readonly],
+.media-frame textarea[readonly] {
+        background-color: #eee;
+}
+
</ins><span class="cx"> .media-frame input[type="search"] {
</span><span class="cx">         -webkit-appearance: textfield;
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkwpincludesjsmediamodelsjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/media-models.js (23031 => 23032)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/media-models.js        2012-12-04 18:23:47 UTC (rev 23031)
+++ trunk/wp-includes/js/media-models.js        2012-12-04 18:33:51 UTC (rev 23032)
</span><span class="lines">@@ -219,7 +219,7 @@
</span><span class="cx">                         // If the attachment does not yet have an `id`, return an instantly
</span><span class="cx">                         // rejected promise. Otherwise, all of our requests will fail.
</span><span class="cx">                         if ( _.isUndefined( this.id ) )
</span><del>-                                return $.Deferred().reject().promise();
</del><ins>+                                return $.Deferred().rejectWith( this ).promise();
</ins><span class="cx">
</span><span class="cx">                         // Overload the `read` request so Attachment.fetch() functions correctly.
</span><span class="cx">                         if ( 'read' === method ) {
</span><span class="lines">@@ -233,8 +233,9 @@
</span><span class="cx">
</span><span class="cx">                         // Overload the `update` request so properties can be saved.
</span><span class="cx">                         } else if ( 'update' === method ) {
</span><del>-                                if ( ! this.get('nonces') )
-                                        return $.Deferred().resolveWith( this ).promise();
</del><ins>+                                // If we do not have the necessary nonce, fail immeditately.
+                                if ( ! this.get('nonces') || ! this.get('nonces').update )
+                                        return $.Deferred().rejectWith( this ).promise();
</ins><span class="cx">
</span><span class="cx">                                 options = options || {};
</span><span class="cx">                                 options.context = this;
</span><span class="lines">@@ -286,6 +287,10 @@
</span><span class="cx">                 saveCompat: function( data, options ) {
</span><span class="cx">                         var model = this;
</span><span class="cx">
</span><ins>+                        // If we do not have the necessary nonce, fail immeditately.
+                        if ( ! this.get('nonces') || ! this.get('nonces').update )
+                                return $.Deferred().rejectWith( this ).promise();
+
</ins><span class="cx">                         return media.post( 'save-attachment-compat', _.defaults({
</span><span class="cx">                                 id: this.id,
</span><span class="cx">                                 nonce: this.get('nonces').update,
</span></span></pre></div>
<a id="trunkwpincludesjsmediaviewsjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/media-views.js (23031 => 23032)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/media-views.js        2012-12-04 18:23:47 UTC (rev 23031)
+++ trunk/wp-includes/js/media-views.js        2012-12-04 18:33:51 UTC (rev 23032)
</span><span class="lines">@@ -2756,8 +2756,7 @@
</span><span class="cx">                 },
</span><span class="cx">
</span><span class="cx">                 render: function() {
</span><del>-                        var attachment = this.model.toJSON(),
-                                options = _.defaults( this.model.toJSON(), {
</del><ins>+                        var options = _.defaults( this.model.toJSON(), {
</ins><span class="cx">                                         orientation: 'landscape',
</span><span class="cx">                                         uploading: false,
</span><span class="cx">                                         type: '',
</span><span class="lines">@@ -2779,6 +2778,12 @@
</span><span class="cx">                         if ( 'image' === options.type )
</span><span class="cx">                                 options.size = this.imageSize();
</span><span class="cx">
</span><ins>+                        options.can = {};
+                        if ( options.nonces ) {
+                                options.can.remove = !! options.nonces['delete'];
+                                options.can.save = !! options.nonces.update;
+                        }
+
</ins><span class="cx">                         this.views.detach();
</span><span class="cx">                         this.$el.html( this.template( options ) );
</span><span class="cx">
</span><span class="lines">@@ -2967,12 +2972,12 @@
</span><span class="cx">
</span><span class="cx">                         this.updateSave('waiting');
</span><span class="cx">                         save.requests = requests;
</span><del>-                        requests.done( function() {
</del><ins>+                        requests.always( function() {
</ins><span class="cx">                                 // If we've performed another request since this one, bail.
</span><span class="cx">                                 if ( save.requests !== requests )
</span><span class="cx">                                         return;
</span><span class="cx">
</span><del>-                                view.updateSave('complete');
</del><ins>+                                view.updateSave( requests.state() === 'resolved' ? 'complete' : 'error' );
</ins><span class="cx">                                 save.savedTimer = setTimeout( function() {
</span><span class="cx">                                         view.updateSave('ready');
</span><span class="cx">                                         delete save.savedTimer;
</span></span></pre></div>
<a id="trunkwpincludesmediaphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/media.php (23031 => 23032)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/media.php        2012-12-04 18:23:47 UTC (rev 23031)
+++ trunk/wp-includes/media.php        2012-12-04 18:33:51 UTC (rev 23032)
</span><span class="lines">@@ -1334,11 +1334,17 @@
</span><span class="cx">                 'icon' => wp_mime_type_icon( $attachment->ID ),
</span><span class="cx">                 'dateFormatted' => mysql2date( get_option('date_format'), $attachment->post_date ),
</span><span class="cx">                 'nonces' => array(
</span><del>-                        'update' => wp_create_nonce( 'update-post_' . $attachment->ID ),
-                        'delete' => wp_create_nonce( 'delete-post_' . $attachment->ID ),
</del><ins>+                        'update' => false,
+                        'delete' => false,
</ins><span class="cx">                 ),
</span><span class="cx">         );
</span><span class="cx">
</span><ins>+        if ( current_user_can( 'edit_post', $attachment->ID ) )
+                $response['nonces']['update'] = wp_create_nonce( 'update-post_' . $attachment->ID );
+
+        if ( current_user_can( 'delete_post', $attachment->ID ) )
+                $response['nonces']['delete'] = wp_create_nonce( 'delete-post_' . $attachment->ID );
+
</ins><span class="cx">         if ( $meta && 'image' === $type ) {
</span><span class="cx">                 $sizes = array();
</span><span class="cx">                 $possible_sizes = apply_filters( 'image_size_names_choose', array(
</span><span class="lines">@@ -1690,7 +1696,7 @@
</span><span class="cx">                                 <# if ( 'image' === data.type && ! data.uploading && data.width && data.height ) { #>
</span><span class="cx">                                         <div class="dimensions">{{ data.width }} &times; {{ data.height }}</div>
</span><span class="cx">                                 <# } #>
</span><del>-                                <# if ( ! data.uploading ) { #>
</del><ins>+                                <# if ( ! data.uploading && data.can.remove ) { #>
</ins><span class="cx">                                         <div class="delete-attachment">
</span><span class="cx">                                                 <a href="#"><?php _e( 'Delete Permanently' ); ?></a>
</span><span class="cx">                                         </div>
</span></span></pre>
</div>
</div>
</body>
</html>