Make WordPress Core

Ticket #10329: 10329.2.diff

File 10329.2.diff, 6.7 KB (added by Denis-de-Bernardy, 17 years ago)
  • wp-includes/author-template.php

     
    247247 * or as a string.</li>
    248248 * <li>html (bool) (true): Whether to list the items in html for or plaintext.
    249249 * </li>
     250 * <li>orderby (string) ('name'): The sort order of the list.</li>
     251 * <li>order (string) ('ASC'): Sort order for authors (either ascending or
     252 * descending).</li>
     253 * <li>number (int) (NULL): Sets the number of authors to display. This
     254 * causes the SQL LIMIT value to be defined. Default to no LIMIT.</li>
     255 * <li>min_count (int) (NULL): Sets a minimum post count threshold. Defaults to
     256 * no minimum.</li>
    250257 * </ul>
    251258 *
    252259 * @link http://codex.wordpress.org/Template_Tags/wp_list_authors
    … …  
    255262 * @return null|string The output, if echo is set to false.
    256263 */
    257264function wp_list_authors($args = '') {
    258         global $wpdb;
     265        global $wpdb, $blog_id;
    259266
    260267        $defaults = array(
    261268                'optioncount' => false, 'exclude_admin' => true,
    262269                'show_fullname' => false, 'hide_empty' => true,
    263270                'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
    264                 'style' => 'list', 'html' => true
     271                'style' => 'list', 'html' => true, 'orderby' => 'name',
     272                'order' => 'ASC', 'number' => NULL, 'min_count' => NULL
    265273        );
    266274
    267275        $r = wp_parse_args( $args, $defaults );
    268276        extract($r, EXTR_SKIP);
    269277        $return = '';
    270278
    271         /** @todo Move select to get_authors(). */
    272         $users = get_users_of_blog();
    273         $author_ids = array();
    274         foreach ( (array) $users as $user )
    275                 $author_ids[] = $user->user_id;
    276         if ( count($author_ids) > 0  ) {
    277                 $author_ids = implode(',', $author_ids );
    278                 $authors = $wpdb->get_results( "SELECT ID, user_nicename from $wpdb->users WHERE ID IN($author_ids) " . ($exclude_admin ? "AND user_login <> 'admin' " : '') . "ORDER BY display_name" );
     279        if ( empty($id) )
     280                $id = (int) $blog_id;
     281        $blog_prefix = $wpdb->get_blog_prefix($id);
     282
     283        // Generate appropriate SQL selections and conditions based on the arguments
     284        if ( $show_fullname ) {
     285                // Select first and last name if they exist. Otherwise, show the display name.
     286                $author_name_field = "CASE
     287                WHEN TRIM(CONCAT_WS(' ', COALESCE(meta_first_name.meta_value, ''), COALESCE(meta_last_name.meta_value, ''))) <> ''
     288                THEN
     289                        TRIM(CONCAT_WS(' ', COALESCE(meta_first_name.meta_value, ''), COALESCE(meta_last_name.meta_value, '')))
     290                ELSE
     291                        $wpdb->users.display_name
     292                END";
     293                // Joins to support selection of first and last name
     294                $author_name_join = "
     295                LEFT JOIN $wpdb->usermeta AS meta_first_name ON meta_first_name.user_id = $wpdb->users.ID AND meta_first_name.meta_key = 'first_name'
     296                LEFT JOIN $wpdb->usermeta AS meta_last_name ON meta_last_name.user_id = $wpdb->users.ID AND meta_last_name.meta_key = 'last_name'
     297                ";
    279298        } else {
    280                 $authors = array();
     299                // Select display name. No usermeta join needed for first and last name.
     300                $author_name_field = "$wpdb->users.display_name";
     301                $author_name_join = "";
    281302        }
    282303
    283         $author_count = array();
    284         foreach ( (array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row )
    285                 $author_count[$row->post_author] = $row->count;
     304        // Order ASC or DESC based on the order and orderby arguments
     305        if ( $orderby == 'count' ) {
     306                $author_sort = "ORDER BY author_count $order, author_name";
     307        } else {
     308                $author_sort = "ORDER BY author_name $order";
     309        }
    286310
     311        // Limit the results based on the min_count argument
     312        $min_count = absint( $min_count );
     313        if ( $hide_empty )
     314                $min_count = max(1, $min_count);
     315        if ( $min_count ) {
     316                $author_having = "HAVING author_count >= $min_count";
     317        } else {
     318                $author_having = '';
     319        }
     320
     321        // Limit the results based on the number argument
     322        $number = intval( $number );
     323        if ( $number > 0 ) {
     324                $author_limit = "LIMIT $number";
     325        } else {
     326                $author_limit = '';
     327        }
     328
     329        // Join to exclude authors from other blogs. Only needed if hide_empty is
     330        // false or and no min_count is set (the INNER JOIN to $wpdb->posts would
     331        // already exclude non-authors in both cases)
     332        if ( !$hide_empty && !$min_count ) {
     333                $author_cap_join = "JOIN $wpdb->usermeta AS meta_capabilities ON meta_capabilities.user_id = $wpdb->users.ID AND meta_capabilities.meta_key = '{$blog_prefix}capabilities'";
     334        } else {
     335                $author_cap_join = '';
     336        }
     337       
     338        $author_select = "$wpdb->users.ID, $wpdb->users.user_nicename, $author_name_field as author_name";
     339       
     340        // join on posts only when necessary
     341        if ( $hide_empty || $min_count || $optioncount || $orderby == 'count' ) {
     342                $author_select .= ", COUNT($wpdb->posts.ID) as author_count";
     343                $author_posts_join = "JOIN $wpdb->posts ON $wpdb->posts.post_author = $wpdb->users.ID";
     344                $author_where = "$wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status = 'publish'";
     345                $author_group_by = "GROUP BY $wpdb->users.ID";
     346                // a left join is needed if we're only interested in fetching numbers of posts
     347                if ( !$min_count && !$hide_empty )
     348                        $author_posts_join = 'LEFT ' . $author_posts_join;
     349        } else {
     350                $author_posts_join = '';
     351                $author_where = '';
     352                $author_group_by = '';
     353        }
     354       
     355        if ( $exclude_admin )
     356                $author_where .= ( $author_where ? ' AND ' : '' ) . "$wpdb->users.user_login <> 'admin'";
     357       
     358        $author_where = $author_where ? ( 'WHERE ' . $author_where ) : '';
     359       
     360        // Query the list of users
     361        $blog_prefix = $wpdb->get_blog_prefix($blog_id);
     362        $sql = "
     363                SELECT $author_select
     364                FROM $wpdb->users
     365                $author_posts_join
     366                $author_name_join
     367                $author_cap_join
     368                $author_where
     369                $author_group_by
     370                $author_having
     371                $author_sort
     372                $author_limit
     373                ";
     374
     375        $authors = $wpdb->get_results($sql);
     376
    287377        foreach ( (array) $authors as $author ) {
    288378
    289379                $link = '';
    290380
    291                 $author = get_userdata( $author->ID );
    292                 $posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0;
    293                 $name = $author->display_name;
     381                $posts = $author->author_count;
     382                $name = $author->author_name;
    294383
    295                 if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') )
    296                         $name = "$author->first_name $author->last_name";
    297 
    298384                if( !$html ) {
    299385                        if ( $posts == 0 ) {
    300386                                if ( ! $hide_empty )
    … …  
    312398                        if ( ! $hide_empty )
    313399                                $link = $name;
    314400                } else {
    315                         $link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';
     401                        $link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->author_name) ) . '">' . $name . '</a>';
    316402
    317403                        if ( (! empty($feed_image)) || (! empty($feed)) ) {
    318404                                $link .= ' ';