class-ld-rest-questions-controller.php
23.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
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
<?php
/**
* LearnDash REST API V2 Quiz Questions Post Controller.
*
* This Controller class is used for the LearnDash Questions (sfwd-question)
* custom post type.
*
* This class extends the LD_REST_Posts_Controller_V2 class.
*
* @since 3.3.0
* @package LearnDash\REST\V2
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( ! class_exists( 'LD_REST_Questions_Controller_V2' ) ) && ( class_exists( 'LD_REST_Posts_Controller_V2' ) ) ) {
/**
* Class LearnDash REST API V2 Quiz Questions Post Controller.
*
* @since 3.3.0
* @uses LD_REST_Posts_Controller_V2
*/
class LD_REST_Questions_Controller_V2 extends LD_REST_Posts_Controller_V2 /* phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound */ {
/**
* Current Post Metaboxes Fields
*
* @var array $fields.
*/
protected $fields = array();
/**
* Metaboxes fields map
*
* @var array
*/
protected $fields_map = array();
/**
* Public constructor for class
*
* @since 3.3.0
*
* @param string $post_type Post type.
*/
public function __construct( $post_type = '' ) {
if ( empty( $post_type ) ) {
$post_type = learndash_get_post_type_slug( 'question' );
}
$this->post_type = $post_type;
$this->metaboxes = array();
parent::__construct( $this->post_type );
/**
* Set the rest_base after the parent __constructor
* as it will set these var with WP specific details.
*/
$this->rest_base = $this->get_rest_base( 'questions' );
}
/**
* Prepare the LearnDash Post Type Settings.
*
* @since 3.3.0
*/
protected function register_fields() {
$this->register_fields_metabox();
do_action( 'learndash_rest_register_fields', $this->post_type, $this );
}
/**
* Register the Settings Fields from the Post Metaboxes.
*
* @since 3.3.0
*/
protected function register_fields_metabox() {
global $learndash_question_types;
$this->fields_map = array(
'_quizId' => 'quiz',
'_answerType' => 'question_type',
'_points' => 'points_total',
'_answerPointsActivated' => 'points_per_answer',
'_showPointsInBox' => 'points_show_in_message',
'_answerPointsDiffModusActivated' => 'points_diff_modus',
'_disableCorrect' => 'disable_correct',
'_correctMsg' => 'correct_message',
'_incorrectMsg' => 'incorrect_message',
'_correctSameText' => 'correct_same',
'_tipEnabled' => 'hints_enabled',
'_tipMsg' => 'hints_message',
'_answer_data' => 'answers',
);
$this->fields = array(
'quiz' => array(
'schema' => array(
'field_key' => 'quiz',
'description' => sprintf(
// translators: placeholder: Quiz.
esc_html_x(
'%s ID.',
'placeholder: Quiz',
'learndash'
),
LearnDash_Custom_Label::get_label( 'quiz' )
),
'type' => 'integer',
'required' => false,
'default' => '',
'context' => array( 'view', 'edit' ),
),
'get_callback' => array( $this, 'get_rest_settings_field_value' ),
'update_callback' => array( $this, 'update_rest_settings_field_value' ),
),
'question_type' => array(
'schema' => array(
'field_key' => 'question_type',
'description' => sprintf(
// translators: placeholder: question.
esc_html_x(
'%s type.',
'placeholder: question',
'learndash'
),
LearnDash_Custom_Label::get_label( 'question' )
),
'type' => 'enum',
'enum' => array_keys( $learndash_question_types ),
'required' => false,
'default' => 'single',
'context' => array( 'view', 'edit' ),
),
'get_callback' => array( $this, 'get_rest_settings_field_value' ),
'update_callback' => array( $this, 'update_rest_settings_field_value' ),
),
'points_total' => array(
'schema' => array(
'field_key' => 'points_total',
'description' => esc_html__( 'Total Points amount', 'learndash' ),
'type' => 'integer',
'required' => false,
'default' => '',
'context' => array( 'view', 'edit' ),
),
'get_callback' => array( $this, 'get_rest_settings_field_value' ),
'update_callback' => array( $this, 'update_rest_settings_field_value' ),
),
'points_per_answer' => array(
'schema' => array(
'field_key' => 'points_per_answer',
'description' => esc_html__( 'Different points for each answer', 'learndash' ),
'type' => 'boolean',
'required' => false,
'default' => '',
'context' => array( 'view', 'edit' ),
),
'get_callback' => array( $this, 'get_rest_settings_field_value' ),
'update_callback' => array( $this, 'update_rest_settings_field_value' ),
),
'points_show_in_message' => array(
'schema' => array(
'field_key' => 'points_show_in_message',
'description' => esc_html__( 'Show reached points in the correct/incorrect message?', 'learndash' ),
'type' => 'boolean',
'required' => false,
'default' => '',
'context' => array( 'view', 'edit' ),
),
'get_callback' => array( $this, 'get_rest_settings_field_value' ),
'update_callback' => array( $this, 'update_rest_settings_field_value' ),
),
'points_diff_modus' => array(
'schema' => array(
'field_key' => 'points_diff_modus',
'description' => esc_html__( 'Different points - modus 2 activate', 'learndash' ),
'type' => 'boolean',
'required' => false,
'default' => '',
'context' => array( 'view', 'edit' ),
),
'get_callback' => array( $this, 'get_rest_settings_field_value' ),
'update_callback' => array( $this, 'update_rest_settings_field_value' ),
),
'disable_correct' => array(
'schema' => array(
'field_key' => 'disable_correct',
'description' => esc_html__( 'Disable answer correct setting.', 'learndash' ),
'type' => 'boolean',
'required' => false,
'default' => '',
'context' => array( 'view', 'edit' ),
),
'get_callback' => array( $this, 'get_rest_settings_field_value' ),
'update_callback' => array( $this, 'update_rest_settings_field_value' ),
),
'correct_message' => array(
'schema' => array(
'field_key' => 'correct_message',
'description' => sprintf(
// translators: placeholder: question.
esc_html_x(
'Message shown when %s is correct.',
'placeholder: question',
'learndash'
),
LearnDash_Custom_Label::get_label( 'question' )
),
'type' => 'object',
'required' => false,
'properties' => array(
'raw' => array(
'description' => 'Content for the object, as it exists in the database.',
'type' => 'string',
'context' => array( 'edit' ),
),
'rendered' => array(
'description' => 'HTML content for the object, transformed for display.',
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
),
'get_callback' => array( $this, 'get_rest_settings_field_value' ),
'update_callback' => array( $this, 'update_rest_settings_field_value' ),
),
'incorrect_message' => array(
'schema' => array(
'field_key' => 'incorrect_message',
'description' => sprintf(
// translators: placeholder: question.
esc_html_x(
'Message shown when %s is correct.',
'placeholder: question',
'learndash'
),
LearnDash_Custom_Label::get_label( 'question' )
),
'type' => 'object',
'required' => false,
'properties' => array(
'raw' => array(
'description' => 'Content for the object, as it exists in the database.',
'type' => 'string',
'context' => array( 'edit' ),
),
'rendered' => array(
'description' => 'HTML content for the object, transformed for display.',
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
),
'get_callback' => array( $this, 'get_rest_settings_field_value' ),
'update_callback' => array( $this, 'update_rest_settings_field_value' ),
),
'correct_same' => array(
'schema' => array(
'field_key' => 'correct_same',
'description' => sprintf(
// translators: placeholder: question.
esc_html_x(
'Activate hint for this %s.',
'placeholder: question',
'learndash'
),
LearnDash_Custom_Label::get_label( 'question' )
),
'type' => 'boolean',
'required' => false,
'default' => '',
'context' => array( 'view', 'edit' ),
),
'get_callback' => array( $this, 'get_rest_settings_field_value' ),
'update_callback' => array( $this, 'update_rest_settings_field_value' ),
),
'hints_enabled' => array(
'schema' => array(
'field_key' => 'hints_enabled',
'description' => sprintf(
// translators: placeholder: question.
esc_html_x(
'Activate hint for this %s.',
'placeholder: question',
'learndash'
),
LearnDash_Custom_Label::get_label( 'question' )
),
'type' => 'boolean',
'required' => false,
'default' => '',
'context' => array( 'view', 'edit' ),
),
'get_callback' => array( $this, 'get_rest_settings_field_value' ),
'update_callback' => array( $this, 'update_rest_settings_field_value' ),
),
'hints_message' => array(
'schema' => array(
'field_key' => 'hints_message',
'description' => esc_html__( 'Hint message.', 'learndash' ),
'type' => 'object',
'required' => false,
'properties' => array(
'raw' => array(
'description' => 'Content for the object, as it exists in the database.',
'type' => 'string',
'context' => array( 'edit' ),
),
'rendered' => array(
'description' => 'HTML content for the object, transformed for display.',
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
),
'get_callback' => array( $this, 'get_rest_settings_field_value' ),
'update_callback' => array( $this, 'update_rest_settings_field_value' ),
),
'answers' => array(
'schema' => array(
'field_key' => 'answers',
'description' => sprintf(
// translators: placeholder: question.
esc_html_x(
'%s answer sets.',
'placeholder: question',
'learndash'
),
LearnDash_Custom_Label::get_label( 'question' )
),
'type' => 'object',
'required' => true,
'context' => array( 'view', 'edit' ),
),
'get_callback' => array( $this, 'get_rest_settings_field_value' ),
'update_callback' => array( $this, 'update_rest_settings_field_value' ),
),
);
foreach ( $this->fields as $field_key => $field_args ) {
register_rest_field(
$this->post_type,
$field_key,
$field_args
);
}
}
/**
* Gets public schema.
*
* @since 3.3.0
*
* @return array
*/
public function get_public_item_schema() {
$schema = parent::get_public_item_schema();
$schema['title'] = 'question';
return $schema;
}
/**
* For LearnDash post type we override the default order/orderby
* to ASC/title instead of the WP default DESC/date.
*
* @since 3.3.0
*
* @param array $query_params Quest params array.
* @param WP_Post_Type $post_type Post type string.
*/
public function rest_collection_params_filter( array $query_params, WP_Post_Type $post_type ) {
$query_params = parent::rest_collection_params_filter( $query_params, $post_type );
if ( ! isset( $query_params['quiz'] ) ) {
$query_params['quiz'] = array(
'description' => sprintf(
// translators: placeholder: quiz.
esc_html_x(
'Limit results to be within a specific %s.',
'placeholder: quiz',
'learndash'
),
LearnDash_Custom_Label::get_label( 'quiz' )
),
'type' => 'integer',
);
}
return $query_params;
}
/**
* Check user permission to get/access Quizzes.
*
* @since 3.3.0
*
* @param object $request WP_REST_Request instance.
* @return bool True is used can get item.
*/
public function get_items_permissions_check( $request ) {
$return = parent::get_items_permissions_check( $request );
if ( ( true === $return ) && ( 'view' === $request['context'] ) ) {
$this->rest_init_request_posts( $request );
// If the archive setting is enabled we allow full listing.
if ( ! $this->rest_post_type_has_archive( $this->post_type ) ) {
if ( is_null( $this->quiz_post ) ) {
return new WP_Error(
'rest_post_invalid_id',
sprintf(
// translators: placeholder: Quiz.
esc_html_x(
'Invalid %s ID',
'placeholder: Quiz',
'learndash'
),
LearnDash_Custom_Label::get_label( 'quiz' )
) . ' ' . __CLASS__,
array( 'status' => 404 )
);
}
if ( ! sfwd_lms_has_access( $this->quiz_post->ID ) ) {
return new WP_Error( 'ld_rest_cannot_view', esc_html__( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) );
}
}
}
return $return;
}
/**
* Filter Topics query args.
*
* @since 3.3.0
*
* @param array $query_args Key value array of query var to query value.
* @param WP_REST_Request $request The request used.
*
* @return array Key value array of query var to query value.
*/
public function rest_query_filter( $query_args, $request ) {
if ( ! $this->is_rest_request( $request ) ) {
return $query_args;
}
$query_args = parent::rest_query_filter( $query_args, $request );
// The course_post should be set in the local method get_items_permissions_check().
if ( ! is_null( $this->quiz_post ) ) {
$questions_ids = array_keys( learndash_get_quiz_questions( $this->quiz_post->ID ) );
if ( ! empty( $questions_ids ) ) {
$query_args['post__in'] = $query_args['post__in'] ? array_intersect( $questions_ids, $query_args['post__in'] ) : $questions_ids;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! isset( $_GET['orderby'] ) ) {
$query_args['orderby'] = 'post__in';
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! isset( $_GET['order'] ) ) {
$query_args['order'] = '';
}
} else {
$query_args['post__in'] = array( 0 );
}
}
return $query_args;
}
/**
* Delete one item from the collection.
*
* @since 3.3.0
*
* @param WP_REST_Request $request Full data about the request.
*
* @return WP_Error|WP_REST_Request
*/
public function delete_item( $request ) {
$params = $request->get_params();
$question_id = $params['id'];
if ( ! empty( $question_id ) ) {
$question_pro_id = (int) get_post_meta( $question_id, 'question_pro_id', true );
$question_mapper = new \WpProQuiz_Model_QuestionMapper();
if ( false !== $question_mapper->delete( $question_pro_id ) &&
false !== wp_delete_post( $params['id'], false ) ) {
return new WP_REST_Response( true, 200 );
}
}
return new WP_Error(
'cant-delete',
sprintf(
// translators: placeholder: Question label.
esc_html_x( 'Could not delete the %s.', 'placeholder: Question label', 'learndash' ),
\LearnDash_Custom_Label::get_label( 'question' )
),
array( 'status' => 500 )
);
}
/**
* Update one item from the collection
*
* @since 3.3.0
*
* @param WP_REST_Request $request Full data about the request.
*
* @return WP_Error|WP_REST_Request
*/
public function update_item( $request ) {
$params = $request->get_params();
$question_id = $params['id'];
if ( ! empty( $question_id ) ) {
$question_pro_id = (int) get_post_meta( $question_id, 'question_pro_id', true );
$question_mapper = new \WpProQuiz_Model_QuestionMapper();
$question_model = $question_mapper->fetch( $question_pro_id );
// Update answer data if available.
if ( isset( $params['_answerData'] ) && is_string( $params['_answerData'] ) ) {
$params['_answerData'] = json_decode( $params['_answerData'], true );
}
// Also save points at question's post meta data.
if ( isset( $params['_points'] ) ) {
update_post_meta( $question_id, 'question_points', $params['_points'] );
}
// Update question's post content.
if ( isset( $params['_question'] ) ) {
wp_update_post(
array(
'ID' => $question_id,
'post_content' => wp_slash( $params['_question'] ),
)
);
}
// Update the question object with new data.
$question_model->set_array_to_object( $params );
// Save the new data to database.
$question_mapper->save( $question_model );
return new WP_REST_Response( $this->get_question_data( $question_id ), 200 );
}
return new WP_Error(
'cant-delete',
sprintf(
// translators: placeholder: Question.
esc_html_x( 'Could not update the %s.', 'placeholder: Question', 'learndash' ),
\LearnDash_Custom_Label::get_label( 'question' )
),
array( 'status' => 500 )
);
}
/**
* Get question data.
*
* @since 3.3.0
*
* @param int $question_id The question ID.
*
* @return object
*/
public function get_question_data( $question_id = 0 ) {
$data = array();
if ( ! empty( $question_id ) ) {
// Get Answers from Question.
$question_pro_id = (int) get_post_meta( $question_id, 'question_pro_id', true );
$question_mapper = new \WpProQuiz_Model_QuestionMapper();
if ( ! empty( $question_pro_id ) ) {
$question_model = $question_mapper->fetch( $question_pro_id );
} else {
$question_model = $question_mapper->fetch( null );
}
// Get data as array.
$question_data = $question_model->get_object_as_array();
$answer_data = array();
// Get answer data.
foreach ( $question_data['_answerData'] as $answer ) {
$answer_data[] = $answer->get_object_as_array();
}
unset( $question_data['_answerData'] );
$question_data['_answerData'] = $answer_data;
// Generate output object.
$data = array_merge(
$question_data,
array(
'question_id' => $question_id,
'question_post_title' => get_the_title( $question_id ),
)
);
}
return $data;
}
/**
* Get REST Setting Field value.
*
* @since 3.3.0
*
* @param array $postdata Post data array.
* @param string $field_name Field Name for $postdata value.
* @param WP_REST_Request $request Request object.
* @param string $post_type Post Type for request.
*/
public function get_rest_settings_field_value( array $postdata, $field_name, WP_REST_Request $request, $post_type ) {
static $question_pro_data = array();
$return = null;
$field_map_idx = array_search( $field_name, $this->fields_map, true );
if ( false !== $field_map_idx ) {
$question_post_id = 0;
if ( ( isset( $postdata['id'] ) ) && ( ! empty( $postdata['id'] ) ) ) {
$question_post_id = absint( $postdata['id'] );
}
if ( ! empty( $question_post_id ) ) {
if ( isset( $question_pro_data[ $question_post_id ] ) ) {
$question_data = $question_pro_data[ $question_post_id ];
} else {
$question_data = array();
$quiz_pro_id = get_post_meta( $question_post_id, 'question_pro_id', true );
$quiz_pro_id = absint( $quiz_pro_id );
if ( ! empty( $quiz_pro_id ) ) {
$question_mapper = new \WpProQuiz_Model_QuestionMapper();
$question_model = $question_mapper->fetch( $quiz_pro_id );
$question_data = $question_model->get_object_as_array();
$question_pro_data[ $question_post_id ] = $question_data;
}
}
switch ( $field_name ) {
case 'quiz':
$return = get_post_meta( $question_post_id, 'quiz_id', true );
$return = absint( $return );
break;
case 'correct_message':
case 'incorrect_message':
case 'hints_message':
$return = array(
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
'rendered' => apply_filters( 'the_content', $question_data[ $field_map_idx ] ),
);
// If the context is 'edit' we provide the raw content.
if ( ( 'edit' === $request['context'] ) ) {
$return['raw'] = $question_data[ $field_map_idx ];
}
break;
case 'points_per_answer':
case 'points_show_in_message':
case 'points_diff_modus':
case 'disable_correct':
case 'correct_same':
case 'hints_enabled':
$return = (bool) $question_data[ $field_map_idx ];
break;
case 'answers':
break;
default:
if ( isset( $question_data[ $field_map_idx ] ) ) {
$return = $question_data[ $field_map_idx ];
}
break;
}
}
}
return $return;
}
/**
* Override the REST response links.
*
* @since 3.3.0
*
* @param WP_REST_Response $response WP_REST_Response instance.
* @param WP_Post $post WP_Post instance.
* @param WP_REST_Request $request WP_REST_Request instance.
*/
public function rest_prepare_response_filter( WP_REST_Response $response, WP_Post $post, WP_REST_Request $request ) {
if ( $this->post_type === $post->post_type ) {
$base = sprintf( '/%s/%s', $this->namespace, $this->rest_base );
$request_route = $request->get_route();
if ( ( ! empty( $request_route ) ) && ( strpos( $request_route, $base ) !== false ) ) {
$links = array();
$current_links = $response->get_links();
if ( ! isset( $current_links[ $this->get_rest_base( 'question-types' ) ] ) ) {
$quiz_pro_id = get_post_meta( $post->ID, 'question_pro_id', true );
$quiz_pro_id = absint( $quiz_pro_id );
if ( ! empty( $quiz_pro_id ) ) {
$question_mapper = new \WpProQuiz_Model_QuestionMapper();
$question_model = $question_mapper->fetch( $quiz_pro_id );
$question_answer_type = $question_model->getAnswerType();
if ( ! empty( $question_answer_type ) ) {
$links[ $this->get_rest_base( 'question-types' ) ] = array(
'href' => rest_url( trailingslashit( $this->namespace ) . $this->get_rest_base( 'question-types' ) . '/' . $question_answer_type ),
'embeddable' => true,
);
}
}
}
if ( ! empty( $links ) ) {
$response->add_links( $links );
}
}
}
return $response;
}
// End of functions.
}
}