Make WordPress Core


Ignore:
Timestamp:
07/30/2009 01:39:34 PM (17 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/post.php

    r11734 r11749  
    999999        $count = $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
    10001000
    1001         $stats = array( 'publish' => 0, 'private' => 0, 'draft' => 0, 'pending' => 0, 'future' => 0 );
     1001        $stats = array( 'publish' => 0, 'private' => 0, 'draft' => 0, 'pending' => 0, 'future' => 0, 'trash' => 0 );
    10021002        foreach( (array) $count as $row_num => $row ) {
    10031003                $stats[$row['post_status']] = $row['num_posts'];
     
    10281028
    10291029        $and = wp_post_mime_type_where( $mime_type );
    1030         $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' $and GROUP BY post_mime_type", ARRAY_A );
     1030        $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A );
    10311031
    10321032        $stats = array( );
     
    10341034                $stats[$row['post_mime_type']] = $row['num_posts'];
    10351035        }
     1036        $stats['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and");
    10361037
    10371038        return (object) $stats;
     
    11411142                return $post;
    11421143
    1143         if ( 'attachment' == $post->post_type )
     1144        if ( ($post->post_type == 'post' || $post->post_type == 'page') && get_post_status($postid) != 'trash' && EMPTY_TRASH_DAYS > 0 )
     1145                return wp_trash_post($postid);
     1146               
     1147        if ( $post->post_type == 'attachment' )
    11441148                return wp_delete_attachment($postid);
    11451149
    11461150        do_action('delete_post', $postid);
     1151       
     1152        $trash_meta = get_option('wp_trash_meta');
     1153        if ( is_array($trash_meta) && isset($trash_meta['posts'][$postid]) ) {
     1154                unset($trash_meta['posts'][$postid]);
     1155                update_option('wp_trash_meta', $trash_meta);
     1156        }
    11471157
    11481158        /** @todo delete for pluggable post taxonomies too */
     
    12011211
    12021212        do_action('deleted_post', $postid);
     1213
     1214        return $post;
     1215}
     1216
     1217/**
     1218 * Moves a post or page to the Trash
     1219 *
     1220 * @since 2.9.0
     1221 * @uses do_action() on 'trash_post' before trashing
     1222 * @uses do_action() on 'trashed_post' after trashing
     1223 *
     1224 * @param int $postid Post ID.
     1225 * @return mixed False on failure
     1226 */
     1227function wp_trash_post($postid = 0) {
     1228        if ( EMPTY_TRASH_DAYS == 0 )
     1229                return wp_delete_post($postid);
     1230
     1231        if ( !$post = wp_get_single_post($postid, ARRAY_A) )
     1232                return $post;
     1233
     1234        do_action('trash_post', $postid);
     1235
     1236        $trash_meta = get_option('wp_trash_meta');
     1237        if ( !is_array($trash_meta) )
     1238                $trash_meta = array();
     1239        $trash_meta['posts'][$postid]['status'] = $post['post_status'];
     1240        $trash_meta['posts'][$postid]['time'] = time();
     1241        update_option('wp_trash_meta', $trash_meta);
     1242
     1243        $post['post_status'] = 'trash';
     1244        wp_insert_post($post);
     1245
     1246        do_action('trashed_post', $postid);
     1247
     1248        return $post;
     1249}
     1250
     1251/**
     1252 * Removes a post or page from the Trash
     1253 *
     1254 * @since 2.9.0
     1255 * @uses do_action() on 'untrash_post' before undeletion
     1256 * @uses do_action() on 'untrashed_post' after undeletion
     1257 *
     1258 * @param int $postid Post ID.
     1259 * @return mixed False on failure
     1260 */
     1261function wp_untrash_post($postid = 0) {
     1262        if (!$post = wp_get_single_post($postid, ARRAY_A))
     1263                return $post;
     1264
     1265        do_action('untrash_post', $postid);
     1266       
     1267        $post['post_status'] = 'draft';
     1268       
     1269        $trash_meta = get_option('wp_trash_meta');
     1270        if (is_array($trash_meta) && isset($trash_meta['posts'][$postid])) {
     1271                $post['post_status'] = $trash_meta['posts'][$postid]['status'];
     1272                unset($trash_meta['posts'][$postid]);
     1273                update_option('wp_trash_meta', $trash_meta);
     1274        }
     1275       
     1276        wp_insert_post($post);
     1277       
     1278        do_action('untrashed_post', $postid);
    12031279
    12041280        return $post;
     
    25872663        if ( 'attachment' != $post->post_type )
    25882664                return false;
     2665       
     2666        if ( 'trash' != $post->post_status )
     2667                return wp_trash_post($postid);
     2668
     2669        $trash_meta = get_option('wp_trash_meta');
     2670        if ( is_array($trash_meta) && isset($trash_meta['posts'][$postid]) ) {
     2671                unset($trash_meta['posts'][$postid]);
     2672                update_option('wp_trash_meta', $trash_meta);
     2673        }
    25892674
    25902675        $meta = wp_get_attachment_metadata( $postid );
Note: See TracChangeset for help on using the changeset viewer.