Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 52 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| WP_REST_Help_Center_Ticket | |
0.00% |
0 / 52 |
|
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 / 30 |
|
0.00% |
0 / 1 |
2 | |||
| 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-mu-wpcom |
| 6 | */ |
| 7 | |
| 8 | namespace A8C\FSE; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Client; |
| 11 | |
| 12 | /** |
| 13 | * Class WP_REST_Help_Center_Ticket. |
| 14 | */ |
| 15 | class WP_REST_Help_Center_Ticket extends \WP_REST_Controller { |
| 16 | /** |
| 17 | * WP_REST_Help_Center_Ticket constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | $this->namespace = 'help-center'; |
| 21 | $this->rest_base = '/ticket'; |
| 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 . '/new', |
| 31 | array( |
| 32 | 'methods' => \WP_REST_Server::CREATABLE, |
| 33 | 'callback' => array( $this, 'new_ticket' ), |
| 34 | 'permission_callback' => 'is_user_logged_in', |
| 35 | 'args' => array( |
| 36 | 'subject' => array( |
| 37 | 'type' => 'string', |
| 38 | ), |
| 39 | 'message' => array( |
| 40 | 'type' => 'string', |
| 41 | ), |
| 42 | 'locale' => array( |
| 43 | 'type' => 'string', |
| 44 | 'default' => 'en', |
| 45 | ), |
| 46 | 'is_chat_overflow' => array( |
| 47 | 'type' => 'boolean', |
| 48 | ), |
| 49 | 'source' => array( |
| 50 | 'type' => 'string', |
| 51 | ), |
| 52 | 'blog_url' => array( |
| 53 | 'type' => 'string', |
| 54 | ), |
| 55 | ), |
| 56 | ) |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Should create a new ticket |
| 62 | * |
| 63 | * @param \WP_REST_Request $request The request sent to the API. |
| 64 | */ |
| 65 | public function new_ticket( \WP_REST_Request $request ) { |
| 66 | $ticket = array( |
| 67 | 'subject' => $request['subject'], |
| 68 | 'message' => $request['message'], |
| 69 | 'locale' => $request['locale'], |
| 70 | 'is_chat_overflow' => $request['is_chat_overflow'], |
| 71 | 'source' => $request['source'], |
| 72 | 'blog_url' => $request['blog_url'], |
| 73 | ); |
| 74 | |
| 75 | $body = Client::wpcom_json_api_request_as_user( |
| 76 | '/help/ticket/new', |
| 77 | '2', |
| 78 | array( |
| 79 | 'method' => 'POST', |
| 80 | ), |
| 81 | $ticket |
| 82 | ); |
| 83 | |
| 84 | if ( is_wp_error( $body ) ) { |
| 85 | return $body; |
| 86 | } |
| 87 | |
| 88 | $response = json_decode( wp_remote_retrieve_body( $body ) ); |
| 89 | |
| 90 | return rest_ensure_response( $response ); |
| 91 | } |
| 92 | } |