Addons.php
6 years ago
Admin.php
6 years ago
Ajax.php
6 years ago
Assets.php
6 years ago
Course.php
6 years ago
Course_Settings_Tabs.php
6 years ago
Course_Widget.php
6 years ago
Dashboard.php
6 years ago
Email.php
6 years ago
FormHandler.php
6 years ago
Frontend.php
6 years ago
Gutenberg.php
6 years ago
Instructor.php
6 years ago
Instructors_List.php
6 years ago
Lesson.php
6 years ago
Options.php
6 years ago
Post_types.php
6 years ago
Q_and_A.php
6 years ago
Question_Answers_List.php
6 years ago
Quiz.php
6 years ago
Quiz_Attempts_List.php
6 years ago
RestAPI.php
6 years ago
Rewrite_Rules.php
6 years ago
Shortcode.php
6 years ago
Student.php
6 years ago
Students_List.php
6 years ago
Taxonomies.php
6 years ago
Template.php
6 years ago
Theme_Compatibility.php
6 years ago
Tools.php
6 years ago
Tutor.php
6 years ago
TutorEDD.php
6 years ago
Tutor_Base.php
6 years ago
Tutor_List_Table.php
6 years ago
Upgrader.php
6 years ago
User.php
6 years ago
Utils.php
6 years ago
Video_Stream.php
6 years ago
Withdraw.php
6 years ago
Withdraw_Requests_List.php
6 years ago
WooCommerce.php
6 years ago
Lesson.php
318 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 | |
| 33 | /** |
| 34 | * Autoplay next video |
| 35 | * @since v.1.4.9 |
| 36 | */ |
| 37 | add_action('wp_ajax_autoload_next_course_content', array($this, 'autoload_next_course_content')); |
| 38 | add_action('wp_ajax_nopriv_autoload_next_course_content', array($this, 'autoload_next_course_content_noprev')); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Registering metabox |
| 43 | */ |
| 44 | public function register_meta_box(){ |
| 45 | $lesson_post_type = $this->lesson_post_type; |
| 46 | |
| 47 | add_meta_box( 'tutor-course-select', __( 'Select Course', 'tutor' ), array($this, 'lesson_metabox'), $lesson_post_type ); |
| 48 | add_meta_box( 'tutor-lesson-videos', __( 'Lesson Video', 'tutor' ), array($this, 'lesson_video_metabox'), $lesson_post_type ); |
| 49 | add_meta_box( 'tutor-lesson-attachments', __( 'Attachments', 'tutor' ), array($this, 'lesson_attachments_metabox'), $lesson_post_type ); |
| 50 | } |
| 51 | |
| 52 | public function lesson_metabox(){ |
| 53 | include tutor()->path.'views/metabox/lesson-metabox.php'; |
| 54 | } |
| 55 | |
| 56 | public function lesson_video_metabox(){ |
| 57 | include tutor()->path.'views/metabox/video-metabox.php'; |
| 58 | } |
| 59 | |
| 60 | public function lesson_attachments_metabox(){ |
| 61 | include tutor()->path.'views/metabox/lesson-attachments-metabox.php'; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @param $post_ID |
| 66 | * |
| 67 | * Saving lesson meta and assets |
| 68 | * |
| 69 | */ |
| 70 | public function save_lesson_meta($post_ID){ |
| 71 | //Course |
| 72 | if (isset($_POST['selected_course'])) { |
| 73 | $course_id = (int) sanitize_text_field( $_POST['selected_course'] ); |
| 74 | if ( $course_id ) { |
| 75 | update_post_meta( $post_ID, '_tutor_course_id_for_lesson', $course_id ); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | //Video |
| 80 | $video_source = tutils()->array_get('video.source', $_POST); |
| 81 | if ( $video_source === '-1'){ |
| 82 | delete_post_meta($post_ID, '_video'); |
| 83 | }elseif($video_source) { |
| 84 | $video = tutor_utils()->array_get('video', $_POST); |
| 85 | update_post_meta($post_ID, '_video', $video); |
| 86 | } |
| 87 | |
| 88 | //Attachments |
| 89 | $attachments = array(); |
| 90 | if ( ! empty($_POST['tutor_attachments'])){ |
| 91 | $attachments = tutor_utils()->sanitize_array($_POST['tutor_attachments']); |
| 92 | $attachments = array_unique($attachments); |
| 93 | } |
| 94 | update_post_meta($post_ID, '_tutor_attachments', $attachments); |
| 95 | } |
| 96 | |
| 97 | public function tutor_load_edit_lesson_modal(){ |
| 98 | $lesson_id = (int) tutor_utils()->avalue_dot('lesson_id', $_POST); |
| 99 | $topic_id = (int) sanitize_text_field( $_POST['topic_id'] ); |
| 100 | |
| 101 | /** |
| 102 | * If Lesson Not Exists, create One |
| 103 | */ |
| 104 | if ( ! $lesson_id){ |
| 105 | $course_id = (int) sanitize_text_field( $_POST['course_id'] ); |
| 106 | |
| 107 | $post_arr = array( |
| 108 | 'post_type' => $this->lesson_post_type, |
| 109 | 'post_title' => __('Draft Lesson', 'tutor'), |
| 110 | 'post_status' => 'publish', |
| 111 | 'post_author' => get_current_user_id(), |
| 112 | 'post_parent' => $topic_id, |
| 113 | ); |
| 114 | $lesson_id = wp_insert_post( $post_arr ); |
| 115 | if ($lesson_id ) { |
| 116 | do_action('tutor/lesson/created', $lesson_id); |
| 117 | update_post_meta( $lesson_id, '_tutor_course_id_for_lesson', $course_id ); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | $post = get_post($lesson_id); |
| 122 | ob_start(); |
| 123 | include tutor()->path.'views/modal/edit-lesson.php'; |
| 124 | $output = ob_get_clean(); |
| 125 | |
| 126 | wp_send_json_success(array('output' => $output)); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @since v.1.0.0 |
| 131 | * @updated v.1.5.1 |
| 132 | */ |
| 133 | public function tutor_modal_create_or_update_lesson(){ |
| 134 | $lesson_id = (int) sanitize_text_field(tutor_utils()->avalue_dot('lesson_id', $_POST)); |
| 135 | $_lesson_thumbnail_id = (int) sanitize_text_field(tutor_utils()->avalue_dot('_lesson_thumbnail_id', $_POST)); |
| 136 | |
| 137 | $title = sanitize_text_field($_POST['lesson_title']); |
| 138 | $lesson_content = wp_kses_post($_POST['lesson_content']); |
| 139 | |
| 140 | $lesson_data = array( |
| 141 | 'ID' => $lesson_id, |
| 142 | 'post_title' => $title, |
| 143 | 'post_name' => sanitize_title($title), |
| 144 | 'post_content' => $lesson_content, |
| 145 | ); |
| 146 | |
| 147 | if ($_lesson_thumbnail_id){ |
| 148 | $lesson_data['_thumbnail_id'] = $_lesson_thumbnail_id; |
| 149 | } |
| 150 | |
| 151 | do_action('tutor/lesson_update/before', $lesson_id); |
| 152 | wp_update_post($lesson_data); |
| 153 | do_action('tutor/lesson_update/after', $lesson_id); |
| 154 | |
| 155 | $course_id = tutor_utils()->get_course_id_by_lesson($lesson_id); |
| 156 | |
| 157 | ob_start(); |
| 158 | include tutor()->path.'views/metabox/course-contents.php'; |
| 159 | $course_contents = ob_get_clean(); |
| 160 | |
| 161 | wp_send_json_success(array('course_contents' => $course_contents)); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Delete Lesson from course builder |
| 166 | */ |
| 167 | public function tutor_delete_lesson_by_id(){ |
| 168 | $lesson_id = (int) sanitize_text_field(tutor_utils()->avalue_dot('lesson_id', $_POST)); |
| 169 | wp_delete_post($lesson_id, true); |
| 170 | delete_post_meta($lesson_id, '_tutor_course_id_for_lesson'); |
| 171 | wp_send_json_success(); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | /** |
| 176 | * @param $uri |
| 177 | * @param $lesson_id |
| 178 | * |
| 179 | * @return mixed |
| 180 | * |
| 181 | * Changed the URI based |
| 182 | */ |
| 183 | |
| 184 | public function change_lesson_permalink($uri, $lesson_id){ |
| 185 | $post = get_post($lesson_id); |
| 186 | |
| 187 | if ($post && $post->post_type === $this->lesson_post_type){ |
| 188 | $uri_base = trailingslashit(site_url()); |
| 189 | |
| 190 | $sample_course = "sample-course"; |
| 191 | $is_course = get_post_meta(get_the_ID(), '_tutor_course_id_for_lesson', true); |
| 192 | if ($is_course){ |
| 193 | $course = get_post($is_course); |
| 194 | if ($course){ |
| 195 | $sample_course = $course->post_name; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | $new_course_base = $uri_base."course/{$sample_course}/lesson/%pagename%/"; |
| 200 | $uri[0] = $new_course_base; |
| 201 | } |
| 202 | |
| 203 | return $uri; |
| 204 | } |
| 205 | |
| 206 | |
| 207 | public function flush_rewrite_rules(){ |
| 208 | $is_required_flush = get_option('required_rewrite_flush'); |
| 209 | if ($is_required_flush){ |
| 210 | flush_rewrite_rules(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | |
| 215 | public function add_column($columns){ |
| 216 | $date_col = $columns['date']; |
| 217 | unset($columns['date']); |
| 218 | $columns['course'] = __('Course', 'tutor'); |
| 219 | $columns['date'] = $date_col; |
| 220 | |
| 221 | return $columns; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * @param $column |
| 226 | * @param $post_id |
| 227 | * |
| 228 | */ |
| 229 | public function custom_lesson_column($column, $post_id ){ |
| 230 | if ($column === 'course'){ |
| 231 | |
| 232 | $course_id = get_post_meta($post_id, '_tutor_course_id_for_lesson', true); |
| 233 | if ($course_id){ |
| 234 | echo '<a href="'.admin_url('post.php?post='.$course_id.'&action=edit').'">'.get_the_title($course_id).'</a>'; |
| 235 | } |
| 236 | |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * |
| 242 | * Mark lesson completed |
| 243 | * |
| 244 | * @since v.1.0.0 |
| 245 | */ |
| 246 | public function mark_lesson_complete(){ |
| 247 | if ( ! isset($_POST['tutor_action']) || $_POST['tutor_action'] !== 'tutor_complete_lesson' ){ |
| 248 | return; |
| 249 | } |
| 250 | //Checking nonce |
| 251 | tutor_utils()->checking_nonce(); |
| 252 | |
| 253 | $user_id = get_current_user_id(); |
| 254 | |
| 255 | //TODO: need to show view if not signed_in |
| 256 | if ( ! $user_id){ |
| 257 | die(__('Please Sign-In', 'tutor')); |
| 258 | } |
| 259 | |
| 260 | $lesson_id = (int) sanitize_text_field($_POST['lesson_id']); |
| 261 | |
| 262 | do_action('tutor_lesson_completed_before', $lesson_id); |
| 263 | /** |
| 264 | * Marking lesson at user meta, meta format, _tutor_completed_lesson_id_{id} and value = tutor_time(); |
| 265 | */ |
| 266 | tutor_utils()->mark_lesson_complete($lesson_id); |
| 267 | |
| 268 | do_action('tutor_lesson_completed_after', $lesson_id); |
| 269 | |
| 270 | |
| 271 | wp_redirect(get_the_permalink($lesson_id)); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Render the lesson content |
| 276 | */ |
| 277 | public function tutor_render_lesson_content(){ |
| 278 | $lesson_id = (int) sanitize_text_field(tutor_utils()->avalue_dot('lesson_id', $_POST)); |
| 279 | |
| 280 | ob_start(); |
| 281 | global $post; |
| 282 | |
| 283 | $post = get_post($lesson_id); |
| 284 | setup_postdata($post); |
| 285 | tutor_lesson_content(); |
| 286 | wp_reset_postdata(); |
| 287 | |
| 288 | $html = ob_get_clean(); |
| 289 | wp_send_json_success(array('html' => $html)); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Load next course item automatically |
| 294 | * |
| 295 | * @since v.1.4.9 |
| 296 | */ |
| 297 | public function autoload_next_course_content(){ |
| 298 | tutor_utils()->checking_nonce(); |
| 299 | $post_id = sanitize_text_field($_POST['post_id']); |
| 300 | $content_id = tutils()->get_post_id($post_id); |
| 301 | $contents = tutor_utils()->get_course_prev_next_contents_by_id($content_id); |
| 302 | |
| 303 | $autoload_course_content = (bool) get_tutor_option('autoload_next_course_content'); |
| 304 | $next_url = false; |
| 305 | if($autoload_course_content) { |
| 306 | $next_url = get_the_permalink($contents->next_id); |
| 307 | } |
| 308 | wp_send_json_success(array('next_url' => $next_url)); |
| 309 | } |
| 310 | |
| 311 | public function autoload_next_course_content_noprev(){ |
| 312 | |
| 313 | } |
| 314 | |
| 315 | } |
| 316 | |
| 317 | |
| 318 |