class-ld-rest-users-groups-controller.php
14.1 KB
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<?php
/**
* LearnDash REST API V1 Users Groups Controller.
*
* @since 2.5.8
* @package LearnDash\REST\V1
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( ! class_exists( 'LD_REST_Users_Groups_Controller_V1' ) ) && ( class_exists( 'LD_REST_Posts_Controller_V1' ) ) ) {
/**
* Class LearnDash REST API V1 Users Groups Controller.
*
* @since 2.5.8
*/
class LD_REST_Users_Groups_Controller_V1 extends LD_REST_Posts_Controller_V1 /* phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound */ {
/**
* Supported Collection Parameters.
*
* @since 2.5.8
*
* @var array $supported_collection_params.
*/
private $supported_collection_params = array(
'exclude' => 'post__not_in',
'include' => 'post__in',
'offset' => 'offset',
'order' => 'order',
'orderby' => 'orderby',
'per_page' => 'posts_per_page',
'page' => 'paged',
'search' => 's',
'fields' => 'fields',
);
/**
* Public constructor for class
*
* @since 2.5.8
*/
public function __construct() {
$this->post_type = 'groups';
$this->taxonomies = array();
parent::__construct( $this->post_type );
$this->namespace = LEARNDASH_REST_API_NAMESPACE . '/' . $this->version;
$this->rest_base = LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_General_REST_API', 'users' );
}
/**
* Registers the routes for the objects of the controller.
*
* @since 2.5.8
*
* @see register_rest_route() in WordPress core.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)/groups',
array(
'args' => array(
'id' => array(
'description' => esc_html__( 'User ID', 'learndash' ),
'required' => true,
'type' => 'integer',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_user_groups' ),
'permission_callback' => array( $this, 'get_user_groups_permissions_check' ),
'args' => $this->get_collection_params(),
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_user_groups' ),
'permission_callback' => array( $this, 'update_user_groups_permissions_check' ),
'args' => array(
'group_ids' => array(
// translators: group.
'description' => sprintf( esc_html_x( '%s IDs to add to User.', 'placeholder: g roup', 'learndash' ), learndash_get_custom_label( 'group' ) ),
'required' => true,
'type' => 'array',
'items' => array(
'type' => 'integer',
),
),
),
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_user_groups' ),
'permission_callback' => array( $this, 'delete_user_groups_permissions_check' ),
'args' => array(
'group_ids' => array(
// translators: group.
'description' => sprintf( esc_html_x( '%s IDs to remove from User.', 'placeholder: group', 'learndash' ), learndash_get_custom_label( 'group' ) ),
'required' => true,
'type' => 'array',
'items' => array(
'type' => 'integer',
),
),
),
),
'schema' => array( $this, 'get_schema' ),
)
);
}
/**
* Gets the user groups schema.
*
* @since 2.5.8
*
* @return array
*/
public function get_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'user-group',
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'Unique identifier for the object.', 'learndash' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
'group_ids' => array(
'description' => sprintf(
// translators: placeholder: group.
esc_html_x(
'The %s IDs.',
'placeholder: group',
'learndash'
),
learndash_get_custom_label_lower( 'group' )
),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'context' => array( 'view', 'edit' ),
),
),
);
return $schema;
}
/**
* Check Users Groups Read Permissions.
*
* @since 2.5.8
*
* @param object $request WP_REST_Request instance.
*/
public function get_user_groups_permissions_check( $request ) {
if ( learndash_is_admin_user() ) {
return true;
} elseif ( get_current_user_id() == $request['id'] ) {
return true;
}
}
/**
* Check Users Groups Update Permissions.
*
* @since 2.5.8
*
* @param object $request WP_REST_Request instance.
*/
public function update_user_groups_permissions_check( $request ) {
if ( learndash_is_admin_user() ) {
return true;
} elseif ( get_current_user_id() == $request['id'] ) {
return true;
}
}
/**
* Check Users Groups Delete Permissions.
*
* @since 2.5.8
*
* @param object $request WP_REST_Request instance.
*/
public function delete_user_groups_permissions_check( $request ) {
if ( learndash_is_admin_user() ) {
return true;
} elseif ( get_current_user_id() == $request['id'] ) {
return true;
}
}
/**
* Update Users Groups.
*
* @since 2.5.8
*
* @param object $request WP_REST_Request instance.
*/
public function update_user_groups( $request ) {
$user_id = $request['id'];
if ( empty( $user_id ) ) {
return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid User ID.', 'learndash' ) . ' ' . __CLASS__, array( 'status' => 404 ) );
}
$group_ids = $request['group_ids'];
if ( ( ! is_array( $group_ids ) ) || ( empty( $group_ids ) ) ) {
return new WP_Error(
'rest_post_invalid_id',
sprintf(
// translators: placeholder: Group.
esc_html_x(
'Invalid %s ID.',
'placeholder: Group',
'learndash'
),
LearnDash_Custom_Label::get_label( 'group' )
),
array( 'status' => 404 )
);
} else {
$group_ids = array_map( 'intval', $group_ids );
}
foreach ( $group_ids as $group_id ) {
ld_update_group_access( $user_id, $group_id, false );
}
$data = array();
// Create the response object.
$response = rest_ensure_response( $data );
// Add a custom status code.
$response->set_status( 200 );
return $response;
}
/**
* Delete Users Groups.
*
* @since 2.5.8
*
* @param object $request WP_REST_Request instance.
*/
public function delete_user_groups( $request ) {
$user_id = $request['id'];
if ( empty( $user_id ) ) {
return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid User ID.', 'learndash' ) . ' ' . __CLASS__, array( 'status' => 404 ) );
}
$group_ids = $request['group_ids'];
if ( ( ! is_array( $group_ids ) ) || ( empty( $group_ids ) ) ) {
return new WP_Error(
'rest_post_invalid_id',
sprintf(
// translators: placeholder: Group.
esc_html_x(
'Invalid %s ID.',
'placeholder: Group',
'learndash'
),
LearnDash_Custom_Label::get_label( 'group' )
),
array( 'status' => 404 )
);
} else {
$group_ids = array_map( 'intval', $group_ids );
}
foreach ( $group_ids as $group_id ) {
ld_update_group_access( $user_id, $group_id, true );
}
$data = array();
// Create the response object.
$response = rest_ensure_response( $data );
// Add a custom status code.
$response->set_status( 200 );
return $response;
}
/**
* Get Users Groups.
*
* @since 2.5.8
*
* @param object $request WP_REST_Request instance.
*/
public function get_user_groups( $request ) {
$user_id = $request['id'];
if ( empty( $user_id ) ) {
return new WP_Error( 'rest_user_invalid_id', esc_html__( 'Invalid user ID. #1', 'learndash' ), array( 'status' => 404 ) );
}
if ( is_user_logged_in() ) {
$current_user_id = get_current_user_id();
} else {
$current_user_id = 0;
}
// Ensure a search string is set in case the orderby is set to 'relevance'.
if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) {
return new WP_Error( 'rest_no_search_term_defined', __( 'You need to define a search term to order by relevance.', 'learndash' ), array( 'status' => 400 ) );
}
// Ensure an include parameter is set in case the orderby is set to 'include'.
if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) {
return new WP_Error( 'rest_orderby_include_missing_include', __( 'You need to define an include parameter to order by include.', 'learndash' ), array( 'status' => 400 ) );
}
// Retrieve the list of registered collection query parameters.
$registered = $this->get_collection_params();
$args = array();
/*
* For each known parameter which is both registered and present in the request,
* set the parameter's value on the query $args.
*/
foreach ( $this->supported_collection_params as $api_param => $wp_param ) {
if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {
$args[ $wp_param ] = $request[ $api_param ];
}
}
// Check for & assign any parameters which require special handling or setting.
$args['date_query'] = array();
// Set before into date query. Date query must be specified as an array of an array.
if ( isset( $registered['before'], $request['before'] ) ) {
$args['date_query'][0]['before'] = $request['before'];
}
// Set after into date query. Date query must be specified as an array of an array.
if ( isset( $registered['after'], $request['after'] ) ) {
$args['date_query'][0]['after'] = $request['after'];
}
// Ensure our per_page parameter overrides any provided posts_per_page filter.
if ( isset( $registered['per_page'] ) ) {
$args['posts_per_page'] = $request['per_page'];
}
// Force the post_type argument, since it's not a user input variable.
$args['post_type'] = $this->post_type;
$args['post__in'] = array( 0 );
$group_ids = learndash_get_users_group_ids( $user_id );
if ( ! empty( $group_ids ) ) {
$args['post__in'] = $group_ids;
}
if ( ! isset( $args['fields'] ) ) {
$args['fields'] = 'ids';
} elseif ( 'ids' != $args['fields'] ) {
unset( $args['fields'] );
}
/**
* Filters the query arguments for user groups REST request.
*
* Enables adding extra arguments or setting defaults for a post collection request.
*
* @since 2.5.8
*
* @link https://developer.wordpress.org/reference/classes/wp_query/
*
* @param array $args An array of query arguments for getting user groups.
* @param WP_REST_Request $request The REST request object.
*/
$args = apply_filters( 'learndash_rest_user_groups_query', $args, $request );
$query_args = $this->prepare_items_query( $args, $request );
$posts_query = new WP_Query();
$query_result = $posts_query->query( $query_args );
// Allow access to all password protected posts if the context is edit.
if ( 'edit' === $request['context'] ) {
add_filter( 'post_password_required', '__return_false' );
}
if ( ( ! isset( $args['fields'] ) ) || ( 'post' == $args['fields'] ) ) {
$posts = array();
foreach ( $query_result as $post ) {
if ( ! $this->check_read_permission( $post ) ) {
continue;
}
$data = $this->prepare_item_for_response( $post, $request );
$posts[] = $this->prepare_response_for_collection( $data );
}
$response = rest_ensure_response( $posts );
} else {
$response = rest_ensure_response( $query_result );
}
// Reset filter.
if ( 'edit' === $request['context'] ) {
remove_filter( 'post_password_required', '__return_false' );
}
$page = (int) $query_args['paged'];
$total_posts = $posts_query->found_posts;
if ( $total_posts < 1 ) {
// Out-of-bounds, run the query again without LIMIT for total count.
unset( $query_args['paged'] );
$count_query = new WP_Query();
$count_query->query( $query_args );
$total_posts = $count_query->found_posts;
}
$max_pages = ceil( $total_posts / (int) $posts_query->query_vars['posts_per_page'] );
if ( $page > $max_pages && $total_posts > 0 ) {
return new WP_Error( 'rest_post_invalid_page_number', __( 'The page number requested is larger than the number of pages available.', 'learndash' ), array( 'status' => 400 ) );
}
$response->header( 'X-WP-Total', (int) $total_posts );
$response->header( 'X-WP-TotalPages', (int) $max_pages );
$request_params = $request->get_query_params();
$base = add_query_arg( $request_params, rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );
if ( $page > 1 ) {
$prev_page = $page - 1;
if ( $prev_page > $max_pages ) {
$prev_page = $max_pages;
}
$prev_link = add_query_arg( 'page', $prev_page, $base );
$response->link_header( 'prev', $prev_link );
}
if ( $max_pages > $page ) {
$next_page = $page + 1;
$next_link = add_query_arg( 'page', $next_page, $base );
$response->link_header( 'next', $next_link );
}
return $response;
}
/**
* Get Collection parameters
*
* @since 2.5.8
*/
public function get_collection_params() {
$query_params_default = parent::get_collection_params();
$query_params_default['context']['default'] = 'view';
$query_params = array();
$query_params['context'] = $query_params_default['context'];
$query_params['fields'] = array(
'description' => __( 'Returned values.', 'learndash' ),
'type' => 'string',
'default' => 'ids',
'enum' => array(
'ids',
'objects',
),
);
foreach ( $this->supported_collection_params as $external_key => $internal_key ) {
if ( isset( $query_params_default[ $external_key ] ) ) {
$query_params[ $external_key ] = $query_params_default[ $external_key ];
}
}
return $query_params;
}
}
}