Make WordPress Core

Ticket #39242: 39242.3.diff

File 39242.3.diff, 3.4 KB (added by johnregan3, 3 years ago)

Not fully functional. Unit tests added, but others are failing.

  • src/wp-includes/user.php

    diff --git src/wp-includes/user.php src/wp-includes/user.php
    index 3c0f917bbc..c2d8970567 100644
    function count_user_posts( $userid, $post_type = 'post', $public_only = false ) 
    556556
    557557        $where = get_posts_by_author_sql( $post_type, true, $userid, $public_only );
    558558
    559         $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
     559        $post_type_label = $post_type;
     560        if ( is_array( $post_type ) ) {
     561                $post_type_label = implode( '_', $post_type );
     562        }
     563        $cache_key = "count_user_{$post_type_label}_{$userid}";
     564
     565        if ( $public_only ) {
     566                $cache_group = 'user_posts_count_public';
     567        } else {
     568                $cache_group = 'user_posts_count';
     569        }
     570
     571        $count = wp_cache_get( $cache_key, $cache_group );
     572
     573        if ( false === $count ) {
     574                $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
     575                wp_cache_add( $cache_key, $count, $cache_group );
     576        }
    560577
    561578        /**
    562579         * Filters the number of posts a user has written.
    function count_many_users_posts( $users, $post_type = 'post', $public_only = fal 
    593610                return $count;
    594611        }
    595612
    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];
     613        foreach ( $users as $user_id ) {
     614                $count[ $user_id ] = count_user_posts( $user_id, $post_type, $public_only );
    602615        }
    603616
    604617        foreach ( $users as $id ) {
  • tests/phpunit/tests/user/countUserPosts.php

    diff --git tests/phpunit/tests/user/countUserPosts.php tests/phpunit/tests/user/countUserPosts.php
    index a4244e2814..991edc3b4f 100644
    class Tests_User_CountUserPosts extends WP_UnitTestCase { 
    8989        public function test_count_user_posts_should_ignore_non_existent_post_types() {
    9090                $this->assertEquals( 4, count_user_posts( self::$user_id, array( 'foo', 'post' ) ) );
    9191        }
     92
     93        /**
     94         * Test the count_user_posts cache.
     95         *
     96         * @ticket 39242
     97         *
     98         * @dataProvider data_count_user_posts_cache
     99         *
     100         * Primarily, this tests that the cache is created when count_user_posts() is called.
     101         * Additionally, this tests using both a single post type string and an array of post types as a parameter.
     102         *
     103         * This ticket adds a cache key string to the function so we check that it is generated as exptected.
     104         *
     105         * @param string|array $post_type       Used to test the first param in count_user_posts().
     106         * @param string       $cache_key_lable Provides the expected cache key.
     107         */
     108        public function test_count_user_posts_cache( $post_type, $cache_key_label) {
     109                // Validate the cache is empty.
     110                $cache = wp_cache_get( 'count_user_' . $cache_key_label . '_' . self::$user_id, 'user_posts_count' );
     111                $this->assertFalse( $cache );
     112
     113                // Call the function.
     114                $count = count_user_posts( self::$user_id, $post_type );
     115
     116                // Validate the cache is populated.
     117                $cache = wp_cache_get( 'count_user_' . $cache_key_label . '_' . self::$user_id, 'user_posts_count' );
     118                $this->assertEquals( $count, $cache );
     119        }
     120
     121        /**
     122         * @ticket 39242
     123         */
     124        public function data_count_user_posts_cache() {
     125                return array (
     126                        array(
     127                                'post',
     128                                'post'
     129                        ),
     130                        array(
     131                                array(
     132                                        'post',
     133                                        'wptests_pt',
     134                                ),
     135                                'post_wptests_pt',
     136                        ),
     137                );
     138        }
    92139}