283 | | /** @todo Move select to get_authors(). */ |
284 | | $users = get_users(); |
285 | | $author_ids = array(); |
286 | | foreach ( (array) $users as $user ) |
287 | | $author_ids[] = $user->user_id; |
288 | | if ( count($author_ids) > 0 ) { |
289 | | $author_ids = implode(',', $author_ids ); |
290 | | $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" ); |
| 291 | if ( empty($id) ) |
| 292 | $id = (int) $blog_id; |
| 293 | $blog_prefix = $wpdb->get_blog_prefix($id); |
| 294 | |
| 295 | // Order ASC or DESC based on the order and orderby arguments |
| 296 | if ( $show_fullname ) { |
| 297 | // Full names will be looked up via user meta-data and sorted later |
| 298 | // order by NULL avoids the implicit sorting of the GROUP BY |
| 299 | $author_sort = 'ORDER BY NULL'; |
| 300 | } else { |
| 301 | if ( $orderby == 'count' ) { |
| 302 | $author_sort = "ORDER BY post_count $order, author_name"; |
| 303 | } else { |
| 304 | $author_sort = "ORDER BY author_name $order"; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | // Limit the results based on the min_count argument |
| 309 | $min_count = absint( $min_count ); |
| 310 | if ( $hide_empty ) |
| 311 | $min_count = max(1, $min_count); |
| 312 | if ( $min_count ) { |
| 313 | $author_having = "HAVING post_count >= $min_count"; |
| 326 | // Join to exclude authors from other blogs. Only needed if hide_empty is |
| 327 | // false or and no min_count is set (the INNER JOIN to $wpdb->posts would |
| 328 | // already exclude non-authors in both cases) |
| 329 | if ( !$hide_empty && !$min_count ) { |
| 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 | $author_select = "$wpdb->users.ID, $wpdb->users.user_nicename, $wpdb->users.display_name AS author_name"; |
| 336 | |
| 337 | // join on posts only when necessary |
| 338 | if ( $hide_empty || $min_count || $optioncount || $orderby == 'count' ) { |
| 339 | $author_select .= ", COUNT($wpdb->posts.ID) as post_count"; |
| 340 | $author_posts_join = "JOIN $wpdb->posts ON $wpdb->posts.post_author = $wpdb->users.ID"; |
| 341 | $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)"; |
| 342 | $author_group_by = "GROUP BY $wpdb->users.ID"; |
| 343 | // a left join is needed if we're only interested in fetching numbers of posts |
| 344 | if ( !$min_count && !$hide_empty ) |
| 345 | $author_posts_join = 'LEFT ' . $author_posts_join; |
| 346 | } else { |
| 347 | $author_posts_join = ''; |
| 348 | $author_where = ''; |
| 349 | $author_group_by = ''; |
| 350 | } |
| 351 | |
| 352 | if ( $exclude_admin ) |
| 353 | $author_where .= ( $author_where ? ' AND ' : '' ) . "$wpdb->users.user_login <> 'admin'"; |
| 354 | |
| 355 | $author_where = $author_where ? ( 'WHERE ' . $author_where ) : ''; |
| 356 | |
| 357 | // Query the list of users |
| 358 | $sql = " |
| 359 | SELECT $author_select |
| 360 | FROM $wpdb->users |
| 361 | $author_posts_join |
| 362 | $author_name_join |
| 363 | $author_cap_join |
| 364 | $author_where |
| 365 | $author_group_by |
| 366 | $author_having |
| 367 | $author_sort |
| 368 | $author_limit |
| 369 | "; |
| 370 | |
| 371 | $authors = $wpdb->get_results($sql); |
| 372 | |
| 373 | if ( $show_fullname ) { |
| 374 | // Lookup first and last name via cached user meta-data |
| 375 | foreach ( (array) $authors as $author ) { |
| 376 | $userdata = get_userdata( $author->ID ); |
| 377 | if ( !empty( $userdata->first_name ) || !empty( $userdata->last_name ) ) { |
| 378 | $author->author_name = $userdata->first_name . ' ' . $userdata->last_name; |
| 379 | trim ( $author->author_name ); |
| 380 | } |
| 381 | } |
| 382 | // Sort the objects |
| 383 | if ( $orderby == 'name' ) { |
| 384 | usort( $authors, '_wp_list_authors_usort_callback_name' ); |
| 385 | } else { |
| 386 | usort( $authors, '_wp_list_authors_usort_callback_count' ); |
| 387 | } |
| 388 | if ( $order == 'DESC' ) { |
| 389 | $authors = array_reverse( $authors ); |
| 390 | } |
| 391 | } |
| 392 | |