diff --git src/js/_enqueues/wp/util.js src/js/_enqueues/wp/util.js
index b5ba1801d7..8384371173 100644
|
|
window.wp = window.wp || {}; |
115 | 115 | } |
116 | 116 | |
117 | 117 | if ( _.isObject( response ) && ! _.isUndefined( response.success ) ) { |
118 | | deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] ); |
| 118 | deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( deferred.jqXHR, [response.data] ); |
119 | 119 | } else { |
120 | 120 | deferred.rejectWith( this, [response] ); |
121 | 121 | } |
diff --git src/js/media/models/attachments.js src/js/media/models/attachments.js
index a0ba72869b..e9924ae912 100644
|
|
var Attachments = Backbone.Collection.extend(/** @lends wp.media.model.Attachmen |
375 | 375 | * passing through the JSON response. We override this to add attributes to |
376 | 376 | * the collection items. |
377 | 377 | * |
378 | | * @since 5.8.0 The response returns the attachments under `response.attachments` and |
379 | | * `response.totalAttachments` holds the total number of attachments found. |
380 | | * |
381 | 378 | * @param {Object|Array} response The raw response Object/Array. |
382 | 379 | * @param {Object} xhr |
383 | 380 | * @return {Array} The array of model attributes to be added to the collection |
384 | 381 | */ |
385 | 382 | parse: function( response, xhr ) { |
386 | | if ( ! _.isArray( response.attachments ) ) { |
387 | | response = [ response.attachments ]; |
388 | | } |
389 | | |
390 | | this.totalAttachments = parseInt( response.totalAttachments, 10 ); |
391 | | |
392 | | return _.map( response.attachments, function( attrs ) { |
| 383 | return _.map( response, function( attrs ) { |
393 | 384 | var id, attachment, newAttributes; |
394 | 385 | |
395 | 386 | if ( attrs instanceof Backbone.Model ) { |
… |
… |
var Attachments = Backbone.Collection.extend(/** @lends wp.media.model.Attachmen |
409 | 400 | return attachment; |
410 | 401 | }); |
411 | 402 | }, |
| 403 | |
| 404 | // Customize fetch so we can extract the total post count from the response headers. |
| 405 | fetch: function(options) { |
| 406 | var collection = this; |
| 407 | var fetched = Backbone.Collection.prototype.fetch.call(this, options) |
| 408 | .done( function() { |
| 409 | collection.totalAttachments = parseInt( this.getResponseHeader( 'X-WP-Total' ), 10 ); |
| 410 | } ); |
| 411 | return fetched; |
| 412 | }, |
| 413 | |
412 | 414 | /** |
413 | 415 | * If the collection is a query, create and mirror an Attachments Query collection. |
414 | | * |
| 416 | * |
415 | 417 | * @access private |
416 | 418 | * @param {Boolean} refresh Deprecated, refresh parameter no longer used. |
417 | 419 | */ |
diff --git src/js/media/models/query.js src/js/media/models/query.js
index d744537c44..dcbf84b00b 100644
|
|
Query = Attachments.extend(/** @lends wp.media.model.Query.prototype */{ |
112 | 112 | options = options || {}; |
113 | 113 | options.remove = false; |
114 | 114 | |
115 | | return this._more = this.fetch( options ).done( function( response ) { |
116 | | var attachments = response.attachments; |
117 | | |
118 | | if ( _.isEmpty( attachments ) || -1 === this.args.posts_per_page || attachments.length < this.args.posts_per_page ) { |
| 115 | return this._more = this.fetch( options ).done( function( attachments ) { |
| 116 | if ( _.isEmpty( attachments ) || -1 === query.args.posts_per_page || attachments.length < query.args.posts_per_page ) { |
119 | 117 | query._hasMore = false; |
120 | 118 | } |
121 | 119 | }); |
diff --git src/js/media/views/attachments/browser.js src/js/media/views/attachments/browser.js
index 6a75fb9a7d..4f7625cfe3 100644
|
|
AttachmentsBrowser = View.extend(/** @lends wp.media.view.AttachmentsBrowser.pro |
635 | 635 | }); |
636 | 636 | |
637 | 637 | view.loadMoreSpinner.show(); |
638 | | |
639 | | this.collection.more().done( function() { |
640 | | // Within done(), `this` is the returned collection. |
| 638 | this.collection.once( 'attachments:received', function() { |
641 | 639 | view.loadMoreSpinner.hide(); |
642 | 640 | } ); |
| 641 | this.collection.more(); |
643 | 642 | }, |
644 | 643 | |
645 | 644 | /** |
diff --git src/wp-admin/includes/ajax-actions.php src/wp-admin/includes/ajax-actions.php
index feb35ca40c..32e777e569 100644
|
|
function wp_ajax_query_attachments() { |
2990 | 2990 | * @param array $query An array of query variables. |
2991 | 2991 | */ |
2992 | 2992 | $query = apply_filters( 'ajax_query_attachments_args', $query ); |
2993 | | $query = new WP_Query( $query ); |
| 2993 | $attachments_query = new WP_Query( $query ); |
2994 | 2994 | |
2995 | | $posts = array_map( 'wp_prepare_attachment_for_js', $query->posts ); |
| 2995 | $posts = array_map( 'wp_prepare_attachment_for_js', $attachments_query->posts ); |
2996 | 2996 | $posts = array_filter( $posts ); |
| 2997 | $total_posts = $attachments_query->found_posts; |
2997 | 2998 | |
2998 | | $result = array( |
2999 | | 'attachments' => $posts, |
3000 | | 'totalAttachments' => $query->found_posts, |
3001 | | ); |
| 2999 | if ( $total_posts < 1 ) { |
| 3000 | // Out-of-bounds, run the query again without LIMIT for total count. |
| 3001 | unset( $query['paged'] ); |
| 3002 | |
| 3003 | $count_query = new WP_Query(); |
| 3004 | $count_query->query( $query_args ); |
| 3005 | $total_posts = $count_query->found_posts; |
| 3006 | } |
| 3007 | |
| 3008 | $max_pages = ceil( $total_posts / (int) $attachments_query->query['posts_per_page'] ); |
| 3009 | |
| 3010 | header( 'X-WP-Total: ' . (int) $total_posts ); |
| 3011 | header( 'X-WP-TotalPages: ' . (int) $max_pages ); |
3002 | 3012 | |
3003 | | wp_send_json_success( $result ); |
| 3013 | wp_send_json_success( $posts ); |
3004 | 3014 | } |
3005 | 3015 | |
3006 | 3016 | /** |