WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#30520 closed defect (bug) (duplicate)

Adding mime-type-filter failed at Media

Reported by: hogetan Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.1
Component: Media Keywords: needs-testing
Focuses: administration Cc:
PR Number:

Description

Hello.

I posted this article at support forum but no reply.
https://wordpress.org/support/topic/adding-mime-type-filter-failed-at-media?replies=1

add_filter('post_mime_types', 'my_post_mime_types' );
function my_post_mime_types($post_mime_types) {
    $post_mime_types['application/zip'] = array(__('ZIP'), __('Manage ZIP'), _n_noop('ZIP <span class="count">(%s)</span>', 'ZIP <span class="count">(%s)</span>'));
    $post_mime_types['application/pdf'] = array( __( 'PDFs' ), __( 'Manage PDFs' ), _n_noop( 'PDF <span class="count">(%s)</span>', 'PDFs <span class="count">(%s)</span>' ) );
    return $post_mime_types;
}

(This code is from someone's code. I get this code by google)
This code can filter out at Media only grid mode.
Media has 2 views, list and grid. I expect this code can work out both mode.

The reason why this code fails at list mode is list mode sends filtering data with html escaping.

At list view, GET query has double escaped slash, then it failed.
"/wp-admin/upload.php?mode=list&attachment-filter=post_mime_type%3Aapplication%252Fzip"

"%25" means "%"
At HTML, "application/zip" was encoded to "application%2Fzip"
Then sending this query, it will be "post_mime_type%3Aapplication%252Fzip""

At grid view, POST query string has correct slash
"query[post_mime_type]:application/zip"

I added a patch to prevent this double escaping.

--- includes/class-wp-media-list-table.php      2014-11-21 14:04:40.000000000 +0900
+++ ../wp-admin_/includes/class-wp-media-list-table.php 2014-11-21 13:36:18.000000000 +0900
@@ -70,7 +70,7 @@
                        if ( !empty( $_GET['attachment-filter'] ) && strpos( $_GET['attachment-filter'], 'post_mime_type:' ) === 0 && wp_match_mime_types( $mime_type, str_replace( 'post_mime_type:', '', $_GET['attachment-filter'] ) ) )
                                $selected = ' selected="selected"';
                        if ( !empty( $num_posts[$mime_type] ) )
-                               $type_links[$mime_type] = '<option value="post_mime_type:' . $mime_type. '"' . $selected . '>' . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), number_format_i18n( $num_posts[$mime_type] )) . '</option>';
+                               $type_links[$mime_type] = '<option value="post_mime_type:' . urlencode( $mime_type ) . '"' . $selected . '>' . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), number_format_i18n( $num_posts[$mime_type] )) . '</option>';
                }
                $type_links['detached'] = '<option value="detached"' . ( $this->detached ? ' selected="selected"' : '' ) . '>' . sprintf( _nx( 'Unattached (%s)', 'Unattached (%s)', $total_orphans, 'detached files' ), number_format_i18n( $total_orphans ) ) . '</option>';

I guess this should be fixed at core file.

Change History (3)

#1 @johnbillion
5 years ago

  • Component changed from General to Media
  • Keywords needs-testing needs-patch added
  • Version changed from trunk to 3.1

#2 @mdgl
5 years ago

Looks like a duplicate of #30123.

#3 @SergeyBiryukov
5 years ago

  • Keywords needs-patch removed
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #30123.

Note: See TracTickets for help on using tickets.