Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
64.92% |
124 / 191 |
|
33.33% |
2 / 6 |
CRAP | |
0.00% |
0 / 1 |
| WP_REST_Help_Center_Support_Interactions | |
64.92% |
124 / 191 |
|
33.33% |
2 / 6 |
37.27 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| register_rest_route | |
100.00% |
121 / 121 |
|
100.00% |
1 / 1 |
1 | |||
| get_support_interactions | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| create_support_interaction | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
56 | |||
| create_support_interaction_event | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
42 | |||
| update_support_interaction_status | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * WP_REST_Help_Center_Support_Interactions file. |
| 4 | * |
| 5 | * @package automattic/jetpack-help-center |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Help_Center; |
| 9 | |
| 10 | /** |
| 11 | * Class WP_REST_Help_Center_Support_Interactions. |
| 12 | */ |
| 13 | class WP_REST_Help_Center_Support_Interactions extends WP_REST_Help_Center_Controller { |
| 14 | /** |
| 15 | * WP_REST_Help_Center_Support_Interactions 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 = '/support-interactions'; |
| 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::READABLE, |
| 34 | 'callback' => array( $this, 'get_support_interactions' ), |
| 35 | 'permission_callback' => '__return_true', |
| 36 | 'args' => array( |
| 37 | 'status' => array( |
| 38 | 'type' => 'array', |
| 39 | 'required' => false, |
| 40 | 'items' => array( |
| 41 | 'type' => 'string', |
| 42 | 'enum' => array( |
| 43 | 'open', |
| 44 | 'resolved', |
| 45 | 'solved', |
| 46 | 'closed', |
| 47 | ), |
| 48 | ), |
| 49 | 'default' => array(), |
| 50 | ), |
| 51 | 'page' => array( |
| 52 | 'type' => 'integer', |
| 53 | 'required' => false, |
| 54 | 'default' => 1, |
| 55 | 'minimum' => 1, |
| 56 | ), |
| 57 | 'per_page' => array( |
| 58 | 'type' => 'integer', |
| 59 | 'required' => false, |
| 60 | 'default' => 10, |
| 61 | 'minimum' => 1, |
| 62 | 'maximum' => 100, |
| 63 | ), |
| 64 | ), |
| 65 | ) |
| 66 | ); |
| 67 | |
| 68 | register_rest_route( |
| 69 | $this->namespace, |
| 70 | $this->rest_base . '/(?P<support_interaction_id>[a-zA-Z0-9-]+)', |
| 71 | array( |
| 72 | 'methods' => \WP_REST_Server::READABLE, |
| 73 | 'callback' => array( $this, 'get_support_interactions' ), |
| 74 | 'permission_callback' => '__return_true', |
| 75 | ) |
| 76 | ); |
| 77 | |
| 78 | register_rest_route( |
| 79 | $this->namespace, |
| 80 | $this->rest_base, |
| 81 | array( |
| 82 | 'methods' => \WP_REST_Server::CREATABLE, |
| 83 | 'callback' => array( $this, 'create_support_interaction' ), |
| 84 | 'permission_callback' => '__return_true', |
| 85 | 'args' => array( |
| 86 | 'event_external_id' => array( |
| 87 | 'type' => 'string', |
| 88 | 'required' => true, |
| 89 | ), |
| 90 | 'event_source' => array( |
| 91 | 'type' => 'string', |
| 92 | 'required' => true, |
| 93 | ), |
| 94 | 'event_metadata' => array( |
| 95 | 'type' => 'string', |
| 96 | 'required' => false, |
| 97 | ), |
| 98 | 'bot_slug' => array( |
| 99 | 'type' => 'string', |
| 100 | 'required' => false, |
| 101 | ), |
| 102 | 'is_test_mode' => array( |
| 103 | 'type' => 'boolean', |
| 104 | 'required' => false, |
| 105 | ), |
| 106 | ), |
| 107 | ) |
| 108 | ); |
| 109 | |
| 110 | register_rest_route( |
| 111 | $this->namespace, |
| 112 | $this->rest_base . '/(?P<support_interaction_id>[a-zA-Z0-9-]+)/events', |
| 113 | array( |
| 114 | 'methods' => \WP_REST_Server::CREATABLE, |
| 115 | 'callback' => array( $this, 'create_support_interaction_event' ), |
| 116 | 'permission_callback' => '__return_true', |
| 117 | 'args' => array( |
| 118 | 'event_external_id' => array( |
| 119 | 'type' => 'string', |
| 120 | 'required' => true, |
| 121 | ), |
| 122 | 'event_source' => array( |
| 123 | 'type' => 'string', |
| 124 | 'required' => true, |
| 125 | ), |
| 126 | 'event_metadata' => array( |
| 127 | 'type' => 'string', |
| 128 | 'required' => false, |
| 129 | ), |
| 130 | ), |
| 131 | ) |
| 132 | ); |
| 133 | |
| 134 | register_rest_route( |
| 135 | $this->namespace, |
| 136 | $this->rest_base . '/(?P<support_interaction_id>[a-zA-Z0-9-]+)/status', |
| 137 | array( |
| 138 | 'methods' => \WP_REST_Server::EDITABLE, |
| 139 | 'callback' => array( $this, 'update_support_interaction_status' ), |
| 140 | 'permission_callback' => '__return_true', |
| 141 | 'args' => array( |
| 142 | 'status' => array( |
| 143 | 'type' => 'string', |
| 144 | 'required' => true, |
| 145 | 'enum' => array( |
| 146 | 'open', |
| 147 | 'resolved', |
| 148 | 'closed', |
| 149 | ), |
| 150 | ), |
| 151 | ), |
| 152 | ) |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Get support interactions. |
| 158 | * |
| 159 | * @param \WP_REST_Request $request The request sent to the API. |
| 160 | */ |
| 161 | public function get_support_interactions( \WP_REST_Request $request ) { |
| 162 | if ( isset( $request['support_interaction_id'] ) ) { |
| 163 | $url = "/support-interactions/{$request['support_interaction_id']}"; |
| 164 | } else { |
| 165 | $url = '/support-interactions/?' . http_build_query( stripslashes_deep( $request->get_params() ) ); |
| 166 | } |
| 167 | |
| 168 | $body = $this->wpcom_request_client->request( $url ); |
| 169 | |
| 170 | if ( is_wp_error( $body ) ) { |
| 171 | return $body; |
| 172 | } |
| 173 | |
| 174 | $response = json_decode( wp_remote_retrieve_body( $body ) ); |
| 175 | |
| 176 | return rest_ensure_response( $response ); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Create support interaction. |
| 181 | * |
| 182 | * @param \WP_REST_Request $request The request sent to the API. |
| 183 | */ |
| 184 | public function create_support_interaction( \WP_REST_Request $request ) { |
| 185 | $data = array(); |
| 186 | |
| 187 | if ( isset( $request['event_external_id'] ) ) { |
| 188 | $data['event_external_id'] = $request['event_external_id']; |
| 189 | } |
| 190 | |
| 191 | if ( isset( $request['event_source'] ) ) { |
| 192 | $data['event_source'] = $request['event_source']; |
| 193 | } |
| 194 | |
| 195 | if ( isset( $request['event_metadata'] ) ) { |
| 196 | $data['event_metadata'] = $request['event_metadata']; |
| 197 | } |
| 198 | |
| 199 | if ( isset( $request['bot_slug'] ) ) { |
| 200 | $data['bot_slug'] = $request['bot_slug']; |
| 201 | } |
| 202 | |
| 203 | if ( isset( $request['is_test_mode'] ) ) { |
| 204 | $data['is_test_mode'] = $request['is_test_mode']; |
| 205 | } |
| 206 | |
| 207 | $body = $this->wpcom_request_client->request( |
| 208 | '/support-interactions', |
| 209 | '2', |
| 210 | array( |
| 211 | 'method' => 'POST', |
| 212 | ), |
| 213 | $data |
| 214 | ); |
| 215 | |
| 216 | if ( is_wp_error( $body ) ) { |
| 217 | return $body; |
| 218 | } |
| 219 | |
| 220 | $response = json_decode( wp_remote_retrieve_body( $body ) ); |
| 221 | |
| 222 | return rest_ensure_response( $response ); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Create support interaction event. |
| 227 | * |
| 228 | * @param \WP_REST_Request $request The request sent to the API. |
| 229 | */ |
| 230 | public function create_support_interaction_event( \WP_REST_Request $request ) { |
| 231 | $support_interaction_id = $request['support_interaction_id']; |
| 232 | |
| 233 | $data = array(); |
| 234 | |
| 235 | if ( isset( $request['event_external_id'] ) ) { |
| 236 | $data['event_external_id'] = $request['event_external_id']; |
| 237 | } |
| 238 | |
| 239 | if ( isset( $request['event_source'] ) ) { |
| 240 | $data['event_source'] = $request['event_source']; |
| 241 | } |
| 242 | |
| 243 | if ( isset( $request['event_metadata'] ) ) { |
| 244 | $data['event_metadata'] = $request['event_metadata']; |
| 245 | } |
| 246 | |
| 247 | if ( isset( $request['is_test_mode'] ) ) { |
| 248 | $data['is_test_mode'] = $request['is_test_mode']; |
| 249 | } |
| 250 | |
| 251 | $body = $this->wpcom_request_client->request( |
| 252 | "/support-interactions/$support_interaction_id/events", |
| 253 | '2', |
| 254 | array( |
| 255 | 'method' => 'POST', |
| 256 | ), |
| 257 | $data |
| 258 | ); |
| 259 | |
| 260 | if ( is_wp_error( $body ) ) { |
| 261 | return $body; |
| 262 | } |
| 263 | |
| 264 | $response = json_decode( wp_remote_retrieve_body( $body ) ); |
| 265 | |
| 266 | return rest_ensure_response( $response ); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Update support interaction status. |
| 271 | * |
| 272 | * @param \WP_REST_Request $request The request sent to the API. |
| 273 | */ |
| 274 | public function update_support_interaction_status( \WP_REST_Request $request ) { |
| 275 | $support_interaction_id = $request['support_interaction_id']; |
| 276 | |
| 277 | $status = $request['status']; |
| 278 | |
| 279 | $body = $this->wpcom_request_client->request( |
| 280 | "/support-interactions/$support_interaction_id/status", |
| 281 | '2', |
| 282 | array( |
| 283 | 'method' => 'PUT', |
| 284 | ), |
| 285 | array( 'status' => $status ) |
| 286 | ); |
| 287 | |
| 288 | if ( is_wp_error( $body ) ) { |
| 289 | return $body; |
| 290 | } |
| 291 | |
| 292 | $response = json_decode( wp_remote_retrieve_body( $body ) ); |
| 293 | |
| 294 | return rest_ensure_response( $response ); |
| 295 | } |
| 296 | } |