From 39ecd7846d24f5dd46833b68c6cb90dcdc070398 Mon Sep 17 00:00:00 2001 From: Joe Dolson Date: Sun, 20 Jun 2021 23:25:55 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@51187 602fd350-edb4-49c9-b593-d223f7449a82 --- src/js/_enqueues/wp/util.js | 16 +++++++++++++++- src/js/media/models/attachments.js | 14 -------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/js/_enqueues/wp/util.js b/src/js/_enqueues/wp/util.js index 8384371173..8c37db324b 100644 --- a/src/js/_enqueues/wp/util.js +++ b/src/js/_enqueues/wp/util.js @@ -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] ); } diff --git a/src/js/media/models/attachments.js b/src/js/media/models/attachments.js index e06d719939..42b05d7546 100644 --- a/src/js/media/models/attachments.js +++ b/src/js/media/models/attachments.js @@ -404,20 +404,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. *