Make WordPress Core

Changeset 41642


Ignore:
Timestamp:
09/29/2017 01:35:42 PM (9 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r41638 r41642  
    24212421 * @param bool $force_delete Optional. Whether to bypass trash and force deletion.
    24222422 *                           Default false.
    2423  * @return array|false|WP_Post False on failure.
     2423 * @return WP_Post|false|null Post data on success, false or null on failure.
    24242424 */
    24252425function wp_delete_post( $postid = 0, $force_delete = false ) {
    24262426        global $wpdb;
    24272427
    2428         if ( !$post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) )
     2428        $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $postid ) );
     2429
     2430        if ( ! $post ) {
    24292431                return $post;
    2430 
    2431         if ( !$force_delete && ( $post->post_type == 'post' || $post->post_type == 'page') && get_post_status( $postid ) != 'trash' && EMPTY_TRASH_DAYS )
     2432        }
     2433
     2434        $post = get_post( $post );
     2435
     2436        if ( ! $force_delete && ( 'post' === $post->post_type || 'page' === $post->post_type ) && 'trash' !== get_post_status( $postid ) && EMPTY_TRASH_DAYS ) {
    24322437                return wp_trash_post( $postid );
    2433 
    2434         if ( $post->post_type == 'attachment' )
     2438        }
     2439
     2440        if ( 'attachment' === $post->post_type ) {
    24352441                return wp_delete_attachment( $postid, $force_delete );
     2442        }
    24362443
    24372444        /**
     
    25842591 * @param int $post_id Optional. Post ID. Default is ID of the global $post
    25852592 *                     if EMPTY_TRASH_DAYS equals true.
    2586  * @return false|array|WP_Post|null Post data array, otherwise false.
     2593 * @return WP_Post|false|null Post data on success, false or null on failure.
    25872594 */
    25882595function wp_trash_post( $post_id = 0 ) {
    2589         if ( !EMPTY_TRASH_DAYS )
    2590                 return wp_delete_post($post_id, true);
    2591 
    2592         if ( !$post = get_post($post_id, ARRAY_A) )
     2596        if ( ! EMPTY_TRASH_DAYS ) {
     2597                return wp_delete_post( $post_id, true );
     2598        }
     2599
     2600        $post = get_post( $post_id );
     2601
     2602        if ( ! $post ) {
    25932603                return $post;
    2594 
    2595         if ( $post['post_status'] == 'trash' )
     2604        }
     2605
     2606        if ( 'trash' === $post->post_status ) {
    25962607                return false;
     2608        }
    25972609
    25982610        /**
     
    26182630        do_action( 'wp_trash_post', $post_id );
    26192631
    2620         add_post_meta($post_id,'_wp_trash_meta_status', $post['post_status']);
    2621         add_post_meta($post_id,'_wp_trash_meta_time', time());
    2622 
    2623         $post['post_status'] = 'trash';
    2624         wp_insert_post( wp_slash( $post ) );
    2625 
    2626         wp_trash_post_comments($post_id);
     2632        add_post_meta( $post_id, '_wp_trash_meta_status', $post->post_status );
     2633        add_post_meta( $post_id, '_wp_trash_meta_time', time() );
     2634
     2635        wp_update_post( array( 'ID' => $post_id, 'post_status' => 'trash' ) );
     2636
     2637        wp_trash_post_comments( $post_id );
    26272638
    26282639        /**
     
    26442655 *
    26452656 * @param int $post_id Optional. Post ID. Default is ID of the global $post.
    2646  * @return WP_Post|false WP_Post object. False on failure.
     2657 * @return WP_Post|false|null Post data on success, false or null on failure.
    26472658 */
    26482659function wp_untrash_post( $post_id = 0 ) {
    2649         if ( !$post = get_post($post_id, ARRAY_A) )
     2660        $post = get_post( $post_id );
     2661
     2662        if ( ! $post ) {
    26502663                return $post;
    2651 
    2652         if ( $post['post_status'] != 'trash' )
     2664        }
     2665
     2666        if ( 'trash' !== $post->post_status ) {
    26532667                return false;
     2668        }
    26542669
    26552670        /**
     
    26752690        do_action( 'untrash_post', $post_id );
    26762691
    2677         $post_status = get_post_meta($post_id, '_wp_trash_meta_status', true);
    2678 
    2679         $post['post_status'] = $post_status;
    2680 
    2681         delete_post_meta($post_id, '_wp_trash_meta_status');
    2682         delete_post_meta($post_id, '_wp_trash_meta_time');
    2683 
    2684         wp_insert_post( wp_slash( $post ) );
    2685 
    2686         wp_untrash_post_comments($post_id);
     2692        $post_status = get_post_meta( $post_id, '_wp_trash_meta_status', true );
     2693
     2694        delete_post_meta( $post_id, '_wp_trash_meta_status' );
     2695        delete_post_meta( $post_id, '_wp_trash_meta_time' );
     2696
     2697        wp_update_post( array( 'ID' => $post_id, 'post_status' => $post_status ) );
     2698
     2699        wp_untrash_post_comments( $post_id );
    26872700
    26882701        /**
     
    48864899 * @param bool $force_delete Optional. Whether to bypass trash and force deletion.
    48874900 *                           Default false.
    4888  * @return mixed False on failure. Post data on success.
     4901 * @return WP_Post|false|null Post data on success, false or null on failure.
    48894902 */
    48904903function wp_delete_attachment( $post_id, $force_delete = false ) {
    48914904        global $wpdb;
    48924905
    4893         if ( !$post = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id) ) )
     4906        $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id ) );
     4907
     4908        if ( ! $post ) {
    48944909                return $post;
    4895 
    4896         if ( 'attachment' != $post->post_type )
     4910        }
     4911
     4912        $post = get_post( $post );
     4913
     4914        if ( 'attachment' !== $post->post_type ) {
    48974915                return false;
    4898 
    4899         if ( !$force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' != $post->post_status )
     4916        }
     4917
     4918        if ( ! $force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' !== $post->post_status ) {
    49004919                return wp_trash_post( $post_id );
     4920        }
    49014921
    49024922        delete_post_meta($post_id, '_wp_trash_meta_status');
Note: See TracChangeset for help on using the changeset viewer.