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