Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| WP_REST_Help_Center_User_Fields | |
0.00% |
0 / 29 |
|
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 / 15 |
|
0.00% |
0 / 1 |
2 | |||
| 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-mu-wpcom |
| 6 | */ |
| 7 | |
| 8 | namespace A8C\FSE; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Client; |
| 11 | |
| 12 | /** |
| 13 | * Class WP_REST_Help_Center_User_Fields. |
| 14 | */ |
| 15 | class WP_REST_Help_Center_User_Fields extends \WP_REST_Controller { |
| 16 | /** |
| 17 | * WP_REST_Help_Center_User_Fields constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | $this->namespace = 'help-center'; |
| 21 | $this->rest_base = '/zendesk/user-fields'; |
| 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, 'update_user_fields' ), |
| 34 | 'permission_callback' => 'is_user_logged_in', |
| 35 | 'args' => array( |
| 36 | 'fields' => array( |
| 37 | 'type' => 'object', |
| 38 | 'required' => true, |
| 39 | ), |
| 40 | ), |
| 41 | ) |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Callback to update user fields in Zendesk |
| 47 | * |
| 48 | * @param \WP_REST_Request $request The request sent to the API. |
| 49 | */ |
| 50 | public function update_user_fields( \WP_REST_Request $request ) { |
| 51 | $body = Client::wpcom_json_api_request_as_user( |
| 52 | 'help/zendesk/update-user-fields', |
| 53 | '2', |
| 54 | array( |
| 55 | 'method' => 'POST', |
| 56 | ), |
| 57 | array( 'fields' => $request['fields'] ) |
| 58 | ); |
| 59 | |
| 60 | if ( is_wp_error( $body ) ) { |
| 61 | return $body; |
| 62 | } |
| 63 | $response = json_decode( wp_remote_retrieve_body( $body ) ); |
| 64 | return rest_ensure_response( $response ); |
| 65 | } |
| 66 | } |