Skip to content

Fix the All Media count in caption and document pickers - #2258

Merged
subodhr258 merged 6 commits into
mainfrom
fix/all-media-count-mime-types
Oct 28, 2026
Merged

subodhr258 merged 6 commits into
mainfrom
fix/all-media-count-mime-types

Conversation

@subodhr258

@subodhr258 subodhr258 commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Review packet

In one line: the media picker's All Media count now works when the picker only allows caption files or documents, instead of failing with a 400 and showing 0. Fixes #2239.

1. Criteria and proof

Acceptance criterion (from the issue) Proof
Caption picker (video editor > Transcription > Upload File) with 1 .vtt in the library: All Media reads 1, and no count request fails Recording V2 step 1; B1 step 1 shows 0 and a 400 on 2.5.1
Document block picker (+ Add Document) with 4 documents in the library: All Media reads 4, no failed request V2 step 3; B1 step 3 shows 0 and a 400
The plain Media Library page is unchanged: All Media reads 71 before and after V2 and B1 step 4
Works on every WordPress version GoDAM supports (6.5 and up) WordPress 6.8.11 check in section 6 below: 1 caption file of 2 counts as 1
Pickers with one whole type (video, image) still use the REST route as before; more than one type goes to query-attachments Unit test "keeps whole types on the REST route as media_type"

2. Before and after

Same four steps on both runs, on godam-dev with one test .vtt caption file in the library (removed after). Left pane: the video editor's caption picker. Right pane: the Document block's picker, then the plain Media Library page as an untouched control.

▶ Video (16 s): B1, before, on 2.5.1. Caption picker: All Media 0, and the count request fails with 400. Document picker: All Media 0, also 400. Media Library page: 71.

Video: before the fix, both pickers show All Media 0

▶ Play the video

▶ Video (14 s): V2, after, with this PR. Caption picker: All Media 1. Document picker: All Media 4 (the 4 documents in the library). Both count requests return 200. Media Library page: still 71.

Video: after the fix, the caption picker shows All Media 1 and the document picker 4

▶ Play the video

3. Tests

  • Added: pages/media-library/data/utilities.test.js, 9 tests for getAllMediaCountRequest:
    • the caption picker's list becomes a query-attachments POST and never goes to media_type
    • video and [ 'image' ] stay on the REST route as media_type
    • a comma-separated string is split and trimmed
    • a mix of whole and exact types goes to query-attachments together
    • more than one whole type (image, video) goes to query-attachments, since media_type takes one value before WordPress 6.9
    • with no admin-ajax URL on the page, it falls back to the REST route's mime_type
    • no filter asks the REST route for everything
    • with no argument, it reads the open media frame's filter
    • with no argument, it reads the post_mime_type URL param
  • Changed: none.
  • Revert check: with the old behaviour (every list sent to the REST route as media_type), the tests fail (the two added after review fail on the first version of this PR too). For example, "counts exact MIME types through a query-attachments POST" gets wp/v2/media with media_type.

4. Risk flags

No risk flags fired on #2258. Nothing in the diff touched the watched areas.

5. Independent review

