Make WordPress Core

Ticket #39242: 39242.4.diff

File 39242.4.diff, 4.7 KB (added by wpgurudev, 2 years ago)

Refactor count_user_posts function

  • src/wp-includes/user.php

    diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php
    index ca4b3d03fd..713f6d7124 100644
    a b function wp_validate_logged_in_cookie( $user_id ) { 
    536536        return wp_validate_auth_cookie( $_COOKIE[ LOGGED_IN_COOKIE ], 'logged_in' );
    537537}
    538538
     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 */
     547function 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 */
     571function 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 */
     590function 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}
     598add_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 */
     608function 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
    539618/**
    540619 * Gets the number of posts a user has written.
    541620 *
    function wp_validate_logged_in_cookie( $user_id ) { 
    552631 * @return string Number of posts the user has written in this post type.
    553632 */
    554633function count_user_posts( $userid, $post_type = 'post', $public_only = false ) {
    555         global $wpdb;
    556 
    557         $where = get_posts_by_author_sql( $post_type, true, $userid, $public_only );
     634        $count = 0;
    558635
    559         $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
     636        if ( is_array( $post_type ) ) {
     637        $count = count_user_posts_for_multiple_types( $userid, $post_type, $public_only );
     638    } else {
     639                $count = count_user_posts_for_single_type( $userid, $post_type, $public_only );
     640        }
    560641
    561642        /**
    562643         * Filters the number of posts a user has written.
    function count_user_posts( $userid, $post_type = 'post', $public_only = false ) 
    586667 * @return string[] Amount of posts each user has written, as strings, keyed by user ID.
    587668 */
    588669function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) {
    589         global $wpdb;
    590 
    591670        $count = array();
    592671        if ( empty( $users ) || ! is_array( $users ) ) {
    593672                return $count;
    594673        }
    595674
    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 );
    602677        }
    603678
    604679        foreach ( $users as $id ) {