[wp-trac] [WordPress Trac] #49695: REST API check_template function can return false error

WordPress Trac noreply at wordpress.org
Tue Mar 24 23:51:00 UTC 2020


#49695: REST API check_template function can return false error
--------------------------+-----------------------------
 Reporter:  Kipperlenny   |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  REST API      |    Version:  5.3.2
 Severity:  normal        |   Keywords:  has-patch
  Focuses:  rest-api      |
--------------------------+-----------------------------
 wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php Line
 1209

 function check_template


 ----

 This function does not check get_post for an empty post:

 {{{#!php
 <?php
 $allowed_templates = wp_get_theme()->get_page_templates( get_post(
 $request['id'] ), $this->post_type );
 }}}

 On my Setup, this returned the WP_Error rest_invalid_param because
 get_post() returned an empty Post (with $post->ID == 0).

 I changed this to:

 {{{#!php
 <?php
 $post = get_post( $request['id'] );
 if($post->ID == 0) {
         $post = null;
 }

 // If this is a create request, get_post() will return null and wp theme
 will fallback to the passed post type.
 $allowed_templates = wp_get_theme()->get_page_templates( $post,
 $this->post_type );
 }}}

 This checks, if post is empty. This will save some API Requests from
 running in errors and does no harm.

 PS: No idea, why get_post() can return an empty WP_Post object. My API did
 not send an ID and was a POST Request.

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


More information about the wp-trac mailing list