<!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>[22967] trunk: Allow the 'Uploaded to this post' view to be sorted, saving the resulting order as menu_order.</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/22967">22967</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2012-12-02 16:06:31 +0000 (Sun, 02 Dec 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Allow the 'Uploaded to this post' view to be sorted, saving the resulting order as menu_order.

This functionality is designed to be backwards compatible with manual querying for attachments by menu_order.

props koopersmith.
see <a href="http://core.trac.wordpress.org/ticket/22607">#22607</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminadminajaxphp">trunk/wp-admin/admin-ajax.php</a></li>
<li><a href="#trunkwpadminincludesajaxactionsphp">trunk/wp-admin/includes/ajax-actions.php</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="trunkwpadminadminajaxphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/admin-ajax.php (22966 => 22967)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/admin-ajax.php        2012-12-02 04:35:23 UTC (rev 22966)
+++ trunk/wp-admin/admin-ajax.php        2012-12-02 16:06:31 UTC (rev 22967)
</span><span class="lines">@@ -56,7 +56,7 @@
</span><span class="cx">         'save-widget', 'set-post-thumbnail', 'date_format', 'time_format', 'wp-fullscreen-save-post',
</span><span class="cx">         'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment', 'get-attachment',
</span><span class="cx">         'query-attachments', 'save-attachment', 'save-attachment-compat', 'send-link-to-editor',
</span><del>-        'send-attachment-to-editor',
</del><ins>+        'send-attachment-to-editor', 'save-attachment-order',
</ins><span class="cx"> );
</span><span class="cx"> 
</span><span class="cx"> // Register core Ajax calls.
</span></span></pre></div>
<a id="trunkwpadminincludesajaxactionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/ajax-actions.php (22966 => 22967)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/ajax-actions.php        2012-12-02 04:35:23 UTC (rev 22966)
+++ trunk/wp-admin/includes/ajax-actions.php        2012-12-02 16:06:31 UTC (rev 22967)
</span><span class="lines">@@ -1927,6 +1927,39 @@
</span><span class="cx">         wp_send_json_success( $attachment );
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+function wp_ajax_save_attachment_order() {
+        if ( ! isset( $_REQUEST['post_id'] ) )
+                wp_send_json_error();
+
+        if ( ! $post_id = absint( $_REQUEST['post_id'] ) )
+                wp_send_json_error();
+
+        if ( empty( $_REQUEST['attachments'] ) )
+                wp_send_json_error();
+
+        check_ajax_referer( 'update-post_' . $post_id, 'nonce' );
+
+        $attachments = $_REQUEST['attachments'];
+
+        if ( ! current_user_can( 'edit_post', $post_id ) )
+                wp_send_json_error();
+
+        $post = get_post( $post_id, ARRAY_A );
+
+        foreach ( $attachments as $attachment_id =&gt; $menu_order ) {
+                if ( ! current_user_can( 'edit_post', $attachment_id ) )
+                        continue;
+                if ( ! $attachment = get_post( $attachment_id ) )
+                        continue;
+                if ( 'attachment' != $attachment-&gt;post_type )
+                        continue;
+
+                wp_update_post( array( 'ID' =&gt; $attachment_id, 'menu_order' =&gt; $menu_order ) );
+        }
+
+        wp_send_json_success();
+}
+
</ins><span class="cx"> /**
</span><span class="cx">  * Generates the HTML to send an attachment to the editor.
</span><span class="cx">  * Backwards compatible with the media_send_to_editor filter and the chain
</span></span></pre></div>
<a id="trunkwpincludesjsmediamodelsjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/media-models.js (22966 => 22967)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/media-models.js        2012-12-02 04:35:23 UTC (rev 22966)
+++ trunk/wp-includes/js/media-models.js        2012-12-02 16:06:31 UTC (rev 22967)
</span><span class="lines">@@ -523,6 +523,34 @@
</span><span class="cx">                 _requery: function() {
</span><span class="cx">                         if ( this.props.get('query') )
</span><span class="cx">                                 this.mirror( Query.get( this.props.toJSON() ) );
</span><ins>+                },
+
+                // If this collection is sorted by `menuOrder`, recalculates and saves
+                // the menu order to the database.
+                saveMenuOrder: function() {
+                        if ( 'menuOrder' !== this.props.get('orderby') )
+                                return;
+
+                        // Removes any uploading attachments, updates each attachment's
+                        // menu order, and returns an object with an { id: menuOrder }
+                        // mapping to pass to the request.
+                        var attachments = this.chain().filter( function( attachment ) {
+                                return ! _.isUndefined( attachment.id );
+                        }).map( function( attachment, index ) {
+                                // Indices start at 1.
+                                index = index + 1;
+                                attachment.set( 'menuOrder', index );
+                                return [ attachment.id, index ];
+                        }).object().value();
+
+                        if ( _.isEmpty( attachments ) )
+                                return;
+
+                        return media.post( 'save-attachment-order', {
+                                nonce:       media.model.settings.updatePostNonce,
+                                post_id:     media.model.settings.postId,
+                                attachments: attachments
+                        });
</ins><span class="cx">                 }
</span><span class="cx">         }, {
</span><span class="cx">                 comparator: function( a, b, options ) {
</span></span></pre></div>
<a id="trunkwpincludesjsmediaviewsjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/media-views.js (22966 => 22967)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/media-views.js        2012-12-02 04:35:23 UTC (rev 22966)
+++ trunk/wp-includes/js/media-views.js        2012-12-02 16:06:31 UTC (rev 22967)
</span><span class="lines">@@ -14,6 +14,7 @@
</span><span class="cx"> 
</span><span class="cx">         // Copy the `postId` setting over to the model settings.
</span><span class="cx">         media.model.settings.postId = media.view.settings.postId;
</span><ins>+        media.model.settings.updatePostNonce = media.view.settings.nonce.updatePost;
</ins><span class="cx"> 
</span><span class="cx">         // Check if the browser supports CSS 3.0 transitions
</span><span class="cx">         $.support.transition = (function(){
</span><span class="lines">@@ -267,7 +268,8 @@
</span><span class="cx">                         content:    'browse',
</span><span class="cx">                         searchable: true,
</span><span class="cx">                         filterable: false,
</span><del>-                        uploads:    true
</del><ins>+                        uploads:    true,
+                        sortable:   true
</ins><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 initialize: function() {
</span><span class="lines">@@ -2690,7 +2692,6 @@
</span><span class="cx">                         this.scroll = _.chain( this.scroll ).bind( this ).throttle( this.options.refreshSensitivity ).value();
</span><span class="cx"> 
</span><span class="cx">                         this.initSortable();
</span><del>-                        this.collection.props.on( 'change:orderby', this.refreshSortable, this );
</del><span class="cx"> 
</span><span class="cx">                         _.bindAll( this, 'css' );
</span><span class="cx">                         this.model.on( 'change:edge change:gutter', this.css, this );
</span><span class="lines">@@ -2734,7 +2735,8 @@
</span><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 initSortable: function() {
</span><del>-                        var collection = this.collection,
</del><ins>+                        var view = this,
+                                collection = this.collection,
</ins><span class="cx">                                 from;
</span><span class="cx"> 
</span><span class="cx">                         if ( ! this.options.sortable || ! $.fn.sortable )
</span><span class="lines">@@ -2760,14 +2762,30 @@
</span><span class="cx">                                 // Update the model's index in the collection.
</span><span class="cx">                                 // Do so silently, as the view is already accurate.
</span><span class="cx">                                 update: function( event, ui ) {
</span><del>-                                        var model = collection.at( from );
</del><ins>+                                        var model = collection.at( from ),
+                                                comparator = collection.comparator;
</ins><span class="cx"> 
</span><ins>+                                        // Temporarily disable the comparator to prevent `add`
+                                        // from re-sorting.
+                                        delete collection.comparator;
+
+                                        // Silently shift the model to its new index.
</ins><span class="cx">                                         collection.remove( model, {
</span><span class="cx">                                                 silent: true
</span><span class="cx">                                         }).add( model, {
</span><span class="cx">                                                 at:     ui.item.index(),
</span><span class="cx">                                                 silent: true
</span><span class="cx">                                         });
</span><ins>+
+                                        // Restore the comparator.
+                                        collection.comparator = comparator;
+
+                                        // If the collection is sorted by menu order,
+                                        // update the menu order.
+                                        view.saveMenuOrder();
+
+                                        // Make sure any menu-order-related callbacks are bound.
+                                        view.refreshSortable();
</ins><span class="cx">                                 }
</span><span class="cx">                         });
</span><span class="cx"> 
</span><span class="lines">@@ -2776,6 +2794,9 @@
</span><span class="cx">                         collection.props.on( 'change:orderby', function() {
</span><span class="cx">                                 this.$el.sortable( 'option', 'disabled', !! collection.comparator );
</span><span class="cx">                         }, this );
</span><ins>+
+                        this.collection.props.on( 'change:orderby', this.refreshSortable, this );
+                        this.refreshSortable();
</ins><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 refreshSortable: function() {
</span><span class="lines">@@ -2783,9 +2804,31 @@
</span><span class="cx">                                 return;
</span><span class="cx"> 
</span><span class="cx">                         // If the `collection` has a `comparator`, disable sorting.
</span><del>-                        this.$el.sortable( 'option', 'disabled', !! this.collection.comparator );
</del><ins>+                        var collection = this.collection,
+                                orderby = collection.props.get('orderby'),
+                                enabled = 'menuOrder' === orderby || ! collection.comparator,
+                                hasMenuOrder;
+
+                        this.$el.sortable( 'option', 'disabled', ! enabled );
+
+                        // Check if any attachments have a specified menu order.
+                        hasMenuOrder = this.collection.any( function( attachment ) {
+                                return attachment.get('menuOrder');
+                        });
+
+                        // Always unbind the `saveMenuOrder` callback to prevent multiple
+                        // callbacks stacking up.
+                        this.collection.off( 'change:uploading', this.saveMenuOrder, this );
+
+                        if ( hasMenuOrder )
+                                this.collection.on( 'change:uploading', this.saveMenuOrder, this );
+
</ins><span class="cx">                 },
</span><span class="cx"> 
</span><ins>+                saveMenuOrder: function() {
+                        this.collection.saveMenuOrder();
+                },
+
</ins><span class="cx">                 createAttachmentView: function( attachment ) {
</span><span class="cx">                         var view = new this.options.AttachmentView({
</span><span class="cx">                                 controller: this.controller,
</span><span class="lines">@@ -3049,7 +3092,7 @@
</span><span class="cx">                                 }).render() );
</span><span class="cx">                         }
</span><span class="cx"> 
</span><del>-                        if ( this.options.sortable ) {
</del><ins>+                        if ( this.options.sortable &amp;&amp; ! this.options.filters ) {
</ins><span class="cx">                                 this.toolbar.set( 'dragInfo', new media.View({
</span><span class="cx">                                         el: $( '&lt;div class=&quot;instructions&quot;&gt;' + l10n.dragInfo + '&lt;/div&gt;' )[0],
</span><span class="cx">                                         priority: -40
</span></span></pre></div>
<a id="trunkwpincludesmediaphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/media.php (22966 => 22967)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/media.php        2012-12-02 04:35:23 UTC (rev 22966)
+++ trunk/wp-includes/media.php        2012-12-02 16:06:31 UTC (rev 22967)
</span><span class="lines">@@ -1433,6 +1433,7 @@
</span><span class="cx">         if ( isset( $args['post'] ) ) {
</span><span class="cx">                 $post = get_post( $args['post'] );
</span><span class="cx">                 $settings['postId'] = $post-&gt;ID;
</span><ins>+                $settings['nonce']['updatePost'] = wp_create_nonce( 'update-post_' . $post-&gt;ID );
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         $hier = $post &amp;&amp; is_post_type_hierarchical( $post-&gt;post_type );
</span></span></pre>
</div>
</div>

</body>
</html>