Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
62.26% |
33 / 53 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| WP_REST_Help_Center_Ticket | |
62.26% |
33 / 53 |
|
66.67% |
2 / 3 |
4.86 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| register_rest_route | |
100.00% |
30 / 30 |
|
100.00% |
1 / 1 |
1 | |||
| new_ticket | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * WP_REST_Help_Center_Ticket file. |
| 4 | * |
| 5 | * @package automattic/jetpack-help-center |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Help_Center; |
| 9 | |
| 10 | /** |
| 11 | * Class WP_REST_Help_Center_Ticket. |
| 12 | */ |
| 13 | class WP_REST_Help_Center_Ticket extends WP_REST_Help_Center_Controller { |
| 14 | /** |
| 15 | * WP_REST_Help_Center_Ticket constructor. |
| 16 | * |
| 17 | * @param Wpcom_Request_Client|null $wpcom_request_client WP.com request client. |
| 18 | */ |
| 19 | public function __construct( ?Wpcom_Request_Client $wpcom_request_client = null ) { |
| 20 | parent::__construct( $wpcom_request_client ); |
| 21 | $this->namespace = 'help-center'; |
| 22 | $this->rest_base = '/ticket'; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Register available routes. |
| 27 | */ |
| 28 | public function register_rest_route() { |
| 29 | register_rest_route( |
| 30 | $this->namespace, |
| 31 | $this->rest_base . '/new', |
| 32 | array( |
| 33 | 'methods' => \WP_REST_Server::CREATABLE, |
| 34 | 'callback' => array( $this, 'new_ticket' ), |
| 35 | 'permission_callback' => '__return_true', |
| 36 | 'args' => array( |
| 37 | 'subject' => array( |
| 38 | 'type' => 'string', |
| 39 | ), |
| 40 | 'message' => array( |
| 41 | 'type' => 'string', |
| 42 | ), |
| 43 | 'locale' => array( |
| 44 | 'type' => 'string', |
| 45 | 'default' => 'en', |
| 46 | ), |
| 47 | 'is_chat_overflow' => array( |
| 48 | 'type' => 'boolean', |
| 49 | ), |
| 50 | 'source' => array( |
| 51 | 'type' => 'string', |
| 52 | ), |
| 53 | 'blog_url' => array( |
| 54 | 'type' => 'string', |
| 55 | ), |
| 56 | ), |
| 57 | ) |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Should create a new ticket |
| 63 | * |
| 64 | * @param \WP_REST_Request $request The request sent to the API. |
| 65 | */ |
| 66 | public function new_ticket( \WP_REST_Request $request ) { |
| 67 | $ticket = array( |
| 68 | 'subject' => $request['subject'], |
| 69 | 'message' => $request['message'], |
| 70 | 'locale' => $request['locale'], |
| 71 | 'is_chat_overflow' => $request['is_chat_overflow'], |
| 72 | 'source' => $request['source'], |
| 73 | 'blog_url' => $request['blog_url'], |
| 74 | ); |
| 75 | |
| 76 | $body = $this->wpcom_request_client->request( |
| 77 | '/help/ticket/new', |
| 78 | '2', |
| 79 | array( |
| 80 | 'method' => 'POST', |
| 81 | ), |
| 82 | $ticket |
| 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 | } |