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