/
class-llms-rest-webhook-data.php
323 lines (267 loc) · 7.65 KB
/
class-llms-rest-webhook-data.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
<?php
/**
* Webhook Getters & Setters
*
* @package LifterLMS_REST/Abstracts
*
* @since 1.0.0-beta.1
* @version 1.0.0-beta.17
*/
defined( 'ABSPATH' ) || exit;
/**
* LLMS_REST_Webhook class.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.6 Retrieve proper payload for enrollment and progress resources.
* @since 1.0.0-beta.17 Remove unused 'pending_delivery' column.
*/
abstract class LLMS_REST_Webhook_Data extends LLMS_Abstract_Database_Store {
/**
* Array of table column name => format
*
* @var string[]
*/
protected $columns = array(
'status' => '%s',
'name' => '%s',
'delivery_url' => '%s',
'secret' => '%s',
'topic' => '%s',
'user_id' => '%d',
'created' => '%s',
'updated' => '%s',
'failure_count' => '%d',
);
/**
* Database Table Name
*
* @var string
*/
protected $table = 'webhooks';
/**
* The record type
*
* Used for filters/actions.
*
* @var string
*/
protected $type = 'webhook';
/**
* Constructor
*
* @since 1.0.0-beta.1
*
* @param int $id API Key ID.
* @param bool $hydrate If true, hydrates the object on instantiation if an ID is supplied.
*/
public function __construct( $id = null, $hydrate = true ) {
$this->id = $id;
if ( $this->id && $hydrate ) {
$this->hydrate();
}
// Adds created and updated dates on instantiation.
parent::__construct();
}
/**
* Retrieve an admin nonce url for deleting an API key.
*
* @since 1.0.0-beta.1
*
* @return string
*/
public function get_delete_link() {
return add_query_arg(
array(
'section' => 'webhooks',
'delete-webhook' => $this->get( 'id' ),
'delete-webhook-nonce' => wp_create_nonce( 'delete' ),
),
LLMS_REST_API()->keys()->get_admin_url()
);
}
/**
* Generate a delivery signature from a delivery payload.
*
* @since 1.0.0-beta.1
*
* @param string $payload JSON-encoded payload.
* @return string
*/
public function get_delivery_signature( $payload ) {
/**
* Allow overriding of signature generation.
*
* @since 1.0.0-beta.1
*
* @param string $signature Custom signature. Return a string to replace the default signature.
* @param string $payload JSON-encoded body to be delivered.
* @param int $id Webhook id.
*/
$signature = apply_filters( 'llms_rest_webhook_signature_pre', null, $payload, $this->get( 'id' ) );
if ( $signature && is_string( $signature ) ) {
return $signature;
}
/**
* Customize the hash algorithm used to generate the webhook delivery signature.
*
* @since 1.0.0-beta.1
*
* @param string $algo Hash algorithm. Defaults to 'sha256'. List of supported algorithms available at https://www.php.net/manual/en/function.hash-hmac-algos.php.
* @param string $payload JSON-encoded body to be delivered.
* @param int $id Webhook ID.
*/
$hash_algo = apply_filters( 'llms_rest_webhook_hash_algorithm', 'sha256', $payload, $this->get( 'id' ) );
$ts = llms_current_time( 'timestamp' );
$message = $ts . '.' . $payload;
$hash = hash_hmac( $hash_algo, $message, $this->get( 'secret' ) );
return sprintf( 't=%1$d,v1=%2$s', $ts, $hash );
}
/**
* Retrieve the admin URL where the api key is managed.
*
* @since 1.0.0-beta.1
*
* @return string
*/
public function get_edit_link() {
return add_query_arg(
array(
'section' => 'webhooks',
'edit-webhook' => $this->get( 'id' ),
),
LLMS_REST_API()->keys()->get_admin_url()
);
}
/**
* Retrieve the topic event
*
* @since 1.0.0-beta.1
*
* @return string
*/
public function get_event() {
$topic = explode( '.', $this->get( 'topic' ) );
return apply_filters( 'llms_rest_webhook_get_event', isset( $topic[1] ) ? $topic[1] : '', $this->get( 'id' ) );
}
/**
* Retrieve an array of hooks for the webhook topic.
*
* @since 1.0.0-beta.1
*
* @return string[]
*/
public function get_hooks() {
if ( 'action' === $this->get_resource() ) {
$hooks = array( $this->get_event() => 1 );
} else {
$all_hooks = LLMS_REST_API()->webhooks()->get_hooks();
$topic = $this->get( 'topic' );
$hooks = isset( $all_hooks[ $topic ] ) ? $all_hooks[ $topic ] : array();
}
return apply_filters( 'llms_rest_webhook_get_hooks', $hooks, $this->get( 'id' ) );
}
/**
* Retrieve a payload for webhook delivery.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.6 Retrieve proper payload for enrollment and progress resources.
*
* @param array $args Numeric array of arguments from the originating hook.
* @return array
*/
protected function get_payload( $args ) {
// Switch current user to the user who created the webhook.
$current_user = get_current_user_id();
wp_set_current_user( $this->get( 'user_id' ) );
$resource = $this->get_resource();
$event = $this->get_event();
$payload = array();
if ( 'deleted' === $event ) {
if ( in_array( $this->get_resource(), array( 'enrollment', 'progress' ), true ) ) {
$payload['student_id'] = $args[0];
$payload['post_id'] = $args[1];
} else {
$payload['id'] = $args[0];
}
} elseif ( 'action' === $resource ) {
$payload['action'] = current( $this->get_hooks() );
$payload['args'] = $args;
} else {
if ( 'enrollment' === $resource ) {
$endpoint = sprintf( '/llms/v1/students/%1$d/enrollments/%2$d', $args[0], $args[1] );
} elseif ( 'progress' === $resource ) {
$endpoint = sprintf( '/llms/v1/students/%1$d/progress/%2$d', $args[0], $args[1] );
} else {
$endpoint = sprintf( '/llms/v1/%1$ss/%2$d', $resource, $args[0] );
}
$payload = llms_rest_get_api_endpoint_data( $endpoint );
}
// Restore the current user.
wp_set_current_user( $current_user );
/**
* Filter the webhook payload prior to delivery
*
* @since 1.0.0-beta.1
*
* @param array $payload Webhook payload.
* @param string $resource Webhook resource.
* @param string $event Webhook event.
* @param array $args Numeric array of arguments from the originating hook.
* @param LLMS_REST_Webhook $this Webhook object.
*/
return apply_filters( 'llms_rest_webhook_get_payload', $payload, $resource, $event, $args, $this );
}
/**
* Retrieve the topic resource.
*
* @since 1.0.0-beta.1
*
* @return string
*/
public function get_resource() {
$topic = explode( '.', $this->get( 'topic' ) );
return apply_filters( 'llms_rest_webhook_get_resource', $topic[0], $this->get( 'id' ) );
}
/**
* Retrieve a user agent string to use for delivering webhooks.
*
* @since 1.0.0-beta.1
*
* @return string
*/
protected function get_user_agent() {
global $wp_version;
return sprintf( 'LifterLMS/%1$s Hookshot (WordPress/%2$s)', LLMS()->version, $wp_version );
}
/**
* Increment delivery failures and after max allowed failures are reached, set status to disabled.
*
* @since 1.0.0-beta.1
*
* @return LLMS_REST_Webhook
*/
protected function set_delivery_failure() {
$failures = absint( $this->get( 'failure_count' ) );
$this->set( 'failure_count', ++$failures );
/**
* Filter the number of times a webhook is allowed to fail before it is automatically disabled.
*
* @since 1.0.0-beta.1
*
* @param int $num Number of allowed failures. Default: 5.
*/
$max_allowed = apply_filters( 'llms_rest_webhook_max_delivery_failures', 5 );
if ( $failures > $max_allowed ) {
$this->set( 'status', 'disabled' );
/**
* Fires immediately after a webhook has been disabled due to exceeding its maximum allowed failures.
*
* @since 1.0.0-beta.1
*
* @param int $webhook_id ID of the webhook.
*/
do_action( 'llms_rest_webhook_disabled_by_delivery_failures', $this->get( 'id' ) );
}
return $this;
}
}