Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 53 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| WP_REST_Help_Center_Ticket_CSAT | |
0.00% |
0 / 53 |
|
0.00% |
0 / 3 |
20 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| register_rest_route | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
2 | |||
| submit_rating | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * WP_REST_Help_Center_Ticket_CSAT file. |
| 4 | * |
| 5 | * @package automattic/jetpack-mu-wpcom |
| 6 | */ |
| 7 | |
| 8 | namespace A8C\FSE; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Client; |
| 11 | |
| 12 | /** |
| 13 | * Class WP_REST_Help_Center_Ticket_CSAT. |
| 14 | */ |
| 15 | class WP_REST_Help_Center_Ticket_CSAT extends \WP_REST_Controller { |
| 16 | /** |
| 17 | * WP_REST_Help_Center_Ticket_CSAT constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | $this->namespace = 'help-center'; |
| 21 | $this->rest_base = '/csat'; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Register available routes. |
| 26 | */ |
| 27 | public function register_rest_route() { |
| 28 | register_rest_route( |
| 29 | $this->namespace, |
| 30 | $this->rest_base, |
| 31 | array( |
| 32 | 'methods' => \WP_REST_Server::CREATABLE, |
| 33 | 'callback' => array( $this, 'submit_rating' ), |
| 34 | 'permission_callback' => 'is_user_logged_in', |
| 35 | 'args' => array( |
| 36 | 'ticket_id' => array( |
| 37 | 'required' => true, |
| 38 | 'type' => 'int', |
| 39 | ), |
| 40 | 'score' => array( |
| 41 | 'required' => true, |
| 42 | 'enum' => array( 'good', 'bad' ), |
| 43 | 'type' => 'string', |
| 44 | ), |
| 45 | 'comment' => array( |
| 46 | 'required' => false, |
| 47 | 'type' => 'string', |
| 48 | ), |
| 49 | 'reason_id' => array( |
| 50 | 'required' => false, |
| 51 | 'type' => 'string', |
| 52 | ), |
| 53 | 'test_mode' => array( |
| 54 | 'required' => false, |
| 55 | 'type' => 'boolean', |
| 56 | ), |
| 57 | ), |
| 58 | ) |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Post customer satisfaction for a ticket. |
| 64 | * |
| 65 | * @param \WP_REST_Request $request The request sent to the API. |
| 66 | */ |
| 67 | public function submit_rating( \WP_REST_Request $request ) { |
| 68 | $payload = array( |
| 69 | 'ticket_id' => $request['ticket_id'], |
| 70 | 'score' => $request['score'], |
| 71 | 'comment' => $request['comment'], |
| 72 | 'reason_id' => $request['reason_id'], |
| 73 | 'test_mode' => $request['test_mode'], |
| 74 | ); |
| 75 | |
| 76 | $body = Client::wpcom_json_api_request_as_user( |
| 77 | '/help/csat', |
| 78 | '2', |
| 79 | array( |
| 80 | 'method' => 'POST', |
| 81 | ), |
| 82 | $payload |
| 83 | ); |
| 84 | |
| 85 | if ( is_wp_error( $body ) ) { |
| 86 | return $body; |
| 87 | } |
| 88 | |
| 89 | $response = json_decode( wp_remote_retrieve_body( $body ) ); |
| 90 | |
| 91 | return rest_ensure_response( $response ); |
| 92 | } |
| 93 | } |