| 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 | "; |
| | 311 | // Limit the results based on the min_count argument |
| | 312 | $min_count = absint( $min_count ); |
| | 313 | if ( $hide_empty ) |
| | 314 | $min_count = max(1, $min_count); |
| | 315 | if ( $min_count ) { |
| | 316 | $author_having = "HAVING author_count >= $min_count"; |
| | 317 | } else { |
| | 318 | $author_having = ''; |
| | 319 | } |
| | 320 | |
| | 321 | // Limit the results based on the number argument |
| | 322 | $number = intval( $number ); |
| | 323 | if ( $number > 0 ) { |
| | 324 | $author_limit = "LIMIT $number"; |
| | 325 | } else { |
| | 326 | $author_limit = ''; |
| | 327 | } |
| | 328 | |
| | 329 | // Join to exclude authors from other blogs. Only needed if hide_empty is |
| | 330 | // false or and no min_count is set (the INNER JOIN to $wpdb->posts would |
| | 331 | // already exclude non-authors in both cases) |
| | 332 | if ( !$hide_empty && !$min_count ) { |
| | 333 | $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'"; |
| | 334 | } else { |
| | 335 | $author_cap_join = ''; |
| | 336 | } |
| | 337 | |
| | 338 | $author_select = "$wpdb->users.ID, $wpdb->users.user_nicename, $author_name_field as author_name"; |
| | 339 | |
| | 340 | // join on posts only when necessary |
| | 341 | if ( $hide_empty || $min_count || $optioncount || $orderby == 'count' ) { |
| | 342 | $author_select .= ", COUNT($wpdb->posts.ID) as author_count"; |
| | 343 | $author_posts_join = "JOIN $wpdb->posts ON $wpdb->posts.post_author = $wpdb->users.ID"; |
| | 344 | $author_where = "$wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status = 'publish'"; |
| | 345 | $author_group_by = "GROUP BY $wpdb->users.ID"; |
| | 346 | // a left join is needed if we're only interested in fetching numbers of posts |
| | 347 | if ( !$min_count && !$hide_empty ) |
| | 348 | $author_posts_join = 'LEFT ' . $author_posts_join; |
| | 349 | } else { |
| | 350 | $author_posts_join = ''; |
| | 351 | $author_where = ''; |
| | 352 | $author_group_by = ''; |
| | 353 | } |
| | 354 | |
| | 355 | if ( $exclude_admin ) |
| | 356 | $author_where .= ( $author_where ? ' AND ' : '' ) . "$wpdb->users.user_login <> 'admin'"; |
| | 357 | |
| | 358 | $author_where = $author_where ? ( 'WHERE ' . $author_where ) : ''; |
| | 359 | |
| | 360 | // Query the list of users |
| | 361 | $blog_prefix = $wpdb->get_blog_prefix($blog_id); |
| | 362 | $sql = " |
| | 363 | SELECT $author_select |
| | 364 | FROM $wpdb->users |
| | 365 | $author_posts_join |
| | 366 | $author_name_join |
| | 367 | $author_cap_join |
| | 368 | $author_where |
| | 369 | $author_group_by |
| | 370 | $author_having |
| | 371 | $author_sort |
| | 372 | $author_limit |
| | 373 | "; |
| | 374 | |
| | 375 | $authors = $wpdb->get_results($sql); |
| | 376 | |