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.


git-svn-id: https://develop.svn.wordpress.org/trunk@53813 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jb Audras
2022-08-03 09:01:05 +00:00
parent 86fd171713
commit 5dff1e8503
2 changed files with 79 additions and 0 deletions

View File

@@ -5236,6 +5236,47 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$GLOBALS['wp_rest_server']->override_by_default = false;
}
/**
* @ticket 52422
*
* @covers WP_REST_Request::create_item
*/
public function test_draft_post_do_not_have_the_same_slug_as_existing_post() {
wp_set_current_user( self::$editor_id );
$this->factory()->post->create( array( 'post_name' => 'sample-slug' ) );
$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
$params = $this->set_post_data(
array(
'status' => 'draft',
'slug' => 'sample-slug',
)
);
$request->set_body_params( $params );
$response = rest_get_server()->dispatch( $request );
$new_data = $response->get_data();
$this->assertSame(
'sample-slug-2',
$new_data['slug'],
'The slug from the REST response did not match'
);
$post = get_post( $new_data['id'] );
$this->assertSame(
'draft',
$post->post_status,
'The post status is not draft'
);
$this->assertSame(
'sample-slug-2',
$post->post_name,
'The post slug was not set to "sample-slug-2"'
);
}
public function tear_down() {
if ( isset( $this->attachment_id ) ) {
$this->remove_added_uploads();