PluginProbe ʕ •ᴥ•ʔ
Tutor LMS – eLearning and online course solution / 1.4.3
Tutor LMS – eLearning and online course solution v1.4.3
3.9.14 3.9.13 3.9.12 3.9.11 trunk 1.0.0 1.0.0-alpha 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.2.0 1.2.1 1.2.11 1.2.12 1.2.13 1.2.20 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 1.8.0 1.8.1 1.8.10 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.14 1.9.15 1.9.16 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.10 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.10 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.3.0 2.4.0 2.5.0 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 3.0.0 3.0.1 3.0.2 3.1.0 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.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 / Course.php
tutor / classes Last commit date
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 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 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
Course.php
787 lines
1 <?php
2 namespace TUTOR;
3
4 if ( ! defined( 'ABSPATH' ) )
5 exit;
6
7 class Course 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->course_post_type, array($this, 'save_course_meta'), 10, 2);
13 add_action('wp_ajax_tutor_add_course_topic', array($this, 'tutor_add_course_topic'));
14 add_action('wp_ajax_tutor_update_topic', array($this, 'tutor_update_topic'));
15
16 //Add Column
17 add_filter( "manage_{$this->course_post_type}_posts_columns", array($this, 'add_column'), 10,1 );
18 add_action( "manage_{$this->course_post_type}_posts_custom_column" , array($this, 'custom_lesson_column'), 10, 2 );
19
20 add_action('admin_action_tutor_delete_topic', array($this, 'tutor_delete_topic'));
21 add_action('admin_action_tutor_delete_announcement', array($this, 'tutor_delete_announcement'));
22
23 //Frontend Action
24 add_action('template_redirect', array($this, 'enroll_now'));
25 add_action('template_redirect', array($this, 'mark_course_complete'));
26
27 //Modal Perform
28 add_action('wp_ajax_tutor_load_instructors_modal', array($this, 'tutor_load_instructors_modal'));
29 add_action('wp_ajax_tutor_add_instructors_to_course', array($this, 'tutor_add_instructors_to_course'));
30 add_action('wp_ajax_detach_instructor_from_course', array($this, 'detach_instructor_from_course'));
31
32 /**
33 * Frontend Dashboard
34 */
35 add_action('wp_ajax_tutor_delete_dashboard_course', array($this, 'tutor_delete_dashboard_course'));
36
37 /**
38 * Gutenberg author support
39 */
40
41 add_filter('wp_insert_post_data', array($this, 'tutor_add_gutenberg_author'), '99', 2);
42
43 /**
44 * Frontend metabox supports for course builder
45 * @since v.1.3.4
46 */
47
48 add_action('tutor/dashboard_course_builder_form_field_after', array($this, 'register_meta_box_in_frontend'));
49
50
51 /**
52 * Do Stuff for the course save from frontend
53 */
54 add_action('save_tutor_course', array($this, 'attach_product_with_course'), 10, 2);
55
56 /**
57 * Add course level to course settings
58 * @since v.1.4.1
59 */
60
61 add_action('tutor_course/settings_tab_content/after/general', array($this, 'add_course_level_to_settings'));
62 }
63 /**
64 * Registering metabox
65 */
66 public function register_meta_box(){
67 $coursePostType = tutor()->course_post_type;
68 $course_marketplace = tutor_utils()->get_option('enable_course_marketplace');
69 //add_meta_box( 'tutor-course-levels', __( 'Course Level', 'tutor' ), array($this, 'course_level_metabox'), $coursePostType );
70 add_meta_box( 'tutor-course-topics', __( 'Course Builder', 'tutor' ), array($this, 'course_meta_box'), $coursePostType );
71 add_meta_box( 'tutor-course-additional-data', __( 'Additional Data', 'tutor' ), array($this, 'course_additional_data_meta_box'), $coursePostType );
72 add_meta_box( 'tutor-course-videos', __( 'Video', 'tutor' ), array($this, 'video_metabox'), $coursePostType );
73 if ($course_marketplace) {
74 add_meta_box( 'tutor-instructors', __( 'Instructors', 'tutor' ), array( $this, 'instructors_metabox' ), $coursePostType );
75 }
76 add_meta_box( 'tutor-announcements', __( 'Announcements', 'tutor' ), array($this, 'announcements_metabox'), $coursePostType );
77 }
78 public function course_meta_box($echo = true){
79 ob_start();
80 include tutor()->path.'views/metabox/course-topics.php';
81 $content = ob_get_clean();
82
83 if ($echo){
84 echo $content;
85 }else{
86 return $content;
87 }
88 }
89 public function course_additional_data_meta_box($echo = true){
90
91 ob_start();
92 include tutor()->path.'views/metabox/course-additional-data.php';
93 $content = ob_get_clean();
94
95 if ($echo){
96 echo $content;
97 }else{
98 return $content;
99 }
100 }
101 public function video_metabox($echo = true){
102 ob_start();
103 include tutor()->path.'views/metabox/video-metabox.php';
104 $content = ob_get_clean();
105
106 if ($echo){
107 echo $content;
108 }else{
109 return $content;
110 }
111 }
112
113 public function course_level_metabox($echo = true){
114 ob_start();
115 include tutor()->path.'views/metabox/course-level-metabox.php';
116 $content = ob_get_clean();
117
118 if ($echo){
119 echo $content;
120 }else{
121 return $content;
122 }
123 }
124
125 public function announcements_metabox($echo = true){
126 ob_start();
127 include tutor()->path.'views/metabox/announcements-metabox.php';
128 $content = ob_get_clean();
129
130 if ($echo){
131 echo $content;
132 }else{
133 return $content;
134 }
135 }
136
137 public function instructors_metabox($echo = true){
138 ob_start();
139 include tutor()->path . 'views/metabox/instructors-metabox.php';
140 $content = ob_get_clean();
141
142 if ($echo){
143 echo $content;
144 }else{
145 return $content;
146 }
147 }
148
149 /**
150 * Register metabox in course builder tutor
151 * @since v.1.3.4
152 */
153 public function register_meta_box_in_frontend(){
154 do_action('tutor_course_builder_metabox_before', get_the_ID());
155 course_builder_section_wrap($this->video_metabox($echo = false), 'Video');
156 course_builder_section_wrap($this->course_meta_box($echo = false), 'Course Builder');
157 course_builder_section_wrap($this->instructors_metabox($echo = false), 'Instructors');
158 course_builder_section_wrap($this->course_additional_data_meta_box($echo = false), 'Additional Data');
159 course_builder_section_wrap($this->announcements_metabox($echo = false), 'Announcements');
160 do_action('tutor_course_builder_metabox_after', get_the_ID());
161 }
162
163 /**
164 * @param $post_ID
165 *
166 * Insert Topic and attached it with Course
167 */
168 public function save_course_meta($post_ID, $post){
169 global $wpdb;
170
171 do_action( "tutor_save_course", $post_ID, $post);
172
173 /**
174 * Insert Topic
175 */
176 /*
177 if ( ! empty($_POST['topic_title'])) {
178 $topic_title = sanitize_text_field( $_POST['topic_title'] );
179 $topic_summery = wp_kses_post( $_POST['topic_summery'] );
180
181 $post_arr = array(
182 'post_type' => 'topics',
183 'post_title' => $topic_title,
184 'post_content' => $topic_summery,
185 'post_status' => 'publish',
186 'post_author' => get_current_user_id(),
187 'post_parent' => $post_ID,
188 );
189 wp_insert_post( $post_arr );
190 }*/
191
192 /**
193 * Save course price type
194 */
195 $price_type = tutils()->array_get('tutor_course_price_type', $_POST);
196 if ($price_type){
197 update_post_meta($post_ID, '_tutor_course_price_type', $price_type);
198 }
199
200 //Course Duration
201 if ( ! empty($_POST['course_duration'])){
202 $video = tutor_utils()->sanitize_array($_POST['course_duration']);
203 update_post_meta($post_ID, '_course_duration', $video);
204 }
205
206 if ( ! empty($_POST['course_level'])){
207 $course_level = sanitize_text_field($_POST['course_level']);
208 update_post_meta($post_ID, '_tutor_course_level', $course_level);
209 }
210
211 if ( ! empty($_POST['course_benefits'])){
212 $course_benefits = wp_kses_post($_POST['course_benefits']);
213 update_post_meta($post_ID, '_tutor_course_benefits', $course_benefits);
214 }
215
216 if ( ! empty($_POST['course_requirements'])){
217 $requirements = wp_kses_post($_POST['course_requirements']);
218 update_post_meta($post_ID, '_tutor_course_requirements', $requirements);
219 }
220
221 if ( ! empty($_POST['course_target_audience'])){
222 $target_audience = wp_kses_post($_POST['course_target_audience']);
223 update_post_meta($post_ID, '_tutor_course_target_audience', $target_audience);
224 }
225
226 if ( ! empty($_POST['course_material_includes'])){
227 $material_includes = wp_kses_post($_POST['course_material_includes']);
228 update_post_meta($post_ID, '_tutor_course_material_includes', $material_includes);
229 }
230 /**
231 * Sorting Topics and lesson
232 */
233 if ( ! empty($_POST['tutor_topics_lessons_sorting'])){
234 $new_order = sanitize_text_field(stripslashes($_POST['tutor_topics_lessons_sorting']));
235 $order = json_decode($new_order, true);
236
237 if (is_array($order) && count($order)){
238 $i = 0;
239 foreach ($order as $topic ){
240 $i++;
241 $wpdb->update(
242 $wpdb->posts,
243 array('menu_order' => $i),
244 array('ID' => $topic['topic_id'])
245 );
246
247 /**
248 * Removing All lesson with topic
249 */
250
251 $wpdb->update(
252 $wpdb->posts,
253 array('post_parent' => 0),
254 array('post_parent' => $topic['topic_id'])
255 );
256
257 /**
258 * Lesson Attaching with topic ID
259 * sorting lesson
260 */
261 if (isset($topic['lesson_ids'])){
262 $lesson_ids = $topic['lesson_ids'];
263 }else{
264 $lesson_ids = array();
265 }
266 if (count($lesson_ids)){
267 foreach ($lesson_ids as $lesson_key => $lesson_id ){
268 $wpdb->update(
269 $wpdb->posts,
270 array('post_parent' => $topic['topic_id'], 'menu_order' => $lesson_key),
271 array('ID' => $lesson_id)
272 );
273 }
274 }
275 }
276 }
277 }
278
279 //Video
280 if ( ! empty($_POST['video']['source'])){
281 //$video = tutor_utils()->sanitize_array($_POST['video']);
282 $video = tutor_utils()->array_get('video', $_POST);
283 update_post_meta($post_ID, '_video', $video);
284 }else{
285 delete_post_meta($post_ID, '_video');
286 }
287
288 /**
289 * Adding author to instructor automatically
290 */
291
292 $author_id = $post->post_author;
293 $attached = (int) $wpdb->get_var(" SELECT COUNT(umeta_id) FROM {$wpdb->usermeta} WHERE user_id = {$author_id} AND meta_key = '_tutor_instructor_course_id' AND meta_value = {$post_ID} ");
294 if ( ! $attached){
295 add_user_meta($author_id, '_tutor_instructor_course_id', $post_ID);
296 }
297
298 //Announcements
299 if ( ! wp_doing_ajax()) {
300 $announcement_title = tutor_utils()->avalue_dot( 'announcements.title', $_POST );
301 if ( ! empty( $announcement_title ) ) {
302 $title = sanitize_text_field( tutor_utils()->avalue_dot( 'announcements.title', $_POST ) );
303 $content = wp_kses_post( tutor_utils()->avalue_dot( 'announcements.content', $_POST ) );
304
305 $post_arr = array(
306 'post_type' => 'tutor_announcements',
307 'post_title' => $title,
308 'post_content' => $content,
309 'post_status' => 'publish',
310 'post_author' => get_current_user_id(),
311 'post_parent' => $post_ID,
312 );
313 wp_insert_post( $post_arr );
314 }
315 }
316 }
317
318 /**
319 * Tutor add course topic
320 */
321 public function tutor_add_course_topic(){
322 if (empty($_POST['topic_title']) ) {
323 wp_send_json_error();
324 }
325 $course_id = (int) tutor_utils()->avalue_dot('tutor_topic_course_ID', $_POST);
326 $next_topic_order_id = tutor_utils()->get_next_topic_order_id($course_id);
327
328 $topic_title = sanitize_text_field( $_POST['topic_title'] );
329 $topic_summery = wp_kses_post( $_POST['topic_summery'] );
330
331 $post_arr = array(
332 'post_type' => 'topics',
333 'post_title' => $topic_title,
334 'post_content' => $topic_summery,
335 'post_status' => 'publish',
336 'post_author' => get_current_user_id(),
337 'post_parent' => $course_id,
338 'menu_order' => $next_topic_order_id,
339 );
340 $current_topic_id = wp_insert_post( $post_arr );
341
342 ob_start();
343 include tutor()->path.'views/metabox/course-contents.php';
344 $course_contents = ob_get_clean();
345
346 wp_send_json_success(array('course_contents' => $course_contents));
347 }
348
349 /**
350 * Update the topic
351 */
352 public function tutor_update_topic(){
353 $topic_id = (int) sanitize_text_field($_POST['topic_id']);
354 $topic_title = sanitize_text_field($_POST['topic_title']);
355 $topic_summery = wp_kses_post($_POST['topic_summery']);
356
357 $topic_attr = array(
358 'ID' => $topic_id,
359 'post_title' => $topic_title,
360 'post_content' => $topic_summery,
361 );
362 wp_update_post( $topic_attr );
363
364 wp_send_json_success(array('msg' => __('Topic has been updated', 'tutor') ));
365 }
366
367
368 /**
369 * @param $columns
370 *
371 * @return mixed
372 *
373 * Add Lesson column
374 */
375
376 public function add_column($columns){
377 $date_col = $columns['date'];
378 unset($columns['date']);
379 $columns['lessons'] = __('Lessons', 'tutor');
380 $columns['students'] = __('Students', 'tutor');
381 $columns['price'] = __('Price', 'tutor');
382 $columns['date'] = $date_col;
383
384 return $columns;
385 }
386
387 /**
388 * @param $column
389 * @param $post_id
390 *
391 */
392 public function custom_lesson_column($column, $post_id ){
393 if ($column === 'lessons'){
394 echo tutor_utils()->get_lesson_count_by_course($post_id);
395 }
396
397 if ($column === 'students'){
398 echo tutor_utils()->count_enrolled_users_by_course($post_id);
399 }
400
401 if ($column === 'price'){
402 $price = tutor_utils()->get_course_price($post_id);
403 if ($price){
404 $monetize_by = tutils()->get_option('monetize_by');
405 if (function_exists('wc_price') && $monetize_by === 'wc'){
406 echo '<span class="tutor-label-success">'.wc_price($price).'</span>';
407 }else{
408 echo '<span class="tutor-label-success">'.$price.'</span>';
409 }
410 }else{
411 echo 'free';
412 }
413 }
414 }
415
416
417 public function tutor_delete_topic(){
418 if (!isset($_GET[tutor()->nonce]) || !wp_verify_nonce($_GET[tutor()->nonce], tutor()->nonce_action)) {
419 exit();
420 }
421 if ( ! isset($_GET['topic_id'])){
422 exit();
423 }
424
425 global $wpdb;
426
427 $topic_id = (int) sanitize_text_field($_GET['topic_id']);
428 $wpdb->update(
429 $wpdb->posts,
430 array('post_parent' => 0),
431 array('post_parent' => $topic_id)
432 );
433
434 $wpdb->delete(
435 $wpdb->postmeta,
436 array('post_id' => $topic_id)
437 );
438
439 wp_delete_post($topic_id);
440 wp_safe_redirect(wp_get_referer());
441 }
442
443 public function tutor_delete_announcement(){
444 tutor_utils()->checking_nonce('get');
445
446 $announcement_id = (int) sanitize_text_field($_GET['topic_id']);
447
448 wp_delete_post($announcement_id);
449 wp_safe_redirect(wp_get_referer());
450 }
451
452 public function enroll_now(){
453
454 //Checking if action comes from Enroll form
455 if (tutor_utils()->array_get('tutor_course_action', $_POST) !== '_tutor_course_enroll_now' || ! isset($_POST['tutor_course_id']) ){
456 return;
457 }
458 //Checking Nonce
459 tutor_utils()->checking_nonce();
460
461 $user_id = get_current_user_id();
462 if ( ! $user_id){
463 exit(__('Please Sign In first', 'tutor'));
464 }
465
466 $course_id = (int) sanitize_text_field($_POST['tutor_course_id']);
467 $user_id = get_current_user_id();
468
469 /**
470 * TODO: need to check purchase information
471 */
472
473 $is_purchasable = tutor_utils()->is_course_purchasable($course_id);
474
475 /**
476 * If is is not purchasable, it's free, and enroll right now
477 *
478 * if purchasable, then process purchase.
479 *
480 * @since: v.1.0.0
481 */
482 if ($is_purchasable){
483 //process purchase
484
485 }else{
486 //Free enroll
487 tutor_utils()->do_enroll($course_id);
488 }
489
490 $referer_url = wp_get_referer();
491 wp_redirect($referer_url);
492 }
493
494 /**
495 *
496 * Mark complete completed
497 *
498 * @since v.1.0.0
499 */
500 public function mark_course_complete(){
501 if ( ! isset($_POST['tutor_action']) || $_POST['tutor_action'] !== 'tutor_complete_course' ){
502 return;
503 }
504 //Checking nonce
505 tutor_utils()->checking_nonce();
506
507 $user_id = get_current_user_id();
508
509 //TODO: need to show view if not signed_in
510 if ( ! $user_id){
511 die(__('Please Sign-In', 'tutor'));
512 }
513
514 $course_id = (int) sanitize_text_field($_POST['course_id']);
515
516 do_action('tutor_course_complete_before', $course_id);
517 /**
518 * Marking course completed at Comment
519 */
520
521 global $wpdb;
522
523 $date = date("Y-m-d H:i:s");
524
525 //Making sure that, hash is unique
526 do{
527 $hash = substr(md5(wp_generate_password(32).$date.$course_id.$user_id), 0, 16);
528 $hasHash = (int) $wpdb->get_var("SELECT COUNT(comment_ID) from {$wpdb->comments} WHERE comment_agent = 'TutorLMSPlugin' AND comment_type = 'course_completed' AND comment_content = '{$hash}' ");
529 }while($hasHash > 0);
530
531 $data = array(
532 'comment_post_ID' => $course_id,
533 'comment_author' => $user_id,
534 'comment_date' => $date,
535 'comment_date_gmt' => get_gmt_from_date($date),
536 'comment_content' => $hash, //Identification Hash
537 'comment_approved' => 'approved',
538 'comment_agent' => 'TutorLMSPlugin',
539 'comment_type' => 'course_completed',
540 'user_id' => $user_id,
541 );
542
543 $wpdb->insert($wpdb->comments, $data);
544
545 do_action('tutor_course_complete_after', $course_id);
546
547 wp_redirect(get_the_permalink($course_id));
548 }
549
550
551 public function tutor_load_instructors_modal(){
552 global $wpdb;
553
554 $course_id = (int) sanitize_text_field($_POST['course_id']);
555 $search_terms = sanitize_text_field(tutor_utils()->avalue_dot('search_terms', $_POST));
556
557 $saved_instructors = tutor_utils()->get_instructors_by_course($course_id);
558
559 $instructors = array();
560
561
562 $not_in_sql = apply_filters('tutor_instructor_query_when_exists', " AND ID <1 ");
563
564 if ($saved_instructors){
565 $saved_instructors_ids = wp_list_pluck($saved_instructors, 'ID');
566 $instructor_not_in_ids = implode(',', $saved_instructors_ids);
567 $not_in_sql .= "AND ID NOT IN($instructor_not_in_ids) ";
568 }
569
570 $search_sql = '';
571 if ($search_terms){
572 $search_sql = "AND (user_login like '%{$search_terms}%' or user_nicename like '%{$search_terms}%' or display_name like '%{$search_terms}%') ";
573 }
574
575 $instructors = $wpdb->get_results("select ID, display_name from {$wpdb->users}
576 INNER JOIN {$wpdb->usermeta} ON ID = user_id AND meta_key = '_tutor_instructor_status' AND meta_value = 'approved'
577 WHERE 1=1 {$not_in_sql} {$search_sql} limit 10 ");
578
579 $output = '';
580 if (is_array($instructors) && count($instructors)){
581 $instructor_output = '';
582 foreach ($instructors as $instructor){
583 $instructor_output .= "<p><label><input type='radio' name='tutor_instructor_ids[]' value='{$instructor->ID}' > {$instructor->display_name} </label></p>";
584 }
585
586 $output .= apply_filters('tutor_course_instructors_html', $instructor_output, $instructors);
587
588 }else{
589 $output .= __('<p>No instructor available or you have already added maximum instructors</p>', 'tutor');
590 }
591
592
593 if ( ! defined('TUTOR_MT_VERSION')){
594 $output .= '<p class="tutor-notice-warning" style="margin-top: 50px; font-size: 14px;">'. sprintf( __('To add unlimited multiple instructors in your course, get %sTutor LMS Pro%s', 'tutor'), '<a href="https://www.themeum.com/product/tutor-lms" target="_blank">', "</a>" ) .'</p>';
595 }
596
597 wp_send_json_success(array('output' => $output));
598 }
599
600 public function tutor_add_instructors_to_course(){
601 $course_id = (int) sanitize_text_field($_POST['course_id']);
602 $instructor_ids = tutor_utils()->avalue_dot('tutor_instructor_ids', $_POST);
603
604 if (is_array($instructor_ids) && count($instructor_ids)){
605 foreach ($instructor_ids as $instructor_id){
606 add_user_meta($instructor_id, '_tutor_instructor_course_id', $course_id);
607 }
608 }
609
610 $saved_instructors = tutor_utils()->get_instructors_by_course($course_id);
611 $output = '';
612
613 if ($saved_instructors){
614 foreach ($saved_instructors as $t){
615
616 $output .= '<div id="added-instructor-id-'.$t->ID.'" class="added-instructor-item added-instructor-item-'.$t->ID.'" data-instructor-id="'.$t->ID.'">
617 <span class="instructor-icon">'.get_avatar($t->ID, 30).'</span>
618 <span class="instructor-name"> '.$t->display_name.' </span>
619 <span class="instructor-control">
620 <a href="javascript:;" class="tutor-instructor-delete-btn"><i class="tutor-icon-line-cross"></i></a>
621 </span>
622 </div>';
623 }
624 }
625
626 wp_send_json_success(array('output' => $output));
627 }
628
629 public function detach_instructor_from_course(){
630 global $wpdb;
631
632 $instructor_id = (int) sanitize_text_field($_POST['instructor_id']);
633 $course_id = (int) sanitize_text_field($_POST['course_id']);
634
635 $wpdb->delete($wpdb->usermeta, array('user_id' => $instructor_id, 'meta_key' => '_tutor_instructor_course_id', 'meta_value' => $course_id) );
636 wp_send_json_success();
637 }
638
639 public function tutor_delete_dashboard_course(){
640 $course_id = intval(sanitize_text_field($_POST['course_id']));
641 wp_trash_post($course_id);
642 wp_send_json_success();
643 }
644
645
646 public function tutor_add_gutenberg_author($data , $postarr){
647 global $wpdb;
648
649 $post_author = (int) tutor_utils()->avalue_dot('post_author', $data);
650
651 if ( ! $post_author){
652 $user_ID = (int) tutor_utils()->avalue_dot('user_ID', $postarr);
653 if ($user_ID){
654 $data['post_author'] = $user_ID;
655 }else{
656 global $wpdb;
657
658 $post_ID = (int) tutor_utils()->avalue_dot('ID', $postarr);
659 $post_author = (int) $wpdb->get_var("SELECT post_author FROM {$wpdb->posts} WHERE ID = {$post_ID} ");
660
661 $data['post_author'] = $post_author;
662 }
663 }
664
665 return $data;
666 }
667
668
669 /**
670 * @param $post_ID
671 * @param $postData
672 *
673 * Attach product during save course from the frontend course dashboard.
674 *
675 * @return string
676 *
677 * @since v.1.3.4
678 */
679
680 public function attach_product_with_course($post_ID, $postData){
681 $attached_product_id = tutor_utils()->get_course_product_id($post_ID);
682 $course_price = sanitize_text_field(tutor_utils()->array_get('course_price', $_POST));
683
684 if ( ! $course_price){
685 return;
686 }
687
688 $monetize_by = tutor_utils()->get_option('monetize_by');
689 $course = get_post($post_ID);
690
691 if ($monetize_by === 'wc'){
692
693 $is_update = false;
694 if ($attached_product_id){
695 $wc_product = get_post_meta($attached_product_id, '_product_version', true);
696 if ($wc_product){
697 $is_update = true;
698 }
699 }
700
701 if ($is_update){
702
703 $productObj = new \WC_Product($attached_product_id);
704 $productObj->set_price($course_price); // set product price
705 $productObj->set_regular_price($course_price); // set product regular price
706 $product_id = $productObj->save();
707
708 }else{
709
710 $productObj = new \WC_Product();
711 $productObj->set_name($course->post_title);
712 $productObj->set_status('publish');
713 $productObj->set_price($course_price); // set product price
714 $productObj->set_regular_price($course_price); // set product regular price
715
716 $product_id = $productObj->save();
717 if ($product_id) {
718 update_post_meta( $post_ID, '_tutor_course_product_id', $product_id );
719 //Mark product for woocommerce
720 update_post_meta( $product_id, '_virtual', 'yes' );
721 update_post_meta( $product_id, '_tutor_product', 'yes' );
722
723 $coursePostThumbnail = get_post_meta( $post_ID, '_thumbnail_id', true );
724 if ( $coursePostThumbnail ) {
725 set_post_thumbnail( $product_id, $coursePostThumbnail );
726 }
727 }
728 }
729
730 }elseif ($monetize_by === 'edd'){
731
732 $is_update = false;
733
734 if ($attached_product_id){
735 $edd_price = get_post_meta($attached_product_id, 'edd_price', true);
736 if ($edd_price){
737 $is_update = true;
738 }
739 }
740
741 if ($is_update){
742 //Update the product
743 update_post_meta( $attached_product_id, 'edd_price', $course_price );
744 }else{
745 //Create new product
746
747 $post_arr = array(
748 'post_type' => 'download',
749 'post_title' => $course->post_title,
750 'post_status' => 'publish',
751 'post_author' => get_current_user_id(),
752 );
753 $download_id = wp_insert_post( $post_arr );
754 if ($download_id ) {
755 //edd_price
756 update_post_meta( $download_id, 'edd_price', $course_price );
757
758 update_post_meta( $post_ID, '_tutor_course_product_id', $download_id );
759 //Mark product for EDD
760 update_post_meta( $download_id, '_tutor_product', 'yes' );
761
762 $coursePostThumbnail = get_post_meta( $post_ID, '_thumbnail_id', true );
763 if ( $coursePostThumbnail ) {
764 set_post_thumbnail( $download_id, $coursePostThumbnail );
765 }
766
767 }
768
769 }
770
771
772 }
773
774 }
775
776
777 /**
778 * Add Course level to course settings
779 * @since v.1.4.1
780 */
781 public function add_course_level_to_settings(){
782 include tutor()->path.'views/metabox/course-level-metabox.php';
783 }
784
785
786
787 }