/
class-llms-rest-webhooks-controller.php
411 lines (356 loc) · 11 KB
/
class-llms-rest-webhooks-controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<?php
/**
* REST Controller for Webhooks.
*
* @package LifterLMS_REST/Classes
*
* @since 1.0.0-beta.3
* @version 1.0.0-beta.27
*/
defined( 'ABSPATH' ) || exit;
/**
* LLMS_REST_Webhooks_Controller class.
*
* @since 1.0.0-beta.3
*/
class LLMS_REST_Webhooks_Controller extends LLMS_REST_Controller {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'webhooks';
/**
* Schema properties available for ordering the collection.
*
* @var string[]
*/
protected $orderby_properties = array(
'id',
'name',
'created',
'updated',
);
/**
* Check if the authenticated user can perform the request action.
*
* @since 1.0.0-beta.3
*
* @return boolean
*/
protected function check_permissions() {
return current_user_can( 'manage_lifterlms_webhooks' ) ? true : llms_rest_authorization_required_error();
}
/**
* Check if a given request has access to create an item.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function create_item_permissions_check( $request ) {
return $this->check_permissions();
}
/**
* Insert the prepared data into the database.
*
* @since 1.0.0-beta.3
*
* @param array $prepared Prepared item data.
* @param WP_REST_Request $request Request object.
* @return obj Object Instance of object from $this->get_object().
*/
protected function create_object( $prepared, $request ) {
return LLMS_REST_API()->webhooks()->create( $prepared );
}
/**
* Check if a given request has access to delete an item.
*
* @since 1.0.0-beta.3
*
* @param WP_REST_Request $request Full details about the request.
* @return bool|WP_Error
*/
public function delete_item_permissions_check( $request ) {
return $this->check_permissions();
}
/**
* Delete the object.
*
* Note: we do not return 404s when the resource to delete cannot be found. We assume it's already been deleted and respond with 204.
* Errors returned by this method should be any error other than a 404!
*
* @since 1.0.0-beta.3
*
* @param obj $object Instance of the object from $this->get_object().
* @param WP_REST_Request $request Request object.
* @return true|WP_Error true when the object is removed, WP_Error on failure.
*/
protected function delete_object( $object, $request ) {
return $object->delete();
}
/**
* Retrieves the query params for the objects collection.
*
* @since 1.0.0-beta.3
*
* @return array Collection parameters.
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['status'] = array(
'description' => __( 'Include only webhooks matching a specific status.', 'lifterlms' ),
'type' => 'string',
'enum' => array_keys( LLMS_REST_API()->webhooks()->get_statuses() ),
);
return $params;
}
/**
* Get the Webhook's schema, conforming to JSON Schema.
*
* @since 1.0.0-beta.27
*
* @return array
*/
protected function get_item_schema_base() {
return array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'webhook',
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'Webhook ID.', 'lifterlms' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'name' => array(
'description' => __( 'Friendly, human-readable name or description.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'sanitize_text_field',
),
),
'status' => array(
'description' => __( 'The status of the webhook.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'enum' => array_keys( LLMS_REST_API()->webhooks()->get_statuses() ),
'default' => 'disabled',
),
'topic' => array(
'description' => __( 'The webhook topic', 'lifterlms' ),
'type' => 'string',
'required' => true,
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'validate_callback' => array( LLMS_REST_API()->webhooks(), 'is_topic_valid' ),
),
),
'delivery_url' => array(
'description' => __( 'The webhook payload delivery URL.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'required' => true,
),
'secret' => array(
'description' => __( 'An optional secret key used to generate the delivery signature.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'created' => array(
'description' => __( 'Creation date. Format: Y-m-d H:i:s', 'lifterlms' ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'updated' => array(
'description' => __( 'Date last modified. Format: Y-m-d H:i:s', 'lifterlms' ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'resource' => array(
'description' => __( 'The parsed resource from the `topic`.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'event' => array(
'description' => __( 'The parsed event from the `topic`.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'hooks' => array(
'description' => __( 'List of WordPress action hook associated with the webhook.', 'lifterlms' ),
'type' => 'array',
'items' => array(
'type' => 'string',
),
'context' => array( 'view', 'edit' ),
),
),
);
}
/**
* Check if a given request has access to read an item.
*
* @since 1.0.0-beta.3
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_item_permissions_check( $request ) {
return $this->check_permissions();
}
/**
* Check if a given request has access to read items.
*
* @since 1.0.0-beta.3
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
return $this->check_permissions();
}
/**
* Retrieve pagination information from an objects query.
*
* @since 1.0.0-beta.3
* @since 1.0.0-beta-24 Fixed access of protected LLMS_Abstract_Query properties.
*
* @param LLMS_REST_Webhooks_Query $query Objects query result returned by {@see LLMS_REST_Webhooks_Controller::get_objects_query()}.
* @param array $prepared Array of collection arguments.
* @param WP_REST_Request $request Request object.
* @return array {
* Array of pagination information.
*
* @type int $current_page Current page number.
* @type int $total_results Total number of results.
* @type int $total_pages Total number of results pages.
* }
*/
protected function get_pagination_data_from_query( $query, $prepared, $request ) {
return array(
'current_page' => $query->get( 'page' ),
'total_results' => $query->get_found_results(),
'total_pages' => $query->get_max_pages(),
);
}
/**
* Retrieve An Webhook object by ID.
*
* @since 1.0.0-beta.3
*
* @param int $id Webhook ID.
* @param bool $hydrate If true, pulls all key data from the database on instantiation.
* @return WP_Error|LLMS_REST_API_Key
*/
protected function get_object( $id, $hydrate = true ) {
if ( ! is_numeric( $id ) ) {
$id = $this->get_object_id( $id );
}
$key = LLMS_REST_API()->webhooks()->get( $id, $hydrate );
return $key ? $key : llms_rest_not_found_error();
}
/**
* Retrieve a query object based on arguments from a `get_items()` (collection) request.
*
* @since 1.0.0-beta.3
*
* @param array $prepared Array of collection arguments.
* @param WP_REST_Request $request Request object.
* @return LLMS_REST_Webhooks_Query
*/
protected function get_objects_query( $prepared, $request ) {
return new LLMS_REST_Webhooks_Query( $prepared );
}
/**
* Retrieve an array of objects from the result of $this->get_objects_query().
*
* @since 1.0.0-beta.3
*
* @param obj $query Objects query result.
* @return obj[]
*/
protected function get_objects_from_query( $query ) {
return $query->get_results();
}
/**
* Map request keys to database keys for insertion.
*
* Array keys are the request fields (as defined in the schema) and
* array values are the database fields.
*
* @since 1.0.0-beta.3
*
* @return array
*/
protected function map_schema_to_database() {
$map = parent::map_schema_to_database();
// Not inserted/read via database calls.
unset( $map['resource'], $map['event'], $map['hooks'] );
return $map;
}
/**
* Prepare an object for response.
*
* @since 1.0.0-beta.3
*
* @param LLMS_Abstract_User_Data $object User object.
* @param WP_REST_Request $request Request object.
* @return array
*/
protected function prepare_object_for_response( $object, $request ) {
$prepared = parent::prepare_object_for_response( $object, $request );
$prepared['id'] = absint( $prepared['id'] );
$prepared['resource'] = $object->get_resource();
$prepared['event'] = $object->get_event();
$prepared['hooks'] = $object->get_hooks();
$prepared['created'] = mysql_to_rfc3339( $prepared['created'] );
$prepared['updated'] = mysql_to_rfc3339( $prepared['updated'] );
return $prepared;
}
/**
* Update a Webhook.
*
* @since 1.0.0-beta.3
* @since 1.0.0-beta.27 Handle custom rest fields registered via `register_rest_field()`.
*
* @param WP_REST_Request $request Request object.
* @return WP_Error|WP_REST_Response
*/
public function update_item( $request ) {
$prepared = $this->prepare_item_for_database( $request );
// Exclude additional fields registered via `register_rest_field()`.
$prepared = array_diff_key( $prepared, $this->get_additional_fields() );
$webhook = LLMS_REST_API()->webhooks()->update( $prepared );
if ( is_wp_error( $request ) ) {
$request->add_data( array( 'status' => 400 ) );
return $request;
}
// Fields registered via `register_rest_field()`.
$fields_update = $this->update_additional_fields_for_object( $webhook, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $webhook, $request );
return $response;
}
/**
* Check if a given request has access to update an item.
*
* @since 1.0.0-beta.3
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function update_item_permissions_check( $request ) {
return $this->check_permissions();
}
}