Index: /trunk/wp-admin/includes/ajax-actions.php
===================================================================
--- /trunk/wp-admin/includes/ajax-actions.php (revision 21813)
+++ /trunk/wp-admin/includes/ajax-actions.php (revision 21814)
@@ -1612,10 +1612,10 @@
if ( ! current_user_can( 'upload_files' ) )
- wp_die( -1 );
+ wp_send_json_error();
if ( isset( $_REQUEST['post_id'] ) ) {
$post_id = $_REQUEST['post_id'];
if ( ! current_user_can( 'edit_post', $post_id ) )
- wp_die( -1 );
+ wp_send_json_error();
} else {
$post_id = null;
@@ -1627,12 +1627,8 @@
if ( is_wp_error( $attachment_id ) ) {
- echo json_encode( array(
- 'type' => 'error',
- 'data' => array(
- 'message' => $attachment_id->get_error_message(),
- 'filename' => $_FILES['async-upload']['name'],
- ),
+ wp_send_json_error( array(
+ 'message' => $attachment_id->get_error_message(),
+ 'filename' => $_FILES['async-upload']['name'],
) );
- wp_die();
}
@@ -1645,17 +1641,8 @@
}
- $post = get_post( $attachment_id );
-
- echo json_encode( array(
- 'type' => 'success',
- 'data' => array(
- 'id' => $attachment_id,
- 'title' => esc_attr( $post->post_title ),
- 'filename' => esc_html( basename( $post->guid ) ),
- 'url' => wp_get_attachment_url( $attachment_id ),
- 'meta' => wp_get_attachment_metadata( $attachment_id ),
- ),
- ) );
- wp_die();
+ if ( ! $attachment = wp_prepare_attachment_for_js( $attachment_id ) )
+ wp_send_json_error();
+
+ wp_send_json_success( $attachment );
}
Index: /trunk/wp-admin/js/customize-controls.js
===================================================================
--- /trunk/wp-admin/js/customize-controls.js (revision 21813)
+++ /trunk/wp-admin/js/customize-controls.js (revision 21814)
@@ -169,5 +169,5 @@
},
success: function( attachment ) {
- this.setting.set( attachment.url );
+ this.setting.set( attachment.get('url') );
},
removerVisibility: function( to ) {
@@ -273,7 +273,8 @@
this.tabs.uploaded.both.removeClass('hidden');
+ // @todo: Do NOT store this on the attachment model. That is bad.
attachment.element = $( '' )
- .data( 'customizeImageValue', attachment.url )
- .append( '' )
+ .data( 'customizeImageValue', attachment.get('url') )
+ .append( '' )
.appendTo( this.tabs.uploaded.target );
}
@@ -946,14 +947,14 @@
data = {
- attachment_id: attachment.id,
- url: attachment.url,
- thumbnail_url: attachment.url,
- height: attachment.meta.height,
- width: attachment.meta.width
+ attachment_id: attachment.get('id'),
+ url: attachment.get('url'),
+ thumbnail_url: attachment.get('url'),
+ height: attachment.get('height'),
+ width: attachment.get('width')
};
attachment.element.data( 'customizeHeaderImageData', data );
control.settings.data.set( data );
- }
+ };
});
Index: /trunk/wp-includes/js/media-views.js
===================================================================
--- /trunk/wp-includes/js/media-views.js (revision 21813)
+++ /trunk/wp-includes/js/media-views.js (revision 21814)
@@ -280,24 +280,12 @@
// Track uploading attachments.
- 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 );
+ wp.Uploader.queue.on( 'add remove reset change:percent', this.renderUploadProgress, this );
},
render: function() {
this.attachmentsView.render();
+ this.renderUploadProgress();
this.$el.html( this.template( this.options ) ).append( this.$content );
- this.$bar = this.$('.media-progress-bar div');
+ this.$bar = this.$('.upload-attachments .media-progress-bar div');
return this;
},
@@ -313,42 +301,22 @@
container: this.$el,
dropzone: this.$el,
- browser: this.$('.upload-attachments a'),
-
- added: function( file ) {
- file.attachment = Attachment.create( _.extend({
- file: file,
- uploading: true,
- date: new Date()
- }, _.pick( file, 'loaded', 'size', 'percent' ) ) );
-
- workspace.pending.add( file.attachment );
- },
-
- progress: function( file ) {
- file.attachment.set( _.pick( file, 'loaded', 'percent' ) );
- },
-
- 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();
- }
+ browser: this.$('.upload-attachments a')
}, this.options.uploader ) );
+ },
+
+ renderUploadProgress: function() {
+ var queue = wp.Uploader.queue;
+
+ this.$el.toggleClass( 'uploading', !! queue.length );
+
+ if ( ! this.$bar || ! queue.length )
+ return;
+
+ 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 ) + '%' );
},
Index: /trunk/wp-includes/js/plupload/wp-plupload.js
===================================================================
--- /trunk/wp-includes/js/plupload/wp-plupload.js (revision 21813)
+++ /trunk/wp-includes/js/plupload/wp-plupload.js (revision 21814)
@@ -29,5 +29,5 @@
dropzone: 'drop_element'
},
- key;
+ key, error;
this.supports = {
@@ -84,4 +84,10 @@
this.param( this.params || {} );
delete this.params;
+
+ error = function( message, data, file ) {
+ if ( file.attachment )
+ file.attachment.destroy();
+ self.error( message, data, file );
+ };
this.uploader.init();
@@ -135,24 +141,55 @@
}
+ 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();
+ });
+
this.uploader.bind( 'UploadProgress', function( up, file ) {
- self.progress( file );
+ file.attachment.set( _.pick( file, 'loaded', 'percent' ) );
+ self.progress( file.attachment );
});
this.uploader.bind( 'FileUploaded', function( up, file, response ) {
+ var complete;
+
try {
response = JSON.parse( response.response );
} catch ( e ) {
- return self.error( pluploadL10n.default_error, e );
- }
-
- if ( ! response || ! response.type || ! response.data )
- return self.error( pluploadL10n.default_error );
-
- if ( 'error' === response.type )
- return self.error( response.data.message, response.data, file );
-
- if ( 'success' === response.type )
- return self.success( response.data, file );
-
+ return error( pluploadL10n.default_error, e, file );
+ }
+
+ 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 );
+
+ _.each(['file','loaded','size','uploading','percent'], function( key ) {
+ file.attachment.unset( key );
+ });
+
+ file.attachment.set( response.data );
+ wp.media.model.Attachment.get( response.data.id, file.attachment );
+
+ complete = Uploader.queue.all( function( attachment ) {
+ return ! attachment.get('uploading');
+ });
+
+ if ( complete )
+ Uploader.queue.reset();
+
+ self.success( file.attachment );
});
@@ -169,15 +206,6 @@
}
- self.error( message, error, error.file );
+ error( message, error, error.file );
up.refresh();
- });
-
- this.uploader.bind( 'FilesAdded', function( up, files ) {
- $.each( files, function() {
- self.added( this );
- });
-
- up.refresh();
- up.start();
});
@@ -238,4 +266,6 @@
});
+ Uploader.queue = new wp.media.model.Attachments( [], { query: false });
+
exports.Uploader = Uploader;
})( wp, jQuery );
Index: /trunk/wp-includes/script-loader.php
===================================================================
--- /trunk/wp-includes/script-loader.php (revision 21813)
+++ /trunk/wp-includes/script-loader.php (revision 21814)
@@ -233,5 +233,5 @@
did_action( 'init' ) && $scripts->localize( 'plupload-handlers', 'pluploadL10n', $uploader_l10n );
- $scripts->add( 'wp-plupload', "/wp-includes/js/plupload/wp-plupload$suffix.js", array('plupload-all', 'jquery', 'json2') );
+ $scripts->add( 'wp-plupload', "/wp-includes/js/plupload/wp-plupload$suffix.js", array('plupload-all', 'jquery', 'json2', 'media-models'), false, 1 );
did_action( 'init' ) && $scripts->localize( 'wp-plupload', 'pluploadL10n', $uploader_l10n );