class-learndash.php
7.96 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
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if(class_exists( 'SFWD_LMS' )) {
class WPF_LearnDash extends WPF_Integrations_Base {
/**
* Gets things started
*
* @access public
* @since 1.0
* @return void
*/
public function init() {
add_action( 'learndash_course_completed', array( $this, 'apply_tags_learndash_complete') );
add_action( 'learndash_topic_completed', array( $this, 'apply_tags_learndash_complete') );
add_action( 'learndash_lesson_completed', array( $this, 'apply_tags_learndash_complete') );
add_action( 'learndash_quiz_completed', array( $this, 'apply_tags_learndash_complete') );
add_action( 'add_meta_boxes', array( $this, 'configure_meta_box' ));
add_action( 'wpf_meta_box_content', array( $this, 'meta_box_content' ), 40, 2 );
add_action( 'wpf_meta_box_save', array( $this, 'meta_box_save' ), 20, 2 );
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ), 20, 2 );
add_action( 'save_post', array( $this, 'save_meta_box_data' ), 20, 2 );
add_action( 'wpf_tags_modified', array( $this, 'update_course_access' ), 10, 2);
}
/**
* Applies tags when a LearnDash course/lesson/topic is completed
*
* @access public
* @return void
*/
public function apply_tags_learndash_complete( $data ) {
$wpf_settings = array();
if(isset($data['topic'])) {
$wpf_settings = get_post_meta($data['topic']->ID, 'wpf-settings', true);
} elseif(isset($data['lesson'])) {
$wpf_settings = get_post_meta($data['lesson']->ID, 'wpf-settings', true);
} elseif(isset($data['course']) && !isset($data['quiz'])) {
$wpf_settings = get_post_meta($data['course']->ID, 'wpf-settings', true);
} if(isset($data['quiz']) && $data['pass'] == true) {
$wpf_settings = get_post_meta($data['quiz']->ID, 'wpf-settings', true);
}
if(isset($wpf_settings['apply_tags_ld']))
wp_fusion()->user->apply_tags($wpf_settings['apply_tags_ld'], $data['user']->ID);
}
/**
* Remove standard "Apply to children" field from meta box
*
* @access public
* @return void
*/
public function configure_meta_box() {
global $post;
if($post->post_type == 'sfwd-courses' || $post->post_type == 'sfwd-lessons' || $post->post_type == 'sfwd-topic')
remove_action( 'wpf_meta_box_content', 'apply_to_children', 35 );
}
/**
* Adds learndash fields to WPF meta box
*
* @access public
* @return void
*/
public function meta_box_content( $post, $settings ) {
if($post->post_type != 'sfwd-courses' && $post->post_type != 'sfwd-lessons' && $post->post_type != 'sfwd-topic' && $post->post_type != 'sfwd-quiz')
return;
echo '<p><label for="wpf-apply-tags-ld"><small>Apply these tags when marked complete:</small></label>';
wpf_render_tag_multiselect( $settings['apply_tags_ld'], 'wpf-settings', 'apply_tags_ld' );
if($post->post_type == 'sfwd-courses') {
$children = get_posts(array('posts_per_page' => -1, 'post_type' => array('sfwd-lessons', 'sfwd-topic'), 'meta_key' => 'course_id', 'meta_value' => $post->ID));
if(count($children) > 0)
echo '<p><input class="checkbox" type="checkbox" id="wpf-apply-children-courses" name="wpf-settings[apply_children_courses]" value="1" '.checked($settings['apply_children_courses'], 1, FALSE).' /> <small>Apply to ' . count($children) . ' related lessons and topics</small></p>';
} elseif ($post->post_type == 'sfwd-lessons') {
$children = get_posts(array('posts_per_page' => -1, 'post_type' => array('sfwd-topic'), 'meta_key' => 'lesson_id', 'meta_value' => $post->ID));
if(count($children) > 0)
echo '<p><input class="checkbox" type="checkbox" id="wpf-apply-children-lessons" name="wpf-settings[apply_children_lessons]" value="1" '.checked($settings['apply_children_lessons'], 1, FALSE).' /> <small>Apply to ' . count($children) . ' related topics</small></p>';
}
}
/**
* Runs when WPF meta box is saved
*
* @access public
* @return void
*/
public function meta_box_save( $post_id, $data ) {
if(isset($data['apply_children_courses'])) {
$children = get_posts(array('posts_per_page' => -1, 'post_type' => array('sfwd-lessons', 'sfwd-topic'), 'meta_key' => 'course_id', 'meta_value' => $post_id));
} elseif (isset($data['apply_children_lessons'])) {
$children = get_posts(array('posts_per_page' => -1, 'post_type' => array('sfwd-topic'), 'meta_key' => 'lesson_id', 'meta_value' => $post_id));
}
if(isset($children)) {
foreach($children as $child) {
update_post_meta( $child->ID, 'wpf-settings', $data );
}
}
}
/**
* Adds meta box
*
* @access public
* @return mixed
*/
public function add_meta_box( $post_id, $data ) {
add_meta_box('wpf-learndash-meta', 'WP Fusion - Course Settings', array($this, 'meta_box_callback'), 'sfwd-courses');
}
/**
* Displays meta box content
*
* @access public
* @return mixed
*/
public function meta_box_callback( $post ) {
wp_nonce_field( 'wpf_meta_box_learndash', 'wpf_meta_box_learndash_nonce' );
$settings = array(
'link_tag' => array()
);
if(get_post_meta( $post->ID, 'wpf-settings-learndash', true ))
$settings = array_merge($settings, get_post_meta( $post->ID, 'wpf-settings-learndash', true ));
echo '<table class="form-table"><tbody>';
echo '<tr>';
echo '<th scope="row"><label for="tag_link">Link with tag:</label></th>';
echo '<td>';
wpf_render_tag_multiselect($settings['tag_link'], 'wpf-settings-learndash', 'tag_link', null, false, 'Select Tag', 1 );
echo '<span class="description">Select a tag to link with this course. When the tag is applied, the user will automatically be enrolled.</span>';
echo '</td>';
echo '</tr>';
echo '</tbody></table>';
}
/**
* Runs when WPF meta box is saved
*
* @access public
* @return void
*/
public function save_meta_box_data ( $post_id ){
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['wpf_meta_box_learndash_nonce'] ) )
return;
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['wpf_meta_box_learndash_nonce'], 'wpf_meta_box_learndash' ) )
return;
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// Don't update on revisions
if($_POST['post_type'] == 'revision')
return;
if(isset($_POST['wpf-settings-learndash'])) {
$data = $_POST['wpf-settings-learndash'];
} else {
$data = array();
}
// Update the meta field in the database.
update_post_meta( $post_id, 'wpf-settings-learndash', $data );
}
/**
* Update user course enrollment when tags are modified
*
* @access public
* @return void
*/
public function update_course_access($user_id, $user_tags) {
if($user_tags == false)
return;
$linked_courses = get_posts( array(
'post_type' => 'sfwd-courses',
'nopaging' => true,
'meta_query' => array(
array(
'key' => 'wpf-settings-learndash',
'compare' => 'EXISTS'
),
),
'fields' => 'ids'
) );
// Update course access based on user tags
foreach( $linked_courses as $course_id ) {
$settings = get_post_meta( $course_id, 'wpf-settings-learndash', true );
if(empty($settings) || empty($settings['tag_link']))
continue;
$tag_id = $settings['tag_link'][0];
if(in_array($tag_id, $user_tags) && !sfwd_lms_has_access($course_id, $user_id)) {
ld_update_course_access($user_id, $course_id, $remove = false);
} else if(!in_array($tag_id, $user_tags) && sfwd_lms_has_access($course_id, $user_id)){
ld_update_course_access($user_id, $course_id, $remove = true);
}
}
}
}
new WPF_LearnDash;
}