Make WordPress Core


Ignore:
Timestamp:
10/11/2020 01:37:04 PM (5 years ago)
Author:
johnbillion
Message:

Posts, Post Types: Switch to restoring posts to draft status by default when they are untrashed.

This allows for edits to be made to a restored post before it goes live again. This also prevents scheduled posts being published unexpectedly if they are untrashed after their originally scheduled date.

The old behaviour of restoring untrashed posts to their original status can be reinstated using the wp_untrash_post_set_previous_status() helper function.

Also fixes an issue where the incorrect post ID gets passed to hooks if no post ID is passed to the function.

Props harrym, bananastalktome, jaredcobb, chriscct7, melchoyce, johnbillion, pankajmohale

Fixes #23022

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/wpInsertPost.php

    r48937 r49125  
    160160
    161161        wp_untrash_post( $about_page_id );
     162        wp_update_post(
     163            array(
     164                'ID'          => $about_page_id,
     165                'post_status' => 'publish',
     166            )
     167        );
    162168
    163169        $this->assertSame( 'about', get_post( $another_about_page_id )->post_name );
     
    166172
    167173    /**
     174     * @ticket 23022
     175     * @dataProvider data_various_post_statuses
     176     */
     177    function test_untrashing_a_post_should_always_restore_it_to_draft_status( $post_status ) {
     178        $page_id = self::factory()->post->create(
     179            array(
     180                'post_type'   => 'page',
     181                'post_status' => $post_status,
     182            )
     183        );
     184
     185        wp_trash_post( $page_id );
     186        wp_untrash_post( $page_id );
     187
     188        $this->assertSame( 'draft', get_post( $page_id )->post_status );
     189    }
     190
     191    /**
     192     * @ticket 23022
     193     * @dataProvider data_various_post_statuses
     194     */
     195    function test_wp_untrash_post_status_filter_restores_post_to_correct_status( $post_status ) {
     196        add_filter( 'wp_untrash_post_status', 'wp_untrash_post_set_previous_status', 10, 3 );
     197
     198        $page_id = self::factory()->post->create(
     199            array(
     200                'post_type'   => 'page',
     201                'post_status' => $post_status,
     202            )
     203        );
     204
     205        wp_trash_post( $page_id );
     206        wp_untrash_post( $page_id );
     207
     208        remove_filter( 'wp_untrash_post_status', 'wp_untrash_post_set_previous_status', 10, 3 );
     209
     210        $this->assertSame( $post_status, get_post( $page_id )->post_status );
     211    }
     212
     213    /**
    168214     * Data for testing the ability for users to set the post slug.
    169215     *
     
    180226            array(
    181227                'post',
     228            ),
     229        );
     230    }
     231
     232    /**
     233     * Data for testing post statuses.
     234     *
     235     * @return array Array of test arguments.
     236     */
     237    function data_various_post_statuses() {
     238        return array(
     239            array(
     240                'draft',
     241            ),
     242            array(
     243                'pending',
     244            ),
     245            array(
     246                'private',
     247            ),
     248            array(
     249                'publish',
    182250            ),
    183251        );
Note: See TracChangeset for help on using the changeset viewer.