Make WordPress Core

Ticket #10329: 10329.3.diff

File 10329.3.diff, 7.0 KB (added by takaitra, 17 years ago)
  • 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        // Order ASC or DESC based on the order and orderby arguments
     284        if ( $show_fullname ) {
     285        // Full names will be looked up via user meta-data and sorted later
     286        // order by NULL avoids the implicit sorting of the GROUP BY
     287        $author_sort = 'ORDER BY NULL';
     288    } else {
     289                if ( $orderby == 'count' ) {
     290                        $author_sort = "ORDER BY author_count $order, author_name";
     291                } else {
     292                        $author_sort = "ORDER BY author_name $order";
     293                }
     294        }
     295
     296        // Limit the results based on the min_count argument
     297        $min_count = absint( $min_count );
     298        if ( $hide_empty )
     299                $min_count = max(1, $min_count);
     300        if ( $min_count ) {
     301                $author_having = "HAVING post_count >= $min_count";
    279302        } else {
    280                 $authors = array();
     303                $author_having = '';
    281304        }
    282305
    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;
     306        // Limit the results based on the number argument
     307        $number = intval( $number );
     308        if ( $number > 0 ) {
     309                $author_limit = "LIMIT $number";
     310        } else {
     311                $author_limit = '';
     312        }
    286313
     314        // Join to exclude authors from other blogs. Only needed if hide_empty is
     315        // false or and no min_count is set (the INNER JOIN to $wpdb->posts would
     316        // already exclude non-authors in both cases)
     317        if ( !$hide_empty && !$min_count ) {
     318                $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'";
     319        } else {
     320                $author_cap_join = '';
     321        }
     322       
     323        $author_select = "$wpdb->users.ID, $wpdb->users.user_nicename, $wpdb->users.display_name AS author_name";
     324       
     325        // join on posts only when necessary
     326        if ( $hide_empty || $min_count || $optioncount || $orderby == 'count' ) {
     327                $author_select .= ", COUNT($wpdb->posts.ID) as post_count";
     328                $author_posts_join = "JOIN $wpdb->posts ON $wpdb->posts.post_author = $wpdb->users.ID";
     329                $author_where = "($wpdb->posts.post_type = 'post' OR $wpdb->posts.post_type IS NULL) AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status IS NULL)";
     330                $author_group_by = "GROUP BY $wpdb->users.ID";
     331                // a left join is needed if we're only interested in fetching numbers of posts
     332                if ( !$min_count && !$hide_empty )
     333                        $author_posts_join = 'LEFT ' . $author_posts_join;
     334        } else {
     335                $author_posts_join = '';
     336                $author_where = '';
     337                $author_group_by = '';
     338        }
     339       
     340        if ( $exclude_admin )
     341                $author_where .= ( $author_where ? ' AND ' : '' ) . "$wpdb->users.user_login <> 'admin'";
     342       
     343        $author_where = $author_where ? ( 'WHERE ' . $author_where ) : '';
     344       
     345        // Query the list of users
     346        $sql = "
     347                SELECT $author_select
     348                FROM $wpdb->users
     349                $author_posts_join
     350                $author_name_join
     351                $author_cap_join
     352                $author_where
     353                $author_group_by
     354                $author_having
     355                $author_sort
     356                $author_limit
     357                ";
     358
     359        $authors = $wpdb->get_results($sql);
     360
     361        if ( $show_fullname ) {
     362        // Lookup first and last name via cached user meta-data
     363        foreach ( (array) $authors as $author ) {
     364            $userdata = get_userdata( $author->ID );
     365            if ( !empty( $userdata->first_name ) || !empty( $userdata->last_name ) ) {
     366                                $author->author_name = $userdata->first_name . ' ' . $userdata->last_name;
     367                                trim ( $author->author_name );
     368            }
     369        }
     370                // Sort the objects
     371                if ( $orderby == 'name' ) {
     372                        usort( $authors, '_wp_list_authors_usort_callback_name' );
     373                } else {
     374                        usort( $authors, '_wp_list_authors_usort_callback_count' );
     375                }
     376                if ( $order == 'DESC' ) {
     377                        $authors = array_reverse( $authors );
     378                }
     379    }
     380
    287381        foreach ( (array) $authors as $author ) {
    288382
    289383                $link = '';
    290384
    291                 $author = get_userdata( $author->ID );
    292                 $posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0;
    293                 $name = $author->display_name;
     385                $posts = $author->post_count;
     386                $name = $author->author_name;
    294387
    295                 if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') )
    296                         $name = "$author->first_name $author->last_name";
    297 
    298388                if( !$html ) {
    299389                        if ( $posts == 0 ) {
    300390                                if ( ! $hide_empty )
    … …  
    308398
    309399                if ( !($posts == 0 && $hide_empty) && 'list' == $style )
    310400                        $return .= '<li>';
    311                 if ( $posts == 0 ) {
     401                if ( $optioncount && $posts == 0 ) {
    312402                        if ( ! $hide_empty )
    313403                                $link = $name;
    314404                } 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>';
     405                        $link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->author_name) ) . '">' . $name . '</a>';
    316406
    317407                        if ( (! empty($feed_image)) || (! empty($feed)) ) {
    318408                                $link .= ' ';
    … …  
    358448        echo $return;
    359449}
    360450
     451function _wp_list_authors_usort_callback_name($a, $b) {
     452    return strcasecmp( $a->author_name, $b->author_name );
     453}
     454
     455function _wp_list_authors_usort_callback_count($a, $b) {
     456        return $a->post_count - $b->post_count;
     457}
     458
    361459?>