Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
60.00% |
18 / 30 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| WP_REST_Help_Center_User_Fields | |
60.00% |
18 / 30 |
|
66.67% |
2 / 3 |
5.02 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| register_rest_route | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 | |||
| update_user_fields | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * WP_REST_Help_Center_User_Fields file. |
| 4 | * |
| 5 | * @package automattic/jetpack-help-center |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Help_Center; |
| 9 | |
| 10 | /** |
| 11 | * Class WP_REST_Help_Center_User_Fields. |
| 12 | */ |
| 13 | class WP_REST_Help_Center_User_Fields extends WP_REST_Help_Center_Controller { |
| 14 | /** |
| 15 | * WP_REST_Help_Center_User_Fields 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 = '/zendesk/user-fields'; |
| 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, |
| 32 | array( |
| 33 | 'methods' => \WP_REST_Server::CREATABLE, |
| 34 | 'callback' => array( $this, 'update_user_fields' ), |
| 35 | 'permission_callback' => '__return_true', |
| 36 | 'args' => array( |
| 37 | 'fields' => array( |
| 38 | 'type' => 'object', |
| 39 | 'required' => true, |
| 40 | ), |
| 41 | ), |
| 42 | ) |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Callback to update user fields in Zendesk |
| 48 | * |
| 49 | * @param \WP_REST_Request $request The request sent to the API. |
| 50 | */ |
| 51 | public function update_user_fields( \WP_REST_Request $request ) { |
| 52 | $body = $this->wpcom_request_client->request( |
| 53 | 'help/zendesk/update-user-fields', |
| 54 | '2', |
| 55 | array( |
| 56 | 'method' => 'POST', |
| 57 | ), |
| 58 | array( 'fields' => $request['fields'] ) |
| 59 | ); |
| 60 | |
| 61 | if ( is_wp_error( $body ) ) { |
| 62 | return $body; |
| 63 | } |
| 64 | $response = json_decode( wp_remote_retrieve_body( $body ) ); |
| 65 | return rest_ensure_response( $response ); |
| 66 | } |
| 67 | } |