WordPress.org

Make WordPress Core


Ignore:
Timestamp:
06/14/2021 08:49:05 PM (6 months 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.