Trash comments when trashing a post. see #11073

git-svn-id: http://svn.automattic.com/wordpress/trunk@12148 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2009-11-05 21:03:09 +00:00
parent 13c02951d1
commit 33a8043b97
3 changed files with 102 additions and 2 deletions

View File

@ -30,6 +30,9 @@ if ( empty($status->comment_status) ) {
} elseif ( in_array($status->post_status, array('draft', 'pending') ) ) {
do_action('comment_on_draft', $comment_post_ID);
exit;
} elseif ( 'trash' == $status->post_status ) {
do_action('comment_on_trash', $comment_post_ID);
exit;
} else {
do_action('pre_comment_on_post', $comment_post_ID);
}

View File

@ -773,10 +773,12 @@ function wp_count_comments( $post_id = 0 ) {
$count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A );
$total = 0;
$approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash');
$approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
$known_types = array_keys( $approved );
foreach( (array) $count as $row_num => $row ) {
$total += $row['num_comments'];
// Don't count post-trashed toward totals
if ( 'post-trashed' != $row['comment_approved'] )
$total += $row['num_comments'];
if ( in_array( $row['comment_approved'], $known_types ) )
$stats[$approved[$row['comment_approved']]] = $row['num_comments'];
}
@ -1178,9 +1180,13 @@ function wp_set_comment_status($comment_id, $comment_status, $wp_error = false)
$status = '0';
switch ( $comment_status ) {
case 'hold':
case '0':
case 0:
$status = '0';
break;
case 'approve':
case '1':
case 1:
$status = '1';
if ( get_option('comments_notify') ) {
$comment = get_comment($comment_id);

View File

@ -1268,6 +1268,8 @@ function wp_trash_post($post_id = 0) {
$post['post_status'] = 'trash';
wp_insert_post($post);
wp_trash_post_comments($post_id);
do_action('trashed_post', $post_id);
return $post;
@ -1301,11 +1303,100 @@ function wp_untrash_post($post_id = 0) {
wp_insert_post($post);
wp_untrash_post_comments($post_id);
do_action('untrashed_post', $post_id);
return $post;
}
/**
* Moves comments for a post to the trash
*
* @since 2.9.0
* @uses do_action() on 'trash_post_comments' before trashing
* @uses do_action() on 'trashed_post_comments' after trashing
*
* @param int $post Post ID or object.
* @return mixed False on failure
*/
function wp_trash_post_comments($post = null) {
global $wpdb;
$post = get_post($post);
if ( empty($post) )
return;
$post_id = $post->ID;
do_action('trash_post_comments', $post_id);
$comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id) );
if ( empty($comments) )
return;
// Cache current status for each comment
$statuses = array();
foreach ( $comments as $comment )
$statuses[$comment->comment_ID] = $comment->comment_approved;
add_post_meta($post_id, '_wp_trash_meta_comments_status', $statuses);
// Set status for all comments to post-trashed
$result = $wpdb->update($wpdb->comments, array('comment_approved' => 'post-trashed'), array('comment_post_ID' => $post_id));
clean_comment_cache( array_keys($statuses) );
do_action('trashed_post_comments', $post_id, $statuses);
return $result;
}
/**
* Restore comments for a post from the trash
*
* @since 2.9.0
* @uses do_action() on 'untrash_post_comments' before trashing
* @uses do_action() on 'untrashed_post_comments' after trashing
*
* @param int $post Post ID or object.
* @return mixed False on failure
*/
function wp_untrash_post_comments($post = null) {
global $wpdb;
$post = get_post($post);
if ( empty($post) )
return;
$post_id = $post->ID;
$statuses = get_post_meta($post_id, '_wp_trash_meta_comments_status', true);
if ( empty($statuses) )
return true;
do_action('untrash_post_comments', $post_id);
// Restore each comment to its original status
$group_by_status = array();
foreach ( $statuses as $comment_id => $comment_status )
$group_by_status[$comment_status][] = $comment_id;
foreach ( $group_by_status as $status => $comments ) {
// Sanity check. This shouldn't happen.
if ( 'post-trashed' == $status )
$status = '0';
$comments_in = implode( "', '", $comments );
$wpdb->query( "UPDATE $wpdb->comments SET comment_approved = '$status' WHERE comment_ID IN ('" . $comments_in . "')" );
}
clean_comment_cache( array_keys($statuses) );
delete_post_meta($post_id, '_wp_trash_meta_comments_status');
do_action('untrashed_post_comments', $post_id);
}
/**
* Retrieve the list of categories for a post.
*