Ticket #10329: author-template.r12704.diff

File author-template.r12704.diff, 4.8 KB (added by takaitra, 2 years ago)

Updated patch for latest trunk. Multi-blog logic. Options renamed "orderby," "order," and "number" to match the other template tags.

  • 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> 
    250255 * </ul> 
    251256 * 
    252257 * @link http://codex.wordpress.org/Template_Tags/wp_list_authors 
     
    255260 * @return null|string The output, if echo is set to false. 
    256261 */ 
    257262function wp_list_authors($args = '') { 
    258         global $wpdb; 
     263        global $wpdb, $blog_id; 
    259264 
    260265        $defaults = array( 
    261266                'optioncount' => false, 'exclude_admin' => true, 
    262267                'show_fullname' => false, 'hide_empty' => true, 
    263268                'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true, 
    264                 'style' => 'list', 'html' => true 
     269                'style' => 'list', 'html' => true, 'orderby' => 'name', 
     270                'order' => 'ASC', 'number' => NULL 
    265271        ); 
    266272 
    267273        $r = wp_parse_args( $args, $defaults ); 
    268274        extract($r, EXTR_SKIP); 
    269275        $return = ''; 
    270276 
    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" ); 
     277        if ( $show_fullname ) { 
     278                $author_name_field = "CASE 
     279                WHEN TRIM(CONCAT_WS(' ', COALESCE(meta_first_name.meta_value, ''), COALESCE(meta_last_name.meta_value, ''))) <> '' 
     280                THEN 
     281                        TRIM(CONCAT_WS(' ', COALESCE(meta_first_name.meta_value, ''), COALESCE(meta_last_name.meta_value, ''))) 
     282                ELSE 
     283                        $wpdb->users.display_name 
     284                END"; 
     285                $author_name_join = " 
     286                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' 
     287                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' 
     288                "; 
    279289        } else { 
    280                 $authors = array(); 
     290                $author_name_field = "$wpdb->users.display_name"; 
     291                $author_name_join = ""; 
    281292        } 
    282293 
    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; 
     294        if ( $orderby == 'name' ) { 
     295                $author_sort = "ORDER BY author_name $order"; 
     296        } else { 
     297                $author_sort = "ORDER BY author_count $order"; 
     298        } 
    286299 
     300        $number = intval( $number ); 
     301        if ( $number > 0 ) { 
     302                $author_limit = "LIMIT $number"; 
     303        } else { 
     304                $author_limit = ''; 
     305        } 
     306 
     307        $blog_prefix = $wpdb->get_blog_prefix($blog_id); 
     308        $authors = $wpdb->get_results(" 
     309                SELECT $wpdb->users.ID, $wpdb->users.user_nicename, COUNT($wpdb->posts.ID) as author_count, $author_name_field as author_name 
     310                FROM $wpdb->users 
     311                " . ( !$hide_empty ? "LEFT " : '' ) . "JOIN $wpdb->posts ON $wpdb->posts.post_author = $wpdb->users.ID 
     312                $author_name_join 
     313                LEFT JOIN $wpdb->usermeta AS meta_capabilities ON meta_capabilities.user_id = $wpdb->users.ID AND meta_capabilities.meta_key = '{$blog_prefix}capabilities' 
     314                WHERE ($wpdb->posts.post_type = 'post' OR $wpdb->posts.post_type IS NULL) 
     315                AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'private' OR $wpdb->posts.post_status IS NULL) " . 
     316                ( $exclude_admin ? "AND $wpdb->users.user_login <> 'admin' " : '' ) . " 
     317                GROUP BY $wpdb->users.ID 
     318                $author_sort 
     319                $author_limit 
     320                "); 
     321 
    287322        foreach ( (array) $authors as $author ) { 
    288323 
    289324                $link = ''; 
    290325 
    291                 $author = get_userdata( $author->ID ); 
    292                 $posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0; 
    293                 $name = $author->display_name; 
     326                $posts = $author->author_count; 
     327                $name = $author->author_name; 
    294328 
    295                 if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') ) 
    296                         $name = "$author->first_name $author->last_name"; 
    297  
    298329                if( !$html ) { 
    299330                        if ( $posts == 0 ) { 
    300331                                if ( ! $hide_empty ) 
     
    312343                        if ( ! $hide_empty ) 
    313344                                $link = $name; 
    314345                } 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>'; 
     346                        $link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->author_name) ) . '">' . $name . '</a>'; 
    316347 
    317348                        if ( (! empty($feed_image)) || (! empty($feed)) ) { 
    318349                                $link .= ' ';