Make WordPress Core


Ignore:
Timestamp:
07/30/2009 01:39:34 PM (15 years ago)
Author:
azaozz
Message:

Trash status updates for posts, pages, comments and attachments, props caesarsgrunt, see #4529

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/comment.php

    r11741 r11749  
    209209    elseif ( 'spam' == $status )
    210210        $approved = "comment_approved = 'spam'";
    211     elseif ( 'deleted' == $status )
    212         $approved = "comment_approved = 'deleted'";
     211    elseif ( 'trash' == $status )
     212        $approved = "comment_approved = 'trash'";
    213213    else
    214214        $approved = "( comment_approved = '0' OR comment_approved = '1' )";
     
    695695        return $count;
    696696
    697     $where = '';
     697    $where = 'WHERE ';
    698698    if( $post_id > 0 )
    699         $where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id );
    700 
    701     $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A );
     699        $where .= $wpdb->prepare( "c.comment_post_ID = %d AND ", $post_id );
     700    $where .= "p.post_status <> 'trash'";
     701
     702    $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} c LEFT JOIN {$wpdb->posts} p ON c.comment_post_ID = p.ID {$where} GROUP BY comment_approved", ARRAY_A );
    702703
    703704    $total = 0;
    704     $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'deleted' => 'deleted');
     705    $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash');
    705706    $known_types = array_keys( $approved );
    706707    foreach( (array) $count as $row_num => $row ) {
     
    738739 */
    739740function wp_delete_comment($comment_id) {
    740     if (wp_get_comment_status($comment_id) != 'deleted' && wp_get_comment_status($comment_id) != 'spam')
    741         return wp_set_comment_status($comment_id, 'delete');
     741    global $wpdb;
     742    if (!$comment = get_comment($comment_id))
     743        return false;
     744
     745    if (wp_get_comment_status($comment_id) != 'trash' && wp_get_comment_status($comment_id) != 'spam' && EMPTY_TRASH_DAYS > 0)
     746        return wp_trash_comment($comment_id);
    742747   
    743     global $wpdb;
    744748    do_action('delete_comment', $comment_id);
    745749   
    746     wp_unschedule_comment_delete($comment_id);
    747 
    748     $comment = get_comment($comment_id);
     750    $trash_meta = get_option('wp_trash_meta');
     751    if (is_array($trash_meta) && isset($trash_meta['comments'][$comment_id])) {
     752        unset($trash_meta['comments'][$comment_id]);
     753        update_option('wp_trash_meta', $trash_meta);
     754    }
    749755
    750756    if ( ! $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id) ) )
     
    770776
    771777/**
     778 * Moves a comment to the Trash
     779 *
     780 * @since 2.9.0
     781 * @uses do_action() on 'trash_comment' before trashing
     782 * @uses do_action() on 'trashed_comment' after trashing
     783 *
     784 * @param int $comment_id Comment ID.
     785 * @return mixed False on failure
     786 */
     787function wp_trash_comment($comment_id = 0) {
     788    if (EMPTY_TRASH_DAYS == 0)
     789        return wp_delete_comment($comment_id);
     790
     791    if (!$comment = get_comment($comment_id))
     792        return false;
     793
     794    do_action('trash_comment', $comment_id);
     795
     796    $trash_meta = get_option('wp_trash_meta', array());
     797    $trash_meta['comments'][$comment_id]['status'] = $comment->comment_approved;
     798    $trash_meta['comments'][$comment_id]['time'] = time();
     799    update_option('wp_trash_meta', $trash_meta);
     800
     801    wp_set_comment_status($comment_id, 'trash');
     802
     803    do_action('trashed_comment', $comment_id);
     804
     805    return true;
     806}
     807
     808/**
     809 * Removes a comment from the Trash
     810 *
     811 * @since 2.9.0
     812 * @uses do_action() on 'untrash_comment' before undeletion
     813 * @uses do_action() on 'untrashed_comment' after undeletion
     814 *
     815 * @param int $comment_id Comment ID.
     816 * @return mixed False on failure
     817 */
     818function wp_untrash_comment($comment_id = 0) {
     819    do_action('untrash_comment', $comment_id);
     820
     821    $comment = array('comment_ID'=>$comment_id, 'comment_approved'=>'0');
     822
     823    $trash_meta = get_option('wp_trash_meta');
     824    if (is_array($trash_meta) && isset($trash_meta['comments'][$comment_id])) {
     825        $comment['comment_approved'] = $trash_meta['comments'][$comment_id]['status'];
     826        unset($trash_meta['comments'][$comment_id]);
     827        update_option('wp_trash_meta', $trash_meta);
     828    }
     829
     830    wp_update_comment($comment);
     831
     832    do_action('untrashed_comment', $comment_id);
     833
     834    return true;
     835}
     836
     837/**
    772838 * The status of a comment by ID.
    773839 *
     
    775841 *
    776842 * @param int $comment_id Comment ID
    777  * @return string|bool Status might be 'deleted', 'approved', 'unapproved', 'spam'. False on failure.
     843 * @return string|bool Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure.
    778844 */
    779845function wp_get_comment_status($comment_id) {
     
    785851
    786852    if ( $approved == NULL )
    787         return 'deleted';
     853        return false;
    788854    elseif ( $approved == '1' )
    789855        return 'approved';
     
    792858    elseif ( $approved == 'spam' )
    793859        return 'spam';
    794     elseif ( $approved == 'deleted' )
    795         return 'deleted';
     860    elseif ( $approved == 'trash' )
     861        return 'trash';
    796862    else
    797863        return false;
     
    10381104function wp_set_comment_status($comment_id, $comment_status, $wp_error = false) {
    10391105    global $wpdb;
    1040     wp_unschedule_comment_delete($comment_id);
    1041    
     1106       
    10421107    $status = '0';
    10431108    switch ( $comment_status ) {
     
    10551120            $status = 'spam';
    10561121            break;
    1057         case 'delete':
    1058             if (wp_get_comment_status($comment_id) == 'deleted' || wp_get_comment_status($comment_id) == 'spam')
    1059                 return wp_delete_comment($comment_id);
    1060             $status = 'deleted';
    1061             wp_schedule_comment_delete($comment_id);
     1122        case 'trash':
     1123            $status = 'trash';
    10621124            break;
    10631125        default:
     
    10821144
    10831145    return true;
    1084 }
    1085 
    1086 /**
    1087  * Schedules a comment for destruction in 30 days.
    1088  *
    1089  * @since 2.9.0
    1090  *
    1091  * @param int $comment_id Comment ID.
    1092  * @return void
    1093  */
    1094 function wp_schedule_comment_delete($comment_id) {
    1095     $to_delete = get_option('wp_scheduled_delete');
    1096     if ( !is_array($to_delete) )
    1097         $to_delete = array();
    1098    
    1099     $to_delete['comments'][$comment_id] = time();
    1100    
    1101     update_option('wp_scheduled_delete', $to_delete);
    1102 }
    1103 
    1104 /**
    1105  * Unschedules a comment for destruction.
    1106  *
    1107  * @since 2.9.0
    1108  *
    1109  * @param int $comment_id Comment ID.
    1110  * @return void
    1111  */
    1112 function wp_unschedule_comment_delete($comment_id) {
    1113     $to_delete = get_option('wp_scheduled_delete');
    1114     if ( !is_array($to_delete) )
    1115         return;
    1116    
    1117     unset($to_delete['comments'][$comment_id]);
    1118    
    1119     update_option('wp_scheduled_delete', $to_delete);
    11201146}
    11211147
Note: See TracChangeset for help on using the changeset viewer.