| 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"; |
| | 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 | |