WordPress.org

Make WordPress Core

Ticket #36409: 36409-3.patch

File 36409-3.patch, 1.4 KB (added by sidati, 5 years ago)
  • wp-includes/comment.php

     
    21512151        $new = apply_filters( 'pre_wp_update_comment_count_now', null, $old, $post_id );
    21522152
    21532153        if ( is_null( $new ) ) {
    2154                 $new = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id ) );
     2154
     2155                $bad_parents = array();
     2156                $_bad_parents = $wpdb->get_col( "SELECT comment_ID from $wpdb->comments WHERE comment_post_ID = $post_id AND comment_approved != '1'" );
     2157
     2158                do {
     2159                        $children = $wpdb->get_col( "SELECT comment_ID from $wpdb->comments WHERE comment_post_ID = $post_id AND comment_parent IN (" . implode( ',', array_map( 'intval', $_bad_parents ) ) . ")");
     2160                        $bad_parents = array_merge($bad_parents, $_bad_parents, $children);
     2161                        $_bad_parents = array_unique($children);
     2162                } while ( $children );
     2163
     2164                $bad_parent_query = null;
     2165                if ($bad_parents = array_unique($bad_parents)) {
     2166                        $bad_parents = implode(',', array_map( 'intval', $bad_parents ));
     2167                        $bad_parent_query = " AND comment_parent NOT IN ($bad_parents)";
     2168                }
     2169
     2170                $new = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'".$bad_parent_query, $post_id ) );
    21552171        } else {
    21562172                $new = (int) $new;
    21572173        }