-
Notifications
You must be signed in to change notification settings - Fork 7
/
class-llms-rest-posts-controller.php
1732 lines (1464 loc) · 54.5 KB
/
class-llms-rest-posts-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 LLMS Posts Controller Class
*
* @package LifterLMS_REST/Abstracts
*
* @since 1.0.0-beta.1
* @version 1.0.0-beta.18
*/
defined( 'ABSPATH' ) || exit;
/**
* LLMS_REST_Posts_Controller
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.2 Filter taxonomies by `public` property instead of `show_in_rest`.
* @since 1.0.0-beta.3 Filter taxonomies by `show_in_llms_rest` property instead of `public`.
* @since 1.0.0-beta.7 Added: `check_read_object_permissions()`, `get_objects_from_query()`, `get_objects_query()`, `get_pagination_data_from_query()`, `prepare_collection_items_for_response()` methods overrides.
* `get_items()` method removed, now abstracted in LLMS_REST_Controller.
* `prepare_objects_query()` renamed to `prepare_collection_query_args()`.
* On `update_item`, don't execute `$object->set_bulk()` when there's no data to update.
* Fix wp:featured_media link, we don't expose any embeddable field.
* Also `self` and `collection` links prepared in the parent class.
* Added `"llms_rest_insert_{$this->post_type}"` and `"llms_rest_insert_{$this->post_type}"` action hooks:
* fired after inserting/updateing an llms post into the database.
* @since 1.0.0-beta.8 Return links to those taxonomies which have an accessible rest route.
* Initialize `$prepared_item` array before adding values to it.
* @since 1.0.0-beta.9 Implemented a generic way to create and get an llms post object instance given a `post_type`.
* In `get_objects_from_query()` avoid performing an additional query, just return the already retrieved posts.
* Removed `"llms_rest_{$this->post_type}_filters_removed_for_reponse"` filter hooks,
* `"llms_rest_{$this->post_type}_filters_removed_for_response"` added.
* @since 1.0.0-beta.11 Fixed `"llms_rest_insert_{$this->post_type}"` and `"llms_rest_insert_{$this->post_type}"` action hooks fourth param:
* must be false when updating.
* @since 1.0.0-beta.12 Moved parameters to query args mapping from `$this->prepare_collection_params()` to `$this->map_params_to_query_args()`.
* @since 1.0.0-beta.14 Update `prepare_links()` to accept a second parameter, `WP_REST_Request`.
*/
abstract class LLMS_REST_Posts_Controller extends LLMS_REST_Controller {
/**
* Post type.
*
* @var string
*/
protected $post_type;
/**
* Route base.
*
* @var string
*/
protected $collection_route_base_for_pagination;
/**
* Schema properties available for ordering the collection.
*
* @var string[]
*/
protected $orderby_properties = array(
'id',
'title',
'date_created',
'date_updated',
'menu_order',
);
/**
* LLMS post class name.
*
* @since 1.0.0-beta.9
* @var string;
*/
protected $llms_post_class;
/**
* 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(
'force' => array(
'description' => __( 'Bypass the trash and force course deletion.', 'lifterlms' ),
'type' => 'boolean',
'default' => false,
),
);
}
/**
* Retrieves the query params for retrieving a single resource.
*
* @since 1.0.0-beta.1
*
* @return array
*/
public function get_get_item_params() {
$params = parent::get_get_item_params();
$schema = $this->get_item_schema();
if ( isset( $schema['properties']['password'] ) ) {
$params['password'] = array(
'description' => __( 'Post password. Required if the post is password protected.', 'lifterlms' ),
'type' => 'string',
);
}
return $params;
}
/**
* Determine if the current user can view the object.
*
* @since 1.0.0-beta.7
*
* @param object $object Object.
* @return bool
*/
protected function check_read_object_permissions( $object ) {
return $this->check_read_permission( $object );
}
/**
* Check if a given request has access to read items.
*
* @since 1.0.0-beta.1
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
// Everybody can list llms posts (in read mode).
if ( 'edit' === $request['context'] && ! $this->check_update_permission() ) {
return llms_rest_authorization_required_error();
}
return true;
}
/**
* Retrieve pagination information from an objects query.
*
* @since 1.0.0-beta.7
*
* @param obj $query Objects query result.
* @param array $prepared Array of collection arguments.
* @param WP_REST_Request $request Request object.
* @return array {
* Array of pagination information.
*
* @type int $current_page Current page number.
* @type int $total_results Total number of results.
* @type int $total_pages Total number of results pages.
* }
*/
protected function get_pagination_data_from_query( $query, $prepared, $request ) {
$total_results = (int) $query->found_posts;
$current_page = isset( $prepared['paged'] ) ? (int) $prepared['paged'] : 1;
$total_pages = (int) ceil( $total_results / (int) $query->get( 'posts_per_page' ) );
return compact( 'current_page', 'total_results', 'total_pages' );
}
/**
* Check if a given request has access to create an item.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.18 Use plural post type name.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function create_item_permissions_check( $request ) {
$post_type_object = get_post_type_object( $this->post_type );
$post_type_name = $post_type_object->labels->name;
if ( ! empty( $request['id'] ) ) {
// Translators: %s = The post type name.
return llms_rest_bad_request_error( sprintf( __( 'Cannot create existing %s.', 'lifterlms' ), $post_type_name ) );
}
if ( ! $this->check_create_permission() ) {
// Translators: %s = The post type name.
return llms_rest_authorization_required_error( sprintf( __( 'Sorry, you are not allowed to create %s as this user.', 'lifterlms' ), $post_type_name ) );
}
if ( ! $this->check_assign_terms_permission( $request ) ) {
return llms_rest_authorization_required_error( __( 'Sorry, you are not allowed to assign the provided terms.', 'lifterlms' ) );
}
return true;
}
/**
* Creates a single LLMS post.
*
* Extending classes can add additional object fields by overriding the method `update_additional_object_fields()`.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.7 Added `"llms_rest_insert_{$this->post_type}"` and `"llms_rest_insert_{$this->post_type}"` action hooks:
* fired after inserting/uodateing an llms post into the database.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function create_item( $request ) {
$prepared_item = $this->prepare_item_for_database( $request );
if ( is_wp_error( $prepared_item ) ) {
return $prepared_item;
}
$object = $this->create_llms_post( $prepared_item );
if ( is_wp_error( $object ) ) {
if ( 'db_insert_error' === $object->get_error_code() ) {
$object->add_data( array( 'status' => 500 ) );
} else {
$object->add_data( array( 'status' => 400 ) );
}
return $object;
}
$schema = $this->get_item_schema();
/**
* Fires after a single llms post is created or updated via the REST API.
*
* The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
*
* @since 1.0.0-beta.7
*
* @param LLMS_Post $object Inserted or updated llms object.
* @param WP_REST_Request $request Request object.
* @param array $schema The item schema.
* @param bool $creating True when creating a post, false when updating.
*/
do_action( "llms_rest_insert_{$this->post_type}", $object, $request, $schema, true );
// Set all the other properties.
// TODO: maybe we want to filter the post properties that have already been inserted before.
$set_bulk_result = $object->set_bulk( $prepared_item, true );
if ( is_wp_error( $set_bulk_result ) ) {
if ( 'db_update_error' === $set_bulk_result->get_error_code() ) {
$set_bulk_result->add_data( array( 'status' => 500 ) );
} else {
$set_bulk_result->add_data( array( 'status' => 400 ) );
}
return $set_bulk_result;
}
$object_id = $object->get( 'id' );
$additional_fields = $this->update_additional_object_fields( $object, $request, $schema, $prepared_item );
if ( is_wp_error( $additional_fields ) ) {
return $additional_fields;
}
if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) {
$this->handle_featured_media( $request['featured_media'], $object_id );
}
$terms_update = $this->handle_terms( $object_id, $request );
if ( is_wp_error( $terms_update ) ) {
return $terms_update;
}
/**
* TODO: understand how to treat possible conflicting properties => instructors are registered as additional rest field by llms_blocks
*/
// $fields_update = $this->update_additional_fields_for_object( $object, $request );
// if ( is_wp_error( $fields_update ) ) {
// return $fields_update;
// }
$request->set_param( 'context', 'edit' );
/**
* Fires after a single llms post is completely created or updated via the REST API.
*
* The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
*
* @since 1.0.0-beta.7
*
* @param LLMS_Post $object Inserted or updated llms object.
* @param WP_REST_Request $request Request object.
* @param array $schema The item schema.
* @param bool $creating True when creating a post, false when updating.
*/
do_action( "llms_rest_after_insert_{$this->post_type}", $object, $request, $schema, true );
$response = $this->prepare_item_for_response( $object, $request );
$response->set_status( 201 );
$response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $object_id ) ) );
return $response;
}
/**
* Check if a given request has access to read an item.
*
* @since 1.0.0-beta.1
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_item_permissions_check( $request ) {
$object = $this->get_object( (int) $request['id'] );
if ( is_wp_error( $object ) ) {
return $object;
}
if ( 'edit' === $request['context'] && ! $this->check_update_permission( $object ) ) {
return llms_rest_authorization_required_error();
}
if ( ! empty( $request['password'] ) ) {
// Check post password, and return error if invalid.
if ( ! hash_equals( $object->get( 'password' ), $request['password'] ) ) {
return llms_rest_authorization_required_error( __( 'Incorrect password.', 'lifterlms' ) );
}
}
// Allow access to all password protected posts if the context is edit.
if ( 'edit' === $request['context'] ) {
add_filter( 'post_password_required', '__return_false' );
}
if ( ! $this->check_read_permission( $object ) ) {
return llms_rest_authorization_required_error();
}
return true;
}
/**
* Format query arguments to retrieve a collection of objects.
*
* @since 1.0.0-beta.7
* @since 1.0.0-beta.12 Moved parameters to query args mapping into a different method.
* @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 ) {
$prepared = parent::prepare_collection_query_args( $request );
if ( is_wp_error( $prepared ) ) {
return $prepared;
}
// Force the post_type argument, since it's not a user input variable.
$prepared['post_type'] = $this->post_type;
$query_args = $this->prepare_items_query( $prepared, $request );
return $query_args;
}
/**
* Map schema to query arguments to retrieve a collection of objects.
*
* @since 1.0.0-beta.12
*
* @param array $prepared Array of collection arguments.
* @param array $registered Registered collection params.
* @param WP_REST_Request $request Full details about the request.
* @return array|WP_Error
*/
protected function map_params_to_query_args( $prepared, $registered, $request ) {
$args = array();
/*
* This array defines mappings between public API query parameters whose
* values are accepted as-passed, and their internal WP_Query parameter
* name equivalents (some are the same). Only values which are also
* present in $registered will be set.
*/
$parameter_mappings = array(
'order' => 'order',
'orderby' => 'orderby',
'page' => 'paged',
'exclude' => 'post__not_in',
'include' => 'post__in',
'search' => 's',
);
/*
* For each known parameter which is both registered and present in the request,
* set the parameter's value on the query $args.
*/
foreach ( $parameter_mappings as $api_param => $wp_param ) {
if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {
$args[ $wp_param ] = $request[ $api_param ];
}
}
// Ensure our per_page parameter overrides any provided posts_per_page filter.
if ( isset( $registered['per_page'] ) ) {
$args['posts_per_page'] = $request['per_page'];
}
return $args;
}
/**
* Check if a given request has access to update an item.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.18 Use plural post type name.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function update_item_permissions_check( $request ) {
$object = $this->get_object( (int) $request['id'] );
if ( is_wp_error( $object ) ) {
return $object;
}
$post_type_object = get_post_type_object( $this->post_type );
$post_type_name = $post_type_object->labels->name;
if ( ! $this->check_update_permission( $object ) ) {
// Translators: %s = The post type name.
return llms_rest_authorization_required_error( sprintf( __( 'Sorry, you are not allowed to update %s as this user.', 'lifterlms' ), $post_type_name ) );
}
if ( ! $this->check_assign_terms_permission( $request ) ) {
return llms_rest_authorization_required_error( __( 'Sorry, you are not allowed to assign the provided terms.', 'lifterlms' ) );
}
return true;
}
/**
* Updates a single llms post.
*
* Extending classes can add additional object fields by overriding the method `update_additional_object_fields()`.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.7 Don't execute `$object->set_bulk()` when there's no data to update:
* this fixes an issue when updating only properties which are not handled in `prepare_item_for_database()`.
* Added `"llms_rest_insert_{$this->post_type}"` and `"llms_rest_insert_{$this->post_type}"` action hooks:
* fired after inserting/uodateing an llms post into the database.
* @since 1.0.0-beta.11 Fixed `"llms_rest_insert_{$this->post_type}"` and `"llms_rest_insert_{$this->post_type}"` action hooks fourth param:
* must be false when updating.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function update_item( $request ) {
$object = $this->get_object( (int) $request['id'] );
if ( is_wp_error( $object ) ) {
return $object;
}
$prepared_item = $this->prepare_item_for_database( $request );
if ( is_wp_error( $prepared_item ) ) {
return $prepared_item;
}
$update_result = empty( array_diff_key( $prepared_item, array_flip( array( 'id' ) ) ) ) ? false : $object->set_bulk( $prepared_item, true );
if ( is_wp_error( $update_result ) ) {
if ( 'db_update_error' === $update_result->get_error_code() ) {
$update_result->add_data( array( 'status' => 500 ) );
} else {
$update_result->add_data( array( 'status' => 400 ) );
}
return $update_result;
}
$schema = $this->get_item_schema();
/**
* Fires after a single llms post is created or updated via the REST API.
*
* The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
*
* @since 1.0.0-beta.7
*
* @param LLMS_Post $object Inserted or updated llms object.
* @param WP_REST_Request $request Request object.
* @param array $schema The item schema.
* @param bool $creating True when creating a post, false when updating.
*/
do_action( "llms_rest_insert_{$this->post_type}", $object, $request, $schema, false );
$object_id = $object->get( 'id' );
$additional_fields = $this->update_additional_object_fields( $object, $request, $schema, $prepared_item, false );
if ( is_wp_error( $additional_fields ) ) {
return $additional_fields;
}
if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) {
$this->handle_featured_media( $request['featured_media'], $object_id );
}
$terms_update = $this->handle_terms( $object_id, $request );
if ( is_wp_error( $terms_update ) ) {
return $terms_update;
}
/**
* TODO: understand how to treat possible conflicting properties => instructors are registered as additional rest field by llms_blocks
*/
// $fields_update = $this->update_additional_fields_for_object( $object, $request );
// if ( is_wp_error( $fields_update ) ) {
// return $fields_update;
// }
$request->set_param( 'context', 'edit' );
/**
* Fires after a single llms post is completely created or updated via the REST API.
*
* The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
*
* @since 1.0.0-beta.7
*
* @param LLMS_Post $object Inserted or updated llms object.
* @param WP_REST_Request $request Request object.
* @param array $schema The item schema.
* @param bool $creating True when creating a post, false when updating.
*/
do_action( "llms_rest_after_insert_{$this->post_type}", $object, $request, $schema, false );
return $this->prepare_item_for_response( $object, $request );
}
/**
* Updates a single llms post.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.7 return description updated.
*
* @param LLMS_Post_Model $object LMMS_Post_Model instance.
* @param array $prepared_item Array.
* @param WP_REST_Request $request Full details about the request.
* @param array $schema The item schema.
* @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( $object, $prepared_item, $request, $schema ) {
return true;
}
/**
* Check if a given request has access to delete an item.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.18 Provide a more significant error message when trying to delete an item without permissions.
*
* @param WP_REST_Request $request Full details about the request.
* @return bool|WP_Error
*/
public function delete_item_permissions_check( $request ) {
$object = $this->get_object( (int) $request['id'] );
if ( is_wp_error( $object ) ) {
// LLMS_Post not found, we don't return a 404.
if ( in_array( 'llms_rest_not_found', $object->get_error_codes(), true ) ) {
return true;
}
return $object;
}
if ( ! $this->check_delete_permission( $object ) ) {
return llms_rest_authorization_required_error(
sprintf(
// Translators: %s = The post type name.
__( 'Sorry, you are not allowed to delete %s as this user.', 'lifterlms' ),
get_post_type_object( $this->post_type )->labels->name
)
);
}
return true;
}
/**
* Deletes a single llms post.
*
* @since 1.0.0-beta.1
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function delete_item( $request ) {
$object = $this->get_object( (int) $request['id'] );
$response = new WP_REST_Response();
$response->set_status( 204 );
if ( is_wp_error( $object ) ) {
// Course not found, we don't return a 404.
if ( in_array( 'llms_rest_not_found', $object->get_error_codes(), true ) ) {
return $response;
}
return $object;
}
$post_type_object = get_post_type_object( $this->post_type );
$post_type_name = $post_type_object->labels->singular_name;
$id = $object->get( 'id' );
$force = $this->is_delete_forced( $request );
// If we're forcing, then delete permanently.
if ( $force ) {
$result = wp_delete_post( $id, true );
} else {
$supports_trash = $this->is_trash_supported();
// If we don't support trashing for this type, error out.
if ( ! $supports_trash ) {
return new WP_Error(
'llms_rest_trash_not_supported',
/* translators: %1$s: post type name, %2$s: force=true */
sprintf( __( 'The %1$s does not support trashing. Set \'%2$s\' to delete.', 'lifterlms' ), $post_type_name, 'force=true' ),
array( 'status' => 501 )
);
}
// Otherwise, only trash if we haven't already.
if ( 'trash' !== $object->get( 'status' ) ) {
// (Note that internally this falls through to `wp_delete_post` if
// the trash is disabled.)
$result = wp_trash_post( $id );
} else {
$result = true;
}
$request->set_param( 'context', 'edit' );
$object = $this->get_object( $id );
$response = $this->prepare_item_for_response( $object, $request );
}
if ( ! $result ) {
return new WP_Error(
'llms_rest_cannot_delete',
/* translators: %s: post type name */
sprintf( __( 'The %s cannot be deleted.', 'lifterlms' ), $post_type_name ),
array( 'status' => 500 )
);
}
return $response;
}
/**
* 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 isset( $request['force'] ) && (bool) $request['force'];
}
/**
* 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 ( EMPTY_TRASH_DAYS > 0 );
}
/**
* Retrieve a query object based on arguments from a `get_items()` (collection) request.
*
* @since 1.0.0-beta.7
*
* @param array $prepared Array of collection arguments.
* @param WP_REST_Request $request Full details about the request.
* @return WP_Query
*/
protected function get_objects_query( $prepared, $request ) {
return new WP_Query( $prepared );
}
/**
* Retrieve an array of objects from the result of `$this->get_objects_query()`.
*
* @since 1.0.0-beta.7
* @since 1.0.0-beta.9 Avoid performing an additional query, just return the already retrieved posts.
*
* @param WP_Query $query WP_Query query result.
* @return WP_Post[]
*/
protected function get_objects_from_query( $query ) {
return $query->posts;
}
/**
* Prepare collection items for response.
*
* @since 1.0.0-beta.7
*
* @param array $objects Array of objects to be prepared for response.
* @param WP_REST_Request $request Full details about the request.
* @return array
*/
protected function prepare_collection_items_for_response( $objects, $request ) {
$items = array();
// Allow access to all password protected posts if the context is edit.
if ( 'edit' === $request['context'] ) {
add_filter( 'post_password_required', '__return_false' );
}
$items = parent::prepare_collection_items_for_response( $objects, $request );
// Reset filter.
if ( 'edit' === $request['context'] ) {
remove_filter( 'post_password_required', '__return_false' );
}
return $items;
}
/**
* Prepare a single object output for response.
*
* @since 1.0.0-beta.1
*
* @param LLMS_Post_Model $object object object.
* @param WP_REST_Request $request Full details about the request.
* @return array
*/
protected function prepare_object_for_response( $object, $request ) {
$object_id = $object->get( 'id' );
$password_required = post_password_required( $object_id );
$password = $object->get( 'password' );
$data = array(
'id' => $object->get( 'id' ),
'date_created' => $object->get_date( 'date', 'Y-m-d H:i:s' ),
'date_created_gmt' => $object->get_date( 'date_gmt', 'Y-m-d H:i:s' ),
'date_updated' => $object->get_date( 'modified', 'Y-m-d H:i:s' ),
'date_updated_gmt' => $object->get_date( 'modified_gmt', 'Y-m-d H:i:s' ),
'menu_order' => $object->get( 'menu_order' ),
'title' => array(
'raw' => $object->get( 'title', true ),
'rendered' => $object->get( 'title' ),
),
'password' => $password,
'slug' => $object->get( 'name' ),
'post_type' => $this->post_type,
'permalink' => get_permalink( $object_id ),
'status' => $object->get( 'status' ),
'featured_media' => (int) get_post_thumbnail_id( $object_id ),
'comment_status' => $object->get( 'comment_status' ),
'ping_status' => $object->get( 'ping_status' ),
'content' => array(
'raw' => $object->get( 'content', true ),
'rendered' => $password_required ? '' : apply_filters( 'the_content', $object->get( 'content', true ) ),
'protected' => (bool) $password,
),
'excerpt' => array(
'raw' => $object->get( 'excerpt', true ),
'rendered' => $password_required ? '' : apply_filters( 'the_excerpt', $object->get( 'excerpt' ) ),
'protected' => (bool) $password,
),
);
return $data;
}
/**
* Prepare a single item for the REST response
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.14 Pass the `$request` parameter to `prepare_links()`.
*
* @param LLMS_Post_Model $object LLMS post object.
* @param WP_REST_Request $request Request object.
* @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
*/
public function prepare_item_for_response( $object, $request ) {
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
// Need to set the global $post because of references to the global $post when e.g. filtering the content, or processing blocks/shortcodes.
global $post;
$temp = $post;
$post = $object->get( 'post' ); // phpcs:ignore
setup_postdata( $post );
$removed_filters_for_response = $this->maybe_remove_filters_for_response( $object );
$has_password_filter = false;
if ( $this->can_access_password_content( $object, $request ) ) {
// Allow access to the post, permissions already checked before.
add_filter( 'post_password_required', '__return_false' );
$has_password_filter = true;
}
$data = $this->prepare_object_for_response( $object, $request );
if ( $has_password_filter ) {
// Reset filter.
remove_filter( 'post_password_required', '__return_false' );
}
$this->maybe_add_removed_filters_for_response( $removed_filters_for_response );
$post = $temp; // phpcs:ignore
wp_reset_postdata();
// Filter data including only schema props.
$data = array_intersect_key( $data, array_flip( $this->get_fields_for_response( $request ) ) );
// Filter data by context. E.g. in "view" mode the password property won't be allowed.
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $object, $request ) );
return $response;
}
/**
* Determines the allowed query_vars for a get_items() response and prepares
* them for WP_Query.
*
* @since 1.0.0-beta.1
*
* @param array $prepared_args Optional. Prepared WP_Query arguments. Default empty array.
* @param WP_REST_Request $request Optional. Full details about the request.
* @return array Items query arguments.
*/
protected function prepare_items_query( $prepared_args = array(), $request = null ) {
$query_args = array();
foreach ( $prepared_args as $key => $value ) {
$query_args[ $key ] = $value;
}
$query_args = $this->prepare_items_query_orderby_mappings( $query_args, $request );
// Turn exclude and include params into proper arrays.
foreach ( array( 'post__in', 'post__not_in' ) as $arg ) {
if ( isset( $query_args[ $arg ] ) && ! is_array( $query_args[ $arg ] ) ) {
$query_args[ $arg ] = array_map( 'absint', explode( ',', $query_args[ $arg ] ) );
}
}
return $query_args;
}
/**
* Map to proper WP_Query orderby param.
*
* @since 1.0.0-beta.1
*
* @param array $query_args WP_Query arguments.
* @param WP_REST_Request $request Full details about the request.
* @return array Query arguments.
*/
protected function prepare_items_query_orderby_mappings( $query_args, $request ) {
// Map to proper WP_Query orderby param.
if ( isset( $query_args['orderby'] ) && isset( $request['orderby'] ) ) {
$orderby_mappings = array(
'id' => 'ID',
'title' => 'title',
'data_created' => 'post_date',
'date_updated' => 'post_modified',
);
if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
$query_args['orderby'] = $orderby_mappings[ $request['orderby'] ];
}
}
return $query_args;
}
/**
* Prepares a single post for create or update.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.8 Initialize `$prepared_item` array before adding values to it.
*
* @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 = array();
// LLMS Post ID.
if ( isset( $request['id'] ) ) {
$existing_object = $this->get_object( absint( $request['id'] ) );
if ( is_wp_error( $existing_object ) ) {
return $existing_object;
}
$prepared_item['id'] = absint( $request['id'] );
}
$schema = $this->get_item_schema();
// LLMS Post title.
if ( ! empty( $schema['properties']['title'] ) && isset( $request['title'] ) ) {
if ( is_string( $request['title'] ) ) {
$prepared_item['post_title'] = $request['title'];
} elseif ( ! empty( $request['title']['raw'] ) ) {
$prepared_item['post_title'] = $request['title']['raw'];
}
}
// LLMS Post content.
if ( ! empty( $schema['properties']['content'] ) && isset( $request['content'] ) ) {
if ( is_string( $request['content'] ) ) {
$prepared_item['post_content'] = $request['content'];
} elseif ( isset( $request['content']['raw'] ) ) {
$prepared_item['post_content'] = $request['content']['raw'];
}
}
// LLMS Post excerpt.
if ( ! empty( $schema['properties']['excerpt'] ) && isset( $request['excerpt'] ) ) {
if ( is_string( $request['excerpt'] ) ) {
$prepared_item['post_excerpt'] = $request['excerpt'];
} elseif ( isset( $request['excerpt']['raw'] ) ) {
$prepared_item['post_excerpt'] = $request['excerpt']['raw'];
}
}
// LLMS Post status.
if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) ) {
$status = $this->handle_status_param( $request['status'] );
if ( is_wp_error( $status ) ) {
return $status;
}
$prepared_item['post_status'] = $status;
}
// LLMS Post date.
if ( ! empty( $schema['properties']['date_created'] ) && ! empty( $request['date_created'] ) ) {
$date_data = rest_get_date_with_gmt( $request['date_created'] );
if ( ! empty( $date_data ) ) {
list( $prepared_item['post_date'], $prepared_item['post_date_gmt'] ) = $date_data;
$prepared_item['edit_date'] = true;
}
} elseif ( ! empty( $schema['properties']['date_gmt'] ) && ! empty( $request['date_gmt'] ) ) {
$date_data = rest_get_date_with_gmt( $request['date_created_gmt'], true );