PluginProbe
Tutor LMS – eLearning and online course solution / 3.4.2
Tutor LMS – eLearning and online course solution v3.4.2
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 +295 -807 trunk3.4.2 View file →
@@ -9,26 +9,20 @@
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;
17 +use stdClass;
18 +use TUTOR\Input;
21 19 use Tutor\Helpers\HttpHelper;
22 20 use Tutor\Models\CourseModel;
23 -use Tutor\Components\Tooltip;
24 21 use Tutor\Ecommerce\Ecommerce;
25 -use Tutor\Helpers\DateTimeHelper;
26 22 use Tutor\Traits\JsonResponse;
27 23 use Tutor\Helpers\ValidationHelper;
28 -use Tutor\Models\EnrollmentModel;
29 -use Tutor\Options_V2;
30 -use TUTOR_ASSIGNMENTS\Assignments;
24 +use TutorPro\CourseBundle\Models\BundleModel;
31 25
32 26 /**
33 27 * Course Class
34 28 *
@@ -69,46 +63,10 @@
69 63 const SELLING_OPTION_BOTH = 'both'; // onetime and subscription.
70 64 const SELLING_OPTION_MEMBERSHIP = 'membership';
71 65 const SELLING_OPTION_ALL = 'all';
72 66
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 67
81 68 /**
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 69 * Additional course meta info
112 70 *
113 71 * @var array
114 72 */
@@ -160,8 +118,15 @@
160 118 */
161 119 add_action( 'save_tutor_course', array( $this, 'attach_product_with_course' ), 10, 2 );
162 120
163 121 /**
122 + * Add course level to course settings
123 + *
124 + * @since v.1.4.1
125 + */
126 + add_filter( 'tutor_course_settings_tabs', array( $this, 'add_course_level_to_settings' ) );
127 +
128 + /**
164 129 * Enable Disable Course Details Page Feature
165 130 *
166 131 * @since v.1.4.8
167 132 */
@@ -207,14 +172,9 @@
207 172 * Delete course data after deleted course
208 173 *
209 174 * @since v.1.6.6
210 175 */
211 - add_action(
212 - 'deleted_post',
213 - function ( $post_id ) {
214 - ( new CourseModel() )->delete_course_data( $post_id );
215 - }
216 - );
176 + add_action( 'deleted_post', array( new CourseModel(), 'delete_course_data' ) );
217 177
218 178 /**
219 179 * Delete course data after deleted course
220 180 *
@@ -257,9 +217,9 @@
257 217 * @since 1.9.8
258 218 */
259 219 add_action( 'tutor_do_enroll_after_login_if_attempt', array( $this, 'enroll_after_login_if_attempt' ), 10, 2 );
260 220
261 - add_action( 'wp_ajax_tutor_update_course_content_order', array( $this, 'ajax_update_course_content_order' ) );
221 + add_action( 'wp_ajax_tutor_update_course_content_order', array( $this, 'tutor_update_course_content_order' ) );
262 222
263 223 add_action( 'wp_ajax_tutor_get_wc_product', array( $this, 'get_wc_product' ) );
264 224 add_action( 'wp_ajax_tutor_get_wc_products', array( $this, 'get_wc_products' ) );
265 225
@@ -307,39 +267,11 @@
307 267 add_filter( 'tutor_user_list_args', array( $this, 'user_list_args_for_instructor' ) );
308 268
309 269 add_filter( 'template_include', array( $this, 'handle_password_protected' ) );
310 270 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 271 }
317 272
318 273 /**
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 274 * Handle password protected course and bundle form submission.
343 275 *
344 276 * @since 3.2.1
345 277 *
@@ -355,31 +287,8 @@
355 287 }
356 288 }
357 289
358 290 /**
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 291 * Handle password protected course/bundle.
383 292 *
384 293 * @since 3.0.0
385 294 *
@@ -553,34 +462,48 @@
553 462 }
554 463 }
555 464
556 465 /**
557 - * Setup course categories and tags.
466 + * Setup course categories and tags
558 467 *
559 468 * @since 3.0.0
560 469 *
561 - * @param int $post_id Post ID.
562 - * @param array $params Array of params.
470 + * @param int $post_id post id.
471 + * @param array $params array of params.
563 472 *
564 473 * @return void
565 474 */
566 475 public function setup_course_categories_tags( $post_id, $params ) {
567 476 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 );
477 + $category_names = array();
478 +
479 + foreach ( $params['course_categories'] as $category_id ) {
480 + $term = get_term( $category_id, CourseModel::COURSE_CATEGORY );
481 +
482 + if ( ! is_wp_error( $term ) && $term ) {
483 + $category_names[] = $term->name;
484 + }
485 + }
486 +
487 + // Set category names on the post.
488 + wp_set_object_terms( $post_id, $category_names, CourseModel::COURSE_CATEGORY );
573 489 } else {
574 490 wp_set_object_terms( $post_id, array(), CourseModel::COURSE_CATEGORY );
575 491 }
576 492
577 493 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 );
494 + $tag_names = array();
495 +
496 + foreach ( $params['course_tags'] as $tag_id ) {
497 + $term = get_term( $tag_id, CourseModel::COURSE_TAG );
498 +
499 + if ( ! is_wp_error( $term ) && $term ) {
500 + $tag_names[] = $term->name;
501 + }
502 + }
503 +
504 + // Set tag names on the post.
505 + wp_set_object_terms( $post_id, $tag_names, CourseModel::COURSE_TAG );
583 506 } else {
584 507 wp_set_object_terms( $post_id, array(), CourseModel::COURSE_TAG );
585 508 }
586 509 }
@@ -794,8 +717,11 @@
794 717 try {
795 718 if ( isset( $params['pricing']['type'] ) ) {
796 719 update_post_meta( $post_id, self::COURSE_PRICE_TYPE_META, $params['pricing']['type'] );
797 720 }
721 + if ( isset( $params['pricing']['product_id'] ) ) {
722 + update_post_meta( $post_id, '_tutor_course_product_id', $params['pricing']['product_id'] );
723 + }
798 724 } catch ( \Throwable $th ) {
799 725 throw new \Exception( $th->getMessage() );
800 726 }
801 727 }
@@ -803,21 +729,8 @@
803 729 update_post_meta( $post_id, '_tutor_enable_qa', $params['enable_qna'] ?? 'yes' );
804 730 update_post_meta( $post_id, '_tutor_is_public_course', $params['is_public_course'] ?? 'no' );
805 731 update_post_meta( $post_id, '_tutor_course_level', $params['course_level'] );
806 732
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 733 do_action( 'tutor_after_prepare_update_post_meta', $post_id, $params );
821 734 }
822 735
823 736 /**
@@ -917,9 +830,9 @@
917 830
918 831 update_post_meta( $course_id, self::COURSE_PRICE_TYPE_META, self::PRICE_TYPE_FREE );
919 832
920 833 $link = admin_url( 'admin.php?page=create-course' );
921 - if ( tutor()->has_pro && Input::post( 'from_dashboard', false, Input::TYPE_BOOL ) ) {
834 + if ( Input::post( 'from_dashboard', false, Input::TYPE_BOOL ) ) {
922 835 $link = tutor_utils()->tutor_dashboard_url( 'create-course' );
923 836 }
924 837
925 838 $link = add_query_arg( array( 'course_id' => $course_id ), $link );
@@ -937,20 +850,20 @@
937 850 * Get course list
938 851 *
939 852 * @since 3.0.0
940 853 *
941 - * @since 3.2.0 Refactor the arguments & response as per new design.
854 + * @since 3.2.0
942 855 *
856 + * Refactor the arguments & response as per new design
857 + *
943 858 * @return void
944 859 */
945 860 public function ajax_course_list() {
946 - tutor_utils()->check_nonce();
947 861 $this->check_access();
948 862
949 863 $limit = Input::post( 'limit', 10, Input::TYPE_INT );
950 864 $offset = Input::post( 'offset', 0, Input::TYPE_INT );
951 865 $search_term = '';
952 - $post_status = Input::post( 'post_status', null );
953 866
954 867 $filter = json_decode( wp_unslash( $_POST['filter'] ) ); //phpcs:ignore --sanitized already
955 868 if ( ! empty( $filter ) && property_exists( $filter, 'search' ) ) {
956 869 $search_term = Input::sanitize( $filter->search );
@@ -956,9 +869,8 @@
956 869 $search_term = Input::sanitize( $filter->search );
957 870 }
958 871
959 872 $args = array(
960 - 'post_status' => is_null( $post_status ) ? 'publish' : $post_status,
961 873 'posts_per_page' => $limit,
962 874 'offset' => $offset,
963 875 's' => $search_term,
964 876 );
@@ -964,9 +876,14 @@
964 876 );
965 877
966 878 $exclude = Input::post( 'exclude', array(), Input::TYPE_ARRAY );
967 879 if ( count( $exclude ) ) {
968 - $exclude = array_filter( $exclude, fn( $id ) => is_numeric( $id ) && $id > 0 );
880 + $exclude = array_filter(
881 + $exclude,
882 + function ( $id ) {
883 + return is_numeric( $id );
884 + }
885 + );
969 886 $args['post__not_in'] = $exclude;
970 887 }
971 888
972 889 $courses = CourseModel::get_courses_by_args( $args );
@@ -1102,17 +1019,12 @@
1102 1019 'course_benefits' => 'sanitize_textarea_field',
1103 1020 'course_target_audience' => 'sanitize_textarea_field',
1104 1021 'course_material_includes' => 'sanitize_textarea_field',
1105 1022 'course_requirements' => 'sanitize_textarea_field',
1106 - ),
1107 - true
1023 + )
1108 1024 );
1109 1025
1110 1026 $course_id = (int) $params['course_id'];
1111 - if ( ! $course_id ) {
1112 - $this->response_bad_request( __( 'Invalid course id', 'tutor' ) );
1113 - }
1114 -
1115 1027 $this->check_access( $course_id );
1116 1028
1117 1029 $errors = array();
1118 1030 $validation = $this->validate_inputs( $params );
@@ -1201,32 +1113,15 @@
1201 1113 public function ajax_unlink_page_builder() {
1202 1114 tutor_utils()->check_nonce();
1203 1115
1204 1116 $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 -
1117 + $builder = Input::post( 'builder' );
1209 1118 $this->check_access( $course_id );
1210 1119
1211 - $builder = Input::post( 'builder' );
1212 1120 if ( 'elementor' === $builder ) {
1213 1121 delete_post_meta( $course_id, '_elementor_edit_mode' );
1214 1122 } elseif ( 'droip' === $builder ) {
1215 1123 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 1124 }
1230 1125
1231 1126 $this->json_response(
1232 1127 __( 'Builder unlinked successfully.', 'tutor' ),
@@ -1259,18 +1154,15 @@
1259 1154
1260 1155 $topic_contents = tutor_utils()->get_course_contents_by_topic( $post->ID, -1 );
1261 1156
1262 1157 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 - );
1158 + foreach ( $topic_contents->get_posts() as $post ) {
1159 + if ( tutor()->quiz_post_type === $post->post_type ) {
1160 + $questions = tutor_utils()->get_questions_by_quiz( $post->ID );
1161 + $post->total_question = is_array( $questions ) ? count( $questions ) : 0;
1270 1162 }
1271 1163
1272 - array_push( $current_topic['contents'], $content );
1164 + array_push( $current_topic['contents'], $post );
1273 1165 }
1274 1166 }
1275 1167
1276 1168 $current_topic = apply_filters( 'tutor_filter_course_content', $current_topic );
@@ -1285,23 +1177,20 @@
1285 1177 /**
1286 1178 * Get course contents
1287 1179 *
1288 1180 * @since 3.0.0
1289 - *
1290 - * @return void
1291 1181 */
1292 1182 public function ajax_course_contents() {
1293 1183 tutor_utils()->check_nonce();
1294 1184
1295 - $errors = array();
1296 1185 $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
1297 1186
1298 - if ( ! $course_id || tutor()->course_post_type !== get_post_type( $course_id ) ) {
1187 + $this->check_access( $course_id );
1188 +
1189 + if ( tutor()->course_post_type !== get_post_type( $course_id ) ) {
1299 1190 $errors['course_id'] = __( 'Invalid course id', 'tutor' );
1300 1191 }
1301 1192
1302 - $this->check_access( $course_id );
1303 -
1304 1193 if ( ! empty( $errors ) ) {
1305 1194 $this->json_response( __( 'Invalid input', 'tutor' ), $errors, HttpHelper::STATUS_UNPROCESSABLE_ENTITY );
1306 1195 }
1307 1196
@@ -1325,14 +1214,14 @@
1325 1214
1326 1215 $errors = array();
1327 1216 $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
1328 1217
1329 - if ( ! $course_id || tutor()->course_post_type !== get_post_type( $course_id ) ) {
1218 + $this->check_access( $course_id );
1219 +
1220 + if ( tutor()->course_post_type !== get_post_type( $course_id ) ) {
1330 1221 $errors['course_id'] = __( 'Invalid course id', 'tutor' );
1331 1222 }
1332 1223
1333 - $this->check_access( $course_id );
1334 -
1335 1224 if ( ! empty( $errors ) ) {
1336 1225 $this->json_response( __( 'Invalid input', 'tutor' ), $errors, HttpHelper::STATUS_UNPROCESSABLE_ENTITY );
1337 1226 }
1338 1227
@@ -1343,9 +1232,9 @@
1343 1232 $price = 0;
1344 1233 $sale_price = 0;
1345 1234 $product_id = tutor_utils()->get_course_product_id( $course_id );
1346 1235
1347 - if ( WooCommerce::MONETIZE_BY === $monetize_by ) {
1236 + if ( 'wc' === $monetize_by ) {
1348 1237 $product = wc_get_product( $product_id );
1349 1238 if ( $product ) {
1350 1239 $product_name = $product->get_name();
1351 1240 $price = $product->get_regular_price();
@@ -1352,9 +1241,9 @@
1352 1241 $sale_price = $product->get_sale_price();
1353 1242 }
1354 1243 }
1355 1244
1356 - if ( Ecommerce::MONETIZE_BY === $monetize_by ) {
1245 + if ( 'tutor' === $monetize_by ) {
1357 1246 $price = get_post_meta( $course_id, self::COURSE_PRICE_META, true );
1358 1247 $sale_price = get_post_meta( $course_id, self::COURSE_SALE_PRICE_META, true );
1359 1248 }
1360 1249
@@ -1369,12 +1258,12 @@
1369 1258 $video_intro = get_post_meta( $course_id, '_video', true );
1370 1259 if ( $video_intro ) {
1371 1260 $source = $video_intro['source'] ?? '';
1372 1261 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;
1262 + $poster_url = wp_get_attachment_url( $video['poster'] ?? 0 );
1263 + $source_html5 = wp_get_attachment_url( $video['source_video_id'] ?? 0 );
1264 + $video['poster_url'] = $poster_url;
1265 + $video['source_html5'] = $source_html5;
1377 1266 }
1378 1267 }
1379 1268
1380 1269 $course = get_post( $course_id, ARRAY_A );
@@ -1383,23 +1272,13 @@
1383 1272 }
1384 1273
1385 1274 $editors = tutor_utils()->get_editor_list( $course_id );
1386 1275
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 1276 $data = array(
1398 1277 'editors' => array_values( $editors ),
1399 1278 'editor_used' => tutor_utils()->get_editor_used( $course_id ),
1400 1279 'preview_link' => get_preview_post_link( $course_id ),
1401 - 'post_author' => tutor_utils()->get_tutor_user( $post_author ),
1280 + 'post_author' => tutor_utils()->get_tutor_user( $course['post_author'] ),
1402 1281 'course_categories' => wp_get_post_terms( $course_id, CourseModel::COURSE_CATEGORY ),
1403 1282 'course_tags' => wp_get_post_terms( $course_id, CourseModel::COURSE_TAG ),
1404 1283 'thumbnail_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
1405 1284 'thumbnail' => get_the_post_thumbnail_url( $course_id ),
@@ -1423,16 +1302,8 @@
1423 1302 'certificate' => false,
1424 1303 ),
1425 1304 );
1426 1305
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 1306 $data = apply_filters( 'tutor_course_details_response', array_merge( $course, $data ) );
1436 1307
1437 1308 $this->json_response( __( 'Data retrieved successfully!', 'tutor' ), $data );
1438 1309 }
@@ -1497,10 +1368,9 @@
1497 1368 wp_enqueue_script( 'mce-view' );
1498 1369 wp_enqueue_editor();
1499 1370
1500 1371 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/' );
1372 + wp_enqueue_script( 'tutor-course-builder', tutor()->url . 'assets/js/tutor-course-builder.min.js', array( 'wp-date', 'wp-i18n', 'wp-element', 'wp-api' ), TUTOR_VERSION, true );
1503 1373
1504 1374 wp_localize_script(
1505 1375 'mce-view',
1506 1376 'mceViewL10n',
@@ -1539,9 +1409,9 @@
1539 1409 $default_data = ( new Assets( false ) )->get_default_localized_data();
1540 1410
1541 1411 if ( isset( $default_data['current_user']['data']['id'] ) ) {
1542 1412 $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 : '';
1413 + $default_data['current_user']['data']['tutor_profile_photo_url'] = $tutor_user->tutor_profile_photo_url;
1544 1414 }
1545 1415
1546 1416 /**
1547 1417 * Localized only options to protect sensitive info like API keys.
@@ -1546,9 +1416,8 @@
1546 1416 /**
1547 1417 * Localized only options to protect sensitive info like API keys.
1548 1418 */
1549 1419 $required_options = array(
1550 - 'learning_mode',
1551 1420 'monetize_by',
1552 1421 'enable_course_marketplace',
1553 1422 'course_permalink_base',
1554 1423 'supported_video_sources',
@@ -1568,12 +1437,8 @@
1568 1437 $settings['course_builder_logo_url'] = wp_get_attachment_image_url( $full_settings['tutor_frontend_course_page_logo_id'] ?? 0, 'full' );
1569 1438 $settings['chatgpt_key_exist'] = tutor()->has_pro && ! empty( $full_settings['chatgpt_api_key'] ?? '' );
1570 1439 $settings['youtube_api_key_exist'] = ! empty( $full_settings['lesson_video_duration_youtube_api_key'] ?? '' );
1571 1440
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 1441 $new_data = array( 'settings' => $settings );
1577 1442
1578 1443 $data = array_merge( $default_data, $new_data );
1579 1444
@@ -1619,15 +1484,10 @@
1619 1484 $data['timezones'] = tutor_global_timezone_lists();
1620 1485 $data['difficulty_levels'] = $difficulty_levels;
1621 1486 $data['supported_video_sources'] = $supported_video_sources;
1622 1487 $data['wp_rest_nonce'] = wp_create_nonce( 'wp_rest' );
1488 + $data['max_upload_size'] = size_format( wp_max_upload_size() );
1623 1489
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 1490 $data = apply_filters( 'tutor_course_builder_localized_data', $data );
1631 1491
1632 1492 return $data;
1633 1493 }
@@ -1750,38 +1610,23 @@
1750 1610 /**
1751 1611 * Update course content order
1752 1612 *
1753 1613 * @since 1.0.0
1754 - * @since 3.9.9 Check if user can manage course before updating order.
1755 - *
1756 1614 * @return void
1757 1615 */
1758 - public function ajax_update_course_content_order() {
1616 + public function tutor_update_course_content_order() {
1759 1617 tutor_utils()->checking_nonce();
1760 1618
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 1619 if ( Input::has( 'content_parent' ) ) {
1780 1620 $content_parent = Input::post( 'content_parent', array(), Input::TYPE_ARRAY );
1781 1621 $topic_id = tutor_utils()->array_get( 'parent_topic_id', $content_parent );
1782 1622 $content_id = tutor_utils()->array_get( 'content_id', $content_parent );
1783 1623
1624 + if ( ! tutor_utils()->can_user_manage( 'topic', $topic_id ) ) {
1625 + wp_send_json_success( array( 'message' => __( 'Access Denied!', 'tutor' ) ) );
1626 + exit;
1627 + }
1628 +
1784 1629 // Update the parent topic id of the content.
1785 1630 global $wpdb;
1786 1631 $wpdb->update( $wpdb->posts, array( 'post_parent' => $topic_id ), array( 'ID' => $content_id ) );
1787 1632 }
@@ -1786,11 +1631,11 @@
1786 1631 $wpdb->update( $wpdb->posts, array( 'post_parent' => $topic_id ), array( 'ID' => $content_id ) );
1787 1632 }
1788 1633
1789 1634 // Save course content order.
1790 - $this->save_course_content_order( $sorting_order );
1635 + $this->save_course_content_order();
1791 1636
1792 - $this->response_success( __( 'Course content order updated successfully!', 'tutor' ) );
1637 + wp_send_json_success();
1793 1638 }
1794 1639
1795 1640 /**
1796 1641 * Restrict new student entry
@@ -1815,11 +1660,9 @@
1815 1660 /**
1816 1661 * Restrict media
1817 1662 *
1818 1663 * @since 1.0.0
1819 - *
1820 1664 * @param string $where where clause.
1821 - *
1822 1665 * @return string
1823 1666 */
1824 1667 public function restrict_media( $where ) {
1825 1668 $action = Input::post( 'action' );
@@ -1835,58 +1678,58 @@
1835 1678 /**
1836 1679 * Save course content order
1837 1680 *
1838 1681 * @since 1.0.0
1839 - * @since 3.9.8 param $order added.
1840 - *
1841 - * @param array $sort_order the lesson and topic order array.
1842 - *
1843 1682 * @return void
1844 1683 */
1845 - private function save_course_content_order( $sort_order = array() ) {
1684 + private function save_course_content_order() {
1846 1685 global $wpdb;
1847 1686
1848 - if ( ! tutor_utils()->count( $sort_order ) ) {
1849 - return;
1850 - }
1687 + $new_order = Input::post( 'tutor_topics_lessons_sorting' );
1688 + if ( ! empty( $new_order ) ) {
1689 + $order = json_decode( $new_order, true );
1851 1690
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 - );
1691 + if ( is_array( $order ) && count( $order ) ) {
1692 + $i = 0;
1693 + foreach ( $order as $topic ) {
1694 + $i++;
1695 + $wpdb->update(
1696 + $wpdb->posts,
1697 + array( 'menu_order' => $i ),
1698 + array( 'ID' => $topic['topic_id'] )
1699 + );
1860 1700
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 - );
1701 + /**
1702 + * Removing All lesson with topic
1703 + */
1869 1704
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 1705 $wpdb->update(
1882 1706 $wpdb->posts,
1883 - array(
1884 - 'post_parent' => $topic['topic_id'],
1885 - 'menu_order' => $lesson_key,
1886 - ),
1887 - array( 'ID' => $lesson_id )
1707 + array( 'post_parent' => 0 ),
1708 + array( 'post_parent' => $topic['topic_id'] )
1888 1709 );
1710 +
1711 + /**
1712 + * Lesson Attaching with topic ID
1713 + * Sorting lesson
1714 + */
1715 + if ( isset( $topic['lesson_ids'] ) ) {
1716 + $lesson_ids = $topic['lesson_ids'];
1717 + } else {
1718 + $lesson_ids = array();
1719 + }
1720 + if ( count( $lesson_ids ) ) {
1721 + foreach ( $lesson_ids as $lesson_key => $lesson_id ) {
1722 + $wpdb->update(
1723 + $wpdb->posts,
1724 + array(
1725 + 'post_parent' => $topic['topic_id'],
1726 + 'menu_order' => $lesson_key,
1727 + ),
1728 + array( 'ID' => $lesson_id )
1729 + );
1730 + }
1731 + }
1889 1732 }
1890 1733 }
1891 1734 }
1892 1735 }
@@ -1957,14 +1800,12 @@
1957 1800 }
1958 1801 //phpcs:enable WordPress.Security.NonceVerification.Missing
1959 1802 }
1960 1803
1961 - $sorting_order = Input::post( 'tutor_topics_lessons_sorting', '' );
1962 - $sorting_order = json_decode( $sorting_order, true ) ?? array();
1963 1804 /**
1964 1805 * Sorting Topics and lesson
1965 1806 */
1966 - $this->save_course_content_order( $sorting_order );
1807 + $this->save_course_content_order();
1967 1808
1968 1809 // Additional data like course intro video.
1969 1810 if ( $additional_data_edit ) {
1970 1811 // Sanitize data through helper method.
@@ -1975,9 +1816,8 @@
1975 1816 'source_embedded' => 'wp_kses_post',
1976 1817 ),
1977 1818 true
1978 1819 );
1979 - $video = tutor_utils()->filter_video_meta( $video );
1980 1820 $video_source = tutor_utils()->array_get( 'source', $video );
1981 1821 if ( -1 !== $video_source ) {
1982 1822 update_post_meta( $post_ID, '_video', $video );
1983 1823 } elseif ( ! tutor_is_rest() ) {
@@ -1987,23 +1827,12 @@
1987 1827
1988 1828 /**
1989 1829 * Adding author to instructor automatically
1990 1830 */
1991 - $requested_author_id = Input::post( 'post_author_override', 0, Input::TYPE_INT );
1992 1831
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(
1832 + // Override post author id.
1833 + $author_id = isset( $_POST['post_author_override'] ) ? $_POST['post_author_override'] : $post->post_author; //phpcs:ignore
1834 + $attached = (int) $wpdb->get_var(
2006 1835 $wpdb->prepare(
2007 1836 "SELECT COUNT(umeta_id) FROM {$wpdb->usermeta}
2008 1837 WHERE user_id = %d
2009 1838 AND meta_key = '_tutor_instructor_course_id'
@@ -2028,20 +1857,8 @@
2028 1857 update_post_meta( $post_ID, $key, ( isset( $_POST[ $key ] ) ? 'yes' : 'no' ) );
2029 1858 }
2030 1859 }
2031 1860
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 1861 do_action( 'tutor_save_course_after', $post_ID, $post );
2045 1862 }
2046 1863
2047 1864 /**
@@ -2183,14 +2000,8 @@
2183 2000 */
2184 2001
2185 2002 $is_purchasable = tutor_utils()->is_course_purchasable( $course_id );
2186 2003
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 2004 /**
2194 2005 * If is is not purchasable, it's free, and enroll right now
2195 2006 * If purchasable, then process purchase.
2196 2007 *
@@ -2200,9 +2011,9 @@
2200 2011 // Process purchase.
2201 2012
2202 2013 } else {
2203 2014 // Free enroll.
2204 - EnrollmentModel::do_enroll( $course_id );
2015 + tutor_utils()->do_enroll( $course_id );
2205 2016 }
2206 2017
2207 2018 $referer_url = wp_get_referer();
2208 2019 wp_safe_redirect( tutor_utils()->get_nocache_url( $referer_url ) );
@@ -2212,11 +2023,8 @@
2212 2023 /**
2213 2024 * Mark complete completed
2214 2025 *
2215 2026 * @since 1.0.0
2216 - *
2217 - * @since 3.7.1 Filter hook: tutor_user_can_complete_course added
2218 - *
2219 2027 * @return void
2220 2028 */
2221 2029 public function mark_course_complete() {
2222 2030 $tutor_action = Input::post( 'tutor_action' );
@@ -2224,10 +2032,8 @@
2224 2032 if ( 'tutor_complete_course' !== $tutor_action || ! $course_id ) {
2225 2033 return;
2226 2034 }
2227 2035
2228 - $permalink = get_the_permalink( $course_id );
2229 -
2230 2036 // Checking nonce.
2231 2037 tutor_utils()->checking_nonce();
2232 2038
2233 2039 $user_id = get_current_user_id();
@@ -2236,78 +2042,20 @@
2236 2042 if ( ! $user_id ) {
2237 2043 die( esc_html__( 'Please Sign-In', 'tutor' ) );
2238 2044 }
2239 2045
2240 - if ( ! EnrollmentModel::is_enrolled( $course_id, $user_id ) ) {
2241 - die( esc_html__( 'User is not enrolled in course', 'tutor' ) );
2242 - }
2046 + CourseModel::mark_course_as_completed( $course_id, $user_id );
2243 2047
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 );
2048 + $permalink = get_the_permalink( $course_id );
2250 2049
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 );
2050 + // Set temporary identifier to show review pop up.
2051 + self::set_review_popup_data( $user_id, $course_id, $permalink );
2259 2052
2260 - wp_safe_redirect( $permalink );
2261 - exit;
2262 - }
2053 + wp_safe_redirect( $permalink );
2054 + exit;
2263 2055 }
2264 2056
2265 2057 /**
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 2058 * Set data for review popup.
2311 2059 *
2312 2060 * @since 2.2.5
2313 2061 * @since 2.4.0 removed $permalink param. store user meta instead of option data.
@@ -2333,34 +2081,18 @@
2333 2081 * @since 1.0.0
2334 2082 * @return void
2335 2083 */
2336 2084 public function popup_review_form() {
2337 - if ( ! is_user_logged_in() ) {
2338 - return;
2339 - }
2085 + if ( is_user_logged_in() ) {
2086 + $user_id = get_current_user_id();
2087 + $course_id = get_the_ID();
2088 + $meta_key = User::get_review_popup_meta( $course_id );
2089 + $review_course_id = (int) get_user_meta( $user_id, $meta_key, true );
2340 2090
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;
2091 + if ( is_single() && $course_id === $review_course_id ) {
2092 + include tutor()->path . 'views/modal/review.php';
2093 + }
2345 2094 }
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 2095 }
2364 2096
2365 2097 /**
2366 2098 * Review popup data clear
@@ -2388,9 +2120,8 @@
2388 2120 /**
2389 2121 * Delete course delete from frontend dashboard
2390 2122 *
2391 2123 * @since 2.0.0
2392 - *
2393 2124 * @return void
2394 2125 */
2395 2126 public function tutor_delete_dashboard_course() {
2396 2127 tutor_utils()->checking_nonce();
@@ -2409,9 +2140,9 @@
2409 2140 wp_send_json_error( array( 'message' => __( 'Only main instructor can delete this course', 'tutor' ) ) );
2410 2141 }
2411 2142
2412 2143 // Check if user is only an instructor.
2413 - if ( ! User::is_admin() ) {
2144 + if ( ! current_user_can( 'administrator' ) ) {
2414 2145 // Check if instructor can trash course.
2415 2146 $can_trash_post = tutor_utils()->get_option( 'instructor_can_delete_course' );
2416 2147
2417 2148 if ( ! $can_trash_post ) {
@@ -2421,9 +2152,9 @@
2421 2152
2422 2153 $trash_course = wp_update_post(
2423 2154 array(
2424 2155 'ID' => $course_id,
2425 - 'post_status' => CourseModel::STATUS_TRASH,
2156 + 'post_status' => 'trash',
2426 2157 )
2427 2158 );
2428 2159
2429 2160 if ( $trash_course ) {
@@ -2482,24 +2213,21 @@
2482 2213 *
2483 2214 * @return void
2484 2215 */
2485 2216 public function attach_product_with_course( $post_ID, $post_data ) {
2217 +
2486 2218 $monetize_by = tutor_utils()->get_option( 'monetize_by' );
2487 2219 $product_id = Input::post( '_tutor_course_product_id', 0, Input::TYPE_INT );
2488 2220
2489 - /**
2490 - * For native monetization, just return
2491 - * No need to attach anything.
2492 - */
2493 2221 if ( Ecommerce::MONETIZE_BY === $monetize_by ) {
2494 2222 return;
2495 2223 }
2496 2224
2497 2225 /**
2498 - * When course moved paid to free
2499 - * Keep the product linked and return.
2226 + * Unlink product from course.
2500 2227 */
2501 2228 if ( -1 === $product_id ) {
2229 + delete_post_meta( $post_ID, self::COURSE_PRODUCT_ID_META );
2502 2230 return;
2503 2231 }
2504 2232
2505 2233 /**
@@ -2526,31 +2254,36 @@
2526 2254 update_post_meta( $post_ID, self::COURSE_PRICE_TYPE_META, self::PRICE_TYPE_PAID );
2527 2255
2528 2256 if ( 'wc' === $monetize_by ) {
2529 2257
2530 - $is_update = ( $product_id && wc_get_product( $product_id ) ) ? true : false;
2258 + $is_update = ( $attached_product_id && wc_get_product( $attached_product_id ) ) ? true : false;
2531 2259
2532 2260 if ( $is_update ) {
2261 + $attached_product_id = $product_id;
2533 2262 update_post_meta( $post_ID, self::COURSE_PRODUCT_ID_META, $product_id );
2534 2263
2535 - $product_id = self::create_wc_product( $course->post_title, $course_price, $sale_price, $product_id );
2264 + $product_id = self::create_wc_product( $course->post_title, $course_price, $sale_price, $attached_product_id );
2536 2265 $product_obj = wc_get_product( $product_id );
2537 2266 if ( $product_obj->is_type( 'subscription' ) ) {
2538 - update_post_meta( $product_id, '_subscription_price', $course_price );
2267 + update_post_meta( $attached_product_id, '_subscription_price', $course_price );
2539 2268 }
2540 2269
2541 2270 // 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() );
2271 + update_post_meta( $post_ID, self::COURSE_PRICE_META, $product_obj->get_regular_price() );
2272 + update_post_meta( $post_ID, self::COURSE_SALE_PRICE_META, $product_obj->get_sale_price() );
2543 2273 } else {
2544 2274 // Create new WC product name with course title.
2545 2275 $product_id = self::create_wc_product( $course->post_title, $course_price, $sale_price );
2546 2276 if ( $product_id ) {
2547 2277 $product_obj = wc_get_product( $product_id );
2278 + update_post_meta( $post_ID, self::COURSE_PRODUCT_ID_META, $product_id );
2279 + // Mark product for woocommerce.
2280 + update_post_meta( $product_id, '_virtual', 'yes' );
2281 + update_post_meta( $product_id, '_tutor_product', 'yes' );
2548 2282
2549 - self::sync_course_with_wc_product( $post_ID, $product_id );
2550 -
2551 2283 // 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() );
2284 + update_post_meta( $post_ID, self::COURSE_PRICE_META, $product_obj->get_regular_price() );
2285 + update_post_meta( $post_ID, self::COURSE_SALE_PRICE_META, $product_obj->get_sale_price() );
2553 2286 }
2554 2287 }
2555 2288
2556 2289 $course_post_thumbnail = Input::post( 'thumbnail_id', 0, Input::TYPE_INT );
@@ -2598,8 +2331,33 @@
2598 2331 }
2599 2332 }
2600 2333
2601 2334 /**
2335 + * Add Course level to course settings
2336 + *
2337 + * @since 1.4.1
2338 + *
2339 + * @param array $args arguments.
2340 + * @return array
2341 + */
2342 + public function add_course_level_to_settings( $args ) {
2343 + $course_id = get_the_ID();
2344 + $levels = tutor_utils()->course_levels();
2345 + $course_level = get_post_meta( $course_id, '_tutor_course_level', true );
2346 +
2347 + $args['general']['fields']['_tutor_course_level'] = array(
2348 + 'type' => 'select',
2349 + 'label' => __( 'Difficulty Level', 'tutor' ),
2350 + 'label_title' => __( 'Enable', 'tutor' ),
2351 + 'options' => $levels,
2352 + 'value' => $course_level ? $course_level : 'intermediate',
2353 + 'desc' => __( 'Course difficulty level', 'tutor' ),
2354 + );
2355 +
2356 + return $args;
2357 + }
2358 +
2359 + /**
2602 2360 * Check if course starting
2603 2361 *
2604 2362 * @since 1.4.8
2605 2363 * @return void
@@ -2619,9 +2377,8 @@
2619 2377 /**
2620 2378 * Add Course level to course settings
2621 2379 *
2622 2380 * @since 1.4.8
2623 - *
2624 2381 * @return void
2625 2382 */
2626 2383 public function course_elements_enable_disable() {
2627 2384 add_filter( 'tutor_course/single/completing-progress-bar', array( $this, 'enable_disable_course_progress_bar' ) );
@@ -2654,13 +2411,12 @@
2654 2411 *
2655 2412 * @since 1.4.8
2656 2413 *
2657 2414 * @param string $html HTML string.
2658 - *
2659 2415 * @return string
2660 2416 */
2661 2417 public function enable_disable_material_includes( $html ) {
2662 - $disable_option = ! (bool) get_tutor_option( 'enable_course_material', true );
2418 + $disable_option = ! (bool) get_tutor_option( 'enable_course_material', true, true );
2663 2419 if ( $disable_option ) {
2664 2420 return '';
2665 2421 }
2666 2422 return $html;
@@ -2671,9 +2427,8 @@
2671 2427 *
2672 2428 * @since 1.4.8
2673 2429 *
2674 2430 * @param string $html HTML string.
2675 - *
2676 2431 * @return string
2677 2432 */
2678 2433 public function enable_disable_course_content( $html ) {
2679 2434 $disable_option = ! (bool) tutor_utils()->get_option( 'enable_course_description', true, true );
@@ -2688,9 +2443,8 @@
2688 2443 *
2689 2444 * @since 1.4.8
2690 2445 *
2691 2446 * @param string $html HTML string.
2692 - *
2693 2447 * @return string
2694 2448 */
2695 2449 public function enable_disable_course_benefits( $html ) {
2696 2450 $disable_option = ! (bool) tutor_utils()->get_option( 'enable_course_benefits', true, true );
@@ -2705,9 +2459,8 @@
2705 2459 *
2706 2460 * @since 1.4.8
2707 2461 *
2708 2462 * @param string $html HTML string.
2709 - *
2710 2463 * @return string
2711 2464 */
2712 2465 public function enable_disable_course_requirements( $html ) {
2713 2466 $disable_option = ! (bool) tutor_utils()->get_option( 'enable_course_requirements', true, true );
@@ -2722,9 +2475,8 @@
2722 2475 *
2723 2476 * @since 1.4.8
2724 2477 *
2725 2478 * @param string $html HTML string.
2726 - *
2727 2479 * @return string
2728 2480 */
2729 2481 public function enable_disable_course_target_audience( $html ) {
2730 2482 $disable_option = ! (bool) tutor_utils()->get_option( 'enable_course_target_audience', true, true );
@@ -2769,9 +2521,9 @@
2769 2521 unset( $items['reviews'] );
2770 2522 }
2771 2523
2772 2524 // Whether enrollment require.
2773 - $is_enrolled = EnrollmentModel::is_enrolled();
2525 + $is_enrolled = tutor_utils()->is_enrolled();
2774 2526
2775 2527 return array_filter(
2776 2528 $items,
2777 2529 function ( $item ) use ( $is_enrolled ) {
@@ -2786,9 +2538,8 @@
2786 2538 /**
2787 2539 * Filter product in shop page
2788 2540 *
2789 2541 * @since 1.4.9
2790 - *
2791 2542 * @return void|null
2792 2543 */
2793 2544 public function filter_product_in_shop_page() {
2794 2545 $hide_course_from_shop_page = (bool) get_tutor_option( 'hide_course_from_shop_page' );
@@ -2794,11 +2545,11 @@
2794 2545 $hide_course_from_shop_page = (bool) get_tutor_option( 'hide_course_from_shop_page' );
2795 2546 if ( ! $hide_course_from_shop_page ) {
2796 2547 return;
2797 2548 }
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 );
2549 + add_action( 'woocommerce_product_query', array( $this, 'filter_woocommerce_product_query' ) );
2550 + add_filter( 'edd_downloads_query', array( $this, 'filter_edd_downloads_query' ), 10, 2 );
2551 + add_action( 'pre_get_posts', array( $this, 'filter_archive_meta_query' ), 1 );
2801 2552 }
2802 2553
2803 2554
2804 2555 /**
@@ -2804,9 +2555,8 @@
2804 2555 /**
2805 2556 * Tutor product meta query
2806 2557 *
2807 2558 * @since 1.4.9
2808 - *
2809 2559 * @return array
2810 2560 */
2811 2561 public function tutor_product_meta_query() {
2812 2562 $meta_query = array(
@@ -2821,9 +2571,8 @@
2821 2571 *
2822 2572 * @since 1.4.9
2823 2573 *
2824 2574 * @param \WP_Query $wp_query WP Query instance.
2825 - *
2826 2575 * @return \WP_Query
2827 2576 */
2828 2577 public function filter_woocommerce_product_query( $wp_query ) {
2829 2578 $product_ids = $this->get_connected_wc_product_ids();
@@ -2865,9 +2614,8 @@
2865 2614 *
2866 2615 * @since 1.4.9
2867 2616 *
2868 2617 * @param \WP_Query $query WP Query instance.
2869 - *
2870 2618 * @return \WP_Query
2871 2619 */
2872 2620 public function filter_edd_downloads_query( $query ) {
2873 2621 $query['meta_query'][] = $this->tutor_product_meta_query();
@@ -2879,9 +2627,8 @@
2879 2627 *
2880 2628 * @since 1.4.9
2881 2629 *
2882 2630 * @param \WP_Query $wp_query WP Query instance.
2883 - *
2884 2631 * @return \WP_Query
2885 2632 */
2886 2633 public function filter_archive_meta_query( $wp_query ) {
2887 2634 if ( ! is_admin() && $wp_query->is_archive && $wp_query->get( 'post_type' ) === 'download' ) {
@@ -2895,9 +2642,8 @@
2895 2642 *
2896 2643 * @since 1.5.8
2897 2644 *
2898 2645 * @param string $html HTML string.
2899 - *
2900 2646 * @return string
2901 2647 */
2902 2648 public function remove_price_if_enrolled( $html ) {
2903 2649 $should_removed = apply_filters( 'should_remove_price_if_enrolled', true );
@@ -2903,9 +2649,9 @@
2903 2649 $should_removed = apply_filters( 'should_remove_price_if_enrolled', true );
2904 2650
2905 2651 if ( $should_removed ) {
2906 2652 $course_id = get_the_ID();
2907 - $enrolled = EnrollmentModel::is_enrolled( $course_id );
2653 + $enrolled = tutor_utils()->is_enrolled( $course_id );
2908 2654 if ( $enrolled ) {
2909 2655 $html = '';
2910 2656 }
2911 2657 }
@@ -2912,119 +2658,112 @@
2912 2658 return $html;
2913 2659 }
2914 2660
2915 2661 /**
2916 - * Get course completion missing requirements message.
2662 + * Check if all lessons and quizzes done before mark course complete.
2917 2663 *
2918 - * @since 4.0.0
2664 + * @since 1.5.8
2919 2665 *
2920 - * @param int $course_id Course ID.
2921 - * @param int $user_id User ID.
2922 - *
2923 - * @return string|null
2666 + * @param string $html HTML string.
2667 + * @return string
2924 2668 */
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 );
2669 + public function tutor_lms_hide_course_complete_btn( $html ) {
2928 2670
2929 - if ( 'strict' !== tutor_utils()->get_option( 'course_completion_process' ) ) {
2930 - return null;
2671 + $completion_mode = tutor_utils()->get_option( 'course_completion_process' );
2672 + if ( 'strict' !== $completion_mode ) {
2673 + return $html;
2931 2674 }
2932 2675
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
2676 + $completed_lesson = tutor_utils()->get_completed_lesson_count_by_course();
2677 + $lesson_count = tutor_utils()->get_lesson_count_by_course();
2935 2678
2936 - if ( $completed_lessons < $total_lessons ) {
2937 - return __( 'Complete all lessons to mark this course as complete', 'tutor' );
2679 + if ( $completed_lesson < $lesson_count ) {
2680 + return '<div class="tutor-alert tutor-warning tutor-mt-28">
2681 + <div class="tutor-alert-text">
2682 + <span class="tutor-alert-icon tutor-fs-4 tutor-icon-circle-info tutor-mr-12"></span>
2683 + <span>' . __( 'Complete all lessons to mark this course as complete', 'tutor' ) . '</span>
2684 + </div>
2685 + </div>';
2938 2686 }
2939 2687
2940 - $course_contents = tutor_utils()->get_course_contents_by_id( $course_id );
2688 + $quizzes = array();
2689 + $assignments = array();
2941 2690
2942 - $required_quiz_pass_count = 0;
2943 - $required_assignment_pass_count = 0;
2691 + $course_contents = tutor_utils()->get_course_contents_by_id();
2692 + if ( tutor_utils()->count( $course_contents ) ) {
2693 + foreach ( $course_contents as $content ) {
2694 + if ( 'tutor_quiz' === $content->post_type ) {
2695 + $quizzes[] = $content;
2696 + }
2697 + if ( 'tutor_assignments' === $content->post_type ) {
2698 + $assignments[] = $content;
2699 + }
2700 + }
2701 + }
2944 2702
2945 - $is_assignment_addon_enabled = tutor_utils()->is_addon_enabled( 'tutor-assignments' );
2703 + $required_assignment_pass = 0;
2946 2704
2947 - foreach ( $course_contents as $content ) {
2705 + foreach ( $assignments as $row ) {
2948 2706
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 - }
2707 + $submitted_assignment = tutor_utils()->is_assignment_submitted( $row->ID );
2708 + $is_reviewed_by_instructor = null === $submitted_assignment
2709 + ? false
2710 + : get_comment_meta( $submitted_assignment->comment_ID, 'evaluate_time', true );
2954 2711
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;
2712 + if ( $submitted_assignment && $is_reviewed_by_instructor ) {
2713 + $pass_mark = tutor_utils()->get_assignment_option( $submitted_assignment->comment_post_ID, 'pass_mark' );
2714 + $given_mark = get_comment_meta( $submitted_assignment->comment_ID, 'assignment_mark', true );
2715 +
2716 + if ( $given_mark < $pass_mark ) {
2717 + $required_assignment_pass++;
2958 2718 }
2719 + } else {
2720 + $required_assignment_pass++;
2959 2721 }
2960 2722 }
2961 2723
2962 - if ( ! $required_quiz_pass_count && ! $required_assignment_pass_count ) {
2963 - return null;
2964 - }
2724 + $is_quiz_pass = true;
2725 + $required_quiz_pass = 0;
2965 2726
2966 - return self::get_course_completion_requirement_message(
2967 - $required_quiz_pass_count,
2968 - $required_assignment_pass_count
2969 - );
2970 - }
2727 + if ( tutor_utils()->count( $quizzes ) ) {
2728 + foreach ( $quizzes as $quiz ) {
2971 2729
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' );
2730 + $attempt = tutor_utils()->get_quiz_attempt( $quiz->ID );
2731 + if ( $attempt ) {
2732 + $passing_grade = tutor_utils()->get_quiz_option( $quiz->ID, 'passing_grade', 0 );
2733 + $earned_percentage = $attempt->earned_marks > 0 ? ( number_format( ( $attempt->earned_marks * 100 ) / $attempt->total_marks ) ) : 0;
2985 2734
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 - );
2735 + if ( $earned_percentage < $passing_grade ) {
2736 + $required_quiz_pass++;
2737 + $is_quiz_pass = false;
2738 + }
2739 + } else {
2740 + $required_quiz_pass++;
2741 + $is_quiz_pass = false;
2742 + }
2743 + }
2993 2744 }
2994 2745
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 - }
2746 + if ( ! $is_quiz_pass || $required_assignment_pass > 0 ) {
2747 + $_msg = '';
2748 + $quiz_str = _n( 'quiz', 'quizzes', $required_quiz_pass, 'tutor' );
2749 + $assignment_str = _n( 'assignment', 'assignments', $required_assignment_pass, 'tutor' );
3003 2750
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 - }
2751 + if ( ! $is_quiz_pass && 0 == $required_assignment_pass ) {
2752 + /* translators: %1$s: number of quiz/assignment pass required; %2$s: quiz/assignment string */
2753 + $_msg = sprintf( __( 'You have to pass %1$s %2$s to complete this course.', 'tutor' ), $required_quiz_pass, $quiz_str );
2754 + }
3013 2755
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();
2756 + if ( $is_quiz_pass && $required_assignment_pass > 0 ) {
2757 + //phpcs:ignore
2758 + $_msg = sprintf( __( 'You have to pass %1$s %2$s to complete this course.', 'tutor' ), $required_assignment_pass, $assignment_str );
2759 + }
3025 2760
3026 - if ( $_msg ) {
2761 + if ( ! $is_quiz_pass && $required_assignment_pass > 0 ) {
2762 + /* translators: %1$s: number of quiz pass required; %2$s: quiz string; %3$s: number of assignment pass required; %4$s: assignment string */
2763 + $_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 );
2764 + }
2765 +
3027 2766 return '<div class="tutor-alert tutor-warning tutor-mt-28">
3028 2767 <div class="tutor-alert-text">
3029 2768 <span class="tutor-alert-icon tutor-fs-4 tutor-icon-circle-info tutor-mr-12"></span>
3030 2769 <span>' . $_msg . '</span>
@@ -3040,9 +2779,8 @@
3040 2779 *
3041 2780 * @since 1.5.8
3042 2781 *
3043 2782 * @param string $html HTML string.
3044 - *
3045 2783 * @return string
3046 2784 */
3047 2785 public function get_generate_greadbook( $html ) {
3048 2786 if ( ! tutor_utils()->is_completed_course() ) {
@@ -3054,9 +2792,8 @@
3054 2792 /**
3055 2793 * Add social share content in header
3056 2794 *
3057 2795 * @since 1.6.3
3058 - *
3059 2796 * @return void
3060 2797 */
3061 2798 public function social_share_content() {
3062 2799 global $wp_query, $post;
@@ -3080,9 +2817,8 @@
3080 2817 *
3081 2818 * @since 1.8.2
3082 2819 *
3083 2820 * @param integer $post_id post ID.
3084 - *
3085 2821 * @return void
3086 2822 */
3087 2823 public function delete_associated_enrollment( $post_id ) {
3088 2824 global $wpdb;
@@ -3093,12 +2829,11 @@
3093 2829 post_id
3094 2830 FROM
3095 2831 {$wpdb->postmeta}
3096 2832 WHERE
3097 - meta_key=%s
2833 + meta_key='_tutor_enrolled_by_order_id'
3098 2834 AND meta_value = %d
3099 2835 ",
3100 - EnrollmentModel::ENROLLMENT_ORDER_ID_META,
3101 2836 $post_id
3102 2837 )
3103 2838 );
3104 2839
@@ -3114,35 +2849,21 @@
3114 2849 /**
3115 2850 * Reset course progress.
3116 2851 *
3117 2852 * @since 1.5.8
3118 - *
3119 2853 * @return void
3120 2854 */
3121 2855 public function tutor_reset_course_progress() {
3122 2856 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 );
2857 + $course_id = Input::post( 'course_id' );
3127 2858
3128 - if ( ! $course_reset_progress && 'learning-area-sidebar' === $context ) {
3129 - $this->response_bad_request( __( 'You do not have permission to reset this course.', 'tutor' ) );
2859 + if ( ! $course_id || ! is_numeric( $course_id ) || ! tutor_utils()->is_enrolled( $course_id ) ) {
2860 + wp_send_json_error( array( 'message' => __( 'Invalid Course ID or Access Denied.', 'tutor' ) ) );
3130 2861 return;
3131 2862 }
3132 2863
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 2864 tutor_utils()->delete_course_progress( $course_id );
3144 - $this->json_response( '', array( 'redirect_to' => tutor_utils()->get_course_first_lesson( $course_id ) ) );
2865 + wp_send_json_success( array( 'redirect_to' => tutor_utils()->get_course_first_lesson( $course_id ) ) );
3145 2866 }
3146 2867
3147 2868 /**
3148 2869 * Do enroll if guest attempt to enroll and course is free
@@ -3150,9 +2871,9 @@
3150 2871 * @since 1.9.8
3151 2872 *
3152 2873 * @param integer $course_id course ID.
3153 2874 * @param integer $user_id user ID.
3154 - *
2875 +
3155 2876 * @return void
3156 2877 */
3157 2878 public function enroll_after_login_if_attempt( int $course_id, int $user_id ) {
3158 2879 $course_id = sanitize_text_field( $course_id );
@@ -3160,9 +2881,9 @@
3160 2881
3161 2882 if ( $course_id && $is_allowed ) {
3162 2883 $is_purchasable = tutor_utils()->is_course_purchasable( $course_id );
3163 2884 if ( ! $is_purchasable ) {
3164 - EnrollmentModel::do_enroll( $course_id, $order_id = 0, $user_id );
2885 + tutor_utils()->do_enroll( $course_id, $order_id = 0, $user_id );
3165 2886 do_action( 'guest_attempt_after_enrollment', $course_id );
3166 2887 }
3167 2888 }
3168 2889 }
@@ -3170,9 +2891,8 @@
3170 2891 /**
3171 2892 * Handle course enrollment
3172 2893 *
3173 2894 * @since 2.1.0
3174 - *
3175 2895 * @return void
3176 2896 */
3177 2897 public function course_enrollment() {
3178 2898 tutor_utils()->checking_nonce();
@@ -3179,46 +2899,21 @@
3179 2899
3180 2900 $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
3181 2901 $user_id = get_current_user_id();
3182 2902
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' ) );
2903 + if ( $course_id ) {
2904 + $password_protected = post_password_required( $course_id );
2905 + if ( $password_protected ) {
2906 + wp_send_json_error( __( 'This course is password protected', 'tutor' ) );
3213 2907 }
3214 - }
3215 -
3216 - $enroll = EnrollmentModel::do_enroll( $course_id, 0, $user_id );
3217 - if ( $enroll ) {
3218 - wp_send_json_success( __( 'Enrollment successfully done!', 'tutor' ) );
2908 + $enroll = tutor_utils()->do_enroll( $course_id, 0, $user_id );
2909 + if ( $enroll ) {
2910 + wp_send_json_success( __( 'Enrollment successfully done!', 'tutor' ) );
2911 + } else {
2912 + wp_send_json_error( __( 'Enrollment failed, please try again!', 'tutor' ) );
2913 + }
3219 2914 } else {
3220 - wp_send_json_error( __( 'Enrollment failed, please try again!', 'tutor' ) );
2915 + wp_send_json_error( __( 'Invalid course ID', 'tutor' ) );
3221 2916 }
3222 2917 }
3223 2918
3224 2919 /**
@@ -3305,10 +3000,14 @@
3305 3000 'regular_price' => $regular_price,
3306 3001 'sale_price' => $sale_price,
3307 3002 );
3308 3003
3309 - $card_data = apply_filters( 'tutor_course_mini_info', $info, $post );
3004 + if ( 'course-bundle' === $post->post_type && tutor_utils()->is_addon_enabled( 'tutor-pro/addons/course-bundle/course-bundle.php' ) ) {
3005 + $info['total_course'] = count( BundleModel::get_bundle_course_ids( $post->ID ) );
3006 + }
3310 3007
3008 + $card_data = apply_filters( 'tutor_add_course_plan_info', $info, $post );
3009 +
3311 3010 return $card_data;
3312 3011 }
3313 3012
3314 3013 /**
@@ -3329,9 +3028,9 @@
3329 3028 $info['last_updated'] = tutor_i18n_get_formated_date( $post->post_modified_at );
3330 3029 $info['course_duration'] = tutor_utils()->get_course_duration( $post->ID, false );
3331 3030 $info['total_enrolled'] = tutor_utils()->count_enrolled_users_by_course( $post->ID );
3332 3031
3333 - $card_data = apply_filters( 'tutor_course_card_data', $info, $post );
3032 + $card_data = apply_filters( 'tutor_add_course_plan_info', $info, $post );
3334 3033
3335 3034 return $card_data;
3336 3035 }
3337 3036
@@ -3370,217 +3069,6 @@
3370 3069 $args['role__in'] = array_map( 'sanitize_text_field', $filter->role );
3371 3070 }
3372 3071
3373 3072 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 3073 }
3586 3074 }