Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ public function create_item( $request ) {
*/
$prepared_post->post_name = wp_unique_post_slug(
$prepared_post->post_name,
$prepared_post->id,
! empty( $prepared_post->ID ) ? $prepared_post->ID : 0,
'publish',
$prepared_post->post_type,
$prepared_post->post_parent
Expand Down
46 changes: 46 additions & 0 deletions tests/phpunit/tests/rest-api/rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2526,6 +2526,52 @@ public function test_create_item() {
$this->check_create_post_response( $response );
}

/**
* Ensures creating a draft/pending post with an explicit slug does not emit
* "Undefined property: stdClass::$id" (PHP 8+) from wp_unique_post_slug().
*
* @ticket XXXXX

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't have a ticket number, you should open one.

*
* @covers WP_REST_Posts_Controller::create_item
*/
public function test_create_draft_with_slug_does_not_emit_undefined_property_warning() {
wp_set_current_user( self::$editor_id );

$caught = array();
set_error_handler(
static function ( $errno, $errstr ) use ( &$caught ) {
$caught[] = $errstr;
return true;
},
E_WARNING | E_NOTICE | E_DEPRECATED
);

try {
$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
$request->set_body_params(
array(
'title' => 'Draft with explicit slug',
'status' => 'draft',
'slug' => 'draft-with-explicit-slug',
)
);
$response = rest_get_server()->dispatch( $request );
} finally {
restore_error_handler();
}

$this->assertSame( 201, $response->get_status() );
$this->assertSame( 'draft-with-explicit-slug', $response->get_data()['slug'] );

foreach ( $caught as $message ) {
$this->assertStringNotContainsString(
'Undefined property: stdClass::$id',
$message,
'Creating a draft with a slug should not emit an undefined property warning.'
);
}
}

public function data_post_dates() {
$all_statuses = array(
'draft',
Expand Down
Loading