Make WordPress Core

Ticket #23421: 23421.diff

File 23421.diff, 4.8 KB (added by jtsternberg, 11 years ago)

Allow sortable columns via the show_column_sortable taxonomy argument.

  • src/wp-includes/taxonomy.php

     
    5656                'public' => true,
    5757                'show_ui' => true,
    5858                'show_admin_column' => true,
     59                'show_column_sortable' => true,
    5960                '_builtin' => true,
    6061        ) );
    6162
     
    6667                'public' => true,
    6768                'show_ui' => true,
    6869                'show_admin_column' => true,
     70                'show_column_sortable' => true,
    6971                '_builtin' => true,
    7072        ) );
    7173
     
    129131add_action( 'init', 'create_initial_taxonomies', 0 ); // highest priority
    130132
    131133/**
     134 * Performs column sorting for taxonomies registered with 'show_column_sortable'.
     135 * @since  4.0.0
     136 * @param array    $clauses  The list of clauses for the query.
     137 * @param WP_Query $wp_query The WP_Query instance (passed by reference).
     138 *
     139 * @return string Modified clauses
     140 */
     141function peform_sortable_taxonomy_columns_query( $clauses, $wp_query ) {
     142        global $wpdb;
     143
     144        if ( ! isset( $wp_query->query['orderby'] ) ) {
     145                return $clauses;
     146        }
     147
     148        $taxonomies = get_sortable_taxonomies( $wp_query->query['post_type'] );
     149
     150        if ( empty( $taxonomies ) || ! is_array( $taxonomies ) ) {
     151                return $clauses;
     152        }
     153
     154        $sortable_taxonomies = array();
     155        foreach ( $taxonomies as $taxonomy ) {
     156                if ( 'category' == $taxonomy )
     157                        $column_key = 'categories';
     158                elseif ( 'post_tag' == $taxonomy )
     159                        $column_key = 'tags';
     160                else
     161                        $column_key = 'taxonomy-' . $taxonomy;
     162
     163                $sortable_taxonomies[ $taxonomy ] = $column_key;
     164        }
     165
     166        $key = array_search( $wp_query->query['orderby'], $sortable_taxonomies, true );
     167
     168        if ( false === $key ) {
     169                return $clauses;
     170        }
     171
     172        $clauses['join'] .= "
     173                LEFT OUTER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id
     174                LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id)
     175                LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
     176        ";
     177        $clauses['where'] = $wpdb->prepare( " AND taxonomy = '%s'", $key );
     178        $clauses['groupby'] = "object_id";
     179        $clauses['orderby']  = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC) ";
     180        $clauses['orderby'] .= ( 'ASC' == strtoupper( $wp_query->get('order') ) ) ? 'ASC' : 'DESC';
     181
     182        return $clauses;
     183}
     184add_filter( 'posts_clauses', 'peform_sortable_taxonomy_columns_query', 10, 2 );
     185
     186/**
    132187 * Get a list of registered taxonomy objects.
    133188 *
    134189 * @since 3.0.0
     
    337392                'show_in_nav_menus'     => null,
    338393                'show_tagcloud'         => null,
    339394                'show_admin_column'     => false,
     395                'show_column_sortable'  => false,
    340396                'meta_box_cb'           => null,
    341397                'capabilities'          => array(),
    342398                'rewrite'               => true,
     
    39313987                wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) );
    39323988
    39333989        return $parent;
    3934 }
    3935  No newline at end of file
     3990}
     3991
     3992/**
     3993 * Get a list of all taxonomies registered as sortable.
     3994 * Passes through the "manage_taxonomies_for_{$post_type}_sortable_columns" filter.
     3995 *
     3996 * @since  4.0.0
     3997 * @param  string $post_type Post type to retrieve Taxonomies
     3998 *
     3999 * @return array Array of sortable taxonomies
     4000 */
     4001function get_sortable_taxonomies( $post_type ) {
     4002        $taxonomies = get_object_taxonomies( $post_type, 'objects' );
     4003        $taxonomies = wp_filter_object_list( $taxonomies, array( 'show_admin_column' => true, 'show_column_sortable' => true ), 'and', 'name' );
     4004
     4005        /**
     4006         * Filter the sortable taxonomy columns in the Posts list table.
     4007         *
     4008         * The dynamic portion of the hook name, $post_type, refers to the post
     4009         * type slug.
     4010         *
     4011         * @since 3.5.0
     4012         *
     4013         * @param array  $taxonomies Array of taxonomies to enable sortable columns for.
     4014         * @param string $post_type  The post type.
     4015         */
     4016        $taxonomies = apply_filters( "manage_taxonomies_for_{$post_type}_sortable_columns", $taxonomies );
     4017        $taxonomies = array_filter( $taxonomies, 'taxonomy_exists' );
     4018
     4019        return $taxonomies;
     4020}
  • src/wp-admin/includes/class-wp-posts-list-table.php

     
    346346        }
    347347
    348348        function get_sortable_columns() {
    349                 return array(
     349                $sortables = array(
    350350                        'title'    => 'title',
    351351                        'parent'   => 'parent',
    352352                        'comments' => 'comment_count',
    353353                        'date'     => array( 'date', true )
    354354                );
     355
     356                $taxonomies = get_sortable_taxonomies( $this->screen->post_type );
     357
     358                if ( ! empty( $taxonomies ) && is_array( $taxonomies ) ) {
     359                        foreach ( $taxonomies as $taxonomy ) {
     360                                if ( 'category' == $taxonomy )
     361                                        $column_key = 'categories';
     362                                elseif ( 'post_tag' == $taxonomy )
     363                                        $column_key = 'tags';
     364                                else
     365                                        $column_key = 'taxonomy-' . $taxonomy;
     366
     367                                $sortables[ $column_key ] = $column_key;
     368                        }
     369                }
     370
     371                return $sortables;
    355372        }
    356373
    357374        function display_rows( $posts = array(), $level = 0 ) {