Skip to content

Media: Count uploading attachments in the media library total - #14777

Open
rejaulalomkhan wants to merge 1 commit into
WordPress:trunkfrom
rejaulalomkhan:fix/57488-media-count
Open

rejaulalomkhan wants to merge 1 commit into
WordPress:trunkfrom
rejaulalomkhan:fix/57488-media-count

Conversation

@rejaulalomkhan

Copy link
Copy Markdown

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 sets totalAttachments to the X-WP-Total header, 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_scrolling filter). This is a different cause from #65053 (r62744); it still reproduces on trunk after that change.

Testing Instructions

  1. Turn off infinite scrolling for your user (Profile > Infinite Scrolling), or wp user meta update 1 infinite_scrolling false.
  2. Open the "Create gallery" media modal (Add Media > Create gallery, or a Gallery block > Media Library) and switch to Media Library.
  3. Right away, upload 3 images while the library is still loading.
  4. Before this PR the count shows more items than the total (e.g. "Showing 55 of 52 media items"). With this PR it matches (e.g. "Showing 58 of 58 media items").

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:

npm run build
npm run grunt 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 to util.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.

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.
@github-actions

Copy link
Copy Markdown

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:

Props rejaulalomkhan.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant