PluginProbe
Tutor LMS – eLearning and online course solution / 2.7.7
Tutor LMS – eLearning and online course solution v2.7.7
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 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 All 191 releases
← All changes | classes/Course.php +477 -2224 4.0.32.7.7 View file →
@@ -9,26 +9,14 @@
9 9 */
10 10
11 11 namespace TUTOR;
12 12
13 -defined( 'ABSPATH' ) || exit;
13 +if ( ! defined( 'ABSPATH' ) ) {
14 + exit;
15 +}
14 16
15 -use Tutor\Components\Button;
16 -use Tutor\Components\Constants\Size;
17 -use Tutor\Components\Constants\Variant;
18 -use Tutor\Components\Progress;
19 -use Tutor\Ecommerce\Tax;
20 -use Tutor\Models\QuizModel;
21 -use Tutor\Helpers\HttpHelper;
17 +use TUTOR\Input;
22 18 use Tutor\Models\CourseModel;
23 -use Tutor\Components\Tooltip;
24 -use Tutor\Ecommerce\Ecommerce;
25 -use Tutor\Helpers\DateTimeHelper;
26 -use Tutor\Traits\JsonResponse;
27 -use Tutor\Helpers\ValidationHelper;
28 -use Tutor\Models\EnrollmentModel;
29 -use Tutor\Options_V2;
30 -use TUTOR_ASSIGNMENTS\Assignments;
31 19
32 20 /**
33 21 * Course Class
34 22 *
@@ -34,81 +22,10 @@
34 22 *
35 23 * @since 1.0.0
36 24 */
37 25 class Course extends Tutor_Base {
38 - use JsonResponse;
39 26
40 27 /**
41 - * Course Price type
42 - *
43 - * @since 3.0.0
44 - *
45 - * @var string
46 - */
47 - const PRICE_TYPE_FREE = 'free';
48 - const PRICE_TYPE_PAID = 'paid';
49 - const PRICE_TYPE_SUBSCRIPTION = 'subscription';
50 -
51 - /**
52 - * Course price and sale price
53 - *
54 - * @since 3.0.0
55 - */
56 - const COURSE_PRICE_TYPE_META = '_tutor_course_price_type';
57 - const COURSE_PRICE_META = 'tutor_course_price';
58 - const COURSE_SALE_PRICE_META = 'tutor_course_sale_price';
59 - const COURSE_SELLING_OPTION_META = 'tutor_course_selling_option';
60 - const COURSE_PRODUCT_ID_META = '_tutor_course_product_id';
61 -
62 - /**
63 - * Selling option constants
64 - *
65 - * @since 3.0.0
66 - */
67 - const SELLING_OPTION_ONE_TIME = 'one_time';
68 - const SELLING_OPTION_SUBSCRIPTION = 'subscription';
69 - const SELLING_OPTION_BOTH = 'both'; // onetime and subscription.
70 - const SELLING_OPTION_MEMBERSHIP = 'membership';
71 - const SELLING_OPTION_ALL = 'all';
72 -
73 - /**
74 - * Tax collection settings meta
75 - *
76 - * @since 3.7.0
77 - */
78 - const TAX_ON_SINGLE_META = 'tutor_tax_on_single';
79 - const TAX_ON_SUBSCRIPTION_META = 'tutor_tax_on_subscription';
80 -
81 - /**
82 - * Additional data meta
83 - *
84 - * @since 3.6.0
85 - */
86 - const COURSE_BENEFITS_META = '_tutor_course_benefits';
87 - const COURSE_REQUIREMENTS_META = '_tutor_course_requirements';
88 - const COURSE_TARGET_AUDIENCE_META = '_tutor_course_target_audience';
89 - const COURSE_MATERIAL_INCLUDE_META = '_tutor_course_material_includes';
90 - const COURSE_DURATION_META = '_course_duration';
91 -
92 - /**
93 - * Course settings meta
94 - *
95 - * @since 3.6.0
96 - */
97 - const COURSE_ENABLE_QA_META = '_tutor_enable_qa';
98 - const PUBLIC_COURSE_META = '_tutor_is_public_course';
99 - const COURSE_SETTINGS_META = '_tutor_course_settings';
100 - const COURSE_LEVEL_META = '_tutor_course_level';
101 -
102 - /**
103 - * Meta keys used to identify and store Tutor course order data.
104 - *
105 - * @since 4.0.0
106 - */
107 - const IS_TUTOR_ORDER_FOR_COURSE_META = '_is_tutor_order_for_course';
108 - const TUTOR_ORDER_FOR_COURSE_ID_META = '_tutor_order_for_course_id_';
109 -
110 - /**
111 28 * Additional course meta info
112 29 *
113 30 * @var array
114 31 */
@@ -120,24 +37,23 @@
120 37 /**
121 38 * Constructor
122 39 *
123 40 * @since 1.0.0
124 - * @since 3.0.0 $register_hooks param added to reuse this class.
125 - *
126 - * @param bool $register_hooks register hooks.
127 - *
128 41 * @return void
129 42 */
130 - public function __construct( $register_hooks = true ) {
43 + public function __construct() {
131 44 parent::__construct();
132 45
133 - if ( ! $register_hooks ) {
134 - return;
135 - }
46 + add_action( 'add_meta_boxes', array( $this, 'register_meta_box' ) );
47 + add_action( 'save_post_' . $this->course_post_type, array( $this, 'save_course_meta' ), 10, 2 );
48 + add_action( 'wp_ajax_tutor_save_topic', array( $this, 'tutor_save_topic' ) );
136 49
137 - add_action( 'save_post_' . $this->course_post_type, array( $this, 'save_course_meta' ), 10, 2 );
50 + /**
51 + * Add Column
52 + */
53 + add_filter( "manage_{$this->course_post_type}_posts_columns", array( $this, 'add_column' ), 10, 1 );
54 + add_action( "manage_{$this->course_post_type}_posts_custom_column", array( $this, 'custom_lesson_column' ), 10, 2 );
138 55
139 - add_action( 'wp_ajax_tutor_save_topic', array( $this, 'tutor_save_topic' ) );
140 56 add_action( 'wp_ajax_tutor_delete_topic', array( $this, 'tutor_delete_topic' ) );
141 57
142 58 /**
143 59 * Frontend Action
@@ -152,16 +68,30 @@
152 68
153 69 /**
154 70 * Gutenberg author support
155 71 */
156 - add_filter( 'wp_insert_post_data', array( $this, 'tutor_add_gutenberg_author' ), 99, 2 );
72 + add_filter( 'wp_insert_post_data', array( $this, 'tutor_add_gutenberg_author' ), '99', 2 );
157 73
158 74 /**
75 + * Frontend metabox supports for course builder
76 + *
77 + * @since v.1.3.4
78 + */
79 + add_action( 'tutor/dashboard_course_builder_form_field_after', array( $this, 'register_meta_box_in_frontend' ) );
80 +
81 + /**
159 82 * Do Stuff for the course save from frontend
160 83 */
161 84 add_action( 'save_tutor_course', array( $this, 'attach_product_with_course' ), 10, 2 );
162 85
163 86 /**
87 + * Add course level to course settings
88 + *
89 + * @since v.1.4.1
90 + */
91 + add_filter( 'tutor_course_settings_tabs', array( $this, 'add_course_level_to_settings' ) );
92 +
93 + /**
164 94 * Enable Disable Course Details Page Feature
165 95 *
166 96 * @since v.1.4.8
167 97 */
@@ -252,12 +182,11 @@
252 182 * @since 1.9.8
253 183 */
254 184 add_action( 'tutor_do_enroll_after_login_if_attempt', array( $this, 'enroll_after_login_if_attempt' ), 10, 2 );
255 185
256 - add_action( 'wp_ajax_tutor_update_course_content_order', array( $this, 'ajax_update_course_content_order' ) );
186 + add_action( 'wp_ajax_tutor_update_course_content_order', array( $this, 'tutor_update_course_content_order' ) );
257 187
258 - add_action( 'wp_ajax_tutor_get_wc_product', array( $this, 'get_wc_product' ) );
259 - add_action( 'wp_ajax_tutor_get_wc_products', array( $this, 'get_wc_products' ) );
188 + add_action( 'wp_ajax_tutor_get_wc_product', array( $this, 'tutor_get_wc_product' ) );
260 189
261 190 add_action( 'wp_ajax_tutor_course_enrollment', array( $this, 'course_enrollment' ) );
262 191
263 192 /**
@@ -267,9 +196,8 @@
267 196 */
268 197 add_action( 'trashed_post', __CLASS__ . '::redirect_to_course_list_page' );
269 198
270 199 add_filter( 'tutor_enroll_required_login_class', array( $this, 'add_enroll_required_login_class' ) );
271 -
272 200 /**
273 201 * Remove wp trash button if instructor settings is disabled
274 202 *
275 203 * @since 2.7.3
@@ -274,129 +202,11 @@
274 202 *
275 203 * @since 2.7.3
276 204 */
277 205 add_action( 'tutor_option_save_after', array( $this, 'disable_course_trash_instructor' ) );
278 -
279 - /**
280 - * New course builder
281 - *
282 - * @since 3.0.0
283 - */
284 - add_action( 'template_redirect', array( $this, 'load_course_builder' ) );
285 - add_action( 'tutor_before_course_builder_load', array( $this, 'enqueue_course_builder_assets' ) );
286 - add_filter( 'tutor_localize_data', array( $this, 'localize_course_builder_data' ) );
287 -
288 - /**
289 - * Ajax list
290 - *
291 - * @since 3.0.0
292 - */
293 - add_action( 'wp_ajax_tutor_create_new_draft_course', array( $this, 'ajax_create_new_draft_course' ) );
294 - add_action( 'wp_ajax_tutor_course_list', array( $this, 'ajax_course_list' ) );
295 - add_action( 'wp_ajax_tutor_create_course', array( $this, 'ajax_create_course' ) );
296 - add_action( 'wp_ajax_tutor_course_details', array( $this, 'ajax_course_details' ) );
297 - add_action( 'wp_ajax_tutor_course_contents', array( $this, 'ajax_course_contents' ) );
298 - add_action( 'wp_ajax_tutor_update_course', array( $this, 'ajax_update_course' ) );
299 - add_action( 'wp_ajax_tutor_unlink_page_builder', array( $this, 'ajax_unlink_page_builder' ) );
300 -
301 - add_filter( 'tutor_user_list_access', array( $this, 'user_list_access_for_instructor' ) );
302 - add_filter( 'tutor_user_list_args', array( $this, 'user_list_args_for_instructor' ) );
303 -
304 - add_filter( 'template_include', array( $this, 'handle_password_protected' ) );
305 - add_action( 'login_form_postpass', array( $this, 'handle_password_submit' ) );
306 - add_filter( 'post_password_required', array( $this, 'bypass_password_for_enrolled' ), 10, 2 );
307 - add_filter( 'the_preview', array( $this, 'handle_schedule_courses' ) );
308 -
309 - add_action( 'tutor_course_action_btn', array( $this, 'render_course_action_btn' ) );
310 - add_action( 'wp_ajax_tutor_complete_course', array( $this, 'ajax_tutor_complete_course' ) );
311 206 }
312 207
313 208 /**
314 - * Handle schedule courses preview for instructors.
315 - *
316 - * @since 3.5.0
317 - *
318 - * @param \WP_Post $content the preview post content.
319 - *
320 - * @return \WP_Post
321 - */
322 - public function handle_schedule_courses( $content ) {
323 - global $wp_query;
324 - $course_coming_soon_enabled = (int) get_post_meta( $content->ID, '_tutor_course_enable_coming_soon', true );
325 - $is_instructor = tutor_utils()->is_instructor_of_this_course( get_current_user_id(), $content->ID, true );
326 - if ( ! CourseModel::get_post_types( $content ) || current_user_can( 'administrator' ) || $is_instructor || $course_coming_soon_enabled ) {
327 - return $content;
328 - }
329 -
330 - $wp_query->set_404();
331 - status_header( 404 );
332 - nocache_headers();
333 - return $content;
334 - }
335 -
336 - /**
337 - * Handle password protected course and bundle form submission.
338 - *
339 - * @since 3.2.1
340 - *
341 - * @return void
342 - */
343 - public function handle_password_submit() {
344 - if ( Input::has( 'post_password' ) && Input::has( 'course_id' ) ) {
345 - $course_id = Input::post( 'course_id', 0, Input::TYPE_NUMERIC );
346 - $password_required = post_password_required( $course_id );
347 - if ( $password_required ) {
348 - set_transient( 'tutor_post_password_error', __( 'Invalid password', 'tutor' ) );
349 - }
350 - }
351 - }
352 -
353 - /**
354 - * Bypass password protection for enrolled users.
355 - *
356 - * @since 4.0.0
357 - *
358 - * @param bool $required Whether the password is required.
359 - * @param WP_Post $post The post object.
360 - *
361 - * @return bool false if the current user is enrolled, original value otherwise.
362 - */
363 - public function bypass_password_for_enrolled( $required, $post ) {
364 - if ( ! $required ) {
365 - return $required;
366 - }
367 -
368 - $post_types = array( tutor()->course_post_type, tutor()->bundle_post_type );
369 - if ( in_array( $post->post_type, $post_types, true ) && EnrollmentModel::is_enrolled( $post->ID ) ) {
370 - return false;
371 - }
372 -
373 - return $required;
374 - }
375 -
376 - /**
377 - * Handle password protected course/bundle.
378 - *
379 - * @since 3.0.0
380 - *
381 - * @param string $template template path.
382 - *
383 - * @return string template path.
384 - */
385 - public function handle_password_protected( $template ) {
386 - if ( is_single() ) {
387 - $current_post_type = get_post_type( get_the_ID() );
388 - $post_types = array( tutor()->course_post_type, tutor()->bundle_post_type );
389 - if ( in_array( $current_post_type, $post_types, true ) && post_password_required() ) {
390 - remove_all_filters( 'template_include' );
391 - return tutor()->path . '/templates/single/password-protected.php';
392 - }
393 - }
394 -
395 - return $template;
396 - }
397 -
398 - /**
399 209 * Remove move to trash button on WordPress editor for instructor.
400 210 *
401 211 * @since 2.7.3
402 212 *
@@ -414,1231 +224,8 @@
414 224 }
415 225 }
416 226
417 227 /**
418 - * Check if the video source type is valid
419 - *
420 - * @since 3.0.0
421 - *
422 - * @param string $source_type source type.
423 - *
424 - * @return boolean
425 - */
426 - private function is_valid_video_source_type( string $source_type ): bool {
427 - $supported_types = tutor_utils()->get_option( 'supported_video_sources', array() );
428 - if ( is_string( $supported_types ) ) {
429 - $supported_types = array( $supported_types );
430 - }
431 -
432 - return in_array( $source_type, $supported_types, true );
433 - }
434 -
435 - /**
436 - * Get course selling options.
437 - *
438 - * @since 3.0.0
439 - *
440 - * @return array
441 - */
442 - public static function get_selling_options() {
443 - return array(
444 - self::SELLING_OPTION_ONE_TIME,
445 - self::SELLING_OPTION_SUBSCRIPTION,
446 - self::SELLING_OPTION_BOTH,
447 - self::SELLING_OPTION_MEMBERSHIP,
448 - self::SELLING_OPTION_ALL,
449 - );
450 - }
451 -
452 - /**
453 - * Get course selling option
454 - *
455 - * @since 3.0.0
456 - *
457 - * @param int $course_id course id.
458 - *
459 - * @return string
460 - */
461 - public static function get_selling_option( $course_id ) {
462 - return get_post_meta( $course_id, self::COURSE_SELLING_OPTION_META, true );
463 - }
464 -
465 - /**
466 - * Validate video source
467 - *
468 - * @since 3.0.0
469 - *
470 - * @param array $params array of params.
471 - * @param array $errors array of errors.
472 - *
473 - * @return void
474 - */
475 - public function validate_video_source( $params, &$errors ) {
476 - if ( isset( $params['video'] ) ) {
477 - $video_source_type = isset( $params['video']['source'] ) ? $params['video']['source'] : '';
478 - if ( tutor_is_rest() ) {
479 - $video_source_type = isset( $params['video']['source_type'] ) ? $params['video']['source_type'] : '';
480 - }
481 -
482 - if ( '' === $video_source_type ) {
483 - $errors['video_source'] = __( 'Video source is required', 'tutor' );
484 - } elseif ( ! $this->is_valid_video_source_type( $video_source_type ) ) {
485 - $errors['video_source'] = __( 'Invalid video source', 'tutor' );
486 - }
487 - }
488 - }
489 -
490 - /**
491 - * Validate scheduled courses
492 - *
493 - * @since 3.3.0
494 - *
495 - * @param array $params array of params.
496 - * @param array $errors array of errors.
497 - *
498 - * @return void
499 - */
500 - public function validate_scheduled_course( $params, &$errors ) {
501 - if ( isset( $params['post_status'] ) && isset( $params['course_settings'] ) ) {
502 - if ( 'future' !== $params['post_status'] ) {
503 - return;
504 - }
505 -
506 - $course_settings = $params['course_settings'];
507 -
508 - if ( $course_settings['course_enrollment_period'] && 'no' === $course_settings['course_enrollment_period'] ) {
509 - return;
510 - }
511 -
512 - if ( isset( $course_settings['enrollment_starts_at'] ) && ! empty( $course_settings['enrollment_starts_at'] ) ) {
513 - $enrollment_start = strtotime( $course_settings['enrollment_starts_at'] );
514 - $scheduled_date = strtotime( $params['post_date_gmt'] );
515 -
516 - if ( $enrollment_start < $scheduled_date ) {
517 - $errors['scheduled_course'] = __( 'The enrollment start date cannot be earlier than the course start date', 'tutor' );
518 - }
519 - }
520 - }
521 - }
522 -
523 - /**
524 - * Prepare course categories & tags
525 - *
526 - * @since 3.0.0
527 - *
528 - * @param array $params post params.
529 - * @param array $errors array of errors.
530 - *
531 - * @return void
532 - */
533 - public function prepare_course_cats_tags( &$params, &$errors ) {
534 - if ( isset( $params['course_categories'] ) ) {
535 - if ( ! is_array( $params['course_categories'] ) || empty( $params['course_categories'] ) ) {
536 - $errors['course_categories'] = __( 'Invalid course categories', 'tutor' );
537 - } else {
538 - $params['course_categories'] = $params['course_categories'];
539 - }
540 - }
541 -
542 - if ( isset( $params['course_tags'] ) ) {
543 - if ( ! is_array( $params['course_tags'] ) || empty( $params['course_tags'] ) ) {
544 - $errors['course_tags'] = __( 'Invalid course tags', 'tutor' );
545 - } else {
546 - $params['course_tags'] = $params['course_tags'];
547 - }
548 - }
549 - }
550 -
551 - /**
552 - * Setup course categories and tags.
553 - *
554 - * @since 3.0.0
555 - *
556 - * @param int $post_id Post ID.
557 - * @param array $params Array of params.
558 - *
559 - * @return void
560 - */
561 - public function setup_course_categories_tags( $post_id, $params ) {
562 - if ( isset( $params['course_categories'] ) && is_array( $params['course_categories'] ) ) {
563 - $valid_category_ids = ValidationHelper::validate_term_ids(
564 - $params['course_categories'],
565 - CourseModel::COURSE_CATEGORY
566 - );
567 - wp_set_object_terms( $post_id, $valid_category_ids, CourseModel::COURSE_CATEGORY );
568 - } else {
569 - wp_set_object_terms( $post_id, array(), CourseModel::COURSE_CATEGORY );
570 - }
571 -
572 - if ( isset( $params['course_tags'] ) && is_array( $params['course_tags'] ) ) {
573 - $valid_tag_ids = ValidationHelper::validate_term_ids(
574 - $params['course_tags'],
575 - CourseModel::COURSE_TAG
576 - );
577 - wp_set_object_terms( $post_id, $valid_tag_ids, CourseModel::COURSE_TAG );
578 - } else {
579 - wp_set_object_terms( $post_id, array(), CourseModel::COURSE_TAG );
580 - }
581 - }
582 -
583 - /**
584 - * Validate price for course create
585 - *
586 - * @since 3.0.0
587 - *
588 - * @param array $params array of params.
589 - * @param array $errors array of errors.
590 - *
591 - * @return void
592 - */
593 - public function validate_price( $params, &$errors ) {
594 - if ( isset( $params['pricing'] ) ) {
595 - $type = $params['pricing']['type'] ?? '';
596 -
597 - if ( '' === $type || ! in_array( $type, array( self::PRICE_TYPE_FREE, self::PRICE_TYPE_PAID ), true ) ) {
598 - $errors['pricing'] = __( 'Invalid price type', 'tutor' );
599 - }
600 -
601 - if ( self::PRICE_TYPE_PAID === $type ) {
602 - $monetize_by = tutor_utils()->get_option( 'monetize_by' );
603 - if ( 'wc' === $monetize_by ) {
604 - $product_id = (int) isset( $params['pricing']['product_id'] ) ? $params['pricing']['product_id'] : 0;
605 - // $product_id = 0 then new WC product will be created.
606 - if ( $product_id ) {
607 - $product = wc_get_product( $product_id );
608 - if ( is_a( $product, 'WC_Product' ) ) {
609 - $is_linked_with_course = tutor_utils()->product_belongs_with_course( $product_id );
610 - if ( $is_linked_with_course ) {
611 - $errors['pricing'] = __( 'Product already linked with course', 'tutor' );
612 - }
613 - }
614 - }
615 - }
616 - }
617 - }
618 - }
619 -
620 - /**
621 - * Validate price
622 - *
623 - * @since 3.0.0
624 - *
625 - * @param array $params array of params.
626 - * @param array $errors array of errors.
627 - * @param int $course_id course id.
628 - *
629 - * @return void
630 - */
631 - public function validate_price_for_update( $params, &$errors, $course_id ) {
632 - if ( isset( $params['pricing'] ) ) {
633 - $type = $params['pricing']['type'] ?? '';
634 -
635 - if ( '' === $type || ! in_array( $type, array( self::PRICE_TYPE_FREE, self::PRICE_TYPE_PAID, self::PRICE_TYPE_SUBSCRIPTION ), true ) ) {
636 - $errors['pricing'] = __( 'Invalid price type', 'tutor' );
637 - }
638 -
639 - if ( self::PRICE_TYPE_PAID === $type ) {
640 - $monetize_by = tutor_utils()->get_option( 'monetize_by' );
641 - if ( 'wc' === $monetize_by ) {
642 - $course_product_id = tutor_utils()->get_course_product_id( $course_id );
643 - $product_id = isset( $params['pricing']['product_id'] ) ? (int) $params['pricing']['product_id'] : 0;
644 -
645 - if ( $product_id ) {
646 - $product = wc_get_product( $product_id );
647 - if ( is_a( $product, 'WC_Product' ) ) {
648 - if ( $course_product_id !== $product_id ) {
649 - $is_linked_with_course = tutor_utils()->product_belongs_with_course( $product_id );
650 - if ( $is_linked_with_course ) {
651 - $errors['pricing'] = __( 'Product already linked with course', 'tutor' );
652 - }
653 - }
654 - } else {
655 - $errors['pricing'] = __( 'Invalid product', 'tutor' );
656 - }
657 - } else {
658 - /**
659 - * If user does not select WC product
660 - * Then automatic WC product will be create name with course title.
661 - */
662 - if ( ! isset( $params['course_price'] ) || ! floatval( $params['course_price'] ) ) {
663 - $errors['pricing'] = __( 'Price is required', 'tutor' );
664 - }
665 - }
666 - }
667 - }
668 - }
669 - }
670 -
671 - /**
672 - * Prepare course meta data for update
673 - *
674 - * @param array $params params.
675 - *
676 - * @return void
677 - */
678 - public function prepare_create_post_meta( $params ) {
679 - $additional_content = isset( $params['additional_content'] ) ? $params['additional_content'] : array();
680 -
681 - $course_benefits = isset( $additional_content['course_benefits'] ) ? $additional_content['course_benefits'] : '';
682 -
683 - $course_target_audience = isset( $additional_content['course_target_audience'] ) ? $additional_content['course_target_audience'] : '';
684 -
685 - $course_duration = isset( $additional_content['course_duration'] ) ? array(
686 - 'hours' => $additional_content['course_duration']['hours'] ?? '',
687 - 'minutes' => $additional_content['course_duration']['minutes'] ?? '',
688 - ) : array();
689 -
690 - $course_materials = isset( $additional_content['course_material_includes'] ) ? $additional_content['course_material_includes'] : '';
691 -
692 - $course_requirements = isset( $additional_content['course_requirements'] ) ? $additional_content['course_requirements'] : '';
693 -
694 - $pricing = isset( $params['pricing'] ) ? array(
695 - 'type' => $params['pricing']['type'] ?? self::PRICE_TYPE_FREE,
696 - 'product_id' => (int) $params['pricing']['product_id'] ?? -1,
697 - ) : array(
698 - 'type' => self::PRICE_TYPE_FREE,
699 - 'product_id' => -1,
700 - );
701 -
702 - // Setup global $_POST array.
703 - $_POST['_tutor_course_additional_data_edit'] = true;
704 -
705 - $_POST['tutor_course_price_type'] = $pricing['type'];
706 - $_POST['course_duration'] = $course_duration;
707 - $_POST['tutor_course_price_type'] = $pricing['type'];
708 - $_POST['_tutor_course_product_id'] = $pricing['product_id'];
709 - $_POST['_tutor_course_level'] = $params['course_level'];
710 - $_POST['course_benefits'] = $course_benefits;
711 - $_POST['course_requirements'] = $course_requirements;
712 - $_POST['course_target_audience'] = $course_target_audience;
713 - $_POST['course_material_includes'] = $course_materials;
714 -
715 - if ( isset( $params['enable_qna'] ) && 'yes' === $params['enable_qna'] ) {
716 - $_POST['_tutor_enable_qa'] = 'yes';
717 - }
718 -
719 - if ( isset( $params['_tutor_is_public_course'] ) && 'yes' === $params['_tutor_is_public_course'] ) {
720 - $_POST['_tutor_is_public_course'] = 'yes';
721 - }
722 -
723 - // Set course price.
724 - if ( -1 !== $pricing['product_id'] ) {
725 - $product = wc_get_product( $pricing['product_id'] );
726 - if ( is_a( $product, 'WC_Product' ) ) {
727 - $regular_price = $product->get_regular_price();
728 - $sale_price = $product->get_sale_price();
729 -
730 - $_POST['course_price'] = $regular_price;
731 - $_POST['course_sale_price'] = $sale_price;
732 - }
733 - }
734 - }
735 -
736 - /**
737 - * Prepare course meta data for update
738 - *
739 - * @since 3.0.0
740 - *
741 - * @param array $params params.
742 - *
743 - * @throws \Exception Throw new exception.
744 - *
745 - * @return mixed
746 - */
747 - public function prepare_update_post_meta( $params ) {
748 - $post_id = (int) $params['ID'];
749 -
750 - $additional_content = isset( $params['additional_content'] ) ? $params['additional_content'] : array();
751 -
752 - if ( ! empty( $additional_content ) ) {
753 -
754 - $course_benefits = isset( $additional_content['course_benefits'] ) ? $additional_content['course_benefits'] : '';
755 -
756 - $course_target_audience = isset( $additional_content['course_target_audience'] ) ? $additional_content['course_target_audience'] : '';
757 -
758 - $course_duration = isset( $additional_content['course_duration'] ) ? array(
759 - 'hours' => $additional_content['course_duration']['hours'] ?? '',
760 - 'minutes' => $additional_content['course_duration']['minutes'] ?? '',
761 - ) : array();
762 -
763 - $course_materials = isset( $additional_content['course_material_includes'] ) ? $additional_content['course_material_includes'] : '';
764 -
765 - $course_requirements = isset( $additional_content['course_requirements'] ) ? $additional_content['course_requirements'] : '';
766 -
767 - if ( '' !== $course_benefits ) {
768 - update_post_meta( $post_id, '_tutor_course_benefits', $course_benefits );
769 - }
770 -
771 - if ( '' !== $course_requirements ) {
772 - update_post_meta( $post_id, '_tutor_course_requirements', $course_requirements );
773 - }
774 -
775 - if ( '' !== $course_target_audience ) {
776 - update_post_meta( $post_id, '_tutor_course_target_audience', $course_target_audience );
777 - }
778 -
779 - if ( '' !== $course_materials ) {
780 - update_post_meta( $post_id, '_tutor_course_material_includes', $course_materials );
781 - }
782 -
783 - if ( ! empty( $course_duration ) ) {
784 - update_post_meta( $post_id, '_course_duration', $course_duration );
785 - }
786 - }
787 -
788 - if ( isset( $params['pricing'] ) && ! empty( $params['pricing'] ) ) {
789 - try {
790 - if ( isset( $params['pricing']['type'] ) ) {
791 - update_post_meta( $post_id, self::COURSE_PRICE_TYPE_META, $params['pricing']['type'] );
792 - }
793 - } catch ( \Throwable $th ) {
794 - throw new \Exception( $th->getMessage() );
795 - }
796 - }
797 -
798 - update_post_meta( $post_id, '_tutor_enable_qa', $params['enable_qna'] ?? 'yes' );
799 - update_post_meta( $post_id, '_tutor_is_public_course', $params['is_public_course'] ?? 'no' );
800 - update_post_meta( $post_id, '_tutor_course_level', $params['course_level'] );
801 -
802 - /**
803 - * Save tax collection settings
804 - *
805 - * @since 3.7.0
806 - */
807 - if ( isset( $params['tax_on_single'] ) ) {
808 - update_post_meta( $post_id, self::TAX_ON_SINGLE_META, $params['tax_on_single'] );
809 - }
810 -
811 - if ( isset( $params['tax_on_subscription'] ) ) {
812 - update_post_meta( $post_id, self::TAX_ON_SUBSCRIPTION_META, $params['tax_on_subscription'] );
813 - }
814 -
815 - do_action( 'tutor_after_prepare_update_post_meta', $post_id, $params );
816 - }
817 -
818 - /**
819 - * Prepare course settings meta
820 - *
821 - * @since 3.0.0
822 - *
823 - * @param array $params params.
824 - *
825 - * @return void
826 - */
827 - public function prepare_course_settings( $params ) {
828 - if ( isset( $params['course_settings'] ) ) {
829 - $_POST['_tutor_course_settings'] = $params['course_settings'];
830 - }
831 - }
832 -
833 - /**
834 - * Check access before course builder ajax request.
835 - *
836 - * @since 3.0.0
837 - *
838 - * @param int $course_id course id.
839 - *
840 - * @return void
841 - */
842 - public function check_access( $course_id = null ) {
843 - $has_access = false;
844 -
845 - if ( $course_id ) {
846 - $has_access = tutor_utils()->can_user_edit_course( get_current_user_id(), $course_id );
847 - } else {
848 - $has_access = User::is_admin() || User::is_instructor();
849 - }
850 -
851 - if ( ! $has_access ) {
852 - $this->json_response(
853 - tutor_utils()->error_message( HttpHelper::STATUS_UNAUTHORIZED ),
854 - null,
855 - HttpHelper::STATUS_UNAUTHORIZED
856 - );
857 - }
858 - }
859 -
860 - /**
861 - * Validate request inputs.
862 - *
863 - * @param array $params input params.
864 - * @param array $exclude exclude key from rules.
865 - *
866 - * @return object
867 - */
868 - public function validate_inputs( $params, $exclude = array() ) {
869 - $status_str = implode( ',', CourseModel::get_status_list() );
870 - $rules = array(
871 - 'course_id' => 'required|numeric',
872 - 'post_title' => 'required',
873 - 'post_author' => 'user_exists',
874 - 'post_status' => "required|match_string:{$status_str}",
875 - 'enable_qna' => 'if_input|match_string:yes,no',
876 - 'is_public_course' => 'if_input|match_string:yes,no',
877 - );
878 -
879 - foreach ( $exclude as $key ) {
880 - if ( isset( $rules[ $key ] ) ) {
881 - unset( $rules[ $key ] );
882 - }
883 - }
884 -
885 - return ValidationHelper::validate( $rules, $params );
886 - }
887 -
888 - /**
889 - * Create new draft course
890 - *
891 - * @since 3.0.0
892 - *
893 - * @return void JSON response
894 - */
895 - public function ajax_create_new_draft_course() {
896 - tutor_utils()->check_nonce();
897 -
898 - $this->check_access();
899 -
900 - $course_id = wp_insert_post(
901 - array(
902 - 'post_title' => __( 'New Course', 'tutor' ),
903 - 'post_type' => tutor()->course_post_type,
904 - 'post_status' => 'draft',
905 - 'post_name' => 'new-course',
906 - )
907 - );
908 -
909 - if ( is_wp_error( $course_id ) ) {
910 - $this->json_response( $course_id->get_error_message(), null, HttpHelper::STATUS_INTERNAL_SERVER_ERROR );
911 - }
912 -
913 - update_post_meta( $course_id, self::COURSE_PRICE_TYPE_META, self::PRICE_TYPE_FREE );
914 -
915 - $link = admin_url( 'admin.php?page=create-course' );
916 - if ( tutor()->has_pro && Input::post( 'from_dashboard', false, Input::TYPE_BOOL ) ) {
917 - $link = tutor_utils()->tutor_dashboard_url( 'create-course' );
918 - }
919 -
920 - $link = add_query_arg( array( 'course_id' => $course_id ), $link );
921 -
922 - do_action( 'tutor_draft_course_created', $course_id );
923 -
924 - $this->json_response(
925 - __( 'Draft course created', 'tutor' ),
926 - $link,
927 - HttpHelper::STATUS_CREATED
928 - );
929 - }
930 -
931 - /**
932 - * Get course list
933 - *
934 - * @since 3.0.0
935 - *
936 - * @since 3.2.0 Refactor the arguments & response as per new design.
937 - *
938 - * @return void
939 - */
940 - public function ajax_course_list() {
941 - tutor_utils()->check_nonce();
942 - $this->check_access();
943 -
944 - $limit = Input::post( 'limit', 10, Input::TYPE_INT );
945 - $offset = Input::post( 'offset', 0, Input::TYPE_INT );
946 - $search_term = '';
947 - $post_status = Input::post( 'post_status', null );
948 -
949 - $filter = json_decode( wp_unslash( $_POST['filter'] ) ); //phpcs:ignore --sanitized already
950 - if ( ! empty( $filter ) && property_exists( $filter, 'search' ) ) {
951 - $search_term = Input::sanitize( $filter->search );
952 - }
953 -
954 - $args = array(
955 - 'post_status' => is_null( $post_status ) ? 'publish' : $post_status,
956 - 'posts_per_page' => $limit,
957 - 'offset' => $offset,
958 - 's' => $search_term,
959 - );
960 -
961 - $exclude = Input::post( 'exclude', array(), Input::TYPE_ARRAY );
962 - if ( count( $exclude ) ) {
963 - $exclude = array_filter( $exclude, fn( $id ) => is_numeric( $id ) && $id > 0 );
964 - $args['post__not_in'] = $exclude;
965 - }
966 -
967 - $courses = CourseModel::get_courses_by_args( $args );
968 -
969 - $response = array(
970 - 'results' => array(),
971 - 'total_items' => 0,
972 - );
973 -
974 - $response['total_items'] = is_a( $courses, 'WP_Query' ) ? $courses->found_posts : 0;
975 -
976 - if ( is_a( $courses, 'WP_Query' ) && $courses->have_posts() ) {
977 - $courses = $courses->get_posts();
978 - foreach ( $courses as $course ) {
979 - $response['results'][] = self::get_mini_info( $course );
980 - }
981 - }
982 -
983 - $this->json_response(
984 - __( 'Course list retrieved successfully!', 'tutor' ),
985 - $response
986 - );
987 - }
988 -
989 - /**
990 - * Create course by ajax request.
991 - *
992 - * @since 3.0.0
993 - *
994 - * @return void
995 - */
996 - public function ajax_create_course() {
997 - tutor_utils()->check_nonce();
998 -
999 - $this->check_access();
1000 -
1001 - $params = Input::sanitize_array(
1002 - //phpcs:ignore WordPress.Security.NonceVerification.Missing
1003 - $_POST,
1004 - array(
1005 - 'post_content' => 'wp_kses_post',
1006 - 'course_material_includes' => 'sanitize_textarea_field',
1007 - )
1008 - );
1009 -
1010 - $params['post_type'] = tutor()->course_post_type;
1011 -
1012 - // Validate inputs.
1013 - $errors = array();
1014 - $validation = $this->validate_inputs( $params, array( 'course_id' ) );
1015 - if ( ! $validation->success ) {
1016 - $errors = $validation->errors;
1017 - }
1018 -
1019 - if ( User::is_instructor() ) {
1020 - $params['post_author'] = get_current_user_id();
1021 - }
1022 -
1023 - // Validate video source if user set video.
1024 - $this->validate_video_source( $params, $errors );
1025 -
1026 - // Validate WC product.
1027 - $this->validate_price( $params, $errors );
1028 -
1029 - // Set course categories and tags.
1030 - $this->prepare_course_cats_tags( $params, $errors );
1031 - $this->setup_course_price( $params );
1032 -
1033 - if ( ! empty( $errors ) ) {
1034 - $this->json_response( __( 'Invalid input', 'tutor' ), $errors, HttpHelper::STATUS_UNPROCESSABLE_ENTITY );
1035 - }
1036 -
1037 - $this->prepare_course_settings( $params );
1038 -
1039 - try {
1040 - $this->prepare_create_post_meta( $params );
1041 - } catch ( \Exception $e ) {
1042 - $this->json_response( $e->getMessage(), null, HttpHelper::STATUS_INTERNAL_SERVER_ERROR );
1043 - }
1044 -
1045 - $course_id = wp_insert_post( $params );
1046 - if ( is_wp_error( $course_id ) ) {
1047 - $this->json_response( $course_id->get_error_message(), null, HttpHelper::STATUS_INTERNAL_SERVER_ERROR );
1048 - }
1049 -
1050 - // Set course cats & tags.
1051 - $this->setup_course_categories_tags( $course_id, $params );
1052 -
1053 - // Update course thumb.
1054 - if ( isset( $params['thumbnail_id'] ) ) {
1055 - set_post_thumbnail( $course_id, $params['thumbnail_id'] );
1056 - }
1057 -
1058 - $this->json_response(
1059 - __( 'Course created successfully', 'tutor' ),
1060 - $course_id,
1061 - HttpHelper::STATUS_CREATED
1062 - );
1063 - }
1064 -
1065 - /**
1066 - * Setup course price
1067 - *
1068 - * @since 3.0.0
1069 - *
1070 - * @param array $params params.
1071 - *
1072 - * @return void
1073 - */
1074 - public function setup_course_price( $params ) {
1075 - if ( isset( $params['pricing'] )
1076 - && isset( $params['pricing']['product_id'] )
1077 - && is_numeric( $params['pricing']['product_id'] ) ) {
1078 - $_POST['_tutor_course_product_id'] = $params['pricing']['product_id'];
1079 - }
1080 - }
1081 -
1082 - /**
1083 - * Update course by ajax request.
1084 - *
1085 - * @since 3.0.0
1086 - *
1087 - * @return void
1088 - */
1089 - public function ajax_update_course() {
1090 - tutor_utils()->check_nonce();
1091 -
1092 - $params = Input::sanitize_array(
1093 - //phpcs:ignore WordPress.Security.NonceVerification.Missing
1094 - wp_slash( $_POST ),
1095 - array(
1096 - 'post_content' => 'wp_kses_post',
1097 - 'course_benefits' => 'sanitize_textarea_field',
1098 - 'course_target_audience' => 'sanitize_textarea_field',
1099 - 'course_material_includes' => 'sanitize_textarea_field',
1100 - 'course_requirements' => 'sanitize_textarea_field',
1101 - )
1102 - );
1103 -
1104 - $course_id = (int) $params['course_id'];
1105 - if ( ! $course_id ) {
1106 - $this->response_bad_request( __( 'Invalid course id', 'tutor' ) );
1107 - }
1108 -
1109 - $this->check_access( $course_id );
1110 -
1111 - $errors = array();
1112 - $validation = $this->validate_inputs( $params );
1113 - if ( ! $validation->success ) {
1114 - $errors = $validation->errors;
1115 - }
1116 -
1117 - // Validate video source if user set video.
1118 - $this->validate_video_source( $params, $errors );
1119 -
1120 - // Validate WC product.
1121 - $this->validate_price_for_update( $params, $errors, $course_id );
1122 -
1123 - // Set course categories and tags.
1124 - $this->prepare_course_cats_tags( $params, $errors );
1125 -
1126 - $this->prepare_course_settings( $params );
1127 -
1128 - // Validate scheduled courses.
1129 - $this->validate_scheduled_course( $params, $errors );
1130 -
1131 - $this->setup_course_price( $params );
1132 -
1133 - if ( ! empty( $errors ) ) {
1134 - $this->json_response( __( 'Invalid input', 'tutor' ), $errors, HttpHelper::STATUS_UNPROCESSABLE_ENTITY );
1135 - }
1136 -
1137 - /**
1138 - * Can trash a course when user is admin or option `instructor_can_delete_course` is turned on.
1139 - */
1140 - if ( CourseModel::STATUS_TRASH === $params['post_status'] ) {
1141 - if ( User::is_admin() || tutor_utils()->get_option( 'instructor_can_delete_course', false ) ) {
1142 - $params['post_status'] = CourseModel::STATUS_TRASH;
1143 - } else {
1144 - unset( $params['post_status'] );
1145 - }
1146 - }
1147 -
1148 - /**
1149 - * Can publish a course when user is admin or option `instructor_can_publish_course` is turned on.
1150 - * If instructor_can_publish_course is turned off then course status will be pending.
1151 - */
1152 - if ( CourseModel::STATUS_PUBLISH === $params['post_status'] ) {
1153 - $is_instructor_allowed_to_publish = (bool) tutor_utils()->get_option( 'instructor_can_publish_course', false );
1154 - if ( ! User::is_admin() && ! $is_instructor_allowed_to_publish ) {
1155 - $params['post_status'] = CourseModel::STATUS_PENDING;
1156 - }
1157 - }
1158 -
1159 - $is_error = apply_filters( 'tutor_is_error_before_course_update', false, $params );
1160 - if ( is_wp_error( $is_error ) ) {
1161 - $this->response_bad_request( $is_error->get_error_message() );
1162 - }
1163 -
1164 - $params['ID'] = $course_id;
1165 - $update_id = wp_update_post( $params, true );
1166 - if ( is_wp_error( $update_id ) ) {
1167 - $this->json_response( $update_id->get_error_message(), null, HttpHelper::STATUS_INTERNAL_SERVER_ERROR );
1168 - }
1169 -
1170 - $this->setup_course_categories_tags( $update_id, $params );
1171 - $this->prepare_update_post_meta( $params );
1172 -
1173 - // Update course thumb.
1174 - $thumbnail_id = Input::post( 'thumbnail_id', 0, Input::TYPE_INT );
1175 - if ( $thumbnail_id ) {
1176 - set_post_thumbnail( $update_id, $thumbnail_id );
1177 - } else {
1178 - delete_post_meta( $update_id, '_thumbnail_id' );
1179 - }
1180 -
1181 - $this->json_response(
1182 - __( 'Course updated successfully.', 'tutor' ),
1183 - $update_id,
1184 - HttpHelper::STATUS_OK
1185 - );
1186 - }
1187 -
1188 - /**
1189 - * Unlink page builder from editor.
1190 - *
1191 - * @since 3.0.0
1192 - *
1193 - * @return void
1194 - */
1195 - public function ajax_unlink_page_builder() {
1196 - tutor_utils()->check_nonce();
1197 -
1198 - $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
1199 - if ( ! $course_id ) {
1200 - $this->response_bad_request( __( 'Invalid course id', 'tutor' ) );
1201 - }
1202 -
1203 - $this->check_access( $course_id );
1204 -
1205 - $builder = Input::post( 'builder' );
1206 - if ( 'elementor' === $builder ) {
1207 - delete_post_meta( $course_id, '_elementor_edit_mode' );
1208 - } elseif ( 'droip' === $builder ) {
1209 - delete_post_meta( $course_id, 'droip_editor_mode' );
1210 - } elseif ( 'divi' === $builder ) {
1211 - $old_post_content = get_post_meta( $course_id, '_et_pb_old_content', true );
1212 - $course = get_post( $course_id );
1213 - $course->post_content = $old_post_content;
1214 - $result = wp_update_post( $course );
1215 -
1216 - if ( $result && ! is_wp_error( $result ) ) {
1217 - update_post_meta( $course_id, '_et_pb_use_builder', 'off' );
1218 - update_post_meta( $course_id, '_et_pb_old_content', '' );
1219 - delete_post_meta( $course_id, '_et_dynamic_cached_shortcodes' );
1220 - delete_post_meta( $course_id, '_et_dynamic_cached_attributes' );
1221 - delete_post_meta( $course_id, '_et_builder_module_features_cache' );
1222 - }
1223 - }
1224 -
1225 - $this->json_response(
1226 - __( 'Builder unlinked successfully.', 'tutor' ),
1227 - $course_id,
1228 - HttpHelper::STATUS_OK
1229 - );
1230 - }
1231 -
1232 - /**
1233 - * Get all course contents by course id.
1234 - *
1235 - * @since 3.0.0
1236 - *
1237 - * @param int $course_id course id.
1238 - *
1239 - * @return array
1240 - */
1241 - public function get_course_contents( $course_id ) {
1242 - $data = array();
1243 - $topics = tutor_utils()->get_topics( $course_id );
1244 -
1245 - if ( $topics->have_posts() ) {
1246 - foreach ( $topics->get_posts() as $post ) {
1247 - $current_topic = array(
1248 - 'id' => $post->ID,
1249 - 'title' => $post->post_title,
1250 - 'summary' => $post->post_content,
1251 - 'contents' => array(),
1252 - );
1253 -
1254 - $topic_contents = tutor_utils()->get_course_contents_by_topic( $post->ID, -1 );
1255 -
1256 - if ( $topic_contents->have_posts() ) {
1257 - foreach ( $topic_contents->get_posts() as $post ) {
1258 - if ( tutor()->quiz_post_type === $post->post_type ) {
1259 - $questions = tutor_utils()->get_questions_by_quiz( $post->ID );
1260 - $post->total_question = is_array( $questions ) ? count( $questions ) : 0;
1261 - }
1262 -
1263 - array_push( $current_topic['contents'], $post );
1264 - }
1265 - }
1266 -
1267 - $current_topic = apply_filters( 'tutor_filter_course_content', $current_topic );
1268 -
1269 - array_push( $data, $current_topic );
1270 - }
1271 - }
1272 -
1273 - return $data;
1274 - }
1275 -
1276 - /**
1277 - * Get course contents
1278 - *
1279 - * @since 3.0.0
1280 - *
1281 - * @return void
1282 - */
1283 - public function ajax_course_contents() {
1284 - tutor_utils()->check_nonce();
1285 -
1286 - $errors = array();
1287 - $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
1288 -
1289 - if ( ! $course_id || tutor()->course_post_type !== get_post_type( $course_id ) ) {
1290 - $errors['course_id'] = __( 'Invalid course id', 'tutor' );
1291 - }
1292 -
1293 - $this->check_access( $course_id );
1294 -
1295 - if ( ! empty( $errors ) ) {
1296 - $this->json_response( __( 'Invalid input', 'tutor' ), $errors, HttpHelper::STATUS_UNPROCESSABLE_ENTITY );
1297 - }
1298 -
1299 - $contents = $this->get_course_contents( $course_id );
1300 -
1301 - $this->json_response(
1302 - __( 'Course contents fetched successfully', 'tutor' ),
1303 - $contents
1304 - );
1305 - }
1306 -
1307 - /**
1308 - * Get course details by ID
1309 - *
1310 - * @since 3.0.0
1311 - *
1312 - * @return void
1313 - */
1314 - public function ajax_course_details() {
1315 - tutor_utils()->check_nonce();
1316 -
1317 - $errors = array();
1318 - $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
1319 -
1320 - if ( ! $course_id || tutor()->course_post_type !== get_post_type( $course_id ) ) {
1321 - $errors['course_id'] = __( 'Invalid course id', 'tutor' );
1322 - }
1323 -
1324 - $this->check_access( $course_id );
1325 -
1326 - if ( ! empty( $errors ) ) {
1327 - $this->json_response( __( 'Invalid input', 'tutor' ), $errors, HttpHelper::STATUS_UNPROCESSABLE_ENTITY );
1328 - }
1329 -
1330 - $price_type = tutor_utils()->price_type( $course_id );
1331 - $monetize_by = tutor_utils()->get_option( 'monetize_by' );
1332 -
1333 - $product_name = '';
1334 - $price = 0;
1335 - $sale_price = 0;
1336 - $product_id = tutor_utils()->get_course_product_id( $course_id );
1337 -
1338 - if ( WooCommerce::MONETIZE_BY === $monetize_by ) {
1339 - $product = wc_get_product( $product_id );
1340 - if ( $product ) {
1341 - $product_name = $product->get_name();
1342 - $price = $product->get_regular_price();
1343 - $sale_price = $product->get_sale_price();
1344 - }
1345 - }
1346 -
1347 - if ( Ecommerce::MONETIZE_BY === $monetize_by ) {
1348 - $price = get_post_meta( $course_id, self::COURSE_PRICE_META, true );
1349 - $sale_price = get_post_meta( $course_id, self::COURSE_SALE_PRICE_META, true );
1350 - }
1351 -
1352 - $course_pricing = array(
1353 - 'type' => $price_type,
1354 - 'product_id' => $product_id,
1355 - 'product_name' => $product_name,
1356 - 'price' => $price,
1357 - 'sale_price' => $sale_price,
1358 - );
1359 -
1360 - $video_intro = get_post_meta( $course_id, '_video', true );
1361 - if ( $video_intro ) {
1362 - $source = $video_intro['source'] ?? '';
1363 - if ( 'html5' === $source ) {
1364 - $poster_url = wp_get_attachment_url( $video['poster'] ?? 0 );
1365 - $source_html5 = wp_get_attachment_url( $video['source_video_id'] ?? 0 );
1366 - $video['poster_url'] = $poster_url;
1367 - $video['source_html5'] = $source_html5;
1368 - }
1369 - }
1370 -
1371 - $course = get_post( $course_id, ARRAY_A );
1372 - if ( $course ) {
1373 - $course['post_name'] = urldecode( $course['post_name'] );
1374 - }
1375 -
1376 - $editors = tutor_utils()->get_editor_list( $course_id );
1377 -
1378 - $data = array(
1379 - 'editors' => array_values( $editors ),
1380 - 'editor_used' => tutor_utils()->get_editor_used( $course_id ),
1381 - 'preview_link' => get_preview_post_link( $course_id ),
1382 - 'post_author' => tutor_utils()->get_tutor_user( $course['post_author'] ),
1383 - 'course_categories' => wp_get_post_terms( $course_id, CourseModel::COURSE_CATEGORY ),
1384 - 'course_tags' => wp_get_post_terms( $course_id, CourseModel::COURSE_TAG ),
1385 - 'thumbnail_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
1386 - 'thumbnail' => get_the_post_thumbnail_url( $course_id ),
1387 -
1388 - 'enable_qna' => get_post_meta( $course_id, '_tutor_enable_qa', true ),
1389 - 'is_public_course' => get_post_meta( $course_id, '_tutor_is_public_course', true ),
1390 - 'course_level' => get_post_meta( $course_id, '_tutor_course_level', true ),
1391 - 'video' => $video_intro,
1392 - 'course_duration' => get_post_meta( $course_id, '_course_duration', true ),
1393 - 'course_benefits' => get_post_meta( $course_id, '_tutor_course_benefits', true ),
1394 - 'course_requirements' => get_post_meta( $course_id, '_tutor_course_requirements', true ),
1395 - 'course_target_audience' => get_post_meta( $course_id, '_tutor_course_target_audience', true ),
1396 - 'course_material_includes' => get_post_meta( $course_id, '_tutor_course_material_includes', true ),
1397 - 'monetize_by' => $monetize_by,
1398 - 'course_pricing' => $course_pricing,
1399 - 'course_settings' => get_post_meta( $course_id, '_tutor_course_settings', true ),
1400 - 'step_completion_status' => array(
1401 - 'basic' => true,
1402 - 'curriculum' => false,
1403 - 'additional' => false,
1404 - 'certificate' => false,
1405 - ),
1406 - );
1407 -
1408 - $tax_on_single = get_post_meta( $course_id, self::TAX_ON_SINGLE_META, true );
1409 - $tax_on_subscription = get_post_meta( $course_id, self::TAX_ON_SUBSCRIPTION_META, true );
1410 -
1411 - $data['tax_collection'] = array(
1412 - 'tax_on_single' => '' === $tax_on_single ? '1' : $tax_on_single,
1413 - 'tax_on_subscription' => '' === $tax_on_subscription ? '1' : $tax_on_subscription,
1414 - );
1415 -
1416 - $data = apply_filters( 'tutor_course_details_response', array_merge( $course, $data ) );
1417 -
1418 - $this->json_response( __( 'Data retrieved successfully!', 'tutor' ), $data );
1419 - }
1420 -
1421 - /**
1422 - * Load course builder.
1423 - *
1424 - * @since 3.0.0
1425 - *
1426 - * @return void
1427 - */
1428 - public function load_course_builder() {
1429 - global $pagenow;
1430 -
1431 - $has_pro = tutor()->has_pro;
1432 - $has_access_role = User::has_any_role( array( User::ADMIN, User::INSTRUCTOR ) );
1433 -
1434 - $course_id = Input::get( 'course_id', 0, Input::TYPE_INT );
1435 - $backend_builder = is_admin() && 'admin.php' === $pagenow && 'create-course' === Input::get( 'page' );
1436 - $backend_edit = $backend_builder && $course_id;
1437 -
1438 - $is_frontend_builder = tutor_utils()->is_tutor_frontend_dashboard( 'create-course' );
1439 - $frontend_edit = $is_frontend_builder && $course_id;
1440 -
1441 - if ( $has_access_role && ( $backend_edit || ( $has_pro && $frontend_edit ) ) ) {
1442 - $post_type = get_post_type( $course_id );
1443 - $can_edit_course = tutor_utils()->can_user_edit_course( get_current_user_id(), $course_id );
1444 -
1445 - if ( tutor()->course_post_type === $post_type && ( User::is_admin() || $can_edit_course ) ) {
1446 - /**
1447 - * Edit trash course behavior
1448 - *
1449 - * @since 3.0.0
1450 - */
1451 - if ( CourseModel::STATUS_TRASH === get_post_status( $course_id ) ) {
1452 - $message = User::is_admin()
1453 - ? __( 'You cannot edit this course because it is in the Trash. Please restore it and try again', 'tutor' )
1454 - : tutor_utils()->error_message();
1455 - wp_die( esc_html( $message ) );
1456 - }
1457 -
1458 - $this->load_course_builder_view();
1459 - }
1460 - }
1461 - }
1462 -
1463 - /**
1464 - * Enqueue course builder assets like CSS, JS
1465 - *
1466 - * @since 3.0.0
1467 - *
1468 - * @return void
1469 - */
1470 - public function enqueue_course_builder_assets() {
1471 - // Fix: function print_emoji_styles is deprecated since version 6.4.0!
1472 - remove_action( 'wp_print_styles', 'print_emoji_styles' );
1473 - remove_action( 'wp_head', 'wp_admin_bar_header' );
1474 - add_action( 'wp_head', 'wp_enqueue_admin_bar_header_styles' );
1475 -
1476 - do_action( 'tutor_course_builder_before_wp_editor_load' );
1477 - wp_enqueue_script( 'wp-tinymce' );
1478 - wp_enqueue_script( 'mce-view' );
1479 - wp_enqueue_editor();
1480 -
1481 - wp_enqueue_media();
1482 - wp_enqueue_script( 'tutor-course-builder', tutor()->url . 'assets/js/tutor-course-builder.js', array( 'wp-date', 'wp-i18n', 'wp-element', 'wp-api' ), TUTOR_VERSION, true );
1483 - wp_set_script_translations( 'tutor-course-builder', 'tutor', tutor()->path . 'languages/' );
1484 -
1485 - wp_localize_script(
1486 - 'mce-view',
1487 - 'mceViewL10n',
1488 - array(
1489 - 'shortcodes' => ! empty( $GLOBALS['shortcode_tags'] ) ? array_keys( $GLOBALS['shortcode_tags'] ) : array(),
1490 - )
1491 - );
1492 - }
1493 -
1494 - /**
1495 - * Localize custom course builder data for _tutorobject.
1496 - *
1497 - * @since 3.3.1
1498 - *
1499 - * @param array $data the localized data.
1500 - *
1501 - * @return array
1502 - */
1503 - public function localize_course_builder_data( $data ) {
1504 - global $pagenow;
1505 -
1506 - $course_id = Input::get( 'course_id', 0, Input::TYPE_INT );
1507 - $backend_builder = is_admin() && 'admin.php' === $pagenow && 'create-course' === Input::get( 'page' );
1508 - $backend_edit = $backend_builder && $course_id;
1509 -
1510 - $is_frontend_builder = tutor_utils()->is_tutor_frontend_dashboard( 'create-course' );
1511 - $frontend_edit = $is_frontend_builder && $course_id;
1512 -
1513 - if ( ! $backend_edit && ! $frontend_edit ) {
1514 - return $data;
1515 - }
1516 -
1517 - /**
1518 - * Prepare course builder data.
1519 - */
1520 - $default_data = ( new Assets( false ) )->get_default_localized_data();
1521 -
1522 - if ( isset( $default_data['current_user']['data']['id'] ) ) {
1523 - $tutor_user = tutor_utils()->get_tutor_user( $default_data['current_user']['data']['id'] );
1524 - $default_data['current_user']['data']['tutor_profile_photo_url'] = $tutor_user->tutor_profile_photo_url;
1525 - }
1526 -
1527 - /**
1528 - * Localized only options to protect sensitive info like API keys.
1529 - */
1530 - $required_options = array(
1531 - 'learning_mode',
1532 - 'monetize_by',
1533 - 'enable_course_marketplace',
1534 - 'course_permalink_base',
1535 - 'supported_video_sources',
1536 - 'enrollment_expiry_enabled',
1537 - 'enable_q_and_a_on_course',
1538 - 'instructor_can_delete_course',
1539 - 'chatgpt_enable',
1540 - 'hide_admin_bar_for_users',
1541 - 'enable_redirect_on_course_publish_from_frontend',
1542 - 'instructor_can_publish_course',
1543 - 'instructor_can_change_course_author',
1544 - 'instructor_can_manage_co_instructors',
1545 - );
1546 -
1547 - $full_settings = get_option( 'tutor_option', array() );
1548 - $settings = Options_V2::get_only( $required_options );
1549 - $settings['course_builder_logo_url'] = wp_get_attachment_image_url( $full_settings['tutor_frontend_course_page_logo_id'] ?? 0, 'full' );
1550 - $settings['chatgpt_key_exist'] = tutor()->has_pro && ! empty( $full_settings['chatgpt_api_key'] ?? '' );
1551 - $settings['youtube_api_key_exist'] = ! empty( $full_settings['lesson_video_duration_youtube_api_key'] ?? '' );
1552 -
1553 - $settings['enable_tax'] = Tax::get_setting( 'enable_tax', true );
1554 - $settings['is_tax_included_in_price'] = Tax::is_tax_included_in_price();
1555 - $settings['enable_individual_tax_control'] = Tax::get_setting( 'enable_individual_tax_control' );
1556 -
1557 - $new_data = array( 'settings' => $settings );
1558 -
1559 - $data = array_merge( $default_data, $new_data );
1560 -
1561 - /**
1562 - * Course builder dashboard URL based on role and settings.
1563 - */
1564 - $dashboard_url = tutor_utils()->tutor_dashboard_url();
1565 - if ( User::is_admin() ) {
1566 - $dashboard_url = get_admin_url();
1567 - }
1568 -
1569 - /**
1570 - * EDD product list
1571 - */
1572 - $monetize_by = tutor_utils()->get_option( 'monetize_by' );
1573 - if ( 'edd' === $monetize_by && tutor_utils()->has_edd() ) {
1574 - $data['edd_products'] = tutor_utils()->get_edd_products();
1575 - }
1576 -
1577 - $difficulty_levels = array();
1578 - foreach ( tutor_utils()->course_levels() as $value => $label ) {
1579 - $difficulty_levels[] = array(
1580 - 'label' => $label,
1581 - 'value' => $value,
1582 - );
1583 - }
1584 -
1585 - $supported_video_sources = array();
1586 - $saved_video_source_list = (array) ( $settings['supported_video_sources'] ?? array() );
1587 -
1588 - foreach ( tutor_utils()->get_video_sources( true ) as $value => $label ) {
1589 - if ( in_array( $value, $saved_video_source_list, true ) ) {
1590 - $supported_video_sources[] = array(
1591 - 'label' => $label,
1592 - 'value' => $value,
1593 - );
1594 - }
1595 - }
1596 -
1597 - $data['dashboard_url'] = $dashboard_url;
1598 - $data['backend_course_list_url'] = get_admin_url( null, 'admin.php?page=tutor' );
1599 - $data['frontend_course_list_url'] = tutor_utils()->tutor_dashboard_url( 'my-courses' );
1600 - $data['timezones'] = tutor_global_timezone_lists();
1601 - $data['difficulty_levels'] = $difficulty_levels;
1602 - $data['supported_video_sources'] = $supported_video_sources;
1603 - $data['wp_rest_nonce'] = wp_create_nonce( 'wp_rest' );
1604 -
1605 - if ( 'en_US' !== $data['local'] ) {
1606 - $data['course_builder_basic_locales'] = tutils()->get_script_locale_data( 'tutor-course-builder-basic', $data['local'] );
1607 - $data['course_builder_curriculum_locales'] = tutils()->get_script_locale_data( 'tutor-course-builder-curriculum', $data['local'] );
1608 - $data['course_builder_additional_locales'] = tutils()->get_script_locale_data( 'tutor-course-builder-additional', $data['local'] );
1609 - }
1610 -
1611 - $data = apply_filters( 'tutor_course_builder_localized_data', $data );
1612 -
1613 - return $data;
1614 - }
1615 -
1616 - /**
1617 - * Load view for course builder.
1618 - *
1619 - * @since 3.0.0
1620 - *
1621 - * @return void
1622 - */
1623 - public function load_course_builder_view() {
1624 - /**
1625 - * Hide admin menu and footer.
1626 - *
1627 - * @since 3.3.0
1628 - */
1629 - echo '<style>
1630 - #adminmenumain, #wpfooter, .notice, #tutor-page-wrap { display: none !important; }
1631 - #wpcontent { margin: 0 !important; padding: 0 !important; }
1632 - #wpbody-content { padding-bottom: 0px !important; float: none; }
1633 - </style>';
1634 -
1635 - do_action( 'tutor_before_course_builder_load' );
1636 - include_once tutor()->path . 'views/pages/course-builder.php';
1637 - do_action( 'tutor_after_course_builder_load' );
1638 - }
1639 -
1640 - /**
1641 228 * Add enroll require login class
1642 229 *
1643 230 * @since 2.6.0
1644 231 *
@@ -1655,48 +242,14 @@
1655 242 return $class_name;
1656 243 }
1657 244
1658 245 /**
1659 - * Get list of WC products.
1660 - *
1661 - * @since 2.5.0
1662 - * @since 3.0.0 exclude_linked_products, course_id are added.
1663 - *
1664 - * @return void
1665 - */
1666 - public function get_wc_products() {
1667 - $exclude = array();
1668 - $exclude_linked_products = Input::has( 'exclude_linked_products' );
1669 - $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
1670 -
1671 - if ( $exclude_linked_products ) {
1672 - $exclude = tutor_utils()->get_linked_product_ids();
1673 - }
1674 -
1675 - if ( $course_id ) {
1676 - $linked_product_id = tutor_utils()->get_course_product_id( $course_id );
1677 - if ( $linked_product_id ) {
1678 - $exclude = array_filter( $exclude, fn( $id )=> $linked_product_id !== (int) $id );
1679 - }
1680 - }
1681 -
1682 - $exclude = array_unique( $exclude );
1683 -
1684 - $this->json_response(
1685 - __( 'Products retrieved successfully!', 'tutor' ),
1686 - tutor_utils()->get_wc_products_db( $exclude ),
1687 - HttpHelper::STATUS_OK
1688 - );
1689 - }
1690 -
1691 - /**
1692 246 * Get course associate WC product info by Ajax request
1693 247 *
1694 248 * @since 2.0.7
1695 - *
1696 249 * @return void
1697 250 */
1698 - public function get_wc_product() {
251 + public function tutor_get_wc_product() {
1699 252 tutor_utils()->checking_nonce();
1700 253 $product_id = Input::post( 'product_id' );
1701 254 $product = wc_get_product( $product_id );
1702 255 $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
@@ -1701,15 +254,14 @@
1701 254 $product = wc_get_product( $product_id );
1702 255 $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
1703 256
1704 257 $is_linked_with_course = tutor_utils()->product_belongs_with_course( $product_id );
1705 -
1706 258 /**
1707 259 * If selected product is already linked with
1708 260 * a course & it is not the current course the
1709 261 * return error
1710 262 *
1711 - * @since 2.1.0
263 + * @since v2.1.0
1712 264 */
1713 265 if ( is_object( $is_linked_with_course ) && $is_linked_with_course->post_id != $course_id ) {
1714 266 wp_send_json_error(
1715 267 __( 'One product can not be added to multiple course!', 'tutor' )
@@ -1731,38 +283,23 @@
1731 283 /**
1732 284 * Update course content order
1733 285 *
1734 286 * @since 1.0.0
1735 - * @since 3.9.9 Check if user can manage course before updating order.
1736 - *
1737 287 * @return void
1738 288 */
1739 - public function ajax_update_course_content_order() {
289 + public function tutor_update_course_content_order() {
1740 290 tutor_utils()->checking_nonce();
1741 291
1742 - $sorting_order = Input::post( 'tutor_topics_lessons_sorting', '' );
1743 - $sorting_order = json_decode( $sorting_order, true ) ?? array();
1744 -
1745 - if ( ! tutor_utils()->count( $sorting_order ) ) {
1746 - wp_send_json_error( __( 'Sorting order is required', 'tutor' ) );
1747 - }
1748 -
1749 - $topic_id = (int) isset( $sorting_order[0], $sorting_order[0]['topic_id'] ) ? $sorting_order[0]['topic_id'] : 0;
1750 - $course_id = wp_get_post_parent_id( $topic_id );
1751 -
1752 - if ( ! $topic_id || ! $course_id ) {
1753 - wp_send_json_error( tutor_utils()->error_message( 'invalid_req' ) );
1754 - }
1755 -
1756 - if ( ! tutor_utils()->can_user_manage( 'course', $course_id ) || ! User::is_admin() ) {
1757 - wp_send_json_error( tutor_utils()->error_message() );
1758 - }
1759 -
1760 292 if ( Input::has( 'content_parent' ) ) {
1761 293 $content_parent = Input::post( 'content_parent', array(), Input::TYPE_ARRAY );
1762 294 $topic_id = tutor_utils()->array_get( 'parent_topic_id', $content_parent );
1763 295 $content_id = tutor_utils()->array_get( 'content_id', $content_parent );
1764 296
297 + if ( ! tutor_utils()->can_user_manage( 'topic', $topic_id ) ) {
298 + wp_send_json_success( array( 'message' => __( 'Access Denied!', 'tutor' ) ) );
299 + exit;
300 + }
301 +
1765 302 // Update the parent topic id of the content.
1766 303 global $wpdb;
1767 304 $wpdb->update( $wpdb->posts, array( 'post_parent' => $topic_id ), array( 'ID' => $content_id ) );
1768 305 }
@@ -1767,9 +304,9 @@
1767 304 $wpdb->update( $wpdb->posts, array( 'post_parent' => $topic_id ), array( 'ID' => $content_id ) );
1768 305 }
1769 306
1770 307 // Save course content order.
1771 - $this->save_course_content_order( $sorting_order );
308 + $this->save_course_content_order();
1772 309
1773 310 wp_send_json_success();
1774 311 }
1775 312
@@ -1777,9 +314,8 @@
1777 314 * Restrict new student entry
1778 315 *
1779 316 * @since 1.0.0
1780 317 * @param mixed $content content.
1781 - *
1782 318 * @return mixed
1783 319 */
1784 320 public function restrict_new_student_entry( $content ) {
1785 321
@@ -1796,11 +332,9 @@
1796 332 /**
1797 333 * Restrict media
1798 334 *
1799 335 * @since 1.0.0
1800 - *
1801 336 * @param string $where where clause.
1802 - *
1803 337 * @return string
1804 338 */
1805 339 public function restrict_media( $where ) {
1806 340 $action = Input::post( 'action' );
@@ -1813,61 +347,169 @@
1813 347 return $where;
1814 348 }
1815 349
1816 350 /**
1817 - * Save course content order
351 + * Registering metabox
1818 352 *
1819 353 * @since 1.0.0
1820 - * @since 3.9.8 param $order added.
354 + * @return void
355 + */
356 + public function register_meta_box() {
357 + $course_post_type = tutor()->course_post_type;
358 +
359 + tutor_meta_box_wrapper( 'tutor-course-topics', __( 'Course Builder', 'tutor' ), array( $this, 'course_meta_box' ), $course_post_type, 'advanced', 'default', 'tutor-admin-post-meta' );
360 +
361 + tutor_meta_box_wrapper( 'tutor-course-additional-data', __( 'Additional Data', 'tutor' ), array( $this, 'course_additional_data_meta_box' ), $course_post_type, 'advanced', 'default', 'tutor-admin-post-meta' );
362 +
363 + tutor_meta_box_wrapper( 'tutor-course-videos', __( 'Video', 'tutor' ), array( $this, 'video_metabox' ), $course_post_type, 'advanced', 'default', 'tutor-admin-post-meta' );
364 + }
365 +
366 + /**
367 + * Course meta box (Topics)
1821 368 *
1822 - * @param array $sort_order the lesson and topic order array.
369 + * @since 1.0.0
370 + * @param boolean $echo display or not.
371 + * @return string
372 + */
373 + public function course_meta_box( $echo = true ) {
374 + $file_path = tutor()->path . 'views/metabox/course-topics.php';
375 +
376 + if ( $echo ) {
377 + /**
378 + * Use echo raise WPCS security issue
379 + * Helper wp_kses_post break content.
380 + */
381 + include $file_path;
382 + } else {
383 + ob_start();
384 + include $file_path;
385 + return ob_get_clean();
386 + }
387 + }
388 +
389 + /**
390 + * Additional data meta box
1823 391 *
392 + * @since 1.0.0
393 + * @param boolean $echo print data or return.
394 + * @return string
395 + */
396 + public function course_additional_data_meta_box( $echo = true ) {
397 + $file_path = tutor()->path . 'views/metabox/course-additional-data.php';
398 +
399 + if ( $echo ) {
400 + /**
401 + * Use echo raise WPCS security issue
402 + * Helper wp_kses_post break content.
403 + */
404 + include $file_path;
405 + } else {
406 + ob_start();
407 + include $file_path;
408 + return ob_get_clean();
409 + }
410 + }
411 +
412 + /**
413 + * Video meta box
414 + *
415 + * @since 1.0.0
416 + * @param boolean $echo print data or return.
417 + * @return string
418 + */
419 + public function video_metabox( $echo = true ) {
420 + $file_path = tutor()->path . 'views/metabox/video-metabox.php';
421 +
422 + if ( $echo ) {
423 + /**
424 + * Use echo raise WPCS security issue
425 + * Helper wp_kses_post break content.
426 + */
427 + include $file_path;
428 + } else {
429 + ob_start();
430 + include $file_path;
431 + return ob_get_clean();
432 + }
433 + }
434 +
435 + /**
436 + * Register metabox in course builder tutor
437 + *
438 + * @since 1.3.4
1824 439 * @return void
1825 440 */
1826 - private function save_course_content_order( $sort_order = array() ) {
441 + public function register_meta_box_in_frontend() {
442 + global $post;
443 +
444 + do_action( 'tutor_course_builder_metabox_before', get_the_ID() );
445 +
446 + course_builder_section_wrap( $this->video_metabox( false ), __( 'Video', 'tutor' ) );
447 + do_action( 'tutor/frontend_course_edit/after/video', $post );
448 +
449 + course_builder_section_wrap( $this->course_meta_box( false ), __( 'Course Builder', 'tutor' ) );
450 + do_action( 'tutor/frontend_course_edit/after/course_builder', $post );
451 +
452 + course_builder_section_wrap( $this->course_additional_data_meta_box( false ), __( 'Additional Data', 'tutor' ) );
453 + do_action( 'tutor/frontend_course_edit/after/additional_data', $post );
454 +
455 + do_action( 'tutor_course_builder_metabox_after', get_the_ID() );
456 + }
457 +
458 + /**
459 + * Save course content order
460 + *
461 + * @since 1.0.0
462 + * @return void
463 + */
464 + private function save_course_content_order() {
1827 465 global $wpdb;
1828 466
1829 - if ( ! tutor_utils()->count( $sort_order ) ) {
1830 - return;
1831 - }
467 + $new_order = Input::post( 'tutor_topics_lessons_sorting' );
468 + if ( ! empty( $new_order ) ) {
469 + $order = json_decode( $new_order, true );
1832 470
1833 - $i = 0;
1834 - foreach ( $sort_order as $topic ) {
1835 - ++$i;
1836 - $wpdb->update(
1837 - $wpdb->posts,
1838 - array( 'menu_order' => $i ),
1839 - array( 'ID' => $topic['topic_id'] )
1840 - );
471 + if ( is_array( $order ) && count( $order ) ) {
472 + $i = 0;
473 + foreach ( $order as $topic ) {
474 + $i++;
475 + $wpdb->update(
476 + $wpdb->posts,
477 + array( 'menu_order' => $i ),
478 + array( 'ID' => $topic['topic_id'] )
479 + );
1841 480
1842 - /**
1843 - * Removing All lesson with topic
1844 - */
1845 - $wpdb->update(
1846 - $wpdb->posts,
1847 - array( 'post_parent' => 0 ),
1848 - array( 'post_parent' => $topic['topic_id'] )
1849 - );
481 + /**
482 + * Removing All lesson with topic
483 + */
1850 484
1851 - /**
1852 - * Lesson Attaching with topic ID
1853 - * Sorting lesson
1854 - */
1855 - if ( isset( $topic['lesson_ids'] ) ) {
1856 - $lesson_ids = $topic['lesson_ids'];
1857 - } else {
1858 - $lesson_ids = array();
1859 - }
1860 - if ( count( $lesson_ids ) ) {
1861 - foreach ( $lesson_ids as $lesson_key => $lesson_id ) {
1862 485 $wpdb->update(
1863 486 $wpdb->posts,
1864 - array(
1865 - 'post_parent' => $topic['topic_id'],
1866 - 'menu_order' => $lesson_key,
1867 - ),
1868 - array( 'ID' => $lesson_id )
487 + array( 'post_parent' => 0 ),
488 + array( 'post_parent' => $topic['topic_id'] )
1869 489 );
490 +
491 + /**
492 + * Lesson Attaching with topic ID
493 + * Sorting lesson
494 + */
495 + if ( isset( $topic['lesson_ids'] ) ) {
496 + $lesson_ids = $topic['lesson_ids'];
497 + } else {
498 + $lesson_ids = array();
499 + }
500 + if ( count( $lesson_ids ) ) {
501 + foreach ( $lesson_ids as $lesson_key => $lesson_id ) {
502 + $wpdb->update(
503 + $wpdb->posts,
504 + array(
505 + 'post_parent' => $topic['topic_id'],
506 + 'menu_order' => $lesson_key,
507 + ),
508 + array( 'ID' => $lesson_id )
509 + );
510 + }
511 + }
1870 512 }
1871 513 }
1872 514 }
1873 515 }
@@ -1891,9 +533,9 @@
1891 533 * Save course price type
1892 534 */
1893 535 $price_type = Input::post( 'tutor_course_price_type' );
1894 536 if ( $price_type ) {
1895 - update_post_meta( $post_ID, self::COURSE_PRICE_TYPE_META, $price_type );
537 + update_post_meta( $post_ID, '_tutor_course_price_type', $price_type );
1896 538 }
1897 539
1898 540 //phpcs:disable WordPress.Security.NonceVerification.Missing
1899 541 // Course Duration.
@@ -1911,41 +553,47 @@
1911 553 if ( $additional_data_edit ) {
1912 554 if ( ! empty( $_POST['course_benefits'] ) ) {
1913 555 $course_benefits = Input::post( 'course_benefits', '', Input::TYPE_KSES_POST );
1914 556 update_post_meta( $post_ID, '_tutor_course_benefits', $course_benefits );
1915 - } elseif ( ! tutor_is_rest() ) {
557 + } else {
558 + if ( ! tutor_is_rest() ) {
1916 559 delete_post_meta( $post_ID, '_tutor_course_benefits' );
560 + }
1917 561 }
1918 562
1919 563 if ( ! empty( $_POST['course_requirements'] ) ) {
1920 564 $requirements = Input::post( 'course_requirements', '', Input::TYPE_KSES_POST );
1921 565 update_post_meta( $post_ID, '_tutor_course_requirements', $requirements );
1922 - } elseif ( ! tutor_is_rest() ) {
566 + } else {
567 + if ( ! tutor_is_rest() ) {
1923 568 delete_post_meta( $post_ID, '_tutor_course_requirements' );
569 + }
1924 570 }
1925 571
1926 572 if ( ! empty( $_POST['course_target_audience'] ) ) {
1927 573 $target_audience = Input::post( 'course_target_audience', '', Input::TYPE_KSES_POST );
1928 574 update_post_meta( $post_ID, '_tutor_course_target_audience', $target_audience );
1929 - } elseif ( ! tutor_is_rest() ) {
575 + } else {
576 + if ( ! tutor_is_rest() ) {
1930 577 delete_post_meta( $post_ID, '_tutor_course_target_audience' );
578 + }
1931 579 }
1932 580
1933 581 if ( ! empty( $_POST['course_material_includes'] ) ) {
1934 582 $material_includes = Input::post( 'course_material_includes', '', Input::TYPE_KSES_POST );
1935 583 update_post_meta( $post_ID, '_tutor_course_material_includes', $material_includes );
1936 - } elseif ( ! tutor_is_rest() ) {
584 + } else {
585 + if ( ! tutor_is_rest() ) {
1937 586 delete_post_meta( $post_ID, '_tutor_course_material_includes' );
587 + }
1938 588 }
1939 589 //phpcs:enable WordPress.Security.NonceVerification.Missing
1940 590 }
1941 591
1942 - $sorting_order = Input::post( 'tutor_topics_lessons_sorting', '' );
1943 - $sorting_order = json_decode( $sorting_order, true ) ?? array();
1944 592 /**
1945 593 * Sorting Topics and lesson
1946 594 */
1947 - $this->save_course_content_order( $sorting_order );
595 + $this->save_course_content_order();
1948 596
1949 597 // Additional data like course intro video.
1950 598 if ( $additional_data_edit ) {
1951 599 // Sanitize data through helper method.
@@ -1959,10 +607,12 @@
1959 607 );
1960 608 $video_source = tutor_utils()->array_get( 'source', $video );
1961 609 if ( -1 !== $video_source ) {
1962 610 update_post_meta( $post_ID, '_video', $video );
1963 - } elseif ( ! tutor_is_rest() ) {
611 + } else {
612 + if ( ! tutor_is_rest() ) {
1964 613 delete_post_meta( $post_ID, '_video' );
614 + }
1965 615 }
1966 616 }
1967 617
1968 618 /**
@@ -2004,33 +654,23 @@
2004 654 /**
2005 655 * Save course topic
2006 656 *
2007 657 * @since 1.0.0
2008 - * @since 3.0.0 response and input name updated.
2009 - *
2010 658 * @return void
2011 659 */
2012 660 public function tutor_save_topic() {
2013 - tutor_utils()->check_nonce();
661 + tutor_utils()->checking_nonce();
2014 662
2015 - $is_update = false;
2016 - $errors = array();
2017 - $topic_title = Input::post( 'title' );
2018 -
2019 - if ( empty( $topic_title ) ) {
2020 - $errors['topic_title'] = __( 'Topic title is required!', 'tutor' );
2021 - $this->json_response(
2022 - __( 'Invalid inputs', 'tutor' ),
2023 - $errors,
2024 - HttpHelper::STATUS_UNPROCESSABLE_ENTITY
2025 - );
663 + // Check required fields.
664 + if ( empty( Input::post( 'topic_title' ) ) ) {
665 + wp_send_json_error( array( 'message' => __( 'Topic title is required!', 'tutor' ) ) );
2026 666 }
2027 667
2028 668 // Gather parameters.
2029 - $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
2030 - $topic_id = Input::post( 'topic_id', 0, Input::TYPE_INT );
2031 - $topic_summary = Input::post( 'summary', '', Input::TYPE_KSES_POST );
2032 -
669 + $course_id = Input::post( 'topic_course_id', 0, Input::TYPE_INT );
670 + $topic_id = Input::post( 'topic_id', 0, Input::TYPE_INT );
671 + $topic_title = Input::post( 'topic_title' );
672 + $topic_summery = Input::post( 'topic_summery', '', Input::TYPE_KSES_POST );
2033 673 $next_topic_order_id = tutor_utils()->get_next_topic_order_id( $course_id, $topic_id );
2034 674
2035 675 // Validate if user can manage the topic.
2036 676 if ( ! tutor_utils()->can_user_manage( 'course', $course_id ) || ( $topic_id && ! tutor_utils()->can_user_manage( 'topic', $topic_id ) ) ) {
@@ -2037,61 +677,110 @@
2037 677 wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) );
2038 678 }
2039 679
2040 680 // Create payload to create/update the topic.
2041 - $post_arr = array(
681 + $post_arr = array(
2042 682 'post_type' => 'topics',
2043 683 'post_title' => $topic_title,
2044 - 'post_content' => $topic_summary,
684 + 'post_content' => $topic_summery,
2045 685 'post_status' => 'publish',
2046 686 'post_author' => get_current_user_id(),
2047 687 'post_parent' => $course_id,
2048 688 'menu_order' => $next_topic_order_id,
2049 689 );
690 + $topic_id ? $post_arr['ID'] = $topic_id : 0;
691 + $current_topic_id = wp_insert_post( $post_arr );
2050 692
2051 - if ( $topic_id ) {
2052 - $is_update = true;
2053 - $post_arr['ID'] = $topic_id;
693 + ob_start();
694 + include tutor()->path . 'views/metabox/course-contents.php';
695 +
696 + wp_send_json_success(
697 + array(
698 + 'topic_title' => $topic_title,
699 + 'course_contents' => ob_get_clean(),
700 + )
701 + );
702 + }
703 +
704 + /**
705 + * Add columns to course row in default WP list table
706 + *
707 + * @since 1.0.0
708 + *
709 + * @param array $columns column list.
710 + * @return mixed
711 + */
712 + public function add_column( $columns ) {
713 + $date_col = $columns['date'];
714 + unset( $columns['date'] );
715 + $columns['lessons'] = __( 'Lessons', 'tutor' );
716 + $columns['students'] = __( 'Students', 'tutor' );
717 + $columns['price'] = __( 'Price', 'tutor' );
718 + $columns['date'] = $date_col;
719 +
720 + return $columns;
721 + }
722 +
723 + /**
724 + * Add data to custom column
725 + *
726 + * @since 1.0.0
727 + *
728 + * @param string $column column name.
729 + * @param integer $post_id post ID.
730 + *
731 + * @return void
732 + */
733 + public function custom_lesson_column( $column, $post_id ) {
734 + if ( 'lessons' === $column ) {
735 + echo esc_html( tutor_utils()->get_lesson_count_by_course( $post_id ) );
2054 736 }
2055 737
2056 - $current_topic_id = wp_insert_post( $post_arr );
738 + if ( 'students' === $column ) {
739 + echo esc_html( tutor_utils()->count_enrolled_users_by_course( $post_id ) );
740 + }
2057 741
2058 - if ( $is_update ) {
2059 - $this->json_response(
2060 - __( 'Topic updated successfully!', 'tutor' ),
2061 - $current_topic_id
2062 - );
2063 - } else {
2064 - $this->json_response(
2065 - __( 'Topic created successfully!', 'tutor' ),
2066 - $current_topic_id,
2067 - HttpHelper::STATUS_CREATED
2068 - );
742 + if ( 'price' === $column ) {
743 + $price = tutor_utils()->get_course_price( $post_id );
744 + if ( $price ) {
745 + $monetize_by = tutils()->get_option( 'monetize_by' );
746 + if ( function_exists( 'wc_price' ) && 'wc' === $monetize_by ) {
747 + echo wp_kses(
748 + '<span class="tutor-label-success">' . wc_price( $price ) . '</span>',
749 + array(
750 + 'span' => array( 'class' => true ),
751 + )
752 + );
753 + } else {
754 + echo wp_kses(
755 + '<span class="tutor-label-success">' . $price . '</span>',
756 + array( 'span' => array( 'class' => true ) )
757 + );
758 + }
759 + } else {
760 + echo esc_html( apply_filters( 'tutor-loop-default-price', __( 'free', 'tutor' ) ) );
761 + }
2069 762 }
2070 763 }
2071 764
765 +
2072 766 /**
2073 767 * Delete a course topic
2074 768 *
2075 769 * @since 1.0.0
2076 - * @since 3.0.0 code refactor and response updated.
2077 - *
2078 770 * @return void
2079 771 */
2080 772 public function tutor_delete_topic() {
2081 - tutor_utils()->check_nonce();
2082 773
2083 - $topic_id = Input::post( 'topic_id', 0, Input::TYPE_INT );
774 + tutor_utils()->checking_nonce();
775 +
776 + global $wpdb;
777 + $topic_id = Input::post( 'topic_id', '' );
778 +
2084 779 if ( ! $topic_id || ! is_numeric( $topic_id ) || ! tutor_utils()->can_user_manage( 'topic', $topic_id ) ) {
2085 - $this->json_response(
2086 - tutor_utils()->error_message(),
2087 - null,
2088 - HttpHelper::STATUS_FORBIDDEN
2089 - );
780 + wp_send_json_error( array( 'message' => 'Access Forbidden' ) );
2090 781 }
2091 782
2092 - global $wpdb;
2093 -
2094 783 // Assign course ID to orphan content IDs since the topic will be deleted.
2095 784 $course_id = tutor_utils()->get_course_id_by( 'topic', $topic_id );
2096 785 $content_ids = tutor_utils()->get_course_content_ids_by( null, 'topic', $topic_id );
2097 786 foreach ( $content_ids as $content_id ) {
@@ -2096,9 +785,9 @@
2096 785 $content_ids = tutor_utils()->get_course_content_ids_by( null, 'topic', $topic_id );
2097 786 foreach ( $content_ids as $content_id ) {
2098 787 update_post_meta( $content_id, '_tutor_course_id_for_lesson', $course_id );
2099 788 // Actually all kind of contents.
2100 - // This keyword '_tutor_course_id_for_lesson' used just to support backward compatibility.
789 + // This keyword '_tutor_course_id_for_lesson' used just to support backward compatibillity.
2101 790 }
2102 791
2103 792 // Set contents under the topic orphan.
2104 793 $wpdb->update( $wpdb->posts, array( 'post_parent' => 0 ), array( 'post_parent' => $topic_id ) );
@@ -2106,11 +795,9 @@
2106 795 // Then delete the topic from database.
2107 796 $wpdb->delete( $wpdb->postmeta, array( 'post_id' => $topic_id ) );
2108 797 wp_delete_post( $topic_id );
2109 798
2110 - $this->json_response(
2111 - __( 'Topic deleted successfully!', 'tutor' )
2112 - );
799 + wp_send_json_success();
2113 800 }
2114 801
2115 802 /**
2116 803 * Handle enroll now action
@@ -2115,14 +802,15 @@
2115 802 /**
2116 803 * Handle enroll now action
2117 804 *
2118 805 * @since 1.0.0
2119 - *
2120 806 * @return void
2121 807 */
2122 808 public function enroll_now() {
2123 809
2124 - if ( '_tutor_course_enroll_now' !== Input::post( 'tutor_course_action' ) || ! Input::has( 'tutor_course_id' ) ) {
810 + // Checking if action comes from Enroll form.
811 + // phpcs:ignore WordPress.Security.NonceVerification.Missing
812 + if ( tutor_utils()->array_get( 'tutor_course_action', tutor_sanitize_data( $_POST ) ) !== '_tutor_course_enroll_now' || ! isset( $_POST['tutor_course_id'] ) ) {
2125 813 return;
2126 814 }
2127 815
2128 816 // Checking Nonce.
@@ -2133,8 +821,9 @@
2133 821 exit( esc_html__( 'Please Sign In first', 'tutor' ) );
2134 822 }
2135 823
2136 824 $course_id = Input::post( 'tutor_course_id', 0, Input::TYPE_INT );
825 + $user_id = get_current_user_id();
2137 826
2138 827 /**
2139 828 * TODO: need to check purchase information
2140 829 */
@@ -2140,14 +829,8 @@
2140 829 */
2141 830
2142 831 $is_purchasable = tutor_utils()->is_course_purchasable( $course_id );
2143 832
2144 - $course = get_post( $course_id );
2145 -
2146 - if ( 'private' === $course->post_status && ! current_user_can( 'read_private_tutor_courses' ) ) {
2147 - wp_send_json_error( __( 'You do not have permission to enroll in this course', 'tutor' ) );
2148 - }
2149 -
2150 833 /**
2151 834 * If is is not purchasable, it's free, and enroll right now
2152 835 * If purchasable, then process purchase.
2153 836 *
@@ -2157,13 +840,13 @@
2157 840 // Process purchase.
2158 841
2159 842 } else {
2160 843 // Free enroll.
2161 - EnrollmentModel::do_enroll( $course_id );
844 + tutor_utils()->do_enroll( $course_id );
2162 845 }
2163 846
2164 847 $referer_url = wp_get_referer();
2165 - wp_safe_redirect( tutor_utils()->get_nocache_url( $referer_url ) );
848 + wp_safe_redirect( $referer_url . '?nocache=' . time() );
2166 849 exit;
2167 850 }
2168 851
2169 852 /**
@@ -2169,11 +852,8 @@
2169 852 /**
2170 853 * Mark complete completed
2171 854 *
2172 855 * @since 1.0.0
2173 - *
2174 - * @since 3.7.1 Filter hook: tutor_user_can_complete_course added
2175 - *
2176 856 * @return void
2177 857 */
2178 858 public function mark_course_complete() {
2179 859 $tutor_action = Input::post( 'tutor_action' );
@@ -2181,10 +861,8 @@
2181 861 if ( 'tutor_complete_course' !== $tutor_action || ! $course_id ) {
2182 862 return;
2183 863 }
2184 864
2185 - $permalink = get_the_permalink( $course_id );
2186 -
2187 865 // Checking nonce.
2188 866 tutor_utils()->checking_nonce();
2189 867
2190 868 $user_id = get_current_user_id();
@@ -2193,78 +871,20 @@
2193 871 if ( ! $user_id ) {
2194 872 die( esc_html__( 'Please Sign-In', 'tutor' ) );
2195 873 }
2196 874
2197 - if ( ! EnrollmentModel::is_enrolled( $course_id, $user_id ) ) {
2198 - die( esc_html__( 'User is not enrolled in course', 'tutor' ) );
2199 - }
875 + CourseModel::mark_course_as_completed( $course_id, $user_id );
2200 876
2201 - /**
2202 - * Filter hook provided to restrict course completion. This is useful
2203 - * for specific cases like prerequisites. WP_Error should be returned
2204 - * from the filter value to prevent the completion.
2205 - */
2206 - $can_complete = apply_filters( 'tutor_user_can_complete_course', CourseModel::can_complete_course( $course_id, $user_id ), $user_id, $course_id );
877 + $permalink = get_the_permalink( $course_id );
2207 878
2208 - if ( is_wp_error( $can_complete ) ) {
2209 - tutor_utils()->redirect_to( $permalink, $can_complete->get_error_message(), 'error' );
2210 - } elseif ( ! $can_complete ) {
2211 - tutor_utils()->redirect_to( $permalink, __( 'You do not have permission to complete this course.', 'tutor' ), 'error' );
2212 - } else {
2213 - CourseModel::mark_course_as_completed( $course_id, $user_id );
2214 - // Set temporary identifier to show review pop up.
2215 - self::set_review_popup_data( $user_id, $course_id, $permalink );
879 + // Set temporary identifier to show review pop up.
880 + self::set_review_popup_data( $user_id, $course_id, $permalink );
2216 881
2217 - wp_safe_redirect( $permalink );
2218 - exit;
2219 - }
882 + wp_safe_redirect( $permalink );
883 + exit;
2220 884 }
2221 885
2222 886 /**
2223 - * Ajax course complete handler
2224 - *
2225 - * @since 4.0.0
2226 - *
2227 - * @return void
2228 - */
2229 - public function ajax_tutor_complete_course() {
2230 - $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
2231 -
2232 - // Checking nonce.
2233 - if ( ! tutor_utils()->is_nonce_verified() ) {
2234 - $this->response_bad_request( tutor_utils()->error_message( 'nonce' ) );
2235 - }
2236 -
2237 - if ( ! $course_id ) {
2238 - $this->response_bad_request( __( 'Invalid course', 'tutor' ) );
2239 - }
2240 -
2241 - $user_id = get_current_user_id();
2242 - if ( ! EnrollmentModel::is_enrolled( $course_id, $user_id ) ) {
2243 - $this->response_bad_request( __( 'You are not enrolled in this course', 'tutor' ) );
2244 - }
2245 -
2246 - /**
2247 - * Filter hook provided to restrict course completion. This is useful
2248 - * for specific cases like prerequisites. WP_Error should be returned
2249 - * from the filter value to prevent the completion.
2250 - */
2251 - $can_complete = apply_filters( 'tutor_user_can_complete_course', CourseModel::can_complete_course( $course_id, $user_id ), $user_id, $course_id );
2252 -
2253 - if ( is_wp_error( $can_complete ) ) {
2254 - $this->response_bad_request( $can_complete->get_error_message() );
2255 - } elseif ( ! $can_complete ) {
2256 - $this->response_bad_request( __( 'You do not have permission to complete this course.', 'tutor' ) );
2257 - } else {
2258 - CourseModel::mark_course_as_completed( $course_id, $user_id );
2259 - // Set temporary identifier to show review pop up.
2260 - self::set_review_popup_data( $user_id, $course_id );
2261 -
2262 - $this->response_success( __( 'Course completed successfully', 'tutor' ) );
2263 - }
2264 - }
2265 -
2266 - /**
2267 887 * Set data for review popup.
2268 888 *
2269 889 * @since 2.2.5
2270 890 * @since 2.4.0 removed $permalink param. store user meta instead of option data.
@@ -2290,34 +910,18 @@
2290 910 * @since 1.0.0
2291 911 * @return void
2292 912 */
2293 913 public function popup_review_form() {
2294 - if ( ! is_user_logged_in() ) {
2295 - return;
2296 - }
914 + if ( is_user_logged_in() ) {
915 + $user_id = get_current_user_id();
916 + $course_id = get_the_ID();
917 + $meta_key = User::get_review_popup_meta( $course_id );
918 + $review_course_id = (int) get_user_meta( $user_id, $meta_key, true );
2297 919
2298 - $is_learning_area = tutor_utils()->is_learning_area();
2299 - $is_legacy_learning = tutor_utils()->is_legacy_learning_mode();
2300 - if ( $is_legacy_learning && $is_learning_area ) {
2301 - return;
920 + if ( is_single() && $course_id === $review_course_id ) {
921 + include tutor()->path . 'views/modal/review.php';
922 + }
2302 923 }
2303 -
2304 - $course_id = get_the_ID();
2305 -
2306 - if ( $is_learning_area && ! Input::has( 'subpage' ) ) {
2307 - $course_id = wp_get_post_parent_id( wp_get_post_parent_id( get_the_ID() ) );
2308 - }
2309 -
2310 - if ( empty( $course_id ) ) {
2311 - return;
2312 - }
2313 -
2314 - $user_id = get_current_user_id();
2315 - $meta_key = User::get_review_popup_meta( $course_id );
2316 - $review_course_id = (int) get_user_meta( $user_id, $meta_key, true );
2317 - if ( is_single() && $course_id === $review_course_id ) {
2318 - include tutor()->path . 'views/modal/review.php';
2319 - }
2320 924 }
2321 925
2322 926 /**
2323 927 * Review popup data clear
@@ -2345,9 +949,8 @@
2345 949 /**
2346 950 * Delete course delete from frontend dashboard
2347 951 *
2348 952 * @since 2.0.0
2349 - *
2350 953 * @return void
2351 954 */
2352 955 public function tutor_delete_dashboard_course() {
2353 956 tutor_utils()->checking_nonce();
@@ -2366,9 +969,9 @@
2366 969 wp_send_json_error( array( 'message' => __( 'Only main instructor can delete this course', 'tutor' ) ) );
2367 970 }
2368 971
2369 972 // Check if user is only an instructor.
2370 - if ( ! User::is_admin() ) {
973 + if ( ! current_user_can( 'administrator' ) ) {
2371 974 // Check if instructor can trash course.
2372 975 $can_trash_post = tutor_utils()->get_option( 'instructor_can_delete_course' );
2373 976
2374 977 if ( ! $can_trash_post ) {
@@ -2378,9 +981,9 @@
2378 981
2379 982 $trash_course = wp_update_post(
2380 983 array(
2381 984 'ID' => $course_id,
2382 - 'post_status' => CourseModel::STATUS_TRASH,
985 + 'post_status' => 'trash',
2383 986 )
2384 987 );
2385 988
2386 989 if ( $trash_course ) {
@@ -2431,41 +1034,39 @@
2431 1034 * Attach product with course when course save from frontend or backend.
2432 1035 *
2433 1036 * @since 1.3.4
2434 1037 *
2435 - * @since 3.0.0 Store regular & sale price in meta to make compatible with Tutor monetization
2436 - *
2437 1038 * @param integer $post_ID course ID.
2438 - * @param array $post_data created course post details.
1039 + * @param array $post_data cretaed course post details.
2439 1040 *
2440 1041 * @return void
2441 1042 */
2442 1043 public function attach_product_with_course( $post_ID, $post_data ) {
1044 +
2443 1045 $monetize_by = tutor_utils()->get_option( 'monetize_by' );
2444 - $product_id = Input::post( '_tutor_course_product_id', 0, Input::TYPE_INT );
2445 1046
2446 1047 /**
2447 - * For native monetization, just return
2448 - * No need to attach anything.
1048 + * The function is_admin will check only loaded page from WP admin.
1049 + * It does not check any role
1050 + *
1051 + * @since 2.6.0
1052 + *
1053 + * tutor_is_rest() check added, if loaded from rest api
2449 1054 */
2450 - if ( Ecommerce::MONETIZE_BY === $monetize_by ) {
2451 - return;
2452 - }
1055 + $is_admin_panel = is_admin() || tutor_is_rest();
1056 + // From backend course select box.
1057 + $product_id = Input::post( '_tutor_course_product_id', 0, Input::TYPE_INT );
2453 1058
2454 1059 /**
2455 - * When course moved paid to free
2456 - * Keep the product linked and return.
1060 + * From Admin Panel, Free user can only select product from dropdown
2457 1061 */
2458 - if ( -1 === $product_id ) {
2459 - return;
2460 - }
2461 -
2462 - /**
2463 - * Free user can only select product from dropdown
2464 - */
2465 - if ( tutor()->has_pro === false && 'wc' === $monetize_by ) {
1062 + if ( $is_admin_panel && 'wc' === $monetize_by && tutor()->has_pro === false ) {
2466 1063 if ( $product_id > 0 ) {
2467 - update_post_meta( $post_ID, self::COURSE_PRODUCT_ID_META, $product_id );
1064 + update_post_meta( $post_ID, '_tutor_course_product_id', $product_id );
1065 + } elseif ( -1 === $product_id ) {
1066 + if ( ! tutor_is_rest() ) {
1067 + delete_post_meta( $post_ID, '_tutor_course_product_id' );
1068 + }
2468 1069 }
2469 1070
2470 1071 return;
2471 1072 }
@@ -2479,42 +1080,44 @@
2479 1080 }
2480 1081
2481 1082 $course = get_post( $post_ID );
2482 1083
2483 - update_post_meta( $post_ID, self::COURSE_PRICE_TYPE_META, self::PRICE_TYPE_PAID );
2484 -
2485 1084 if ( 'wc' === $monetize_by ) {
2486 1085
2487 - $is_update = ( $product_id && wc_get_product( $product_id ) ) ? true : false;
1086 + $is_update = false;
1087 + if ( $attached_product_id ) {
1088 + $wc_product = get_post_meta( $attached_product_id, '_product_version', true );
1089 + if ( $wc_product ) {
1090 + $is_update = true;
1091 + }
1092 + }
2488 1093
2489 - if ( $is_update ) {
2490 - update_post_meta( $post_ID, self::COURSE_PRODUCT_ID_META, $product_id );
1094 + if ( $is_update || ( $product_id > 0 && $is_admin_panel ) ) {
1095 + // Added in @since 2.0.7.
1096 + if ( $product_id > 0 && $is_admin_panel ) {
1097 + $attached_product_id = $product_id;
1098 + update_post_meta( $post_ID, '_tutor_course_product_id', $product_id );
1099 + }
2491 1100
2492 - $product_id = self::create_wc_product( $course->post_title, $course_price, $sale_price, $product_id );
2493 - $product_obj = wc_get_product( $product_id );
1101 + $product_obj = wc_get_product( $attached_product_id );
1102 + $product_id = self::create_wc_product( $course->post_title, $course_price, $sale_price, $attached_product_id );
2494 1103 if ( $product_obj->is_type( 'subscription' ) ) {
2495 - update_post_meta( $product_id, '_subscription_price', $course_price );
1104 + update_post_meta( $attached_product_id, '_subscription_price', $course_price );
2496 1105 }
2497 -
2498 - // Set course regular & sale price.
2499 - self::set_course_regular_and_sale_price( $post_ID, $product_obj->get_regular_price(), $product_obj->get_sale_price() );
2500 1106 } else {
2501 - // Create new WC product name with course title.
2502 1107 $product_id = self::create_wc_product( $course->post_title, $course_price, $sale_price );
2503 1108 if ( $product_id ) {
2504 - $product_obj = wc_get_product( $product_id );
1109 + update_post_meta( $post_ID, '_tutor_course_product_id', $product_id );
1110 + // Mark product for woocommerce.
1111 + update_post_meta( $product_id, '_virtual', 'yes' );
1112 + update_post_meta( $product_id, '_tutor_product', 'yes' );
2505 1113
2506 - self::sync_course_with_wc_product( $post_ID, $product_id );
2507 -
2508 - // Set course regular & sale price.
2509 - self::set_course_regular_and_sale_price( $post_ID, $product_obj->get_regular_price(), $product_obj->get_sale_price() );
1114 + $course_post_thumbnail = get_post_meta( $post_ID, '_thumbnail_id', true );
1115 + if ( $course_post_thumbnail ) {
1116 + set_post_thumbnail( $product_id, $course_post_thumbnail );
1117 + }
2510 1118 }
2511 1119 }
2512 -
2513 - $course_post_thumbnail = Input::post( 'thumbnail_id', 0, Input::TYPE_INT );
2514 - if ( $product_id && $course_post_thumbnail ) {
2515 - set_post_thumbnail( $product_id, $course_post_thumbnail );
2516 - }
2517 1120 } elseif ( 'edd' === $monetize_by ) {
2518 1121
2519 1122 $is_update = false;
2520 1123
@@ -2541,9 +1144,9 @@
2541 1144 if ( $download_id ) {
2542 1145 // EDD edd_price.
2543 1146 update_post_meta( $download_id, 'edd_price', $course_price );
2544 1147
2545 - update_post_meta( $post_ID, self::COURSE_PRODUCT_ID_META, $download_id );
1148 + update_post_meta( $post_ID, '_tutor_course_product_id', $download_id );
2546 1149 // Mark product for EDD.
2547 1150 update_post_meta( $download_id, '_tutor_product', 'yes' );
2548 1151
2549 1152 $course_post_thumbnail = get_post_meta( $post_ID, '_thumbnail_id', true );
@@ -2555,8 +1158,33 @@
2555 1158 }
2556 1159 }
2557 1160
2558 1161 /**
1162 + * Add Course level to course settings
1163 + *
1164 + * @since 1.4.1
1165 + *
1166 + * @param array $args arguments.
1167 + * @return array
1168 + */
1169 + public function add_course_level_to_settings( $args ) {
1170 + $course_id = get_the_ID();
1171 + $levels = tutor_utils()->course_levels();
1172 + $course_level = get_post_meta( $course_id, '_tutor_course_level', true );
1173 +
1174 + $args['general']['fields']['_tutor_course_level'] = array(
1175 + 'type' => 'select',
1176 + 'label' => __( 'Difficulty Level', 'tutor' ),
1177 + 'label_title' => __( 'Enable', 'tutor' ),
1178 + 'options' => $levels,
1179 + 'value' => $course_level ? $course_level : 'intermediate',
1180 + 'desc' => __( 'Course difficulty level', 'tutor' ),
1181 + );
1182 +
1183 + return $args;
1184 + }
1185 +
1186 + /**
2559 1187 * Check if course starting
2560 1188 *
2561 1189 * @since 1.4.8
2562 1190 * @return void
@@ -2576,9 +1204,8 @@
2576 1204 /**
2577 1205 * Add Course level to course settings
2578 1206 *
2579 1207 * @since 1.4.8
2580 - *
2581 1208 * @return void
2582 1209 */
2583 1210 public function course_elements_enable_disable() {
2584 1211 add_filter( 'tutor_course/single/completing-progress-bar', array( $this, 'enable_disable_course_progress_bar' ) );
@@ -2611,9 +1238,8 @@
2611 1238 *
2612 1239 * @since 1.4.8
2613 1240 *
2614 1241 * @param string $html HTML string.
2615 - *
2616 1242 * @return string
2617 1243 */
2618 1244 public function enable_disable_material_includes( $html ) {
2619 1245 $disable_option = ! (bool) get_tutor_option( 'enable_course_material', true, true );
@@ -2628,9 +1254,8 @@
2628 1254 *
2629 1255 * @since 1.4.8
2630 1256 *
2631 1257 * @param string $html HTML string.
2632 - *
2633 1258 * @return string
2634 1259 */
2635 1260 public function enable_disable_course_content( $html ) {
2636 1261 $disable_option = ! (bool) tutor_utils()->get_option( 'enable_course_description', true, true );
@@ -2645,9 +1270,8 @@
2645 1270 *
2646 1271 * @since 1.4.8
2647 1272 *
2648 1273 * @param string $html HTML string.
2649 - *
2650 1274 * @return string
2651 1275 */
2652 1276 public function enable_disable_course_benefits( $html ) {
2653 1277 $disable_option = ! (bool) tutor_utils()->get_option( 'enable_course_benefits', true, true );
@@ -2662,9 +1286,8 @@
2662 1286 *
2663 1287 * @since 1.4.8
2664 1288 *
2665 1289 * @param string $html HTML string.
2666 - *
2667 1290 * @return string
2668 1291 */
2669 1292 public function enable_disable_course_requirements( $html ) {
2670 1293 $disable_option = ! (bool) tutor_utils()->get_option( 'enable_course_requirements', true, true );
@@ -2679,9 +1302,8 @@
2679 1302 *
2680 1303 * @since 1.4.8
2681 1304 *
2682 1305 * @param string $html HTML string.
2683 - *
2684 1306 * @return string
2685 1307 */
2686 1308 public function enable_disable_course_target_audience( $html ) {
2687 1309 $disable_option = ! (bool) tutor_utils()->get_option( 'enable_course_target_audience', true, true );
@@ -2726,9 +1348,9 @@
2726 1348 unset( $items['reviews'] );
2727 1349 }
2728 1350
2729 1351 // Whether enrollment require.
2730 - $is_enrolled = EnrollmentModel::is_enrolled();
1352 + $is_enrolled = tutor_utils()->is_enrolled();
2731 1353
2732 1354 return array_filter(
2733 1355 $items,
2734 1356 function ( $item ) use ( $is_enrolled ) {
@@ -2743,9 +1365,8 @@
2743 1365 /**
2744 1366 * Filter product in shop page
2745 1367 *
2746 1368 * @since 1.4.9
2747 - *
2748 1369 * @return void|null
2749 1370 */
2750 1371 public function filter_product_in_shop_page() {
2751 1372 $hide_course_from_shop_page = (bool) get_tutor_option( 'hide_course_from_shop_page' );
@@ -2761,9 +1382,8 @@
2761 1382 /**
2762 1383 * Tutor product meta query
2763 1384 *
2764 1385 * @since 1.4.9
2765 - *
2766 1386 * @return array
2767 1387 */
2768 1388 public function tutor_product_meta_query() {
2769 1389 $meta_query = array(
@@ -2778,9 +1398,8 @@
2778 1398 *
2779 1399 * @since 1.4.9
2780 1400 *
2781 1401 * @param \WP_Query $wp_query WP Query instance.
2782 - *
2783 1402 * @return \WP_Query
2784 1403 */
2785 1404 public function filter_woocommerce_product_query( $wp_query ) {
2786 1405 $product_ids = $this->get_connected_wc_product_ids();
@@ -2822,9 +1441,8 @@
2822 1441 *
2823 1442 * @since 1.4.9
2824 1443 *
2825 1444 * @param \WP_Query $query WP Query instance.
2826 - *
2827 1445 * @return \WP_Query
2828 1446 */
2829 1447 public function filter_edd_downloads_query( $query ) {
2830 1448 $query['meta_query'][] = $this->tutor_product_meta_query();
@@ -2836,9 +1454,8 @@
2836 1454 *
2837 1455 * @since 1.4.9
2838 1456 *
2839 1457 * @param \WP_Query $wp_query WP Query instance.
2840 - *
2841 1458 * @return \WP_Query
2842 1459 */
2843 1460 public function filter_archive_meta_query( $wp_query ) {
2844 1461 if ( ! is_admin() && $wp_query->is_archive && $wp_query->get( 'post_type' ) === 'download' ) {
@@ -2852,9 +1469,8 @@
2852 1469 *
2853 1470 * @since 1.5.8
2854 1471 *
2855 1472 * @param string $html HTML string.
2856 - *
2857 1473 * @return string
2858 1474 */
2859 1475 public function remove_price_if_enrolled( $html ) {
2860 1476 $should_removed = apply_filters( 'should_remove_price_if_enrolled', true );
@@ -2860,9 +1476,9 @@
2860 1476 $should_removed = apply_filters( 'should_remove_price_if_enrolled', true );
2861 1477
2862 1478 if ( $should_removed ) {
2863 1479 $course_id = get_the_ID();
2864 - $enrolled = EnrollmentModel::is_enrolled( $course_id );
1480 + $enrolled = tutor_utils()->is_enrolled( $course_id );
2865 1481 if ( $enrolled ) {
2866 1482 $html = '';
2867 1483 }
2868 1484 }
@@ -2869,119 +1485,112 @@
2869 1485 return $html;
2870 1486 }
2871 1487
2872 1488 /**
2873 - * Get course completion missing requirements message.
1489 + * Check if all lessons and quizzes done before mark course complete.
2874 1490 *
2875 - * @since 4.0.0
1491 + * @since 1.5.8
2876 1492 *
2877 - * @param int $course_id Course ID.
2878 - * @param int $user_id User ID.
2879 - *
2880 - * @return string|null
1493 + * @param string $html HTML string.
1494 + * @return string
2881 1495 */
2882 - public static function get_course_completion_restrict_msg( $course_id = 0, $user_id = 0 ) {
2883 - $course_id = tutor_utils()->get_post_id( $course_id );
2884 - $user_id = tutor_utils()->get_user_id( $user_id );
1496 + public function tutor_lms_hide_course_complete_btn( $html ) {
2885 1497
2886 - if ( 'strict' !== tutor_utils()->get_option( 'course_completion_process' ) ) {
2887 - return null;
1498 + $completion_mode = tutor_utils()->get_option( 'course_completion_process' );
1499 + if ( 'strict' !== $completion_mode ) {
1500 + return $html;
2888 1501 }
2889 1502
2890 - $completed_lessons = tutor_utils()->get_completed_lesson_count_by_course( $course_id, $user_id );
2891 - $total_lessons = tutor_utils()->get_lesson_count_by_course( $course_id );
1503 + $completed_lesson = tutor_utils()->get_completed_lesson_count_by_course();
1504 + $lesson_count = tutor_utils()->get_lesson_count_by_course();
2892 1505
2893 - if ( $completed_lessons < $total_lessons ) {
2894 - return __( 'Complete all lessons to mark this course as complete', 'tutor' );
1506 + if ( $completed_lesson < $lesson_count ) {
1507 + return '<div class="tutor-alert tutor-warning tutor-mt-28">
1508 + <div class="tutor-alert-text">
1509 + <span class="tutor-alert-icon tutor-fs-4 tutor-icon-circle-info tutor-mr-12"></span>
1510 + <span>' . __( 'Complete all lessons to mark this course as complete', 'tutor' ) . '</span>
1511 + </div>
1512 + </div>';
2895 1513 }
2896 1514
2897 - $course_contents = tutor_utils()->get_course_contents_by_id( $course_id );
1515 + $quizzes = array();
1516 + $assignments = array();
2898 1517
2899 - $required_quiz_pass_count = 0;
2900 - $required_assignment_pass_count = 0;
1518 + $course_contents = tutor_utils()->get_course_contents_by_id();
1519 + if ( tutor_utils()->count( $course_contents ) ) {
1520 + foreach ( $course_contents as $content ) {
1521 + if ( 'tutor_quiz' === $content->post_type ) {
1522 + $quizzes[] = $content;
1523 + }
1524 + if ( 'tutor_assignments' === $content->post_type ) {
1525 + $assignments[] = $content;
1526 + }
1527 + }
1528 + }
2901 1529
2902 - $is_assignment_addon_enabled = tutor_utils()->is_addon_enabled( 'tutor-assignments' );
1530 + $required_assignment_pass = 0;
2903 1531
2904 - foreach ( $course_contents as $content ) {
1532 + foreach ( $assignments as $row ) {
2905 1533
2906 - if ( tutor()->quiz_post_type === $content->post_type ) {
2907 - if ( ! QuizModel::is_quiz_passed( $content->ID, $user_id ) ) {
2908 - ++$required_quiz_pass_count;
2909 - }
2910 - }
1534 + $submitted_assignment = tutor_utils()->is_assignment_submitted( $row->ID );
1535 + $is_reviewed_by_instructor = null === $submitted_assignment
1536 + ? false
1537 + : get_comment_meta( $submitted_assignment->comment_ID, 'evaluate_time', true );
2911 1538
2912 - if ( $is_assignment_addon_enabled && tutor()->assignment_post_type === $content->post_type ) {
2913 - if ( ! Assignments::is_assignment_passed( $content->ID, $user_id ) ) {
2914 - ++$required_assignment_pass_count;
1539 + if ( $submitted_assignment && $is_reviewed_by_instructor ) {
1540 + $pass_mark = tutor_utils()->get_assignment_option( $submitted_assignment->comment_post_ID, 'pass_mark' );
1541 + $given_mark = get_comment_meta( $submitted_assignment->comment_ID, 'assignment_mark', true );
1542 +
1543 + if ( $given_mark < $pass_mark ) {
1544 + $required_assignment_pass++;
2915 1545 }
1546 + } else {
1547 + $required_assignment_pass++;
2916 1548 }
2917 1549 }
2918 1550
2919 - if ( ! $required_quiz_pass_count && ! $required_assignment_pass_count ) {
2920 - return null;
2921 - }
1551 + $is_quiz_pass = true;
1552 + $required_quiz_pass = 0;
2922 1553
2923 - return self::get_course_completion_requirement_message(
2924 - $required_quiz_pass_count,
2925 - $required_assignment_pass_count
2926 - );
2927 - }
1554 + if ( tutor_utils()->count( $quizzes ) ) {
1555 + foreach ( $quizzes as $quiz ) {
2928 1556
2929 - /**
2930 - * Build missing course completion requirements message.
2931 - *
2932 - * @since 4.0.0
2933 - *
2934 - * @param int $quiz_count Total quiz count.
2935 - * @param int $assignment_count Total assignment count.
2936 - *
2937 - * @return string
2938 - */
2939 - private static function get_course_completion_requirement_message( $quiz_count, $assignment_count ) {
2940 - $quiz_label = _n( 'quiz', 'quizzes', $quiz_count, 'tutor' );
2941 - $assignment_label = _n( 'assignment', 'assignments', $assignment_count, 'tutor' );
1557 + $attempt = tutor_utils()->get_quiz_attempt( $quiz->ID );
1558 + if ( $attempt ) {
1559 + $passing_grade = tutor_utils()->get_quiz_option( $quiz->ID, 'passing_grade', 0 );
1560 + $earned_percentage = $attempt->earned_marks > 0 ? ( number_format( ( $attempt->earned_marks * 100 ) / $attempt->total_marks ) ) : 0;
2942 1561
2943 - if ( $quiz_count && ! $assignment_count ) {
2944 - return sprintf(
2945 - /* translators: %1$s: item count; %2$s: item label */
2946 - __( 'You have to pass %1$s %2$s to complete this course.', 'tutor' ),
2947 - $quiz_count,
2948 - $quiz_label
2949 - );
1562 + if ( $earned_percentage < $passing_grade ) {
1563 + $required_quiz_pass++;
1564 + $is_quiz_pass = false;
1565 + }
1566 + } else {
1567 + $required_quiz_pass++;
1568 + $is_quiz_pass = false;
1569 + }
1570 + }
2950 1571 }
2951 1572
2952 - if ( ! $quiz_count && $assignment_count ) {
2953 - return sprintf(
2954 - /* translators: %1$s: item count; %2$s: item label */
2955 - __( 'You have to pass %1$s %2$s to complete this course.', 'tutor' ),
2956 - $assignment_count,
2957 - $assignment_label
2958 - );
2959 - }
1573 + if ( ! $is_quiz_pass || $required_assignment_pass > 0 ) {
1574 + $_msg = '';
1575 + $quiz_str = _n( 'quiz', 'quizzes', $required_quiz_pass, 'tutor' );
1576 + $assignment_str = _n( 'assignment', 'assignments', $required_assignment_pass, 'tutor' );
2960 1577
2961 - return sprintf(
2962 - /* translators: %1$s: quiz count; %2$s: quiz label; %3$s: assignment count; %4$s: assignment label */
2963 - __( 'You have to pass %1$s %2$s and %3$s %4$s to complete this course.', 'tutor' ),
2964 - $quiz_count,
2965 - $quiz_label,
2966 - $assignment_count,
2967 - $assignment_label
2968 - );
2969 - }
1578 + if ( ! $is_quiz_pass && 0 == $required_assignment_pass ) {
1579 + /* translators: %1$s: number of quiz/assignment pass required; %2$s: quiz/assignment string */
1580 + $_msg = sprintf( __( 'You have to pass %1$s %2$s to complete this course.', 'tutor' ), $required_quiz_pass, $quiz_str );
1581 + }
2970 1582
2971 - /**
2972 - * Check if all lessons and quizzes done before mark course complete.
2973 - *
2974 - * @since 1.5.8
2975 - *
2976 - * @param string $html HTML string.
2977 - *
2978 - * @return string
2979 - */
2980 - public function tutor_lms_hide_course_complete_btn( $html ) {
2981 - $_msg = self::get_course_completion_restrict_msg();
1583 + if ( $is_quiz_pass && $required_assignment_pass > 0 ) {
1584 + //phpcs:ignore
1585 + $_msg = sprintf( __( 'You have to pass %1$s %2$s to complete this course.', 'tutor' ), $required_assignment_pass, $assignment_str );
1586 + }
2982 1587
2983 - if ( $_msg ) {
1588 + if ( ! $is_quiz_pass && $required_assignment_pass > 0 ) {
1589 + /* translators: %1$s: number of quiz pass required; %2$s: quiz string; %3$s: number of assignment pass required; %4$s: assignment string */
1590 + $_msg = sprintf( __( 'You have to pass %1$s %2$s and %3$s %4$s to complete this course.', 'tutor' ), $required_quiz_pass, $quiz_str, $required_assignment_pass, $assignment_str );
1591 + }
1592 +
2984 1593 return '<div class="tutor-alert tutor-warning tutor-mt-28">
2985 1594 <div class="tutor-alert-text">
2986 1595 <span class="tutor-alert-icon tutor-fs-4 tutor-icon-circle-info tutor-mr-12"></span>
2987 1596 <span>' . $_msg . '</span>
@@ -2997,9 +1606,8 @@
2997 1606 *
2998 1607 * @since 1.5.8
2999 1608 *
3000 1609 * @param string $html HTML string.
3001 - *
3002 1610 * @return string
3003 1611 */
3004 1612 public function get_generate_greadbook( $html ) {
3005 1613 if ( ! tutor_utils()->is_completed_course() ) {
@@ -3011,9 +1619,8 @@
3011 1619 /**
3012 1620 * Add social share content in header
3013 1621 *
3014 1622 * @since 1.6.3
3015 - *
3016 1623 * @return void
3017 1624 */
3018 1625 public function social_share_content() {
3019 1626 global $wp_query, $post;
@@ -3037,9 +1644,8 @@
3037 1644 *
3038 1645 * @since 1.8.2
3039 1646 *
3040 1647 * @param integer $post_id post ID.
3041 - *
3042 1648 * @return void
3043 1649 */
3044 1650 public function delete_associated_enrollment( $post_id ) {
3045 1651 global $wpdb;
@@ -3050,12 +1656,11 @@
3050 1656 post_id
3051 1657 FROM
3052 1658 {$wpdb->postmeta}
3053 1659 WHERE
3054 - meta_key=%s
1660 + meta_key='_tutor_enrolled_by_order_id'
3055 1661 AND meta_value = %d
3056 1662 ",
3057 - EnrollmentModel::ENROLLMENT_ORDER_ID_META,
3058 1663 $post_id
3059 1664 )
3060 1665 );
3061 1666
@@ -3071,35 +1676,21 @@
3071 1676 /**
3072 1677 * Reset course progress.
3073 1678 *
3074 1679 * @since 1.5.8
3075 - *
3076 1680 * @return void
3077 1681 */
3078 1682 public function tutor_reset_course_progress() {
3079 1683 tutor_utils()->checking_nonce();
3080 - $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
3081 - $context = Input::post( 'context', '' );
3082 - $course_reset_progress = tutor_utils()->get_option( 'course_reset_progress', false );
3083 - $course_retake_feature = tutor_utils()->get_option( 'course_retake_feature', false );
1684 + $course_id = Input::post( 'course_id' );
3084 1685
3085 - if ( ! $course_reset_progress && 'learning-area-sidebar' === $context ) {
3086 - $this->response_bad_request( __( 'You do not have permission to reset this course.', 'tutor' ) );
1686 + if ( ! $course_id || ! is_numeric( $course_id ) || ! tutor_utils()->is_enrolled( $course_id ) ) {
1687 + wp_send_json_error( array( 'message' => __( 'Invalid Course ID or Access Denied.', 'tutor' ) ) );
3087 1688 return;
3088 1689 }
3089 1690
3090 - if ( ! $course_retake_feature && ( 'course-landing' === $context || 'learning-area' === $context ) ) {
3091 - $this->response_bad_request( __( 'You do not have permission to retake this course.', 'tutor' ) );
3092 - return;
3093 - }
3094 -
3095 - if ( ! $course_id || ! is_numeric( $course_id ) || ! EnrollmentModel::is_enrolled( $course_id ) ) {
3096 - $this->response_bad_request( __( 'Invalid Course ID or Access Denied.', 'tutor' ) );
3097 - return;
3098 - }
3099 -
3100 1691 tutor_utils()->delete_course_progress( $course_id );
3101 - $this->json_response( '', array( 'redirect_to' => tutor_utils()->get_course_first_lesson( $course_id ) ) );
1692 + wp_send_json_success( array( 'redirect_to' => tutor_utils()->get_course_first_lesson( $course_id ) ) );
3102 1693 }
3103 1694
3104 1695 /**
3105 1696 * Do enroll if guest attempt to enroll and course is free
@@ -3107,19 +1698,17 @@
3107 1698 * @since 1.9.8
3108 1699 *
3109 1700 * @param integer $course_id course ID.
3110 1701 * @param integer $user_id user ID.
3111 - *
1702 +
3112 1703 * @return void
3113 1704 */
3114 1705 public function enroll_after_login_if_attempt( int $course_id, int $user_id ) {
3115 - $course_id = sanitize_text_field( $course_id );
3116 - $is_allowed = apply_filters( 'tutor_allow_guest_attempt_enrollment', true, $course_id, $user_id );
3117 -
3118 - if ( $course_id && $is_allowed ) {
1706 + $course_id = sanitize_text_field( $course_id );
1707 + if ( $course_id ) {
3119 1708 $is_purchasable = tutor_utils()->is_course_purchasable( $course_id );
3120 1709 if ( ! $is_purchasable ) {
3121 - EnrollmentModel::do_enroll( $course_id, $order_id = 0, $user_id );
1710 + tutor_utils()->do_enroll( $course_id, $order_id = 0, $user_id );
3122 1711 do_action( 'guest_attempt_after_enrollment', $course_id );
3123 1712 }
3124 1713 }
3125 1714 }
@@ -3127,9 +1716,8 @@
3127 1716 /**
3128 1717 * Handle course enrollment
3129 1718 *
3130 1719 * @since 2.1.0
3131 - *
3132 1720 * @return void
3133 1721 */
3134 1722 public function course_enrollment() {
3135 1723 tutor_utils()->checking_nonce();
@@ -3136,46 +1724,17 @@
3136 1724
3137 1725 $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
3138 1726 $user_id = get_current_user_id();
3139 1727
3140 - if ( ! $course_id || ! $user_id ) {
3141 - wp_send_json_error( tutor_utils()->error_message( 'invalid_req' ) );
3142 - }
3143 -
3144 - $password_protected = post_password_required( $course_id );
3145 - if ( $password_protected ) {
3146 - wp_send_json_error( __( 'This course is password protected', 'tutor' ) );
3147 - }
3148 -
3149 - $course = get_post( $course_id );
3150 -
3151 - if ( CourseModel::STATUS_PRIVATE === $course->post_status && ! current_user_can( 'read_private_tutor_courses' ) ) {
3152 - wp_send_json_error( __( 'You do not have permission to enroll in this course', 'tutor' ) );
3153 - }
3154 -
3155 - /**
3156 - * This check was added to address a security issue where users could
3157 - * enroll in a course via an AJAX call without purchasing it.
3158 - *
3159 - * To prevent this, we now verify whether the course is paid.
3160 - * Additionally, we check if the user is already enrolled, since
3161 - * Tutor's default behavior enrolls users automatically upon purchase.
3162 - *
3163 - * @since 3.9.4
3164 - */
3165 - if ( tutor_utils()->is_course_purchasable( $course_id ) ) {
3166 - $is_enrolled = (bool) EnrollmentModel::is_enrolled( $course_id, $user_id );
3167 -
3168 - if ( ! $is_enrolled ) {
3169 - wp_send_json_error( __( 'Please purchase the course before enrolling', 'tutor' ) );
1728 + if ( $course_id ) {
1729 + $enroll = tutor_utils()->do_enroll( $course_id, 0, $user_id );
1730 + if ( $enroll ) {
1731 + wp_send_json_success( __( 'Enrollment successfully done!', 'tutor' ) );
1732 + } else {
1733 + wp_send_json_error( __( 'Enrollment failed, please try again!', 'tutor' ) );
3170 1734 }
3171 - }
3172 -
3173 - $enroll = EnrollmentModel::do_enroll( $course_id, 0, $user_id );
3174 - if ( $enroll ) {
3175 - wp_send_json_success( __( 'Enrollment successfully done!', 'tutor' ) );
3176 1735 } else {
3177 - wp_send_json_error( __( 'Enrollment failed, please try again!', 'tutor' ) );
1736 + wp_send_json_error( __( 'Invalid course ID', 'tutor' ) );
3178 1737 }
3179 1738 }
3180 1739
3181 1740 /**
@@ -3210,15 +1769,11 @@
3210 1769 * @param string $sale_price product sale price.
3211 1770 * @param int $product_id product ID.
3212 1771 * @param string $status product status.
3213 1772 *
3214 - * @return integer Product id or return 0 if WC not exists
1773 + * @return integer
3215 1774 */
3216 1775 public static function create_wc_product( $title, $reg_price, $sale_price, $product_id = 0, $status = 'publish' ) {
3217 - if ( ! tutor_utils()->has_wc() ) {
3218 - return 0;
3219 - }
3220 -
3221 1776 $product_obj = new \WC_Product();
3222 1777 if ( $product_id ) {
3223 1778 $product_obj = wc_get_product( $product_id );
3224 1779 }
@@ -3236,308 +1791,6 @@
3236 1791
3237 1792 $product_obj->set_sold_individually( true );
3238 1793
3239 1794 return $product_obj->save();
3240 - }
3241 -
3242 - /**
3243 - * Get course/bundle mini info
3244 - *
3245 - * @since 3.0.0
3246 - *
3247 - * @param object $post Course or bundle post.
3248 - *
3249 - * @return array
3250 - */
3251 - public static function get_mini_info( object $post ) {
3252 - $is_purchasable = tutor_utils()->is_course_purchasable( $post->ID );
3253 - $course_price = tutor_utils()->get_raw_course_price( $post->ID );
3254 - $regular_price = tutor_get_formatted_price( $course_price->regular_price );
3255 - $sale_price = ! empty( $course_price->sale_price ) ? tutor_get_formatted_price( $course_price->sale_price ) : null;
3256 -
3257 - $info = array(
3258 - 'id' => $post->ID,
3259 - 'title' => $post->post_title,
3260 - 'image' => get_tutor_course_thumbnail_src( 'post-thumbnail', $post->ID ),
3261 - 'is_purchasable' => $is_purchasable,
3262 - 'regular_price' => $regular_price,
3263 - 'sale_price' => $sale_price,
3264 - );
3265 -
3266 - $card_data = apply_filters( 'tutor_course_mini_info', $info, $post );
3267 -
3268 - return $card_data;
3269 - }
3270 -
3271 - /**
3272 - * Get course/bundle card data
3273 - *
3274 - * This method will return all data that contain in
3275 - * course card
3276 - *
3277 - * @since 3.0.0
3278 - *
3279 - * @param object $post Course or bundle post.
3280 - *
3281 - * @return array
3282 - */
3283 - public static function get_card_data( object $post ) {
3284 - $info = self::get_mini_info( $post );
3285 -
3286 - $info['last_updated'] = tutor_i18n_get_formated_date( $post->post_modified_at );
3287 - $info['course_duration'] = tutor_utils()->get_course_duration( $post->ID, false );
3288 - $info['total_enrolled'] = tutor_utils()->count_enrolled_users_by_course( $post->ID );
3289 -
3290 - $card_data = apply_filters( 'tutor_course_card_data', $info, $post );
3291 -
3292 - return $card_data;
3293 - }
3294 -
3295 - /**
3296 - * Filter user list access for instructor
3297 - *
3298 - * @since 3.0.0
3299 - *
3300 - * @param bool $access access.
3301 - *
3302 - * @return bool
3303 - */
3304 - public function user_list_access_for_instructor( $access ) {
3305 - $is_instructor = User::is_instructor();
3306 - return $access || $is_instructor;
3307 - }
3308 -
3309 - /**
3310 - * Filter user list args for instructor
3311 - *
3312 - * @since 3.0.0
3313 - *
3314 - * @param array $args args.
3315 - *
3316 - * @return array
3317 - */
3318 - public function user_list_args_for_instructor( $args ) {
3319 - if ( User::is_instructor() ) {
3320 - if ( isset( $args['fields'] ) && isset( $args['fields']['user_email'] ) ) {
3321 - unset( $args['fields']['user_email'] );
3322 - }
3323 - }
3324 -
3325 - $filter = json_decode( wp_unslash( $_POST['filter'] ?? '{}' ) );//phpcs:ignore
3326 - if ( isset( $filter->role ) && is_array( $filter->role ) ) {
3327 - $args['role__in'] = array_map( 'sanitize_text_field', $filter->role );
3328 - }
3329 -
3330 - return $args;
3331 - }
3332 -
3333 - /**
3334 - * Get a list of possible course status.
3335 - *
3336 - * @since 3.6.2
3337 - *
3338 - * @return array
3339 - */
3340 - public static function course_status_list() {
3341 - return array(
3342 - 'publish',
3343 - 'private',
3344 - 'draft',
3345 - 'trash',
3346 - 'pending',
3347 - 'future',
3348 - );
3349 - }
3350 -
3351 - /**
3352 - * Link a course/bundle post to a WooCommerce product.
3353 - *
3354 - * @since 3.8.2
3355 - *
3356 - * @param int $post_ID The WordPress post ID of the course.
3357 - * @param int $product_id The WooCommerce product ID to associate with the course.
3358 - *
3359 - * @return void
3360 - */
3361 - public static function sync_course_with_wc_product( $post_ID, $product_id ) {
3362 -
3363 - update_post_meta( $post_ID, self::COURSE_PRODUCT_ID_META, $product_id );
3364 -
3365 - // Mark product for woocommerce.
3366 - update_post_meta( $product_id, '_virtual', 'yes' );
3367 - update_post_meta( $product_id, '_tutor_product', 'yes' );
3368 - }
3369 -
3370 - /**
3371 - * Map Tutor's course prices to WooCommerce.
3372 - *
3373 - * @since 3.8.2
3374 - *
3375 - * @param int $post_ID The WordPress post ID of the course.
3376 - * @param string|int|float $regular_price The regular price.
3377 - * @param string|int|float $sale_price The sale price.
3378 - * @return void
3379 - */
3380 - private static function set_course_regular_and_sale_price( $post_ID, $regular_price, $sale_price ) {
3381 -
3382 - // Set course regular & sale price.
3383 - update_post_meta( $post_ID, self::COURSE_PRICE_META, $regular_price );
3384 - update_post_meta( $post_ID, self::COURSE_SALE_PRICE_META, $sale_price );
3385 - }
3386 -
3387 - /**
3388 - * Render start/resume button in enrolled course action btn.
3389 - *
3390 - * @since 4.0.0
3391 - *
3392 - * @param int $course_id course id.
3393 - *
3394 - * @return void
3395 - */
3396 - public function render_course_action_btn( int $course_id ) {
3397 - $is_completed = tutor_utils()->is_completed_course( $course_id );
3398 - if ( $is_completed ) {
3399 - $certificate_addon_active = tutor_utils()->is_addon_enabled( 'tutor-certificate' );
3400 - if ( ! $certificate_addon_active ) {
3401 - Button::make()
3402 - ->tag( 'a' )
3403 - ->label( __( 'Completed', 'tutor' ) )
3404 - ->icon( Icon::COMPLETED_CIRCLE )
3405 - ->variant( Variant::PRIMARY )
3406 - ->size( Size::X_SMALL )
3407 - ->attr( 'href', esc_url( get_the_permalink( $course_id ) ) )
3408 - ->render();
3409 - }
3410 - return;
3411 - }
3412 -
3413 - $course_progress = tutor_utils()->get_course_completed_percent( $course_id, 0, true );
3414 - $button_text = $course_progress['completed_percent'] > 0 ? __( 'Resume', 'tutor' ) : __( 'Start', 'tutor' );
3415 - $button_url = tutor_utils()->get_course_first_lesson( $course_id );
3416 -
3417 - if ( ! $button_url ) {
3418 - $button_url = get_the_permalink( $course_id );
3419 - }
3420 -
3421 - Button::make()
3422 - ->tag( 'a' )
3423 - ->label( $button_text )
3424 - ->icon( Icon::PLAY_2 )
3425 - ->variant( Variant::PRIMARY )
3426 - ->size( Size::X_SMALL )
3427 - ->attr( 'href', esc_url( $button_url ) )
3428 - ->render();
3429 - }
3430 -
3431 - /**
3432 - * Get the content for the course completion modal.
3433 - *
3434 - * This method returns HTML for displaying a modal that informs the user
3435 - * that they have not completed all required lessons and assessments. It includes
3436 - * the user's current progress shown as a percentage and a progress bar.
3437 - *
3438 - * @since 4.0.0
3439 - *
3440 - * @param float $course_progress The completion percentage of the course.
3441 - *
3442 - * @return string The rendered HTML content for the modal.
3443 - */
3444 - public static function get_complete_modal_content( float $course_progress = 0 ): string {
3445 - ob_start();
3446 - ?>
3447 - <div>
3448 - <p class="tutor-p3 tutor-text-secondary tutor-text-center tutor-mb-7 tutor-px-11">
3449 - <?php esc_html_e( 'You have not completed all required lessons and assessments. ', 'tutor' ); ?>
3450 - </p>
3451 - <div class="tutor-finish-course-progress tutor-border tutor-p-5 tutor-flex tutor-flex-column tutor-gap-4 tutor-surface-base tutor-rounded-md">
3452 - <div class="tutor-flex tutor-items-center tutor-justify-between">
3453 - <div class="tutor-p3 tutor-text-secondary">
3454 - <?php esc_html_e( 'Your Progress', 'tutor' ); ?>
3455 - </div>
3456 - <div class="tutor-p1 tutor-font-bold">
3457 - <?php echo esc_html( number_format_i18n( $course_progress ) . '%' ); ?>
3458 - </div>
3459 - </div>
3460 - <?php
3461 - Progress::make()->type( 'bar' )->value( $course_progress )->render();
3462 - ?>
3463 - </div>
3464 - </div>
3465 - <?php
3466 - return ob_get_clean();
3467 - }
3468 -
3469 - /**
3470 - * Render the course complete button.
3471 - *
3472 - * Displays a button that allows the user to complete the course. If the course
3473 - * progress is less than 100%, clicking the button shows a modal informing the user
3474 - * that not all requirements are fulfilled. If the course progress is 100%,
3475 - * clicking the button attempts to complete the course.
3476 - *
3477 - * @since 4.0.0
3478 - *
3479 - * @param string $modal_id The HTML ID of the modal to display if not complete.
3480 - * @param int $course_id The ID of the course.
3481 - * @param float $course_progress The current completion percentage of the course.
3482 - * @param string $size The button size.
3483 - * @param string $tooltip Optional. Tooltip message.
3484 - * @param bool $block Optional. Whether the button is full-width.
3485 - *
3486 - * @return void
3487 - */
3488 - public static function render_course_complete_btn( string $modal_id, int $course_id, float $course_progress = 0, string $size = Size::MEDIUM, string $tooltip = '', bool $block = false ): void {
3489 - $button = Button::make()
3490 - ->variant( Variant::SECONDARY )
3491 - ->label( __( 'Complete the Course', 'tutor' ) )
3492 - ->icon( Icon::TICK_MARK )
3493 - ->size( $size )
3494 - ->block( $block )
3495 - ->attr( 'type', 'button' );
3496 -
3497 - if ( ! empty( $tooltip ) ) {
3498 - $button->disabled();
3499 - }
3500 -
3501 - if ( $course_progress < 100 ) {
3502 - $button->attr( '@click', "TutorCore.modal.showModal('{$modal_id}')" );
3503 - } else {
3504 - $button->attr( '@click', "handleCourseComplete({$course_id})" );
3505 - $button->attr( ':class', "courseCompleteMutation?.isPending ? 'tutor-btn-loading' : ''" );
3506 - $button->attr( ':disabled', 'courseCompleteMutation?.isPending' );
3507 - }
3508 -
3509 - if ( ! empty( $tooltip ) ) {
3510 - Tooltip::make()
3511 - ->content( $tooltip )
3512 - ->placement( Tooltip::PLACEMENT_BOTTOM )
3513 - ->arrow( Tooltip::ARROW_CENTER )
3514 - ->trigger_element( $button->get() )
3515 - ->render();
3516 - } else {
3517 - $button->render();
3518 - }
3519 - }
3520 -
3521 - /**
3522 - * Render course retake button
3523 - *
3524 - * @since 4.0.0
3525 - *
3526 - * @param string $modal_id Modal id.
3527 - * @param string $size The button size.
3528 - * @param bool $block Optional. Whether the button is full-width.
3529 - *
3530 - * @return void
3531 - */
3532 - public static function render_course_retake_btn( string $modal_id, string $size = Size::MEDIUM, bool $block = false ): void {
3533 - Button::make()
3534 - ->variant( Variant::SECONDARY )
3535 - ->label( __( 'Retake this Course', 'tutor' ) )
3536 - ->icon( Icon::RELOAD_4 )
3537 - ->size( $size )
3538 - ->block( $block )
3539 - ->attr( 'type', 'button' )
3540 - ->attr( '@click', "TutorCore.modal.showModal('{$modal_id}')" )
3541 - ->render();
3542 1795 }
3543 1796 }