<!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>[21814] trunk: Use JS Attachment models in wp.Uploader.</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/21814">21814</a></dd>
<dt>Author</dt> <dd>koopersmith</dd>
<dt>Date</dt> <dd>2012-09-11 16:55:58 +0000 (Tue, 11 Sep 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Use JS Attachment models in wp.Uploader. fixes <a href="http://core.trac.wordpress.org/ticket/21868">#21868</a>.

Moves the uploading Attachments queue from the media workspace view to the uploader itself. This ensures that all attachments are added to the central attachmnet store.

Updates wp.Uploader to pass Attachment models to callbacks instead of Plupload file objects. Attachments in the process of uploading have a reference to the file object (which can be fetched by calling `attachment.get('file');`).

Also updates the customizer to be compatible with the API changes.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminincludesajaxactionsphp">trunk/wp-admin/includes/ajax-actions.php</a></li>
<li><a href="#trunkwpadminjscustomizecontrolsjs">trunk/wp-admin/js/customize-controls.js</a></li>
<li><a href="#trunkwpincludesjsmediaviewsjs">trunk/wp-includes/js/media-views.js</a></li>
<li><a href="#trunkwpincludesjspluploadwppluploadjs">trunk/wp-includes/js/plupload/wp-plupload.js</a></li>
<li><a href="#trunkwpincludesscriptloaderphp">trunk/wp-includes/script-loader.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 (21813 => 21814)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/ajax-actions.php        2012-09-11 12:27:25 UTC (rev 21813)
+++ trunk/wp-admin/includes/ajax-actions.php        2012-09-11 16:55:58 UTC (rev 21814)
</span><span class="lines">@@ -1611,12 +1611,12 @@
</span><span class="cx">         check_ajax_referer( 'media-form' );
</span><span class="cx"> 
</span><span class="cx">         if ( ! current_user_can( 'upload_files' ) )
</span><del>-                wp_die( -1 );
</del><ins>+                wp_send_json_error();
</ins><span class="cx"> 
</span><span class="cx">         if ( isset( $_REQUEST['post_id'] ) ) {
</span><span class="cx">                 $post_id = $_REQUEST['post_id'];
</span><span class="cx">                 if ( ! current_user_can( 'edit_post', $post_id ) )
</span><del>-                        wp_die( -1 );
</del><ins>+                        wp_send_json_error();
</ins><span class="cx">         } else {
</span><span class="cx">                 $post_id = null;
</span><span class="cx">         }
</span><span class="lines">@@ -1626,14 +1626,10 @@
</span><span class="cx">         $attachment_id = media_handle_upload( 'async-upload', $post_id, $post_data );
</span><span class="cx"> 
</span><span class="cx">         if ( is_wp_error( $attachment_id ) ) {
</span><del>-                echo json_encode( array(
-                        'type' =&gt; 'error',
-                        'data' =&gt; array(
-                                'message'  =&gt; $attachment_id-&gt;get_error_message(),
-                                'filename' =&gt; $_FILES['async-upload']['name'],
-                        ),
</del><ins>+                wp_send_json_error( array(
+                        'message'  =&gt; $attachment_id-&gt;get_error_message(),
+                        'filename' =&gt; $_FILES['async-upload']['name'],
</ins><span class="cx">                 ) );
</span><del>-                wp_die();
</del><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if ( isset( $post_data['context'] ) &amp;&amp; isset( $post_data['theme'] ) ) {
</span><span class="lines">@@ -1644,19 +1640,10 @@
</span><span class="cx">                         update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', $post_data['theme'] );
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        $post = get_post( $attachment_id );
</del><ins>+        if ( ! $attachment = wp_prepare_attachment_for_js( $attachment_id ) )
+                wp_send_json_error();
</ins><span class="cx"> 
</span><del>-        echo json_encode( array(
-                'type' =&gt; 'success',
-                'data' =&gt; array(
-                        'id'       =&gt; $attachment_id,
-                        'title'    =&gt; esc_attr( $post-&gt;post_title ),
-                        'filename' =&gt; esc_html( basename( $post-&gt;guid ) ),
-                        'url'      =&gt; wp_get_attachment_url( $attachment_id ),
-                        'meta'     =&gt; wp_get_attachment_metadata( $attachment_id ),
-                ),
-        ) );
-        wp_die();
</del><ins>+        wp_send_json_success( $attachment );
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> function wp_ajax_image_editor() {
</span></span></pre></div>
<a id="trunkwpadminjscustomizecontrolsjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/js/customize-controls.js (21813 => 21814)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/js/customize-controls.js        2012-09-11 12:27:25 UTC (rev 21813)
+++ trunk/wp-admin/js/customize-controls.js        2012-09-11 16:55:58 UTC (rev 21814)
</span><span class="lines">@@ -168,7 +168,7 @@
</span><span class="cx">                         this.removerVisibility( this.setting.get() );
</span><span class="cx">                 },
</span><span class="cx">                 success: function( attachment ) {
</span><del>-                        this.setting.set( attachment.url );
</del><ins>+                        this.setting.set( attachment.get('url') );
</ins><span class="cx">                 },
</span><span class="cx">                 removerVisibility: function( to ) {
</span><span class="cx">                         this.remover.toggle( to != this.params.removed );
</span><span class="lines">@@ -272,9 +272,10 @@
</span><span class="cx">                         if ( this.tabs.uploaded &amp;&amp; this.tabs.uploaded.target.length ) {
</span><span class="cx">                                 this.tabs.uploaded.both.removeClass('hidden');
</span><span class="cx"> 
</span><ins>+                                // @todo: Do NOT store this on the attachment model. That is bad.
</ins><span class="cx">                                 attachment.element = $( '&lt;a href=&quot;#&quot; class=&quot;thumbnail&quot;&gt;&lt;/a&gt;' )
</span><del>-                                        .data( 'customizeImageValue', attachment.url )
-                                        .append( '&lt;img src=&quot;' +  attachment.url+ '&quot; /&gt;' )
</del><ins>+                                        .data( 'customizeImageValue', attachment.get('url') )
+                                        .append( '&lt;img src=&quot;' +  attachment.get('url')+ '&quot; /&gt;' )
</ins><span class="cx">                                         .appendTo( this.tabs.uploaded.target );
</span><span class="cx">                         }
</span><span class="cx">                 },
</span><span class="lines">@@ -945,16 +946,16 @@
</span><span class="cx">                                 api.ImageControl.prototype.success.call( control, attachment );
</span><span class="cx"> 
</span><span class="cx">                                 data = {
</span><del>-                                        attachment_id: attachment.id,
-                                        url:           attachment.url,
-                                        thumbnail_url: attachment.url,
-                                        height:        attachment.meta.height,
-                                        width:         attachment.meta.width
</del><ins>+                                        attachment_id: attachment.get('id'),
+                                        url:           attachment.get('url'),
+                                        thumbnail_url: attachment.get('url'),
+                                        height:        attachment.get('height'),
+                                        width:         attachment.get('width')
</ins><span class="cx">                                 };
</span><span class="cx"> 
</span><span class="cx">                                 attachment.element.data( 'customizeHeaderImageData', data );
</span><span class="cx">                                 control.settings.data.set( data );
</span><del>-                        }
</del><ins>+                        };
</ins><span class="cx">                 });
</span><span class="cx"> 
</span><span class="cx">                 api.trigger( 'ready' );
</span></span></pre></div>
<a id="trunkwpincludesjsmediaviewsjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/media-views.js (21813 => 21814)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/media-views.js        2012-09-11 12:27:25 UTC (rev 21813)
+++ trunk/wp-includes/js/media-views.js        2012-09-11 16:55:58 UTC (rev 21814)
</span><span class="lines">@@ -279,26 +279,14 @@
</span><span class="cx">                         this.$content.append( this.attachmentsView.$el );
</span><span class="cx"> 
</span><span class="cx">                         // Track uploading attachments.
</span><del>-                        this.pending = new Attachments( [], { query: false });
-                        this.pending.on( 'add remove reset change:percent', function() {
-                                this.$el.toggleClass( 'uploading', !! this.pending.length );
-
-                                if ( ! this.$bar || ! this.pending.length )
-                                        return;
-
-                                this.$bar.width( ( this.pending.reduce( function( memo, attachment ) {
-                                        if ( attachment.get('uploading') )
-                                                return memo + ( attachment.get('percent') || 0 );
-                                        else
-                                                return memo + 100;
-                                }, 0 ) / this.pending.length ) + '%' );
-                        }, this );
</del><ins>+                        wp.Uploader.queue.on( 'add remove reset change:percent', this.renderUploadProgress, this );
</ins><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 render: function() {
</span><span class="cx">                         this.attachmentsView.render();
</span><ins>+                        this.renderUploadProgress();
</ins><span class="cx">                         this.$el.html( this.template( this.options ) ).append( this.$content );
</span><del>-                        this.$bar = this.$('.media-progress-bar div');
</del><ins>+                        this.$bar = this.$('.upload-attachments .media-progress-bar div');
</ins><span class="cx">                         return this;
</span><span class="cx">                 },
</span><span class="cx"> 
</span><span class="lines">@@ -312,44 +300,24 @@
</span><span class="cx">                         this.uploader = new wp.Uploader( _.extend({
</span><span class="cx">                                 container: this.$el,
</span><span class="cx">                                 dropzone:  this.$el,
</span><del>-                                browser:   this.$('.upload-attachments a'),
</del><ins>+                                browser:   this.$('.upload-attachments a')
+                        }, this.options.uploader ) );
+                },
</ins><span class="cx"> 
</span><del>-                                added: function( file ) {
-                                        file.attachment = Attachment.create( _.extend({
-                                                file: file,
-                                                uploading: true,
-                                                date: new Date()
-                                        }, _.pick( file, 'loaded', 'size', 'percent' ) ) );
</del><ins>+                renderUploadProgress: function() {
+                        var queue = wp.Uploader.queue;
</ins><span class="cx"> 
</span><del>-                                        workspace.pending.add( file.attachment );
-                                },
</del><ins>+                        this.$el.toggleClass( 'uploading', !! queue.length );
</ins><span class="cx"> 
</span><del>-                                progress: function( file ) {
-                                        file.attachment.set( _.pick( file, 'loaded', 'percent' ) );
-                                },
</del><ins>+                        if ( ! this.$bar || ! queue.length )
+                                return;
</ins><span class="cx"> 
</span><del>-                                success: function( resp, file ) {
-                                        var complete;
-
-                                        _.each(['file','loaded','size','uploading','percent'], function( key ) {
-                                                file.attachment.unset( key );
-                                        });
-
-                                        file.attachment.set( 'id', resp.id );
-                                        Attachment.get( resp.id, file.attachment ).fetch();
-
-                                        complete = workspace.pending.all( function( attachment ) {
-                                                return ! attachment.get('uploading');
-                                        });
-
-                                        if ( complete )
-                                                workspace.pending.reset();
-                                },
-
-                                error: function( message, error, file ) {
-                                        file.attachment.destroy();
-                                }
-                        }, this.options.uploader ) );
</del><ins>+                        this.$bar.width( ( queue.reduce( function( memo, attachment ) {
+                                if ( attachment.get('uploading') )
+                                        return memo + ( attachment.get('percent') || 0 );
+                                else
+                                        return memo + 100;
+                        }, 0 ) / queue.length ) + '%' );
</ins><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 // Initializes the toolbar view. Currently uses defaults set for
</span></span></pre></div>
<a id="trunkwpincludesjspluploadwppluploadjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/plupload/wp-plupload.js (21813 => 21814)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/plupload/wp-plupload.js        2012-09-11 12:27:25 UTC (rev 21813)
+++ trunk/wp-includes/js/plupload/wp-plupload.js        2012-09-11 16:55:58 UTC (rev 21814)
</span><span class="lines">@@ -28,7 +28,7 @@
</span><span class="cx">                                 browser:   'browse_button',
</span><span class="cx">                                 dropzone:  'drop_element'
</span><span class="cx">                         },
</span><del>-                        key;
</del><ins>+                        key, error;
</ins><span class="cx"> 
</span><span class="cx">                 this.supports = {
</span><span class="cx">                         upload: Uploader.browser.supported
</span><span class="lines">@@ -84,6 +84,12 @@
</span><span class="cx">                 this.param( this.params || {} );
</span><span class="cx">                 delete this.params;
</span><span class="cx"> 
</span><ins>+                error = function( message, data, file ) {
+                        if ( file.attachment )
+                                file.attachment.destroy();
+                        self.error( message, data, file );
+                };
+
</ins><span class="cx">                 this.uploader.init();
</span><span class="cx"> 
</span><span class="cx">                 this.supports.dragdrop = this.uploader.features.dragdrop &amp;&amp; ! Uploader.browser.mobile;
</span><span class="lines">@@ -134,26 +140,57 @@
</span><span class="cx">                         $('#' + this.uploader.id + '_html5_container').hide();
</span><span class="cx">                 }
</span><span class="cx"> 
</span><ins>+                this.uploader.bind( 'FilesAdded', function( up, files ) {
+                        _.each( files, function( file ) {
+                                file.attachment = wp.media.model.Attachment.create( _.extend({
+                                        file:      file,
+                                        uploading: true,
+                                        date:      new Date()
+                                }, _.pick( file, 'loaded', 'size', 'percent' ) ) );
+
+                                Uploader.queue.add( file.attachment );
+
+                                self.added( file.attachment );
+                        });
+
+                        up.refresh();
+                        up.start();
+                });
+
</ins><span class="cx">                 this.uploader.bind( 'UploadProgress', function( up, file ) {
</span><del>-                        self.progress( file );
</del><ins>+                        file.attachment.set( _.pick( file, 'loaded', 'percent' ) );
+                        self.progress( file.attachment );
</ins><span class="cx">                 });
</span><span class="cx"> 
</span><span class="cx">                 this.uploader.bind( 'FileUploaded', function( up, file, response ) {
</span><ins>+                        var complete;
+
</ins><span class="cx">                         try {
</span><span class="cx">                                 response = JSON.parse( response.response );
</span><span class="cx">                         } catch ( e ) {
</span><del>-                                return self.error( pluploadL10n.default_error, e );
</del><ins>+                                return error( pluploadL10n.default_error, e, file );
</ins><span class="cx">                         }
</span><span class="cx"> 
</span><del>-                        if ( ! response || ! response.type || ! response.data )
-                                return self.error( pluploadL10n.default_error );
</del><ins>+                        if ( ! _.isObject( response ) || _.isUndefined( response.success ) )
+                                return error( pluploadL10n.default_error, null, file );
+                        else if ( ! response.success )
+                                return error( response.data.message, response.data, file );
</ins><span class="cx"> 
</span><del>-                        if ( 'error' === response.type )
-                                return self.error( response.data.message, response.data, file );
</del><ins>+                        _.each(['file','loaded','size','uploading','percent'], function( key ) {
+                                file.attachment.unset( key );
+                        });
</ins><span class="cx"> 
</span><del>-                        if ( 'success' === response.type )
-                                return self.success( response.data, file );
</del><ins>+                        file.attachment.set( response.data );
+                        wp.media.model.Attachment.get( response.data.id, file.attachment );
</ins><span class="cx"> 
</span><ins>+                        complete = Uploader.queue.all( function( attachment ) {
+                                return ! attachment.get('uploading');
+                        });
+
+                        if ( complete )
+                                Uploader.queue.reset();
+
+                        self.success( file.attachment );
</ins><span class="cx">                 });
</span><span class="cx"> 
</span><span class="cx">                 this.uploader.bind( 'Error', function( up, error ) {
</span><span class="lines">@@ -168,19 +205,10 @@
</span><span class="cx">                                 }
</span><span class="cx">                         }
</span><span class="cx"> 
</span><del>-                        self.error( message, error, error.file );
</del><ins>+                        error( message, error, error.file );
</ins><span class="cx">                         up.refresh();
</span><span class="cx">                 });
</span><span class="cx"> 
</span><del>-                this.uploader.bind( 'FilesAdded', function( up, files ) {
-                        $.each( files, function() {
-                                self.added( this );
-                        });
-
-                        up.refresh();
-                        up.start();
-                });
-
</del><span class="cx">                 this.init();
</span><span class="cx">         };
</span><span class="cx"> 
</span><span class="lines">@@ -237,5 +265,7 @@
</span><span class="cx">                 }
</span><span class="cx">         });
</span><span class="cx"> 
</span><ins>+        Uploader.queue = new wp.media.model.Attachments( [], { query: false });
+
</ins><span class="cx">         exports.Uploader = Uploader;
</span><span class="cx"> })( wp, jQuery );
</span></span></pre></div>
<a id="trunkwpincludesscriptloaderphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/script-loader.php (21813 => 21814)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/script-loader.php        2012-09-11 12:27:25 UTC (rev 21813)
+++ trunk/wp-includes/script-loader.php        2012-09-11 16:55:58 UTC (rev 21814)
</span><span class="lines">@@ -232,7 +232,7 @@
</span><span class="cx">         $scripts-&gt;add( 'plupload-handlers', &quot;/wp-includes/js/plupload/handlers$suffix.js&quot;, array('plupload-all', 'jquery') );
</span><span class="cx">         did_action( 'init' ) &amp;&amp; $scripts-&gt;localize( 'plupload-handlers', 'pluploadL10n', $uploader_l10n );
</span><span class="cx"> 
</span><del>-        $scripts-&gt;add( 'wp-plupload', &quot;/wp-includes/js/plupload/wp-plupload$suffix.js&quot;, array('plupload-all', 'jquery', 'json2') );
</del><ins>+        $scripts-&gt;add( 'wp-plupload', &quot;/wp-includes/js/plupload/wp-plupload$suffix.js&quot;, array('plupload-all', 'jquery', 'json2', 'media-models'), false, 1 );
</ins><span class="cx">         did_action( 'init' ) &amp;&amp; $scripts-&gt;localize( 'wp-plupload', 'pluploadL10n', $uploader_l10n );
</span><span class="cx"> 
</span><span class="cx">         // keep 'swfupload' for back-compat.
</span></span></pre>
</div>
</div>

</body>
</html>