-
Notifications
You must be signed in to change notification settings - Fork 199
/
includes_class-sensei-modules.php.html
2876 lines (2441 loc) · 116 KB
/
includes_class-sensei-modules.php.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Source: includes/class-sensei-modules.php - Sensei LMS Hook Docs</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
<link href="https://fonts.googleapis.com/css?family=IBM+Plex+Mono|IBM+Plex+Sans:300,400|Playfair+Display:900&display=swap" rel="stylesheet">
<link type="text/css" rel="stylesheet" href="styles.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: includes/class-sensei-modules.php</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code><?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Sensei Modules Class
*
* Sensei Module Functionality
*
* @package Content
* @author Automattic
*
* @since 1.8.0
*/
class Sensei_Core_Modules {
private $file;
private $order_page_slug;
public $taxonomy;
public function __construct( $file ) {
$this->file = $file;
$this->taxonomy = 'module';
$this->order_page_slug = 'module-order';
// setup taxonomy
add_action( 'init', array( $this, 'setup_modules_taxonomy' ), 10 );
// Manage lesson meta boxes for taxonomy
add_action( 'add_meta_boxes', array( $this, 'modules_metaboxes' ), 20, 2 );
// Save lesson meta box
add_action( 'save_post', array( $this, 'save_lesson_module' ), 10, 1 );
// Frontend styling
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
// Admin styling
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ), 20, 2 );
// Handle module completion record
add_action( 'sensei_lesson_status_updated', array( $this, 'update_lesson_status_module_progress' ), 10, 3 );
add_action( 'sensei_user_lesson_reset', array( $this, 'save_lesson_module_progress' ), 10, 2 );
add_action( 'wp', array( $this, 'save_module_progress' ), 10 );
add_action( 'admin_menu', array( $this, 'add_submenus' ) );
add_action( 'admin_post_order_modules', array( $this, 'handle_order_modules' ) );
add_filter( 'manage_course_posts_columns', array( $this, 'course_columns' ), 11, 1 );
add_action( 'manage_course_posts_custom_column', array( $this, 'course_column_content' ), 11, 2 );
add_filter( 'manage_lesson_posts_columns', array( $this, 'add_lesson_columns' ), 11, 1 );
add_action( 'manage_lesson_posts_custom_column', array( $this, 'add_lesson_column_content' ), 11, 2 );
// Ensure modules always show under courses
add_action( 'admin_menu', array( $this, 'remove_lessons_menu_model_taxonomy' ), 10 );
add_action( 'admin_menu', array( $this, 'remove_courses_menu_model_taxonomy' ), 10 );
add_action( 'admin_menu', array( $this, 'redirect_to_lesson_module_taxonomy_to_course' ), 20 );
// Add course field to taxonomy
add_action( $this->taxonomy . '_add_form_fields', array( $this, 'add_module_fields' ), 50, 1 );
add_action( $this->taxonomy . '_edit_form_fields', array( $this, 'edit_module_fields' ), 1, 1 );
add_action( 'created_' . $this->taxonomy, array( $this, 'track_module_creation' ), 10 );
add_action( 'admin_init', array( $this, 'add_module_admin_hooks' ) );
add_action( 'wp_ajax_sensei_json_search_courses', array( $this, 'search_courses_json' ) );
// Manage module taxonomy archive page
add_filter( 'template_include', array( $this, 'module_archive_template' ), 10 );
add_action( 'pre_get_posts', array( $this, 'module_archive_filter' ), 10, 1 );
add_filter( 'sensei_lessons_archive_text', array( $this, 'module_archive_title' ) );
add_action( 'sensei_loop_lesson_inside_before', array( $this, 'module_archive_description' ), 30 );
add_action( 'sensei_taxonomy_module_content_inside_before', array( $this, 'course_signup_link' ), 30 );
add_action( 'sensei_taxonomy_module_content_inside_before', array( $this, 'module_archive_description' ), 30 );
add_filter( 'body_class', array( $this, 'module_archive_body_class' ) );
// Single Course modules actions. Add to single-course/course-modules.php
add_action( 'sensei_single_course_modules_before', array( $this, 'course_modules_title' ), 20 );
// Set up display on single lesson page
add_filter( 'sensei_breadcrumb_output', array( $this, 'module_breadcrumb_link' ), 10, 2 );
// Add 'Modules' columns to Analysis tables
add_filter( 'sensei_analysis_overview_columns', array( $this, 'analysis_overview_column_title' ), 10, 2 );
add_filter( 'sensei_analysis_course_columns', array( $this, 'analysis_course_column_title' ), 10, 2 );
add_filter( 'sensei_analysis_course_column_data', array( $this, 'analysis_course_column_data' ), 10, 3 );
// Manage module taxonomy columns
add_filter( 'manage_edit-' . $this->taxonomy . '_columns', array( $this, 'taxonomy_column_headings' ), 1, 1 );
add_filter( 'manage_' . $this->taxonomy . '_custom_column', array( $this, 'taxonomy_column_content' ), 1, 3 );
add_filter( 'sensei_module_lesson_list_title', array( $this, 'sensei_course_preview_titles' ), 10, 2 );
// store new modules created on the course edit screen
add_action( 'wp_ajax_sensei_add_new_module_term', array( 'Sensei_Core_Modules', 'add_new_module_term' ) );
add_action( 'wp_ajax_sensei_get_course_modules', array( $this, 'ajax_get_course_modules' ) );
add_action( 'wp_ajax_sensei_get_lesson_module_metabox', array( $this, 'handle_get_lesson_module_metabox' ) );
// for non admin users, only show taxonomies that belong to them
add_filter( 'get_terms', array( $this, 'filter_module_terms' ), 20, 3 );
// add the teacher name next to the module term in for admin users
add_filter( 'get_terms', array( $this, 'append_teacher_name_to_module' ), 70, 3 );
add_filter( 'get_object_terms', array( $this, 'filter_course_selected_terms' ), 20, 3 );
// remove the default modules metabox
add_action( 'admin_init', array( 'Sensei_Core_Modules', 'remove_default_modules_box' ) );
// Add custom navigation.
add_action( 'in_admin_header', [ $this, 'add_custom_navigation' ] );
// Update module teacher meta when added to course.
add_action( 'added_term_relationship', [ $this, 'add_teacher_id_in_module_meta_when_added_to_course' ], 10, 3 );
// Remove module teacher meta when removed from course.
add_action( 'delete_term_relationships', [ $this, 'remove_teacher_id_from_module_meta_when_removed_from_course' ], 10, 3 );
// Update module teacher meta on course teacher update.
add_action( 'post_updated', [ $this, 'update_module_teacher_id_meta_on_post_teacher_update' ], 10, 3 );
}
/**
* Add teacher id as term meta when a module is added to a course.
*
* @since 4.9.0
* @access private
*
* @param int $post_ID Post ID.
* @param WP_Post $post_after Post object following the update.
* @param WP_Post $post_before Post object before the update.
*/
public function update_module_teacher_id_meta_on_post_teacher_update( int $post_ID, WP_Post $post_after, WP_Post $post_before ) {
if ( 'course' !== get_post( $post_ID )->post_type ) {
return;
}
if ( $post_after->post_author !== $post_before->post_author ) {
$modules = Sensei()->modules->get_course_modules( $post_ID );
foreach ( $modules as $module ) {
self::update_module_teacher_meta( $module->term_id, $post_after->post_author );
}
}
}
/**
* Add teacher id as term meta when a module is added to a course.
*
* @since 4.9.0
* @access private
*
* @param int $object_id Object ID.
* @param int $tt_id Term taxonomy ID.
* @param string $taxonomy Taxonomy slug.
*/
public function add_teacher_id_in_module_meta_when_added_to_course( int $object_id, int $tt_id, string $taxonomy ) {
if ( 'module' !== $taxonomy ) {
return;
}
$course = get_post( $object_id );
self::update_module_teacher_meta( $tt_id, $course->post_author );
}
/**
* Remove teacher id from term meta when a module is added to a course.
*
* @since 4.9.0
* @access private
*
* @param int $object_id Object ID.
* @param array $tt_ids An array of term taxonomy IDs.
* @param string $taxonomy Taxonomy slug.
*/
public function remove_teacher_id_from_module_meta_when_removed_from_course( int $object_id, array $tt_ids, string $taxonomy ) {
if ( 'module' !== $taxonomy ) {
return;
}
foreach ( $tt_ids as $tt_id ) {
$args = array(
'post_type' => 'course',
'post_status' => array( 'publish', 'draft', 'future', 'private' ),
'posts_per_page' => -1,
'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
array(
'taxonomy' => $this->taxonomy,
'field' => 'id',
'terms' => $tt_id,
),
),
);
$courses = get_posts( $args );
// Don't remove teacher id if this module is still being used in other courses.
if ( count( $courses ) < 2 ) {
delete_term_meta( $tt_id, 'module_author' );
}
}
}
/**
* Highlight the menu item for the modules pages.
*
* @deprecated 4.8.0
*
* @since 4.0.0
* @access private
*
* @param string $submenu_file The submenu file points to the certain item of the submenu.
*
* @return string
*/
public function highlight_menu_item( $submenu_file ) {
_deprecated_function( __METHOD__, '4.8.0' );
$screen = get_current_screen();
if ( $screen && in_array( $screen->id, [ 'edit-module', 'course_page_module-order' ], true ) ) {
$submenu_file = 'edit-tags.php?taxonomy=module&post_type=course';
}
return $submenu_file;
}
/**
* Add custom navigation to the admin pages.
*
* @since 4.0.0
* @access private
*/
public function add_custom_navigation() {
$screen = get_current_screen();
if ( ! $screen ) {
return;
}
if ( ( 'edit-module' === $screen->id ) && ( 'term' !== $screen->base ) ) {
$this->display_modules_navigation( $screen );
}
}
/**
* Display the modules' navigation.
*
* @param WP_Screen $screen WordPress current screen object.
*/
private function display_modules_navigation( WP_Screen $screen ) {
?>
<div id="sensei-custom-navigation" class="sensei-custom-navigation">
<div class="sensei-custom-navigation__heading">
<div class="sensei-custom-navigation__title">
<h1><?php esc_html_e( 'Modules', 'sensei-lms' ); ?></h1>
</div>
<div class="sensei-custom-navigation__links">
<a href="<?php echo esc_url( admin_url( 'admin.php?page=module-order' ) ); ?>"><?php esc_html_e( 'Order Modules', 'sensei-lms' ); ?></a>
</div>
</div>
</div>
<?php
}
/**
* Hook in all meta boxes related tot he modules taxonomy
*
* @since 1.8.0
*
* @param string $post_type
* @param WP_Post $post
*
* @return void
*/
public function modules_metaboxes( $post_type, $post ) {
if ( 'lesson' == $post_type ) {
// Remove default taxonomy meta box from Lesson edit screen
remove_meta_box( $this->taxonomy . 'div', 'lesson', 'side' );
// Add custom meta box to limit module selection to one per lesson
add_meta_box( $this->taxonomy . '_select', __( 'Module', 'sensei-lms' ), array( $this, 'lesson_module_metabox' ), 'lesson', 'side', 'default' );
}
if ( 'course' == $post_type ) {
// Course modules selection metabox
add_meta_box( $this->taxonomy . '_course_mb', __( 'Course Modules', 'sensei-lms' ), array( $this, 'course_module_metabox' ), 'course', 'side', 'core' );
}
}
/**
* Build content for custom module meta box
*
* @since 1.8.0
* @param WP_Post $post Current post object.
* @return void
*/
public function lesson_module_metabox( $post ) {
$course_id = (int) get_post_meta( $post->ID, '_lesson_course', true );
$this->output_lesson_module_metabox( $post, $course_id );
}
/**
* Outputs the lesson module meta box HTML.
*
* @since 3.15.0
*
* @param WP_Post $lesson_post The lesson post object.
* @param int $course_id The course id.
*/
private function output_lesson_module_metabox( WP_Post $lesson_post, int $course_id ) {
// Get current lesson module.
$module_id = $course_id ? $this->get_lesson_module_if_exists( $lesson_post ) : null;
$html = '<div id="lesson-module-metabox-select">';
$html .= $this->render_lesson_module_select_for_course( $course_id, $module_id );
$html .= '</div>';
echo wp_kses(
$html,
array_merge(
wp_kses_allowed_html( 'post' ),
array(
'input' => array(
'id' => array(),
'name' => array(),
'type' => array(),
'value' => array(),
),
'option' => array(
'selected' => array(),
'value' => array(),
),
'select' => array(
'class' => array(),
'id' => array(),
'name' => array(),
'style' => array(),
),
)
)
);
}
/**
* Get the lesson module if it Exists. Defaults to 0 if none found.
*
* @param WP_Post $post The post.
* @return int
*/
public function get_lesson_module_if_exists( $post ) {
// Get existing lesson module.
$lesson_module = 0;
$lesson_module_list = wp_get_post_terms( $post->ID, $this->taxonomy );
if ( is_array( $lesson_module_list ) && count( $lesson_module_list ) > 0 ) {
foreach ( $lesson_module_list as $single_module ) {
$lesson_module = $single_module->term_id;
break;
}
}
return $lesson_module;
}
/**
* Renders the lesson module select input.
*
* @since 3.15.0
*
* @param int|null $course_id The course post ID.
* @param int|null $current_module_id The currently selected module post ID.
*
* @return string The lesson module select HTML.
*/
private function render_lesson_module_select_for_course( int $course_id = null, int $current_module_id = null ): string {
// Get the available modules for this lesson's course.
$modules = $course_id ? $this->get_course_modules( $course_id ) : [];
// Build the HTML.
$input_name = 'lesson_module';
$html = '';
$html .= '<input type="hidden" name="' . esc_attr( 'woo_lesson_' . $this->taxonomy . '_nonce' ) . '" id="' . esc_attr( 'woo_lesson_' . $this->taxonomy . '_nonce' ) . '" value="' . esc_attr( wp_create_nonce( plugin_basename( $this->file ) ) ) . '" />';
if ( $modules ) {
$html .= '<select id="lesson-module-options" name="' . esc_attr( $input_name ) . '" class="widefat" style="width: 100%">' . "\n";
$html .= '<option value="">' . esc_html__( 'None', 'sensei-lms' ) . '</option>';
foreach ( $modules as $module ) {
$html .= '<option value="' . esc_attr( absint( $module->term_id ) ) . '"' . selected( $module->term_id, $current_module_id, false ) . '>' . esc_html( $module->name ) . '</option>' . "\n";
}
$html .= '</select>' . "\n";
} else {
$html .= '<input type="hidden" name="' . esc_attr( $input_name ) . '" value="">';
if ( $course_id ) {
$course_url = admin_url( 'post.php?post=' . $course_id . '&action=edit' );
/*
* translators: The placeholders are as follows:
*
* %1$s - <em>
* %2$s - </em>
* %3$s - Opening <a> tag to link to the Course URL.
* %4$s - </a>
*/
$html .= '<p>' . wp_kses_post( sprintf( __( 'No modules are available for this lesson yet. %1$sPlease add some to %3$sthe course%4$s.%2$s', 'sensei-lms' ), '<em>', '</em>', '<a href="' . esc_url( $course_url ) . '">', '</a>' ) ) . '</p>';
} else {
/*
* translators: The placeholders are as follows:
*
* %1$s - <em>
* %2$s - </em>
*/
$html .= '<p>' . sprintf( __( 'No modules are available for this lesson yet. %1$sPlease select a course first.%2$s', 'sensei-lms' ), '<em>', '</em>' ) . '</p>';
}
}
return $html;
}
/**
* Delete a term if it is childless and not associated with a lesson or course.
*
* @param int $module_term_id Term ID for the module.
*/
public function remove_if_unused( $module_term_id ) {
if ( ! $this->is_term_used( $module_term_id ) ) {
wp_delete_term( $module_term_id, 'module' );
}
}
/**
* Check if term either has children or is associated with a lesson or course.
*
* @param int $module_term_id Term ID for the module.
* @return bool True if term is has children or is associated with a lesson or course.
*/
public function is_term_used( $module_term_id ) {
$term_children = get_term_children( $module_term_id, 'module' );
if ( ! is_wp_error( $term_children ) && ! empty( $term_children ) ) {
return true;
}
$post_query = new WP_Query(
array(
'post_type' => array( 'lesson', 'course' ),
'tax_query' => array(
array(
'taxonomy' => 'module',
'field' => 'id',
'terms' => intval( $module_term_id ),
),
),
'fields' => 'ids',
'posts_per_page' => 1,
)
);
if ( $post_query->found_posts > 0 ) {
return true;
}
return false;
}
/**
* Save module to lesson. This method checks for authorization, and checks
* the incoming nonce.
*
* @since 1.8.0
* @param integer $post_id ID of post
* @return mixed Post ID on permissions failure, boolean true on success
*/
public function save_lesson_module( $post_id ) {
$post = get_post( $post_id );
// Verify post type and nonce
if ( ( get_post_type( $post ) != 'lesson' ) || ! isset( $_POST[ 'woo_lesson_' . $this->taxonomy . '_nonce' ] )
|| ! wp_verify_nonce( $_POST[ 'woo_lesson_' . $this->taxonomy . '_nonce' ], plugin_basename( $this->file ) ) ) {
return $post_id;
}
// Check if user has permissions to edit lessons
$post_type = get_post_type_object( $post->post_type );
if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) ) {
return $post_id;
}
// Check if user has permissions to edit this specific post
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
// Get module and course IDs
$lesson_module_id_key = 'lesson_module';
$lesson_course_id_key = 'lesson_course';
$module_id = isset( $_POST[ $lesson_module_id_key ] ) ? $_POST[ $lesson_module_id_key ] : null;
$course_id = isset( $_POST[ $lesson_course_id_key ] ) ? $_POST[ $lesson_course_id_key ] : null;
// Set the module on the lesson
$lesson_modules = new Sensei_Core_Lesson_Modules( $post_id );
$lesson_modules->set_module( $module_id, $course_id );
return true;
}
/**
* Display course field on new module screen
*
* @since 1.8.0
* @param object $taxonomy Taxonomy object
* @return void
*/
public function add_module_fields( $taxonomy ) {
?>
<div class="form-field">
<?php $this->render_module_course_multi_select(); ?>
</div>
<input type="hidden" name="from_page" value="module">
<?php
}
/**
* Render the Course Multi-Select (used by select2)
*
* @param array $module_courses The Module courses.
* @since 1.9.15
* @return void
*/
private function render_module_course_multi_select( $module_courses = array() ) {
?>
<label for="module_courses"><?php echo esc_html__( 'Course(s)', 'sensei-lms' ); ?></label>
<select name="module_courses[]"
id="module_courses"
class="ajax_chosen_select_courses"
multiple="multiple"
data-placeholder="<?php echo esc_attr__( 'Search for courses...', 'sensei-lms' ); ?>"
>
<?php foreach ( $module_courses as $module_course ) { ?>
<option value="<?php echo esc_attr( $module_course['id'] ); ?>" selected="selected">
<?php echo esc_html( $module_course['details'] ); ?>
</option>
<?php } ?>
</select>
<span
class="description"><?php echo esc_html__( 'Search for and select the courses that this module will belong to.', 'sensei-lms' ); ?>
</span>
<?php
}
/**
* Adds hooks for use with editing a taxonomy in WP Admin.
*
* @since 3.6.0
* @access private
*/
public function add_module_admin_hooks() {
add_action( 'edited_' . $this->taxonomy, array( $this, 'save_module_course' ), 10, 2 );
add_action( 'created_' . $this->taxonomy, array( $this, 'save_module_course' ), 10, 2 );
}
/**
* Display course field on module edit screen
*
* @since 1.8.0
* @param object $module Module term object
* @return void
*/
public function edit_module_fields( $module ) {
$module_id = $module->term_id;
// Get module's existing courses
$args = array(
'post_type' => 'course',
'post_status' => array( 'publish', 'draft', 'future', 'private' ),
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $this->taxonomy,
'field' => 'id',
'terms' => $module_id,
),
),
);
$courses = get_posts( $args );
// build the defaults array
$module_courses = array();
if ( isset( $courses ) && is_array( $courses ) ) {
foreach ( $courses as $course ) {
$module_courses[] = array(
'id' => $course->ID,
'details' => $course->post_title,
);
}
}
?>
<tr class="form-field">
<th scope="row" valign="top"><label
for="module_courses"><?php esc_html_e( 'Course(s)', 'sensei-lms' ); ?></label></th>
<td>
<?php $this->render_module_course_multi_select( $module_courses ); ?>
</td>
</tr>
<?php
}
/**
* Save module course on add/edit
*
* @since 1.8.0
* @param int $module_id ID of module.
* @return void
*/
public function save_module_course( $module_id ) {
/*
* It is safe to ignore nonce verification here because this is called
* from `edited_{$taxonomy}` and `created_{$taxonomy}` on a post to
* `edit-tags.php`, which occur after WordPress performs its own nonce
* verification.
*/
$is_rest_request = defined( 'REST_REQUEST' ) && REST_REQUEST;
// phpcs:ignore WordPress.Security.NonceVerification
if ( $is_rest_request || ( isset( $_POST['action'] ) && 'inline-save-tax' == $_POST['action'] ) ) {
return;
}
// Get module's existing courses
$args = array(
'post_type' => 'course',
'post_status' => array( 'publish', 'draft', 'future', 'private' ),
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $this->taxonomy,
'field' => 'id',
'terms' => $module_id,
),
),
);
$courses = get_posts( $args );
// Remove module from existing courses
if ( isset( $courses ) && is_array( $courses ) ) {
foreach ( $courses as $course ) {
wp_remove_object_terms( $course->ID, (int) $module_id, $this->taxonomy );
}
}
// Add module to selected courses
// phpcs:ignore WordPress.Security.NonceVerification
if ( isset( $_POST['module_courses'] ) && ! empty( $_POST['module_courses'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification
$course_ids = is_array( $_POST['module_courses'] ) ? $_POST['module_courses'] : explode( ',', $_POST['module_courses'] );
foreach ( $course_ids as $course_id ) {
wp_set_object_terms( absint( $course_id ), $module_id, $this->taxonomy, true );
}
}
}
/**
* Track module creation.
*
* @since 2.1.0
* @access private
*
* @param int $module_id ID of module.
*/
public function track_module_creation( $module_id ) {
$module = get_term( $module_id );
$event_properties = [
// phpcs:ignore WordPress.Security.NonceVerification
'page' => isset( $_REQUEST['from_page'] ) ? $_REQUEST['from_page'] : '',
'parent_id' => -1,
];
if ( $module->parent ) {
$event_properties['parent_id'] = $module->parent;
}
sensei_log_event( 'module_add', $event_properties );
}
/**
* Ajax function to search for courses matching term
*
* @since 1.8.0
* @return void
*/
public function search_courses_json() {
// Security check
check_ajax_referer( 'search-courses', 'security' );
// Set content type
header( 'Content-Type: application/json; charset=utf-8' );
// Get user input
$term = urldecode( stripslashes( $_GET['term'] ) );
// Return nothing if term is empty
if ( empty( $term ) ) {
die();
}
// Set a default if none is given
$default = isset( $_GET['default'] ) ? $_GET['default'] : __( 'No course', 'sensei-lms' );
// Set up array of results
$found_courses = array( '' => $default );
// Fetch results
$args = array(
'post_type' => 'course',
'post_status' => array( 'publish', 'draft', 'future', 'private' ),
'posts_per_page' => -1,
'orderby' => 'title',
's' => $term,
);
$courses = get_posts( $args );
// Add results to array
if ( $courses ) {
foreach ( $courses as $course ) {
$found_courses[ $course->ID ] = $course->post_title;
}
}
// Encode and return results for processing & selection
echo json_encode( $found_courses );
die();
}
public function sensei_course_preview_titles( $title, $lesson_id ) {
global $post, $current_user;
$course_id = $post->ID;
$title_text = '';
if ( method_exists( 'Sensei_Utils', 'is_preview_lesson' ) && Sensei_Utils::is_preview_lesson( $lesson_id ) ) {
$is_user_taking_course = Sensei()->course_progress_repository->has( $course_id, $current_user->ID );
if ( ! $is_user_taking_course ) {
if ( method_exists( 'Sensei_Frontend', 'sensei_lesson_preview_title_text' ) ) {
$title_text = Sensei()->frontend->sensei_lesson_preview_title_text( $course_id );
// Remove brackets for display here
$title_text = str_replace( '(', '', $title_text );
$title_text = str_replace( ')', '', $title_text );
$title_text = '<span class="preview-label">' . $title_text . '</span>';
}
$title .= ' ' . $title_text;
}
}
return $title;
}
public function module_breadcrumb_link( $html, $separator ) {
global $post;
// Lesson
if ( is_singular( 'lesson' ) ) {
if ( has_term( '', $this->taxonomy, $post->ID ) ) {
$module = $this->get_lesson_module( $post->ID );
if ( $module ) {
if ( $this->do_link_to_module( $module ) ) {
$html .= ' ' . $separator . ' <a href="' . esc_url( $module->url ) . '" title="' . __( 'Back to the module', 'sensei-lms' ) . '">' . $module->name . '</a>';
} else {
$html .= ' ' . $separator . ' ' . $module->name;
}
}
}
}
// Module
if ( is_tax( $this->taxonomy ) ) {
if ( isset( $_GET['course_id'] ) && 0 < intval( $_GET['course_id'] ) ) {
$course_id = intval( $_GET['course_id'] );
$html .= '<a href="' . esc_url( get_permalink( $course_id ) ) . '" title="' . __( 'Back to the course', 'sensei-lms' ) . '">' . get_the_title( $course_id ) . '</a>';
}
}
return $html;
}
/**
* Check if we should link to a module in the course outline.
*
* True if there is a module description or if the `taxonomy-module.php` template has been overridden.
*
* @since 1.10.0
*
* @param WP_Term $module
* @param bool $link_to_current Set to true to disable checks for currently displayed module. Default false.
* @return bool
*/
public function do_link_to_module( WP_Term $module, $link_to_current = false ) {
// Perhaps don't link to module when on the module page already.
if ( ! $link_to_current && is_tax( 'module', $module->term_id ) ) {
$do_link_to_module = false;
} else {
$description = trim( $module->description );
if ( ! empty( $description ) ) {
$do_link_to_module = true;
} else {
$do_link_to_module = $this->is_module_tax_template_overridden();
}
}
/**
* Determine if a particular module should be linked to.
*
* @since 1.10.0
*
* @hook sensei_do_link_to_module
*
* @param {bool} $do_link_to_module True if module should be linked to.
* @param {WP_Term} $module Module to check if it should be linked to.
* @param {bool} $link_to_current Allow for linking to the currently displayed module.
* @return {bool} Filtered value of $do_link_to_module.
*/
return apply_filters( 'sensei_do_link_to_module', $do_link_to_module, $module, $link_to_current );
}
/**
* Checks if a module taxonomy template file has been overridden.
*
* @since 1.10.0
*
* @return bool True if taxonomy template has been overridden.
*/
protected function is_module_tax_template_overridden() {
$file = 'taxonomy-module.php';
$find = array( $file, Sensei()->template_url . $file );
$template = locate_template( $find );
return (bool) $template;
}
/**
* Set lesson archive template to display on module taxonomy archive page
*
* @since 1.8.0
* @param string $template Default template
* @return string Modified template
*/
public function module_archive_template( $template ) {
if ( ! is_tax( $this->taxonomy ) ) {
return $template;
}
$file = 'taxonomy-module.php';
$find = array( $file, Sensei()->template_url . $file );
// locate the template file
$template = locate_template( $find );
if ( ! $template ) {
$template = Sensei()->plugin_path() . 'templates/' . $file;
}
return $template;
}
/**
* Modify module taxonomy archive query
*
* @since 1.8.0
* @param object $query The query object passed by reference
* @return void
*/
public function module_archive_filter( $query ) {
// If there is already an "orderby" property set
// then no need to do anything.
if ( $query->get( 'orderby' ) ) {
return;
}
if ( $query->is_main_query() && is_tax( $this->taxonomy ) ) {
// Limit to lessons only
$query->set( 'post_type', 'lesson' );
// Set order of lessons
if ( version_compare( Sensei()->version, '1.6.0', '>=' ) ) {
$module_id = $query->queried_object_id;
$query->set( 'meta_key', '_order_module_' . $module_id );
$query->set( 'orderby', 'meta_value_num date' );
} else {
$query->set( 'orderby', 'menu_order' );
}
$query->set( 'order', 'ASC' );
// Limit to specific course if specified
if ( isset( $_GET['course_id'] ) && 0 < intval( $_GET['course_id'] ) ) {
$course_id = intval( $_GET['course_id'] );
$meta_query = [];
$meta_query[] = array(
'key' => '_lesson_course',
'value' => intval( $course_id ),
);
$query->set( 'meta_query', $meta_query );
}
}
}
/**
* Modify archive page title
*
* @since 1.8.0
* @param string $title Default title
* @return string Modified title
*/
public function module_archive_title( $title ) {
if ( is_tax( $this->taxonomy ) ) {
/**
* Filter the module archive title.
*
* @hook sensei_module_archive_title
*
* @param {string} $title Archive title.
* @return {string} Filtered title.
*/
$title = apply_filters( 'sensei_module_archive_title', get_queried_object()->name );
}
return $title;
}
/**
* Display module description on taxonomy archive page
*
* @since 1.8.0
* @return void
*/
public function module_archive_description() {
// ensure this only shows once on the archive.
remove_action( 'sensei_loop_lesson_before', array( $this, 'module_archive_description' ), 30 );
if ( is_tax( $this->taxonomy ) ) {
$module = get_queried_object();
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Safe handling of course ID query var.
$course_id = isset( $_GET['course_id'] ) ? intval( $_GET['course_id'] ) : null;
$user_id = get_current_user_id();
$module_progress = false;
if ( $user_id && ! empty( $course_id ) ) {
$module_progress = $this->get_user_module_progress( $module->term_id, $course_id, $user_id );
}
if ( $module_progress && $module_progress > 0 ) {
$status = __( 'Completed', 'sensei-lms' );
$class = 'completed';
if ( $module_progress < 100 ) {
$status = __( 'In progress', 'sensei-lms' );
$class = 'in-progress';
}
echo '<p class="status ' . esc_attr( $class ) . '">' . esc_html( $status ) . '</p>';
}
if ( $this->can_view_module_content( $module, $course_id, $user_id ) ) {
/**
* Filter the module archive description.