Media: Adapt response shape depending on type of query.

Restore inheriting the backbone fetch in the media library and adapt the AJAX response according to the action performed in the media query.

In [51145], the response shape was restored to the original shape, and a custom fetch was added to handle assigning the totalAttachments information in the collection. The custom fetch triggered a new set of bugs relating to zero-sized collections and loading individual images. 

props adamsilverstein, ryelle, peterwilsoncc, Presskopp, desrosj.
Fixes #53421, #53419.
Built from https://develop.svn.wordpress.org/trunk@51187


git-svn-id: http://core.svn.wordpress.org/trunk@50796 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
joedolson 2021-06-20 23:27:01 +00:00
parent 21bb743811
commit daa1cbbbfe
5 changed files with 18 additions and 18 deletions

View File

@ -783,20 +783,6 @@ var Attachments = Backbone.Collection.extend(/** @lends wp.media.model.Attachmen
});
},
// Customize fetch so we can extract the total post count from the response headers.
fetch: function(options) {
var collection = this;
var fetched = Backbone.Collection.prototype.fetch.call(this, options)
.done( function() {
if ( this.hasOwnProperty( 'getResponseHeader' ) ) {
collection.totalAttachments = parseInt( this.getResponseHeader( 'X-WP-Total' ), 10 );
} else {
collection.totalAttachments = 0;
}
} );
return fetched;
},
/**
* If the collection is a query, create and mirror an Attachments Query collection.
*

File diff suppressed because one or more lines are too long

View File

@ -115,7 +115,21 @@ window.wp = window.wp || {};
}
if ( _.isObject( response ) && ! _.isUndefined( response.success ) ) {
deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( deferred.jqXHR, [response.data] );
// When handling a media attachments request, get the total attachments from response headers.
var context = this;
deferred.done( function() {
if (
'query-attachments' === action.data.action &&
deferred.jqXHR.hasOwnProperty( 'getResponseHeader' ) &&
deferred.jqXHR.getResponseHeader( 'X-WP-Total' )
) {
context.totalAttachments = parseInt( deferred.jqXHR.getResponseHeader( 'X-WP-Total' ), 10 );
} else {
context.totalAttachments = 0;
}
} );
deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
} else {
deferred.rejectWith( this, [response] );
}

View File

@ -1,2 +1,2 @@
/*! This file is auto-generated */
window.wp=window.wp||{},function(i){var e="undefined"==typeof _wpUtilSettings?{}:_wpUtilSettings;wp.template=_.memoize(function(t){var n,s={evaluate:/<#([\s\S]+?)#>/g,interpolate:/\{\{\{([\s\S]+?)\}\}\}/g,escape:/\{\{([^\}]+?)\}\}(?!\})/g,variable:"data"};return function(e){return(n=n||_.template(i("#tmpl-"+t).html(),s))(e)}}),wp.ajax={settings:e.ajax||{},post:function(e,t){return wp.ajax.send({data:_.isObject(e)?e:_.extend(t||{},{action:e})})},send:function(e,n){var t;return _.isObject(e)?n=e:(n=n||{}).data=_.extend(n.data||{},{action:e}),n=_.defaults(n||{},{type:"POST",url:wp.ajax.settings.url,context:this}),(e=(t=i.Deferred(function(t){n.success&&t.done(n.success),n.error&&t.fail(n.error),delete n.success,delete n.error,t.jqXHR=i.ajax(n).done(function(e){"1"!==e&&1!==e||(e={success:!0}),_.isObject(e)&&!_.isUndefined(e.success)?t[e.success?"resolveWith":"rejectWith"](t.jqXHR,[e.data]):t.rejectWith(this,[e])}).fail(function(){t.rejectWith(this,arguments)})})).promise()).abort=function(){return t.jqXHR.abort(),this},e}}}(jQuery);
window.wp=window.wp||{},function(s){var e="undefined"==typeof _wpUtilSettings?{}:_wpUtilSettings;wp.template=_.memoize(function(t){var a,n={evaluate:/<#([\s\S]+?)#>/g,interpolate:/\{\{\{([\s\S]+?)\}\}\}/g,escape:/\{\{([^\}]+?)\}\}(?!\})/g,variable:"data"};return function(e){return(a=a||_.template(s("#tmpl-"+t).html(),n))(e)}}),wp.ajax={settings:e.ajax||{},post:function(e,t){return wp.ajax.send({data:_.isObject(e)?e:_.extend(t||{},{action:e})})},send:function(n,e){var t,a;return _.isObject(n)?e=n:(e=e||{}).data=_.extend(e.data||{},{action:n}),e=_.defaults(e||{},{type:"POST",url:wp.ajax.settings.url,context:this}),(t=(a=s.Deferred(function(a){e.success&&a.done(e.success),e.error&&a.fail(e.error),delete e.success,delete e.error,a.jqXHR=s.ajax(e).done(function(e){var t;"1"!==e&&1!==e||(e={success:!0}),_.isObject(e)&&!_.isUndefined(e.success)?(t=this,a.done(function(){"query-attachments"===n.data.action&&a.jqXHR.hasOwnProperty("getResponseHeader")&&a.jqXHR.getResponseHeader("X-WP-Total")?t.totalAttachments=parseInt(a.jqXHR.getResponseHeader("X-WP-Total"),10):t.totalAttachments=0}),a[e.success?"resolveWith":"rejectWith"](this,[e.data])):a.rejectWith(this,[e])}).fail(function(){a.rejectWith(this,arguments)})})).promise()).abort=function(){return a.jqXHR.abort(),this},t}}}(jQuery);

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.8-beta2-51186';
$wp_version = '5.8-beta2-51187';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.