Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.43% |
27 / 28 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| Knowledge_REST_Controller | |
100.00% |
26 / 26 |
|
100.00% |
5 / 5 |
11 | |
100.00% |
1 / 1 |
| get_items_permissions_check | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| prepare_items_query | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| check_read_permission | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| handle_status_param | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| prepare_item_for_database | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Knowledge REST API Controller. |
| 4 | * |
| 5 | * @package automattic/jetpack-knowledge |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Knowledge; |
| 9 | |
| 10 | use stdClass; |
| 11 | use WP_Error; |
| 12 | use WP_Post; |
| 13 | use WP_Post_Type; |
| 14 | use WP_REST_Posts_Controller; |
| 15 | use WP_REST_Request; |
| 16 | |
| 17 | if ( ! defined( 'ABSPATH' ) ) { |
| 18 | exit; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * REST API controller for knowledge posts. |
| 23 | */ |
| 24 | class Knowledge_REST_Controller extends WP_REST_Posts_Controller { |
| 25 | |
| 26 | /** |
| 27 | * Gate the knowledge collection on the post-type read capability. |
| 28 | * |
| 29 | * @param WP_REST_Request $request Full details about the request. |
| 30 | * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
| 31 | */ |
| 32 | public function get_items_permissions_check( $request ) { |
| 33 | $post_type = get_post_type_object( $this->post_type ); |
| 34 | if ( ! current_user_can( $post_type->cap->read ) ) { |
| 35 | return new WP_Error( |
| 36 | 'rest_cannot_read', |
| 37 | __( 'Sorry, you are not allowed to view knowledge.', 'jetpack-knowledge' ), |
| 38 | array( 'status' => rest_authorization_required_code() ) |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | return parent::get_items_permissions_check( $request ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Scope collection queries to rows readable by the current user. |
| 47 | * |
| 48 | * @param array $prepared_args Prepared WP_Query arguments. |
| 49 | * @param WP_REST_Request|null $request Full details about the request. |
| 50 | * @return array Updated WP_Query arguments. |
| 51 | */ |
| 52 | protected function prepare_items_query( $prepared_args = array(), $request = null ) { |
| 53 | $query_args = parent::prepare_items_query( $prepared_args, $request ); |
| 54 | $query_args['perm'] = 'readable'; |
| 55 | |
| 56 | return $query_args; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Gate per-item reads on the user-specific read capability. |
| 61 | * |
| 62 | * @param WP_Post $post Post object. |
| 63 | * @return bool Whether the post can be read. |
| 64 | */ |
| 65 | public function check_read_permission( $post ) { |
| 66 | if ( ! current_user_can( 'read_post', $post->ID ) ) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | return parent::check_read_permission( $post ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Restrict the status surface for callers without publish capability |
| 75 | * to `private`. Administrators retain the parent's full status surface. |
| 76 | * |
| 77 | * @param string $post_status Requested post status. |
| 78 | * @param WP_Post_Type $post_type Post type object. |
| 79 | * @return string|WP_Error Status, or WP_Error if not permitted. |
| 80 | */ |
| 81 | protected function handle_status_param( $post_status, $post_type ) { |
| 82 | if ( ! current_user_can( $post_type->cap->publish_posts ) ) { |
| 83 | if ( 'private' !== $post_status ) { |
| 84 | return new WP_Error( |
| 85 | 'rest_cannot_publish', |
| 86 | __( 'Sorry, you are only allowed to set knowledge to a private status.', 'jetpack-knowledge' ), |
| 87 | array( 'status' => rest_authorization_required_code() ) |
| 88 | ); |
| 89 | } |
| 90 | return $post_status; |
| 91 | } |
| 92 | |
| 93 | return parent::handle_status_param( $post_status, $post_type ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Default the status to `private` on create when none is supplied (the parent would fall back to |
| 98 | * `draft`). |
| 99 | * |
| 100 | * @param WP_REST_Request $request Request object. |
| 101 | * @return stdClass|WP_Error Prepared post object or error. |
| 102 | */ |
| 103 | protected function prepare_item_for_database( $request ) { |
| 104 | if ( ! isset( $request['id'] ) && null === $request['status'] ) { |
| 105 | $request->set_param( 'status', 'private' ); |
| 106 | } |
| 107 | return parent::prepare_item_for_database( $request ); |
| 108 | } |
| 109 | } |