WordPress.org

Make WordPress Core

Ticket #35060: 35060.2.diff

File 35060.2.diff, 1.1 KB (added by peterwilsoncc, 4 years ago)
  • src/wp-includes/comment.php

    diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php
    index 7dafa1e..f0b28bc 100644
    a b 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_new', 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 );