Make WordPress Core

Ticket #35060: 35060.5.diff

File 35060.5.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 b9d084c..41a2260 100644
    function wp_update_comment_count_now($post_id) { 
    20942094                return false;
    20952095
    20962096        $old = (int) $post->comment_count;
    2097         $new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) );
     2097
     2098        /**
     2099         * Filters a post's comment count before it is updated in the database.
     2100         *
     2101         * @since 4.5.0
     2102         *
     2103         * @param int $new     The new comment count. Default null.
     2104         * @param int $old     The old comment count.
     2105         * @param int $post_id Post ID.
     2106         */
     2107        $new = apply_filters( 'pre_wp_update_comment_count_now', null, $old, $post_id );
     2108
     2109        if ( is_null( $new ) ) {
     2110                $new = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id ) );
     2111        } else {
     2112                $new = (int) $new;
     2113        }
     2114
    20982115        $wpdb->update( $wpdb->posts, array('comment_count' => $new), array('ID' => $post_id) );
    20992116
    21002117        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}