Skip to content

Commit e30ab16

Browse files
committed
Users: Add pre-flight filter to count_many_users_posts().
Introduces the filter `pre_count_many_users_posts()` to allow developers to bypass the function in favour of either avoiding counts or their own counting functionality. Props audrasjb, ethitter, jigar-bhanushali, jorbin. Fixes #63004. git-svn-id: https://develop.svn.wordpress.org/trunk@59900 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 1c2a87b commit e30ab16

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/wp-includes/user.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -660,25 +660,38 @@ function count_user_posts( $userid, $post_type = 'post', $public_only = false )
660660
function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) {
661661
global $wpdb;
662662

663-
$count = array();
664663
if ( empty( $users ) || ! is_array( $users ) ) {
665-
return $count;
664+
return array();
665+
}
666+
667+
/**
668+
* Filters whether to short-circuit performing the post counts.
669+
*
670+
* When filtering, return an array of posts counts as strings, keyed
671+
* by the user ID.
672+
*
673+
* @since 6.8.0
674+
*
675+
* @param string[]|null $count The post counts. Return a non-null value to short-circuit.
676+
* @param int[] $users Array of user IDs.
677+
* @param string|string[] $post_type Single post type or array of post types to check.
678+
* @param bool $public_only Whether to only return counts for public posts.
679+
*/
680+
$pre = apply_filters( 'pre_count_many_users_posts', null, $users, $post_type, $public_only );
681+
if ( null !== $pre ) {
682+
return $pre;
666683
}
667684

668685
$userlist = implode( ',', array_map( 'absint', $users ) );
669686
$where = get_posts_by_author_sql( $post_type, true, null, $public_only );
670687

671688
$result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N );
689+
690+
$count = array_fill_keys( $users, 0 );
672691
foreach ( $result as $row ) {
673692
$count[ $row[0] ] = $row[1];
674693
}
675694

676-
foreach ( $users as $id ) {
677-
if ( ! isset( $count[ $id ] ) ) {
678-
$count[ $id ] = 0;
679-
}
680-
}
681-
682695
return $count;
683696
}
684697

0 commit comments

Comments
 (0)