Make WordPress Core

Changeset 51145


Ignore:
Timestamp:
06/14/2021 08:49:05 PM (4 years ago)
Author:
joedolson
Message:

Media: Restore AJAX response data shape in media library.

Restore the original shape of the AJAX response data in the media library after removing infinite scroll, and pass total number of attachments in the response headers X-WP-Total and X-WP-TotalPages.

Improve backwards compatibility for plugins intercepting the ajax response. Headers match the structure and count calculation used in REST API responses.

Fix an issue with hiding the spinner after the load is completed and ensure that the load more view is created when changing tabs in the media library modal.

Follow up to [50829].

props adamsilverstein, spacedmonkey, joedolson.
Fixes #50105.

Location:
trunk/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/js/_enqueues/wp/util.js

    r48650 r51145  
    116116
    117117                    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] );
    119119                    } else {
    120120                        deferred.rejectWith( this, [response] );
  • trunk/src/js/media/models/attachments.js

    r50831 r51145  
    376376     * the collection items.
    377377     *
    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      *
    381378     * @param {Object|Array} response The raw response Object/Array.
    382379     * @param {Object} xhr
     
    384381     */
    385382    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        if ( ! _.isArray( response ) ) {
     384              response = [response];
     385        }
     386        return _.map( response, function( attrs ) {
    393387            var id, attachment, newAttributes;
    394388
     
    410404        });
    411405    },
     406
     407    // Customize fetch so we can extract the total post count from the response headers.
     408    fetch: function(options) {
     409        var collection = this;
     410        var fetched = Backbone.Collection.prototype.fetch.call(this, options)
     411            .done( function() {
     412                if ( this.hasOwnProperty( 'getResponseHeader' ) ) {
     413                    collection.totalAttachments = parseInt( this.getResponseHeader( 'X-WP-Total' ), 10 );
     414                } else {
     415                    collection.totalAttachments = 0;
     416                }
     417            } );
     418        return fetched;
     419    },
     420
    412421    /**
    413422     * If the collection is a query, create and mirror an Attachments Query collection.
    414      * 
     423     *
    415424     * @access private
    416425     * @param {Boolean} refresh Deprecated, refresh parameter no longer used.
  • trunk/src/js/media/models/query.js

    r50831 r51145  
    114114
    115115        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 ) {
     116            if ( _.isEmpty( response ) || -1 === query.args.posts_per_page || response.length < query.args.posts_per_page ) {
    119117                query._hasMore = false;
    120118            }
  • trunk/src/js/media/views/attachments/browser.js

    r50831 r51145  
    8989
    9090        this.updateContent();
     91        this.updateLoadMoreView();
    9192
    9293        if ( ! this.options.sidebar || 'errors' === this.options.sidebar ) {
     
    636637
    637638        view.loadMoreSpinner.show();
    638 
    639         this.collection.more().done( function() {
    640             // Within done(), `this` is the returned collection.
     639        this.collection.once( 'attachments:received', function() {
    641640            view.loadMoreSpinner.hide();
    642641        } );
     642        this.collection.more();
    643643    },
    644644
  • trunk/src/wp-admin/includes/ajax-actions.php

    r50981 r51145  
    29912991     */
    29922992    $query = apply_filters( 'ajax_query_attachments_args', $query );
    2993     $query = new WP_Query( $query );
    2994 
    2995     $posts = array_map( 'wp_prepare_attachment_for_js', $query->posts );
     2993    $attachments_query = new WP_Query( $query );
     2994
     2995    $posts = array_map( 'wp_prepare_attachment_for_js', $attachments_query->posts );
    29962996    $posts = array_filter( $posts );
    2997 
    2998     $result = array(
    2999         'attachments'      => $posts,
    3000         'totalAttachments' => $query->found_posts,
    3001     );
    3002 
    3003     wp_send_json_success( $result );
     2997    $total_posts = $attachments_query->found_posts;
     2998
     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 );
     3012
     3013    wp_send_json_success( $posts );
    30043014}
    30053015
Note: See TracChangeset for help on using the changeset viewer.