Make WordPress Core

Changeset 3104


Ignore:
Timestamp:
11/16/2005 06:29:36 AM (19 years ago)
Author:
ryan
Message:

Add comment_count to the posts table. Props donncha. fixes #1860

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/list-manipulation.php

    r3061 r3104  
    5757        die('-1');
    5858
    59     if ( wp_set_comment_status($comment->comment_ID, "delete") ) {
    60         do_action('delete_comment', $comment->comment_ID);
     59    if ( wp_delete_comment($comment->comment_ID) ) {
    6160        die('1');
    6261    } else {
  • trunk/wp-admin/upgrade-functions.php

    r3092 r3104  
    3131    }
    3232   
    33     if ( $wp_current_db_version < 3092 )
     33    if ( $wp_current_db_version < 3104 )
    3434        upgrade_160();
    3535
     
    301301        }
    302302    }
    303    
     303
     304    // populate comment_count field of posts table
     305    $comments = $wpdb->get_results( "SELECT comment_post_ID, COUNT(*) as c FROM $wpdb->comments GROUP BY comment_post_ID" );
     306    if( is_array( $comments ) ) {
     307        foreach ($comments as $comment) {
     308            $wpdb->query( "UPDATE $wpdb->posts SET comment_count = $comment->c WHERE ID = '$comment->comment_post_ID}'" );
     309        }
     310    }
     311
    304312    // Some alpha versions used a post status of object instead of attachment and put
    305313    // the mime type in post_type instead of post_mime_type.
  • trunk/wp-admin/upgrade-schema.php

    r3092 r3104  
    122122  post_type varchar(100) NOT NULL,
    123123  post_mime_type varchar(100) NOT NULL,
     124  comment_count bigint(20) NOT NULL default '0',
    124125  PRIMARY KEY  (ID),
    125126  KEY post_name (post_name)
  • trunk/wp-includes/comment-functions.php

    r3078 r3104  
    8282    ");
    8383
    84     return $wpdb->insert_id;
     84    $id = $wpdb->insert_id;
     85
     86    if ( $comment_approved == 1)
     87        $wpdb->query( "UPDATE $wpdb->posts SET comment_count = comment_count + 1 WHERE ID = '$comment_post_ID'" );
     88   
     89    return $id;
    8590}
    8691
     
    177182    $rval = $wpdb->rows_affected;
    178183
     184    $c = $wpdb->get_row( "SELECT count(*) as c FROM {$wpdb->comments} WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'" );
     185    if( is_object( $c ) )
     186        $wpdb->query( "UPDATE $wpdb->posts SET comment_count = '$c->c' WHERE ID = '$comment_post_ID'" );
     187
    179188    do_action('edit_comment', $comment_ID);
    180189
    181     return $rval;   
     190    return $rval;
     191}
     192
     193function wp_delete_comment($comment_id) {
     194    global $wpdb;
     195    do_action('delete_comment', $comment_id);
     196
     197    $comment = get_comment($comment_id);
     198
     199    if ( ! $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1") )
     200        return false;
     201
     202    $post_id = $comment->comment_post_ID;
     203    if ( $post_id )
     204        $wpdb->query( "UPDATE $wpdb->posts SET comment_count = comment_count - 1 WHERE ID = '$post_id'" );
     205
     206    do_action('wp_set_comment_status', $comment_id, 'delete');
     207    return true;
    182208}
    183209
     
    199225
    200226    if ( !isset($comment_count_cache[$post_id]) )
    201         $comment_count_cache[$post_id] =  $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$post_id' AND comment_approved = '1'");
     227        $comment_count_cache[$id] = $wpdb->get_var("SELECT comment_count FROM $wpdb->posts WHERE ID = '$post_id'");
    202228   
    203229    return apply_filters('get_comments_number', $comment_count_cache[$post_id]);
     
    743769        break;
    744770        case 'delete':
    745             $query = "DELETE FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1";
     771            return wp_delete_comment($comment_id);
    746772        break;
    747773        default:
     
    751777    if ($wpdb->query($query)) {
    752778        do_action('wp_set_comment_status', $comment_id, $comment_status);
     779       
     780        $comment = get_comment($comment_id);
     781        $comment_post_ID = $comment->comment_post_ID;
     782        $c = $wpdb->get_row( "SELECT count(*) as c FROM {$wpdb->comments} WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'" );
     783        if( is_object( $c ) )
     784            $wpdb->query( "UPDATE $wpdb->posts SET comment_count = '$c->c' WHERE ID = '$comment_post_ID'" );
    753785        return true;
    754786    } else {
  • trunk/wp-includes/functions.php

    r3103 r3104  
    13071307
    13081308    // Do the same for comment numbers
    1309     $comment_counts = $wpdb->get_results("SELECT comment_post_ID, COUNT( comment_ID ) AS ccount
    1310     FROM $wpdb->comments
    1311     WHERE comment_post_ID IN ($post_id_list)
    1312     AND comment_approved = '1'
    1313     GROUP BY comment_post_ID");
     1309    $comment_counts = $wpdb->get_results( "SELECT ID as comment_post_ID, comment_count as ccount FROM $wpdb->posts WHERE ID in ($post_id_list)" );
    13141310
    13151311    if ( $comment_counts ) {
  • trunk/wp-includes/version.php

    r3092 r3104  
    44
    55$wp_version = '1.6-ALPHA-2-still-dont-use';
    6 $wp_db_version = 3092;
     6$wp_db_version = 3104;
    77
    88?>
Note: See TracChangeset for help on using the changeset viewer.