WordPress.org

Make WordPress Core

Ticket #10329: 10329.diff

File 10329.diff, 6.0 KB (added by takaitra, 8 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        // 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 = intval( $min_count );
     313        if ( $min_count > 0 ) {
     314                $author_having = "HAVING author_count >= $min_count";
     315        } else {
     316                $author_having = '';
     317        }
     318
     319        // Limit the results based on the number argument
     320        $number = intval( $number );
     321        if ( $number > 0 ) {
     322                $author_limit = "LIMIT $number";
     323        } else {
     324                $author_limit = '';
     325        }
     326
     327        // Join to exclude authors from other blogs. Only needed if hide_empty is
     328        // false (the INNER JOIN to $wpdb->posts would already exclude other blogs)
     329        if ( !$hide_empty ) {
     330                $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'";
     331        } else {
     332                $author_cap_join = '';
     333        }
     334
     335        // Query the list of users
     336        $blog_prefix = $wpdb->get_blog_prefix($blog_id);
     337        $authors = $wpdb->get_results("
     338                SELECT $wpdb->users.ID, $wpdb->users.user_nicename, COUNT($wpdb->posts.ID) as author_count, $author_name_field as author_name
     339                FROM $wpdb->users
     340                " . ( !$hide_empty ? "LEFT " : '' ) . "JOIN $wpdb->posts ON $wpdb->posts.post_author = $wpdb->users.ID
     341                $author_name_join
     342                $author_cap_join
     343                WHERE ($wpdb->posts.post_type = 'post' OR $wpdb->posts.post_type IS NULL)
     344                AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'private' OR $wpdb->posts.post_status IS NULL) " .
     345                ( $exclude_admin ? "AND $wpdb->users.user_login <> 'admin' " : '' ) . "
     346                GROUP BY $wpdb->users.ID
     347                $author_having
     348                $author_sort
     349                $author_limit
     350                ");
     351
    287352        foreach ( (array) $authors as $author ) {
    288353
    289354                $link = '';
    290355
    291                 $author = get_userdata( $author->ID );
    292                 $posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0;
    293                 $name = $author->display_name;
     356                $posts = $author->author_count;
     357                $name = $author->author_name;
    294358
    295                 if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') )
    296                         $name = "$author->first_name $author->last_name";
    297 
    298359                if( !$html ) {
    299360                        if ( $posts == 0 ) {
    300361                                if ( ! $hide_empty )
     
    312373                        if ( ! $hide_empty )
    313374                                $link = $name;
    314375                } 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>';
     376                        $link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->author_name) ) . '">' . $name . '</a>';
    316377
    317378                        if ( (! empty($feed_image)) || (! empty($feed)) ) {
    318379                                $link .= ' ';