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