class-ld-settings-section-emails-list.php
10.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
<?php
/**
* LearnDash Settings Section for Email List Metabox.
*
* @since 3.6.0
* @package LearnDash\Settings\Sections
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( class_exists( 'LearnDash_Settings_Section' ) ) && ( ! class_exists( 'LearnDash_Settings_Section_Emails_List' ) ) ) {
/**
* Class LearnDash Settings Section for Emails List Metabox.
*
* @since 3.6.0
*/
class LearnDash_Settings_Section_Emails_List extends LearnDash_Settings_Section {
/**
* Section URL Param
*
* @var string $section_url_param
*/
public static $section_url_param = 'section-email';
/**
* Current Section Shown
*
* @var string $current_section
*/
private $current_sub_section = '';
/**
* Related Sub-Sections
*
* @var array $sub_sections
*/
private $sub_sections = array();
/**
* Protected constructor for class
*
* @since 3.6.0
*/
protected function __construct() {
$this->settings_page_id = 'learndash_lms_emails';
// This is the 'option_name' key used in the wp_options table.
$this->setting_option_key = 'learndash_settings_emails_list';
// This is the HTML form field prefix used.
$this->setting_field_prefix = 'learndash_settings_emails_list';
// Used within the Settings API to uniquely identify this section.
$this->settings_section_key = 'settings_emails_list';
// Section label/header.
$this->settings_section_label = esc_html__( 'Email Notifications', 'learndash' );
add_action( 'learndash_settings_page_init', array( $this, 'learndash_settings_page_init' ), 10, 1 );
parent::__construct();
}
/**
* Show the Email Placeholders side section if we are editing an email.
*
* @since 3.6.0
*
* @param string $settings_page_id Settings page ID.
*/
public function learndash_settings_page_init( $settings_page_id ) {
if ( $settings_page_id === $this->settings_page_id ) {
$this->sub_sections = LearnDash_Settings_Section::get_all_sections_by( 'settings_parent_section_key', $this->settings_section_key );
if ( ! empty( $this->sub_sections ) ) {
add_filter( 'learndash_show_section', array( $this, 'should_show_settings_section' ), 10, 3 );
if ( ! empty( $this->get_current_sub_section() ) ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
// Add extra section/metaboxes as needed.
}
}
}
}
/**
* Function to check if section should be shown.
*
* Called from filter `learndash_show_section` to check if the section should be shown
* on page. This is called just before the `add_meta_box()` function.
*
* @since 3.6.0
*
* @param bool $show_section Default is true.
* @param string $section_key The settings section key to be shown.
* @param string $settings_screen_id The settings Screen ID.
*/
public function should_show_settings_section( $show_section, $section_key, $settings_screen_id ) {
if ( ( $settings_screen_id === $this->settings_screen_id ) && ( ! empty( $section_key ) ) ) {
$current_sub_section = $this->get_current_sub_section();
if ( ! empty( $current_sub_section ) ) {
if ( $section_key !== $current_sub_section ) {
$show_section = false;
}
} elseif ( in_array( $section_key, $this->get_sub_sections_keys(), true ) ) {
$show_section = false;
}
}
return $show_section;
}
/**
* Set the current viewed section.
*
* This is used to control the screen output.
*
* @since 3.6.0
*
* @return string Current section slug or empty.
*/
private function get_current_sub_section() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ( isset( $_GET[ self::$section_url_param ] ) ) && ( ! empty( $_GET[ self::$section_url_param ] ) ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$this->current_sub_section = esc_attr( $_GET[ self::$section_url_param ] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Recommended
if ( ! $this->is_valid_sub_section( $this->current_sub_section ) ) {
$this->current_sub_section = '';
}
}
return $this->current_sub_section;
}
/**
* Customer Show the meta box settings
*
* @since 3.6.0
*
* @param string $section Section to be shown.
*/
public function show_settings_section( $section = null ) {
if ( ! empty( $this->current_sub_section ) ) {
global $wp_settings_sections;
if ( isset( $wp_settings_sections[ $this->settings_page_id ][ $section ] ) ) {
parent::show_settings_section( $wp_settings_sections[ $this->settings_page_id ][ $section ] );
}
return;
}
$this->show_settings_section_nonce_field();
?>
<div class="sfwd sfwd_options">
<table class="learndash-settings-table learndash-settings-table-emails widefat striped" cellspacing="0">
<thead>
<tr>
<th class="col-name-enabled"></th>
<th class="col-name-email"><?php esc_html_e( 'Email', 'learndash' ); ?></th>
<th class="col-name-recipients"><?php esc_html_e( 'Recipient(s)', 'learndash' ); ?></th>
<th class="col-name-content-type"><?php esc_html_e( 'Content type', 'learndash' ); ?></th>
<th class="col-name-manage"></th>
<tr>
</thead>
<tbody>
<?php
if ( ! empty( $this->sub_sections ) ) {
foreach ( (array) $this->get_sub_sections_by_label_order() as $sub_section ) {
$sub_section_fields = $sub_section->setting_option_fields;
?>
<tr>
<td class="col-name-enabled col-valign-middle">
<?php
$sub_section->show_settings_section_nonce_field( false );
?>
<div class="sfwd_option_div">
<?php
if ( isset( $sub_section->setting_option_fields['enabled'] ) ) {
call_user_func( $sub_section->setting_option_fields['enabled']['display_callback'], $sub_section->setting_option_fields['enabled'] );
}
?>
</div>
</td>
<td class="col-name-email">
<?php
$listing_label = '';
if ( ( isset( $sub_section->settings_section_listing_label ) ) && ( ! empty( $sub_section->settings_section_listing_label ) ) ) {
$listing_label = $sub_section->settings_section_listing_label;
} elseif ( ( isset( $sub_section->settings_section_label ) ) && ( ! empty( $sub_section->settings_section_label ) ) ) {
$listing_label = $sub_section->settings_section_label;
}
if ( ! empty( $listing_label ) ) {
echo '<div class="learndash-listing_label"><strong><a href="' . esc_url( add_query_arg( self::$section_url_param, esc_attr( $sub_section->settings_section_key ) ) ) . '">' . esc_html( $listing_label ) . '</a></strong></div>';
}
if ( ( isset( $sub_section->settings_section_listing_description ) ) && ( ! empty( $sub_section->settings_section_listing_description ) ) ) {
echo '<div class="learndash-listing_description">' . wp_kses_post( $sub_section->settings_section_listing_description ) . '</div>';
}
?>
</td>
<td class="col-name-recipients">
<?php
if ( isset( $sub_section->setting_option_fields['recipients'] ) ) {
call_user_func( $sub_section->setting_option_fields['recipients']['display_callback'], $sub_section->setting_option_fields['recipients'] );
}
?>
</td>
<td class="col-name-content-type">
<?php
if ( isset( $sub_section->setting_option_fields['content_type'] ) ) {
if ( isset( $sub_section->setting_option_values['content_type'] ) ) {
$field_value = $sub_section->setting_option_values['content_type'];
if ( isset( $sub_section->setting_option_fields['content_type']['options'][ $field_value ] ) ) {
echo esc_html( $sub_section->setting_option_fields['content_type']['options'][ $field_value ] );
}
}
}
?>
</td>
<td class="col-name-manage col-valign-middle">
<a class="button alignright" href="<?php echo esc_url( add_query_arg( self::$section_url_param, esc_attr( $sub_section->settings_section_key ) ) ); ?>"><?php esc_html_e( 'Manage', 'learndash' ); ?></a>
</td>
</tr>
<?php
}
} else {
?>
<tr><td colspan="4"><?php esc_html_e( 'No Email items found.', 'learndash' ); ?></td></tr>
<?php
}
?>
</tbody>
</table>
</div>
<?php
}
/**
* Utility function to return the sub sections in label (alpha) order.
*
* @since 3.6.0
*/
private function get_sub_sections_by_label_order() {
$sub_sections = array();
if ( ! empty( $this->sub_sections ) ) {
foreach ( (array) $this->sub_sections as $sub_section ) {
$listing_label = '';
if ( ( isset( $sub_section->settings_section_listing_label ) ) && ( ! empty( $sub_section->settings_section_listing_label ) ) ) {
$listing_label = $sub_section->settings_section_listing_label;
} elseif ( ( isset( $sub_section->settings_section_label ) ) && ( ! empty( $sub_section->settings_section_label ) ) ) {
$listing_label = $sub_section->settings_section_label;
}
if ( ! empty( $listing_label ) ) {
$sub_sections[ $listing_label ] = $sub_section;
}
}
}
if ( ! empty( $sub_sections ) ) {
ksort( $sub_sections );
}
return $sub_sections;
}
/**
* Utility function to return array of sub section keys;
*
* @since 3.6.0
*
* @return array
*/
private function get_sub_sections_keys() {
$sub_sections_keys = array();
if ( ! empty( $this->sub_sections ) ) {
foreach ( $this->sub_sections as $sub_section ) {
$sub_sections_keys[] = $sub_section->settings_section_key;
}
}
return $sub_sections_keys;
}
/**
* Utility function to check if sub section is valid.
*
* @since 3.6.0
*
* @param string $sub_section_key Sub-Section key to check.
*
* @return bool True is valid sub-section.
*/
private function is_valid_sub_section( $sub_section_key = '' ) {
$is_sub_section = false;
if ( ! empty( $sub_section_key ) ) {
if ( in_array( $sub_section_key, $this->get_sub_sections_keys(), true ) ) {
$is_sub_section = true;
}
}
return $is_sub_section;
}
// End of functions.
}
}
add_action(
'learndash_settings_sections_init',
function() {
LearnDash_Settings_Section_Emails_List::add_section_instance();
}
);