| 539 | /** |
| 540 | * Counts the number of posts for a particular post type. |
| 541 | * |
| 542 | * @param int $userid User ID. |
| 543 | * @param string $post_type Optional. Single post type or array of post types to count the number of posts for. Default 'post'. |
| 544 | * @param bool $public_only Optional. Whether to only return counts for public posts. Default false. |
| 545 | * @return int Number of posts the user has written in this post type. |
| 546 | */ |
| 547 | function count_user_posts_for_single_type( $userid, $post_type = 'post', $public_only = false ) { |
| 548 | global $wpdb; |
| 549 | |
| 550 | $cache_key = "count_user_{$post_type}_{$userid}"; |
| 551 | $cache_group = $public_only ? 'user_posts_count_public' : 'user_posts_count'; |
| 552 | |
| 553 | $count = wp_cache_get( $cache_key, $cache_group ); |
| 554 | |
| 555 | if( false === $count ) { |
| 556 | $where = get_posts_by_author_sql( $post_type, true, $userid, $public_only ); |
| 557 | $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" ); |
| 558 | |
| 559 | wp_cache_add( $cache_key, $count, $cache_group ); |
| 560 | } |
| 561 | |
| 562 | return absint( $count ); |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Clears the cached count for user for given post type. |
| 567 | * |
| 568 | * @param int $userid User ID. |
| 569 | * @param string $post_type Post type. |
| 570 | */ |
| 571 | function clear_count_user_posts_cache( $userid, $post_type ) { |
| 572 | $cache_key = "count_user_{$post_type}_{$userid}"; |
| 573 | $cache_groups = [ |
| 574 | 'user_posts_count_public', |
| 575 | 'user_posts_count', |
| 576 | ]; |
| 577 | |
| 578 | foreach( $cache_groups as $cache_group ) { |
| 579 | wp_cache_delete( $cache_key, $cache_group ); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Check if post author has changed. If changed, clear count posts cache for user. |
| 585 | * |
| 586 | * @param int $post_ID Post ID. |
| 587 | * @param WP_Post $post_after Post object following the update. |
| 588 | * @param WP_Post $post_before Post object before the update. |
| 589 | */ |
| 590 | function check_author_change( $post_id, $post_after, $post_before ) { |
| 591 | if ( $post_after->post_author !== $post_before->post_author ) { |
| 592 | $post_type = get_post_type( $post_id ); |
| 593 | |
| 594 | clear_count_user_posts_cache( $post_after->post_author, $post_type ); |
| 595 | clear_count_user_posts_cache( $post_before->post_author, $post_type ); |
| 596 | } |
| 597 | } |
| 598 | add_action( 'post_updated', 'check_author_change', 10, 3 ); |
| 599 | |
| 600 | /** |
| 601 | * Counts the number of posts for given post types. |
| 602 | * |
| 603 | * @param int $userid User ID. |
| 604 | * @param array $post_types Optional. Single post type or array of post types to count the number of posts for. Default 'post'. |
| 605 | * @param bool $public_only Optional. Whether to only return counts for public posts. Default false. |
| 606 | * @return int Number of posts the user has written in this post type. |
| 607 | */ |
| 608 | function count_user_posts_for_multiple_types( $userid, $post_types = [ 'post' ], $public_only = false ) { |
| 609 | $numposts = 0; |
| 610 | |
| 611 | foreach( $post_types as $post_type ) { |
| 612 | $numposts += count_user_posts_for_single_type( $userid, $post_type, $public_only ); |
| 613 | } |
| 614 | |
| 615 | return $numposts; |
| 616 | } |
| 617 | |
596 | | $userlist = implode( ',', array_map( 'absint', $users ) ); |
597 | | $where = get_posts_by_author_sql( $post_type, true, null, $public_only ); |
598 | | |
599 | | $result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N ); |
600 | | foreach ( $result as $row ) { |
601 | | $count[ $row[0] ] = $row[1]; |
| 675 | foreach ( $users as $user_id ) { |
| 676 | $count[ $user_id ] = count_user_posts( $user_id, $post_type, $public_only ); |