class-ld-settings-section-groups-cpt.php
10.6 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
<?php
/**
* LearnDash Settings Section for Groups Custom Post Type Metabox.
*
* @since 3.2.0
* @package LearnDash\Settings\Sections
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( class_exists( 'LearnDash_Settings_Section' ) ) && ( ! class_exists( 'LearnDash_Settings_Groups_CPT' ) ) ) {
/**
* Class LearnDash Settings Section for Groups Custom Post Type Metabox.
*
* @since 3.2.0
*/
class LearnDash_Settings_Groups_CPT extends LearnDash_Settings_Section {
/**
* Protected constructor for class
*
* @since 3.2.0
*/
protected function __construct() {
// What screen ID are we showing on.
$this->settings_screen_id = 'groups_page_groups-options';
// The page ID (different than the screen ID).
$this->settings_page_id = 'groups-options';
// This is the 'option_name' key used in the wp_options table.
$this->setting_option_key = 'learndash_settings_groups_cpt';
// This is the HTML form field prefix used.
$this->setting_field_prefix = 'learndash_settings_groups_cpt';
// Used within the Settings API to uniquely identify this section.
$this->settings_section_key = 'cpt_options';
// Section label/header.
$this->settings_section_label = sprintf(
// translators: placeholder: Group.
esc_html_x( '%s Custom Post Type Options', 'Group Custom Post Type Options', 'learndash' ),
learndash_get_custom_label( 'group' )
);
// Used to show the section description above the fields. Can be empty.
$this->settings_section_description = sprintf(
// translators: placeholder: Group.
esc_html_x( 'Control the LearnDash %s Custom Post Type Options.', 'placeholder: Group', 'learndash' ),
learndash_get_custom_label( 'group' )
);
add_filter( 'learndash_settings_row_outside_before', array( $this, 'learndash_settings_row_outside_before' ), 30, 2 );
parent::__construct();
}
/**
* Initialize the metabox settings values.
*
* @since 3.2.0
*/
public function load_settings_values() {
parent::load_settings_values();
if ( ( false === $this->setting_option_values ) || ( '' === $this->setting_option_values ) ) {
if ( '' === $this->setting_option_values ) {
$this->setting_option_values = array();
}
$ld_prior_version = learndash_data_upgrades_setting( 'prior_version' );
$this->setting_option_values = array(
'public' => ( 'new' === $ld_prior_version ) ? 'yes' : '',
'include_in_search' => 'yes',
'has_archive' => 'yes',
'has_feed' => '',
'supports' => array( 'thumbnail', 'revisions' ),
);
}
if ( ! isset( $this->setting_option_values['public'] ) ) {
$this->setting_option_values['public'] = '';
}
if ( ! isset( $this->setting_option_values['include_in_search'] ) ) {
if ( ( isset( $this->setting_option_values['exclude_from_search'] ) ) && ( 'yes' === $this->setting_option_values['exclude_from_search'] ) ) {
$this->setting_option_values['include_in_search'] = '';
} else {
$this->setting_option_values['include_in_search'] = 'yes';
}
}
if ( ! isset( $this->setting_option_values['has_archive'] ) ) {
$this->setting_option_values['has_archive'] = 'yes';
}
if ( ! isset( $this->setting_option_values['has_feed'] ) ) {
$this->setting_option_values['has_feed'] = '';
}
if ( ! isset( $this->setting_option_values['supports'] ) ) {
$this->setting_option_values['supports'] = array( 'thumbnail', 'revisions' );
}
}
/**
* Initialize the metabox settings fields.
*
* @since 3.2.0
*/
public function load_settings_fields() {
$cpt_archive_url = home_url( LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_Permalinks', 'groups' ) );
$cpt_rss_url = add_query_arg( 'post_type', 'sfwd-courses', get_post_type_archive_feed_link( 'post' ) );
$this->setting_option_fields = array(
'public' => array(
'name' => 'public',
'type' => 'checkbox-switch',
'label' => esc_html__( 'Public', 'learndash' ),
'help_text' => sprintf(
// translators: placeholder: Groups.
esc_html_x( '%s are public on the front-end', 'placeholder: Groups', 'learndash' ),
learndash_get_custom_label( 'groups' )
),
'value' => $this->setting_option_values['public'],
'options' => array(
'yes' => '',
),
),
'include_in_search' => array(
'name' => 'include_in_search',
'type' => 'checkbox-switch',
'label' => sprintf(
// translators: placeholder: Group.
esc_html_x( '%s Search', 'placeholder: Group', 'learndash' ),
learndash_get_custom_label( 'group' )
),
'help_text' => sprintf(
// translators: placeholder: group.
esc_html_x( 'Includes the %s post type in front end search results', 'placeholder: group', 'learndash' ),
learndash_get_custom_label_lower( 'group' )
),
'value' => $this->setting_option_values['include_in_search'],
'options' => array(
'yes' => '',
),
),
'has_archive' => array(
'name' => 'has_archive',
'type' => 'checkbox-switch',
'label' => esc_html__( 'Archive Page', 'learndash' ),
'help_text' => sprintf(
// translators: placeholders: groups, link to WP Permalinks page.
esc_html_x( 'Enables the front end archive page where all %1$s are listed. You must %2$s for the change to take effect.', 'placeholders: groups, link to WP Permalinks page', 'learndash' ),
learndash_get_custom_label_lower( 'groups' ),
'<a href="' . admin_url( 'options-permalink.php' ) . '">' . esc_html__( 're-save your permalinks', 'learndash' ) . '</a>'
),
'value' => $this->setting_option_values['has_archive'],
'options' => array(
'' => '',
'yes' => sprintf(
// translators: placeholder: URL for CPT Archive.
esc_html_x( 'Archive URL: %s', 'placeholder: URL for CPT Archive', 'learndash' ),
'<code><a target="blank" href="' . $cpt_archive_url . '">' . $cpt_archive_url . '</a></code>'
),
),
'child_section_state' => ( 'yes' === $this->setting_option_values['has_archive'] ) ? 'open' : 'closed',
),
'has_feed' => array(
'name' => 'has_feed',
'type' => 'checkbox-switch',
'label' => esc_html__( 'RSS/Atom Feed', 'learndash' ),
'help_text' => sprintf(
// translators: placeholder: group.
esc_html_x( 'Enables an RSS feed for all %s posts.', 'placeholder: group', 'learndash' ),
learndash_get_custom_label_lower( 'group' )
),
'value' => $this->setting_option_values['has_feed'],
'options' => array(
'' => '',
'yes' => sprintf(
// translators: placeholder: URL for CPT Archive.
esc_html_x( 'RSS Feed URL: %s', 'placeholder: URL for RSS Feed', 'learndash' ),
'<code><a target="blank" href="' . $cpt_rss_url . '">' . $cpt_rss_url . '</a></code>'
),
),
'parent_setting' => 'has_archive',
),
'supports' => array(
'name' => 'supports',
'type' => 'checkbox',
'label' => esc_html__( 'Editor Supported Settings', 'learndash' ),
'help_text' => esc_html__( 'Enables WordPress supported settings within the editor and theme.', 'learndash' ),
'value' => $this->setting_option_values['supports'],
'options' => array(
'thumbnail' => esc_html__( 'Featured image', 'learndash' ),
'comments' => esc_html__( 'Comments', 'learndash' ),
'custom-fields' => esc_html__( 'Custom Fields', 'learndash' ),
'revisions' => esc_html__( 'Revisions', 'learndash' ),
),
),
);
/** 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();
}
/**
* Intercept the WP options save logic and check that we have a valid nonce.
*
* @since 3.2.0
*
* @param array $new_values Array of section fields values.
* @param array $old_values Array of old values.
* @param string $setting_option_key Section option key should match $this->setting_option_key.
*/
public function section_pre_update_option( $new_values = '', $old_values = '', $setting_option_key = '' ) {
if ( $setting_option_key === $this->setting_option_key ) {
$new_values = parent::section_pre_update_option( $new_values, $old_values, $setting_option_key );
if ( ! isset( $new_values['public'] ) ) {
$new_values['public'] = '';
}
if ( ! isset( $new_values['include_in_search'] ) ) {
$new_values['include_in_search'] = '';
}
if ( ! isset( $new_values['has_archive'] ) ) {
$new_values['has_archive'] = '';
$new_values['has_feed'] = '';
}
if ( ! isset( $new_values['has_feed'] ) ) {
$new_values['has_feed'] = '';
}
if ( ! isset( $new_values['supports'] ) ) {
$new_values['supports'] = array();
}
if ( 'yes' !== $new_values['public'] ) {
$new_values['include_in_search'] = '';
$new_values['has_archive'] = '';
$new_values['has_feed'] = '';
}
if ( $new_values !== $old_values ) {
if ( ( ! isset( $old_values['has_archive'] ) ) || ( $new_values['has_archive'] !== $old_values['has_archive'] ) ) {
learndash_setup_rewrite_flush();
}
}
}
return $new_values;
}
/**
* Settings row outside before
*
* @since 3.2.0
*
* @param string $content Content to show before row.
* @param array $field_args Row field Args.
*/
public function learndash_settings_row_outside_before( $content = '', $field_args = array() ) {
if ( ( isset( $field_args['name'] ) ) && ( in_array( $field_args['name'], array( 'public' ), true ) ) && LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Groups_CPT', 'public' ) === '' ) {
$content .= '<div class="ld-settings-info-banner ld-settings-info-banner-alert">';
$message = sprintf(
// translators: placeholder: group.
esc_html_x(
'%s is not set to public, set to Public to allow access on the front end.',
'placeholder: group',
'learndash'
),
learndash_get_custom_label( 'group' )
);
$content .= wpautop( wptexturize( do_shortcode( $message ) ) );
$content .= '</div>';
}
return $content;
}
// End of functions.
}
}
add_action(
'learndash_settings_sections_init',
function() {
LearnDash_Settings_Groups_CPT::add_section_instance();
}
);