Skip to content

Commit 413b7ea

Browse files
Posts, Post Types: Check the result of creating a draft in get_default_post_to_edit().
As `wp_insert_post()` can return an error for various reasons, this commit ensures that this scenario is properly handled and an error message is displayed. Follow-up to [12987]. Props rishabhwp, tfrommen, SirLouen, siliconforks, SergeyBiryukov. Fixes #37441. git-svn-id: https://develop.svn.wordpress.org/trunk@60701 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 57c1af7 commit 413b7ea

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/wp-admin/includes/post.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -768,13 +768,20 @@ function get_default_post_to_edit( $post_type = 'post', $create_in_db = false )
768768
'post_type' => $post_type,
769769
'post_status' => 'auto-draft',
770770
),
771-
false,
771+
true,
772772
false
773773
);
774-
$post = get_post( $post_id );
774+
775+
if ( is_wp_error( $post_id ) ) {
776+
wp_die( $post_id->get_error_message() );
777+
}
778+
779+
$post = get_post( $post_id );
780+
775781
if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) ) {
776782
set_post_format( $post, get_option( 'default_post_format' ) );
777783
}
784+
778785
wp_after_insert_post( $post, false, null );
779786

780787
// Schedule auto-draft cleanup.

tests/phpunit/tests/admin/includesPost.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,20 @@ public function test_bulk_edit_posts_should_fire_bulk_edit_posts_action() {
534534
$this->assertSame( 1, $action->get_call_count() );
535535
}
536536

537+
/**
538+
* Tests that get_default_post_to_edit() calls wp_die() if wp_insert_post() returns a WP_Error.
539+
*
540+
* @ticket 37441
541+
*
542+
* @covers ::get_default_post_to_edit
543+
*/
544+
public function test_get_default_post_to_edit_with_wp_insert_post_error() {
545+
add_filter( 'wp_insert_post_empty_content', '__return_true' );
546+
547+
$this->expectException( 'WPDieException' );
548+
get_default_post_to_edit( 'post', true );
549+
}
550+
537551
/**
538552
* @ticket 38293
539553
*/

0 commit comments

Comments
 (0)