/
class-llms-rest-sections-controller.php
553 lines (465 loc) · 13.4 KB
/
class-llms-rest-sections-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
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
<?php
/**
* REST Sections Controller
*
* @package LifterLMS_REST/Classes/Controllers
*
* @since 1.0.0-beta.1
* @version 1.0.0-beta.27
*/
defined( 'ABSPATH' ) || exit;
/**
* LLMS_REST_Sections_Controller class.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.7 `prepare_objects_query()` renamed to `prepare_collection_query_args()`.
* Fix the way we get the section's parent course object when building the resource links.
* @since 1.0.0-beta.9 Removed `create_llms_post()` and `get_object()` methods, now abstracted in `LLMS_REST_Posts_Controller` class.
* @since 1.0.0-beta.12 Updated `$this->prepare_collection_query_args()` to reflect changes in the parent class.
* @since 1.0.0-beta.14 Update `prepare_links()` to accept a second parameter, `WP_REST_Request`.
*/
class LLMS_REST_Sections_Controller extends LLMS_REST_Posts_Controller {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'sections';
/**
* Post type.
*
* @var string
*/
protected $post_type = 'section';
/**
* Parent id.
*
* @var int
*/
protected $parent_id;
/**
* Schema properties available for ordering the collection.
*
* @var string[]
*/
protected $orderby_properties = array(
'id',
'title',
'date_created',
'date_updated',
'order',
'relevance',
);
/**
* Lessons controller class.
*
* @var string
*/
protected $content_controller_class;
/**
* Lessons controller.
*
* @var LLMS_REST_Lessons_Controller
*/
protected $content_controller;
/**
* Constructor.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.27 Call parent constructor.
*
* @param string $content_controller_class Optional. The class name of the content controller. Default 'LLMS_REST_Lessons_Controller'.
* @return void
*/
public function __construct( $content_controller_class = 'LLMS_REST_Lessons_Controller' ) {
parent::__construct();
$this->collection_params = $this->build_collection_params();
$this->content_controller_class = $content_controller_class;
if ( $this->content_controller_class && class_exists( $this->content_controller_class ) ) {
$this->content_controller = new $this->content_controller_class();
$this->content_controller->set_collection_params( $this->get_content_collection_params() );
}
}
/**
* Register routes.
*
* @since 1.0.0-beta.1
*
* @return void
*/
public function register_routes() {
parent::register_routes();
if ( isset( $this->content_controller ) ) {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)/content',
array(
'args' => array(
'id' => array(
// translators: %1$s the post type name.
'description' => sprintf( __( 'Unique %1$s Identifier. The WordPress Post ID', 'lifterlms' ), $this->post_type ),
'type' => 'integer',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_content_items' ),
'permission_callback' => array( $this->content_controller, 'get_items_permissions_check' ),
'args' => $this->content_controller->get_collection_params(),
),
'schema' => array( $this->content_controller, 'get_public_item_schema' ),
)
);
}
}
/**
* Retrieves an array of arguments for the delete endpoint.
*
* @since 1.0.0-beta.1
*
* @return array Delete endpoint arguments.
*/
public function get_delete_item_args() {
return array();
}
/**
* Whether the delete should be forced.
*
* @since 1.0.0-beta.1
*
* @param WP_REST_Request $request Full details about the request.
* @return bool True if the delete should be forced, false otherwise.
*/
protected function is_delete_forced( $request ) {
return true;
}
/**
* Whether the trash is supported.
*
* @since 1.0.0-beta.1
*
* @return bool True if the trash is supported, false otherwise.
*/
protected function is_trash_supported() {
return false;
}
/**
* Set parent id.
*
* @since 1.0.0-beta.1
*
* @param int $parent_id Course parent id.
* @return void
*/
public function set_parent_id( $parent_id ) {
$this->parent_id = $parent_id;
}
/**
* Get parent id.
*
* @since 1.0.0-beta.1
*
* @return int|null Course parent id. Null if not set.
*/
public function get_parent_id() {
return isset( $this->parent_id ) ? $this->parent_id : null;
}
/**
* Prepares a single post for create or update.
*
* @since 1.0.0-beta.1
*
* @param WP_REST_Request $request Request object.
* @return array|WP_Error Array of llms post args or WP_Error.
*/
protected function prepare_item_for_database( $request ) {
$prepared_item = parent::prepare_item_for_database( $request );
$schema = $this->get_item_schema();
// LLMS Section parent id.
if ( ! empty( $schema['properties']['parent_id'] ) && isset( $request['parent_id'] ) ) {
$parent_course = llms_get_post( $request['parent_id'] );
if ( ! $parent_course || ! is_a( $parent_course, 'LLMS_Course' ) ) {
return llms_rest_bad_request_error( __( 'Invalid parent_id param. It must be a valid Course ID.', 'lifterlms' ) );
}
$prepared_item['parent_course'] = $request['parent_id'];
}
// LLMS Section order.
if ( ! empty( $schema['properties']['order'] ) && isset( $request['order'] ) ) {
// order must be > 0. It's sanitized as absint so it cannot come as negative value.
if ( 0 === $request['order'] ) {
return llms_rest_bad_request_error( __( 'Invalid order param. It must be greater than 0.', 'lifterlms' ) );
}
$prepared_item['order'] = $request['order'];
}
return $prepared_item;
}
/**
* Get the Section's schema base, conforming to JSON Schema.
*
* @since 1.0.0-beta.27
*
* @return array
*/
public function get_item_schema_base() {
$schema = parent::get_item_schema_base();
// Section's title.
$schema['properties']['title']['description'] = __( 'Section Title', 'lifterlms' );
// Section's parent id.
$schema['properties']['parent_id'] = array(
'description' => __( 'WordPress post ID of the parent item. Must be a Course ID.', 'lifterlms' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'absint',
),
'required' => true,
);
// Section order.
$schema['properties']['order'] = array(
'description' => __( 'Order of the section within the course.', 'lifterlms' ),
'type' => 'integer',
'default' => 1,
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'absint',
),
'required' => true,
);
// remove unnecessary properties.
$unnecessary_properties = array(
'permalink',
'slug',
'content',
'menu_order',
'excerpt',
'featured_media',
'status',
'password',
'featured_media',
'comment_status',
'ping_status',
);
foreach ( $unnecessary_properties as $unnecessary_property ) {
unset( $schema['properties'][ $unnecessary_property ] );
}
return $schema;
}
/**
* Retrieves the query params for the objects collection.
*
* @since 1.0.0-beta.1
*
* @return array The Enrollments collection parameters.
*/
public function get_collection_params() {
return $this->collection_params;
}
/**
* Retrieves the query params for the objects collection.
*
* @since 1.0.0-beta.1
*
* @param array $collection_params The Enrollments collection parameters to be set.
* @return void
*/
public function set_collection_params( $collection_params ) {
$this->collection_params = $collection_params;
}
/**
* Retrieves the query params for the objects collection.
*
* @since 1.0.0-beta.1
*
* @return array Collection parameters.
*/
public function build_collection_params() {
$query_params = parent::get_collection_params();
$query_params['parent'] = array(
'description' => __( 'Filter sections by the parent post (course) ID.', 'lifterlms' ),
'type' => 'integer',
'validate_callback' => 'rest_validate_request_arg',
);
return $query_params;
}
/**
* Prepare a single object output for response.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.23 Replaced call to deprecated `LLMS_Section::get_parent_course()` with `LLMS_Section::get( 'parent_course' )`.
*
* @param LLMS_Section $section Section object.
* @param WP_REST_Request $request Full details about the request.
* @return array
*/
protected function prepare_object_for_response( $section, $request ) {
$data = parent::prepare_object_for_response( $section, $request );
// Parent course.
$data['parent_id'] = $section->get( 'parent_course' );
// Order.
$data['order'] = $section->get( 'order' );
return $data;
}
/**
* Format query arguments to retrieve a collection of objects.
*
* @since 1.0.0-beta.7
* @since 1.0.0-beta.12 Updated to reflect changes in the parent class.
* @since 1.0.0-beta.18 Correctly return errors.
*
* @param WP_REST_Request $request Full details about the request.
* @return array|WP_Error
*/
protected function prepare_collection_query_args( $request ) {
$query_args = parent::prepare_collection_query_args( $request );
if ( is_wp_error( $query_args ) ) {
return $query_args;
}
// Orderby 'order' requires a meta query.
if ( isset( $query_args['orderby'] ) && 'order' === $query_args['orderby'] ) {
$query_args = array_merge(
$query_args,
array(
'meta_key' => '_llms_order',
'orderby' => 'meta_value_num',
)
);
}
if ( isset( $this->parent_id ) ) {
$parent_id = $this->parent_id;
} elseif ( ! empty( $request['parent'] ) && $request['parent'] > 1 ) {
$parent_id = $request['parent'];
}
// Filter by parent.
if ( ! empty( $parent_id ) ) {
$query_args = array_merge(
$query_args,
array(
'meta_query' => array(
array(
'key' => '_llms_parent_course',
'value' => $parent_id,
'compare' => '=',
),
),
)
);
}
return $query_args;
}
/**
* Prepare links for the request.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.7 Fix the way we get the section's parent course object.
* @since 1.0.0-beta.14 Added `$request` parameter.
* @since 1.0.0-beta.23 Replaced call to deprecated `LLMS_Section::get_parent_course()` with `LLMS_Section::get( 'parent_course' )`.
*
* @param LLMS_Section $section LLMS Section.
* @param WP_REST_Request $request Request object.
* @return array Links for the given object.
*/
protected function prepare_links( $section, $request ) {
$links = parent::prepare_links( $section, $request );
$parent_course_id = $section->get( 'parent_course' );
// If the section has no course parent return earlier.
if ( ! $parent_course_id ) {
return $links;
}
$parent_course = llms_get_post( $parent_course_id );
if ( ! is_a( $parent_course, 'LLMS_Course' ) ) {
return $links;
}
$section_id = $section->get( 'id' );
$section_links = array();
// Parent (course).
$section_links['parent'] = array(
'type' => 'course',
'href' => rest_url( sprintf( '/%s/%s/%d', 'llms/v1', 'courses', $parent_course_id ) ),
);
// Siblings.
$section_links['siblings'] = array(
'href' => add_query_arg(
'parent',
$parent_course_id,
$links['collection']['href']
),
);
// Next.
$next_section = $section->get_next();
if ( $next_section ) {
$section_links['next'] = array(
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $next_section->get( 'id' ) ) ),
);
}
// Previous.
$previous_section = $section->get_previous();
if ( $previous_section ) {
$section_links['previous'] = array(
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $previous_section->get( 'id' ) ) ),
);
}
return array_merge( $links, $section_links );
}
/**
* Checks if a Section can be read
*
* @since 1.0.0-beta.1
*
* @param LLMS_Section $section The Section oject.
* @return bool Whether the post can be read.
*/
protected function check_read_permission( $section ) {
/**
* As of now, sections of password protected courses cannot be read
*/
if ( post_password_required( $section->get( 'parent_course' ) ) ) {
return false;
}
return parent::check_read_permission( $section );
}
/**
* Retrieves the content controller.
*
* @since 1.0.0-beta.1
*
* @return LLMS_REST_Lessons_Controller|null
*/
public function get_content_controller() {
return $this->content_controller;
}
/**
* Retrieves the query params for the lessons objects collection.
*
* @since 1.0.0-beta.1
*
* @return array Collection parameters.
*/
public function get_content_collection_params() {
$query_params = $this->content_controller->get_collection_params();
$query_params['orderby']['enum'] = array(
'order',
'id',
'title',
);
$query_params['orderby']['default'] = 'order';
unset( $query_params['parent'] );
return $query_params;
}
/**
* Get a collection of content items (lessons).
*
* @since 1.0.0-beta.1
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response
*/
public function get_content_items( $request ) {
$this->content_controller->set_parent_id( $request['id'] );
$result = $this->content_controller->get_items( $request );
// Specs require 404 when no section's lessons are found.
if ( ! is_wp_error( $result ) && empty( $result->data ) ) {
return llms_rest_not_found_error();
}
return $result;
}
}