Finding Grade What I did
The first version sent exact types to the REST route as a mime_type list. That only works from WordPress 6.9, and GoDAM supports 6.5. On 6.5 to 6.8 the list is dropped and All Media shows every file Valid Exact types are now counted through admin-ajax query-attachments, which takes a list on every version (commits 6620909, ee80ed6). Checked on WordPress 6.8.11
Nothing tested the wiring from the open picker to the request Valid The helper now reads the frame or URL filter itself, and 2 tests cover it
A wildcard such as video/* would count everything Overstated No caller passes wildcards. query-attachments handles them the way the picker's own grid does
post_mime_type=all in the URL still gives a 400 By design Unchanged, and WordPress never builds that URL

6. Not verified

  • The Media Library page's own type filters (Documents, Spreadsheets, Archives) were not clicked through. They pass lists of exact types, the same input the Document picker sends.

  • On WordPress 6.8 I checked only the request this PR makes, not the GoDAM UI. On a throwaway 6.8.11 site with 1 caption file and 1 image:

    Request Result
    Old code: REST media_type=text/vtt,... 400
    First version of this PR: REST mime_type=text/vtt,... 200, total 2 (wrong, every file)
    This PR: query-attachments POST with text/vtt,... 200, total 1
  • Multisite.

7. Scope

None.

What changed

The All Media count used WordPress's wp/v2/media route with the picker's type list in media_type. That filter only takes whole types (image, video, text, application, audio) and answers 400 to exact file types such as text/vtt. The route takes a list of exact types (mime_type) only from WordPress 6.9.

Now a filter with exact file types is counted through admin-ajax query-attachments. That's the same query the picker's grid runs, so the count matches the files the picker lists, on every WordPress version GoDAM supports. A filter of more than one type goes there too. With no filter, or one whole type, the count still comes from the REST route as before.

  • pages/media-library/data/utilities.js: new getAllMediaCountRequest() picks the route and builds the request. admin-ajax only registers query-attachments for POST, so that request is a POST.
  • pages/media-library/redux/api/folders.js: getAllMediaCount uses it. The folder and Uncategorized counts send post_mime_type to GoDAM's own routes, which already accept exact types, so they are unchanged.

This has been broken since 1.10.0 (#1353). The branch starts from the 2.5.0 commit that main and develop share, so it can target either without a rebase.

The media sidebar's All Media count passed the picker's type filter to
wp/v2/media as media_type. That arg only accepts whole types (image,
video, text, application, audio), so pickers limited to exact MIME types
(the video editor's caption picker, the Document block) got a 400 and
showed All Media 0. Entries with a slash now go to mime_type; whole types
stay on media_type.

Fixes #2239
@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

🔍 WordPress Plugin Check Report

⚠️ Status: Passed with warnings

📊 Report

🎯 Total Issues ❌ Errors ⚠️ Warnings
13 0 13

⚠️ Warnings (13)

📁 readme.txt (2 warnings)
📍 Line 🔖 Check 💬 Message
0 mismatched_plugin_name Plugin name "GoDAM - Organize WordPress Media Library & File Manager with Unlimited Folders for Images, Videos & more" is different from the name declared in plugin header "GoDAM".
0 trademarked_term The plugin name includes a restricted term. Your chosen plugin name - "GoDAM - Organize WordPress Media Library & File Manager with Unlimited Folders for Images, Videos & more" - contains the restricted term "wordpress" which cannot be used at all in your plugin name.
📁 assets/build/css/main.css (1 warning)
📍 Line 🔖 Check 💬 Message
0 EnqueuedStylesScope This style is being loaded in all contexts.
📁 assets/src/libs/analytics.min.js (5 warnings)
📍 Line 🔖 Check 💬 Message
0 EnqueuedScriptsScope This script is being loaded in all frontend contexts.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880 (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/2026/10/28/hello-world/ (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/sample-page/ (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/demo-attachment-post/ (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead.
📁 assets/build/js/main.min.js (5 warnings)
📍 Line 🔖 Check 💬 Message
0 EnqueuedScriptsScope This script is being loaded in all frontend contexts.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880 (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/2026/10/28/hello-world/ (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/sample-page/ (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/demo-attachment-post/ (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead.

🤖 Generated by WordPress Plugin Check Action • Learn more about Plugin Check

The first version sent exact MIME types to wp/v2/media as mime_type, but
that route only takes a list there from WordPress 6.9, and GoDAM supports
6.5. On 6.5 to 6.8 the list was dropped and All Media showed every file.
A filter with exact MIME types now asks admin-ajax query-attachments, the
query the picker's own grid runs, which reads the list on every supported
version. No filter and whole types still use the REST route as before.

The helper now also reads the frame or URL filter itself, so the tests
cover the wiring from the open picker to the request.
admin-ajax.php registers core's query-attachments action for POST
requests only; a GET answered 400.
@subodhr258
subodhr258 marked this pull request as ready for review October 26, 2026 16:34
@KMchaudhary

Copy link
Copy Markdown
Collaborator

Code review — fix/all-media-count-mime-types

Reviewed the diff (3 files). Verified the good parts first: core WP does emit X-WP-Total for wp_ajax_query_attachments, so the count header holds for the ajax route; GoDAM's filter_media_library_by_taxonomy only scopes when query[media-folder] is sent (the count request doesn't), so there's no accidental folder-scoping; and post_mime_type as a comma string is handled by WP_Query on every supported version. Nice JSDoc and test coverage overall.

Five findings below, most severe first.

1. Multiple whole types still 400 → All Media 0 (correctness)

pages/media-library/data/utilities.js:264

The routing only sends types containing / to query-attachments. A filter of multiple whole types (e.g. a picker with type: [ 'image', 'video' ] and no exact MIME) falls through to the REST branch as media_type: [ 'image', 'video' ]. fetchBaseQuery serializes that array to the single string media_type=image,video, which the wp/v2/media enum rejects with 400 — the exact 400→0 symptom this PR fixes, left unhandled. A single whole type works only because ['video'] flattens to the scalar video. (Array support for media_type exists only from WP 6.9 and needs media_type[]=.) Pre-existing, but this is the function whose whole job is now to route around it.

2. new URL(undefined, …) builds a bogus ajax URL (correctness)

pages/media-library/data/utilities.js:267

If neither window.wp.ajax.settings.url nor window.ajaxurl is defined, new URL( undefined, location.href ) coerces to the string "undefined" and silently produces …/wp-admin/undefined. The POST then 404s, the response has no X-WP-Total, and the count parses back to 0 with no error surfaced. Unlikely in a normal wp.media picker context, but worth a guard.

3. Missing tests for the two riskiest cases (test-coverage)

pages/media-library/data/utilities.test.js

The suite covers single / exact / mixed-with-exact types, but not a filter of multiple whole types alone (would catch finding 1) nor the admin-ajax URL fallback (finding 2).

4. Obscure flatten round-trip (simplification)

pages/media-library/data/utilities.js:258

[].concat( type ?? [] ).join( ',' ).split( ',' ) flattens arrays and comma-strings via a join→split round-trip that also assumes no token contains a comma. [].concat( type ?? [] ).flatMap( ( t ) => String( t ).split( ',' ) ) reads more directly.

5. Builder coupled to global state via default param (altitude)

pages/media-library/data/utilities.js:257

Reading the current frame/URL filter through the default parameter means calling the builder with an explicit undefined means "auto-detect", not "no filter" — surprising for a reader. Detecting the filter at the call site (folders.js) and keeping this function pure would separate the two concerns.


Automated review (high effort). Findings 1 and 5 are plausible rather than confirmed — worth a second look, not necessarily blockers.

…x URL

A filter of more than one whole type (image and video) sent media_type=image,video,
which the REST route rejects before WordPress 6.9. Send it to query-attachments,
like exact MIME types. With no admin-ajax URL on the page, fall back to the REST
route's mime_type instead of posting to wp-admin/undefined.
@subodhr258

Copy link
Copy Markdown
Collaborator Author

Thanks @KMchaudhary. Fixed in 9ed4073; tests 9 of 9 pass, and the two new ones fail on the previous commit.

  1. More than one whole type. Partly right. On WordPress 6.9 and later, media_type=image,video is fine: core splits a comma list into an array and checks each item against the enum (that's why the original error read media_type[0] is not one of...). On 6.5 to 6.8, which we still support, media_type takes one value, so it would answer 400. Fixed: any filter with more than one type now goes to query-attachments, like exact types.
  2. No admin-ajax URL. Fixed: if neither wp.ajax.settings.url nor ajaxurl exists, it falls back to the REST route's mime_type instead of posting to wp-admin/undefined.
  3. Tests. Added "sends more than one whole type to query-attachments" and "falls back to the REST route's mime_type when the page has no admin-ajax URL".
  4. Flatten. Done: now flatMap( ( entry ) => String( entry ).split( ',' ) ).
  5. Default parameter reads the page's filter. Kept as is. It keeps folders.js to one call, and the docblock says what the default means. Happy to move the detection to the call site if you prefer it.

@KMchaudhary

Copy link
Copy Markdown
Collaborator

Re-review — commit 9ed40730

Re-reviewed after Count multi-type filters through query-attachments, and guard the ajax URL. Four of the five earlier findings are addressed, cleanly:

# Finding Status
1 Multiple whole types → 400 → All Media 0 ✅ Fixed — routing is now hasExactType || types.length > 1, so multi-type filters go to query-attachments
2 new URL(undefined, …) → bogus ajax URL ✅ Fixed — guarded on ajaxUrl, with a REST mime_type fallback
3 No tests for the two riskiest paths ✅ Fixed — added multi-whole-type and no-ajax-URL tests
4 Obscure join→split flatten ✅ Fixed — now flatMap((e) => String(e).split(','))
5 Builder couples to global state via default param Left as-is — a reasonable call; it keeps the single call site in folders.js trivial

One minor note, not blocking: the new !ajaxUrl fallback sends mime_type: types (a list), which wp/v2/media only accepts from WP 6.9 — so on 6.5–6.8 without an admin-ajax URL a multi/exact filter would still 400. That combination is effectively unreachable (admin pages always expose ajaxurl/wp.ajax), and the comment already flags it as 6.9+, so it reads as an intentional best-effort last resort. Single exact types still degrade fine there.

No new issues introduced. LGTM. 👍


Automated re-review (high effort).

@subodhr258
subodhr258 changed the base branch from develop to main October 28, 2026 04:41
@subodhr258
subodhr258 merged commit 1f8fcce into main Oct 28, 2026
10 checks passed
@subodhr258
subodhr258 deleted the fix/all-media-count-mime-types branch October 28, 2026 05:35
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.

Media picker shows All Media 0 and a failed request when limited to caption or document files

2 participants