WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#30788 closed defect (bug) (fixed)

Media library views: inconsistent behaviour with mime type groups

Reported by: mdgl Owned by: wonderboymusic
Milestone: 4.2 Priority: normal
Severity: normal Version: 4.0
Component: Media Keywords: has-patch
Focuses: javascript Cc:

Description

Custom media type groups defined using the post_mime_types filter are not displayed as filter options by the media library "list" view if the group contains multiple MIME types separated by a comma. Such groups are displayed correctly by the "grid" and other JavaScript views.

For example, if you add a custom media group Documents for MIME types text/plain,application/pdf, this is not listed as a filter option in "list" mode but does appear in the "grid" view.

If there is just a single MIME type in the group (e.g. text/plain) it appears correctly in both views.

It appears that function wp_match_mime_types() is not correctly matching against such comma-separated MIME type values.

Further inconsistencies occur in the naming and display of the groups. In "list" view, each group name is appropriately pluralised and suffixed with the count of matching items but this does not occur in the JavaScript views. In addition, the filter to display all items is named All in "list" view but All media items in the JavaScript views (English translation).

Related #22514, #30123.

Attachments (4)

ticket-30788.php (513 bytes) - added by mdgl 3 years ago.
Plugin Ticket 30788
30788.diff (2.2 KB) - added by mdgl 3 years ago.
First attempt at a fix
30788.2.diff (2.2 KB) - added by mdgl 3 years ago.
Corrected patch (sorry!)
30788.3.diff (2.2 KB) - added by mdgl 3 years ago.
Updated patch

Download all attachments as: .zip

Change History (13)

#1 @DrewAPicture
3 years ago

  • Focuses javascript added
  • Keywords reporter-feedback added

Hi mdgl, thanks for the report. Would it be possible for you to post a code example we could use to reproduce this issue?

@mdgl
3 years ago

Plugin Ticket 30788

#2 @mdgl
3 years ago

  • Keywords reporter-feedback removed

Sample code for a plugin "Ticket 30788" attached. Activate and ensure you have at least one text or PDF file uploaded.

Browsing the media library in "grid" mode, or through the "Add Media" button on the post edit screen displays an additional filter category "Documents" that displays all text and PDF files.

The additional filter category does not appear when browsing the media library in "list" mode.

#3 @nacin
3 years ago

  • Version changed from 4.1 to 4.0

So wp_match_mime_types() is not matching correctly, but some other code is? Has this worked anywhere in WP prior to the addition of the grid in 4.0?

#4 @mdgl
3 years ago

So wp_match_mime_types() is not matching correctly, but some other code is?

Some other code??? Welcome to the wonderful world of WordPress media handling :-)

I ran up fresh copies of WP3.5.2 and WP3.9.3 today and the problem still occurs in both of those releases, although at that time the mime type groups are just displayed in a simple list rather than a drop down on the Media Library screen. Again the JavaScript code works correctly, as does the PHP if there is only a single MIME type in the group.

@mdgl
3 years ago

First attempt at a fix

#5 @mdgl
3 years ago

  • Keywords 2nd-opinion added

Patch uploaded which allows wp_match_mime_types() to deal with an array of comma-separated MIME types (effectively a double nesting issue). This appears to fix the problem but the code here is quite convoluted and I would like a second opinion on the appropriateness of this solution. It is also unclear to me why the original code added a third regular expression to match the main MIME type pattern un-anchored, so I have removed this.

Also, because the screen filter selection value may now include a comma, I had to revert to the original solution presented in #30123, namely using esc_attr() rather than sanitize_mime_type() when preparing links for the filter dropdown. This seems more correct in any case.

Last edited 3 years ago by mdgl (previous) (diff)

@mdgl
3 years ago

Corrected patch (sorry!)

@mdgl
3 years ago

Updated patch

#6 @mdgl
3 years ago

I did a lot more testing and code review on this today and am now much more confident in the (updated) patch.

It turns out that wp_match_mime_types() is also used to match what are effectively file names and types by function wp_mime_type_icon() so I have added back the third (and un-anchored) regular expression to retain backwards compatibility. The existing code in wp_mime_type_icon() looks a bit iffy to me and likely to return unexpected results in some situations, but that should be the subject of another ticket.

I have not addressed the issue of inconsistencies in labelling and numbering between the PHP and JavaScript views. Again, those are perhaps better dealt with on a separate ticket.

#7 @mdgl
3 years ago

  • Keywords has-patch added

#8 @wonderboymusic
3 years ago

  • Keywords 2nd-opinion removed
  • Milestone changed from Awaiting Review to 4.2

#9 @wonderboymusic
3 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 31042:

Improve the handling of comma-separated mime-types in wp_match_mime_types(), particularly as pertains to the mime-type selector on the Media list table screen.

Props mdgl.
Fixes #30788.

Note: See TracTickets for help on using tickets.