diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
index dfb97f7b60..496013ffb7 100644
a
|
b
|
class WP_REST_Posts_Controller extends WP_REST_Controller { |
644 | 644 | return $prepared_post; |
645 | 645 | } |
646 | 646 | |
647 | | $prepared_post->post_type = $this->post_type; |
| 647 | if ( |
| 648 | ! empty( $prepared_post->post_name ) |
| 649 | && ! empty( $prepared_post->post_status ) |
| 650 | && in_array( $prepared_post->post_status, array( 'draft', 'pending' ), true ) |
| 651 | ) { |
| 652 | /* |
| 653 | * `wp_unique_post_slug()` returns the same |
| 654 | * slug for 'draft' or 'pending' posts. |
| 655 | * |
| 656 | * To ensure that a unique slug is generated, |
| 657 | * pass the post data with the 'publish' status. |
| 658 | */ |
| 659 | $prepared_post->post_name = wp_unique_post_slug( |
| 660 | $prepared_post->post_name, |
| 661 | $prepared_post->id, |
| 662 | 'publish', |
| 663 | $prepared_post->post_type, |
| 664 | $prepared_post->post_parent |
| 665 | ); |
| 666 | } |
648 | 667 | |
649 | 668 | $post_id = wp_insert_post( wp_slash( (array) $prepared_post ), true, false ); |
650 | 669 | |
… |
… |
class WP_REST_Posts_Controller extends WP_REST_Controller { |
827 | 846 | return $post; |
828 | 847 | } |
829 | 848 | |
| 849 | if ( ! empty( $post->post_status ) ) { |
| 850 | $post_status = $post->post_status; |
| 851 | } else { |
| 852 | $post_status = $post_before->post_status; |
| 853 | } |
| 854 | |
| 855 | /* |
| 856 | * `wp_unique_post_slug()` returns the same |
| 857 | * slug for 'draft' or 'pending' posts. |
| 858 | * |
| 859 | * To ensure that a unique slug is generated, |
| 860 | * pass the post data with the 'publish' status. |
| 861 | */ |
| 862 | if ( ! empty( $post->post_name ) && in_array( $post_status, array( 'draft', 'pending' ), true ) ) { |
| 863 | $post_parent = ! empty( $post->post_parent ) ? $post->post_parent : 0; |
| 864 | $post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, 'publish', $post->post_type, $post_parent ); |
| 865 | } |
| 866 | |
830 | 867 | // Convert the post object to an array, otherwise wp_update_post() will expect non-escaped input. |
831 | 868 | $post_id = wp_update_post( wp_slash( (array) $post ), true, false ); |
832 | 869 | |
diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php
index f02037962c..4fac0dc951 100644
a
|
b
|
class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te |
5156 | 5156 | $GLOBALS['wp_rest_server']->override_by_default = false; |
5157 | 5157 | } |
5158 | 5158 | |
| 5159 | /** |
| 5160 | * @ticket 52422 |
| 5161 | * |
| 5162 | * @covers WP_REST_Request::create_item |
| 5163 | */ |
| 5164 | public function test_draft_post_do_not_have_the_same_slug_as_existing_post() { |
| 5165 | wp_set_current_user( self::$editor_id ); |
| 5166 | $this->factory()->post->create( array( 'post_name' => 'sample-slug' ) ); |
| 5167 | |
| 5168 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| 5169 | $params = $this->set_post_data( |
| 5170 | array( |
| 5171 | 'status' => 'draft', |
| 5172 | 'slug' => 'sample-slug', |
| 5173 | ) |
| 5174 | ); |
| 5175 | $request->set_body_params( $params ); |
| 5176 | $response = rest_get_server()->dispatch( $request ); |
| 5177 | |
| 5178 | $new_data = $response->get_data(); |
| 5179 | $this->assertSame( |
| 5180 | 'sample-slug-2', |
| 5181 | $new_data['slug'], |
| 5182 | 'The slug from the REST response did not match' |
| 5183 | ); |
| 5184 | |
| 5185 | $post = get_post( $new_data['id'] ); |
| 5186 | |
| 5187 | $this->assertSame( |
| 5188 | 'draft', |
| 5189 | $post->post_status, |
| 5190 | 'The post status is not draft' |
| 5191 | ); |
| 5192 | |
| 5193 | $this->assertSame( |
| 5194 | 'sample-slug-2', |
| 5195 | $post->post_name, |
| 5196 | 'The post slug was not set to "sample-slug-2"' |
| 5197 | ); |
| 5198 | } |
| 5199 | |
5159 | 5200 | public function tear_down() { |
5160 | 5201 | if ( isset( $this->attachment_id ) ) { |
5161 | 5202 | $this->remove_added_uploads(); |