class-ld-settings-section-permalinks.php
13.7 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
<?php
/**
* LearnDash Settings Section for Permalinks section shown on WP Settings > Permalinks page.
*
* @since 2.4.0
* @package LearnDash\Settings\Sections
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( class_exists( 'LearnDash_Settings_Section' ) ) && ( ! class_exists( 'LearnDash_Settings_Section_Permalinks' ) ) ) {
/**
* Class LearnDash Settings Section for Permalinks section shown on WP Settings > Permalinks page.
*
* @since 2.4.0
*/
class LearnDash_Settings_Section_Permalinks extends LearnDash_Settings_Section {
/**
* Protected constructor for class
*
* @since 2.4.0
*/
protected function __construct() {
$this->settings_page_id = 'permalink';
// This is the 'option_name' key used in the wp_options table.
$this->setting_option_key = 'learndash_settings_permalinks';
// This is the HTML form field prefix used.
$this->setting_field_prefix = 'learndash_settings_permalinks';
// Used within the Settings API to uniquely identify this section.
$this->settings_section_key = 'learndash_settings_permalinks';
// Section label/header.
$this->settings_section_label = __( 'LearnDash Permalinks', 'learndash' );
// Used to show the section description above the fields. Can be empty.
$this->settings_section_description = __( 'Controls the URL slugs for the custom posts used by LearnDash.', 'learndash' );
add_action( 'admin_init', array( $this, 'admin_init' ) );
parent::__construct();
$this->save_settings_fields();
}
/**
* Hook into the admin init action to fire up the LD settings page init processing.
* Remember the Permalinks page is not a LD page.
*
* @since 2.4.0
*/
public function admin_init() {
/** This filter is documented in includes/settings/class-ld-settings-pages.php */
do_action( 'learndash_settings_page_init', $this->settings_page_id );
}
/**
* Function to handle metabox init.
*
* @since 2.4.0
*
* @param string $settings_screen_id Screen ID of current page.
*/
public function add_meta_boxes( $settings_screen_id = '' ) {
global $wp_rewrite;
if ( $wp_rewrite->using_permalinks() ) {
add_meta_box(
$this->metabox_key,
$this->settings_section_label,
array( $this, 'show_meta_box' ),
$this->settings_screen_id,
$this->metabox_context,
$this->metabox_priority
);
}
}
/**
* Initialize the metabox settings values.
*
* @since 2.4.0
*/
public function load_settings_values() {
parent::load_settings_values();
if ( false === $this->setting_option_values ) {
$this->setting_option_values = array();
// On the initial if we don't have saved values we grab them from the Custom Labels.
$custom_label_settings = get_option( 'learndash_custom_label_settings', array() );
if ( ( isset( $custom_label_settings['courses'] ) ) && ( ! empty( $custom_label_settings['courses'] ) ) ) {
$this->setting_option_values['courses'] = learndash_get_custom_label_slug( 'courses' );
} else {
$this->setting_option_values['courses'] = 'courses';
}
if ( ( isset( $custom_label_settings['lessons'] ) ) && ( ! empty( $custom_label_settings['lessons'] ) ) ) {
$this->setting_option_values['lessons'] = learndash_get_custom_label_slug( 'lessons' );
} else {
$this->setting_option_values['lessons'] = 'lessons';
}
if ( ( isset( $custom_label_settings['topic'] ) ) && ( ! empty( $custom_label_settings['topic'] ) ) ) {
$this->setting_option_values['topics'] = learndash_get_custom_label_slug( 'topic' );
} else {
$this->setting_option_values['topics'] = 'topics';
}
if ( ( isset( $custom_label_settings['quizzes'] ) ) && ( ! empty( $custom_label_settings['quizzes'] ) ) ) {
$this->setting_option_values['quizzes'] = learndash_get_custom_label_slug( 'quizzes' );
} else {
$this->setting_option_values['quizzes'] = 'quizzes';
}
if ( ( isset( $custom_label_settings['groups'] ) ) && ( ! empty( $custom_label_settings['groups'] ) ) ) {
$this->setting_option_values['groups'] = learndash_get_custom_label_slug( 'groups' );
} else {
$this->setting_option_values['groups'] = 'groups';
}
$this->settings_bypass_nonce_check = true;
// As we don't have existing values we want to save here and force the flush rewrite.
update_option( $this->settings_section_key, $this->setting_option_values );
learndash_setup_rewrite_flush();
}
$this->setting_option_values = wp_parse_args(
$this->setting_option_values,
array(
'courses' => 'courses',
'lessons' => 'lessons',
'topics' => 'topic',
'quizzes' => 'quizzes',
'groups' => 'groups',
'nested_urls' => '',
)
);
if ( ( learndash_is_course_shared_steps_enabled() ) && ( 'yes' !== $this->setting_option_values['nested_urls'] ) ) {
$this->setting_option_values['nested_urls'] = 'yes';
update_option( $this->settings_section_key, $this->setting_option_values );
learndash_setup_rewrite_flush();
}
}
/**
* Initialize the metabox settings fields.
*
* @since 2.4.0
*/
public function load_settings_fields() {
global $wp_rewrite;
if ( $wp_rewrite->using_permalinks() ) {
$this->setting_option_fields = array(
'courses' => array(
'name' => 'courses',
'type' => 'text',
'label' => LearnDash_Custom_Label::get_label( 'courses' ),
'value' => $this->setting_option_values['courses'],
'class' => 'regular-text',
),
'lessons' => array(
'name' => 'lessons',
'type' => 'text',
'label' => LearnDash_Custom_Label::get_label( 'lessons' ),
'value' => $this->setting_option_values['lessons'],
'class' => 'regular-text',
),
'topics' => array(
'name' => 'topics',
'type' => 'text',
'label' => LearnDash_Custom_Label::get_label( 'topics' ),
'value' => $this->setting_option_values['topics'],
'class' => 'regular-text',
),
'quizzes' => array(
'name' => 'quizzes',
'type' => 'text',
'label' => LearnDash_Custom_Label::get_label( 'quizzes' ),
'value' => $this->setting_option_values['quizzes'],
'class' => 'regular-text',
),
'groups' => array(
'name' => 'groups',
'type' => 'text',
'label' => LearnDash_Custom_Label::get_label( 'groups' ),
'value' => $this->setting_option_values['groups'],
'class' => 'regular-text',
),
);
}
if ( $wp_rewrite->using_permalinks() ) {
$example_regular_topic_url = get_option( 'home' ) . '/' . $this->setting_option_values['topics'] . '/topic-slug';
$example_nested_topic_url = get_option( 'home' ) . '/' . $this->setting_option_values['courses'] . '/course-slug/' . $this->setting_option_values['lessons'] . '/lesson-slug/' . $this->setting_option_values['topics'] . '/topic-slug';
} else {
$example_regular_topic_url = add_query_arg( learndash_get_post_type_slug( 'topic' ), 'topic-slug', get_option( 'home' ) );
$example_nested_topic_url = get_option( 'home' );
$example_nested_topic_url = add_query_arg( learndash_get_post_type_slug( 'course' ), 'course-slug', $example_nested_topic_url );
$example_nested_topic_url = add_query_arg( learndash_get_post_type_slug( 'lesson' ), 'lesson-slug', $example_nested_topic_url );
$example_nested_topic_url = add_query_arg( learndash_get_post_type_slug( 'topic' ), 'topic-slug', $example_nested_topic_url );
}
$this->setting_option_fields['nested_urls'] = array(
'name' => 'nested_urls',
'type' => 'checkbox',
'label' => __( 'Enable Nested URLs', 'learndash' ),
'desc' => wp_kses_post(
sprintf(
// translators: placeholders: Lesson, Topic, Quiz, Course, topic, Site Home URL, URL to Course Builder Settings.
_x(
'This option will restructure %1$s, %2$s and %3$s URLs so they are nested hierarchically within the %4$s URL.<br />For example instead of the default %5$s URL <code>%6$s</code> the nested URL would be <code>%7$s</code>. If <a href="%7$s">Course Builder Share Steps</a> has been enabled this setting is also automatically enabled.',
'placeholders: Lesson, Topic, Quiz, Course, topic, Site Home URL, URL to Course Builder Settings',
'learndash'
),
learndash_get_custom_label( 'lesson' ),
learndash_get_custom_label( 'topic' ),
learndash_get_custom_label( 'quiz' ),
learndash_get_custom_label( 'course' ),
learndash_get_custom_label_lower( 'topic' ),
$example_regular_topic_url,
$example_nested_topic_url,
admin_url( 'admin.php?page=courses-options' )
)
),
'value' => isset( $this->setting_option_values['nested_urls'] ) ? $this->setting_option_values['nested_urls'] : '',
'options' => array(
'yes' => __( 'Yes', 'learndash' ),
),
);
$this->setting_option_fields['nonce'] = array(
'name' => 'nonce',
'type' => 'hidden',
'label' => '',
'value' => wp_create_nonce( 'learndash_permalinks_nonce' ),
'class' => 'hidden',
);
/** 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();
}
/**
* Save the metabox fields. This is needed due to special processing needs.
*
* @since 2.4.0
*/
public function save_settings_fields() {
if ( isset( $_POST[ $this->setting_field_prefix ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( $this->verify_metabox_nonce_field() ) {
$post_fields = $_POST[ $this->setting_field_prefix ]; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( ( isset( $post_fields['courses'] ) ) && ( ! empty( $post_fields['courses'] ) ) ) {
$this->setting_option_values['courses'] = $this->esc_url( $post_fields['courses'] );
learndash_setup_rewrite_flush();
}
if ( ( isset( $post_fields['lessons'] ) ) && ( ! empty( $post_fields['lessons'] ) ) ) {
$this->setting_option_values['lessons'] = $this->esc_url( $post_fields['lessons'] );
learndash_setup_rewrite_flush();
}
if ( ( isset( $post_fields['topics'] ) ) && ( ! empty( $post_fields['topics'] ) ) ) {
$this->setting_option_values['topics'] = $this->esc_url( $post_fields['topics'] );
learndash_setup_rewrite_flush();
}
if ( ( isset( $post_fields['quizzes'] ) ) && ( ! empty( $post_fields['quizzes'] ) ) ) {
$this->setting_option_values['quizzes'] = $this->esc_url( $post_fields['quizzes'] );
learndash_setup_rewrite_flush();
}
if ( ( isset( $post_fields['groups'] ) ) && ( ! empty( $post_fields['groups'] ) ) ) {
$this->setting_option_values['groups'] = $this->esc_url( $post_fields['groups'] );
learndash_setup_rewrite_flush();
}
if ( ( isset( $post_fields['nested_urls'] ) ) && ( ! empty( $post_fields['nested_urls'] ) ) ) {
$this->setting_option_values['nested_urls'] = $this->esc_url( $post_fields['nested_urls'] );
learndash_setup_rewrite_flush();
} else {
// We check the Course Options > Course Builder setting. If this is set to 'yes' then we MUST keep the nested URLs set to true.
if ( ! isset( $this->setting_option_values['nested_urls'] ) ) {
$this->setting_option_values['nested_urls'] = 'no';
}
if ( 'yes' !== $this->setting_option_values['nested_urls'] ) {
$learndash_settings_courses_builder = get_option( 'learndash_settings_courses_management_display', array() );
if ( ! isset( $learndash_settings_courses_builder['course_builder_shared_steps'] ) ) {
$learndash_settings_courses_builder['course_builder_shared_steps'] = 'no';
}
if ( 'yes' === $learndash_settings_courses_builder['course_builder_shared_steps'] ) {
$this->setting_option_values['nested_urls'] = 'yes';
learndash_setup_rewrite_flush();
}
}
}
update_option( $this->settings_section_key, $this->setting_option_values );
}
}
}
/**
* Class utility function to escape the URL
*
* @since 2.4.0
*
* @param string $value URL to Escape.
*
* @return string filtered URL.
*/
public function esc_url( $value = '' ) {
if ( ! empty( $value ) ) {
$value = esc_url_raw( trim( $value ) );
$value = str_replace( 'http://', '', $value );
return untrailingslashit( $value );
}
return '';
}
/**
* Verify Settings Section nonce field POST value.
*
* @since 3.6.0.1
*/
public function verify_metabox_nonce_field() {
if ( ( true === $this->settings_bypass_nonce_check ) || ( ( isset( $_POST[ $this->setting_field_prefix ]['nonce'] ) ) && ( wp_verify_nonce( $_POST[ $this->setting_field_prefix ]['nonce'], 'learndash_permalinks_nonce' ) ) ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
return true;
}
return false;
}
/**
* Show Settings Section Description
*
* @since 3.6.0.1
*/
public function show_settings_section_description() {
if ( ! empty( $this->settings_section_description ) ) {
echo wp_kses_post( wpautop( $this->settings_section_description ) );
}
}
// End of functions.
}
}
add_action(
'learndash_settings_sections_init',
function() {
LearnDash_Settings_Section_Permalinks::add_section_instance();
}
);