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

WordPress Trac noreply at wordpress.org
Wed Mar 25 01:59:32 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:  4.9
 Severity:  normal                       |  Resolution:
 Keywords:  has-patch reporter-feedback  |     Focuses:  rest-api
-----------------------------------------+------------------------------

Comment (by Kipperlenny):

 YES - and I know the problem is inside a different plugin (it is using
 $post = "something different, not a wp post" inside the main file).

 But - check_template() from WP Rest API should be more reliable about this
 - it cannot be sure, that $post is used somewhere else in a different way.

 Thats why I suggest:

 {{{#!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 );
 }}}

 or even better:


 {{{#!php
 <?php
 $requested_post = get_post( $request['id'] );
 if($post->ID == 0) {
         $requested_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( $requested_post,
 $this->post_type );
 }}}

 if you don't want to use $post here.

 With this easy change, check_template() could work more reliable.

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


More information about the wp-trac mailing list