diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
index 4fce45878c..3dc8d62d1b 100644
|
a
|
b
|
class WP_REST_Comments_Controller extends WP_REST_Controller { |
| 1592 | 1592 | * @return bool Whether post can be read. |
| 1593 | 1593 | */ |
| 1594 | 1594 | protected function check_read_post_permission( $post, $request ) { |
| 1595 | | $post_type = get_post_type_object( $post->post_type ); |
| | 1595 | $post_type = get_post_type_object( $post->post_type ); |
| | 1596 | |
| | 1597 | // Return false if custom post type doesn't exist |
| | 1598 | if ( ! $post_type ) { |
| | 1599 | return false; |
| | 1600 | } |
| | 1601 | |
| 1596 | 1602 | $posts_controller = $post_type->get_rest_controller(); |
| 1597 | 1603 | |
| 1598 | 1604 | // Ensure the posts controller is specifically a WP_REST_Posts_Controller instance |
diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php
index da2d372a54..46738edc72 100644
|
a
|
b
|
class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase |
| 3240 | 3240 | $this->assertArrayNotHasKey( 'raw', $data['content'] ); |
| 3241 | 3241 | } |
| 3242 | 3242 | } |
| | 3243 | |
| | 3244 | /** |
| | 3245 | * @ticket 42238 |
| | 3246 | */ |
| | 3247 | public function test_check_read_post_permission_with_invalid_post_type() { |
| | 3248 | register_post_type( |
| | 3249 | 'bug-post', |
| | 3250 | array( |
| | 3251 | 'label' => 'Bug Posts', |
| | 3252 | 'supports' => array( 'title', 'editor', 'author', 'comments' ), |
| | 3253 | 'show_in_rest' => true, |
| | 3254 | 'public' => true, |
| | 3255 | ) |
| | 3256 | ); |
| | 3257 | create_initial_rest_routes(); |
| | 3258 | |
| | 3259 | $post_id = self::factory()->post->create( array( 'post_type' => 'bug-post' ) ); |
| | 3260 | $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) ); |
| | 3261 | _unregister_post_type( 'bug-post' ); |
| | 3262 | |
| | 3263 | $this->setExpectedIncorrectUsage( 'map_meta_cap' ); |
| | 3264 | |
| | 3265 | wp_set_current_user( self::$admin_id ); |
| | 3266 | $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id ); |
| | 3267 | $response = rest_get_server()->dispatch( $request ); |
| | 3268 | $this->assertEquals( 403, $response->get_status() ); |
| | 3269 | } |
| 3243 | 3270 | } |