class-ld-settings-pages.php
32.3 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
<?php
/**
* LearnDash Settings Page Abstract Class.
*
* @since 2.4.0
* @package LearnDash\Settings\Pages
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'LearnDash_Settings_Page' ) ) {
/**
* Class for LearnDash Settings Pages.
*
* @since 2.4.0
*/
class LearnDash_Settings_Page {
/**
* Variable to hold all settings page instances.
*
* @var array $_instances
*/
protected static $_instances = array(); //phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
/**
* Match the parent menu below LearnDash main menu. This will be the URL as in
* edit.php?post_type=sfwd-courses, admin.php?page=learndash-lms-reports, admin.php?page=learndash_lms_settings
*
* @var string $parent_menu_page_url string.
*/
protected $parent_menu_page_url = '';
/**
* Match the user capability to view this page
*
* @var string $menu_page_capability
*/
protected $menu_page_capability = LEARNDASH_ADMIN_CAPABILITY_CHECK;
/**
* Match the WP Screen ID. DO NOT SET. This is set when from add_submenu_page().
* The value set via WP will be something like 'sfwd-courses_page_courses-options'
* for the URL admin.php?page=courses-options because it will reside within the
* sfwd-courses submenu.
*
* @var string $settings_screen_id
*/
protected $settings_screen_id = '';
/**
* Match the URL 'page=' parameter value. For example admin.php?page=learndash-lms-reports
* value will be 'learndash-lms-reports'
*
* @var string $settings_page_id
*/
protected $settings_page_id = '';
/**
* Title for page <h1></h1> string
*
* @var string $settings_page_title
*/
protected $settings_page_title = '';
/**
* Title for tab string
*
* @var string $settings_tab_title
*/
protected $settings_tab_title = '';
/**
* Priority for tab
*
* @var integer $settings_tab_priority
*/
protected $settings_tab_priority = 30;
/**
* The number of columns to show. Most admin screens will be 2. But we set to 1 for the initial.
*
* @var integer $settings_columns
*/
protected $settings_columns = 2;
/**
* Wether to show the Submit metabox.
*
* @var boolean $show_submit_meta
*/
protected $show_submit_meta = true;
/**
* Wether to show the Quick Links metabox.
*
* @var boolean $show_quick_links_meta
*/
protected $show_quick_links_meta = true;
/**
* Wether to show wrap all settings in a <form></form>.
*
* @var boolean $settings_form_wrap
*/
protected $settings_form_wrap = true;
/**
* Show metaboxes are sub-pages. (One per sub-menu).
*
* @var boolean $settings_metabox_as_sub
*/
protected $settings_metabox_as_sub = false;
/**
* Set from the current metabox showing.
*
* @var string $settings_metabox_as_sub_current
*/
protected $settings_metabox_as_sub_current = '';
/**
* Collection of page sections.
*
* @var array $page_sections
*/
protected $page_sections = null;
/**
* Public constructor for class
*
* @since 2.4.0
*/
public function __construct() {
global $learndash_pages;
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'learndash_admin_tabs_set', array( $this, 'admin_tabs' ), 10 );
if ( empty( $this->settings_tab_title ) ) {
$this->settings_tab_title = $this->settings_page_title;
}
if ( ! empty( $this->settings_page_id ) ) {
add_filter( 'option_page_capability_' . $this->settings_page_id, array( $this, 'filter_option_page_capability' ), 10, 1 );
if ( ! isset( $learndash_pages[ $this->settings_page_id ] ) ) {
$learndash_pages[] = $this->settings_page_id;
}
}
if ( true === $this->settings_metabox_as_sub ) {
add_filter( 'learndash_show_section', array( $this, 'should_show_settings_section' ), 10, 3 );
}
}
/**
* Function to get a specific setting page instance.
*
* @since 2.4.0
*
* @param string $page_key Page key to get instance of.
*
* @return LearnDash_Settings_Page|null Page instance.
*/
final public static function get_page_instance( $page_key = '' ) {
if ( ! empty( $page_key ) ) {
if ( isset( self::$_instances[ $page_key ] ) ) {
return self::$_instances[ $page_key ];
}
}
return null;
}
/**
* Function to get a specific setting page instance.
*
* @since 3.6.0
*
* @param string $criteria_field Page field to compare.
* @param string $criteria_value Page field value to compare.
* @param string $return_field Page field to return. If empty object is returned.
*
* @return mixed Page object or individual field per `$return_field` value.
*/
final public static function get_page_instance_by( $criteria_field = '', $criteria_value = '', $return_field = '' ) {
if ( ( ! empty( $criteria_field ) ) && ( ! empty( $criteria_value ) ) ) {
if ( ! empty( self::$_instances ) ) {
foreach ( self::$_instances as $_instance_key => $_instance_object ) {
if ( ( $_instance_object ) && ( is_a( $_instance_object, 'LearnDash_Settings_Page' ) ) ) {
if ( ( property_exists( $_instance_object, $criteria_field ) ) && ( $_instance_object->$criteria_field === $criteria_value ) ) {
if ( ( ! empty( $return_field ) ) && ( property_exists( $_instance_object, $return_field ) ) ) {
return $_instance_object->$return_field;
}
return $_instance_object;
}
}
}
}
}
}
/**
* Function to set/add setting page to instances array.
*
* @since 2.4.0
*/
final public static function add_page_instance() {
$section_class = get_called_class();
if ( ! isset( self::$_instances[ $section_class ] ) ) {
self::$_instances[ $section_class ] = new $section_class();
}
}
/**
* Returns global settings pages' names.
*
* @since 4.3.0
*
* @return array
*/
final public static function get_global_settings_page_names(): array {
return array_keys(
array_filter(
self::$_instances,
function ( LearnDash_Settings_Page $instance ): bool {
return 'admin.php?page=learndash_lms_settings' === $instance->parent_menu_page_url;
}
)
);
}
/**
* Action hook to handle admin_init processing from WP.
*
* @since 2.4.0
*/
public function admin_init() {
/**
* Fires on LearnDash settings page init.
*
* @param string $setting_page_id Settings page ID.
*/
do_action( 'learndash_settings_page_init', $this->settings_page_id );
/**
* Filters whether the show the section submit.
*
* @param bool $show_submit_meta show section submit.
* @param string $settings_page_id Settings Page ID.
*/
if ( true === apply_filters( 'learndash_settings_show_section_submit', $this->show_submit_meta, $this->settings_page_id ) ) {
$submit_obj = new LearnDash_Settings_Section_Side_Submit(
array(
'settings_screen_id' => $this->settings_screen_id,
'settings_page_id' => $this->settings_page_id,
)
);
}
/**
* Filters whether the show the section quick links .
*
* @param bool $show_quick_links_meta show section quick links.
* @param string $settings_page_id Settings Page ID.
*/
if ( true === apply_filters( 'learndash_settings_show_section_quick_liinks', $this->show_quick_links_meta, $this->settings_page_id ) ) { // cspell:disable-line.
$ql_obj = new LearnDash_Settings_Section_Side_Quick_Links(
array(
'settings_screen_id' => $this->settings_screen_id,
'settings_page_id' => $this->settings_page_id,
)
);
}
}
/**
* Action hook to handle admin_menu processing from WP.
*
* @since 2.4.0
*/
public function admin_menu() {
if ( ! $this->settings_screen_id ) {
$this->settings_screen_id = add_submenu_page(
$this->parent_menu_page_url,
$this->settings_page_title,
$this->settings_page_title,
$this->menu_page_capability,
$this->settings_page_id,
array( $this, 'show_settings_page' )
);
}
add_action( 'load-' . $this->settings_screen_id, array( $this, 'load_settings_page' ) );
}
/**
* Action hook to handle admin_tabs processing from LearnDash.
*
* @since 2.4.0
*
* @param string $admin_menu_section Current admin menu section.
*/
public function admin_tabs( $admin_menu_section ) {
if ( $admin_menu_section === $this->parent_menu_page_url ) {
learndash_add_admin_tab_item(
$this->parent_menu_page_url,
array(
'id' => $this->settings_screen_id,
'link' => add_query_arg( array( 'page' => $this->settings_page_id ), 'admin.php' ),
'cap' => $this->menu_page_capability,
'name' => ! empty( $this->settings_tab_title ) ? $this->settings_tab_title : $this->settings_page_title,
),
$this->settings_tab_priority
);
}
}
/**
* Action hook to handle current settings page load.
*
* @since 2.4.0
*/
public function load_settings_page() {
global $learndash_assets_loaded;
if ( defined( 'LEARNDASH_SETTINGS_SECTION_TYPE' ) && ( 'metabox' === LEARNDASH_SETTINGS_SECTION_TYPE ) ) {
wp_enqueue_script( 'common' );
wp_enqueue_script( 'wp-lists' );
wp_enqueue_script( 'postbox' );
/**
* Fires on adding setting metaboxes.
*
* @param string $setting_screen_id Settings screen ID.
*/
do_action( 'learndash_add_meta_boxes', $this->settings_screen_id );
add_action( 'admin_footer-' . $this->settings_screen_id, array( $this, 'load_footer_scripts' ) );
add_filter( 'screen_layout_columns', array( $this, 'screen_layout_column' ), 10, 2 );
}
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_media();
wp_enqueue_style(
'learndash_style',
LEARNDASH_LMS_PLUGIN_URL . 'assets/css/style' . learndash_min_asset() . '.css',
array(),
LEARNDASH_SCRIPT_VERSION_TOKEN
);
wp_style_add_data( 'learndash_style', 'rtl', 'replace' );
$learndash_assets_loaded['styles']['learndash_style'] = __FUNCTION__;
wp_enqueue_style(
'sfwd-module-style',
LEARNDASH_LMS_PLUGIN_URL . 'assets/css/sfwd_module' . learndash_min_asset() . '.css',
array(),
LEARNDASH_SCRIPT_VERSION_TOKEN
);
wp_style_add_data( 'sfwd-module-style', 'rtl', 'replace' );
$learndash_assets_loaded['styles']['sfwd-module-style'] = __FUNCTION__;
wp_enqueue_script(
'sfwd-module-script',
LEARNDASH_LMS_PLUGIN_URL . 'assets/js/sfwd_module' . learndash_min_asset() . '.js',
array( 'jquery' ),
LEARNDASH_SCRIPT_VERSION_TOKEN,
true
);
$learndash_assets_loaded['scripts']['sfwd-module-script'] = __FUNCTION__;
wp_localize_script( 'sfwd-module-script', 'sfwd_data', array() );
learndash_admin_settings_page_assets();
if ( isset( $_GET['ld_reset_metaboxes'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
delete_user_meta( get_current_user_id(), 'closedpostboxes_' . $this->settings_screen_id );
delete_user_meta( get_current_user_id(), 'metaboxhidden_' . $this->settings_screen_id );
delete_user_meta( get_current_user_id(), 'meta-box-order_' . $this->settings_screen_id );
}
/**
* Fires on LearnDash settings page load.
*
* @deprecated 3.2.3 Use {@see 'learndash_settings_page_load'} instead.
*
* @param string $setting_screen_id Settings screen ID.
* @param string $setting_page_id Settings page ID.
*/
do_action_deprecated(
'learndash-settings-page-load',
array( $this->settings_screen_id, $this->settings_page_id ),
'3.6.0',
'learndash_settings_page_load'
);
/**
* Fires on LearnDash settings page load.
*
* @since 3.6.0
*
* @param string $setting_screen_id Settings screen ID.
* @param string $setting_page_id Settings page ID.
*/
do_action( 'learndash_settings_page_load', $this->settings_screen_id, $this->settings_page_id );
}
/**
* Action hook to handle current settings page layout columns.
*
* @since 2.4.0
*
* @param integer $columns Number of columns to show.
* @param Object $screen_id Current screen object.
*
* @return integer $columns
*/
public function screen_layout_column( $columns = false, $screen_id = '' ) {
if ( $screen_id == $this->settings_screen_id ) {
$columns[ $screen_id ] = $this->settings_columns;
}
/**
* Add this filter to override the get user option logic. This is to force
* the screen layout option for user who don't have this defined.
*
* @since 2.6.0
*/
add_filter(
"get_user_option_screen_layout_{$screen_id}",
function( $option_value = '', $option_key = '' ) {
if ( "screen_layout_{$this->settings_screen_id}" === $option_key ) {
$option_value = $this->settings_columns;
}
return $option_value;
},
1,
2
);
return $columns;
}
/**
* Action hook to handle footer JS/CSS added footer
*
* @since 2.4.0
*/
public function load_footer_scripts() {
?>
<script type="text/javascript">
//<![CDATA[
jQuery( function($) {
// toggle
$('.if-js-closed').removeClass('if-js-closed').addClass('closed');
postboxes.add_postbox_toggles( '<?php echo esc_attr( $this->settings_screen_id ); ?>' );
// display spinner
$('#fx-smb-form').on( 'submit', function() {
$('#publishing-action .spinner').css('display','inline');
});
// confirm before reset
$('.learndash-settings-page-wrap .submitdelete').on('click', function() {
var confirm_message = $(this).data('confirm');
if (typeof confirm_message !== 'undefined') {
return confirm( confirm_message );
}
});
if ( jQuery( '#side-sortables').length ) {
if ( jQuery( '#side-sortables div.postbox').length ) {
jQuery( '#side-sortables').removeClass('empty-container');
}
}
});
//]]>
</script>
<?php
/**
* Fires after settings page footer scripts.
*
* @param string $setting_screen_id Settings screen ID.
* @param string $setting_page_id Settings page ID.
*/
do_action( 'learndash_settings_page_footer_scripts', $this->settings_screen_id, $this->settings_page_id );
}
/**
* Function to handle showing of Settings page. This is the main function for all visible
* output. Extending classes can implement its own function.
*
* @since 2.4.0
*/
public function show_settings_page() {
if ( defined( 'LEARNDASH_SETTINGS_SECTION_TYPE' ) && ( 'metabox' === LEARNDASH_SETTINGS_SECTION_TYPE ) ) {
?>
<div class="wrap learndash-settings-page-wrap">
<?php settings_errors(); ?>
<?php $this->show_settings_sections_sub_menu(); ?>
<?php
/**
* Fires before the settings page title.
*
* @param string $setting_screen_id Settings screen ID.
* @param string $setting_page_id Settings page ID.
*/
do_action( 'learndash_settings_page_before_title', $this->settings_screen_id, $this->settings_page_id );
?>
<?php echo wp_kses_post( $this->get_admin_page_title() ); ?>
<?php
/**
* Fires after the settings page title.
*
* @param string $setting_screen_id Settings screen ID.
* @param string $setting_page_id Settings page ID.
*/
do_action( 'learndash_settings_page_after_title', $this->settings_screen_id, $this->settings_page_id );
?>
<?php
/**
* Fires before the settings page form.
*
* @param string $setting_screen_id Settings screen ID.
* @param string $setting_page_id Settings page ID.
*/
do_action( 'learndash_settings_page_before_form', $this->settings_screen_id, $this->settings_page_id );
?>
<?php echo $this->get_admin_page_form( true ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Need to output HTML. ?>
<?php
/**
* Fires inside settings page form in the top.
*
* @param string $setting_screen_id Settings screen ID.
* @param string $setting_page_id Settings page ID.
*/
do_action( 'learndash_settings_page_inside_form_top', $this->settings_screen_id, $this->settings_page_id );
?>
<?php settings_fields( $this->settings_page_id ); ?>
<?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
<?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-<?php echo 1 == get_current_screen()->get_columns() ? '1' : '2'; ?>">
<div id="postbox-container-1" class="postbox-container">
<?php do_meta_boxes( $this->settings_screen_id, 'side', null ); ?>
</div>
<div id="postbox-container-2" class="postbox-container">
<?php
/**
* Fires before the settings page meta boxes.
*
* @param string $setting_screen_id Settings screen ID.
* @param string $setting_page_id Settings page ID.
*/
do_action( 'learndash_settings_page_before_metaboxes', $this->settings_screen_id, $this->settings_page_id );
?>
<?php do_meta_boxes( $this->settings_screen_id, 'normal', null ); ?>
<?php do_meta_boxes( $this->settings_screen_id, 'advanced', null ); ?>
<?php
/**
* Fires after the settings page meta boxes.
*
* @param string $setting_screen_id Settings screen ID.
* @param string $setting_page_id Settings page ID.
*/
do_action( 'learndash_settings_page_after_metaboxes', $this->settings_screen_id, $this->settings_page_id );
?>
</div>
</div>
<br class="clear">
</div>
<?php
/**
* Fires inside settings page form at the bottom.
*
* @param string $setting_screen_id Settings screen ID.
* @param string $setting_page_id Settings page ID.
*/
do_action( 'learndash_settings_page_inside_form_bottom', $this->settings_screen_id, $this->settings_page_id );
?>
<?php echo $this->get_admin_page_form( false ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Need to output HTML. ?>
<?php
/**
* Fires after the settings page form.
*
* @param string $setting_screen_id Settings screen ID.
* @param string $setting_page_id Settings page ID.
*/
do_action( 'learndash_settings_page_after_form', $this->settings_screen_id, $this->settings_page_id );
?>
</div>
<?php
} else {
?>
<div class="wrap learndash-settings-page-wrap">
<?php settings_errors(); ?>
<?php echo wp_kses_post( $this->get_admin_page_title() ); ?>
<?php echo $this->get_admin_page_form( true ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Need to output HTML. ?>
<?php
// This prints out all hidden setting fields.
settings_fields( $this->settings_page_id );
do_settings_sections( $this->settings_page_id );
?>
<?php submit_button( esc_html__( 'Save Changes', 'learndash' ) ); ?>
<?php echo $this->get_admin_page_form( false ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Need to output HTML. ?>
</div>
<?php
}
}
/**
* Class utility function to return the settings page title.
*
* @since 2.5.0
*
* @return string page title.
*/
public function get_admin_page_title() {
/**
* Filters whether the admin settings page title should be displayed or not.
*
* @param bool $flag Whether to display page title or not. Default false.
*/
if ( true === apply_filters( 'learndash_admin_page_title_should_display', false ) ) {
/**
* Filters admin settings page title HTML output.
*
* @param string $title_output The admin settings title page.
*/
return apply_filters( 'learndash_admin_page_title', '<h1>' . esc_html( get_admin_page_title() ) . '</h1>' );
} else {
return '<h1 class="learndash-empty-page-title"></h1>';
}
}
/**
* Class utility function to return the form wrapper. Supports
* the beginning <form> an ending </form>.
*
* @since 2.5.0
*
* @param boolean $start Flag to indicate if showing start or end of form.
*
* @return string form HTML.
*/
public function get_admin_page_form( $start = true ) {
if ( true === $this->settings_form_wrap ) {
if ( true === $start ) {
/**
* Filters the HTML output for admin page form.
*
* @param string $form_output The HTML output for admin page form.
* @param boolean $start A flag to indicate whether it is start or end of the form.
*/
return apply_filters( 'learndash_admin_page_form', '<form id="learndash-settings-page-form" method="post" action="options.php">', $start );
} else {
/** This filter is documented in includes/settings/class-ld-settings-pages.php */
return apply_filters( 'learndash_admin_page_form', '</form>', $start );
}
}
return '';
}
/**
* Get the Sections associated with the current page.
*
* @since 3.6.0
*
* @return array array of Sections for the current page.
*/
public function get_settings_sections() {
if ( is_null( $this->page_sections ) ) {
$this->page_sections = LearnDash_Settings_Section::get_all_sections_by( 'settings_page_id', $this->settings_page_id );
}
return $this->page_sections;
}
/**
* Show the Page sections sub menu
*
* @since 3.6.0
*/
public function show_settings_sections_sub_menu() {
if ( true === $this->settings_metabox_as_sub ) {
$page_sections = LearnDash_Settings_Section::get_all_sections_by( 'settings_page_id', $this->settings_page_id, null, 'settings_section_key' );
if ( ! empty( $page_sections ) ) {
$current_sub = $this->get_current_settings_section_as_sub();
$q_links = array();
foreach ( $page_sections as $section_id => $section ) {
$q_links[ $section_id ] = array(
'url' => add_query_arg( 'section-advanced', esc_attr( $section_id ) ),
'label' => $section->get_settings_section_sub_label(),
);
}
}
if ( ! empty( $q_links ) ) {
echo '<div id="learndash-settings-sub-wrap" class="wrap">';
echo '<ul id="learndash-settings-sub-advanced" class="learndash-settings-sub subsubsub">';
if ( ! empty( $q_links ) ) {
$links = '';
foreach ( $q_links as $q_link_key => $q_link ) {
if ( ! empty( $links ) ) {
$links .= ' | ';
}
$q_link_class = '';
if ( $current_sub === $q_link_key ) {
$q_link_class = ' class="learndash-settings-sub-current" ';
}
$links .= '<li' . $q_link_class . '><a href="' . esc_url( $q_link['url'] ) . '" >' . esc_html( $q_link['label'] ) . '</a></li>';
}
echo wp_kses_post( $links );
}
echo '</ul>';
}
}
}
/**
* Get the current Settings Section when using section-as-sub logic.
*
* See `$settings_metabox_as_sub` class variable.
*
* @since 3.6.0
*/
protected function get_current_settings_section_as_sub() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ( isset( $_GET['section-advanced'] ) ) && ( ! empty( $_GET['section-advanced'] ) ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$this->settings_metabox_as_sub_current = esc_attr( $_GET['section-advanced'] );
}
$page_sections = LearnDash_Settings_Section::get_all_sections_by( 'settings_page_id', $this->settings_page_id, null, 'settings_section_key' );
if ( ! empty( $page_sections ) ) {
if ( ! isset( $page_sections[ $this->settings_metabox_as_sub_current ] ) ) {
foreach ( $page_sections as $key => $section ) {
$this->settings_metabox_as_sub_current = $key;
break;
}
}
} else {
$this->settings_metabox_as_sub_current = null;
}
return $this->settings_metabox_as_sub_current;
}
/**
* Function to check if metabox should be shown.
*
* Called from filter `learndash_show_metabox` to check if metabox should be shown
* on page. This is called just before the `add_meta_box()` function.
*
* @since 3.6.0
*
* @param bool $show Default is true.
* @param string $section_key The section key to be added/shown.
* @param string $settings_screen_id Screen ID.
*/
public function should_show_settings_section( $show, $section_key, $settings_screen_id ) {
if ( $settings_screen_id === $this->settings_screen_id ) {
if ( true === $show ) {
if ( ( ! empty( $section_key ) ) && ( $section_key !== $this->get_current_settings_section_as_sub() ) ) {
$show = false;
}
}
}
return $show;
}
/**
* Filter function to override default settings save user capability 'manage_options'.
*
* @since 3.6.1
*
* @param string $capability User capability to save options page.
* @return string.
*/
public function filter_option_page_capability( $capability ) {
if ( defined( 'LEARNDASH_ADMIN_CAPABILITY_CHECK' ) && LEARNDASH_ADMIN_CAPABILITY_CHECK ) {
$capability = LEARNDASH_ADMIN_CAPABILITY_CHECK;
}
return $capability;
}
// End of functions.
}
}
/**
* Enqueues the settings page styles and scripts.
*
* @since 3.0.0
*
* @global array $learndash_assets_loaded An array of loaded assets.
*/
function learndash_admin_settings_page_assets() {
global $learndash_assets_loaded;
if ( learndash_use_select2_lib() ) {
if ( ! isset( $learndash_assets_loaded['styles']['learndash-select2-jquery-style'] ) ) {
wp_enqueue_style(
'learndash-select2-jquery-style',
LEARNDASH_LMS_PLUGIN_URL . 'assets/vendor-libs/select2-jquery/css/select2.min.css',
array(),
LEARNDASH_SCRIPT_VERSION_TOKEN
);
$learndash_assets_loaded['styles']['learndash-select2-jquery-style'] = __FUNCTION__;
}
if ( ! isset( $learndash_assets_loaded['scripts']['learndash-select2-jquery-script'] ) ) {
wp_enqueue_script(
'learndash-select2-jquery-script',
LEARNDASH_LMS_PLUGIN_URL . 'assets/vendor-libs/select2-jquery/js/select2.full.min.js',
array( 'jquery' ),
LEARNDASH_SCRIPT_VERSION_TOKEN,
true
);
$learndash_assets_loaded['scripts']['learndash-select2-jquery-script'] = __FUNCTION__;
}
}
if ( ! isset( $learndash_assets_loaded['styles']['learndash-admin-settings-page'] ) ) {
wp_enqueue_style(
'learndash-admin-settings-page',
LEARNDASH_LMS_PLUGIN_URL . 'assets/css/learndash-admin-settings-page' . learndash_min_asset() . '.css',
array(),
LEARNDASH_SCRIPT_VERSION_TOKEN
);
wp_style_add_data( 'learndash-admin-settings-page', 'rtl', 'replace' );
$learndash_assets_loaded['styles']['learndash-admin-settings-page'] = __FUNCTION__;
}
if ( ! isset( $learndash_assets_loaded['scripts']['learndash-admin-settings-page'] ) ) {
wp_enqueue_script(
'learndash-admin-settings-page',
LEARNDASH_LMS_PLUGIN_URL . 'assets/js/learndash-admin-settings-page' . learndash_min_asset() . '.js',
array( 'jquery', 'wp-color-picker' ),
LEARNDASH_SCRIPT_VERSION_TOKEN,
true
);
$learndash_assets_loaded['scripts']['learndash-admin-settings-page'] = __FUNCTION__;
$script_data = array();
$script_data['themes_inheriting_settings'] = array();
foreach ( LearnDash_Theme_Register::get_themes() as $theme ) {
$theme_instance = LearnDash_Theme_Register::get_theme_instance( $theme['theme_key'] );
if ( ! $theme_instance ) {
continue;
}
foreach ( $theme_instance->get_themes_inheriting_settings() as $child_theme_key ) {
$script_data['themes_inheriting_settings'][ $child_theme_key ] = $theme['theme_key'];
}
}
$script_data['ajaxurl'] = admin_url( 'admin-ajax.php' );
$script_data['admin_notice_settings_fields_errors_container'] = '<div id="learndash-settings-fields-notice-errors" class="learndash-settings-fields-notice-errors notice notice-error"><p class="errors-header">' . esc_html__( 'You have errors on the following settings', 'learndash' ) . '</p><ul class="errors-list"></ul></div>';
$script_data['selec2_language'] = array(
'loadingMore' => esc_html__( 'Loading more results…', 'learndash' ),
'noResults' => esc_html__( 'No results found', 'learndash' ),
'searching' => esc_html__( 'Searching…', 'learndash' ),
'removeAllItems' => esc_html__( 'Remove all items', 'learndash' ),
'removeItem' => esc_html__( 'Remove item', 'learndash' ),
);
$script_data['settings_fields_errors'] = array(
'number' => array(
'invalid' => esc_html__( 'Entered value is invalid.', 'learndash' ),
'empty' => esc_html__( 'Value cannot be empty.', 'learndash' ),
'negative' => esc_html__( 'Entered value cannot be negative.', 'learndash' ),
'decimal' => esc_html__( 'Entered value cannot contain decimal.', 'learndash' ),
'minimum' => esc_html__( 'Entered value less than minimum.', 'learndash' ),
'maximum' => esc_html__( 'Entered value greater than maximum.', 'learndash' ),
),
);
/**
* Filters admin settings script data.
*
* @param array $script_data An array of script data to be localized.
*/
$script_data = apply_filters( 'learndash_admin_settings_data', $script_data );
if ( ( empty( $script_data ) ) || ( ! is_array( $script_data ) ) ) {
$script_data = array();
}
$script_data = array( 'json' => wp_json_encode( $script_data ) );
wp_localize_script( 'learndash-admin-settings-page', 'learndash_admin_settings_data', $script_data );
}
}
add_action(
'wp_ajax_learndash_settings_select2_query',
function() {
$result = array(
'items' => array(),
'total_items' => 0,
'page' => 1,
'total_pages' => 1,
);
if ( ! current_user_can( 'read' ) || empty( $_POST['query_data'] ) ) {
echo wp_json_encode( $result );
wp_die();
}
$post_query_data = wp_unslash( $_POST['query_data'] );
if ( empty( $post_query_data['nonce'] ) ) {
echo wp_json_encode( $result );
wp_die();
}
$post_query_data_nonce = $post_query_data['nonce'];
unset( $post_query_data['nonce'] );
if ( empty( $post_query_data['settings_element'] ) ) {
echo wp_json_encode( $result );
wp_die();
}
$post_query_data_json = wp_json_encode( $post_query_data['settings_element'], JSON_FORCE_OBJECT );
if ( ! wp_verify_nonce( $post_query_data_nonce, $post_query_data_json ) ) {
echo wp_json_encode( $result );
wp_die();
}
if ( empty( $post_query_data['query_args'] ) ) {
echo wp_json_encode( $result );
wp_die();
}
$query_args = $post_query_data['query_args'];
$query_args['paged'] = 1;
if ( isset( $_POST['page'] ) ) {
$query_args['paged'] = absint( $_POST['page'] );
}
if ( empty( $query_args['paged'] ) ) {
$query_args['paged'] = 1;
}
if ( ! empty( $_POST['search'] ) ) {
$query_args['s'] = sanitize_text_field( wp_unslash( $_POST['search'] ) );
}
if ( isset( $query_args['posts_per_page'] ) ) {
$query_args['posts_per_page'] = absint( $query_args['posts_per_page'] );
}
if ( empty( $query_args['posts_per_page'] ) ) {
$query_args['posts_per_page'] = 10;
}
if ( ! isset( $query_args['order'] ) ) {
$query_args['order'] = 'ASC';
}
if ( ! isset( $query_args['orderby'] ) ) {
$query_args['orderby'] = 'title';
}
$include_selected = 0;
if ( ( isset( $query_args['ld_include_selected'] ) ) && ( ! empty( $query_args['ld_include_selected'] ) ) ) {
$include_selected = absint( $query_args['ld_include_selected'] );
}
$query_results = new WP_Query( $query_args );
if ( is_a( $query_results, 'WP_Query' ) ) {
$result['items'] = array();
if ( ! empty( $include_selected ) && 1 === $query_args['paged'] ) {
$title = sprintf(
// translators: placeholder: post title.
esc_html_x( '%s (current selection)', 'placeholder: post title', 'learndash' ),
get_the_title( $include_selected )
);
if ( ! empty( $title ) ) {
$result['items'][] = array(
'id' => $include_selected,
'text' => $title,
);
}
}
if (
property_exists( $query_results, 'posts' ) &&
is_array( $query_results->posts ) &&
! empty( $query_results->posts )
) {
foreach ( $query_results->posts as $item ) {
$result['items'][] = array(
'id' => $item->ID,
'text' => html_entity_decode( learndash_format_step_post_title_with_status_label( $item ) ),
);
}
}
if ( property_exists( $query_results, 'found_posts' ) ) {
$result['total_items'] = absint( $query_results->found_posts );
}
if ( property_exists( $query_results, 'max_num_pages' ) ) {
$result['total_pages'] = absint( $query_results->max_num_pages );
}
$result['page'] = absint( $query_args['paged'] );
}
echo wp_json_encode( $result );
wp_die();
},
10
);