PluginProbe
Tutor LMS – eLearning and online course solution / 3.7.2
Tutor LMS – eLearning and online course solution v3.7.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 +254 -663 trunk3.7.2 View file →
@@ -9,26 +9,22 @@
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;
22 +use Tutor\Ecommerce\Tax;
26 23 use Tutor\Traits\JsonResponse;
27 24 use Tutor\Helpers\ValidationHelper;
28 -use Tutor\Models\EnrollmentModel;
29 -use Tutor\Options_V2;
30 -use TUTOR_ASSIGNMENTS\Assignments;
25 +use Tutor\Models\QuizModel;
26 +use TutorPro\CourseBundle\Models\BundleModel;
31 27
32 28 /**
33 29 * Course Class
34 30 *
@@ -98,15 +94,8 @@
98 94 const PUBLIC_COURSE_META = '_tutor_is_public_course';
99 95 const COURSE_SETTINGS_META = '_tutor_course_settings';
100 96 const COURSE_LEVEL_META = '_tutor_course_level';
101 97
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 98
110 99 /**
111 100 * Additional course meta info
112 101 *
@@ -160,8 +149,15 @@
160 149 */
161 150 add_action( 'save_tutor_course', array( $this, 'attach_product_with_course' ), 10, 2 );
162 151
163 152 /**
153 + * Add course level to course settings
154 + *
155 + * @since v.1.4.1
156 + */
157 + add_filter( 'tutor_course_settings_tabs', array( $this, 'add_course_level_to_settings' ) );
158 +
159 + /**
164 160 * Enable Disable Course Details Page Feature
165 161 *
166 162 * @since v.1.4.8
167 163 */
@@ -207,14 +203,9 @@
207 203 * Delete course data after deleted course
208 204 *
209 205 * @since v.1.6.6
210 206 */
211 - add_action(
212 - 'deleted_post',
213 - function ( $post_id ) {
214 - ( new CourseModel() )->delete_course_data( $post_id );
215 - }
216 - );
207 + add_action( 'deleted_post', array( new CourseModel(), 'delete_course_data' ) );
217 208
218 209 /**
219 210 * Delete course data after deleted course
220 211 *
@@ -257,9 +248,9 @@
257 248 * @since 1.9.8
258 249 */
259 250 add_action( 'tutor_do_enroll_after_login_if_attempt', array( $this, 'enroll_after_login_if_attempt' ), 10, 2 );
260 251
261 - add_action( 'wp_ajax_tutor_update_course_content_order', array( $this, 'ajax_update_course_content_order' ) );
252 + add_action( 'wp_ajax_tutor_update_course_content_order', array( $this, 'tutor_update_course_content_order' ) );
262 253
263 254 add_action( 'wp_ajax_tutor_get_wc_product', array( $this, 'get_wc_product' ) );
264 255 add_action( 'wp_ajax_tutor_get_wc_products', array( $this, 'get_wc_products' ) );
265 256
@@ -307,13 +298,9 @@
307 298 add_filter( 'tutor_user_list_args', array( $this, 'user_list_args_for_instructor' ) );
308 299
309 300 add_filter( 'template_include', array( $this, 'handle_password_protected' ) );
310 301 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 302 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 303 }
317 304
318 305 /**
319 306 * Handle schedule courses preview for instructors.
@@ -355,31 +342,8 @@
355 342 }
356 343 }
357 344
358 345 /**
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 346 * Handle password protected course/bundle.
383 347 *
384 348 * @since 3.0.0
385 349 *
@@ -917,9 +881,9 @@
917 881
918 882 update_post_meta( $course_id, self::COURSE_PRICE_TYPE_META, self::PRICE_TYPE_FREE );
919 883
920 884 $link = admin_url( 'admin.php?page=create-course' );
921 - if ( tutor()->has_pro && Input::post( 'from_dashboard', false, Input::TYPE_BOOL ) ) {
885 + if ( Input::post( 'from_dashboard', false, Input::TYPE_BOOL ) ) {
922 886 $link = tutor_utils()->tutor_dashboard_url( 'create-course' );
923 887 }
924 888
925 889 $link = add_query_arg( array( 'course_id' => $course_id ), $link );
@@ -937,14 +901,15 @@
937 901 * Get course list
938 902 *
939 903 * @since 3.0.0
940 904 *
941 - * @since 3.2.0 Refactor the arguments & response as per new design.
905 + * @since 3.2.0
942 906 *
907 + * Refactor the arguments & response as per new design
908 + *
943 909 * @return void
944 910 */
945 911 public function ajax_course_list() {
946 - tutor_utils()->check_nonce();
947 912 $this->check_access();
948 913
949 914 $limit = Input::post( 'limit', 10, Input::TYPE_INT );
950 915 $offset = Input::post( 'offset', 0, Input::TYPE_INT );
@@ -964,9 +929,14 @@
964 929 );
965 930
966 931 $exclude = Input::post( 'exclude', array(), Input::TYPE_ARRAY );
967 932 if ( count( $exclude ) ) {
968 - $exclude = array_filter( $exclude, fn( $id ) => is_numeric( $id ) && $id > 0 );
933 + $exclude = array_filter(
934 + $exclude,
935 + function ( $id ) {
936 + return is_numeric( $id );
937 + }
938 + );
969 939 $args['post__not_in'] = $exclude;
970 940 }
971 941
972 942 $courses = CourseModel::get_courses_by_args( $args );
@@ -1102,17 +1072,12 @@
1102 1072 'course_benefits' => 'sanitize_textarea_field',
1103 1073 'course_target_audience' => 'sanitize_textarea_field',
1104 1074 'course_material_includes' => 'sanitize_textarea_field',
1105 1075 'course_requirements' => 'sanitize_textarea_field',
1106 - ),
1107 - true
1076 + )
1108 1077 );
1109 1078
1110 1079 $course_id = (int) $params['course_id'];
1111 - if ( ! $course_id ) {
1112 - $this->response_bad_request( __( 'Invalid course id', 'tutor' ) );
1113 - }
1114 -
1115 1080 $this->check_access( $course_id );
1116 1081
1117 1082 $errors = array();
1118 1083 $validation = $this->validate_inputs( $params );
@@ -1201,32 +1166,15 @@
1201 1166 public function ajax_unlink_page_builder() {
1202 1167 tutor_utils()->check_nonce();
1203 1168
1204 1169 $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 -
1170 + $builder = Input::post( 'builder' );
1209 1171 $this->check_access( $course_id );
1210 1172
1211 - $builder = Input::post( 'builder' );
1212 1173 if ( 'elementor' === $builder ) {
1213 1174 delete_post_meta( $course_id, '_elementor_edit_mode' );
1214 1175 } elseif ( 'droip' === $builder ) {
1215 1176 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 1177 }
1230 1178
1231 1179 $this->json_response(
1232 1180 __( 'Builder unlinked successfully.', 'tutor' ),
@@ -1259,18 +1207,15 @@
1259 1207
1260 1208 $topic_contents = tutor_utils()->get_course_contents_by_topic( $post->ID, -1 );
1261 1209
1262 1210 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 - );
1211 + foreach ( $topic_contents->get_posts() as $post ) {
1212 + if ( tutor()->quiz_post_type === $post->post_type ) {
1213 + $questions = tutor_utils()->get_questions_by_quiz( $post->ID );
1214 + $post->total_question = is_array( $questions ) ? count( $questions ) : 0;
1270 1215 }
1271 1216
1272 - array_push( $current_topic['contents'], $content );
1217 + array_push( $current_topic['contents'], $post );
1273 1218 }
1274 1219 }
1275 1220
1276 1221 $current_topic = apply_filters( 'tutor_filter_course_content', $current_topic );
@@ -1285,23 +1230,20 @@
1285 1230 /**
1286 1231 * Get course contents
1287 1232 *
1288 1233 * @since 3.0.0
1289 - *
1290 - * @return void
1291 1234 */
1292 1235 public function ajax_course_contents() {
1293 1236 tutor_utils()->check_nonce();
1294 1237
1295 - $errors = array();
1296 1238 $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
1297 1239
1298 - if ( ! $course_id || tutor()->course_post_type !== get_post_type( $course_id ) ) {
1240 + $this->check_access( $course_id );
1241 +
1242 + if ( tutor()->course_post_type !== get_post_type( $course_id ) ) {
1299 1243 $errors['course_id'] = __( 'Invalid course id', 'tutor' );
1300 1244 }
1301 1245
1302 - $this->check_access( $course_id );
1303 -
1304 1246 if ( ! empty( $errors ) ) {
1305 1247 $this->json_response( __( 'Invalid input', 'tutor' ), $errors, HttpHelper::STATUS_UNPROCESSABLE_ENTITY );
1306 1248 }
1307 1249
@@ -1325,14 +1267,14 @@
1325 1267
1326 1268 $errors = array();
1327 1269 $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
1328 1270
1329 - if ( ! $course_id || tutor()->course_post_type !== get_post_type( $course_id ) ) {
1271 + $this->check_access( $course_id );
1272 +
1273 + if ( tutor()->course_post_type !== get_post_type( $course_id ) ) {
1330 1274 $errors['course_id'] = __( 'Invalid course id', 'tutor' );
1331 1275 }
1332 1276
1333 - $this->check_access( $course_id );
1334 -
1335 1277 if ( ! empty( $errors ) ) {
1336 1278 $this->json_response( __( 'Invalid input', 'tutor' ), $errors, HttpHelper::STATUS_UNPROCESSABLE_ENTITY );
1337 1279 }
1338 1280
@@ -1343,9 +1285,9 @@
1343 1285 $price = 0;
1344 1286 $sale_price = 0;
1345 1287 $product_id = tutor_utils()->get_course_product_id( $course_id );
1346 1288
1347 - if ( WooCommerce::MONETIZE_BY === $monetize_by ) {
1289 + if ( 'wc' === $monetize_by ) {
1348 1290 $product = wc_get_product( $product_id );
1349 1291 if ( $product ) {
1350 1292 $product_name = $product->get_name();
1351 1293 $price = $product->get_regular_price();
@@ -1352,9 +1294,9 @@
1352 1294 $sale_price = $product->get_sale_price();
1353 1295 }
1354 1296 }
1355 1297
1356 - if ( Ecommerce::MONETIZE_BY === $monetize_by ) {
1298 + if ( 'tutor' === $monetize_by ) {
1357 1299 $price = get_post_meta( $course_id, self::COURSE_PRICE_META, true );
1358 1300 $sale_price = get_post_meta( $course_id, self::COURSE_SALE_PRICE_META, true );
1359 1301 }
1360 1302
@@ -1369,12 +1311,12 @@
1369 1311 $video_intro = get_post_meta( $course_id, '_video', true );
1370 1312 if ( $video_intro ) {
1371 1313 $source = $video_intro['source'] ?? '';
1372 1314 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;
1315 + $poster_url = wp_get_attachment_url( $video['poster'] ?? 0 );
1316 + $source_html5 = wp_get_attachment_url( $video['source_video_id'] ?? 0 );
1317 + $video['poster_url'] = $poster_url;
1318 + $video['source_html5'] = $source_html5;
1377 1319 }
1378 1320 }
1379 1321
1380 1322 $course = get_post( $course_id, ARRAY_A );
@@ -1383,23 +1325,13 @@
1383 1325 }
1384 1326
1385 1327 $editors = tutor_utils()->get_editor_list( $course_id );
1386 1328
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 1329 $data = array(
1398 1330 'editors' => array_values( $editors ),
1399 1331 'editor_used' => tutor_utils()->get_editor_used( $course_id ),
1400 1332 'preview_link' => get_preview_post_link( $course_id ),
1401 - 'post_author' => tutor_utils()->get_tutor_user( $post_author ),
1333 + 'post_author' => tutor_utils()->get_tutor_user( $course['post_author'] ),
1402 1334 'course_categories' => wp_get_post_terms( $course_id, CourseModel::COURSE_CATEGORY ),
1403 1335 'course_tags' => wp_get_post_terms( $course_id, CourseModel::COURSE_TAG ),
1404 1336 'thumbnail_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
1405 1337 'thumbnail' => get_the_post_thumbnail_url( $course_id ),
@@ -1539,9 +1471,9 @@
1539 1471 $default_data = ( new Assets( false ) )->get_default_localized_data();
1540 1472
1541 1473 if ( isset( $default_data['current_user']['data']['id'] ) ) {
1542 1474 $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 : '';
1475 + $default_data['current_user']['data']['tutor_profile_photo_url'] = $tutor_user->tutor_profile_photo_url;
1544 1476 }
1545 1477
1546 1478 /**
1547 1479 * Localized only options to protect sensitive info like API keys.
@@ -1546,9 +1478,8 @@
1546 1478 /**
1547 1479 * Localized only options to protect sensitive info like API keys.
1548 1480 */
1549 1481 $required_options = array(
1550 - 'learning_mode',
1551 1482 'monetize_by',
1552 1483 'enable_course_marketplace',
1553 1484 'course_permalink_base',
1554 1485 'supported_video_sources',
@@ -1750,38 +1681,23 @@
1750 1681 /**
1751 1682 * Update course content order
1752 1683 *
1753 1684 * @since 1.0.0
1754 - * @since 3.9.9 Check if user can manage course before updating order.
1755 - *
1756 1685 * @return void
1757 1686 */
1758 - public function ajax_update_course_content_order() {
1687 + public function tutor_update_course_content_order() {
1759 1688 tutor_utils()->checking_nonce();
1760 1689
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 1690 if ( Input::has( 'content_parent' ) ) {
1780 1691 $content_parent = Input::post( 'content_parent', array(), Input::TYPE_ARRAY );
1781 1692 $topic_id = tutor_utils()->array_get( 'parent_topic_id', $content_parent );
1782 1693 $content_id = tutor_utils()->array_get( 'content_id', $content_parent );
1783 1694
1695 + if ( ! tutor_utils()->can_user_manage( 'topic', $topic_id ) ) {
1696 + wp_send_json_success( array( 'message' => __( 'Access Denied!', 'tutor' ) ) );
1697 + exit;
1698 + }
1699 +
1784 1700 // Update the parent topic id of the content.
1785 1701 global $wpdb;
1786 1702 $wpdb->update( $wpdb->posts, array( 'post_parent' => $topic_id ), array( 'ID' => $content_id ) );
1787 1703 }
@@ -1786,11 +1702,11 @@
1786 1702 $wpdb->update( $wpdb->posts, array( 'post_parent' => $topic_id ), array( 'ID' => $content_id ) );
1787 1703 }
1788 1704
1789 1705 // Save course content order.
1790 - $this->save_course_content_order( $sorting_order );
1706 + $this->save_course_content_order();
1791 1707
1792 - $this->response_success( __( 'Course content order updated successfully!', 'tutor' ) );
1708 + wp_send_json_success();
1793 1709 }
1794 1710
1795 1711 /**
1796 1712 * Restrict new student entry
@@ -1815,11 +1731,9 @@
1815 1731 /**
1816 1732 * Restrict media
1817 1733 *
1818 1734 * @since 1.0.0
1819 - *
1820 1735 * @param string $where where clause.
1821 - *
1822 1736 * @return string
1823 1737 */
1824 1738 public function restrict_media( $where ) {
1825 1739 $action = Input::post( 'action' );
@@ -1835,58 +1749,58 @@
1835 1749 /**
1836 1750 * Save course content order
1837 1751 *
1838 1752 * @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 1753 * @return void
1844 1754 */
1845 - private function save_course_content_order( $sort_order = array() ) {
1755 + private function save_course_content_order() {
1846 1756 global $wpdb;
1847 1757
1848 - if ( ! tutor_utils()->count( $sort_order ) ) {
1849 - return;
1850 - }
1758 + $new_order = Input::post( 'tutor_topics_lessons_sorting' );
1759 + if ( ! empty( $new_order ) ) {
1760 + $order = json_decode( $new_order, true );
1851 1761
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 - );
1762 + if ( is_array( $order ) && count( $order ) ) {
1763 + $i = 0;
1764 + foreach ( $order as $topic ) {
1765 + $i++;
1766 + $wpdb->update(
1767 + $wpdb->posts,
1768 + array( 'menu_order' => $i ),
1769 + array( 'ID' => $topic['topic_id'] )
1770 + );
1860 1771
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 - );
1772 + /**
1773 + * Removing All lesson with topic
1774 + */
1869 1775
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 1776 $wpdb->update(
1882 1777 $wpdb->posts,
1883 - array(
1884 - 'post_parent' => $topic['topic_id'],
1885 - 'menu_order' => $lesson_key,
1886 - ),
1887 - array( 'ID' => $lesson_id )
1778 + array( 'post_parent' => 0 ),
1779 + array( 'post_parent' => $topic['topic_id'] )
1888 1780 );
1781 +
1782 + /**
1783 + * Lesson Attaching with topic ID
1784 + * Sorting lesson
1785 + */
1786 + if ( isset( $topic['lesson_ids'] ) ) {
1787 + $lesson_ids = $topic['lesson_ids'];
1788 + } else {
1789 + $lesson_ids = array();
1790 + }
1791 + if ( count( $lesson_ids ) ) {
1792 + foreach ( $lesson_ids as $lesson_key => $lesson_id ) {
1793 + $wpdb->update(
1794 + $wpdb->posts,
1795 + array(
1796 + 'post_parent' => $topic['topic_id'],
1797 + 'menu_order' => $lesson_key,
1798 + ),
1799 + array( 'ID' => $lesson_id )
1800 + );
1801 + }
1802 + }
1889 1803 }
1890 1804 }
1891 1805 }
1892 1806 }
@@ -1957,14 +1871,12 @@
1957 1871 }
1958 1872 //phpcs:enable WordPress.Security.NonceVerification.Missing
1959 1873 }
1960 1874
1961 - $sorting_order = Input::post( 'tutor_topics_lessons_sorting', '' );
1962 - $sorting_order = json_decode( $sorting_order, true ) ?? array();
1963 1875 /**
1964 1876 * Sorting Topics and lesson
1965 1877 */
1966 - $this->save_course_content_order( $sorting_order );
1878 + $this->save_course_content_order();
1967 1879
1968 1880 // Additional data like course intro video.
1969 1881 if ( $additional_data_edit ) {
1970 1882 // Sanitize data through helper method.
@@ -1975,9 +1887,8 @@
1975 1887 'source_embedded' => 'wp_kses_post',
1976 1888 ),
1977 1889 true
1978 1890 );
1979 - $video = tutor_utils()->filter_video_meta( $video );
1980 1891 $video_source = tutor_utils()->array_get( 'source', $video );
1981 1892 if ( -1 !== $video_source ) {
1982 1893 update_post_meta( $post_ID, '_video', $video );
1983 1894 } elseif ( ! tutor_is_rest() ) {
@@ -1987,23 +1898,12 @@
1987 1898
1988 1899 /**
1989 1900 * Adding author to instructor automatically
1990 1901 */
1991 - $requested_author_id = Input::post( 'post_author_override', 0, Input::TYPE_INT );
1992 1902
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(
1903 + // Override post author id.
1904 + $author_id = isset( $_POST['post_author_override'] ) ? $_POST['post_author_override'] : $post->post_author; //phpcs:ignore
1905 + $attached = (int) $wpdb->get_var(
2006 1906 $wpdb->prepare(
2007 1907 "SELECT COUNT(umeta_id) FROM {$wpdb->usermeta}
2008 1908 WHERE user_id = %d
2009 1909 AND meta_key = '_tutor_instructor_course_id'
@@ -2028,20 +1928,8 @@
2028 1928 update_post_meta( $post_ID, $key, ( isset( $_POST[ $key ] ) ? 'yes' : 'no' ) );
2029 1929 }
2030 1930 }
2031 1931
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 1932 do_action( 'tutor_save_course_after', $post_ID, $post );
2045 1933 }
2046 1934
2047 1935 /**
@@ -2183,14 +2071,8 @@
2183 2071 */
2184 2072
2185 2073 $is_purchasable = tutor_utils()->is_course_purchasable( $course_id );
2186 2074
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 2075 /**
2194 2076 * If is is not purchasable, it's free, and enroll right now
2195 2077 * If purchasable, then process purchase.
2196 2078 *
@@ -2200,9 +2082,9 @@
2200 2082 // Process purchase.
2201 2083
2202 2084 } else {
2203 2085 // Free enroll.
2204 - EnrollmentModel::do_enroll( $course_id );
2086 + tutor_utils()->do_enroll( $course_id );
2205 2087 }
2206 2088
2207 2089 $referer_url = wp_get_referer();
2208 2090 wp_safe_redirect( tutor_utils()->get_nocache_url( $referer_url ) );
@@ -2236,27 +2118,20 @@
2236 2118 if ( ! $user_id ) {
2237 2119 die( esc_html__( 'Please Sign-In', 'tutor' ) );
2238 2120 }
2239 2121
2240 - if ( ! EnrollmentModel::is_enrolled( $course_id, $user_id ) ) {
2241 - die( esc_html__( 'User is not enrolled in course', 'tutor' ) );
2242 - }
2243 -
2244 2122 /**
2245 2123 * Filter hook provided to restrict course completion. This is useful
2246 2124 * for specific cases like prerequisites. WP_Error should be returned
2247 2125 * from the filter value to prevent the completion.
2248 2126 */
2249 - $can_complete = apply_filters( 'tutor_user_can_complete_course', CourseModel::can_complete_course( $course_id, $user_id ), $user_id, $course_id );
2250 -
2127 + $can_complete = apply_filters( 'tutor_user_can_complete_course', true, $user_id, $course_id );
2251 2128 if ( is_wp_error( $can_complete ) ) {
2252 2129 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 2130 } else {
2256 2131 CourseModel::mark_course_as_completed( $course_id, $user_id );
2257 2132 // Set temporary identifier to show review pop up.
2258 - self::set_review_popup_data( $user_id, $course_id );
2133 + self::set_review_popup_data( $user_id, $course_id, $permalink );
2259 2134
2260 2135 wp_safe_redirect( $permalink );
2261 2136 exit;
2262 2137 }
@@ -2262,52 +2137,8 @@
2262 2137 }
2263 2138 }
2264 2139
2265 2140 /**
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 2141 * Set data for review popup.
2311 2142 *
2312 2143 * @since 2.2.5
2313 2144 * @since 2.4.0 removed $permalink param. store user meta instead of option data.
@@ -2333,34 +2164,18 @@
2333 2164 * @since 1.0.0
2334 2165 * @return void
2335 2166 */
2336 2167 public function popup_review_form() {
2337 - if ( ! is_user_logged_in() ) {
2338 - return;
2339 - }
2168 + if ( is_user_logged_in() ) {
2169 + $user_id = get_current_user_id();
2170 + $course_id = get_the_ID();
2171 + $meta_key = User::get_review_popup_meta( $course_id );
2172 + $review_course_id = (int) get_user_meta( $user_id, $meta_key, true );
2340 2173
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;
2174 + if ( is_single() && $course_id === $review_course_id ) {
2175 + include tutor()->path . 'views/modal/review.php';
2176 + }
2345 2177 }
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 2178 }
2364 2179
2365 2180 /**
2366 2181 * Review popup data clear
@@ -2388,9 +2203,8 @@
2388 2203 /**
2389 2204 * Delete course delete from frontend dashboard
2390 2205 *
2391 2206 * @since 2.0.0
2392 - *
2393 2207 * @return void
2394 2208 */
2395 2209 public function tutor_delete_dashboard_course() {
2396 2210 tutor_utils()->checking_nonce();
@@ -2409,9 +2223,9 @@
2409 2223 wp_send_json_error( array( 'message' => __( 'Only main instructor can delete this course', 'tutor' ) ) );
2410 2224 }
2411 2225
2412 2226 // Check if user is only an instructor.
2413 - if ( ! User::is_admin() ) {
2227 + if ( ! current_user_can( 'administrator' ) ) {
2414 2228 // Check if instructor can trash course.
2415 2229 $can_trash_post = tutor_utils()->get_option( 'instructor_can_delete_course' );
2416 2230
2417 2231 if ( ! $can_trash_post ) {
@@ -2421,9 +2235,9 @@
2421 2235
2422 2236 $trash_course = wp_update_post(
2423 2237 array(
2424 2238 'ID' => $course_id,
2425 - 'post_status' => CourseModel::STATUS_TRASH,
2239 + 'post_status' => 'trash',
2426 2240 )
2427 2241 );
2428 2242
2429 2243 if ( $trash_course ) {
@@ -2538,19 +2352,23 @@
2538 2352 update_post_meta( $product_id, '_subscription_price', $course_price );
2539 2353 }
2540 2354
2541 2355 // 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() );
2356 + update_post_meta( $post_ID, self::COURSE_PRICE_META, $product_obj->get_regular_price() );
2357 + update_post_meta( $post_ID, self::COURSE_SALE_PRICE_META, $product_obj->get_sale_price() );
2543 2358 } else {
2544 2359 // Create new WC product name with course title.
2545 2360 $product_id = self::create_wc_product( $course->post_title, $course_price, $sale_price );
2546 2361 if ( $product_id ) {
2547 2362 $product_obj = wc_get_product( $product_id );
2363 + update_post_meta( $post_ID, self::COURSE_PRODUCT_ID_META, $product_id );
2364 + // Mark product for woocommerce.
2365 + update_post_meta( $product_id, '_virtual', 'yes' );
2366 + update_post_meta( $product_id, '_tutor_product', 'yes' );
2548 2367
2549 - self::sync_course_with_wc_product( $post_ID, $product_id );
2550 -
2551 2368 // 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() );
2369 + update_post_meta( $post_ID, self::COURSE_PRICE_META, $product_obj->get_regular_price() );
2370 + update_post_meta( $post_ID, self::COURSE_SALE_PRICE_META, $product_obj->get_sale_price() );
2553 2371 }
2554 2372 }
2555 2373
2556 2374 $course_post_thumbnail = Input::post( 'thumbnail_id', 0, Input::TYPE_INT );
@@ -2598,8 +2416,33 @@
2598 2416 }
2599 2417 }
2600 2418
2601 2419 /**
2420 + * Add Course level to course settings
2421 + *
2422 + * @since 1.4.1
2423 + *
2424 + * @param array $args arguments.
2425 + * @return array
2426 + */
2427 + public function add_course_level_to_settings( $args ) {
2428 + $course_id = get_the_ID();
2429 + $levels = tutor_utils()->course_levels();
2430 + $course_level = get_post_meta( $course_id, '_tutor_course_level', true );
2431 +
2432 + $args['general']['fields']['_tutor_course_level'] = array(
2433 + 'type' => 'select',
2434 + 'label' => __( 'Difficulty Level', 'tutor' ),
2435 + 'label_title' => __( 'Enable', 'tutor' ),
2436 + 'options' => $levels,
2437 + 'value' => $course_level ? $course_level : 'intermediate',
2438 + 'desc' => __( 'Course difficulty level', 'tutor' ),
2439 + );
2440 +
2441 + return $args;
2442 + }
2443 +
2444 + /**
2602 2445 * Check if course starting
2603 2446 *
2604 2447 * @since 1.4.8
2605 2448 * @return void
@@ -2619,9 +2462,8 @@
2619 2462 /**
2620 2463 * Add Course level to course settings
2621 2464 *
2622 2465 * @since 1.4.8
2623 - *
2624 2466 * @return void
2625 2467 */
2626 2468 public function course_elements_enable_disable() {
2627 2469 add_filter( 'tutor_course/single/completing-progress-bar', array( $this, 'enable_disable_course_progress_bar' ) );
@@ -2654,13 +2496,12 @@
2654 2496 *
2655 2497 * @since 1.4.8
2656 2498 *
2657 2499 * @param string $html HTML string.
2658 - *
2659 2500 * @return string
2660 2501 */
2661 2502 public function enable_disable_material_includes( $html ) {
2662 - $disable_option = ! (bool) get_tutor_option( 'enable_course_material', true );
2503 + $disable_option = ! (bool) get_tutor_option( 'enable_course_material', true, true );
2663 2504 if ( $disable_option ) {
2664 2505 return '';
2665 2506 }
2666 2507 return $html;
@@ -2671,9 +2512,8 @@
2671 2512 *
2672 2513 * @since 1.4.8
2673 2514 *
2674 2515 * @param string $html HTML string.
2675 - *
2676 2516 * @return string
2677 2517 */
2678 2518 public function enable_disable_course_content( $html ) {
2679 2519 $disable_option = ! (bool) tutor_utils()->get_option( 'enable_course_description', true, true );
@@ -2688,9 +2528,8 @@
2688 2528 *
2689 2529 * @since 1.4.8
2690 2530 *
2691 2531 * @param string $html HTML string.
2692 - *
2693 2532 * @return string
2694 2533 */
2695 2534 public function enable_disable_course_benefits( $html ) {
2696 2535 $disable_option = ! (bool) tutor_utils()->get_option( 'enable_course_benefits', true, true );
@@ -2705,9 +2544,8 @@
2705 2544 *
2706 2545 * @since 1.4.8
2707 2546 *
2708 2547 * @param string $html HTML string.
2709 - *
2710 2548 * @return string
2711 2549 */
2712 2550 public function enable_disable_course_requirements( $html ) {
2713 2551 $disable_option = ! (bool) tutor_utils()->get_option( 'enable_course_requirements', true, true );
@@ -2722,9 +2560,8 @@
2722 2560 *
2723 2561 * @since 1.4.8
2724 2562 *
2725 2563 * @param string $html HTML string.
2726 - *
2727 2564 * @return string
2728 2565 */
2729 2566 public function enable_disable_course_target_audience( $html ) {
2730 2567 $disable_option = ! (bool) tutor_utils()->get_option( 'enable_course_target_audience', true, true );
@@ -2769,9 +2606,9 @@
2769 2606 unset( $items['reviews'] );
2770 2607 }
2771 2608
2772 2609 // Whether enrollment require.
2773 - $is_enrolled = EnrollmentModel::is_enrolled();
2610 + $is_enrolled = tutor_utils()->is_enrolled();
2774 2611
2775 2612 return array_filter(
2776 2613 $items,
2777 2614 function ( $item ) use ( $is_enrolled ) {
@@ -2786,9 +2623,8 @@
2786 2623 /**
2787 2624 * Filter product in shop page
2788 2625 *
2789 2626 * @since 1.4.9
2790 - *
2791 2627 * @return void|null
2792 2628 */
2793 2629 public function filter_product_in_shop_page() {
2794 2630 $hide_course_from_shop_page = (bool) get_tutor_option( 'hide_course_from_shop_page' );
@@ -2794,11 +2630,11 @@
2794 2630 $hide_course_from_shop_page = (bool) get_tutor_option( 'hide_course_from_shop_page' );
2795 2631 if ( ! $hide_course_from_shop_page ) {
2796 2632 return;
2797 2633 }
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 );
2634 + add_action( 'woocommerce_product_query', array( $this, 'filter_woocommerce_product_query' ) );
2635 + add_filter( 'edd_downloads_query', array( $this, 'filter_edd_downloads_query' ), 10, 2 );
2636 + add_action( 'pre_get_posts', array( $this, 'filter_archive_meta_query' ), 1 );
2801 2637 }
2802 2638
2803 2639
2804 2640 /**
@@ -2804,9 +2640,8 @@
2804 2640 /**
2805 2641 * Tutor product meta query
2806 2642 *
2807 2643 * @since 1.4.9
2808 - *
2809 2644 * @return array
2810 2645 */
2811 2646 public function tutor_product_meta_query() {
2812 2647 $meta_query = array(
@@ -2821,9 +2656,8 @@
2821 2656 *
2822 2657 * @since 1.4.9
2823 2658 *
2824 2659 * @param \WP_Query $wp_query WP Query instance.
2825 - *
2826 2660 * @return \WP_Query
2827 2661 */
2828 2662 public function filter_woocommerce_product_query( $wp_query ) {
2829 2663 $product_ids = $this->get_connected_wc_product_ids();
@@ -2865,9 +2699,8 @@
2865 2699 *
2866 2700 * @since 1.4.9
2867 2701 *
2868 2702 * @param \WP_Query $query WP Query instance.
2869 - *
2870 2703 * @return \WP_Query
2871 2704 */
2872 2705 public function filter_edd_downloads_query( $query ) {
2873 2706 $query['meta_query'][] = $this->tutor_product_meta_query();
@@ -2879,9 +2712,8 @@
2879 2712 *
2880 2713 * @since 1.4.9
2881 2714 *
2882 2715 * @param \WP_Query $wp_query WP Query instance.
2883 - *
2884 2716 * @return \WP_Query
2885 2717 */
2886 2718 public function filter_archive_meta_query( $wp_query ) {
2887 2719 if ( ! is_admin() && $wp_query->is_archive && $wp_query->get( 'post_type' ) === 'download' ) {
@@ -2895,9 +2727,8 @@
2895 2727 *
2896 2728 * @since 1.5.8
2897 2729 *
2898 2730 * @param string $html HTML string.
2899 - *
2900 2731 * @return string
2901 2732 */
2902 2733 public function remove_price_if_enrolled( $html ) {
2903 2734 $should_removed = apply_filters( 'should_remove_price_if_enrolled', true );
@@ -2903,9 +2734,9 @@
2903 2734 $should_removed = apply_filters( 'should_remove_price_if_enrolled', true );
2904 2735
2905 2736 if ( $should_removed ) {
2906 2737 $course_id = get_the_ID();
2907 - $enrolled = EnrollmentModel::is_enrolled( $course_id );
2738 + $enrolled = tutor_utils()->is_enrolled( $course_id );
2908 2739 if ( $enrolled ) {
2909 2740 $html = '';
2910 2741 }
2911 2742 }
@@ -2912,119 +2743,112 @@
2912 2743 return $html;
2913 2744 }
2914 2745
2915 2746 /**
2916 - * Get course completion missing requirements message.
2747 + * Check if all lessons and quizzes done before mark course complete.
2917 2748 *
2918 - * @since 4.0.0
2749 + * @since 1.5.8
2919 2750 *
2920 - * @param int $course_id Course ID.
2921 - * @param int $user_id User ID.
2922 - *
2923 - * @return string|null
2751 + * @param string $html HTML string.
2752 + * @return string
2924 2753 */
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 );
2754 + public function tutor_lms_hide_course_complete_btn( $html ) {
2928 2755
2929 - if ( 'strict' !== tutor_utils()->get_option( 'course_completion_process' ) ) {
2930 - return null;
2756 + $completion_mode = tutor_utils()->get_option( 'course_completion_process' );
2757 + if ( 'strict' !== $completion_mode ) {
2758 + return $html;
2931 2759 }
2932 2760
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
2761 + $completed_lesson = tutor_utils()->get_completed_lesson_count_by_course();
2762 + $lesson_count = tutor_utils()->get_lesson_count_by_course();
2935 2763
2936 - if ( $completed_lessons < $total_lessons ) {
2937 - return __( 'Complete all lessons to mark this course as complete', 'tutor' );
2764 + if ( $completed_lesson < $lesson_count ) {
2765 + return '<div class="tutor-alert tutor-warning tutor-mt-28">
2766 + <div class="tutor-alert-text">
2767 + <span class="tutor-alert-icon tutor-fs-4 tutor-icon-circle-info tutor-mr-12"></span>
2768 + <span>' . __( 'Complete all lessons to mark this course as complete', 'tutor' ) . '</span>
2769 + </div>
2770 + </div>';
2938 2771 }
2939 2772
2940 - $course_contents = tutor_utils()->get_course_contents_by_id( $course_id );
2773 + $quizzes = array();
2774 + $assignments = array();
2941 2775
2942 - $required_quiz_pass_count = 0;
2943 - $required_assignment_pass_count = 0;
2776 + $course_contents = tutor_utils()->get_course_contents_by_id();
2777 + if ( tutor_utils()->count( $course_contents ) ) {
2778 + foreach ( $course_contents as $content ) {
2779 + if ( 'tutor_quiz' === $content->post_type ) {
2780 + $quizzes[] = $content;
2781 + }
2782 + if ( 'tutor_assignments' === $content->post_type ) {
2783 + $assignments[] = $content;
2784 + }
2785 + }
2786 + }
2944 2787
2945 - $is_assignment_addon_enabled = tutor_utils()->is_addon_enabled( 'tutor-assignments' );
2788 + $required_assignment_pass = 0;
2946 2789
2947 - foreach ( $course_contents as $content ) {
2790 + foreach ( $assignments as $row ) {
2948 2791
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 - }
2792 + $submitted_assignment = tutor_utils()->is_assignment_submitted( $row->ID );
2793 + $is_reviewed_by_instructor = null === $submitted_assignment
2794 + ? false
2795 + : get_comment_meta( $submitted_assignment->comment_ID, 'evaluate_time', true );
2954 2796
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;
2797 + if ( $submitted_assignment && $is_reviewed_by_instructor ) {
2798 + $pass_mark = tutor_utils()->get_assignment_option( $submitted_assignment->comment_post_ID, 'pass_mark' );
2799 + $given_mark = get_comment_meta( $submitted_assignment->comment_ID, 'assignment_mark', true );
2800 +
2801 + if ( $given_mark < $pass_mark ) {
2802 + $required_assignment_pass++;
2958 2803 }
2804 + } else {
2805 + $required_assignment_pass++;
2959 2806 }
2960 2807 }
2961 2808
2962 - if ( ! $required_quiz_pass_count && ! $required_assignment_pass_count ) {
2963 - return null;
2964 - }
2809 + $is_quiz_pass = true;
2810 + $required_quiz_pass = 0;
2965 2811
2966 - return self::get_course_completion_requirement_message(
2967 - $required_quiz_pass_count,
2968 - $required_assignment_pass_count
2969 - );
2970 - }
2812 + if ( tutor_utils()->count( $quizzes ) ) {
2813 + foreach ( $quizzes as $quiz ) {
2971 2814
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' );
2815 + $attempt = tutor_utils()->get_quiz_attempt( $quiz->ID );
2816 + if ( $attempt ) {
2817 + $passing_grade = tutor_utils()->get_quiz_option( $quiz->ID, 'passing_grade', 0 );
2818 + $earned_percentage = QuizModel::calculate_attempt_earned_percentage( $attempt );
2985 2819
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 - );
2820 + if ( $earned_percentage < $passing_grade ) {
2821 + $required_quiz_pass++;
2822 + $is_quiz_pass = false;
2823 + }
2824 + } else {
2825 + $required_quiz_pass++;
2826 + $is_quiz_pass = false;
2827 + }
2828 + }
2993 2829 }
2994 2830
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 - }
2831 + if ( ! $is_quiz_pass || $required_assignment_pass > 0 ) {
2832 + $_msg = '';
2833 + $quiz_str = _n( 'quiz', 'quizzes', $required_quiz_pass, 'tutor' );
2834 + $assignment_str = _n( 'assignment', 'assignments', $required_assignment_pass, 'tutor' );
3003 2835
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 - }
2836 + if ( ! $is_quiz_pass && 0 == $required_assignment_pass ) {
2837 + /* translators: %1$s: number of quiz/assignment pass required; %2$s: quiz/assignment string */
2838 + $_msg = sprintf( __( 'You have to pass %1$s %2$s to complete this course.', 'tutor' ), $required_quiz_pass, $quiz_str );
2839 + }
3013 2840
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();
2841 + if ( $is_quiz_pass && $required_assignment_pass > 0 ) {
2842 + //phpcs:ignore
2843 + $_msg = sprintf( __( 'You have to pass %1$s %2$s to complete this course.', 'tutor' ), $required_assignment_pass, $assignment_str );
2844 + }
3025 2845
3026 - if ( $_msg ) {
2846 + if ( ! $is_quiz_pass && $required_assignment_pass > 0 ) {
2847 + /* translators: %1$s: number of quiz pass required; %2$s: quiz string; %3$s: number of assignment pass required; %4$s: assignment string */
2848 + $_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 );
2849 + }
2850 +
3027 2851 return '<div class="tutor-alert tutor-warning tutor-mt-28">
3028 2852 <div class="tutor-alert-text">
3029 2853 <span class="tutor-alert-icon tutor-fs-4 tutor-icon-circle-info tutor-mr-12"></span>
3030 2854 <span>' . $_msg . '</span>
@@ -3040,9 +2864,8 @@
3040 2864 *
3041 2865 * @since 1.5.8
3042 2866 *
3043 2867 * @param string $html HTML string.
3044 - *
3045 2868 * @return string
3046 2869 */
3047 2870 public function get_generate_greadbook( $html ) {
3048 2871 if ( ! tutor_utils()->is_completed_course() ) {
@@ -3054,9 +2877,8 @@
3054 2877 /**
3055 2878 * Add social share content in header
3056 2879 *
3057 2880 * @since 1.6.3
3058 - *
3059 2881 * @return void
3060 2882 */
3061 2883 public function social_share_content() {
3062 2884 global $wp_query, $post;
@@ -3080,9 +2902,8 @@
3080 2902 *
3081 2903 * @since 1.8.2
3082 2904 *
3083 2905 * @param integer $post_id post ID.
3084 - *
3085 2906 * @return void
3086 2907 */
3087 2908 public function delete_associated_enrollment( $post_id ) {
3088 2909 global $wpdb;
@@ -3093,12 +2914,11 @@
3093 2914 post_id
3094 2915 FROM
3095 2916 {$wpdb->postmeta}
3096 2917 WHERE
3097 - meta_key=%s
2918 + meta_key='_tutor_enrolled_by_order_id'
3098 2919 AND meta_value = %d
3099 2920 ",
3100 - EnrollmentModel::ENROLLMENT_ORDER_ID_META,
3101 2921 $post_id
3102 2922 )
3103 2923 );
3104 2924
@@ -3114,35 +2934,21 @@
3114 2934 /**
3115 2935 * Reset course progress.
3116 2936 *
3117 2937 * @since 1.5.8
3118 - *
3119 2938 * @return void
3120 2939 */
3121 2940 public function tutor_reset_course_progress() {
3122 2941 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 );
2942 + $course_id = Input::post( 'course_id' );
3127 2943
3128 - if ( ! $course_reset_progress && 'learning-area-sidebar' === $context ) {
3129 - $this->response_bad_request( __( 'You do not have permission to reset this course.', 'tutor' ) );
2944 + if ( ! $course_id || ! is_numeric( $course_id ) || ! tutor_utils()->is_enrolled( $course_id ) ) {
2945 + wp_send_json_error( array( 'message' => __( 'Invalid Course ID or Access Denied.', 'tutor' ) ) );
3130 2946 return;
3131 2947 }
3132 2948
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 2949 tutor_utils()->delete_course_progress( $course_id );
3144 - $this->json_response( '', array( 'redirect_to' => tutor_utils()->get_course_first_lesson( $course_id ) ) );
2950 + wp_send_json_success( array( 'redirect_to' => tutor_utils()->get_course_first_lesson( $course_id ) ) );
3145 2951 }
3146 2952
3147 2953 /**
3148 2954 * Do enroll if guest attempt to enroll and course is free
@@ -3150,9 +2956,9 @@
3150 2956 * @since 1.9.8
3151 2957 *
3152 2958 * @param integer $course_id course ID.
3153 2959 * @param integer $user_id user ID.
3154 - *
2960 +
3155 2961 * @return void
3156 2962 */
3157 2963 public function enroll_after_login_if_attempt( int $course_id, int $user_id ) {
3158 2964 $course_id = sanitize_text_field( $course_id );
@@ -3160,9 +2966,9 @@
3160 2966
3161 2967 if ( $course_id && $is_allowed ) {
3162 2968 $is_purchasable = tutor_utils()->is_course_purchasable( $course_id );
3163 2969 if ( ! $is_purchasable ) {
3164 - EnrollmentModel::do_enroll( $course_id, $order_id = 0, $user_id );
2970 + tutor_utils()->do_enroll( $course_id, $order_id = 0, $user_id );
3165 2971 do_action( 'guest_attempt_after_enrollment', $course_id );
3166 2972 }
3167 2973 }
3168 2974 }
@@ -3170,9 +2976,8 @@
3170 2976 /**
3171 2977 * Handle course enrollment
3172 2978 *
3173 2979 * @since 2.1.0
3174 - *
3175 2980 * @return void
3176 2981 */
3177 2982 public function course_enrollment() {
3178 2983 tutor_utils()->checking_nonce();
@@ -3179,46 +2984,21 @@
3179 2984
3180 2985 $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
3181 2986 $user_id = get_current_user_id();
3182 2987
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' ) );
2988 + if ( $course_id ) {
2989 + $password_protected = post_password_required( $course_id );
2990 + if ( $password_protected ) {
2991 + wp_send_json_error( __( 'This course is password protected', 'tutor' ) );
3213 2992 }
3214 - }
3215 -
3216 - $enroll = EnrollmentModel::do_enroll( $course_id, 0, $user_id );
3217 - if ( $enroll ) {
3218 - wp_send_json_success( __( 'Enrollment successfully done!', 'tutor' ) );
2993 + $enroll = tutor_utils()->do_enroll( $course_id, 0, $user_id );
2994 + if ( $enroll ) {
2995 + wp_send_json_success( __( 'Enrollment successfully done!', 'tutor' ) );
2996 + } else {
2997 + wp_send_json_error( __( 'Enrollment failed, please try again!', 'tutor' ) );
2998 + }
3219 2999 } else {
3220 - wp_send_json_error( __( 'Enrollment failed, please try again!', 'tutor' ) );
3000 + wp_send_json_error( __( 'Invalid course ID', 'tutor' ) );
3221 3001 }
3222 3002 }
3223 3003
3224 3004 /**
@@ -3305,10 +3085,14 @@
3305 3085 'regular_price' => $regular_price,
3306 3086 'sale_price' => $sale_price,
3307 3087 );
3308 3088
3309 - $card_data = apply_filters( 'tutor_course_mini_info', $info, $post );
3089 + if ( 'course-bundle' === $post->post_type && tutor_utils()->is_addon_enabled( 'tutor-pro/addons/course-bundle/course-bundle.php' ) ) {
3090 + $info['total_course'] = count( BundleModel::get_bundle_course_ids( $post->ID ) );
3091 + }
3310 3092
3093 + $card_data = apply_filters( 'tutor_add_course_plan_info', $info, $post );
3094 +
3311 3095 return $card_data;
3312 3096 }
3313 3097
3314 3098 /**
@@ -3329,9 +3113,9 @@
3329 3113 $info['last_updated'] = tutor_i18n_get_formated_date( $post->post_modified_at );
3330 3114 $info['course_duration'] = tutor_utils()->get_course_duration( $post->ID, false );
3331 3115 $info['total_enrolled'] = tutor_utils()->count_enrolled_users_by_course( $post->ID );
3332 3116
3333 - $card_data = apply_filters( 'tutor_course_card_data', $info, $post );
3117 + $card_data = apply_filters( 'tutor_add_course_plan_info', $info, $post );
3334 3118
3335 3119 return $card_data;
3336 3120 }
3337 3121
@@ -3388,199 +3172,6 @@
3388 3172 'trash',
3389 3173 'pending',
3390 3174 'future',
3391 3175 );
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 3176 }
3586 3177 }