Make WordPress Core

Changeset 41824


Ignore:
Timestamp:
10/11/2017 05:41:14 AM (7 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.

Location:
trunk
Files:
3 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(
  • trunk/src/wp-includes/theme.php

    r41732 r41824  
    29102910     */
    29112911    if ( ! wp_revisions_enabled( $changeset_post ) ) {
    2912         $post = $changeset_post;
    2913         $post_id = $changeset_post->ID;
    2914 
    2915         /*
    2916          * The following re-formulates the logic from wp_trash_post() as done in
    2917          * wp_publish_post(). The reason for bypassing wp_trash_post() is that it
    2918          * will mutate the post_content and the post_name when they should be
    2919          * untouched.
    2920          */
    2921         if ( ! EMPTY_TRASH_DAYS ) {
    2922             wp_delete_post( $post_id, true );
    2923         } else {
    2924             /** This action is documented in wp-includes/post.php */
    2925             do_action( 'wp_trash_post', $post_id );
    2926 
    2927             add_post_meta( $post_id, '_wp_trash_meta_status', $post->post_status );
    2928             add_post_meta( $post_id, '_wp_trash_meta_time', time() );
    2929 
    2930             $old_status = $post->post_status;
    2931             $new_status = 'trash';
    2932             $wpdb->update( $wpdb->posts, array( 'post_status' => $new_status ), array( 'ID' => $post->ID ) );
    2933             clean_post_cache( $post->ID );
    2934 
    2935             $post->post_status = $new_status;
    2936             wp_transition_post_status( $new_status, $old_status, $post );
    2937 
    2938             /** This action is documented in wp-includes/post.php */
    2939             do_action( 'edit_post', $post->ID, $post );
    2940 
    2941             /** This action is documented in wp-includes/post.php */
    2942             do_action( "save_post_{$post->post_type}", $post->ID, $post, true );
    2943 
    2944             /** This action is documented in wp-includes/post.php */
    2945             do_action( 'save_post', $post->ID, $post, true );
    2946 
    2947             /** This action is documented in wp-includes/post.php */
    2948             do_action( 'wp_insert_post', $post->ID, $post, true );
    2949 
    2950             /** This action is documented in wp-includes/post.php */
    2951             do_action( 'trashed_post', $post_id );
    2952         }
     2912        $wp_customize->trash_changeset_post( $changeset_post->ID );
    29532913    }
    29542914}
  • trunk/tests/phpunit/tests/customize/manager.php

    r41788 r41824  
    17111711
    17121712    /**
     1713     * Test that trash_changeset_post() trashes a changeset post with its name and content preserved.
     1714     *
     1715     * @covers WP_Customize_Manager::trash_changeset_post
     1716     */
     1717    public function test_trash_changeset_post_preserves_properties() {
     1718        $args = array(
     1719            'post_type' => 'customize_changeset',
     1720            'post_content' => wp_json_encode( array(
     1721                'blogname' => array(
     1722                    'value' => 'Test',
     1723                ),
     1724            ) ),
     1725            'post_name' => wp_generate_uuid4(),
     1726            'post_status' => 'draft',
     1727        );
     1728
     1729        $post_id = wp_insert_post( $args );
     1730
     1731        $manager = $this->create_test_manager( $args['post_name'] );
     1732        $manager->trash_changeset_post( $post_id );
     1733
     1734        $post = get_post( $post_id );
     1735
     1736        $this->assertSame( 'trash', get_post_status( $post_id ) );
     1737        $this->assertSame( $args['post_name'], $post->post_name );
     1738        $this->assertSame( $args['post_content'], $post->post_content );
     1739    }
     1740
     1741    /**
    17131742     * Register scratchpad setting.
    17141743     *
Note: See TracChangeset for help on using the changeset viewer.