Make WordPress Core


Ignore:
Timestamp:
08/03/2022 09:01:05 AM (20 months ago)
Author:
audrasjb
Message:

Posts, Post Types: Force unique slugs for draft posts.

This fixes a behavior where a draft created with the same slug as an existing post would set the existing post to a 404.

wp_unique_post_slug() returns the same slug for 'draft' or 'pending' posts, so to ensure that a unique slug is generated, this changeset adds the post data with the 'publish' status to wp_unique_post_slug().

Props Toro_Unit, h2ham, peterwilsoncc, costdev, antonvlasenko, azaozz, ironprogrammer, audrasjb, hellofromTonya.
Fixes #52422.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r53759 r53813  
    52375237    }
    52385238
     5239    /**
     5240     * @ticket 52422
     5241     *
     5242     * @covers WP_REST_Request::create_item
     5243     */
     5244    public function test_draft_post_do_not_have_the_same_slug_as_existing_post() {
     5245        wp_set_current_user( self::$editor_id );
     5246        $this->factory()->post->create( array( 'post_name' => 'sample-slug' ) );
     5247
     5248        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     5249        $params  = $this->set_post_data(
     5250            array(
     5251                'status' => 'draft',
     5252                'slug'   => 'sample-slug',
     5253            )
     5254        );
     5255        $request->set_body_params( $params );
     5256        $response = rest_get_server()->dispatch( $request );
     5257
     5258        $new_data = $response->get_data();
     5259        $this->assertSame(
     5260            'sample-slug-2',
     5261            $new_data['slug'],
     5262            'The slug from the REST response did not match'
     5263        );
     5264
     5265        $post = get_post( $new_data['id'] );
     5266
     5267        $this->assertSame(
     5268            'draft',
     5269            $post->post_status,
     5270            'The post status is not draft'
     5271        );
     5272
     5273        $this->assertSame(
     5274            'sample-slug-2',
     5275            $post->post_name,
     5276            'The post slug was not set to "sample-slug-2"'
     5277        );
     5278    }
     5279
    52395280    public function tear_down() {
    52405281        if ( isset( $this->attachment_id ) ) {
Note: See TracChangeset for help on using the changeset viewer.