PluginProbe ʕ •ᴥ•ʔ
Tutor LMS – eLearning and online course solution / 1.3.4
Tutor LMS – eLearning and online course solution v1.3.4
4.0.1 4.0.0 3.9.15 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_Widget.php 6 years ago Dashboard.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.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
759 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 * Registering metabox
58 */
59 public function register_meta_box(){
60 $coursePostType = tutor()->course_post_type;
61 $course_marketplace = tutor_utils()->get_option('enable_course_marketplace');
62 add_meta_box( 'tutor-course-levels', __( 'Course Level', 'tutor' ), array($this, 'course_level_metabox'), $coursePostType );
63 add_meta_box( 'tutor-course-topics', __( 'Course Builder', 'tutor' ), array($this, 'course_meta_box'), $coursePostType );
64 add_meta_box( 'tutor-course-additional-data', __( 'Additional Data', 'tutor' ), array($this, 'course_additional_data_meta_box'), $coursePostType );
65 add_meta_box( 'tutor-course-videos', __( 'Video', 'tutor' ), array($this, 'video_metabox'), $coursePostType );
66 if ($course_marketplace) {
67 add_meta_box( 'tutor-instructors', __( 'Instructors', 'tutor' ), array( $this, 'instructors_metabox' ), $coursePostType );
68 }
69 add_meta_box( 'tutor-announcements', __( 'Announcements', 'tutor' ), array($this, 'announcements_metabox'), $coursePostType );
70 }
71 public function course_meta_box($echo = true){
72 ob_start();
73 include tutor()->path.'views/metabox/course-topics.php';
74 $content = ob_get_clean();
75
76 if ($echo){
77 echo $content;
78 }else{
79 return $content;
80 }
81 }
82 public function course_additional_data_meta_box($echo = true){
83
84 ob_start();
85 include tutor()->path.'views/metabox/course-additional-data.php';
86 $content = ob_get_clean();
87
88 if ($echo){
89 echo $content;
90 }else{
91 return $content;
92 }
93 }
94 public function video_metabox($echo = true){
95 ob_start();
96 include tutor()->path.'views/metabox/video-metabox.php';
97 $content = ob_get_clean();
98
99 if ($echo){
100 echo $content;
101 }else{
102 return $content;
103 }
104 }
105
106 public function course_level_metabox($echo = true){
107 ob_start();
108 include tutor()->path.'views/metabox/course-level-metabox.php';
109 $content = ob_get_clean();
110
111 if ($echo){
112 echo $content;
113 }else{
114 return $content;
115 }
116 }
117
118 public function announcements_metabox($echo = true){
119 ob_start();
120 include tutor()->path.'views/metabox/announcements-metabox.php';
121 $content = ob_get_clean();
122
123 if ($echo){
124 echo $content;
125 }else{
126 return $content;
127 }
128 }
129
130 public function instructors_metabox($echo = true){
131 ob_start();
132 include tutor()->path . 'views/metabox/instructors-metabox.php';
133 $content = ob_get_clean();
134
135 if ($echo){
136 echo $content;
137 }else{
138 return $content;
139 }
140 }
141
142 /**
143 * Register metabox in course builder tutor
144 * @since v.1.3.4
145 */
146 public function register_meta_box_in_frontend(){
147 do_action('tutor_course_builder_metabox_before', get_the_ID());
148 course_builder_section_wrap($this->video_metabox($echo = false), 'Video');
149 course_builder_section_wrap($this->course_meta_box($echo = false), 'Course Builder');
150 course_builder_section_wrap($this->instructors_metabox($echo = false), 'Instructors');
151 course_builder_section_wrap($this->course_additional_data_meta_box($echo = false), 'Additional Data');
152 course_builder_section_wrap($this->announcements_metabox($echo = false), 'Announcements');
153 do_action('tutor_course_builder_metabox_after', get_the_ID());
154 }
155
156 /**
157 * @param $post_ID
158 *
159 * Insert Topic and attached it with Course
160 */
161 public function save_course_meta($post_ID, $post){
162 global $wpdb;
163 /**
164 * Insert Topic
165 */
166 if ( ! empty($_POST['topic_title'])) {
167 $topic_title = sanitize_text_field( $_POST['topic_title'] );
168 $topic_summery = wp_kses_post( $_POST['topic_summery'] );
169
170 $post_arr = array(
171 'post_type' => 'topics',
172 'post_title' => $topic_title,
173 'post_content' => $topic_summery,
174 'post_status' => 'publish',
175 'post_author' => get_current_user_id(),
176 'post_parent' => $post_ID,
177 );
178 wp_insert_post( $post_arr );
179 }
180
181 //Course Duration
182 if ( ! empty($_POST['course_duration'])){
183 $video = tutor_utils()->sanitize_array($_POST['course_duration']);
184 update_post_meta($post_ID, '_course_duration', $video);
185 }
186
187 if ( ! empty($_POST['course_level'])){
188 $course_level = sanitize_text_field($_POST['course_level']);
189 update_post_meta($post_ID, '_tutor_course_level', $course_level);
190 }
191
192 if ( ! empty($_POST['course_benefits'])){
193 $course_benefits = wp_kses_post($_POST['course_benefits']);
194 update_post_meta($post_ID, '_tutor_course_benefits', $course_benefits);
195 }
196
197 if ( ! empty($_POST['course_requirements'])){
198 $requirements = wp_kses_post($_POST['course_requirements']);
199 update_post_meta($post_ID, '_tutor_course_requirements', $requirements);
200 }
201
202 if ( ! empty($_POST['course_target_audience'])){
203 $target_audience = wp_kses_post($_POST['course_target_audience']);
204 update_post_meta($post_ID, '_tutor_course_target_audience', $target_audience);
205 }
206
207 if ( ! empty($_POST['course_material_includes'])){
208 $material_includes = wp_kses_post($_POST['course_material_includes']);
209 update_post_meta($post_ID, '_tutor_course_material_includes', $material_includes);
210 }
211 /**
212 * Sorting Topics and lesson
213 */
214 if ( ! empty($_POST['tutor_topics_lessons_sorting'])){
215 $new_order = sanitize_text_field(stripslashes($_POST['tutor_topics_lessons_sorting']));
216 $order = json_decode($new_order, true);
217
218 if (is_array($order) && count($order)){
219 $i = 0;
220 foreach ($order as $topic ){
221 $i++;
222 $wpdb->update(
223 $wpdb->posts,
224 array('menu_order' => $i),
225 array('ID' => $topic['topic_id'])
226 );
227
228 /**
229 * Removing All lesson with topic
230 */
231
232 $wpdb->update(
233 $wpdb->posts,
234 array('post_parent' => 0),
235 array('post_parent' => $topic['topic_id'])
236 );
237
238 /**
239 * Lesson Attaching with topic ID
240 * sorting lesson
241 */
242 if (isset($topic['lesson_ids'])){
243 $lesson_ids = $topic['lesson_ids'];
244 }else{
245 $lesson_ids = array();
246 }
247 if (count($lesson_ids)){
248 foreach ($lesson_ids as $lesson_key => $lesson_id ){
249 $wpdb->update(
250 $wpdb->posts,
251 array('post_parent' => $topic['topic_id'], 'menu_order' => $lesson_key),
252 array('ID' => $lesson_id)
253 );
254 }
255 }
256 }
257 }
258 }
259
260 //Video
261 if ( ! empty($_POST['video']['source'])){
262 //$video = tutor_utils()->sanitize_array($_POST['video']);
263 $video = tutor_utils()->array_get('video', $_POST);
264 update_post_meta($post_ID, '_video', $video);
265 }else{
266 delete_post_meta($post_ID, '_video');
267 }
268
269 /**
270 * Adding author to instructor automatically
271 */
272
273 $author_id = $post->post_author;
274 $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} ");
275 if ( ! $attached){
276 add_user_meta($author_id, '_tutor_instructor_course_id', $post_ID);
277 }
278
279 //Announcements
280 if ( ! wp_doing_ajax()) {
281 $announcement_title = tutor_utils()->avalue_dot( 'announcements.title', $_POST );
282 if ( ! empty( $announcement_title ) ) {
283 $title = sanitize_text_field( tutor_utils()->avalue_dot( 'announcements.title', $_POST ) );
284 $content = wp_kses_post( tutor_utils()->avalue_dot( 'announcements.content', $_POST ) );
285
286 $post_arr = array(
287 'post_type' => 'tutor_announcements',
288 'post_title' => $title,
289 'post_content' => $content,
290 'post_status' => 'publish',
291 'post_author' => get_current_user_id(),
292 'post_parent' => $post_ID,
293 );
294 wp_insert_post( $post_arr );
295 }
296 }
297 }
298
299 /**
300 * Tutor add course topic
301 */
302 public function tutor_add_course_topic(){
303 if (empty($_POST['topic_title'])) {
304 wp_send_json_error();
305 }
306 $course_id = (int) tutor_utils()->avalue_dot('tutor_topic_course_ID', $_POST);
307 $next_topic_order_id = tutor_utils()->get_next_topic_order_id($course_id);
308
309 $topic_title = sanitize_text_field( $_POST['topic_title'] );
310 $topic_summery = wp_kses_post( $_POST['topic_summery'] );
311
312 $post_arr = array(
313 'post_type' => 'topics',
314 'post_title' => $topic_title,
315 'post_content' => $topic_summery,
316 'post_status' => 'publish',
317 'post_author' => get_current_user_id(),
318 'post_parent' => $course_id,
319 'menu_order' => $next_topic_order_id,
320 );
321 $current_topic_id = wp_insert_post( $post_arr );
322
323 ob_start();
324 include tutor()->path.'views/metabox/course-contents.php';
325 $course_contents = ob_get_clean();
326
327 wp_send_json_success(array('course_contents' => $course_contents));
328 }
329
330 /**
331 * Update the topic
332 */
333 public function tutor_update_topic(){
334 $topic_id = (int) sanitize_text_field($_POST['topic_id']);
335 $topic_title = sanitize_text_field($_POST['topic_title']);
336 $topic_summery = wp_kses_post($_POST['topic_summery']);
337
338 $topic_attr = array(
339 'ID' => $topic_id,
340 'post_title' => $topic_title,
341 'post_content' => $topic_summery,
342 );
343 wp_update_post( $topic_attr );
344
345 wp_send_json_success(array('msg' => __('Topic has been updated', 'tutor') ));
346 }
347
348
349 /**
350 * @param $columns
351 *
352 * @return mixed
353 *
354 * Add Lesson column
355 */
356
357 public function add_column($columns){
358 $date_col = $columns['date'];
359 unset($columns['date']);
360 $columns['lessons'] = __('Lessons', 'tutor');
361 $columns['students'] = __('Students', 'tutor');
362 $columns['price'] = __('Price', 'tutor');
363 $columns['date'] = $date_col;
364
365 return $columns;
366 }
367
368 /**
369 * @param $column
370 * @param $post_id
371 *
372 */
373 public function custom_lesson_column($column, $post_id ){
374 if ($column === 'lessons'){
375 echo tutor_utils()->get_lesson_count_by_course($post_id);
376 }
377
378 if ($column === 'students'){
379 echo tutor_utils()->count_enrolled_users_by_course($post_id);
380 }
381
382 if ($column === 'price'){
383 $price = tutor_utils()->get_course_price($post_id);
384
385 if ($price){
386 if (function_exists('wc_price')){
387 echo '<span class="tutor-label-success">'.wc_price($price).'</span>';
388 }else{
389 echo '<span class="tutor-label-success">'.$price.'</span>';
390 }
391 }else{
392 echo 'free';
393 }
394 }
395 }
396
397
398 public function tutor_delete_topic(){
399 if (!isset($_GET[tutor()->nonce]) || !wp_verify_nonce($_GET[tutor()->nonce], tutor()->nonce_action)) {
400 exit();
401 }
402 if ( ! isset($_GET['topic_id'])){
403 exit();
404 }
405
406 global $wpdb;
407
408 $topic_id = (int) sanitize_text_field($_GET['topic_id']);
409 $wpdb->update(
410 $wpdb->posts,
411 array('post_parent' => 0),
412 array('post_parent' => $topic_id)
413 );
414
415 $wpdb->delete(
416 $wpdb->postmeta,
417 array('post_id' => $topic_id)
418 );
419
420 wp_delete_post($topic_id);
421 wp_safe_redirect(wp_get_referer());
422 }
423
424 public function tutor_delete_announcement(){
425 tutor_utils()->checking_nonce('get');
426
427 $announcement_id = (int) sanitize_text_field($_GET['topic_id']);
428
429 wp_delete_post($announcement_id);
430 wp_safe_redirect(wp_get_referer());
431 }
432
433 public function enroll_now(){
434
435 //Checking if action comes from Enroll form
436 if (tutor_utils()->array_get('tutor_course_action', $_POST) !== '_tutor_course_enroll_now' || ! isset($_POST['tutor_course_id']) ){
437 return;
438 }
439 //Checking Nonce
440 tutor_utils()->checking_nonce();
441
442 $user_id = get_current_user_id();
443 if ( ! $user_id){
444 exit(__('Please Sign In first', 'tutor'));
445 }
446
447 $course_id = (int) sanitize_text_field($_POST['tutor_course_id']);
448 $user_id = get_current_user_id();
449
450 /**
451 * TODO: need to check purchase information
452 */
453
454 $is_purchasable = tutor_utils()->is_course_purchasable($course_id);
455
456 /**
457 * If is is not purchasable, it's free, and enroll right now
458 *
459 * if purchasable, then process purchase.
460 *
461 * @since: v.1.0.0
462 */
463 if ($is_purchasable){
464 //process purchase
465
466 }else{
467 //Free enroll
468 tutor_utils()->do_enroll($course_id);
469 }
470
471 $referer_url = wp_get_referer();
472 wp_redirect($referer_url);
473 }
474
475 /**
476 *
477 * Mark complete completed
478 *
479 * @since v.1.0.0
480 */
481 public function mark_course_complete(){
482 if ( ! isset($_POST['tutor_action']) || $_POST['tutor_action'] !== 'tutor_complete_course' ){
483 return;
484 }
485 //Checking nonce
486 tutor_utils()->checking_nonce();
487
488 $user_id = get_current_user_id();
489
490 //TODO: need to show view if not signed_in
491 if ( ! $user_id){
492 die(__('Please Sign-In', 'tutor'));
493 }
494
495 $course_id = (int) sanitize_text_field($_POST['course_id']);
496
497 do_action('tutor_course_complete_before', $course_id);
498 /**
499 * Marking course completed at Comment
500 */
501
502 global $wpdb;
503
504 $date = date("Y-m-d H:i:s");
505
506 //Making sure that, hash is unique
507 do{
508 $hash = substr(md5(wp_generate_password(32).$date.$course_id.$user_id), 0, 16);
509 $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}' ");
510 }while($hasHash > 0);
511
512 $data = array(
513 'comment_post_ID' => $course_id,
514 'comment_author' => $user_id,
515 'comment_date' => $date,
516 'comment_date_gmt' => get_gmt_from_date($date),
517 'comment_content' => $hash, //Identification Hash
518 'comment_approved' => 'approved',
519 'comment_agent' => 'TutorLMSPlugin',
520 'comment_type' => 'course_completed',
521 'user_id' => $user_id,
522 );
523
524 $wpdb->insert($wpdb->comments, $data);
525
526 do_action('tutor_course_complete_after', $course_id);
527
528 wp_redirect(get_the_permalink($course_id));
529 }
530
531
532 public function tutor_load_instructors_modal(){
533 global $wpdb;
534
535 $course_id = (int) sanitize_text_field($_POST['course_id']);
536 $search_terms = sanitize_text_field(tutor_utils()->avalue_dot('search_terms', $_POST));
537
538 $saved_instructors = tutor_utils()->get_instructors_by_course($course_id);
539
540 $instructors = array();
541
542
543 $not_in_sql = apply_filters('tutor_instructor_query_when_exists', " AND ID <1 ");
544
545 if ($saved_instructors){
546 $saved_instructors_ids = wp_list_pluck($saved_instructors, 'ID');
547 $instructor_not_in_ids = implode(',', $saved_instructors_ids);
548 $not_in_sql .= "AND ID NOT IN($instructor_not_in_ids) ";
549 }
550
551 $search_sql = '';
552 if ($search_terms){
553 $search_sql = "AND (user_login like '%{$search_terms}%' or user_nicename like '%{$search_terms}%' or display_name like '%{$search_terms}%') ";
554 }
555
556 $instructors = $wpdb->get_results("select ID, display_name from {$wpdb->users}
557 INNER JOIN {$wpdb->usermeta} ON ID = user_id AND meta_key = '_tutor_instructor_status' AND meta_value = 'approved'
558 WHERE 1=1 {$not_in_sql} {$search_sql} limit 10 ");
559
560 $output = '';
561 if (is_array($instructors) && count($instructors)){
562 $instructor_output = '';
563 foreach ($instructors as $instructor){
564 $instructor_output .= "<p><label><input type='radio' name='tutor_instructor_ids[]' value='{$instructor->ID}' > {$instructor->display_name} </label></p>";
565 }
566
567 $output .= apply_filters('tutor_course_instructors_html', $instructor_output, $instructors);
568 $output .= '<p class="quiz-search-suggest-text">'.__('Search to get the specific instructors', 'tutor').'</p>';
569
570 }else{
571 $output .= __('<p>No instructor available or you have already added maximum instructors</p>', 'tutor');
572 }
573
574
575 if ( ! defined('TUTOR_MT_VERSION')){
576 $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>';
577 }
578
579 wp_send_json_success(array('output' => $output));
580 }
581
582 public function tutor_add_instructors_to_course(){
583 $course_id = (int) sanitize_text_field($_POST['course_id']);
584 $instructor_ids = tutor_utils()->avalue_dot('tutor_instructor_ids', $_POST);
585
586 if (is_array($instructor_ids) && count($instructor_ids)){
587 foreach ($instructor_ids as $instructor_id){
588 add_user_meta($instructor_id, '_tutor_instructor_course_id', $course_id);
589 }
590 }
591
592 $saved_instructors = tutor_utils()->get_instructors_by_course($course_id);
593 $output = '';
594
595 if ($saved_instructors){
596 foreach ($saved_instructors as $t){
597
598 $output .= '<div id="added-instructor-id-'.$t->ID.'" class="added-instructor-item added-instructor-item-'.$t->ID.'" data-instructor-id="'.$t->ID.'">
599 <span class="instructor-icon">'.get_avatar($t->ID, 30).'</span>
600 <span class="instructor-name"> '.$t->display_name.' </span>
601 <span class="instructor-control">
602 <a href="javascript:;" class="tutor-instructor-delete-btn"><i class="tutor-icon-line-cross"></i></a>
603 </span>
604 </div>';
605 }
606 }
607
608 wp_send_json_success(array('output' => $output));
609 }
610
611 public function detach_instructor_from_course(){
612 global $wpdb;
613
614 $instructor_id = (int) sanitize_text_field($_POST['instructor_id']);
615 $course_id = (int) sanitize_text_field($_POST['course_id']);
616
617 $wpdb->delete($wpdb->usermeta, array('user_id' => $instructor_id, 'meta_key' => '_tutor_instructor_course_id', 'meta_value' => $course_id) );
618 wp_send_json_success();
619 }
620
621 public function tutor_delete_dashboard_course(){
622 $course_id = intval(sanitize_text_field($_POST['course_id']));
623 wp_trash_post($course_id);
624 wp_send_json_success();
625 }
626
627
628 public function tutor_add_gutenberg_author($data , $postarr){
629 global $wpdb;
630
631 $post_author = (int) tutor_utils()->avalue_dot('post_author', $data);
632
633 if ( ! $post_author){
634 $user_ID = (int) tutor_utils()->avalue_dot('user_ID', $postarr);
635 if ($user_ID){
636 $data['post_author'] = $user_ID;
637 }else{
638 global $wpdb;
639
640 $post_ID = (int) tutor_utils()->avalue_dot('ID', $postarr);
641 $post_author = (int) $wpdb->get_var("SELECT post_author FROM {$wpdb->posts} WHERE ID = {$post_ID} ");
642
643 $data['post_author'] = $post_author;
644 }
645 }
646
647 return $data;
648 }
649
650
651 /**
652 * @param $post_ID
653 * @param $postData
654 *
655 * Attach product during save course from the frontend course dashboard.
656 *
657 * @return string
658 *
659 * @since v.1.3.4
660 */
661
662 public function attach_product_with_course($post_ID, $postData){
663 $attached_product_id = tutor_utils()->get_course_product_id($post_ID);
664 $course_price = sanitize_text_field(tutor_utils()->array_get('course_price', $_POST));
665 if ( ! $course_price){
666 return;
667 }
668
669 $sell_by_wc = tutor_utils()->get_option('enable_course_sell_by_woocommerce');
670 $sell_by_edd = tutor_utils()->get_option('enable_tutor_edd');
671 $course = get_post($post_ID);
672
673 if ($sell_by_wc){
674
675 $is_update = false;
676 if ($attached_product_id){
677 $wc_product = get_post_meta($attached_product_id, '_product_version', true);
678 if ($wc_product){
679 $is_update = true;
680 }
681 }
682
683 if ($is_update){
684
685 $productObj = new \WC_Product($attached_product_id);
686 $productObj->set_price($course_price); // set product price
687 $productObj->set_regular_price($course_price); // set product regular price
688 $product_id = $productObj->save();
689
690 }else{
691
692 $productObj = new \WC_Product();
693 $productObj->set_name($course->post_title);
694 $productObj->set_status('publish');
695 $productObj->set_price($course_price); // set product price
696 $productObj->set_regular_price($course_price); // set product regular price
697
698 $product_id = $productObj->save();
699 if ($product_id) {
700 update_post_meta( $post_ID, '_tutor_course_product_id', $product_id );
701 //Mark product for woocommerce
702 update_post_meta( $product_id, '_virtual', 'yes' );
703 update_post_meta( $product_id, '_tutor_product', 'yes' );
704
705 $coursePostThumbnail = get_post_meta( $post_ID, '_thumbnail_id', true );
706 if ( $coursePostThumbnail ) {
707 set_post_thumbnail( $product_id, $coursePostThumbnail );
708 }
709 }
710 }
711
712 }elseif ($sell_by_edd){
713 $is_update = false;
714
715 if ($attached_product_id){
716 $edd_price = get_post_meta($attached_product_id, 'edd_price', true);
717 if ($edd_price){
718 $is_update = true;
719 }
720 }
721
722 if ($is_update){
723 //Update the product
724 update_post_meta( $attached_product_id, 'edd_price', $course_price );
725 }else{
726 //Create new product
727
728 $post_arr = array(
729 'post_type' => 'download',
730 'post_title' => $course->post_title,
731 'post_status' => 'publish',
732 'post_author' => get_current_user_id(),
733 );
734 $download_id = wp_insert_post( $post_arr );
735 if ($download_id ) {
736 //edd_price
737 update_post_meta( $download_id, 'edd_price', $course_price );
738
739 update_post_meta( $post_ID, '_tutor_course_product_id', $download_id );
740 //Mark product for EDD
741 update_post_meta( $download_id, '_tutor_product', 'yes' );
742
743 $coursePostThumbnail = get_post_meta( $post_ID, '_thumbnail_id', true );
744 if ( $coursePostThumbnail ) {
745 set_post_thumbnail( $download_id, $coursePostThumbnail );
746 }
747
748 }
749
750 }
751
752
753 }
754
755 }
756
757
758
759 }