[wp-trac] [WordPress Trac] #42238: Notice when Rest Comments controler is checking permision for a non existent post type

WordPress Trac noreply at wordpress.org
Mon Oct 16 11:58:55 UTC 2017


#42238: Notice when Rest Comments controler is checking permision for a non
existent post type
--------------------------+-----------------------------
 Reporter:  dragosh635    |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  REST API      |    Version:  4.8.2
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 The problem comes when I try to make a rest get request for the comments.
 The request is something like that http://demo.wp-api.org/wp-
 json/wp/v2/comments ( taken from the docs, in my local environment ), made
 in postman.

 The function check_read_post_permission is usign WP_REST_Posts_Controller
 to get a post controller based on the post type.

 {{{#!php
 <?php
 protected function check_read_post_permission( $post, $request ) {
                 $posts_controller = new WP_REST_Posts_Controller(
 $post->post_type );
                 $post_type        = get_post_type_object( $post->post_type
 );
 }}}

 Next, the WP_REST_Posts_Controller constructor tries to find the post type
 object in order to establish the rest_base variable. The problem is that
 the post type doesn't exists anymore ( for me this happened because I
 deactivate a plugin, woocommerce in my case, but I think this could happen
 in other cases also ). So, the get_post_type_object function returns null
 and than it tries to get a property from a null object -> Notice: Trying
 to get property of non-object in
 ...

 {{{#!php
 <?php
 public function __construct( $post_type ) {
                 $this->post_type = $post_type;
                 $this->namespace = 'wp/v2';
                 $obj = get_post_type_object( $post_type ); //$obj is null

                 $this->rest_base = ! empty( $obj->rest_base ) ?
 $obj->rest_base : $obj->rest_base; //notice is thrown
 }}}

 Obviously since I am on my local machine, the WP_DEBUG was set on true and
 if I set it on false, the notice doesn't appear anyomore. But still, I
 think it's a small issue, that could be solved in a more elegant way.

 To solve this I would probably suggest a check for null on the $obj and
 maybe in that case rest_base could become a default value or an error
 should be thrown ( I am not sure what would be the best approach )

 If there is someone that needs more information on this, let me know and I
 will do my best to help.

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


More information about the wp-trac mailing list