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