Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 44 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| WP_REST_Help_Center_Support_Status | |
0.00% |
0 / 44 |
|
0.00% |
0 / 4 |
42 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| register_rest_route | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
2 | |||
| get_support_status | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| get_messaging_support_availability | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * WP_REST_Help_Center_Support_Status 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_Support_Status. |
| 14 | */ |
| 15 | class WP_REST_Help_Center_Support_Status extends \WP_REST_Controller { |
| 16 | /** |
| 17 | * WP_REST_Help_Center_Support_Status constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | $this->namespace = 'help-center'; |
| 21 | $this->rest_base = '/support-status'; |
| 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::READABLE, |
| 33 | 'callback' => array( $this, 'get_support_status' ), |
| 34 | 'permission_callback' => 'is_user_logged_in', |
| 35 | ) |
| 36 | ); |
| 37 | |
| 38 | register_rest_route( |
| 39 | $this->namespace, |
| 40 | $this->rest_base . '/messaging', |
| 41 | array( |
| 42 | 'methods' => \WP_REST_Server::READABLE, |
| 43 | 'callback' => array( $this, 'get_messaging_support_availability' ), |
| 44 | 'permission_callback' => 'is_user_logged_in', |
| 45 | 'args' => array( |
| 46 | 'group' => array( |
| 47 | 'type' => 'string', |
| 48 | 'required' => true, |
| 49 | ), |
| 50 | 'environment' => array( |
| 51 | 'type' => 'string', |
| 52 | 'required' => true, |
| 53 | ), |
| 54 | ), |
| 55 | ) |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Should return the support status for the user |
| 61 | */ |
| 62 | public function get_support_status() { |
| 63 | $body = Client::wpcom_json_api_request_as_user( 'help/support-status' ); |
| 64 | if ( is_wp_error( $body ) ) { |
| 65 | return $body; |
| 66 | } |
| 67 | $response = json_decode( wp_remote_retrieve_body( $body ) ); |
| 68 | |
| 69 | return rest_ensure_response( $response ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Should return messaging eligibility |
| 74 | * |
| 75 | * @param \WP_REST_Request $request The request sent to the API. |
| 76 | */ |
| 77 | public function get_messaging_support_availability( \WP_REST_Request $request ) { |
| 78 | $query_parameters = array( |
| 79 | 'group' => $request['group'], |
| 80 | 'environment' => $request['environment'], |
| 81 | ); |
| 82 | $body = Client::wpcom_json_api_request_as_user( 'help/support-status/messaging?' . http_build_query( $query_parameters ) ); |
| 83 | if ( is_wp_error( $body ) ) { |
| 84 | return $body; |
| 85 | } |
| 86 | $response = json_decode( wp_remote_retrieve_body( $body ) ); |
| 87 | |
| 88 | return rest_ensure_response( $response ); |
| 89 | } |
| 90 | } |