Posts, Post Types: In `wp_delete_post()`, `wp_trash_post()`, `wp_untrash_post()`, and `wp_delete_attachment()`, standardize on `WP_Post` as a return value and internal representation of the post data.

Props bor0, SergeyBiryukov.
Fixes #42030.

git-svn-id: https://develop.svn.wordpress.org/trunk@41642 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2017-09-29 13:35:42 +00:00
parent 3edd5dac38
commit e549a6c53c
1 changed files with 49 additions and 29 deletions

View File

@ -2420,19 +2420,26 @@ function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) {
* @param int $postid Optional. Post ID. Default 0. * @param int $postid Optional. Post ID. Default 0.
* @param bool $force_delete Optional. Whether to bypass trash and force deletion. * @param bool $force_delete Optional. Whether to bypass trash and force deletion.
* Default false. * Default false.
* @return array|false|WP_Post False on failure. * @return WP_Post|false|null Post data on success, false or null on failure.
*/ */
function wp_delete_post( $postid = 0, $force_delete = false ) { function wp_delete_post( $postid = 0, $force_delete = false ) {
global $wpdb; global $wpdb;
if ( !$post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) ) $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $postid ) );
if ( ! $post ) {
return $post; return $post;
}
if ( !$force_delete && ( $post->post_type == 'post' || $post->post_type == 'page') && get_post_status( $postid ) != 'trash' && EMPTY_TRASH_DAYS ) $post = get_post( $post );
if ( ! $force_delete && ( 'post' === $post->post_type || 'page' === $post->post_type ) && 'trash' !== get_post_status( $postid ) && EMPTY_TRASH_DAYS ) {
return wp_trash_post( $postid ); return wp_trash_post( $postid );
}
if ( $post->post_type == 'attachment' ) if ( 'attachment' === $post->post_type ) {
return wp_delete_attachment( $postid, $force_delete ); return wp_delete_attachment( $postid, $force_delete );
}
/** /**
* Filters whether a post deletion should take place. * Filters whether a post deletion should take place.
@ -2583,17 +2590,22 @@ function _reset_front_page_settings_for_post( $post_id ) {
* *
* @param int $post_id Optional. Post ID. Default is ID of the global $post * @param int $post_id Optional. Post ID. Default is ID of the global $post
* if EMPTY_TRASH_DAYS equals true. * if EMPTY_TRASH_DAYS equals true.
* @return false|array|WP_Post|null Post data array, otherwise false. * @return WP_Post|false|null Post data on success, false or null on failure.
*/ */
function wp_trash_post( $post_id = 0 ) { function wp_trash_post( $post_id = 0 ) {
if ( !EMPTY_TRASH_DAYS ) if ( ! EMPTY_TRASH_DAYS ) {
return wp_delete_post($post_id, true); return wp_delete_post( $post_id, true );
}
if ( !$post = get_post($post_id, ARRAY_A) ) $post = get_post( $post_id );
if ( ! $post ) {
return $post; return $post;
}
if ( $post['post_status'] == 'trash' ) if ( 'trash' === $post->post_status ) {
return false; return false;
}
/** /**
* Filters whether a post trashing should take place. * Filters whether a post trashing should take place.
@ -2617,13 +2629,12 @@ function wp_trash_post( $post_id = 0 ) {
*/ */
do_action( 'wp_trash_post', $post_id ); do_action( 'wp_trash_post', $post_id );
add_post_meta($post_id,'_wp_trash_meta_status', $post['post_status']); add_post_meta( $post_id, '_wp_trash_meta_status', $post->post_status );
add_post_meta($post_id,'_wp_trash_meta_time', time()); add_post_meta( $post_id, '_wp_trash_meta_time', time() );
$post['post_status'] = 'trash'; wp_update_post( array( 'ID' => $post_id, 'post_status' => 'trash' ) );
wp_insert_post( wp_slash( $post ) );
wp_trash_post_comments($post_id); wp_trash_post_comments( $post_id );
/** /**
* Fires after a post is sent to the trash. * Fires after a post is sent to the trash.
@ -2643,14 +2654,18 @@ function wp_trash_post( $post_id = 0 ) {
* @since 2.9.0 * @since 2.9.0
* *
* @param int $post_id Optional. Post ID. Default is ID of the global $post. * @param int $post_id Optional. Post ID. Default is ID of the global $post.
* @return WP_Post|false WP_Post object. False on failure. * @return WP_Post|false|null Post data on success, false or null on failure.
*/ */
function wp_untrash_post( $post_id = 0 ) { function wp_untrash_post( $post_id = 0 ) {
if ( !$post = get_post($post_id, ARRAY_A) ) $post = get_post( $post_id );
return $post;
if ( $post['post_status'] != 'trash' ) if ( ! $post ) {
return $post;
}
if ( 'trash' !== $post->post_status ) {
return false; return false;
}
/** /**
* Filters whether a post untrashing should take place. * Filters whether a post untrashing should take place.
@ -2674,16 +2689,14 @@ function wp_untrash_post( $post_id = 0 ) {
*/ */
do_action( 'untrash_post', $post_id ); do_action( 'untrash_post', $post_id );
$post_status = get_post_meta($post_id, '_wp_trash_meta_status', true); $post_status = get_post_meta( $post_id, '_wp_trash_meta_status', true );
$post['post_status'] = $post_status; delete_post_meta( $post_id, '_wp_trash_meta_status' );
delete_post_meta( $post_id, '_wp_trash_meta_time' );
delete_post_meta($post_id, '_wp_trash_meta_status'); wp_update_post( array( 'ID' => $post_id, 'post_status' => $post_status ) );
delete_post_meta($post_id, '_wp_trash_meta_time');
wp_insert_post( wp_slash( $post ) ); wp_untrash_post_comments( $post_id );
wp_untrash_post_comments($post_id);
/** /**
* Fires after a post is restored from the trash. * Fires after a post is restored from the trash.
@ -4885,19 +4898,26 @@ function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = fa
* @param int $post_id Attachment ID. * @param int $post_id Attachment ID.
* @param bool $force_delete Optional. Whether to bypass trash and force deletion. * @param bool $force_delete Optional. Whether to bypass trash and force deletion.
* Default false. * Default false.
* @return mixed False on failure. Post data on success. * @return WP_Post|false|null Post data on success, false or null on failure.
*/ */
function wp_delete_attachment( $post_id, $force_delete = false ) { function wp_delete_attachment( $post_id, $force_delete = false ) {
global $wpdb; global $wpdb;
if ( !$post = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id) ) ) $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id ) );
if ( ! $post ) {
return $post; return $post;
}
if ( 'attachment' != $post->post_type ) $post = get_post( $post );
if ( 'attachment' !== $post->post_type ) {
return false; return false;
}
if ( !$force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' != $post->post_status ) if ( ! $force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' !== $post->post_status ) {
return wp_trash_post( $post_id ); return wp_trash_post( $post_id );
}
delete_post_meta($post_id, '_wp_trash_meta_status'); delete_post_meta($post_id, '_wp_trash_meta_status');
delete_post_meta($post_id, '_wp_trash_meta_time'); delete_post_meta($post_id, '_wp_trash_meta_time');