Make WordPress Core

Changeset 12148


Ignore:
Timestamp:
11/05/2009 09:03:09 PM (15 years ago)
Author:
ryan
Message:

Trash comments when trashing a post. see #11073

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-comments-post.php

    r11383 r12148  
    3030} elseif ( in_array($status->post_status, array('draft', 'pending') ) ) {
    3131    do_action('comment_on_draft', $comment_post_ID);
     32    exit;
     33} elseif ( 'trash' == $status->post_status ) {
     34    do_action('comment_on_trash', $comment_post_ID);
    3235    exit;
    3336} else {
  • trunk/wp-includes/comment.php

    r12122 r12148  
    774774
    775775    $total = 0;
    776     $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash');
     776    $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
    777777    $known_types = array_keys( $approved );
    778778    foreach( (array) $count as $row_num => $row ) {
    779         $total += $row['num_comments'];
     779        // Don't count post-trashed toward totals
     780        if ( 'post-trashed' != $row['comment_approved'] )
     781            $total += $row['num_comments'];
    780782        if ( in_array( $row['comment_approved'], $known_types ) )
    781783            $stats[$approved[$row['comment_approved']]] = $row['num_comments'];
     
    11791181    switch ( $comment_status ) {
    11801182        case 'hold':
     1183        case '0':
     1184        case 0:
    11811185            $status = '0';
    11821186            break;
    11831187        case 'approve':
     1188        case '1':
     1189        case 1:
    11841190            $status = '1';
    11851191            if ( get_option('comments_notify') ) {
  • trunk/wp-includes/post.php

    r12141 r12148  
    12691269    wp_insert_post($post);
    12701270
     1271    wp_trash_post_comments($post_id);
     1272
    12711273    do_action('trashed_post', $post_id);
    12721274
     
    13021304    wp_insert_post($post);
    13031305
     1306    wp_untrash_post_comments($post_id);
     1307
    13041308    do_action('untrashed_post', $post_id);
    13051309
    13061310    return $post;
     1311}
     1312
     1313/**
     1314 * Moves comments for a post to the trash
     1315 *
     1316 * @since 2.9.0
     1317 * @uses do_action() on 'trash_post_comments' before trashing
     1318 * @uses do_action() on 'trashed_post_comments' after trashing
     1319 *
     1320 * @param int $post Post ID or object.
     1321 * @return mixed False on failure
     1322 */
     1323function wp_trash_post_comments($post = null) {
     1324    global $wpdb;
     1325
     1326    $post = get_post($post);
     1327    if ( empty($post) )
     1328        return;
     1329
     1330    $post_id = $post->ID;
     1331
     1332    do_action('trash_post_comments', $post_id);
     1333
     1334    $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id) );
     1335    if ( empty($comments) )
     1336        return;
     1337
     1338    // Cache current status for each comment
     1339    $statuses = array();
     1340    foreach ( $comments as $comment )
     1341        $statuses[$comment->comment_ID] = $comment->comment_approved;
     1342    add_post_meta($post_id, '_wp_trash_meta_comments_status', $statuses);
     1343
     1344    // Set status for all comments to post-trashed
     1345    $result = $wpdb->update($wpdb->comments, array('comment_approved' => 'post-trashed'), array('comment_post_ID' => $post_id));
     1346
     1347    clean_comment_cache( array_keys($statuses) );
     1348
     1349    do_action('trashed_post_comments', $post_id, $statuses);
     1350
     1351    return $result;
     1352}
     1353
     1354/**
     1355 * Restore comments for a post from the trash
     1356 *
     1357 * @since 2.9.0
     1358 * @uses do_action() on 'untrash_post_comments' before trashing
     1359 * @uses do_action() on 'untrashed_post_comments' after trashing
     1360 *
     1361 * @param int $post Post ID or object.
     1362 * @return mixed False on failure
     1363 */
     1364function wp_untrash_post_comments($post = null) {
     1365    global $wpdb;
     1366
     1367    $post = get_post($post);
     1368    if ( empty($post) )
     1369        return;
     1370
     1371    $post_id = $post->ID;
     1372
     1373    $statuses = get_post_meta($post_id, '_wp_trash_meta_comments_status', true);
     1374
     1375    if ( empty($statuses) )
     1376        return true;
     1377
     1378    do_action('untrash_post_comments', $post_id);
     1379
     1380    // Restore each comment to its original status
     1381    $group_by_status = array();
     1382    foreach ( $statuses as $comment_id => $comment_status )
     1383        $group_by_status[$comment_status][] = $comment_id;
     1384
     1385    foreach ( $group_by_status as $status => $comments ) {
     1386        // Sanity check. This shouldn't happen.
     1387        if ( 'post-trashed' == $status )
     1388            $status = '0';
     1389        $comments_in = implode( "', '", $comments );
     1390        $wpdb->query( "UPDATE $wpdb->comments SET comment_approved = '$status' WHERE comment_ID IN ('" . $comments_in . "')" );
     1391    }
     1392
     1393    clean_comment_cache( array_keys($statuses) );
     1394
     1395    delete_post_meta($post_id, '_wp_trash_meta_comments_status');
     1396
     1397    do_action('untrashed_post_comments', $post_id);
    13071398}
    13081399
Note: See TracChangeset for help on using the changeset viewer.