Make WordPress Core

Ticket #35060: 35060.4.diff

File 35060.4.diff, 3.0 KB (added by swissspidy, 9 years ago)
  • src/wp-includes/comment.php

    diff --git src/wp-includes/comment.php src/wp-includes/comment.php
    index 7dafa1e..220b84d 100644
    function wp_update_comment_count_now($post_id) { 
    20552055                return false;
    20562056
    20572057        $old = (int) $post->comment_count;
    2058         $new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) );
     2058
     2059        /**
     2060        * Filters a post's comment count before it is updated in the database.
     2061        *
     2062        * @since 4.5.0
     2063        *
     2064        * @param null $new     The new comment count.
     2065        * @param int  $old     The old comment count.
     2066        * @param int  $post_id Post ID.
     2067        */
     2068        $new = apply_filters( 'wp_update_comment_count_now', null, $old, $post_id );
     2069
     2070        if ( ( $new !== null ) && is_numeric( $new ) && ( 0 <= $new ) ) {
     2071                $new = (int) $new;
     2072        }
     2073        else {
     2074                $new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) );
     2075        }
     2076
    20592077        $wpdb->update( $wpdb->posts, array('comment_count' => $new), array('ID' => $post_id) );
    20602078
    20612079        clean_post_cache( $post );
  • new file tests/phpunit/tests/comment/wpUpdateCommentCountNow.php

    diff --git tests/phpunit/tests/comment/wpUpdateCommentCountNow.php tests/phpunit/tests/comment/wpUpdateCommentCountNow.php
    new file mode 100644
    index 0000000..cc7346e
    - +  
     1<?php
     2
     3/**
     4 * @group  comment
     5 * @covers wp_update_comment_count_now
     6 */
     7class Tests_Update_Comment_Count_Now extends WP_UnitTestCase {
     8        public function _return_100() {
     9                return 100;
     10        }
     11
     12        public function test_invalid_post_bails_early() {
     13                $this->assertFalse( wp_update_comment_count_now( 100 ) );
     14                $this->assertFalse( wp_update_comment_count_now( null ) );
     15                $this->assertFalse( wp_update_comment_count_now( 0 ) );
     16        }
     17
     18        public function test_regular_post_updates_comment_count() {
     19                global $wpdb;
     20
     21                $post_id = self::factory()->post->create();
     22
     23                self::factory()->comment->create_post_comments( $post_id, 1 );
     24                $this->assertSame( '1', get_comments_number( $post_id ) );
     25
     26                $num_queries = $wpdb->num_queries;
     27                $this->assertTrue( wp_update_comment_count_now( $post_id ) );
     28                $this->assertSame( $num_queries + 2, $wpdb->num_queries );
     29
     30                $this->assertSame( '1', get_comments_number( $post_id ) );
     31        }
     32
     33        public function test_using_filter_adjusts_comment_count_without_an_additional_database_query() {
     34                global $wpdb;
     35
     36                add_filter( 'wp_update_comment_count_now', array( $this, '_return_100' ) );
     37
     38                $post_id = self::factory()->post->create();
     39
     40                self::factory()->comment->create_post_comments( $post_id, 1 );
     41                $this->assertSame( '100', get_comments_number( $post_id ) );
     42
     43                $num_queries = $wpdb->num_queries;
     44                $this->assertTrue( wp_update_comment_count_now( $post_id ) );
     45                // Only one query is made instead of two.
     46                $this->assertSame( $num_queries + 1, $wpdb->num_queries );
     47
     48                $this->assertSame( '100', get_comments_number( $post_id ) );
     49
     50                remove_filter( 'wp_update_comment_count_now', array( $this, '_return_100' ) );
     51        }
     52}