learn-dash-settings-metabox-lesson-group-drip-settings.php
17.5 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
<?php
if ( ( class_exists( 'LearnDash_Settings_Metabox' ) ) && ( ! class_exists( 'LearnDash_Settings_Metabox_Lesson_Group_Drip_Settings' ) ) ) {
/**
* Class LearnDash_Settings_Metabox_Lesson_Group_Drip_Settings
*/
class LearnDash_Settings_Metabox_Lesson_Group_Drip_Settings extends \LearnDash_Settings_Metabox {
/**
* Public constructor for class
*/
public function __construct() {
// What screen ID are we showing on.
$this->settings_screen_id = 'sfwd-lessons';
// Used within the Settings API to uniquely identify this section.
$this->settings_metabox_key = 'learndash-lesson-group-drip-settings';
// Section label/header.
$this->settings_section_label = sprintf(
// translators: placeholder: Lesson.
esc_html_x( '%s Group Drip Settings', 'placeholder: Lesson', 'learndash' ),
learndash_get_custom_label( 'lesson' )
);
$this->settings_section_description = sprintf(
// translators: placeholder: lessons.
esc_html_x( 'Controls the timing and way %s can be accessed.', 'placeholder: lessons', 'learndash' ),
learndash_get_custom_label_lower( 'lessons' )
);
add_filter( 'learndash_metabox_save_fields_' . $this->settings_metabox_key, array(
$this,
'filter_saved_fields'
), 30, 3 );
// Map internal settings field ID to legacy field ID.
$this->settings_fields_map = array(
'drip_visible_after_specific_date' => 'drip_visible_after_specific_date',
);
parent::__construct();
}
/**
* Initialize the metabox settings values.
*/
public function load_settings_values() {
parent::load_settings_values();
if ( true === $this->settings_values_loaded ) {
}
}
/**
* Initialize the metabox settings fields.
*/
public function load_settings_fields() {
global $post;
$groups = get_posts( [
'post_type' => 'groups',
'posts_per_page' => 999,
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
] );
// If any group is not exists, this option will be disabled
if ( ! $groups ) {
return '';
}
if ( ! isset( $_GET['course_id'] ) ) {
$lesson_courses = learndash_get_courses_for_step( $post->ID );
if ( 'yes' === LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Courses_Builder', 'shared_steps' )
&& (
( isset( $lesson_courses['primary'] ) && ! empty( $lesson_courses['primary'] ) )
|| ( isset( $lesson_courses['primary'] ) && ! empty( $lesson_courses['secondary'] ) )
)
) {
echo sprintf(
_x( 'Please select a %s to manage group drip dates.zz', 'Course Label', 'uncanny-pro-toolkit' ),
LearnDash_Custom_Label::get_label( 'course' )
);
echo '<br />';
echo $this->get_course_switch_html( 0 );
} else {
$lessons_course_id = learndash_get_course_id( $post );
if ( ! empty( $lessons_course_id ) ) {
$item_url = get_edit_post_link( $post->ID );
$item_url = add_query_arg( 'course_id', $lessons_course_id, $item_url );
$item_url = add_query_arg( 'currentTab', 'sfwd-lessons-settings', $item_url );
echo "<a href='$item_url'>";
echo esc_attr__( 'Please click here to set group drip dates.', 'uncanny-pro-toolkit' );
echo '</a>';
} else {
echo sprintf(
_x( 'Drip dates can only be added if the %s is associated with a %s.', 'Course Label', 'uncanny-pro-toolkit' ),
LearnDash_Custom_Label::get_label( 'lesson' ),
LearnDash_Custom_Label::get_label( 'course' )
);
echo '<br />';
echo sprintf(
_x( 'This %s has no %s associations.', 'Course Label', 'uncanny-pro-toolkit' ),
LearnDash_Custom_Label::get_label( 'lesson' ),
LearnDash_Custom_Label::get_label( 'course' )
);
}
}
return '';
}
$note = sprintf(
_x( 'Note: A drip date set for a %1$s/Group persists across all %2$s. You cannot set different drip dates for the same %1$s/Group in different %2$s.', '1 Lesson and 2 Course label', 'uncanny-pro-toolkit' ),
LearnDash_Custom_Label::get_label( 'lesson' ),
LearnDash_Custom_Label::get_label( 'courses' )
);
$reload = esc_attr__( 'Note: Page reloads after Save/Remove date', 'uncanny-pro-toolkit' );
ob_start();
?>
<script>
function uoAddSwitcher() {
jQuery('<div style="clear:both;padding: 15px 0;"><?php echo esc_html( $note ); ?></div>').insertBefore("#uo-ld-group-drip");
jQuery('<div style="clear:both; font-weight:bold; padding: 15px 0; text-align:right;"><?php echo $reload ?></div>').insertAfter("#uo-ld-group-drip");
jQuery('<div style="display:inline;"><?php echo $this->get_course_switch_html( $_GET["course_id"] ); ?></div>').appendTo("#uo-ld-group-drip_filter");
jQuery(".dataTables_filter label").addClass("pull-right");
};
</script>
<table id="uo-ld-group-drip" class="striped celled table" style="width:100%">
<thead>
<tr>
<th><?php esc_attr_e( 'LearnDash group', 'uncanny-pro-toolkit' ); ?></th>
<th><?php esc_attr_e( 'Drip date', 'uncanny-pro-toolkit' ); ?></th>
<th><?php esc_attr_e( 'Drip date sort numeric', 'uncanny-pro-toolkit' ); ?></th>
<th><?php esc_attr_e( 'Drip date sort Y-M-D', 'uncanny-pro-toolkit' ); ?></th>
<th><?php esc_attr_e( 'Action', 'uncanny-pro-toolkit' ); ?></th>
</tr>
</thead>
<tbody>
<?php
foreach ( $groups as $group ) {
if ( $group && is_object( $group ) ) {
$group_has_course_access = learndash_group_has_course( $group->ID, $_GET["course_id"] );
if ( ! $group_has_course_access ) {
continue;
}
$date = get_post_meta( $post->ID, stripslashes( '\uncanny_pro_toolkit\UncannyDripLessonsByGroup' ) . '-' . $group->ID, true );
// Add tha ( date ) after group name on selection if exists
$u_date = '';
if ( $date ) {
if ( is_array( $date ) ) {
$date = \uncanny_pro_toolkit\UncannyDripLessonsByGroup::reformat_date( $date );
$u_date = $date;
$date = learndash_adjust_date_time_display( $date );
}
if ( \uncanny_pro_toolkit\UncannyDripLessonsByGroup::is_timestamp( $date ) ) {
$u_date = $date;
$date = \uncanny_pro_toolkit\UncannyDripLessonsByGroup::adjust_for_timezone_difference( $date );
}
}
$date = absint( $date );
?>
<tr class="uo-ld-group-drip-row" data-group="<?php echo $group->ID ?>"
data-post="<?php echo $post->ID; ?>"
data-course="<?php echo absint( $_GET['course_id'] ); ?>"
data-sort="<?php echo ! empty( $u_date ) ? $u_date : ''; ?>>"
>
<td>
<a href="<?php echo get_edit_post_link( $group->ID ) ?>"
target="_blank"><?php echo $group->post_title ?></a>
</td>
<td>
<?php
?>
<div class="ld_date_selector">
<span class="screen-reader-text"><?php esc_attr_e( 'Month', 'uncanny-pro-toolkit' ); ?></span>
<select class="ultp-gutenberg-field-select ld_date_mm" name="month">
<option value="">MM</option>
<option value="1"
data-text="Jan"<?php if ( ! empty( $date ) && 1 === absint( date( 'n', $date ) ) ) {
echo ' selected="selected"';
} ?>><?php esc_attr_e( '01-Jan', 'uncanny-pro-toolkit' ); ?>
</option>
<option value="2"
data-text="Feb"<?php if ( ! empty( $date ) && 2 === absint( date( 'n', $date ) ) ) {
echo ' selected="selected"';
} ?>><?php esc_attr_e( '02-Feb', 'uncanny-pro-toolkit' ); ?>
</option>
<option value="3"
data-text="Mar"<?php if ( ! empty( $date ) && 3 === absint( date( 'n', $date ) ) ) {
echo ' selected="selected"';
} ?>><?php esc_attr_e( '03-Mar', 'uncanny-pro-toolkit' ); ?>
</option>
<option value="4"
data-text="Apr"<?php if ( ! empty( $date ) && 4 === absint( date( 'n', $date ) ) ) {
echo ' selected="selected"';
} ?>><?php esc_attr_e( '04-Apr', 'uncanny-pro-toolkit' ); ?>
</option>
<option value="5"
data-text="May"<?php if ( ! empty( $date ) && 5 === absint( date( 'n', $date ) ) ) {
echo ' selected="selected"';
} ?>><?php esc_attr_e( '05-May', 'uncanny-pro-toolkit' ); ?>
</option>
<option value="6"
data-text="Jun"<?php if ( ! empty( $date ) && 6 === absint( date( 'n', $date ) ) ) {
echo ' selected="selected"';
} ?>><?php esc_attr_e( '06-Jun', 'uncanny-pro-toolkit' ); ?>
</option>
<option value="7"
data-text="Jul"<?php if ( ! empty( $date ) && 7 === absint( date( 'n', $date ) ) ) {
echo ' selected="selected"';
} ?>><?php esc_attr_e( '07-Jul', 'uncanny-pro-toolkit' ); ?>
</option>
<option value="8"
data-text="Aug"<?php if ( ! empty( $date ) && 8 === absint( date( 'n', $date ) ) ) {
echo ' selected="selected"';
} ?>><?php esc_attr_e( '08-Aug', 'uncanny-pro-toolkit' ); ?>
</option>
<option value="9"
data-text="Sep"<?php if ( ! empty( $date ) && 9 === absint( date( 'n', $date ) ) ) {
echo ' selected="selected"';
} ?>><?php esc_attr_e( '09-Sep', 'uncanny-pro-toolkit' ); ?>
</option>
<option value="10"
data-text="Oct"<?php if ( ! empty( $date ) && 10 === absint( date( 'n', $date ) ) ) {
echo ' selected="selected"';
} ?>><?php esc_attr_e( '10-Oct', 'uncanny-pro-toolkit' ); ?>
</option>
<option value="11"
data-text="Nov"<?php if ( ! empty( $date ) && 11 === absint( date( 'n', $date ) ) ) {
echo ' selected="selected"';
} ?>><?php esc_attr_e( '11-Nov', 'uncanny-pro-toolkit' ); ?>
</option>
<option value="12"
data-text="Dec"<?php if ( ! empty( $date ) && 12 === absint( date( 'n', $date ) ) ) {
echo ' selected="selected"';
} ?>><?php esc_attr_e( '12-Dec', 'uncanny-pro-toolkit' ); ?>
</option>
</select>
<span class="screen-reader-text"><?php esc_attr_e( 'Day', 'uncanny-pro-toolkit' ); ?></span>
<input type="number" placeholder="DD" min="1" max="31"
class="ultp-gutenberg-field-text ld_date_jj"
name="day" value="<?php echo ! empty( $date ) ? date( 'd', $date ) : '' ?>" size="2" maxlength="2"
autocomplete="off">,
<span class="screen-reader-text"><?php esc_attr_e( 'Year', 'uncanny-pro-toolkit' ); ?></span>
<input type="number" placeholder="YYYY"
min="<?php echo date( 'Y' ) - 3 ?>" max="<?php echo date( 'Y' ) + 15 ?>"
class="ultp-gutenberg-field-text ld_date_aa"
name="year" value="<?php echo ! empty( $date ) ? date( 'Y', $date ) : '' ?>" size="4" maxlength="4"
step="1" autocomplete="off">
@
<span class="screen-reader-text"><?php esc_attr_e( 'Hour', 'uncanny-pro-toolkit' ); ?></span>
<input type="number" min="0" max="23" placeholder="HH"
class="ultp-gutenberg-field-text ld_date_hh"
name="hour" value="<?php echo ! empty( $date ) ? date( 'H', $date ) : '' ?>" size="2" maxlength="2"
autocomplete="off">:
<span class="screen-reader-text"><?php esc_attr_e( 'Minute', 'uncanny-pro-toolkit' ); ?></span>
<input type="number" min="0" max="59" placeholder="MN"
class="ultp-gutenberg-field-text ld_date_mn"
name="minute" value="<?php echo ! empty( $date ) ? date( 'i', $date ) : '' ?>" size="2" maxlength="2"
autocomplete="off">
</div>
</td>
<td><?php echo ! empty( $date ) ? $date : '' ?></td>
<td><?php echo ! empty( $date ) ? date( 'Y-m-d', $date ) : '' ?></td>
<td class="uo-ld-group-drip-row__actions">
<button type="button"
class="uo-ld-group-drip__action uo-ld-group-drip__action--save ultp-gutenberg-btn">
<?php esc_attr_e( 'Save date', 'uncanny-pro-toolkit' ); ?>
</button>
<button type="button"
class="uo-ld-group-drip__action uo-ld-group-drip__action--remove ultp-gutenberg-btn">
<?php esc_attr_e( 'Remove date', 'uncanny-pro-toolkit' ); ?>
</button>
</td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<?php
echo ob_get_clean();
}
public function get_course_switch_html( $course_post_id ) {
global $post;
if ( 'yes' !== LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Courses_Builder', 'shared_steps' ) ) {
$course_name = get_the_title( $course_post_id );
return '<div style="display: inline-block;padding: 0 20px 0 20px;margin-top: 8px;">' .
sprintf(
esc_attr_x( '%s: %s', 'Course Label', 'learndash' ),
LearnDash_Custom_Label::get_label( 'course' ),
$course_name
) .
'</div>';
}
$cb_courses = learndash_get_courses_for_step( $post->ID );
$item_url = get_edit_post_link( $post->ID );
$html = '';
$html .= '<label style="padding-right:15px;">' . sprintf( esc_html_x( 'Switch %s', 'placeholder: Course', 'learndash' ), LearnDash_Custom_Label::get_label( 'Course' ) ) . ': ';
$html .= '<select onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">';
$html .= '<option value="">' . sprintf( esc_html_x( 'Select a %s', 'placeholder: Course', 'learndash' ), LearnDash_Custom_Label::get_label( 'Course' ) ) . '</option>';
foreach ( $cb_courses as $course_key => $course_set ) {
foreach ( $course_set as $course_id => $course_title ) {
$item_url = add_query_arg( 'course_id', $course_id, $item_url );
$item_url = add_query_arg( 'currentTab', 'sfwd-lessons-settings', $item_url );
$selected = '';
if ( $course_id == $course_post_id ) {
$selected = ' selected="selected" ';
}
$html .= '<option ' . $selected . 'value="' . $item_url . '" >' . get_the_title( $course_id ) . '</option>';
}
}
$html .= '</select></label>';
return $html;
}
/**
* Filter settings values for metabox before save to database.
*
* @param array $settings_values Array of settings values.
* @param string $settings_metabox_key Metabox key.
* @param string $settings_screen_id Screen ID.
*
* @return array $settings_values.
*/
public function filter_saved_fields( $settings_values = array(), $settings_metabox_key = '', $settings_screen_id = '' ) {
return $settings_values;
}
// End of functions.
}
add_filter(
'learndash_post_settings_metaboxes_init_' . learndash_get_post_type_slug( 'lesson' ),
function ( $metaboxes = array() ) {
if ( ( ! isset( $metaboxes['LearnDash_Settings_Metabox_Lesson_Group_Drip_Settings'] ) ) ) {
$metaboxes['LearnDash_Settings_Metabox_Lesson_Group_Drip_Settings'] = \LearnDash_Settings_Metabox_Lesson_Group_Drip_Settings::add_metabox_instance();
}
return $metaboxes;
},
999,
1
);
}