From 25b644e8cedb79f6d345eabd9c1905e9e34bad23 Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 16 Nov 2005 06:29:36 +0000 Subject: [PATCH] Add comment_count to the posts table. Props donncha. fixes #1860 git-svn-id: http://svn.automattic.com/wordpress/trunk@3104 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/list-manipulation.php | 3 +-- wp-admin/upgrade-functions.php | 12 ++++++++-- wp-admin/upgrade-schema.php | 1 + wp-includes/comment-functions.php | 40 +++++++++++++++++++++++++++---- wp-includes/functions.php | 6 +---- wp-includes/version.php | 2 +- 6 files changed, 50 insertions(+), 14 deletions(-) diff --git a/wp-admin/list-manipulation.php b/wp-admin/list-manipulation.php index b056ff4e74..97f144abbb 100644 --- a/wp-admin/list-manipulation.php +++ b/wp-admin/list-manipulation.php @@ -56,8 +56,7 @@ case 'delete-comment' : if ( !current_user_can('edit_post', $comment->comment_post_ID) ) die('-1'); - if ( wp_set_comment_status($comment->comment_ID, "delete") ) { - do_action('delete_comment', $comment->comment_ID); + if ( wp_delete_comment($comment->comment_ID) ) { die('1'); } else { die('0'); diff --git a/wp-admin/upgrade-functions.php b/wp-admin/upgrade-functions.php index 800d47e40b..6a17548f08 100644 --- a/wp-admin/upgrade-functions.php +++ b/wp-admin/upgrade-functions.php @@ -30,7 +30,7 @@ function upgrade_all() { upgrade_130(); } - if ( $wp_current_db_version < 3092 ) + if ( $wp_current_db_version < 3104 ) upgrade_160(); save_mod_rewrite_rules(); @@ -300,7 +300,15 @@ function upgrade_160() { $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'"); } } - + + // populate comment_count field of posts table + $comments = $wpdb->get_results( "SELECT comment_post_ID, COUNT(*) as c FROM $wpdb->comments GROUP BY comment_post_ID" ); + if( is_array( $comments ) ) { + foreach ($comments as $comment) { + $wpdb->query( "UPDATE $wpdb->posts SET comment_count = $comment->c WHERE ID = '$comment->comment_post_ID}'" ); + } + } + // Some alpha versions used a post status of object instead of attachment and put // the mime type in post_type instead of post_mime_type. if ( $wp_current_db_version > 2541 && $wp_current_db_version <= 3091 ) { diff --git a/wp-admin/upgrade-schema.php b/wp-admin/upgrade-schema.php index 0538df8bfd..d5b00f2477 100644 --- a/wp-admin/upgrade-schema.php +++ b/wp-admin/upgrade-schema.php @@ -121,6 +121,7 @@ CREATE TABLE $wpdb->posts ( menu_order int(11) NOT NULL default '0', post_type varchar(100) NOT NULL, post_mime_type varchar(100) NOT NULL, + comment_count bigint(20) NOT NULL default '0', PRIMARY KEY (ID), KEY post_name (post_name) ); diff --git a/wp-includes/comment-functions.php b/wp-includes/comment-functions.php index 566d8efe6e..ee1a666fe8 100644 --- a/wp-includes/comment-functions.php +++ b/wp-includes/comment-functions.php @@ -81,7 +81,12 @@ function wp_insert_comment($commentdata) { ('$comment_post_ID', '$comment_author', '$comment_author_email', '$comment_author_url', '$comment_author_IP', '$comment_date', '$comment_date_gmt', '$comment_content', '$comment_approved', '$comment_agent', '$comment_type', '$comment_parent', '$user_id') "); - return $wpdb->insert_id; + $id = $wpdb->insert_id; + + if ( $comment_approved == 1) + $wpdb->query( "UPDATE $wpdb->posts SET comment_count = comment_count + 1 WHERE ID = '$comment_post_ID'" ); + + return $id; } function wp_filter_comment($commentdata) { @@ -176,9 +181,30 @@ function wp_update_comment($commentarr) { $rval = $wpdb->rows_affected; + $c = $wpdb->get_row( "SELECT count(*) as c FROM {$wpdb->comments} WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'" ); + if( is_object( $c ) ) + $wpdb->query( "UPDATE $wpdb->posts SET comment_count = '$c->c' WHERE ID = '$comment_post_ID'" ); + do_action('edit_comment', $comment_ID); - return $rval; + return $rval; +} + +function wp_delete_comment($comment_id) { + global $wpdb; + do_action('delete_comment', $comment_id); + + $comment = get_comment($comment_id); + + if ( ! $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1") ) + return false; + + $post_id = $comment->comment_post_ID; + if ( $post_id ) + $wpdb->query( "UPDATE $wpdb->posts SET comment_count = comment_count - 1 WHERE ID = '$post_id'" ); + + do_action('wp_set_comment_status', $comment_id, 'delete'); + return true; } function clean_url( $url ) { @@ -198,7 +224,7 @@ function get_comments_number( $post_id = 0 ) { $post_id = $id; if ( !isset($comment_count_cache[$post_id]) ) - $comment_count_cache[$post_id] = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$post_id' AND comment_approved = '1'"); + $comment_count_cache[$id] = $wpdb->get_var("SELECT comment_count FROM $wpdb->posts WHERE ID = '$post_id'"); return apply_filters('get_comments_number', $comment_count_cache[$post_id]); } @@ -742,7 +768,7 @@ function wp_set_comment_status($comment_id, $comment_status) { $query = "UPDATE $wpdb->comments SET comment_approved='spam' WHERE comment_ID='$comment_id' LIMIT 1"; break; case 'delete': - $query = "DELETE FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1"; + return wp_delete_comment($comment_id); break; default: return false; @@ -750,6 +776,12 @@ function wp_set_comment_status($comment_id, $comment_status) { if ($wpdb->query($query)) { do_action('wp_set_comment_status', $comment_id, $comment_status); + + $comment = get_comment($comment_id); + $comment_post_ID = $comment->comment_post_ID; + $c = $wpdb->get_row( "SELECT count(*) as c FROM {$wpdb->comments} WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'" ); + if( is_object( $c ) ) + $wpdb->query( "UPDATE $wpdb->posts SET comment_count = '$c->c' WHERE ID = '$comment_post_ID'" ); return true; } else { return false; diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 9a64a87903..281bd613e0 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -1306,11 +1306,7 @@ function update_post_caches(&$posts) { update_post_category_cache($post_id_list); // Do the same for comment numbers - $comment_counts = $wpdb->get_results("SELECT comment_post_ID, COUNT( comment_ID ) AS ccount - FROM $wpdb->comments - WHERE comment_post_ID IN ($post_id_list) - AND comment_approved = '1' - GROUP BY comment_post_ID"); + $comment_counts = $wpdb->get_results( "SELECT ID as comment_post_ID, comment_count as ccount FROM $wpdb->posts WHERE ID in ($post_id_list)" ); if ( $comment_counts ) { foreach ($comment_counts as $comment_count) { diff --git a/wp-includes/version.php b/wp-includes/version.php index fccbeda6bc..767eb8c305 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -3,6 +3,6 @@ // This just holds the version number, in a separate file so we can bump it without cluttering the SVN $wp_version = '1.6-ALPHA-2-still-dont-use'; -$wp_db_version = 3092; +$wp_db_version = 3104; ?>