• Hi,

    your plug-in is fantastic and I would like to make a suggestion.

    I renamed the labels of some MIME type like 7z, zip and rar to “File Archive” and hoped that only one grouped filter would appear in the media list, but I’ve got three.

    Could you change it so that same labels are grouped?

    Thanks in advance.

    http://wordpress.org/plugins/enhanced-media-library/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author webbistro

    (@webbistro)

    Hello,

    Thank you! Great suggestion. I will check in a bit how we can implement this.

    Nadia

    Thread Starter Jezze

    (@jezze)

    I was able to implement the feature in your plugin. I changed the function wpuxss_eml_post_mime_types() in mime-types.php to this.

    function wpuxss_eml_post_mime_types( $post_mime_types )
    {
      $wpuxss_eml_mimes = get_option('wpuxss_eml_mimes');
    
      if ( !empty($wpuxss_eml_mimes) )
      {
        $mineGroups = array();
        foreach ( $wpuxss_eml_mimes as $type => $mime )
        {
          if ( $mime['filter'] == 1 )
          {
            $key = $mime['singular'];
            if ( !isset($mineGroups[$key]) )
            {
              $mineGroups[$key] = array(
                  'singular' => $mime['singular'],
                  'plural' => $mime['plural'],
                  'mime' => $mime['mime'],
              );
            }
            else
            {
              $mineGroups[$key]['mime'] .= ',' . $mime['mime'];
            }
          }
        }
    
        if ( !empty($mineGroups) )
        {
          foreach ( $mineGroups as $type => $mineGroup )
          {
            $post_mime_types[$mineGroup['mime']] = array(
              __($mineGroup['singular']),
              __('Manage ' . $mineGroup['singular']),
              _n_noop($mineGroup['singular'] . ' <span class="count">(%s)</span>', $mineGroup['plural'] . ' <span class="count">(%s)</span>')
            );
          }
        }
      }
    
      return $post_mime_types;
    }

    Basically it creates comma separated MIME-type keys in $post_mime_types, which should work according to this blog http://ageekandhisblog.com/wordpress-how-to-add-more-upload-categories-filter/

    But unfortunately, there seems to be a bug (?) in /wp-admin/includes/class-wp-media-list-table.php in function get_views() and I had to change a few lines, which would be a quite bad requirement for the plugin. Anyway, I changed the following lines.

    $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));

    to

    $post_mime_types_piped = array_keys($post_mime_types);
    foreach ($post_mime_types_piped as $key => $value)
      $post_mime_types_piped[$key] = str_replace(",", "|", $value);
    
    $matches = wp_match_mime_types($post_mime_types_piped, array_keys($_num_posts));

    and

    if ( !empty( $num_posts[$mime_type] ) )
      $type_links[$mime_type] = "<a href='upload.php?post_mime_type=$mime_type'$class>" . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), number_format_i18n( $num_posts[$mime_type] )) . '</a>';

    to

    $mime_type_piped = str_replace(',', '|', $mime_type);
    
    if ( !empty( $num_posts[$mime_type_piped] ) )
      $type_links[$mime_type] = "<a href='upload.php?post_mime_type=$mime_type'$class>" . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type_piped] ), number_format_i18n( $num_posts[$mime_type_piped] )) . '</a>';

    I hope this will help you.

    Plugin Author webbistro

    (@webbistro)

    Hello Jezze,

    Thank you very much for your research! I’ll give this a careful look as soon as possible. I believe it will be useful for many.

    Nadia

    Note you can handle the comma vs pipe difference directly within the function, by choosing the character based on context. See my implementation below (implemented as a standalone filter):

    add_filter( 'post_mime_types', 'group_post_mime_types', 99 );
    function group_post_mime_types( $post_mime_types ) {
    
    	// WordPress function get_views() expects pipe separators,
    	// whereas media upload dropdown filter expects commas.
    	global $pagenow;
    	$glue = 'upload.php' == $pagenow ? '|' : ',';     
    
    	$mime_types_grouped_by_name = array();
    	foreach( $post_mime_types as $mime => &$labels ){
    		$mime_types_grouped_by_name[ $labels[0] ][ $mime ] = $labels;
    	}
    	$new_post_mime_types = array();
    	foreach ( $mime_types_grouped_by_name as $name => &$mime_labels ) {
    		$new_post_mime_types[ implode( $glue, array_keys( $mime_labels )) ] = reset( $mime_labels );
    	}
    	return $new_post_mime_types;
    }

    I’ve tested and this works well in v3.9.1.

    Plugin Author webbistro

    (@webbistro)

    Thanks guys!

    I’ve completely forgotten this. Will test and add this to the next release. (Should not be forgotten again, I have great ToDo tool now :))

    Nadia

    I noticed something in the old-style media upload screen. (Using WP v3.9.1, but some plugins still employ the old-style media uploader.)

    With a comma separator, filtering works, but the count for grouped mime types at the top is always zero, and PHP notices are thrown from wp-admin/includes/media.php on line 2316.

    With a pipe separator, no notices are thrown, but no media items appear in the list (yet — strangely enough — the count is correct).

    I was able to get the correct count and avoid the notices by using a pipe separator on the old media upload form…

    // WordPress v3.9.1 function get_views() and old-style upload form expect
    // pipe separators, whereas media upload dropdown filter expects commas.
    $glue = in_array( $pagenow, array( 'upload.php', 'media-upload.php' ) ) ? '|' : ',';

    … and get the media items to appear by adding this filter:

    global $pagenow;
    if( 'media-upload.php' == $pagenow )
    	add_filter( 'pre_get_posts', 'old_media_upload_form_fix', 99 );
    function old_media_upload_form_fix( $wp_query ) {
    	if ( $query_var = $wp_query->get( 'post_mime_type' ) )
    		$wp_query->set( 'post_mime_type', str_replace( '|', ',', $query_var ) );
    	return $wp_query;
    }

    Plugin Author webbistro

    (@webbistro)

    Hello @smhmic,

    Thanks for your research! Could you please name some of the plugins that use old-style media uploader? Thanks in advance.

    Nadia

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘grouping MIME type labels’ is closed to new replies.