From 2cfb1592f005f8ecaadf4670fd5dfd161889dcf1 Mon Sep 17 00:00:00 2001 From: ryan Date: Sun, 22 May 2011 23:25:28 +0000 Subject: [PATCH] Mark import attachments as private. Schedule job to delete old import attachments. Introduce attachment context. git-svn-id: http://svn.automattic.com/wordpress/trunk@17999 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/custom-background.php | 3 ++- wp-admin/custom-header.php | 6 ++++-- wp-admin/includes/import.php | 7 ++++++- wp-admin/includes/post.php | 7 ++++++- wp-includes/default-filters.php | 1 + wp-includes/post.php | 27 +++++++++++++++++++-------- 6 files changed, 38 insertions(+), 13 deletions(-) diff --git a/wp-admin/custom-background.php b/wp-admin/custom-background.php index 5bd996b75..28eec84e8 100644 --- a/wp-admin/custom-background.php +++ b/wp-admin/custom-background.php @@ -338,7 +338,8 @@ if ( get_background_image() ) { 'post_title' => $filename, 'post_content' => $url, 'post_mime_type' => $type, - 'guid' => $url + 'guid' => $url, + 'context' => 'custom-background' ); // Save the data diff --git a/wp-admin/custom-header.php b/wp-admin/custom-header.php index e58346e11..18fe4857f 100644 --- a/wp-admin/custom-header.php +++ b/wp-admin/custom-header.php @@ -644,7 +644,8 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?> 'post_title' => $filename, 'post_content' => $url, 'post_mime_type' => $type, - 'guid' => $url); + 'guid' => $url, + 'context' => 'custom-header'); // Save the data $id = wp_insert_attachment($object, $file); @@ -737,7 +738,8 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?> 'post_title' => basename($cropped), 'post_content' => $url, 'post_mime_type' => 'image/jpeg', - 'guid' => $url + 'guid' => $url, + 'context' => 'custom-header' ); // Update the attachment diff --git a/wp-admin/includes/import.php b/wp-admin/includes/import.php index 51861bdcd..f57be766c 100644 --- a/wp-admin/includes/import.php +++ b/wp-admin/includes/import.php @@ -80,12 +80,17 @@ function wp_import_handle_upload() { $object = array( 'post_title' => $filename, 'post_content' => $url, 'post_mime_type' => $type, - 'guid' => $url + 'guid' => $url, + 'context' => 'import', + 'post_status' => 'private' ); // Save the data $id = wp_insert_attachment( $object, $file ); + // schedule a cleanup for one day from now in case of failed import or missing wp_import_cleanup() call + wp_schedule_single_event( time() + 86400, 'importer_scheduled_cleanup', array( $id ) ); + return array( 'file' => $file, 'id' => $id ); } diff --git a/wp-admin/includes/post.php b/wp-admin/includes/post.php index a5a56ef7c..237e3818c 100644 --- a/wp-admin/includes/post.php +++ b/wp-admin/includes/post.php @@ -996,7 +996,12 @@ function wp_edit_attachments_query( $q = false ) { $q['m'] = isset( $q['m'] ) ? (int) $q['m'] : 0; $q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0; $q['post_type'] = 'attachment'; - $q['post_status'] = isset( $q['status'] ) && 'trash' == $q['status'] ? 'trash' : 'inherit'; + $post_type = get_post_type_object( 'attachment' ); + $states = array( 'inherit' ); + if ( current_user_can( $post_type->cap->read_private_posts ) ) + $states[] = 'private'; + + $q['post_status'] = isset( $q['status'] ) && 'trash' == $q['status'] ? 'trash' : $states; $media_per_page = (int) get_user_option( 'upload_per_page' ); if ( empty( $media_per_page ) || $media_per_page < 1 ) $media_per_page = 20; diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php index 7584a491b..a5b6150a5 100644 --- a/wp-includes/default-filters.php +++ b/wp-includes/default-filters.php @@ -257,6 +257,7 @@ add_action( 'wp_scheduled_delete', 'wp_scheduled_delete' ); add_action( 'before_wp_tiny_mce', 'wp_print_editor_js' ); add_action( 'after_wp_tiny_mce', 'wp_preload_dialogs', 10, 1 ); add_action( 'admin_init', 'send_frame_options_header', 10, 0 ); +add_action( 'importer_scheduled_cleanup', 'wp_delete_attachment' ); // Navigation menu actions add_action( 'delete_post', '_wp_delete_post_menu_item' ); diff --git a/wp-includes/post.php b/wp-includes/post.php index a930eda63..efec91be8 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -557,12 +557,18 @@ function get_post_status($ID = '') { if ( !is_object($post) ) return false; - // Unattached attachments are assumed to be published. - if ( ('attachment' == $post->post_type) && ('inherit' == $post->post_status) && ( 0 == $post->post_parent) ) - return 'publish'; + if ( 'attachment' == $post->post_type ) { + if ( 'private' == $post->post_status ) + return 'private'; - if ( ('attachment' == $post->post_type) && $post->post_parent && ($post->ID != $post->post_parent) ) - return get_post_status($post->post_parent); + // Unattached attachments are assumed to be published + if ( ( 'inherit' == $post->post_status ) && ( 0 == $post->post_parent) ) + return 'publish'; + + // Inherit status from the parent + if ( $post->post_parent && ( $post->ID != $post->post_parent ) ) + return get_post_status($post->post_parent); + } return $post->post_status; } @@ -3543,10 +3549,10 @@ function is_local_attachment($url) { function wp_insert_attachment($object, $file = false, $parent = 0) { global $wpdb, $user_ID; - $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID, + $defaults = array('post_status' => 'inherit', 'post_type' => 'post', 'post_author' => $user_ID, 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', - 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0); + 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0, 'context' => ''); $object = wp_parse_args($object, $defaults); if ( !empty($parent) ) @@ -3561,7 +3567,9 @@ function wp_insert_attachment($object, $file = false, $parent = 0) { $post_author = $user_ID; $post_type = 'attachment'; - $post_status = 'inherit'; + + if ( ! in_array( $post_status, array( 'inherit', 'private' ) ) ) + $post_status = 'inherit'; // Make sure we set a valid category. if ( !isset($post_category) || 0 == count($post_category) || !is_array($post_category) ) { @@ -3664,6 +3672,9 @@ function wp_insert_attachment($object, $file = false, $parent = 0) { if ( isset($post_parent) && $post_parent < 0 ) add_post_meta($post_ID, '_wp_attachment_temp_parent', $post_parent, true); + if ( ! empty( $context ) ) + add_post_meta( $post_ID, '_wp_attachment_context', $context, true ); + if ( $update) { do_action('edit_attachment', $post_ID); } else {