/
class-llms-rest-lessons-controller.php
827 lines (706 loc) · 26.1 KB
/
class-llms-rest-lessons-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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
<?php
/**
* REST Lessons Controller
*
* @package LifterLMS_REST/Classes/Controllers
*
* @since 1.0.0-beta.1
* @version 1.0.0-beta.27
*/
defined( 'ABSPATH' ) || exit;
/**
* LLMS_REST_Lessons_Controller class.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.7 `prepare_objects_query()` renamed to `prepare_collection_query_args()`.
* Added the following properties to the item schema: `drip_date`, `drip_days`, `drip_method`, `public`, `quiz`.
* Added the following links: `prerequisite`, `quiz`.
* Fixed `siblings` link that was using the parent course's id instead of the parent section's id.
* Fixed `parent` link href, replacing 'section' with 'sections'.
* Added following properties to the response object: `public`, `points`, `quiz`, `drip_method`, `drip_days`, `drip_date`, `prerequisite`.
* Fixed lesson progression callback name when defining the filters to be removed while preparing the item for response.
* Added `llms_rest_lesson_item_schema`, `llms_rest_pre_insert_lesson`, `llms_rest_prepare_lesson_object_response`, `llms_rest_lesson_links` filter hooks.
* Added `prepare_item_for_database()`, `update_additional_object_fields()` method.
* @since 1.0.0-beta.8 Call `set_bulk()` llms post method passing `true` as second parameter,
* so to instruct it to return a WP_Error on failure.
* @since 1.0.0-beta.9 Removed `create_llms_post()` and `get_object()` methods, now abstracted in `LLMS_REST_Posts_Controller` class.
* `llms_rest_lesson_filters_removed_for_response` filter hook added.
* @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_Lessons_Controller extends LLMS_REST_Posts_Controller {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'lessons';
/**
* Post type.
*
* @var string
*/
protected $post_type = 'lesson';
/**
* Schema properties available for ordering the collection.
*
* @var string[]
*/
protected $orderby_properties = array(
'id',
'title',
'date_created',
'date_updated',
'order',
'relevance',
);
/**
* Parent section id.
*
* @var int
*/
protected $parent_id;
/**
* Meta field names to skip (added via `register_meta()`).
*
* @var string[]
*/
protected $disallowed_meta_fields = array(
'_llms_assignment',
'_llms_quiz',
);
/**
* Constructor.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.27 Call parent constructor.
*
* @return void
*/
public function __construct() {
parent::__construct();
$this->collection_params = $this->build_collection_params();
}
/**
* 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 lesson for create or update.
*
* @since 1.0.0-beta.7
* @since 1.0.0-beta.15 Fixed setting/updating parent section/course.
* @since 1.0.0-beta.23 Replaced the call to the deprecated `LLMS_Lesson::get_parent_course()` method with `LLMS_Lesson::get( 'parent_course' )`.
* @since 1.0.0-beta.25 Remove now useless check on the existing parent course id when setting it automatically on parent section's id update.
*
* @param WP_REST_Request $request Request object.
* @return array|WP_Error Array of lesson args or WP_Error.
*/
protected function prepare_item_for_database( $request ) {
$prepared_item = parent::prepare_item_for_database( $request );
$schema = $this->get_item_schema();
// Lesson's audio embed URL.
if ( ! empty( $schema['properties']['audio_embed'] ) && isset( $request['audio_embed'] ) ) {
$prepared_item['audio_embed'] = $request['audio_embed'];
}
// Lesson's video embed URL.
if ( ! empty( $schema['properties']['video_embed'] ) && isset( $request['video_embed'] ) ) {
$prepared_item['video_embed'] = $request['video_embed'];
}
// Parent (section) id.
if ( ! empty( $schema['properties']['parent_id'] ) && isset( $request['parent_id'] ) ) {
// Retrieve the parent section.
$parent_section = llms_get_post( $request['parent_id'] );
$prepared_item['parent_section'] = $parent_section && is_a( $parent_section, 'LLMS_Section' ) ? $request['parent_id'] : 0;
// Retrieve the parent course id.
if ( $prepared_item['parent_section'] ) {
$parent_course = $parent_section->get_course();
}
$prepared_item['parent_course'] = ! empty( $parent_course ) && is_a( $parent_course, 'LLMS_Course' ) ? $parent_course->get( 'id' ) : 0;
}
// Course id.
if ( ! empty( $schema['properties']['course_id'] ) && isset( $request['course_id'] ) ) {
$parent_course = llms_get_post( $request['course_id'] );
if ( ! $parent_course || ! is_a( $parent_course, 'LLMS_Course' ) ) {
return llms_rest_bad_request_error( __( 'Invalid course_id param. It must be a valid Course ID.', 'lifterlms' ) );
}
$prepared_item['parent_course'] = $request['course_id'];
}
// 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'];
}
// Public (free lesson).
if ( ! empty( $schema['properties']['public'] ) && isset( $request['public'] ) ) {
$prepared_item['free_lesson'] = empty( $request['public'] ) ? 'no' : 'yes';
}
// Points.
if ( ! empty( $schema['properties']['points'] ) && isset( $request['points'] ) ) {
$prepared_item['points'] = $request['points'];
}
// Drip days.
if ( ! empty( $schema['properties']['drip_days'] ) && isset( $request['drip_days'] ) ) {
// drip_days must be > 0. It's sanitized as absint so it cannot come as negative value.
if ( 0 === $request['drip_days'] ) {
return llms_rest_bad_request_error( __( 'Invalid drip_days param. It must be greater than 0.', 'lifterlms' ) );
}
$prepared_item['days_before_available'] = $request['drip_days'];
}
// Drip date.
if ( ! empty( $schema['properties']['drip_date'] ) && isset( $request['drip_date'] ) ) {
$drip_date = rest_parse_date( $request['drip_date'] );
// Drip date is nullable.
if ( empty( $drip_date ) ) {
$prepared_item['date_available'] = '';
$prepared_item['time_available'] = '';
} else {
$prepared_item['date_available'] = date_i18n( 'Y-m-d', $drip_date );
$prepared_item['time_available'] = date_i18n( 'H:i:s', $drip_date );
}
}
// Drip method.
if ( ! empty( $schema['properties']['drip_method'] ) && isset( $request['drip_method'] ) ) {
$prepared_item['drip_method'] = 'none' === $request['drip_method'] ? '' : $request['drip_method'];
}
// Quiz enabled.
if ( ! empty( $schema['properties']['quiz']['properties']['enabled'] ) && isset( $request['quiz']['enabled'] ) ) {
$prepared_item['quiz_enabled'] = empty( $request['quiz']['enabled'] ) ? 'no' : 'yes';
}
// Quiz id.
if ( ! empty( $schema['properties']['quiz']['properties']['id'] ) && isset( $request['quiz']['id'] ) ) {
// check if quiz exists.
$quiz = llms_get_post( $request['quiz']['id'] );
if ( is_a( $quiz, 'LLMS_Quiz' ) ) {
$prepared_item['quiz'] = $request['quiz']['id'];
}
}
// Quiz progression.
if ( ! empty( $schema['properties']['quiz']['properties']['progression'] ) && isset( $request['quiz']['progression'] ) ) {
$prepared_item['require_passing_grade'] = 'complete' === $request['quiz']['progression'] ? 'no' : 'yes';
}
/**
* Filters a lesson before it is inserted via the REST API.
*
* @since 1.0.0-beta.7
*
* @param array $prepared_item Array of lesson item properties prepared for database.
* @param WP_REST_Request $request Full details about the request.
* @param array $schema The item schema.
*/
return apply_filters( 'llms_rest_pre_insert_lesson', $prepared_item, $request, $schema );
}
/**
* Updates a single llms lesson.
*
* @since 1.0.0-beta.7
* @since 1.0.0-beta.8 Call `set_bulk()` llms post method passing `true` as second parameter,
* so to instruct it to return a WP_Error on failure.
* @since 1.0.0-beta.25 Allow updating meta with the same value as the stored one.
*
* @param LLMS_Lesson $lesson LLMS_Lesson instance.
* @param WP_REST_Request $request Full details about the request.
* @param array $schema The item schema.
* @param array $prepared_item Prepared item array.
* @param bool $creating Optional. Whether we're in creation or update phase. Default true (create).
* @return bool|WP_Error True on success or false if nothing to update, WP_Error object if something went wrong during the update.
*/
protected function update_additional_object_fields( $lesson, $request, $schema, $prepared_item, $creating = true ) {
$error = new WP_Error();
$to_set = array();
// Prerequisite.
if ( ! empty( $schema['properties']['prerequisite'] ) && isset( $request['prerequisite'] ) ) {
// check if lesson exists.
$prerequisite = llms_get_post( $request['prerequisite'] );
if ( is_a( $prerequisite, 'LLMS_Lesson' ) ) {
$to_set['prerequisite'] = $request['prerequisite'];
} else {
$to_set['prerequisite'] = 0;
}
}
// Needed until the following will be implemented: https://github.com/gocodebox/lifterlms/issues/908.
$to_set['has_prerequisite'] = empty( $to_set['prerequisite'] ) ? 'no' : 'yes';
// Set bulk.
if ( ! empty( $to_set ) ) {
$update = $lesson->set_bulk( $to_set, true, true );
if ( is_wp_error( $update ) ) {
$error = $update;
}
}
if ( ! empty( $error->errors ) ) {
return $error;
}
return ! empty( $to_set );
}
/**
* Get the Lesson's schema, conforming to JSON Schema.
*
* @since 1.0.0-beta.27
*
* @return array Item schema data.
*/
protected function get_item_schema_base() {
$schema = (array) parent::get_item_schema_base();
$lesson_properties = array(
'parent_id' => array(
'description' => __( 'WordPress post ID of the parent item. Must be a Section ID. 0 indicates an "orphaned" lesson which can be edited and viewed by instructors and admins but cannot be read by students.', 'lifterlms' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'absint',
),
),
'course_id' => array(
'description' => __( 'WordPress post ID of the lesson\'s parent course.', 'lifterlms' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'absint',
),
'readonly' => true,
),
'order' => array(
'description' => __( 'Order of the lesson within its immediate parent.', 'lifterlms' ),
'type' => 'integer',
'default' => 1,
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'absint',
),
'required' => true,
),
'prerequisite' => array(
'description' => __( 'Lesson ID of the prerequisite lesson.', 'lifterlms' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'absint',
),
),
'points' => array(
'description' => __( 'Determines the weight of the lesson when grading the course.', 'lifterlms' ),
'type' => 'integer',
'default' => 1,
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'absint',
),
),
'audio_embed' => array(
'description' => __( 'URL to an oEmbed enable audio URL.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'format' => 'uri',
'arg_options' => array(
'sanitize_callback' => 'esc_url_raw',
),
),
'video_embed' => array(
'description' => __( 'URL to an oEmbed enable video URL.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'format' => 'uri',
'arg_options' => array(
'sanitize_callback' => 'esc_url_raw',
),
),
'drip_date' => array(
'description' => __(
'The date and time when the lesson becomes available. Applicable only when drip_method is date. Format: Y-m-d H:i:s.',
'lifterlms'
),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'drip_days' => array(
'description' => __( 'Number of days to wait before allowing access to the lesson. Applicable only when drip_method is enrollment, start, or prerequisite.', 'lifterlms' ),
'type' => 'integer',
'default' => 1,
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'absint',
),
),
'drip_method' => array(
'description' => __(
'Determine the method with which to make the lesson content available.
<ul>
<li>none: Drip is disabled; the lesson is immediately available.</li>
<li>date: Lesson is made available at a specific date and time.</li>
<li>enrollment: Lesson is made available a specific number of days after enrollment into the course.</li>
<li>start: Lesson is made available a specific number of days after the course\'s start date. Only available on courses with a access_opens_date.</li>
<li>prerequisite: Lesson is made available a specific number of days after the prerequisite lesson is completed.</li>
</ul>',
'lifterlms'
),
'type' => 'string',
'default' => 'none',
'enum' => array( 'none', 'date', 'enrollment', 'start', 'prerequisite' ),
'context' => array( 'view', 'edit' ),
),
'public' => array(
'description' => __( 'Denotes a lesson that\'s publicly accessible regardless of course enrollment.', 'lifterlms' ),
'type' => 'boolean',
'default' => false,
'context' => array( 'view', 'edit' ),
),
'quiz' => array(
'description' => __( 'Associate a quiz with this lesson.', 'lifterlms' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => null, // Note: sanitization implemented in self::prepare_item_for_database().
'validate_callback' => null, // Note: validation implemented in self::prepare_item_for_database().
),
'properties' => array(
'enabled' => array(
'description' => __( 'Determines if a quiz is enabled for the lesson.', 'lifterlms' ),
'type' => 'boolean',
'default' => false,
'context' => array( 'view', 'edit' ),
),
'id' => array(
'description' => __( 'The post ID of the associated quiz.', 'lifterlms' ),
'type' => 'integer',
'default' => 0,
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'absint',
),
),
'progression' => array(
'description' => __(
'Determines lesson progression requirements related to the quiz.
<ul>
<li>complete: The quiz must be completed (with any grade) to progress the lesson.</li>
<li>pass: A passing grade must be earned to progress the lesson.</li>
</ul>',
'lifterlms'
),
'type' => 'string',
'default' => 'complete',
'enum' => array( 'complete', 'pass' ),
'context' => array( 'view', 'edit' ),
),
),
),
);
$schema['properties'] = array_merge( (array) $schema['properties'], $lesson_properties );
return $schema;
}
/**
* Retrieves the query params for the objects collection.
*
* @since 1.0.0-beta.1
*
* @return array The Lessons 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 Lessons 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 lessons by the parent post (section) 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.7 Added following properties to the response object:
* public, points, quiz, drip_method, drip_days, drip_date, prerequisite, audio_embed, video_embed.
* Added `llms_rest_prepare_lesson_object_response` filter hook.
* @since 1.0.0-beta.23 Replaced the call to the deprecated `LLMS_Lesson::get_parent_course()` method with `LLMS_Lesson::get( 'parent_course' )`.
* public, points, quiz, drip_method, drip_days, drip_date, prerequisite, audio_embed, video_embed.
* Added `llms_rest_prepare_lesson_object_response` filter hook.
*
* @param LLMS_Lesson $lesson Lesson object.
* @param WP_REST_Request $request Full details about the request.
* @return array
*/
protected function prepare_object_for_response( $lesson, $request ) {
$data = parent::prepare_object_for_response( $lesson, $request );
// Audio Embed.
$data['audio_embed'] = $lesson->get( 'audio_embed' );
// Video Embed.
$data['video_embed'] = $lesson->get( 'video_embed' );
// Parent section.
$data['parent_id'] = $lesson->get_parent_section();
// Parent course.
$data['course_id'] = $lesson->get( 'parent_course' );
// Order.
$data['order'] = $lesson->get( 'order' );
// Public.
$data['public'] = $lesson->is_free();
// Points.
$data['points'] = $lesson->get( 'points' );
// Quiz.
$data['quiz']['enabled'] = llms_parse_bool( $lesson->get( 'quiz_enabled' ) );
$data['quiz']['id'] = absint( $lesson->get( 'quiz' ) );
$data['quiz']['progression'] = llms_parse_bool( $lesson->get( 'require_passing_grade' ) ) ? 'pass' : 'completed';
// Drip method.
$data['drip_method'] = $lesson->get( 'drip_method' );
$data['drip_method'] = $data['drip_method'] ? $data['drip_method'] : 'none';
// Drip days.
$data['drip_days'] = absint( $lesson->get( 'days_before_available' ) );
// Drip date.
$date = $lesson->get( 'date_available' );
if ( $date ) {
$time = $lesson->get( 'time_available' );
if ( ! $time ) {
$time = '12:00 AM';
}
$drip_date = date_i18n( 'Y-m-d H:i:s', strtotime( $date . ' ' . $time ) );
} else {
$drip_date = '';
}
$data['drip_date'] = $drip_date;
// Prerequisite.
$data['prerequisite'] = absint( $lesson->get_prerequisite() );
/**
* Filters the lesson data for a response.
*
* @since 1.0.0-beta.7
*
* @param array $data Array of lesson properties prepared for response.
* @param LLMS_Lesson $lesson Lesson object.
* @param WP_REST_Request $request Full details about the request.
*/
return apply_filters( 'llms_rest_prepare_lesson_object_response', $data, $lesson, $request );
}
/**
* 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_section',
'value' => $parent_id,
'compare' => '=',
),
),
)
);
}
return $query_args;
}
/**
* Get action/filters to be removed before preparing the item for response.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.7 Fixed lesson progression callback name.
* @since 1.0.0-beta.9 `llms_rest_lesson_filters_removed_for_response` filter hook added.
*
* @param LLMS_Lesson $lesson Lesson object.
* @return array Array of action/filters to be removed for response.
*/
protected function get_filters_to_be_removed_for_response( $lesson ) {
$filters = array();
if ( llms_blocks_is_post_migrated( $lesson->get( 'id' ) ) ) {
$filters = array(
// hook => [callback, priority].
'lifterlms_single_lesson_after_summary' => array(
// Lesson Navigation.
array(
'callback' => 'lifterlms_template_lesson_navigation',
'priority' => 20,
),
// Lesson Progression.
array(
'callback' => 'lifterlms_template_complete_lesson_link',
'priority' => 10,
),
),
);
}
/**
* Modify the array of filters to be removed before building the response.
*
* @since 1.0.0-beta.9
*
* @param array $filters Array of filters to be removed.
* @param LLMS_Lesson $lesson Lesson object.
*/
return apply_filters( 'llms_rest_lesson_filters_removed_for_response', $filters, $lesson );
}
/**
* Prepare links for the request.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.7 Fixed `siblings` link that was using the parent course's id instead of the parent section's id.
* Fixed `parent` link href, replacing 'section' with 'sections'.
* Following links added: `prerequisite`, `quiz`.
* Added `llms_rest_lesson_links` filter hook.
* @since 1.0.0-beta.14 Added `$request` parameter.
* @since 1.0.0-beta.23 Replaced the call to the deprecated `LLMS_Lesson::get_parent_course()` method with `LLMS_Lesson::get( 'parent_course' )`.
*
* @param LLMS_Lesson $lesson LLMS Section.
* @param WP_REST_Request $request Request object.
* @return array Links for the given object.
*/
protected function prepare_links( $lesson, $request ) {
$links = parent::prepare_links( $lesson, $request );
unset( $links['content'] );
$lesson_id = $lesson->get( 'id' );
$parent_course_id = $lesson->get( 'parent_course' );
$parent_section_id = $lesson->get_parent_section();
$lesson_links = array();
// Parent course.
if ( $parent_course_id ) {
$lesson_links['course'] = array(
'href' => rest_url( sprintf( '/%s/%s/%d', 'llms/v1', 'courses', $parent_course_id ) ),
);
}
// Parent section.
if ( $parent_section_id ) {
$lesson_links['parent'] = array(
'type' => 'section',
'href' => rest_url( sprintf( '/%s/%s/%d', 'llms/v1', 'sections', $parent_section_id ) ),
);
}
// Siblings.
$lesson_links['siblings'] = array(
'href' => add_query_arg(
'parent',
$parent_section_id,
$links['collection']['href']
),
);
// Next.
$next_lesson = $lesson->get_next_lesson();
if ( $next_lesson ) {
$lesson_links['next'] = array(
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $next_lesson ) ),
);
}
// Previous.
$previous_lesson = $lesson->get_previous_lesson();
if ( $previous_lesson ) {
$lesson_links['previous'] = array(
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $previous_lesson ) ),
);
}
// Prerequisite.
$prerequisite = $lesson->get_prerequisite();
if ( ! empty( $prerequisite ) ) {
$lesson_links['prerequisite'] = array(
'type' => $this->post_type,
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $prerequisite ) ),
);
}
// Quiz.
if ( $lesson->is_quiz_enabled() ) {
$quiz = $lesson->get_quiz();
$lesson_links['quiz'] = array(
'href' => rest_url( sprintf( '/%s/%s/%d', 'llms/v1', 'quizzes', $quiz->get( 'id' ) ) ),
);
}
$links = array_merge( $links, $lesson_links );
/**
* Filters the lesson's links.
*
* @since 1.0.0-beta.7
*
* @param array $links Links for the given lesson.
* @param LLMS_Lesson $lesson Lesson object.
*/
return apply_filters( 'llms_rest_lesson_links', $links, $lesson );
}
/**
* Checks if a Lesson can be read
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.23 Replaced the call to the deprecated `LLMS_Lesson::get_parent_course()` method with `LLMS_Lesson::get( 'parent_course' )`.
*
* @param LLMS_Lesson $lesson The Lesson object.
* @return bool Whether the post can be read.
*
* @todo Implement read permission based on the section's id:
* 0 indicates an "orphaned" lesson which can be edited and viewed by instructors and admins but cannot be read by students.
*/
protected function check_read_permission( $lesson ) {
/**
* As of now, lessons of password protected courses cannot be read
*/
if ( post_password_required( $lesson->get( 'parent_course' ) ) ) {
return false;
}
/**
* At the moment we grant lessons read permission only to who can edit lessons.
*/
return parent::check_update_permission( $lesson );
}
}