Repository navigation
Media: Count uploading attachments in the media library total - #14777
rejaulalomkhan wants to merge 1 commit into
Conversation
When the media library's query request resolves while files are still uploading, the uploading attachments are already in the collection, but the `X-WP-Total` header from the server does not count them yet. The total was set to the header value, and the uploads were not counted again when they finished, so the modal showed totals such as "Showing 55 of 52 media items" until it was reopened. Add the attachments that are still uploading to the server total. Fixes #57488.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Description
Fixes the "Showing X of Y media items" count in the media modal when the library loads while files are still uploading, for example "Showing 55 of 52 media items" after uploading 3 images into "Create gallery".
Cause: the library's query observes
wp.Uploader.queue, so an attachment that starts uploading is added to the query and counted (_addToTotalAttachments()). If the query request (query-attachments) resolves while those uploads are still running,wp.ajax.send()then setstotalAttachmentsto theX-WP-Totalheader, which does not count the uploads yet. When the uploads finish, their models are only updated, not added again, so the total stays too low until the modal is reopened.Fix: when the total is applied from
X-WP-Total, add the attachments in the collection that are still uploading.The count is shown when infinite scrolling is turned off (the user's "Infinite Scrolling" option or the
media_library_infinite_scrollingfilter). This is a different cause from #65053 (r62744); it still reproduces on trunk after that change.Testing Instructions
wp user meta update 1 infinite_scrolling false.I reproduced it with Playwright by uploading right after the library request started: trunk showed "Showing 55 of 52" and "Showing 64 of 61"; with this PR it showed "Showing 58 of 58", "Showing 61 of 61" and "Showing 67 of 67".
QUnit:
The new tests are in
tests/qunit/wp-includes/js/media/test-models.js("wp.media.ajax total attachments"). The first one fails without the change toutil.js(5 instead of 7). All 706 QUnit tests pass with it.Screenshots
Before (trunk) and after (this PR), same steps: the count below the grid.
Trac ticket: https://core.trac.wordpress.org/ticket/57488
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Reproducing the issue with Playwright, tracing the cause, drafting the fix and the QUnit tests, and taking the screenshots. I reviewed the changes, ran the tests locally, and take responsibility for them.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.