class-ld-settings-section-general-per-page.php
8.48 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
<?php
/**
* LearnDash Settings Section for Per Page Metabox.
*
* @since 2.5.5
* @package LearnDash\Settings\Sections
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( class_exists( 'LearnDash_Settings_Section' ) ) && ( ! class_exists( 'LearnDash_Settings_Section_General_Per_Page' ) ) ) {
/**
* Class LearnDash Settings Section for Per Page Metabox.
*
* @since 2.5.5
*/
class LearnDash_Settings_Section_General_Per_Page extends LearnDash_Settings_Section {
/**
* Protected constructor for class
*
* @since 2.5.5
*/
protected function __construct() {
$this->settings_page_id = 'learndash_lms_settings';
// This is the 'option_name' key used in the wp_options table.
$this->setting_option_key = 'learndash_settings_per_page';
// This is the HTML form field prefix used.
$this->setting_field_prefix = 'learndash_settings_per_page';
// Used within the Settings API to uniquely identify this section.
$this->settings_section_key = 'settings_per_page';
// Section label/header.
$this->settings_section_label = esc_html__( 'Global Pagination Settings', 'learndash' );
$this->settings_section_description = esc_html__( 'Specify the default number of items displayed per page for various listing outputs.', 'learndash' );
parent::__construct();
}
/**
* Initialize the metabox settings values.
*
* @since 2.5.5
*/
public function load_settings_values() {
parent::load_settings_values();
if ( ! isset( $this->setting_option_values['progress_num'] ) ) {
$this->setting_option_values['progress_num'] = LEARNDASH_LMS_DEFAULT_WIDGET_PER_PAGE;
}
if ( ! isset( $this->setting_option_values['quiz_num'] ) ) {
$this->setting_option_values['quiz_num'] = LEARNDASH_LMS_DEFAULT_WIDGET_PER_PAGE;
}
if ( ( LEARNDASH_LMS_DEFAULT_WIDGET_PER_PAGE === $this->setting_option_values['progress_num'] ) && ( LEARNDASH_LMS_DEFAULT_WIDGET_PER_PAGE === $this->setting_option_values['quiz_num'] ) ) {
$this->setting_option_values['profile_enabled'] = '';
} else {
$this->setting_option_values['profile_enabled'] = 'yes';
}
if ( ! isset( $this->setting_option_values['per_page'] ) ) {
$this->setting_option_values['per_page'] = LEARNDASH_LMS_DEFAULT_WIDGET_PER_PAGE;
}
if ( ! isset( $this->setting_option_values['question_num'] ) ) {
$this->setting_option_values['question_num'] = LEARNDASH_LMS_DEFAULT_WIDGET_PER_PAGE;
}
}
/**
* Validate settings field.
*
* @since 2.5.5
*
* @param string $val Value to be validated.
* @param string $key settings fields key.
* @param array $args Settings field args array.
*
* @return integer $val.
*/
public function validate_section_field_per_page( $val, $key, $args = array() ) {
// Get the digits only.
if ( ( isset( $args['field']['validate_args']['allow_empty'] ) ) && ( true === $args['field']['validate_args']['allow_empty'] ) ) {
$val = preg_replace( '/[^0-9]/', '', $val );
}
if ( '' === $val ) {
switch ( $key ) {
case 'per_page':
$val = LEARNDASH_LMS_DEFAULT_WIDGET_PER_PAGE;
break;
case 'progress_num':
$val = LEARNDASH_LMS_DEFAULT_WIDGET_PER_PAGE;
break;
case 'quiz_num':
$val = LEARNDASH_LMS_DEFAULT_WIDGET_PER_PAGE;
break;
case 'question_num':
$val = LEARNDASH_LMS_DEFAULT_WIDGET_PER_PAGE;
break;
}
}
// IF profile is NOT enabled we make sure to clear the quiz and progress values.
if ( ! isset( $args['post_fields']['profile_enabled'] ) ) {
if ( ( 'quiz_num' === $key ) || ( 'progress_num' === $key ) ) {
return LEARNDASH_LMS_DEFAULT_WIDGET_PER_PAGE;
}
}
return intval( $val );
}
/**
* Initialize the metabox settings fields.
*
* @since 2.5.5
*/
public function load_settings_fields() {
$this->setting_option_fields = array(
'profile_enabled' => array(
'name' => 'profile_enabled',
'type' => 'checkbox-switch',
'label' => esc_html__( 'WP Profile', 'learndash' ),
'help_text' => esc_html__( 'Controls the pagination for the WordPress Profile LearnDash elements.', 'learndash' ),
'value' => $this->setting_option_values['profile_enabled'],
'options' => array(
'yes' => '',
'' => sprintf(
// translators: placeholder: default per page number.
esc_html_x( 'Pagination defaults to %d', 'placeholder: default per page number', 'learndash' ),
LEARNDASH_LMS_DEFAULT_WIDGET_PER_PAGE
),
),
'child_section_state' => ( 'yes' === $this->setting_option_values['profile_enabled'] ) ? 'open' : 'closed',
),
'progress_num' => array(
'name' => 'progress_num',
'type' => 'number',
'label' => sprintf(
// translators: placeholder: Course.
esc_html_x( '%s Progress', 'placeholder: Course', 'learndash' ),
LearnDash_Custom_Label::get_label( 'course' )
),
'value' => $this->setting_option_values['progress_num'],
'attrs' => array(
'step' => 1,
'min' => 1,
),
'input_label' => sprintf(
// translators: placeholder: courses.
esc_html_x( '%s per page', 'placeholder: courses', 'learndash' ),
learndash_get_custom_label_lower( 'course' )
),
'validate_callback' => array( $this, 'validate_section_field_per_page' ),
'validate_args' => array(
'allow_empty' => 1,
),
'parent_setting' => 'profile_enabled',
),
'quiz_num' => array(
'name' => 'quiz_num',
'type' => 'number',
'label' => sprintf(
// translators: placeholder: Quiz.
esc_html_x( '%s Attempts', 'placeholder: Quiz.', 'learndash' ),
learndash_get_custom_label( 'quiz' )
),
'value' => $this->setting_option_values['quiz_num'],
'attrs' => array(
'step' => 1,
'min' => 1,
),
'input_label' => sprintf(
// translators: placeholder: quizzes.
esc_html_x( '%s per page', 'placeholder: quizzes', 'learndash' ),
learndash_get_custom_label_lower( 'quizzes' )
),
'validate_callback' => array( $this, 'validate_section_field_per_page' ),
'validate_args' => array(
'allow_empty' => 1,
),
'parent_setting' => 'profile_enabled',
),
'per_page' => array(
'name' => 'per_page',
'type' => 'number',
'label' => esc_html__( 'Shortcodes & Widgets', 'learndash' ),
'help_text' => esc_html__( 'Controls the global pagination for the LD shortcodes as well as courseinfo widget. These can be overridden individually.', 'learndash' ),
'value' => $this->setting_option_values['per_page'],
'attrs' => array(
'step' => 1,
'min' => 1,
),
'validate_callback' => array( $this, 'validate_section_field_per_page' ),
'validate_args' => array(
'allow_empty' => 1,
),
),
'question_num' => array(
'name' => 'question_num',
'type' => 'number',
'label' => sprintf(
// translators: placeholders: Question.
esc_html_x( 'Backend %s Widget', 'placeholder: Question', 'learndash' ),
LearnDash_Custom_Label::get_label( 'question' )
),
'help_text' => sprintf(
// translators: placeholders: Questions, quiz, question.
esc_html_x( 'Controls the pagination for the %1$s admin widget when editing a %2$s or %3$s.', 'Questions, quiz, question', 'learndash' ),
learndash_get_custom_label( 'questions' ),
learndash_get_custom_label_lower( 'quiz' ),
learndash_get_custom_label_lower( 'question' )
),
'value' => $this->setting_option_values['question_num'],
'attrs' => array(
'step' => 1,
'min' => 1,
),
'validate_callback' => array( $this, 'validate_section_field_per_page' ),
'validate_args' => array(
'allow_empty' => 1,
),
),
);
/** This filter is documented in includes/settings/settings-metaboxes/class-ld-settings-metabox-course-access-settings.php */
$this->setting_option_fields = apply_filters( 'learndash_settings_fields', $this->setting_option_fields, $this->settings_section_key );
parent::load_settings_fields();
}
}
}
add_action(
'learndash_settings_sections_init',
function() {
LearnDash_Settings_Section_General_Per_Page::add_section_instance();
}
);