Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.73% |
43 / 44 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| WP_REST_Help_Center_Article_Rating | |
97.73% |
43 / 44 |
|
66.67% |
2 / 3 |
5 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| register_rest_route | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
1 | |||
| save_rating | |
94.12% |
16 / 17 |
|
0.00% |
0 / 1 |
3.00 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * WP_REST_Help_Center_Article_Rating file. |
| 4 | * |
| 5 | * @package automattic/jetpack-help-center |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Help_Center; |
| 9 | |
| 10 | /** |
| 11 | * Class WP_REST_Help_Center_Article_Rating. |
| 12 | * |
| 13 | * Proxies the "Was this helpful?" rating of a support article to WordPress.com, |
| 14 | * so wp-admin users get the same per-user rating memory as Calypso users. |
| 15 | */ |
| 16 | class WP_REST_Help_Center_Article_Rating extends WP_REST_Help_Center_Controller { |
| 17 | /** |
| 18 | * WP_REST_Help_Center_Article_Rating constructor. |
| 19 | * |
| 20 | * @param Wpcom_Request_Client|null $wpcom_request_client WP.com request client. |
| 21 | */ |
| 22 | public function __construct( ?Wpcom_Request_Client $wpcom_request_client = null ) { |
| 23 | parent::__construct( $wpcom_request_client ); |
| 24 | $this->namespace = 'help-center'; |
| 25 | $this->rest_base = '/article-rating'; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Register available routes. |
| 30 | */ |
| 31 | public function register_rest_route() { |
| 32 | register_rest_route( |
| 33 | $this->namespace, |
| 34 | $this->rest_base, |
| 35 | array( |
| 36 | 'methods' => \WP_REST_Server::CREATABLE, |
| 37 | 'callback' => array( $this, 'save_rating' ), |
| 38 | 'permission_callback' => '__return_true', |
| 39 | 'args' => array( |
| 40 | 'blog_id' => array( |
| 41 | 'required' => true, |
| 42 | 'type' => 'integer', |
| 43 | ), |
| 44 | 'post_id' => array( |
| 45 | 'required' => true, |
| 46 | 'type' => 'integer', |
| 47 | ), |
| 48 | 'rating' => array( |
| 49 | 'required' => true, |
| 50 | 'enum' => array( 1, 2 ), |
| 51 | 'type' => 'integer', |
| 52 | ), |
| 53 | ), |
| 54 | ) |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Forward the rating to WordPress.com and return the rating on record. |
| 60 | * |
| 61 | * @param \WP_REST_Request $request The request sent to the API. |
| 62 | */ |
| 63 | public function save_rating( \WP_REST_Request $request ) { |
| 64 | $body = $this->wpcom_request_client->request( |
| 65 | '/help/article/rating', |
| 66 | '2', |
| 67 | array( 'method' => 'POST' ), |
| 68 | array( |
| 69 | 'blog_id' => $request['blog_id'], |
| 70 | 'post_id' => $request['post_id'], |
| 71 | 'rating' => $request['rating'], |
| 72 | ) |
| 73 | ); |
| 74 | |
| 75 | if ( is_wp_error( $body ) ) { |
| 76 | return $body; |
| 77 | } |
| 78 | |
| 79 | $response = rest_ensure_response( json_decode( wp_remote_retrieve_body( $body ) ) ); |
| 80 | |
| 81 | // Pass the WordPress.com status through so clients see upstream failures as failures. |
| 82 | $status = (int) wp_remote_retrieve_response_code( $body ); |
| 83 | if ( $status > 0 ) { |
| 84 | $response->set_status( $status ); |
| 85 | } |
| 86 | |
| 87 | return $response; |
| 88 | } |
| 89 | } |