[wp-trac] [WordPress Trac] #37441: get_default_post_to_edit() function is broken

WordPress Trac noreply at wordpress.org
Fri Jul 22 08:32:10 UTC 2016


#37441: get_default_post_to_edit() function is broken
-------------------------------+-----------------------------
 Reporter:  tfrommen           |      Owner:
     Type:  defect (bug)       |     Status:  new
 Priority:  normal             |  Milestone:  Awaiting Review
Component:  Posts, Post Types  |    Version:
 Severity:  normal             |   Keywords:
  Focuses:  administration     |
-------------------------------+-----------------------------
 The [https://core.trac.wordpress.org/browser/trunk/src/wp-
 admin/includes/post.php?rev=38118#L591 get_default_post_to_edit()]
 function contains the following code (formatted for the sake of better
 readability) in case a new post should be inserted to the database:

 {{{#!php
 <?php
 $post_id = wp_insert_post( array(
     'post_title' => __( 'Auto Draft' ),
     'post_type' => $post_type,
     'post_status' => 'auto-draft',
 ) );
 $post = get_post( $post_id );
 }}}

 Later in the function, we just ''work'' with the `$post` object.

 The problem is, there are
 [https://core.trac.wordpress.org/browser/trunk/src/wp-
 includes/post.php?rev=38125#L2919
 se]-[https://core.trac.wordpress.org/browser/trunk/src/wp-
 includes/post.php?rev=38125#L2966
 ve]-[https://core.trac.wordpress.org/browser/trunk/src/wp-
 includes/post.php?rev=38125#L3038 ral]
 [https://core.trac.wordpress.org/browser/trunk/src/wp-
 includes/post.php?rev=38125#L3205
 rea]-[https://core.trac.wordpress.org/browser/trunk/src/wp-
 includes/post.php?rev=38125#L3220 sons] under which
 [https://core.trac.wordpress.org/browser/trunk/src/wp-
 includes/post.php?rev=38125#L2873 wp_insert_post()] returns `0`. In such a
 case, [https://core.trac.wordpress.org/browser/trunk/src/wp-
 includes/post.php?rev=38125#L451 get_post()], in turn, will return `null`,
 which is neither a `WP_Post` object nor an `object` at all.

 Then, [https://core.trac.wordpress.org/browser/trunk/src/wp-
 admin/includes/post.php?rev=38118#L639 setting a property on this null
 post] will automatically create a new `stdClass`, resulting in the `$post`
 object only having the three explicitly set properties `post_content`,
 `post_title` and `post_excerpt`.

 If you want to see how this affects WordPress, just put `$post = null;`
 before [https://core.trac.wordpress.org/browser/trunk/src/wp-
 admin/includes/post.php?rev=38118#L631 wp-admin/includes/post.php:L631].

--
Ticket URL: <https://core.trac.wordpress.org/ticket/37441>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list