diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index 0b522b022235e..c81c6aae7d96c 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -682,14 +682,25 @@ function count_many_users_posts( $users, $post_type = 'post', $public_only = fal return $pre; } - $userlist = implode( ',', array_map( 'absint', $users ) ); - $where = get_posts_by_author_sql( $post_type, true, null, $public_only ); + // Cleanup the users array. Remove duplicates and sort for consistent ordering. + $users = array_unique( array_filter( array_map( 'absint', (array) $users ) ) ); + sort( $users ); - $result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N ); + $userlist = implode( ',', $users ); + $cache_key = 'count_many_users_posts:' . md5( implode( ':', array( $post_type, $public_only ? 'public' : 'public|private', $userlist, get_current_user_id() ) ) ); + $cache_salts = array( wp_cache_get_last_changed( 'posts' ), wp_cache_get_last_changed( 'users' ) ); + $count = wp_cache_get_salted( $cache_key, 'user-queries', $cache_salts ); - $count = array_fill_keys( $users, 0 ); - foreach ( $result as $row ) { - $count[ $row[0] ] = $row[1]; + if ( false === $count ) { + $where = get_posts_by_author_sql( $post_type, true, null, $public_only ); + $result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N ); + + $count = array_fill_keys( $users, 0 ); + foreach ( $result as $row ) { + $count[ $row[0] ] = $row[1]; + } + + wp_cache_set_salted( $cache_key, $count, 'user-queries', $cache_salts, HOUR_IN_SECONDS ); } return $count; diff --git a/tests/phpunit/tests/user.php b/tests/phpunit/tests/user.php index 535004ee560a8..ea25853796a3f 100644 --- a/tests/phpunit/tests/user.php +++ b/tests/phpunit/tests/user.php @@ -604,6 +604,48 @@ public function test_count_many_users_posts() { $this->assertSame( '1', $counts[ $user_id_b ] ); } + /** + * Test salted cache functionality for count_many_users_posts(). + * + * @ticket 63405 + */ + public function test_count_many_users_posts_salted_cache() { + // Clear any existing cache + wp_cache_flush(); + + $author_id = self::factory()->user->create( array( 'role' => 'author' ) ); + $editor_id = self::factory()->user->create( array( 'role' => 'editor' ) ); + + // Create posts for both users + self::factory()->post->create( array( 'post_author' => $author_id ) ); + self::factory()->post->create( array( 'post_author' => $editor_id ) ); + + // Test cache hit + $counts1 = count_many_users_posts( array( $author_id, $editor_id ), 'post', false ); + $counts2 = count_many_users_posts( array( $author_id, $editor_id ), 'post', false ); + $this->assertSame( $counts1, $counts2 ); + $this->assertSame( '1', $counts1[ $author_id ] ); + $this->assertSame( '1', $counts1[ $editor_id ] ); + + // Test cache invalidation - create new post for author only + self::factory()->post->create( array( 'post_author' => $author_id ) ); + $counts3 = count_many_users_posts( array( $author_id, $editor_id ), 'post', false ); + $this->assertSame( '2', $counts3[ $author_id ] ); + $this->assertSame( '1', $counts3[ $editor_id ] ); + + // Test different post types use different cache keys + self::factory()->post->create( + array( + 'post_author' => $author_id, + 'post_type' => 'page', + ) + ); + $counts_posts = count_many_users_posts( array( $author_id ), 'post', false ); + $counts_pages = count_many_users_posts( array( $author_id ), 'page', false ); + $this->assertSame( '2', $counts_posts[ $author_id ] ); + $this->assertSame( '1', $counts_pages[ $author_id ] ); + } + /** * @ticket 22858 */