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.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / classes / Lesson.php
tutor / classes Last commit date
Addons.php 5 years ago Admin.php 5 years ago Ajax.php 5 years ago Assets.php 5 years ago Course.php 5 years ago Course_Filter.php 5 years ago Course_Settings_Tabs.php 5 years ago Course_Widget.php 5 years ago Custom_Validation.php 5 years ago Dashboard.php 5 years ago Email.php 5 years ago FormHandler.php 5 years ago Frontend.php 5 years ago Gutenberg.php 5 years ago Instructor.php 5 years ago Instructors_List.php 5 years ago Lesson.php 5 years ago Options.php 5 years ago Post_types.php 5 years ago Private_Course_Access.php 5 years ago Q_and_A.php 5 years ago Question_Answers_List.php 5 years ago Quiz.php 5 years ago Quiz_Attempts_List.php 5 years ago RestAPI.php 5 years ago Rewrite_Rules.php 5 years ago Shortcode.php 5 years ago Student.php 5 years ago Students_List.php 5 years ago Taxonomies.php 5 years ago Template.php 5 years ago Theme_Compatibility.php 5 years ago Tools.php 5 years ago Tutor.php 5 years ago TutorEDD.php 5 years ago Tutor_Base.php 5 years ago Tutor_List_Table.php 5 years ago Tutor_Setup.php 5 years ago Upgrader.php 5 years ago User.php 5 years ago Utils.php 5 years ago Video_Stream.php 5 years ago Withdraw.php 5 years ago Withdraw_Requests_List.php 5 years ago WooCommerce.php 5 years ago
Lesson.php
385 lines
1 <?php
2 namespace TUTOR;
3
4 if ( ! defined( 'ABSPATH' ) )
5 exit;
6
7 class Lesson extends Tutor_Base {
8 public function __construct() {
9 parent::__construct();
10
11 add_action( 'add_meta_boxes', array($this, 'register_meta_box') );
12 add_action('save_post_'.$this->lesson_post_type, array($this, "save_lesson_meta"));
13
14 add_action('wp_ajax_tutor_load_edit_lesson_modal', array($this, "tutor_load_edit_lesson_modal"));
15 add_action('wp_ajax_tutor_modal_create_or_update_lesson', array($this, "tutor_modal_create_or_update_lesson"));
16 add_action('wp_ajax_tutor_delete_lesson_by_id', array($this, "tutor_delete_lesson_by_id"));
17
18 add_filter('get_sample_permalink', array($this, 'change_lesson_permalink'), 10, 2);
19 add_action('admin_init', array($this, 'flush_rewrite_rules'));
20
21 /**
22 * Add Column
23 */
24
25 add_filter( "manage_{$this->lesson_post_type}_posts_columns", array($this, 'add_column'), 10,1 );
26 add_action( "manage_{$this->lesson_post_type}_posts_custom_column" , array($this, 'custom_lesson_column'), 10, 2 );
27
28 //Frontend Action
29 add_action('template_redirect', array($this, 'mark_lesson_complete'));
30
31 add_action('wp_ajax_tutor_render_lesson_content', array($this, "tutor_render_lesson_content"));
32 add_action('wp_ajax_nopriv_tutor_render_lesson_content', array($this, "tutor_render_lesson_content")); // For public course access
33
34 /**
35 * Autoplay next video
36 * @since v.1.4.9
37 */
38 add_action('wp_ajax_autoload_next_course_content', array($this, 'autoload_next_course_content'));
39
40 /**
41 * Load next course item after click complete button
42 * @since v.1.5.3
43 */
44 add_action('tutor_lesson_completed_after', array($this, 'tutor_lesson_completed_after'), 999);
45 }
46
47 /**
48 * Registering metabox
49 */
50 public function register_meta_box(){
51 $lesson_post_type = $this->lesson_post_type;
52
53 add_meta_box( 'tutor-course-select', __( 'Select Course', 'tutor' ), array($this, 'lesson_metabox'), $lesson_post_type );
54 add_meta_box( 'tutor-lesson-videos', __( 'Lesson Video', 'tutor' ), array($this, 'lesson_video_metabox'), $lesson_post_type );
55 add_meta_box( 'tutor-lesson-attachments', __( 'Attachments', 'tutor' ), array($this, 'lesson_attachments_metabox'), $lesson_post_type );
56 }
57
58 public function lesson_metabox(){
59 include tutor()->path.'views/metabox/lesson-metabox.php';
60 }
61
62 public function lesson_video_metabox(){
63 include tutor()->path.'views/metabox/video-metabox.php';
64 }
65
66 public function lesson_attachments_metabox(){
67 include tutor()->path.'views/metabox/lesson-attachments-metabox.php';
68 }
69
70 /**
71 * @param $post_ID
72 *
73 * Saving lesson meta and assets
74 *
75 */
76 public function save_lesson_meta($post_ID){
77 //Course
78 if (isset($_POST['selected_course'])) {
79 $course_id = (int) sanitize_text_field( $_POST['selected_course'] );
80 if ( $course_id ) {
81 update_post_meta( $post_ID, '_tutor_course_id_for_lesson', $course_id );
82 }
83 }
84
85 //Video
86 $video_source = tutils()->array_get('video.source', $_POST);
87 if ( $video_source === '-1'){
88 delete_post_meta($post_ID, '_video');
89 }elseif($video_source) {
90 $video = tutor_utils()->array_get('video', $_POST);
91 update_post_meta($post_ID, '_video', $video);
92 }
93
94 //Attachments
95 $attachments = array();
96 if ( ! empty($_POST['tutor_attachments'])){
97 $attachments = tutor_utils()->sanitize_array($_POST['tutor_attachments']);
98 $attachments = array_unique($attachments);
99 }
100 update_post_meta($post_ID, '_tutor_attachments', $attachments);
101 }
102
103 public function tutor_load_edit_lesson_modal(){
104 tutils()->checking_nonce();
105
106 $lesson_id = (int) tutor_utils()->avalue_dot('lesson_id', $_POST);
107 $topic_id = (int) sanitize_text_field( $_POST['topic_id'] );
108
109 if(!tutils()->can_user_manage('topic', $topic_id)) {
110 wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) );
111 }
112
113 /**
114 * If Lesson Not Exists, provide dummy
115 */
116 $post_arr = array(
117 'ID' => 0,
118 'post_content' => '',
119 'post_type' => $this->lesson_post_type,
120 'post_title' => __('Draft Lesson', 'tutor'),
121 'post_status' => 'publish',
122 'post_author' => get_current_user_id(),
123 'post_parent' => $topic_id,
124 );
125
126 $post = $lesson_id ? get_post($lesson_id) : (object)$post_arr;
127
128 ob_start();
129 include tutor()->path.'views/modal/edit-lesson.php';
130 $output = ob_get_clean();
131
132 wp_send_json_success(array('output' => $output));
133 }
134
135 /**
136 * @since v.1.0.0
137 * @updated v.1.5.1
138 */
139 public function tutor_modal_create_or_update_lesson(){
140 tutils()->checking_nonce();
141
142 global $wpdb;
143
144 $lesson_id = (int) sanitize_text_field(tutor_utils()->avalue_dot('lesson_id', $_POST));
145 $topic_id = (int) sanitize_text_field(tutor_utils()->avalue_dot('current_topic_id', $_POST));
146 $_lesson_thumbnail_id = (int) sanitize_text_field(tutor_utils()->avalue_dot('_lesson_thumbnail_id', $_POST));
147
148 if(!tutils()->can_user_manage('topic', $topic_id)) {
149 wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) );
150 }
151
152
153 $title = sanitize_text_field($_POST['lesson_title']);
154 $lesson_content = wp_kses_post($_POST['lesson_content']);
155
156 $lesson_data = array(
157 'post_type' => $this->lesson_post_type,
158 'post_title' => $title,
159 'post_name' => sanitize_title($title),
160 'post_content' => $lesson_content,
161 'post_status' => 'publish',
162 'post_author' => get_current_user_id(),
163 'post_parent' => $topic_id,
164 );
165
166 if($lesson_id==0) {
167
168 $lesson_id = wp_insert_post( $lesson_data );
169
170 if ($lesson_id ) {
171 do_action('tutor/lesson/created', $lesson_id);
172
173 $course_id = $wpdb->get_var( $wpdb->prepare("SELECT post_parent FROM {$wpdb->posts} WHERE ID=%d", $topic_id) );
174 update_post_meta( $lesson_id, '_tutor_course_id_for_lesson', $course_id );
175 }
176 else {
177 wp_send_json_error( array('message' => __('Couldn\'t create lesson.', 'tutor')) );
178 }
179 }
180 else {
181 $lesson_data['ID']=$lesson_id;
182
183 do_action('tutor/lesson_update/before', $lesson_id);
184 wp_update_post($lesson_data);
185 if ($_lesson_thumbnail_id){
186 update_post_meta($lesson_id, '_thumbnail_id', $_lesson_thumbnail_id);
187 }else{
188 delete_post_meta($lesson_id, '_thumbnail_id');
189 }
190 do_action('tutor/lesson_update/after', $lesson_id);
191 }
192
193 $course_id = tutor_utils()->get_course_id_by_lesson($lesson_id);
194
195 ob_start();
196 include tutor()->path.'views/metabox/course-contents.php';
197 $course_contents = ob_get_clean();
198
199 wp_send_json_success(array('course_contents' => $course_contents));
200 }
201
202 /**
203 * Delete Lesson from course builder
204 */
205 public function tutor_delete_lesson_by_id(){
206 tutils()->checking_nonce();
207
208 $lesson_id = (int) sanitize_text_field(tutor_utils()->avalue_dot('lesson_id', $_POST));
209
210 if(!tutils()->can_user_manage('lesson', $lesson_id)) {
211 wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) );
212 }
213
214 wp_delete_post($lesson_id, true);
215 delete_post_meta($lesson_id, '_tutor_course_id_for_lesson');
216 wp_send_json_success();
217 }
218
219
220 /**
221 * @param $uri
222 * @param $lesson_id
223 *
224 * @return mixed
225 *
226 * Changed the URI based
227 */
228
229 public function change_lesson_permalink($uri, $lesson_id){
230 $post = get_post($lesson_id);
231
232 if ($post && $post->post_type === $this->lesson_post_type){
233 $uri_base = trailingslashit(site_url());
234
235 $sample_course = "sample-course";
236 $is_course = get_post_meta(get_the_ID(), '_tutor_course_id_for_lesson', true);
237 if ($is_course){
238 $course = get_post($is_course);
239 if ($course){
240 $sample_course = $course->post_name;
241 }
242 }
243
244 $new_course_base = $uri_base."course/{$sample_course}/lesson/%pagename%/";
245 $uri[0] = $new_course_base;
246 }
247
248 return $uri;
249 }
250
251
252 public function flush_rewrite_rules(){
253 $is_required_flush = get_option('required_rewrite_flush');
254 if ($is_required_flush){
255 flush_rewrite_rules();
256 delete_option( 'required_rewrite_flush' );
257 }
258 }
259
260
261 public function add_column($columns){
262 $date_col = $columns['date'];
263 unset($columns['date']);
264 $columns['course'] = __('Course', 'tutor');
265 $columns['date'] = $date_col;
266
267 return $columns;
268 }
269
270 /**
271 * @param $column
272 * @param $post_id
273 *
274 */
275 public function custom_lesson_column($column, $post_id ){
276 if ($column === 'course'){
277
278 $course_id = get_post_meta($post_id, '_tutor_course_id_for_lesson', true);
279 if ($course_id){
280 echo '<a href="'.admin_url('post.php?post='.$course_id.'&action=edit').'">'.get_the_title($course_id).'</a>';
281 }
282
283 }
284 }
285
286 /**
287 *
288 * Mark lesson completed
289 *
290 * @since v.1.0.0
291 */
292 public function mark_lesson_complete(){
293 if ( ! isset($_POST['tutor_action']) || $_POST['tutor_action'] !== 'tutor_complete_lesson' ){
294 return;
295 }
296 //Checking nonce
297 tutor_utils()->checking_nonce();
298
299 $user_id = get_current_user_id();
300
301 //TODO: need to show view if not signed_in
302 if ( ! $user_id){
303 die(__('Please Sign-In', 'tutor'));
304 }
305
306 $lesson_id = (int) sanitize_text_field($_POST['lesson_id']);
307
308 do_action('tutor_lesson_completed_before', $lesson_id);
309 /**
310 * Marking lesson at user meta, meta format, _tutor_completed_lesson_id_{id} and value = tutor_time();
311 */
312 tutor_utils()->mark_lesson_complete($lesson_id);
313
314 do_action('tutor_lesson_completed_after', $lesson_id, $user_id);
315 }
316
317 /**
318 * Render the lesson content
319 */
320 public function tutor_render_lesson_content(){
321 tutils()->checking_nonce();
322
323 $lesson_id = (int) sanitize_text_field(tutor_utils()->avalue_dot('lesson_id', $_POST));
324
325 $ancestors = get_post_ancestors($lesson_id);
326 $course_id = !empty($ancestors) ? array_pop($ancestors): $lesson_id;
327
328 // Course must be public or current user must be enrolled to access this lesson
329 if(get_post_meta($course_id, '_tutor_is_public_course', true)!=='yes' && !tutils()->is_enrolled($course_id)){
330 http_response_code(400);
331 exit;
332 }
333
334 ob_start();
335 global $post;
336
337 $post = get_post($lesson_id);
338 setup_postdata($post);
339 tutor_lesson_content();
340 wp_reset_postdata();
341
342 $html = ob_get_clean();
343 wp_send_json_success(array('html' => $html));
344 }
345
346 /**
347 * Load next course item automatically
348 *
349 * @since v.1.4.9
350 */
351 public function autoload_next_course_content(){
352 tutor_utils()->checking_nonce();
353
354 $post_id = sanitize_text_field($_POST['post_id']);
355 $content_id = tutils()->get_post_id($post_id);
356 $contents = tutor_utils()->get_course_prev_next_contents_by_id($content_id);
357
358 $autoload_course_content = (bool) get_tutor_option('autoload_next_course_content');
359 $next_url = false;
360 if($autoload_course_content) {
361 $next_url = get_the_permalink($contents->next_id);
362 }
363 wp_send_json_success(array('next_url' => $next_url));
364 }
365
366 /**
367 * Load next course item after click complete button
368 *
369 * @since v.1.5.3
370 */
371 public function tutor_lesson_completed_after($content_id){
372 $contents = tutor_utils()->get_course_prev_next_contents_by_id($content_id);
373 $autoload_course_content = (bool) get_tutor_option('autoload_next_course_content');
374 if($autoload_course_content) {
375 wp_redirect(get_the_permalink($contents->next_id));
376 } else {
377 wp_redirect(get_the_permalink($content_id));
378 }
379 die();
380 }
381
382 }
383
384
385