-
Notifications
You must be signed in to change notification settings - Fork 199
/
includes_class-sensei-admin.php.html
2213 lines (1834 loc) · 108 KB
/
includes_class-sensei-admin.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-admin.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-admin.php</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code><?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Handles all admin views, assets and navigation.
*
* @package Views
* @author Automattic
* @since 1.0.0
*/
class Sensei_Admin {
/**
* @var $course_order_page_slug The slug for the Order Courses page.
*/
private $course_order_page_slug;
/**
* @var $lesson_order_page_slug The slug for the Order Lessons page.
*/
private $lesson_order_page_slug;
/**
* Sensei SVG Icon
*
* @var string The menu icon for Sensei LMS.
*/
private $sensei_icon = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgZmlsbD0ibm9uZSIgdmlld0JveD0iMCAwIDI0IDI0Ij48cGF0aCBmaWxsPSIjMDAwIiBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGQ9Ik04LjkwOCAxNy4xN2MuNDI4LS4zMzUuMjU0LS42OTIuODU2LS44MjMtLjAyNy4yODUuMjA5LjU3My4zNTkuODI0LS4xNTYtLjUzNy0uMDAzLS43ODMuMTEzLTEuMDUzLjQ5Ny0xLjE0Ny0uO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tLjEzLjQxNC0uMDUyLjMyNS0uNDU1LjUyLS41MTEuMTA4LS4wMzIuMjE3LjA4My40MjEtLjExMy4wNy0uMDY4LjE1Ny0uMTMuMzMyLS4wODQtLjIwNC0uMTA2LS4zMTQtLjAyLS40MTguMDQ0LS4yMDkuMTMtLjI5LS4wNTMtLjQ5LjEwMlptLTMuMTg2LTIuODFjLjIyOS0uMDMuNDU3LS4wNTMuNjg4LS4wNjhhLjE4Ni4xODYgMCAwIDAgLjEwNi0uMDg2LjE2Ni4xNjYgMCAwIDAgLjAxLS4xMzEuMzkyLjM5MiAwIDAgMSAuMjEtLjMwM2wtLjAwMi0uMDA0Yy0uMzY5LjAwMS0uNjY2LjE0NS0xLjA0My4zMDgtLjA3NS4wMzMtLjAzNS4xNjMtLjE2OS4zMTFhLjM3OC4zNzggMCAwIDEtLjI4OC4zNDQuMTkzLjE5MyAwIDAgMC0uMTI4LjA2Ny4xNjcuMTY3IDAgMCAwLS4wMzcuMTM0LjM1NC4zNTQgMCAwIDEtLjM5Ni4wOWMtLjI4NS0uMTEzLS40OTgtLjA5NS0uNjQuMDUzYS41NS41NSAwIDAgMSAuNTcuMDIuNDU0LjQ1NCAwIDAgMCAuNTU3LS4xMi4xOS4xOSAwIDAgMSAuMTc5LS4xNDRjLjIwMi0uMDc3LjMzLS4yMzUuMzgzLS40NzFaIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiLz48L3N2Zz4=';
/**
* Constructor.
*
* @since 1.0.0
*/
public function __construct() {
$this->course_order_page_slug = 'course-order';
$this->lesson_order_page_slug = 'lesson-order';
// register admin styles
add_action( 'admin_enqueue_scripts', array( $this, 'admin_styles_global' ) );
// register admin scripts
add_action( 'admin_enqueue_scripts', array( $this, 'register_scripts' ) );
add_action( 'admin_menu', array( $this, 'add_course_order' ) );
add_action( 'admin_menu', array( $this, 'add_lesson_order' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ), 9 );
add_action( 'menu_order', array( $this, 'admin_menu_order' ) );
add_action( 'admin_head', array( $this, 'admin_menu_highlight' ) );
add_action( 'admin_init', array( $this, 'sensei_add_custom_menu_items' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_print_scripts', array( $this, 'sensei_set_plugin_url' ) );
// Duplicate lesson & courses
add_filter( 'post_row_actions', array( $this, 'duplicate_action_link' ), 10, 2 );
add_action( 'admin_action_duplicate_lesson', array( $this, 'duplicate_lesson_action' ) );
add_action( 'admin_action_duplicate_course', array( $this, 'duplicate_course_action' ) );
add_action( 'admin_action_duplicate_course_with_lessons', array( $this, 'duplicate_course_with_lessons_action' ) );
// Handle course and lesson ordering.
add_action( 'admin_post_order_courses', array( $this, 'handle_order_courses' ) );
add_action( 'admin_post_order_lessons', array( $this, 'handle_order_lessons' ) );
// Handle lessons list table filtering
add_action( 'restrict_manage_posts', array( $this, 'lesson_filter_options' ) );
add_filter( 'request', array( $this, 'lesson_filter_actions' ) );
// Add Sensei items to 'at a glance' widget
add_filter( 'dashboard_glance_items', array( $this, 'glance_items' ), 10, 1 );
// Handle course and lesson deletions
add_action( 'trash_course', array( $this, 'delete_content' ), 10, 2 );
add_action( 'trash_lesson', array( $this, 'delete_content' ), 10, 2 );
// Add notices to WP dashboard
add_action( 'admin_notices', array( $this, 'theme_compatibility_notices' ) );
// warn users in case admin_email is not a real WP_User
add_action( 'admin_notices', array( $this, 'notify_if_admin_email_not_real_admin_user' ) );
// remove a course from course order when trashed
add_action( 'transition_post_status', array( $this, 'remove_trashed_course_from_course_order' ) );
// Add workaround for block editor bug on CPT pages. See the function doc for more information.
add_action( 'admin_footer', array( $this, 'output_cpt_block_editor_workaround' ) );
// Add AJAX endpoint for event logging.
add_action( 'wp_ajax_sensei_log_event', array( $this, 'ajax_log_event' ) );
Sensei_Tools::instance()->init();
Sensei_Status::instance()->init();
}
/**
* Add items to admin menu
*
* @since 1.4.0
* @since 4.8.0 Reactivate method since we have a new home page.
*
* @return void
*/
public function admin_menu() {
add_menu_page( 'Sensei LMS', 'Sensei LMS', self::get_top_menu_capability(), 'sensei', '', $this->sensei_icon, '50' );
}
/**
* Get the top menu minimum capability.
*
* @since 4.8.0
*
* @return string
*/
public static function get_top_menu_capability() {
$menu_cap = 'manage_sensei';
if ( ! current_user_can( 'manage_sensei' ) && current_user_can( 'manage_sensei_grades' ) ) {
$menu_cap = 'manage_sensei_grades';
}
return $menu_cap;
}
/**
* Add Course order page to admin panel.
*
* @since 4.0.0
* @access private
*/
public function add_course_order() {
add_submenu_page(
'', // Hide in menu.
__( 'Order Courses', 'sensei-lms' ),
__( 'Order Courses', 'sensei-lms' ),
'manage_sensei',
$this->course_order_page_slug,
array( $this, 'course_order_screen' )
);
}
/**
* Add Lesson order page to admin panel.
*
* @since 4.0.0
* @access private
*/
public function add_lesson_order() {
add_submenu_page(
'',
__( 'Order Lessons', 'sensei-lms' ),
__( 'Order Lessons', 'sensei-lms' ),
'edit_published_lessons',
$this->lesson_order_page_slug,
array( $this, 'lesson_order_screen' )
);
}
/**
* [admin_menu_order description]
*
* @since 1.4.0
* @param array $menu_order Existing menu order
* @return array Modified menu order for Sensei
*/
public function admin_menu_order( $menu_order ) {
// Initialize our custom order array
$sensei_menu_order = array();
// Get the index of our custom separator
$sensei_separator = array_search( 'separator-sensei', $menu_order );
// Loop through menu order and do some rearranging
foreach ( $menu_order as $index => $item ) :
if ( ( ( 'sensei' ) == $item ) ) :
$sensei_menu_order[] = 'separator-sensei';
$sensei_menu_order[] = $item;
unset( $menu_order[ $sensei_separator ] );
elseif ( ! in_array( $item, array( 'separator-sensei' ) ) ) :
$sensei_menu_order[] = $item;
endif;
endforeach;
// Return order
return $sensei_menu_order;
}
/**
* Handle highlighting of admin menu items
*
* @since 1.4.0
* @since 4.8.0 General review after adding the new Sensei Home page.
*
* @return void
*/
public function admin_menu_highlight() {
global $parent_file, $submenu_file, $taxonomy, $_wp_real_parent_file;
$screen = get_current_screen();
if ( empty( $screen ) ) {
return;
}
// phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited -- Only way to highlight our special pages in menu.
if ( 'edit-tags' === $screen->base && 'module' === $taxonomy ) {
$parent_file = 'sensei';
$submenu_file = 'edit-tags.php?taxonomy=module&post_type=course';
} elseif ( in_array( $screen->id, [ 'edit-module', 'admin_page_module-order' ], true ) ) {
// Module pages.
$parent_file = 'sensei';
$_wp_real_parent_file[''] = 'sensei';
$submenu_file = 'edit-tags.php?taxonomy=module&post_type=course';
} elseif ( in_array( $screen->id, [ 'course', 'edit-course-category', 'admin_page_course-order', 'admin_page_' . Sensei_Course::SHOWCASE_COURSES_SLUG ], true ) ) {
// Course pages.
$parent_file = 'sensei';
$_wp_real_parent_file[''] = 'sensei';
$submenu_file = 'edit.php?post_type=course';
} elseif ( in_array( $screen->id, [ 'lesson', 'edit-lesson-tag', 'admin_page_lesson-order' ], true ) ) {
// Lesson pages.
$parent_file = 'sensei';
$_wp_real_parent_file[''] = 'sensei';
$submenu_file = 'edit.php?post_type=lesson';
} elseif ( in_array( $screen->id, [ 'question', 'edit-question-category' ], true ) ) {
// Question pages.
$parent_file = 'sensei';
$_wp_real_parent_file[''] = 'sensei';
$submenu_file = 'edit.php?post_type=question';
} elseif ( in_array( $screen->id, [ 'sensei_message' ], true ) ) {
// Message pages.
$parent_file = 'sensei';
$_wp_real_parent_file[''] = 'sensei';
$submenu_file = 'edit.php?post_type=sensei_message';
} elseif ( in_array( $screen->id, [ 'sensei_email', 'edit-sensei_email' ], true ) ) {
// Message pages.
$parent_file = 'sensei';
$_wp_real_parent_file[''] = 'sensei';
$submenu_file = 'sensei-settings';
}
// phpcs:enable WordPress.WP.GlobalVariablesOverride.Prohibited
}
/**
* Redirect Sensei menu item to Analysis page
*
* @since 1.4.0
* @deprecated 4.0.0
*
* @return void
*/
public function page_redirect() {
_deprecated_function( __METHOD__, '4.0.0' );
if ( isset( $_GET['page'] ) && $_GET['page'] == 'sensei' ) {
wp_safe_redirect( 'admin.php?page=sensei_reports' );
exit;
}
}
/**
* install_pages_output function.
*
* Handles installation of the 2 pages needs for courses and my courses
*
* @deprecated 3.1.0 use Sensei_Setup_Wizard_Pages::create_pages instead.
* @access public
* @return void
*/
function install_pages_output() {
_deprecated_function( __METHOD__, '3.1.0', 'Sensei_Setup_Wizard_Pages::create_pages' );
}
/**
* create_page function.
*
* @deprecated 3.1.0 use Sensei_Setup_Wizard_Pages::create_page instead.
*
* @access public
* @param mixed $slug
* @param mixed $option
* @param string $page_title (default: '')
* @param string $page_content (default: '')
* @param int $post_parent (default: 0)
* @return integer $page_id
*/
function create_page( $slug, $page_title = '', $page_content = '', $post_parent = 0 ) {
_deprecated_function( __METHOD__, '3.1.0', 'Sensei_Setup_Wizard_Pages::create_page' );
return Sensei()->setup_wizard->pages->create_page( $slug, $page_title, $page_content, $post_parent );
}
/**
* create_pages function.
*
* @deprecated 3.1.0 use Sensei_Setup_Wizard_Pages::create_pages instead.
*
* @access public
* @return void
*/
function create_pages() {
_deprecated_function( __METHOD__, '3.1.0', 'Sensei_Setup_Wizard_Pages::create_pages' );
Sensei()->setup_wizard->pages->create_pages();
}
/**
* Load the global admin styles for the menu icon and the relevant page icon.
*
* @access public
* @since 1.0.0
*
* @param string $hook The current admin page.
*/
public function admin_styles_global( $hook ) {
global $post_type;
// Global Styles for icons and menu items
Sensei()->assets->enqueue( 'sensei-global', 'css/global.css', [], 'screen' );
// WordPress component styles with Sensei theming.
Sensei()->assets->register( 'sensei-wp-components', 'shared/styles/wp-components.css', [], 'screen' );
// Select 2 styles
Sensei()->assets->enqueue( 'sensei-core-select2', '../vendor/select2/select2.min.css', [], 'screen' );
Sensei()->assets->register( 'jquery-modal', '../vendor/jquery-modal-0.9.1/jquery.modal.min.css' );
// Test for Write Panel Pages
if ( $this->are_custom_admin_styles_allowed( $post_type, $hook, get_current_screen() ) ) {
Sensei()->assets->enqueue( 'sensei-admin-custom', 'css/admin-custom.css', [], 'screen' );
}
}
/**
* Check if it is allowed to enqueue admin custom styles.
*
* @param string $post_type The post type slug.
* @param string $hook_suffix The current admin page.
* @param WP_Screen|null $screen The current screen.
* @return bool Returns true if admin custom styles are allowed.
*/
private function are_custom_admin_styles_allowed( $post_type, $hook_suffix, $screen ) {
/**
* Filter the list of post types where the admin custom styles should be loaded.
*
* @hook sensei_scripts_allowed_post_types
*
* @param {array} $allowed_post_types The list of post types where the admin custom styles should be loaded.
* @return {array} Filtered list of allowed post types.
*/
$allowed_post_types = apply_filters( 'sensei_scripts_allowed_post_types', array( 'lesson', 'course', 'question' ) );
/**
* Filter the list of admin pages where the admin custom styles should be loaded.
*
* @hook sensei_scripts_allowed_post_type_pages
*
* @param {array} $allowed_post_type_pages The list of admin pages where the admin custom styles should be loaded.
* @return {array} Filtered list of allowed admin pages.
*/
$allowed_post_type_pages = apply_filters( 'sensei_scripts_allowed_post_type_pages', array( 'edit.php', 'post-new.php', 'post.php', 'edit-tags.php' ) );
/**
* Filter the list of admin pages slugs where the admin custom styles should be loaded.
*
* @hook sensei_scripts_allowed_pages
*
* @param {array} $allowed_pages The list of admin pages slugs where the admin custom styles should be loaded.
* @return {array} Filtered list of allowed admin pages.
*/
$allowed_pages = apply_filters( 'sensei_scripts_allowed_pages', array( 'sensei_grading', Sensei_Analysis::PAGE_SLUG, 'sensei_learners', 'sensei_updates', 'sensei-settings', 'sensei_learners', Sensei_Course::SHOWCASE_COURSES_SLUG, $this->lesson_order_page_slug, $this->course_order_page_slug ) );
$module_pages_screen_ids = [ 'edit-module' ];
$is_allowed_type = isset( $post_type ) && in_array( $post_type, $allowed_post_types, true );
$is_allowed_post_type_page = isset( $hook_suffix ) && in_array( $hook_suffix, $allowed_post_type_pages, true );
$is_allowed_page = isset( $_GET['page'] ) && in_array( $_GET['page'], $allowed_pages, true ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$is_modules_page = $screen && in_array( $screen->id, $module_pages_screen_ids, true );
return ( $is_allowed_type && $is_allowed_post_type_page ) || $is_allowed_page || $is_modules_page;
}
/**
* Globally register all scripts needed in admin.
*
* The script users should enqueue the script when needed.
*
* @since 1.8.2
* @access public
*/
public function register_scripts( $hook ) {
$screen = get_current_screen();
Sensei()->assets->register( 'sensei-dismiss-notices', 'js/admin/sensei-notice-dismiss.js', [] );
// Select2 script used to enhance all select boxes.
Sensei()->assets->register( 'sensei-core-select2', '../vendor/select2/select2.full.js', [ 'jquery' ] );
Sensei()->assets->register( 'jquery-modal', '../vendor/jquery-modal-0.9.1/jquery.modal.js', [ 'jquery' ], true );
Sensei()->assets->register(
'sensei-learners-admin-bulk-actions-js',
'js/learners-bulk-actions.js',
[ 'jquery', 'sensei-core-select2', 'jquery-modal', 'wp-i18n' ],
true
);
$ajax_object = array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
);
wp_localize_script( 'sensei-learners-admin-bulk-actions-js', 'ajax_object', $ajax_object );
Sensei()->assets->register( 'sensei-chosen', '../vendor/chosen/chosen.jquery.min.js', [ 'jquery' ], true );
Sensei()->assets->register( 'sensei-chosen-ajax', '../vendor/chosen/ajax-chosen.jquery.min.js', [ 'jquery', 'sensei-chosen' ], true );
// Load ordering script on Order Courses and Order Lessons pages.
if ( in_array( $screen->id, [ 'admin_page_course-order', 'admin_page_lesson-order' ], true ) ) {
Sensei()->assets->enqueue( 'sensei-ordering', 'js/admin/ordering.js', [ 'jquery', 'jquery-ui-sortable', 'sensei-core-select2' ], true );
}
// Load edit module scripts.
if ( 'edit-module' === $screen->id ) {
wp_enqueue_script( 'sensei-chosen-ajax' );
}
Sensei()->assets->enqueue( 'sensei-message-menu-fix', 'js/admin/message-menu-fix.js', [ 'jquery' ], true );
// Event logging.
Sensei()->assets->enqueue( 'sensei-event-logging', 'js/admin/event-logging.js', [ 'jquery' ], true );
if ( $this->has_custom_navigation( $screen ) ) {
Sensei()->assets->enqueue( 'sensei-admin-custom-navigation', 'js/admin/custom-navigation.js', [], true );
}
wp_localize_script( 'sensei-event-logging', 'sensei_event_logging', [ 'enabled' => Sensei_Usage_Tracking::get_instance()->get_tracking_enabled() ] );
}
/**
* Check if the current screen has a custom navigation.
*
* @param WP_Screen|null $screen The current screen.
* @return bool
*/
private function has_custom_navigation( $screen ) {
$screens_with_custom_navigation = [
'edit-course',
'edit-course-category',
'edit-module',
'edit-lesson',
'edit-lesson-tag',
'edit-question',
'edit-question-category',
'sensei-lms_page_' . Sensei_Analysis::PAGE_SLUG,
'sensei-lms_page_sensei_learners',
];
/**
* Allows modifying the list of screens where the scripts for custom
* navigation (which handles some operations like hiding the
* navigation title that is generated by wp, for example, the name of the
* custom post type that's shown on the list page of custom post type)
* should be loaded.
*
* @since 4.5.0
* @hook sensei_custom_navigation_allowed_screens
*
* @param {array} $screens_with_custom_navigation Screens where custom navigation scrips will be loaded.
*
* @return {array} Screens where custom navigation scrips will be loaded.
*/
$screens_with_custom_navigation = apply_filters(
'sensei_custom_navigation_allowed_screens',
$screens_with_custom_navigation
);
return $screen
&& ( in_array( $screen->id, $screens_with_custom_navigation, true ) )
&& ( 'term' !== $screen->base );
}
/**
* admin_install_notice function.
*
* @deprecated 3.1.0
* @access public
* @return void
*/
function admin_install_notice() {
_deprecated_function( __METHOD__, '3.1.0', 'Sensei_Setup_Wizard::setup_wizard_notice' );
}
/**
* admin_installed_notice function.
*
* @deprecated 3.1.0
* @access public
* @return void
*/
function admin_installed_notice() {
_deprecated_function( __METHOD__, '3.1.0', 'Sensei_Setup_Wizard::setup_wizard_notice' );
}
/**
* admin_notices_styles function.
*
* @deprecated 3.1.0
* @access public
* @return void
*/
function admin_notices_styles() {
_deprecated_function( __METHOD__, '3.1.0', 'Sensei_Setup_Wizard::setup_wizard_notice' );
}
/**
* Add links for duplicating lessons & courses
*
* @param array $actions Default actions
* @param object $post Current post
* @return array Modified actions
*/
public function duplicate_action_link( $actions, $post ) {
switch ( $post->post_type ) {
case 'lesson':
$confirm = __( 'This will duplicate the lesson quiz and all of its questions. Are you sure you want to do this?', 'sensei-lms' );
$actions['duplicate'] = "<a onclick='return confirm(\"" . $confirm . "\");' href='" . $this->get_duplicate_link( $post->ID ) . "' title='" . esc_attr( __( 'Duplicate this lesson', 'sensei-lms' ) ) . "'>" . __( 'Duplicate', 'sensei-lms' ) . '</a>';
break;
case 'course':
$confirm = __( 'This will duplicate the course lessons along with all of their quizzes and questions. Are you sure you want to do this?', 'sensei-lms' );
$actions['duplicate'] = '<a href="' . $this->get_duplicate_link( $post->ID ) . '" title="' . esc_attr( __( 'Duplicate this course', 'sensei-lms' ) ) . '">' . __( 'Duplicate', 'sensei-lms' ) . '</a>';
$actions['duplicate_with_lessons'] = '<a onclick="return confirm(\'' . $confirm . '\');" href="' . $this->get_duplicate_link( $post->ID, true ) . '" title="' . esc_attr( __( 'Duplicate this course with its lessons', 'sensei-lms' ) ) . '">' . __( 'Duplicate (with lessons)', 'sensei-lms' ) . '</a>';
break;
}
return $actions;
}
/**
* Generate duplicationlink
*
* @param integer $post_id Post ID
* @param boolean $with_lessons Include lessons or not
* @return string Duplication link
*/
private function get_duplicate_link( $post_id = 0, $with_lessons = false ) {
$post = get_post( $post_id );
$action = 'duplicate_' . $post->post_type;
if ( 'course' == $post->post_type && $with_lessons ) {
$action .= '_with_lessons';
}
$bare_url = admin_url( 'admin.php?action=' . $action . '&post=' . $post_id );
$url = wp_nonce_url( $bare_url, $action . '_' . $post_id );
return apply_filters( $action . '_link', $url, $post_id );
}
/**
* Duplicate lesson
*
* @return void
*/
public function duplicate_lesson_action() {
$this->duplicate_content( 'lesson' );
}
/**
* Duplicate course
*
* @return void
*/
public function duplicate_course_action() {
$this->duplicate_content( 'course' );
}
/**
* Duplicate course with lessons.
*
* @return void
*/
public function duplicate_course_with_lessons_action() {
$this->duplicate_content( 'course', true );
}
/**
* Redirect the user safely.
*
* @access private
* @param string $redirect_url URL to redirect the user.
* @return void
*/
protected function safe_redirect( $redirect_url ) {
wp_safe_redirect( esc_url_raw( $redirect_url ) );
exit;
}
/**
* Duplicate content.
*
* @param string $post_type Post type being duplicated.
* @param boolean $with_lessons Include lessons or not.
* @return void
*/
private function duplicate_content( $post_type = 'lesson', $with_lessons = false ) {
if ( ! isset( $_GET['post'] ) ) {
// translators: Placeholder is the post type string.
wp_die( esc_html( sprintf( __( 'Please supply a %1$s ID.', 'sensei-lms' ) ), $post_type ) );
}
$post_id = $_GET['post'];
$post = get_post( $post_id );
if ( ! in_array( get_post_type( $post_id ), array( 'lesson', 'course' ), true ) ) {
wp_die( esc_html__( 'Invalid post type. Can duplicate only lessons and courses', 'sensei-lms' ) );
}
$event = false;
if ( 'course' === $post_type ) {
$event = 'course_duplicate';
} elseif ( 'lesson' === $post_type ) {
$event = 'lesson_duplicate';
}
$event_properties = [
$post_type . '_id' => $post_id,
];
$action = 'duplicate_' . $post_type;
if ( $with_lessons ) {
$action .= '_with_lessons';
}
check_admin_referer( $action . '_' . $post_id );
if ( ! current_user_can( 'manage_sensei_grades' ) ) {
wp_die( esc_html__( 'Insufficient permissions', 'sensei-lms' ) );
}
if ( ! is_wp_error( $post ) ) {
$new_post = $this->duplicate_post( $post );
if ( $new_post && ! is_wp_error( $new_post ) ) {
if ( 'lesson' == $new_post->post_type ) {
$this->duplicate_lesson_quizzes( $post_id, $new_post->ID );
}
if ( 'course' == $new_post->post_type && $with_lessons ) {
$event = 'course_duplicate_with_lessons';
$event_properties['lesson_count'] = $this->duplicate_course_lessons( $post_id, $new_post->ID );
}
$redirect_url = admin_url( 'post.php?post=' . $new_post->ID . '&action=edit' );
} else {
$redirect_url = admin_url( 'edit.php?post_type=' . $post->post_type . '&message=duplicate_failed' );
}
// Log event.
if ( $event ) {
sensei_log_event( $event, $event_properties );
}
$this->safe_redirect( $redirect_url );
}
}
/**
* Duplicate quizzes inside lessons.
*
* @param integer $old_lesson_id ID of original lesson.
* @param integer $new_lesson_id ID of duplicate lesson.
* @return void
*/
private function duplicate_lesson_quizzes( $old_lesson_id, $new_lesson_id ) {
$old_quiz_id = Sensei()->lesson->lesson_quizzes( $old_lesson_id );
if ( empty( $old_quiz_id ) ) {
return;
}
$old_quiz_questions = Sensei()->lesson->lesson_quiz_questions( $old_quiz_id );
// duplicate the generic wp post information
$new_quiz = $this->duplicate_post( get_post( $old_quiz_id ), '' );
// update the new lesson data
add_post_meta( $new_lesson_id, '_lesson_quiz', $new_quiz->ID );
// update the new quiz data
add_post_meta( $new_quiz->ID, '_quiz_lesson', $new_lesson_id );
wp_update_post(
array(
'ID' => $new_quiz->ID,
'post_parent' => $new_lesson_id,
)
);
foreach ( $old_quiz_questions as $question ) {
// copy the question order over to the new quiz
$old_question_order = get_post_meta( $question->ID, '_quiz_question_order' . $old_quiz_id, true );
$new_question_order = str_ireplace( $old_quiz_id, $new_quiz->ID, $old_question_order );
add_post_meta( $question->ID, '_quiz_question_order' . $new_quiz->ID, $new_question_order );
// Add question to quiz
add_post_meta( $question->ID, '_quiz_id', $new_quiz->ID, false );
}
}
/**
* Update prerequisite ids after course duplication.
*
* @param array $lessons_to_update List with lesson_id and old_prerequisite_id id to update.
* @param array $new_lesson_id_lookup History with the id before and after duplication.
* @return void
*/
private function update_lesson_prerequisite_ids( $lessons_to_update, $new_lesson_id_lookup ) {
foreach ( $lessons_to_update as $lesson_to_update ) {
$old_prerequisite_id = $lesson_to_update['old_prerequisite_id'];
$new_prerequisite_id = $new_lesson_id_lookup[ $old_prerequisite_id ];
add_post_meta( $lesson_to_update['lesson_id'], '_lesson_prerequisite', $new_prerequisite_id );
}
}
/**
* Get an prerequisite update object.
*
* @param integer $old_lesson_id ID of the lesson before the duplication.
* @param integer $new_lesson_id New ID of the lesson.
* @return array Object with the id of the lesson to update and its old prerequisite id.
*/
private function get_prerequisite_update_object( $old_lesson_id, $new_lesson_id ) {
$lesson_prerequisite = get_post_meta( $old_lesson_id, '_lesson_prerequisite', true );
if ( ! empty( $lesson_prerequisite ) ) {
return array(
'lesson_id' => $new_lesson_id,
'old_prerequisite_id' => $lesson_prerequisite,
);
}
return null;
}
/**
* Update the _lesson_order meta on the duplicated Course so that it uses
* the new Lesson IDs.
*
* @since 3.0.0
*
* @param int $course_id The ID of the new Course.
* @param array $new_lesson_id_lookup An array mapping old lesson IDs to the
* IDs of their duplicates.
*/
private function update_lesson_order_on_course( $course_id, $new_lesson_id_lookup ) {
$old_lesson_order_string = get_post_meta( $course_id, '_lesson_order', true );
if ( empty( $old_lesson_order_string ) ) {
return;
}
$old_lesson_order = explode( ',', $old_lesson_order_string );
$new_lesson_order = [];
// Map old lesson IDs to new IDs.
foreach ( $old_lesson_order as $old_lesson_id ) {
if ( ! isset( $new_lesson_id_lookup[ $old_lesson_id ] ) ) {
continue;
}
// Add new lesson ID to order.
$new_lesson_id = $new_lesson_id_lookup[ $old_lesson_id ];
$new_lesson_order[] = $new_lesson_id;
}
// Persist new lesson order to course meta.
$new_lesson_order_string = implode( ',', $new_lesson_order );
update_post_meta( $course_id, '_lesson_order', $new_lesson_order_string );
}
/**
* Update the _order_<course-id> on a newly duplicated Lesson to use the
* new Course ID.
*
* @since 3.0.0
*
* @param WP_Post $lesson The new Lesson.
* @param int $old_course_id The ID of the old Course that was duplicated.
* @param int $new_course_id The ID of the new Course.
*/
private function update_lesson_order_on_lesson( $lesson, $old_course_id, $new_course_id ) {
$lesson_order_value = get_post_meta( $lesson->ID, "_order_$old_course_id", true );
update_post_meta( $lesson->ID, "_order_$new_course_id", $lesson_order_value );
delete_post_meta( $lesson->ID, "_order_$old_course_id" );
}
/**
* Duplicate lessons inside a course.
*
* @param integer $old_course_id ID of original course.
* @param integer $new_course_id ID of duplicated course.
* @return int Number of lessons duplicated.
*/
private function duplicate_course_lessons( $old_course_id, $new_course_id ) {
$lessons = Sensei()->course->course_lessons( $old_course_id, 'any' );
$new_lesson_id_lookup = array();
$lessons_to_update = array();
foreach ( $lessons as $lesson ) {
$new_lesson = $this->duplicate_post( $lesson, '', true );
add_post_meta( $new_lesson->ID, '_lesson_course', $new_course_id );
$update_prerequisite_object = $this->get_prerequisite_update_object( $lesson->ID, $new_lesson->ID );
if ( ! is_null( $update_prerequisite_object ) ) {
$lessons_to_update[] = $update_prerequisite_object;
}
$new_lesson_id_lookup[ $lesson->ID ] = $new_lesson->ID;
$this->duplicate_lesson_quizzes( $lesson->ID, $new_lesson->ID );
// Update the _order_<course-id> meta on the lesson.
$this->update_lesson_order_on_lesson( $new_lesson, $old_course_id, $new_course_id );
}
$this->update_lesson_prerequisite_ids( $lessons_to_update, $new_lesson_id_lookup );
// Update the _lesson_order meta on the course.
$this->update_lesson_order_on_course( $new_course_id, $new_lesson_id_lookup );
return count( $lessons );
}
/**
* Duplicate post.
*
* @param object $post Post to be duplicated.
* @param string $suffix Suffix for duplicated post title.
* @param boolean $ignore_course Ignore lesson course when dulicating.
* @return object Duplicate post object.
*/
private function duplicate_post( $post, $suffix = null, $ignore_course = false ) {
$new_post = array();
foreach ( $post as $k => $v ) {
if ( ! in_array( $k, array( 'ID', 'post_status', 'post_date', 'post_date_gmt', 'post_name', 'post_modified', 'post_modified_gmt', 'guid', 'comment_count' ) ) ) {
$new_post[ $k ] = $v;
}
}
$new_post['post_title'] .= $suffix;
$new_post['post_date'] = current_time( 'mysql' );
$new_post['post_date_gmt'] = get_gmt_from_date( $new_post['post_date'] );
$new_post['post_modified'] = $new_post['post_date'];
$new_post['post_modified_gmt'] = $new_post['post_date_gmt'];
switch ( $post->post_type ) {
case 'course':
$new_post['post_status'] = 'draft';
break;
case 'lesson':
$new_post['post_status'] = 'draft';
break;
case 'quiz':
$new_post['post_status'] = 'publish';
break;
case 'question':
$new_post['post_status'] = 'publish';
break;
}
// As per wp_update_post() we need to escape the data from the db.
$new_post = wp_slash( $new_post );
/**
* Filter arguments for `wp_insert_post` when duplicating a Sensei
* post. This may be a Course, Lesson, or Quiz.
*
* @since 3.11.0
*
* @hook sensei_duplicate_post_args
*
* @param {array} $new_post The arguments for duplicating the post.
* @param {WP_Post} $post The original post being duplicated.
* @return {array} The new arguments to be handed to `wp_insert_post`.
*/
$new_post = apply_filters( 'sensei_duplicate_post_args', $new_post, $post );
$new_post_id = wp_insert_post( $new_post );
if ( ! is_wp_error( $new_post_id ) ) {
$post_meta = get_post_custom( $post->ID );
if ( $post_meta ) {
/**
* Ignored meta fields when duplicating a post.
*
* @since 3.7.0
*
* @hook sensei_duplicate_post_ignore_meta
*
* @param {array} $meta_keys The meta keys to be ignored.
* @param {WP_Post} $new_post The new duplicate post.
* @param {WP_Post} $post The original post that's being duplicated.
* @return {array} $meta_keys The meta keys to be ignored.
*/
$ignore_meta = apply_filters( 'sensei_duplicate_post_ignore_meta', [ '_quiz_lesson', '_quiz_id', '_lesson_quiz', '_lesson_prerequisite' ], $new_post, $post );
if ( $ignore_course ) {
$ignore_meta[] = '_lesson_course';
}
foreach ( $post_meta as $key => $meta ) {
foreach ( $meta as $value ) {
$value = maybe_unserialize( $value );
if ( ! in_array( $key, $ignore_meta ) ) {
add_post_meta( $new_post_id, $key, $value );
}
}
}
}
add_post_meta( $new_post_id, '_duplicate', $post->ID );
$taxonomies = get_object_taxonomies( $post->post_type, 'objects' );
foreach ( $taxonomies as $slug => $tax ) {
$terms = get_the_terms( $post->ID, $slug );
if ( isset( $terms ) && is_array( $terms ) && 0 < count( $terms ) ) {
foreach ( $terms as $term ) {
wp_set_object_terms( $new_post_id, $term->term_id, $slug, true );
}
}
}
$new_post = get_post( $new_post_id );
return $new_post;
}
return false;
}
/**
* Add options to filter lessons
*
* @return void