| | 187 | * Number of posts written by a list of users. |
| | 188 | * |
| | 189 | * @since 3.0.0 |
| | 190 | * @param array $userid User ID number list. |
| | 191 | * @return array Amount of posts each user has written. |
| | 192 | */ |
| | 193 | function get_users_numposts($users) { |
| | 194 | global $wpdb; |
| | 195 | |
| | 196 | if (0 == count($users)) |
| | 197 | return array(); |
| | 198 | |
| | 199 | $userlist = implode(',', $users); |
| | 200 | |
| | 201 | $result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts WHERE post_author IN ($userlist) AND post_type = 'post' AND post_status IN ('publish', 'private') GROUP BY post_author", ARRAY_N ); |
| | 202 | |
| | 203 | $count = array(); |
| | 204 | foreach($result as $row) { |
| | 205 | $count[$row[0]] = $row[1]; |
| | 206 | } |
| | 207 | |
| | 208 | foreach($users as $id) { |
| | 209 | $id = (string) $id; |
| | 210 | if (!isset($count[$id])) |
| | 211 | $count[$id] = 0; |
| | 212 | } |
| | 213 | |
| | 214 | return $count; |
| | 215 | } |
| | 216 | |
| | 217 | /** |
| | 474 | /** |
| | 475 | * Count number of users who have each of the user roles. |
| | 476 | * |
| | 477 | * Assumes there are neither duplicated nor orphaned capabilities meta_values. |
| | 478 | * Assumes the serialized array values are meaningless, only array keys are used. |
| | 479 | * Intended scale for this version is 10^5 users. It is not quite capable of that yet due to WP Bug #12257 |
| | 480 | * Memory exhaustion is expected at higher orders. |
| | 481 | * |
| | 482 | * @since 3.0.0 |
| | 483 | * @return array Includes a grand total and an array of counts indexed by role strings. |
| | 484 | */ |
| | 485 | function count_blog_users_by_role_inmem() { |
| | 486 | global $wpdb, $blog_id; |
| | 487 | |
| | 488 | $id = (int) $blog_id; |
| | 489 | $blog_prefix = $wpdb->get_blog_prefix($id); |
| | 490 | $avail_roles = array(); |
| | 491 | |
| | 492 | $users_of_blog = $wpdb->get_col( "SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'" ); |
| | 493 | |
| | 494 | foreach ( $users_of_blog as $caps_meta ) { |
| | 495 | $b_roles = unserialize($caps_meta); |
| | 496 | if ( is_array($b_roles) ) { |
| | 497 | foreach ( $b_roles as $b_role => $val ) { |
| | 498 | if ( isset($avail_roles[$b_role]) ) |
| | 499 | $avail_roles[$b_role]++; |
| | 500 | else |
| | 501 | $avail_roles[$b_role] = 1; |
| | 502 | } |
| | 503 | } |
| | 504 | } |
| | 505 | |
| | 506 | $result = array(); |
| | 507 | $result['total_users'] = count( $users_of_blog ); |
| | 508 | $result['avail_roles'] =& $avail_roles; |
| | 509 | |
| | 510 | return $result; |
| | 511 | } |
| | 512 | |
| | 513 | /** |
| | 514 | * Count number of users who have each of the user roles. |
| | 515 | * |
| | 516 | * Assumes there are neither duplicated nor orphaned capabilities meta_values. |
| | 517 | * Assumes role names are unique phrases. Same assumption made by WP_User_Search::prepare_query() |
| | 518 | * Intended scale for this version is 10^7 users. |
| | 519 | * CPU exhaustion is expected at higher orders. |
| | 520 | * |
| | 521 | * @since 3.0.0 |
| | 522 | * @return array Includes a grand total and an array of counts indexed by role strings. |
| | 523 | */ |
| | 524 | function count_blog_users_by_role_intime() { |
| | 525 | global $wpdb, $blog_id, $wp_roles; |
| | 526 | |
| | 527 | // Initialize |
| | 528 | $id = (int) $blog_id; |
| | 529 | $blog_prefix = $wpdb->get_blog_prefix($id); |
| | 530 | $avail_roles = $wp_roles->get_names(); |
| | 531 | |
| | 532 | // Build a CPU-intensive query that will return concise information. |
| | 533 | $select_count = array(); |
| | 534 | foreach ( $avail_roles as $this_role => $name ) { |
| | 535 | $select_count[] = "COUNT(NULLIF(`meta_value` LIKE '%" . like_escape($this_role) . "%', FALSE))"; |
| | 536 | } |
| | 537 | $select_count = implode(', ', $select_count); |
| | 538 | |
| | 539 | // Add the meta_value index to the selection list, then run the query. |
| | 540 | $row = $wpdb->get_row( "SELECT $select_count, COUNT(*) FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'", ARRAY_N ); |
| | 541 | |
| | 542 | // Run the previous loop again to associate results with role names. |
| | 543 | $col = 0; |
| | 544 | $role_counts = array(); |
| | 545 | foreach ( $avail_roles as $this_role => $name ) { |
| | 546 | $count = (int) $row[$col++]; |
| | 547 | if ($count > 0) |
| | 548 | $role_counts[$this_role] = $count; |
| | 549 | } |
| | 550 | |
| | 551 | // Get the meta_value index from the end of the result set. |
| | 552 | $total_users = (int) $row[$col]; |
| | 553 | |
| | 554 | $result = array(); |
| | 555 | $result['total_users'] = $total_users; |
| | 556 | $result['avail_roles'] =& $role_counts; |
| | 557 | |
| | 558 | return $result; |
| | 559 | } |
| | 560 | |