Opened 12 days ago
Closed 12 days ago
#66454 closed defect (bug) (fixed)
An extra comma in a post_mime_type string removes the MIME type filter
| Reported by: | ekamran | Owned by: | SergeyBiryukov |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.2 |
| Component: | Media | Version: | 2.5 |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
Description
wp_post_mime_type_where() splits a comma-separated string of MIME types with explode(). An extra comma leaves an empty item, and the empty string is one of the wildcards in array( '', '%', '%/%' ), so the function returns no clause at all.
As a result, 'image/jpeg,', 'image/jpeg, ', ',image/jpeg', 'image/jpeg,,application/pdf' and 'image,' match every MIME type instead of only the listed ones. This affects WP_Query and get_posts() with post_mime_type, wp_count_attachments(), and the media modal's query-attachments Ajax request, which passes query[post_mime_type] on to WP_Query.
I reproduced it on WordPress 7.1.4, which has the same function as trunk, with WP-CLI eval-file on PHP 8.4 and four attachments (image/jpeg, image/png, application/pdf, video/mp4):
get_posts( post_mime_type => ... ) 'image/jpeg' found: image/jpeg 'image/jpeg,' found: image/jpeg, image/png, application/pdf, video/mp4 'image/jpeg, ' found: image/jpeg, image/png, application/pdf, video/mp4 ',image/jpeg' found: image/jpeg, image/png, application/pdf, video/mp4 'image/jpeg,,application/pdf' found: image/jpeg, image/png, application/pdf, video/mp4 'image,' found: image/jpeg, image/png, application/pdf, video/mp4 wp_count_attachments( 'image/jpeg,' ) application/pdf 1, image/jpeg 1, image/png 1, video/mp4 1 wp_ajax_query_attachments() with query[post_mime_type] = 'image/jpeg,' image/jpeg, image/png, application/pdf, video/mp4
Proposed change: after splitting the string, drop the empty items, so only the listed MIME types are used. An empty string, a string of only commas and spaces, and an explicit % still match every MIME type.
One question: an empty value in an array, as in array( 'image/jpeg', '' ), also matches every MIME type. The proposed change leaves arrays as they are. Should an empty array value be skipped too?
wp_post_mime_type_where()splits a comma-separated string of MIME types withexplode(). An extra comma left an empty item, and the empty string is one of the wildcards, so a value likeimage/jpeg,returned no clause and matched every MIME type. This affectedWP_Queryandget_posts()withpost_mime_type,wp_count_attachments()and the media modal'squery-attachmentsAjax request.What changes
wp_post_mime_type_where(): after a string is split, empty items are removed witharray_diff(). A value of'0'is not removed.%still match every MIME type.Testing
test_ignores_empty_items_in_comma_separated_string(5 cases: trailing comma, trailing comma and space, leading comma, double comma, MIME type group with a trailing comma) andtest_matches_every_mime_type(3 cases that must keep matching everything). On trunk, the 5 cases of the first test fail and the 3 cases of the second pass. With this change all pass.tests/post/wpPostMimeTypeWhere.phpand thepostgroup pass on single site and multisite, and themediaandquerygroups pass on single site. PHPCS reports no new issues on the changed files. A full PHPStan run passes.get_posts(),wp_count_attachments()andwp_ajax_query_attachments()withimage/jpeg,return every attachment before this change and only the JPEG after it.Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code and Codex
Used for: Investigation, code, tests, local testing and PR wording. I reviewed the reasoning, the code and the test results, and I take responsibility for the contribution.