/
class-llms-rest-courses-controller.php
1253 lines (1117 loc) · 46.4 KB
/
class-llms-rest-courses-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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* REST Courses Controller
*
* @package LifterLMS_REST/Classes/Controllers
*
* @since 1.0.0-beta.1
* @version 1.0.0-beta.27
*/
defined( 'ABSPATH' ) || exit;
/**
* LLMS_REST_Courses_Controller class.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.7 Make `access_opens_date`, `access_closes_date`, `enrollment_opens_date`, `enrollment_closes_date` nullable.
* Allow `prerequisite` and `prerequisite_track` to be cleared (set to 0).
* Also:
* - if `prerequisite` is not a valid course the course `prerequisite` will be set to 0;
* - if `prerequisite_track` is not a valid course track, the course `prerequisite_track` will be set to 0.
*
* `update_additional_object_fields()` returns false if nothing to update.
* Properties `access_opens_date`, `access_closes_date`, `enrollment_opens_date`, `enrollment_closes_date` handling
* moved here from `prepare_item_for_database()` method to `update_additional_object_fields()` method so to better handle the update of the
* course's properties `time_period` and `enrollment_period`.
* Added logic to prevent trying to update "derived only" courses's properties (`time_period`, `enrollment_period`, `has_prerequisite`)
* if their values didn't really change, otherwise we'd get a WP_Error which the consumer cannot avoid having no direct control on those properties.
* In `update_additional_object_fields()` method, use `WP_Error::$errors` in place of `WP_Error::has_errors()`
* to support WordPress version prior to 5.1.
* Overridden `get_object_id()` method to avoid using the deprecated `LLMS_Course::get_id()` which,
* as coded in the `LLMS_REST_Controller_Stubs::get_object_id()` takes precedence over `get( 'id' )`.
* @since 1.0.0-beta.8 Fixed `sales_page_type` not returned as `none` if course's `sales_page_content_type` property is empty.
* Renamed `sales_page_page_type` and `sales_page_page_url` properties, respectively to `sales_page_type` and `sales_page_url` according to the specs.
* Add missing quotes in enrollment/access default messages shortcodes.
* Call `set_bulk()` llms post method passing `true` as second parameter, so to instruct it to return a WP_Error on failure.
* Add missing quotes in enrollment/access default messages shortcodes.
* `sales_page_page_id` and `sales_page_url` always returned in edit context.
* @since 1.0.0-beta.9 In `update_additional_object_fields()` method, use `WP_Error::$errors` in place of `WP_Error::has_errors()` to support WordPress version prior to 5.1.
* Also made sure course's `instructor` is at least set as the post author.
* Defined `instructors` validate callback so to make sure instructors list is either not empty and composed by real user ids.
* Fixed `sales_page_url` not returned in `edit` context.
* Removed `create_llms_post()` and `get_object()` methods, now abstracted in `LLMS_REST_Posts_Controller` class.
* `llms_rest_course_filters_removed_for_response` filter hook added.
* Added `llms_rest_course_item_schema`, `llms_rest_pre_insert_course`, `llms_rest_prepare_course_object_response`, `llms_rest_course_links` filter hooks.
* @since 1.0.0-beta.14 Update `prepare_links()` to accept a second parameter, `WP_REST_Request`.
*/
class LLMS_REST_Courses_Controller extends LLMS_REST_Posts_Controller {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'courses';
/**
* Post type.
*
* @var string
*/
protected $post_type = 'course';
/**
* Enrollments controller.
*
* @var LLMS_REST_Enrollments_Controller
*/
protected $enrollments_controller;
/**
* Sections controller.
*
* @var LLMS_REST_Sections_Controller
*/
protected $sections_controller;
/**
* Additional rest field names to skip (added via `register_rest_field()`).
*
* @var string[]
*/
protected $disallowed_additional_fields = array(
'visibility', // It maps to `catalog_visibility` in the resource schema.
);
/**
* Meta field names to skip (added via `register_meta()`).
*
* @var string[]
*/
protected $disallowed_meta_fields = array(
'_llms_length',
);
/**
* Constructor.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.27 Call parent constructor.
*
* @return void
*/
public function __construct() {
parent::__construct();
$this->enrollments_controller = new LLMS_REST_Enrollments_Controller();
$this->enrollments_controller->set_collection_params( $this->get_enrollments_collection_params() );
$this->sections_controller = new LLMS_REST_Sections_Controller( '' );
$this->sections_controller->set_collection_params( $this->get_course_content_collection_params() );
}
/**
* Register routes.
*
* @since 1.0.0-beta.1
*
* @return void
*/
public function register_routes() {
parent::register_routes();
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)/enrollments',
array(
'args' => array(
'id' => array(
'description' => __( 'Unique Course Identifier. The WordPress Post ID', 'lifterlms' ),
'type' => 'integer',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this->enrollments_controller, 'get_items' ),
'permission_callback' => array( $this->enrollments_controller, 'get_items_permissions_check' ),
'args' => $this->enrollments_controller->get_collection_params(),
),
'schema' => array( $this->enrollments_controller, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)/content',
array(
'args' => array(
'id' => array(
'description' => __( 'Unique Course Identifier. The WordPress Post ID', 'lifterlms' ),
'type' => 'integer',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_course_content_items' ),
'permission_callback' => array( $this->sections_controller, 'get_items_permissions_check' ),
'args' => $this->sections_controller->get_collection_params(),
),
'schema' => array( $this->sections_controller, 'get_public_item_schema' ),
)
);
}
/**
* Retrieve an ID from the object.
*
* @since 1.0.0-beta.7
*
* @param LLMS_Course $object LLMS_Course object.
* @return int
*/
protected function get_object_id( $object ) {
// For example.
return $object->get( 'id' );
}
/**
* Get the Course's schema, conforming to JSON Schema.
*
* @since 1.0.0-beta.27
*
* @return array
*/
protected function get_item_schema_base() {
$schema = (array) parent::get_item_schema_base();
$course_properties = array(
'catalog_visibility' => array(
'description' => __( 'Visibility of the course in catalogs and search results.', 'lifterlms' ),
'type' => 'string',
'enum' => array_keys( llms_get_product_visibility_options() ),
'default' => 'catalog_search',
'context' => array( 'view', 'edit' ),
),
// consider to move tags and cats in the posts controller abstract.
'categories' => array(
'description' => __( 'List of course categories.', 'lifterlms' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'context' => array( 'view', 'edit' ),
),
'tags' => array(
'description' => __( 'List of course tags.', 'lifterlms' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'context' => array( 'view', 'edit' ),
),
'difficulties' => array(
'description' => __( 'List of course difficulties.', 'lifterlms' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'context' => array( 'view', 'edit' ),
),
'tracks' => array(
'description' => __( 'List of course tracks.', 'lifterlms' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'context' => array( 'view', 'edit' ),
),
'instructors' => array(
'description' => __( 'List of course instructors. Defaults to current user when creating a new post.', 'lifterlms' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'arg_options' => array(
'validate_callback' => 'llms_validate_instructors',
),
'context' => array( 'view', 'edit' ),
),
'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',
),
),
'capacity_enabled' => array(
'description' => __( 'Determines if an enrollment capacity limit is enabled.', 'lifterlms' ),
'type' => 'boolean',
'default' => false,
),
'capacity_limit' => array(
'description' => __( 'Number of students who can be enrolled in the course before enrollment closes.', 'lifterlms' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'absint',
),
),
'capacity_message' => array(
'description' => __( 'Message displayed when enrollment capacity has been reached.', '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(
'raw' => array(
'description' => __( 'Raw message content.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'edit' ),
),
'rendered' => array(
'description' => __( 'Rendered message content.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
),
'prerequisite' => array(
'description' => __( 'Course ID of the prerequisite course.', 'lifterlms' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'absint',
),
),
'prerequisite_track' => array(
'description' => __( 'Term ID of a prerequisite track.', 'lifterlms' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'absint',
),
),
'length' => array(
'description' => __( 'User defined course length.', '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(
'raw' => array(
'description' => __( 'Raw length description.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'edit' ),
),
'rendered' => array(
'description' => __( 'Rendered length description.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
),
'restricted_message' => array(
'description' => __( 'Message displayed when non-enrolled visitors try to access restricted course content (lessons, quizzes, etc..) directly.', '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(
'raw' => array(
'description' => __( 'Raw message content.', 'lifterlms' ),
'default' => __( 'You must enroll in this course to access course content.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'edit' ),
),
'rendered' => array(
'description' => __( 'Rendered message content.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
'default' => __( 'You must enroll in this course to access course content.', 'lifterlms' ),
),
'access_closes_date' => array(
'description' => __(
'Date when the course closes. After this date enrolled students may no longer view and interact with the restricted course content.
If blank the course is open indefinitely after the the access_opens_date has passed.
Does not affect course enrollment, see enrollment_opens_date to control the course enrollment close date.
Format: Y-m-d H:i:s.',
'lifterlms'
),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'access_closes_message' => array(
'description' => __( 'Message displayed to enrolled students when the course is accessed after the access_closes_date has passed.', '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(
'raw' => array(
'description' => __( 'Raw message content.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'edit' ),
),
'rendered' => array(
'description' => __( 'Rendered message content.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
'default' => __( 'This course closed on [lifterlms_course_info id="{{course_id}}" key="end_date"].', 'lifterlms' ),
),
'access_opens_date' => array(
'description' => __(
'Date when the course opens, allowing enrolled students to begin to view and interact with the restricted course content.
If blank the course is open until after the access_closes_date has passed.
Does not affect course enrollment, see enrollment_opens_date to control the course enrollment start date.
Format: Y-m-d H:i:s.',
'lifterlms'
),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'access_opens_message' => array(
'description' => __( 'Message displayed to enrolled students when the course is accessed before the access_opens_date has passed.', '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(
'raw' => array(
'description' => __( 'Raw message content.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'edit' ),
),
'rendered' => array(
'description' => __( 'Rendered message content.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
'default' => __( 'This course opens on [lifterlms_course_info id="{{course_id}}" key="start_date"].', 'lifterlms' ),
),
'enrollment_closes_date' => array(
'description' => __(
'Date when the course enrollment closes.
If blank course enrollment is open indefinitely after the the enrollment_opens_date has passed.
Does not affect course content access, see access_opens_date to control course access close date.
Format: Y-m-d H:i:s.',
'lifterlms'
),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'enrollment_closes_message' => array(
'description' => __( 'Message displayed to visitors when attempting to enroll into a course after the enrollment_closes_date has passed.', '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(
'raw' => array(
'description' => __( 'Raw message content.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'edit' ),
),
'rendered' => array(
'description' => __( 'Rendered message content.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
'default' => __( 'Enrollment in this course closed on [lifterlms_course_info id="{{course_id}}" key="enrollment_end_date"].', 'lifterlms' ),
),
'enrollment_opens_date' => array(
'description' => __(
'Date when the course enrollment opens.
If blank course enrollment is open until after the enrollment_closes_date has passed.
Does not affect course content access, see access_opens_date to control course access start date.
Format: Y-m-d H:i:s.',
'lifterlms'
),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'enrollment_opens_message' => array(
'description' => __( 'Message displayed to visitors when attempting to enroll into a course before the enrollment_opens_date has passed.', '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(
'raw' => array(
'description' => __( 'Raw message content.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'edit' ),
),
'rendered' => array(
'description' => __( 'Rendered message content.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
'default' => __( 'Enrollment in this course opens on [lifterlms_course_info id="{{course_id}}" key="enrollment_start_date"].', 'lifterlms' ),
),
'sales_page_page_id' => array(
'description' => __(
'The WordPress page ID of the sales page. Required when sales_page_type equals page. Only returned when the sales_page_type equals page.',
'lifterlms'
),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'absint',
),
),
'sales_page_type' => array(
'description' => __(
'Determines the type of sales page content to display.<br> - <code>none</code> displays the course content.<br> - <code>content</code> displays alternate content from the <code>excerpt</code> property.<br> - <code>page</code> redirects to the WordPress page defined in <code>content_page_id</code>.<br> - <code>url</code> redirects to the URL defined in <code>content_page_url</code>',
'lifterlms'
),
'type' => 'string',
'default' => 'none',
'enum' => array_keys( llms_get_sales_page_types() ),
'context' => array( 'view', 'edit' ),
),
'sales_page_url' => array(
'description' => __(
'The URL of the sales page content. Required when <code>content_type</code> equals <code>url</code>. Only returned when the <code>content_type</code> equals <code>url</code>.',
'lifterlms'
),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'format' => 'uri',
'arg_options' => array(
'sanitize_callback' => 'esc_url_raw',
),
),
'video_tile' => array(
'description' => __( 'When true the video_embed will be used on the course tiles (on the catalog, for example) instead of the featured image.', 'lifterlms' ),
'type' => 'boolean',
'default' => false,
'context' => array( 'view', 'edit' ),
),
);
$schema['properties'] = array_merge( (array) $schema['properties'], $course_properties );
return $schema;
}
/**
* Prepare a single object output for response.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.8 Fixed `sales_page_type` not set as `none` if course's `sales_page_content_type` property is empty.
* Also Renamed `sales_page_page_type` and `sales_page_page_url` properties, respectively to `sales_page_type` and `sales_page_url` according to the specs.
* Always return `sales_page_url` and `sales_page_page_id` when in `edit` context.
* @since 1.0.0-beta.9 Fixed `sales_page_url` not returned in `edit` context.
* Added `llms_rest_prepare_course_object_response` filter hook.
*
* @param LLMS_Course $course Course object.
* @param WP_REST_Request $request Full details about the request.
* @return array
*/
protected function prepare_object_for_response( $course, $request ) {
$data = parent::prepare_object_for_response( $course, $request );
// Catalog visibility.
$data['catalog_visibility'] = $course->get_product()->get_catalog_visibility();
// Categories.
$data['categories'] = $course->get_categories(
array(
'fields' => 'ids',
)
);
// Tags.
$data['tags'] = $course->get_tags(
array(
'fields' => 'ids',
)
);
// Difficulties.
$difficulties = $course->get_difficulty( 'term_id' );
$difficulties = empty( $difficulties ) ? array() : array( $difficulties );
$data['difficulties'] = $difficulties;
// Tracks.
$data['tracks'] = $course->get_tracks(
array(
'fields' => 'ids',
)
);
// Instructors.
$instructors = $course->get_instructors();
$instructors = empty( $instructors ) ? array() : wp_list_pluck( $instructors, 'id' );
$data['instructors'] = $instructors;
// Audio Embed.
$data['audio_embed'] = $course->get( 'audio_embed' );
// Video Embed.
$data['video_embed'] = $course->get( 'video_embed' );
// Video tile.
$data['video_tile'] = 'yes' === $course->get( 'tile_featured_video' );
// Capacity.
$data['capacity_enabled'] = 'yes' === $course->get( 'enable_capacity' );
$data['capacity_limit'] = $course->get( 'capacity' );
$data['capacity_message'] = array(
'raw' => $course->get( 'capacity_message', $raw = true ),
'rendered' => do_shortcode( $course->get( 'capacity_message' ) ),
);
// Prerequisite.
$data['prerequisite'] = (int) $course->get_prerequisite_id();
// Prerequisite track.
$data['prerequisite_track'] = (int) $course->get_prerequisite_id( 'course_track' );
// Length.
$data['length'] = array(
'raw' => $course->get( 'length', $raw = true ),
'rendered' => do_shortcode( $course->get( 'length' ) ),
);
// Restricted message.
$data['restricted_message'] = array(
'raw' => $course->get( 'content_restricted_message', $raw = true ),
'rendered' => do_shortcode( $course->get( 'content_restricted_message' ) ),
);
// Access open/closed.
$data['access_opens_date'] = $course->get_date( 'start_date', 'Y-m-d H:i:s' );
$data['access_closes_date'] = $course->get_date( 'end_date', 'Y-m-d H:i:s' );
$data['access_opens_message'] = array(
'raw' => $course->get( 'course_opens_message', $raw = true ),
'rendered' => do_shortcode( $course->get( 'course_opens_message' ) ),
);
$data['access_closes_message'] = array(
'raw' => $course->get( 'course_closed_message', $raw = true ),
'rendered' => do_shortcode( $course->get( 'course_closed_message' ) ),
);
// Enrollment open/closed.
$data['enrollment_opens_date'] = $course->get_date( 'enrollment_start_date', 'Y-m-d H:i:s' );
$data['enrollment_closes_date'] = $course->get_date( 'enrollment_end_date', 'Y-m-d H:i:s' );
$data['enrollment_opens_message'] = array(
'raw' => $course->get( 'enrollment_opens_message', $raw = true ),
'rendered' => do_shortcode( $course->get( 'enrollment_opens_message' ) ),
);
$data['enrollment_closes_message'] = array(
'raw' => $course->get( 'enrollment_closed_message', $raw = true ),
'rendered' => do_shortcode( $course->get( 'enrollment_closed_message' ) ),
);
// Sales page page type.
$data['sales_page_type'] = $course->get( 'sales_page_content_type' );
$data['sales_page_type'] = $data['sales_page_type'] ? $data['sales_page_type'] : 'none';
// Sales page id.
if ( 'page' === $data['sales_page_type'] || 'edit' === $request['context'] ) {
$data['sales_page_page_id'] = $course->get( 'sales_page_content_page_id' );
}
// Sales page url.
if ( 'url' === $data['sales_page_type'] || 'edit' === $request['context'] ) {
$data['sales_page_url'] = $course->get( 'sales_page_content_url' );
}
/**
* Filters the course data for a response.
*
* @since 1.0.0-beta.9
*
* @param array $data Array of course properties prepared for response.
* @param LLMS_Course $course Course object.
* @param WP_REST_Request $request Full details about the request.
*/
return apply_filters( 'llms_rest_prepare_course_object_response', $data, $course, $request );
}
/**
* Prepares a single post for create or update.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.7 `access_opens_date`, `access_closes_date`, `enrollment_opens_date`, `enrollment_closes_date`
* treated in @see `update_additional_object_fields()` method so to better handle the update of the
* course's properties `time_period` and `enrollment_period` whose values are derived from them and need to be
* passed to `$course->set_bulk()` only if they differ from their current values, otherwise we'd get a WP_Error
* which the consumer cannot avoid having no direct control on those properties.
* Made `access_opens_date`, `access_closes_date`, `enrollment_opens_date`, `enrollment_closes_date` nullable.
* @since 1.0.0-beta.8 Renamed `sales_page_page_type` and `sales_page_page_url` properties, respectively to `sales_page_type` and `sales_page_url` according to the specs.
* @since 1.0.0-beta.9 Added `llms_rest_pre_insert_course` filter hook.
*
* @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();
// Course Audio embed URL.
if ( ! empty( $schema['properties']['audio_embed'] ) && isset( $request['audio_embed'] ) ) {
$prepared_item['audio_embed'] = $request['audio_embed'];
}
// Course Video embed URL.
if ( ! empty( $schema['properties']['video_embed'] ) && isset( $request['video_embed'] ) ) {
$prepared_item['video_embed'] = $request['video_embed'];
}
// Video tile.
if ( ! empty( $schema['properties']['video_tile'] ) && isset( $request['video_tile'] ) ) {
$prepared_item['tile_featured_video'] = empty( $request['video_tile'] ) ? 'no' : 'yes';
}
// Capacity enabled.
if ( ! empty( $schema['properties']['capacity_enabled'] ) && isset( $request['capacity_enabled'] ) ) {
$prepared_item['enable_capacity'] = empty( $request['capacity_enabled'] ) ? 'no' : 'yes';
}
// Capacity message.
if ( ! empty( $schema['properties']['capacity_message'] ) && isset( $request['capacity_message'] ) ) {
if ( is_string( $request['capacity_message'] ) ) {
$prepared_item['capacity_message'] = $request['capacity_message'];
} elseif ( isset( $request['capacity_message']['raw'] ) ) {
$prepared_item['capacity_message'] = $request['capacity_message']['raw'];
}
}
// Capacity limit.
if ( ! empty( $schema['properties']['capacity_limit'] ) && isset( $request['capacity_limit'] ) ) {
$prepared_item['capacity'] = $request['capacity_limit'];
}
// Restricted message.
if ( ! empty( $schema['properties']['restricted_message'] ) && isset( $request['restricted_message'] ) ) {
if ( is_string( $request['restricted_message'] ) ) {
$prepared_item['content_restricted_message'] = $request['restricted_message'];
} elseif ( isset( $request['restricted_message']['raw'] ) ) {
$prepared_item['content_restricted_message'] = $request['restricted_message']['raw'];
}
}
// Length.
if ( ! empty( $schema['properties']['length'] ) && isset( $request['length'] ) ) {
if ( is_string( $request['length'] ) ) {
$prepared_item['length'] = $request['length'];
} elseif ( isset( $request['length']['raw'] ) ) {
$prepared_item['length'] = $request['length']['raw'];
}
}
// Sales page.
if ( ! empty( $schema['properties']['sales_page_type'] ) && isset( $request['sales_page_type'] ) ) {
$prepared_item['sales_page_content_type'] = $request['sales_page_type'];
}
if ( ! empty( $schema['properties']['sales_page_page_id'] ) && isset( $request['sales_page_page_id'] ) ) {
$sales_page = get_post( $request['sales_page_page_id'] );
if ( $sales_page && is_a( $sales_page, 'WP_Post' ) ) {
$prepared_item['sales_page_content_page_id'] = $request['sales_page_page_id']; // maybe allow only published pages?
} else {
$prepared_item['sales_page_content_page_id'] = 0;
}
}
if ( ! empty( $schema['properties']['sales_page_url'] ) && isset( $request['sales_page_url'] ) ) {
$prepared_item['sales_page_content_url'] = $request['sales_page_url'];
}
/**
* Filters the course data for a response.
*
* @since 1.0.0-beta.9
*
* @param array $prepared_item Array of course 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_course', $prepared_item, $request, $schema );
}
/**
* Updates a single llms course.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.7 Allow `prerequisite` and `prerequisite_track` to be cleared (set to 0).
* Also:
* - if `prerequisite` is not a valid course the course `prerequisite` will be set to 0;
* - if `prerequisite_track` is not a valid course track, the course `prerequisite_track` will be set to 0.
*
* Return false if nothing to update.
*
* Properties `access_opens_date`, `access_closes_date`, `enrollment_opens_date`, `enrollment_closes_date` handling
* moved here from `prepare_item_for_database()` method so to better handle the update of the
* course's properties `time_period` and `enrollment_period`.
*
* Made `access_opens_date`, `access_closes_date`, `enrollment_opens_date`, `enrollment_closes_date` properties nullable.
*
* Added logic to prevent trying to update "derived only" courses's properties (`time_period`, `enrollment_period`, `has_prerequisite`)
* if their values didn't really change, otherwise we'd get a WP_Error which the consumer cannot avoid having no direct control on those properties.
* @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 Use `WP_Error::$errors` in place of `WP_Error::has_errors()` to support WordPress version prior to 5.1.
* Also made sure course's `instructor` is at least set as the post author.
* @since 1.0.0-beta.25 Allow updating meta with the same value as the stored one.
*
* @param LLMS_Course $course LLMS_Course instance.
* @param WP_REST_Request $request Full details about the request.
* @param array $schema The item schema.
* @param array $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( $course, $request, $schema, $prepared_item, $creating = true ) {
$error = new WP_Error();
// Course catalog visibility.
if ( ! empty( $schema['properties']['catalog_visibility'] ) && isset( $request['catalog_visibility'] ) ) {
$course->get_product()->set_catalog_visibility( $request['catalog_visibility'] );
}
// Instructors.
if ( ! empty( $schema['properties']['instructors'] ) ) {
$instructors = array();
if ( isset( $request['instructors'] ) ) {
foreach ( $request['instructors'] as $instructor_id ) {
$user_data = get_userdata( $instructor_id );
if ( ! empty( $user_data ) ) {
$instructors[] = array(
'id' => $instructor_id,
'name' => $user_data->display_name,
);
}
}
}
// When creating always make sure the instructors are set.
// Note: `$course->set_instructor( $instructors )` when `$instructors` is empty
// will set the course's author as course's instructor.
if ( $creating || ( ! $creating && isset( $request['instructors'] ) ) ) {
$course->set_instructors( $instructors );
}
}
$to_set = array();
// Access dates.
if ( ! empty( $schema['properties']['access_opens_date'] ) && isset( $request['access_opens_date'] ) ) {
$access_opens_date = rest_parse_date( $request['access_opens_date'] );
$to_set['start_date'] = empty( $access_opens_date ) ? '' : date_i18n( 'Y-m-d H:i:s', $access_opens_date );
}
if ( ! empty( $schema['properties']['access_closes_date'] ) && isset( $request['access_closes_date'] ) ) {
$access_closes_date = rest_parse_date( $request['access_closes_date'] );
$to_set['end_date'] = empty( $access_closes_date ) ? '' : date_i18n( 'Y-m-d H:i:s', $access_closes_date );
}
// Needed until the following will be implemented: https://github.com/gocodebox/lifterlms/issues/908.
if ( ! empty( $to_set['start_date'] ) || ! empty( $to_set['end_date'] ) ) {
$to_set['time_period'] = 'yes';
} else {
$to_set['time_period'] = 'no';
}
// Enrollment dates.
if ( ! empty( $schema['properties']['enrollment_opens_date'] ) && isset( $request['enrollment_opens_date'] ) ) {
$enrollment_opens_date = rest_parse_date( $request['enrollment_opens_date'] );
$to_set['enrollment_start_date'] = empty( $enrollment_opens_date ) ? '' : date_i18n( 'Y-m-d H:i:s', $enrollment_opens_date );
}
if ( ! empty( $schema['properties']['enrollment_closes_date'] ) && isset( $request['enrollment_closes_date'] ) ) {
$enrollment_closes_date = rest_parse_date( $request['enrollment_closes_date'] );
$to_set['enrollment_end_date'] = empty( $enrollment_closes_date ) ? '' : date_i18n( 'Y-m-d H:i:s', $enrollment_closes_date );
}
// Needed until the following will be implemented: https://github.com/gocodebox/lifterlms/issues/908.
if ( ! empty( $to_set['enrollment_start_date'] ) || ! empty( $to_set['enrollment_end_date'] ) ) {
$to_set['enrollment_period'] = 'yes';
} else {
$to_set['enrollment_period'] = 'no';
}
// Prerequisite.
if ( ! empty( $schema['properties']['prerequisite'] ) && isset( $request['prerequisite'] ) ) {
// check if course exists.
$prerequisite = llms_get_post( $request['prerequisite'] );
if ( is_a( $prerequisite, 'LLMS_Course' ) ) {
$to_set['prerequisite'] = $request['prerequisite'];
} else {
$to_set['prerequisite'] = 0;
}
}
// Prerequisite track.
if ( ! empty( $schema['properties']['prerequisite_track'] ) && isset( $request['prerequisite_track'] ) ) {
// check if the track exists.
$track = new LLMS_Track( $request['prerequisite_track'] );
if ( $track->term ) {
$to_set['prerequisite_track'] = $request['prerequisite_track'];
} else {
$to_set['prerequisite_track'] = 0;
}
}
// Needed until the following will be implemented: https://github.com/gocodebox/lifterlms/issues/908.
if ( ! empty( $to_set['prerequisite'] ) || ! empty( $to_set['prerequisite_track'] ) ) {
$to_set['has_prerequisite'] = 'yes';
} else {
$to_set['has_prerequisite'] = 'no';
}
/**
* The following properties have a default value that contains a placeholder ({{course_id}}) that can be "expanded" only
* after the course has been created.
*/
// Access opens/closes messages.
if ( ! empty( $schema['properties']['access_opens_message'] ) && isset( $request['access_opens_message'] ) ) {
if ( is_string( $request['access_opens_message'] ) ) {
$to_set['course_opens_message'] = $request['access_opens_message'];
} elseif ( isset( $request['access_opens_message']['raw'] ) ) {
$to_set['course_opens_message'] = $request['access_opens_message']['raw'];
}
}
if ( ! empty( $schema['properties']['access_closes_message'] ) && isset( $request['access_closes_message'] ) ) {
if ( is_string( $request['access_closes_message'] ) ) {
$to_set['course_closed_message'] = $request['access_closes_message'];
} elseif ( isset( $request['access_closes_message']['raw'] ) ) {
$to_set['course_closed_message'] = $request['access_closes_message']['raw'];
}
}
// Enrollments opens/closes messages.
if ( ! empty( $schema['properties']['enrollment_opens_message'] ) && isset( $request['enrollment_opens_message'] ) ) {
if ( is_string( $request['enrollment_opens_message'] ) ) {
$to_set['enrollment_opens_message'] = $request['enrollment_opens_message'];
} elseif ( isset( $request['enrollment_opens_message']['raw'] ) ) {
$to_set['enrollment_opens_message'] = $request['enrollment_opens_message']['raw'];
}
}
if ( ! empty( $schema['properties']['enrollment_closes_message'] ) && isset( $request['enrollment_closes_message'] ) ) {
if ( is_string( $request['enrollment_closes_message'] ) ) {
$to_set['enrollment_closed_message'] = $request['enrollment_closes_message'];
} elseif ( isset( $request['enrollment_closes_message']['raw'] ) ) {
$to_set['enrollment_closed_message'] = $request['enrollment_closes_message']['raw'];
}
}
// Are we creating a course?
// If so, replace the placeholder with the actual course id.
if ( $creating ) {
$_to_expand_props = array(
'course_opens_message',
'course_closed_message',
'enrollment_opens_message',
'enrollment_closed_message',
);
$course_id = $course->get( 'id' );
foreach ( $_to_expand_props as $prop ) {
if ( ! empty( $to_set[ $prop ] ) ) {
$to_set[ $prop ] = str_replace( '{{course_id}}', $course_id, $to_set[ $prop ] );
}
}
}
// Set bulk.
if ( ! empty( $to_set ) ) {
$update = $course->set_bulk( $to_set, true, true );
if ( is_wp_error( $update ) ) {
$error = $update;
}
}
if ( $error->errors ) {
return $error;
}
return ! empty( $to_set );
}
/**
* Maps a taxonomy name to the relative rest base