mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-05-23 20:54:24 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@51145 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -2990,17 +2990,27 @@ function wp_ajax_query_attachments() {
|
||||
* @param array $query An array of query variables.
|
||||
*/
|
||||
$query = apply_filters( 'ajax_query_attachments_args', $query );
|
||||
$query = new WP_Query( $query );
|
||||
$attachments_query = new WP_Query( $query );
|
||||
|
||||
$posts = array_map( 'wp_prepare_attachment_for_js', $query->posts );
|
||||
$posts = array_map( 'wp_prepare_attachment_for_js', $attachments_query->posts );
|
||||
$posts = array_filter( $posts );
|
||||
$total_posts = $attachments_query->found_posts;
|
||||
|
||||
$result = array(
|
||||
'attachments' => $posts,
|
||||
'totalAttachments' => $query->found_posts,
|
||||
);
|
||||
if ( $total_posts < 1 ) {
|
||||
// Out-of-bounds, run the query again without LIMIT for total count.
|
||||
unset( $query['paged'] );
|
||||
|
||||
wp_send_json_success( $result );
|
||||
$count_query = new WP_Query();
|
||||
$count_query->query( $query_args );
|
||||
$total_posts = $count_query->found_posts;
|
||||
}
|
||||
|
||||
$max_pages = ceil( $total_posts / (int) $attachments_query->query['posts_per_page'] );
|
||||
|
||||
header( 'X-WP-Total: ' . (int) $total_posts );
|
||||
header( 'X-WP-TotalPages: ' . (int) $max_pages );
|
||||
|
||||
wp_send_json_success( $posts );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user