Make WordPress Core


Ignore:
Timestamp:
03/31/2022 09:07:02 AM (2 years ago)
Author:
spacedmonkey
Message:

Comments: Improve performance of the wp_count_comments function.

Improve performance of the wp_count_comments function by replacing a complex query with multiple calls to the get_comments function. Passing the count parameter to the get_comments function results in a simple
count query that returns quickly. Using get_comments also means that query is cached and run through filters.

Props FolioVision, markjaquith, nacin, ryan, coffee2code, wonderboymusic, ComputerGuru, jb510, SergeyBiryukov, Znuff, Rahe, uday17035, spacedmonkey, peterwilsoncc.
Fixes #19901.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/comment/getCommentCount.php

    r47526 r53036  
    1212        $this->assertSame( 0, $count['post-trashed'] );
    1313        $this->assertSame( 0, $count['total_comments'] );
     14        $this->assertSame( 0, $count['all'] );
    1415    }
    1516
     
    9899        $this->assertSame( 0, $count['total_comments'] );
    99100    }
     101
     102    /**
     103     * @ticket 19901
     104     *
     105     * @covers ::get_comment_count
     106     */
     107    public function test_get_comment_count_validate_cache_comment_deleted() {
     108
     109        $comment_id = self::factory()->comment->create();
     110
     111        $count = get_comment_count();
     112
     113        $this->assertSame( 1, $count['total_comments'] );
     114
     115        wp_delete_comment( $comment_id, true );
     116
     117        $count = get_comment_count();
     118
     119        $this->assertSame( 0, $count['total_comments'] );
     120    }
     121
     122    /**
     123     * @ticket 19901
     124     *
     125     * @covers ::get_comment_count
     126     */
     127    public function test_get_comment_count_validate_cache_post_deleted() {
     128
     129        $post_id = self::factory()->post->create();
     130
     131        $comment_id = self::factory()->comment->create(
     132            array(
     133                'comment_post_ID' => $post_id,
     134            )
     135        );
     136
     137        $count = get_comment_count( $post_id );
     138
     139        $this->assertSame( 1, $count['total_comments'] );
     140
     141        wp_delete_post( $post_id, true );
     142
     143        $count = get_comment_count( $post_id );
     144
     145        $this->assertSame( 0, $count['total_comments'] );
     146    }
     147
     148    /**
     149     * @ticket 19901
     150     *
     151     * @covers ::get_comment_count
     152     */
     153    public function test_get_comment_count_validate_cache_comment_status() {
     154        $comment_id = self::factory()->comment->create();
     155
     156        $count = get_comment_count();
     157
     158        $this->assertSame( 1, $count['approved'] );
     159        $this->assertSame( 0, $count['trash'] );
     160        $this->assertSame( 1, $count['total_comments'] );
     161
     162        wp_set_comment_status( $comment_id, 'trash' );
     163
     164        $count = get_comment_count();
     165
     166        $this->assertSame( 0, $count['approved'] );
     167        $this->assertSame( 1, $count['trash'] );
     168        $this->assertSame( 0, $count['total_comments'] );
     169    }
    100170}
Note: See TracChangeset for help on using the changeset viewer.