Make WordPress Core


Ignore:
Timestamp:
10/11/2017 05:41:14 AM (9 years ago)
Author:
westonruter
Message:

Customize: Introduce WP_Customize_Manager::trash_changeset_post() to reduce duplication and ensure proper changeset trashing logic.

Trashing a changeset via wp_trash_post() does not have the desired result since it mutates post_content (via Kses) and the post_name (even though it is a UUID).

Props dlh.
See #39896, #42030.
Fixes #42175.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r41815 r41824  
    28322832
    28332833        /**
     2834         * Trash or delete a changeset post.
     2835         *
     2836         * The following re-formulates the logic from `wp_trash_post()` as done in
     2837         * `wp_publish_post()`. The reason for bypassing `wp_trash_post()` is that it
     2838         * will mutate the the `post_content` and the `post_name` when they should be
     2839         * untouched.
     2840         *
     2841         * @since 4.9.0
     2842         * @global wpdb $wpdb WordPress database abstraction object.
     2843         * @see wp_trash_post()
     2844         *
     2845         * @param int|WP_Post $post The changeset post.
     2846         * @return mixed A WP_Post object for the trashed post or an empty value on failure.
     2847         */
     2848        public function trash_changeset_post( $post ) {
     2849                global $wpdb;
     2850
     2851                $post = get_post( $post );
     2852
     2853                if ( ! ( $post instanceof WP_Post ) ) {
     2854                        return $post;
     2855                }
     2856                $post_id = $post->ID;
     2857
     2858                if ( ! EMPTY_TRASH_DAYS ) {
     2859                        return wp_delete_post( $post_id, true );
     2860                }
     2861
     2862                if ( 'trash' === get_post_status( $post ) ) {
     2863                        return false;
     2864                }
     2865
     2866                /** This filter is documented in wp-includes/post.php */
     2867                $check = apply_filters( 'pre_trash_post', null, $post );
     2868                if ( null !== $check ) {
     2869                        return $check;
     2870                }
     2871
     2872                /** This action is documented in wp-includes/post.php */
     2873                do_action( 'wp_trash_post', $post_id );
     2874
     2875                add_post_meta( $post_id, '_wp_trash_meta_status', $post->post_status );
     2876                add_post_meta( $post_id, '_wp_trash_meta_time', time() );
     2877
     2878                $old_status = $post->post_status;
     2879                $new_status = 'trash';
     2880                $wpdb->update( $wpdb->posts, array( 'post_status' => $new_status ), array( 'ID' => $post->ID ) );
     2881                clean_post_cache( $post->ID );
     2882
     2883                $post->post_status = $new_status;
     2884                wp_transition_post_status( $new_status, $old_status, $post );
     2885
     2886                /** This action is documented in wp-includes/post.php */
     2887                do_action( 'edit_post', $post->ID, $post );
     2888
     2889                /** This action is documented in wp-includes/post.php */
     2890                do_action( "save_post_{$post->post_type}", $post->ID, $post, true );
     2891
     2892                /** This action is documented in wp-includes/post.php */
     2893                do_action( 'save_post', $post->ID, $post, true );
     2894
     2895                /** This action is documented in wp-includes/post.php */
     2896                do_action( 'wp_insert_post', $post->ID, $post, true );
     2897
     2898                wp_trash_post_comments( $post_id );
     2899
     2900                /** This action is documented in wp-includes/post.php */
     2901                do_action( 'trashed_post', $post_id );
     2902
     2903                return $post;
     2904        }
     2905
     2906        /**
    28342907         * Handle request to trash a changeset.
    28352908         *
     
    28772950                }
    28782951
    2879                 $r = wp_trash_post( $changeset_post_id );
     2952                $r = $this->trash_changeset_post( $changeset_post_id );
    28802953                if ( ! ( $r instanceof WP_Post ) ) {
    28812954                        wp_send_json_error( array(
Note: See TracChangeset for help on using the changeset viewer.