diff --git a/src/wp-admin/includes/class-wp-media-list-table.php b/src/wp-admin/includes/class-wp-media-list-table.php index b4ae67b8fd351..79949016233cb 100644 --- a/src/wp-admin/includes/class-wp-media-list-table.php +++ b/src/wp-admin/includes/class-wp-media-list-table.php @@ -123,11 +123,39 @@ public function prepare_items() { $this->is_trash = isset( $_REQUEST['attachment-filter'] ) && 'trash' === $_REQUEST['attachment-filter']; + $total_items = $wp_query->found_posts; + $total_pages = $wp_query->max_num_pages; + $per_page = (int) $wp_query->query_vars['posts_per_page']; + + /* + * WP_Query doesn't count the matching attachments when the requested page has none, + * for example after every item on the last page was deleted. Count them, with the + * same filters, so that set_pagination_args() can redirect to the last page instead + * of showing an empty list without pagination. + */ + if ( ! $total_items && $per_page > 0 && $this->get_pagenum() > 1 ) { + $count_query = new WP_Query( + array_merge( + wp_edit_attachments_query_vars( $_REQUEST ), + array( + 'paged' => 1, + 'posts_per_page' => 1, + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + ) + ) + ); + + $total_items = $count_query->found_posts; + $total_pages = (int) ceil( $total_items / $per_page ); + } + $this->set_pagination_args( array( - 'total_items' => $wp_query->found_posts, - 'total_pages' => $wp_query->max_num_pages, - 'per_page' => $wp_query->query_vars['posts_per_page'], + 'total_items' => $total_items, + 'total_pages' => $total_pages, + 'per_page' => $per_page, ) ); if ( $wp_query->posts ) { diff --git a/src/wp-admin/upload.php b/src/wp-admin/upload.php index 3c30e2eb4d2b3..058474bc2ba18 100644 --- a/src/wp-admin/upload.php +++ b/src/wp-admin/upload.php @@ -13,6 +13,9 @@ wp_die( __( 'Sorry, you are not allowed to upload files.' ) ); } +// The notice arguments below are removed from the URL, but a redirect to a valid page needs to keep them. +$request_uri_with_notice = $_SERVER['REQUEST_URI']; + $message = ''; if ( ! empty( $_GET['posted'] ) ) { $message = __( 'Media file updated.' ); @@ -358,7 +361,15 @@ function () { exit; } +/* + * If the requested page no longer exists, for example after every media file on the + * last page was deleted, preparing the items redirects to the last page. Keep the + * notice arguments in the URL until then, so the notice is shown on that page. + */ +$request_uri = $_SERVER['REQUEST_URI']; +$_SERVER['REQUEST_URI'] = $request_uri_with_notice; $wp_list_table->prepare_items(); +$_SERVER['REQUEST_URI'] = $request_uri; // Used in the HTML title tag. $title = __( 'Media Library' ); diff --git a/tests/phpunit/tests/admin/wpMediaListTable.php b/tests/phpunit/tests/admin/wpMediaListTable.php index 05dea63c8fbc2..23e6be67b4ffe 100644 --- a/tests/phpunit/tests/admin/wpMediaListTable.php +++ b/tests/phpunit/tests/admin/wpMediaListTable.php @@ -107,6 +107,13 @@ public static function set_up_before_class() { ); } + /** + * The original value of `$_REQUEST`. + * + * @var array|null + */ + private $request_original; + /** * Restores reflections to their original values. */ @@ -114,9 +121,150 @@ public function tear_down() { self::set_is_trash( self::$is_trash_original ); self::set_detached( self::$detached_original ); + if ( null !== $this->request_original ) { + $_REQUEST = $this->request_original; + } + parent::tear_down(); } + /** + * Tests that WP_Media_List_Table::prepare_items() counts the attachments matching the + * current filters when the requested page is past the last one, so that the list table + * can redirect to the last page instead of showing an empty list without pagination. + * + * @ticket 34467 + * + * @covers WP_Media_List_Table::prepare_items + * + * @dataProvider data_prepare_items_counts_attachments_past_the_last_page + * + * @param array $request Request arguments. + * @param int $expected_items The expected total number of items. + * @param int $expected_pages The expected total number of pages. + */ + public function test_prepare_items_counts_attachments_past_the_last_page( $request, $expected_items, $expected_pages ) { + wp_set_current_user( self::$admin ); + + /* + * With the shared image attachment, this makes 12 attachments: 10 images + * (3 of them attached to a post) and 2 audio files. + */ + self::factory()->attachment->create_many( + 4, + array( + 'file' => 'detached.jpg', + 'post_mime_type' => 'image/jpeg', + ) + ); + self::factory()->attachment->create_many( + 3, + array( + 'file' => 'attached.jpg', + 'post_mime_type' => 'image/jpeg', + 'post_parent' => self::$post->ID, + ) + ); + self::factory()->attachment->create_many( + 2, + array( + 'file' => 'audio.mp3', + 'post_mime_type' => 'audio/mpeg', + ) + ); + // Only these two match a search for "ticket-34467", and only by their file names. + foreach ( array( 'ticket-34467-a.jpg', 'ticket-34467-b.jpg' ) as $file ) { + self::factory()->attachment->create( + array( + 'file' => $file, + 'post_mime_type' => 'image/jpeg', + ) + ); + } + + add_filter( + 'upload_per_page', + static function () { + return 3; + } + ); + + $this->request_original = $_REQUEST; + $_REQUEST = $request; + + $mock = $this->getMockBuilder( WP_Media_List_Table::class ) + ->disableOriginalConstructor() + ->disallowMockingUnknownTypes() + ->setMethods( array( 'set_pagination_args' ) ) + ->getMock(); + + $mock->expects( $this->once() ) + ->method( 'set_pagination_args' ) + ->with( + $this->equalTo( + array( + 'total_items' => $expected_items, + 'total_pages' => $expected_pages, + 'per_page' => 3, + ) + ) + ); + + $mock->prepare_items(); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_prepare_items_counts_attachments_past_the_last_page() { + return array( + 'no filter' => array( + 'request' => array( 'paged' => 9 ), + 'expected_items' => 12, + 'expected_pages' => 4, + ), + 'media type filter' => array( + 'request' => array( + 'paged' => 9, + 'attachment-filter' => 'post_mime_type:audio', + ), + 'expected_items' => 2, + 'expected_pages' => 1, + ), + 'unattached filter' => array( + 'request' => array( + 'paged' => 9, + 'attachment-filter' => 'detached', + ), + 'expected_items' => 9, + 'expected_pages' => 3, + ), + 'search by file name' => array( + 'request' => array( + 'paged' => 9, + 's' => 'ticket-34467', + ), + 'expected_items' => 2, + 'expected_pages' => 1, + ), + 'page within range' => array( + 'request' => array( 'paged' => 2 ), + 'expected_items' => 12, + 'expected_pages' => 4, + ), + 'search without any result' => array( + 'request' => array( + 'paged' => 9, + 's' => 'no-attachment-matches-this', + ), + 'expected_items' => 0, + 'expected_pages' => 0, + ), + ); + } + /** * Tests that a call to WP_Media_List_Table::prepare_items() on a site without any scheduled events * does not result in a PHP warning.