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