PluginProbe
Tutor LMS – eLearning and online course solution / 3.7.3
Tutor LMS – eLearning and online course solution v3.7.3
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 +253 -651 4.0.53.7.3 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 );
@@ -1529,9 +1471,9 @@
1529 1471 $default_data = ( new Assets( false ) )->get_default_localized_data();
1530 1472
1531 1473 if ( isset( $default_data['current_user']['data']['id'] ) ) {
1532 1474 $tutor_user = tutor_utils()->get_tutor_user( $default_data['current_user']['data']['id'] );
1533 - $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;
1534 1476 }
1535 1477
1536 1478 /**
1537 1479 * Localized only options to protect sensitive info like API keys.
@@ -1536,9 +1478,8 @@
1536 1478 /**
1537 1479 * Localized only options to protect sensitive info like API keys.
1538 1480 */
1539 1481 $required_options = array(
1540 - 'learning_mode',
1541 1482 'monetize_by',
1542 1483 'enable_course_marketplace',
1543 1484 'course_permalink_base',
1544 1485 'supported_video_sources',
@@ -1740,38 +1681,23 @@
1740 1681 /**
1741 1682 * Update course content order
1742 1683 *
1743 1684 * @since 1.0.0
1744 - * @since 3.9.9 Check if user can manage course before updating order.
1745 - *
1746 1685 * @return void
1747 1686 */
1748 - public function ajax_update_course_content_order() {
1687 + public function tutor_update_course_content_order() {
1749 1688 tutor_utils()->checking_nonce();
1750 1689
1751 - $sorting_order = Input::post( 'tutor_topics_lessons_sorting', '' );
1752 - $sorting_order = json_decode( $sorting_order, true ) ?? array();
1753 -
1754 - if ( ! tutor_utils()->count( $sorting_order ) ) {
1755 - wp_send_json_error( __( 'Sorting order is required', 'tutor' ) );
1756 - }
1757 -
1758 - $topic_id = (int) isset( $sorting_order[0], $sorting_order[0]['topic_id'] ) ? $sorting_order[0]['topic_id'] : 0;
1759 - $course_id = wp_get_post_parent_id( $topic_id );
1760 -
1761 - if ( ! $topic_id || ! $course_id ) {
1762 - $this->response_bad_request( tutor_utils()->error_message( 'invalid_req' ) );
1763 - }
1764 -
1765 - if ( ! tutor_utils()->can_user_manage( 'course', $course_id ) && ! User::is_admin() ) {
1766 - $this->json_response( tutor_utils()->error_message(), null, HttpHelper::STATUS_UNAUTHORIZED );
1767 - }
1768 -
1769 1690 if ( Input::has( 'content_parent' ) ) {
1770 1691 $content_parent = Input::post( 'content_parent', array(), Input::TYPE_ARRAY );
1771 1692 $topic_id = tutor_utils()->array_get( 'parent_topic_id', $content_parent );
1772 1693 $content_id = tutor_utils()->array_get( 'content_id', $content_parent );
1773 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 +
1774 1700 // Update the parent topic id of the content.
1775 1701 global $wpdb;
1776 1702 $wpdb->update( $wpdb->posts, array( 'post_parent' => $topic_id ), array( 'ID' => $content_id ) );
1777 1703 }
@@ -1776,11 +1702,11 @@
1776 1702 $wpdb->update( $wpdb->posts, array( 'post_parent' => $topic_id ), array( 'ID' => $content_id ) );
1777 1703 }
1778 1704
1779 1705 // Save course content order.
1780 - $this->save_course_content_order( $sorting_order );
1706 + $this->save_course_content_order();
1781 1707
1782 - $this->response_success( __( 'Course content order updated successfully!', 'tutor' ) );
1708 + wp_send_json_success();
1783 1709 }
1784 1710
1785 1711 /**
1786 1712 * Restrict new student entry
@@ -1805,11 +1731,9 @@
1805 1731 /**
1806 1732 * Restrict media
1807 1733 *
1808 1734 * @since 1.0.0
1809 - *
1810 1735 * @param string $where where clause.
1811 - *
1812 1736 * @return string
1813 1737 */
1814 1738 public function restrict_media( $where ) {
1815 1739 $action = Input::post( 'action' );
@@ -1825,58 +1749,58 @@
1825 1749 /**
1826 1750 * Save course content order
1827 1751 *
1828 1752 * @since 1.0.0
1829 - * @since 3.9.8 param $order added.
1830 - *
1831 - * @param array $sort_order the lesson and topic order array.
1832 - *
1833 1753 * @return void
1834 1754 */
1835 - private function save_course_content_order( $sort_order = array() ) {
1755 + private function save_course_content_order() {
1836 1756 global $wpdb;
1837 1757
1838 - if ( ! tutor_utils()->count( $sort_order ) ) {
1839 - return;
1840 - }
1758 + $new_order = Input::post( 'tutor_topics_lessons_sorting' );
1759 + if ( ! empty( $new_order ) ) {
1760 + $order = json_decode( $new_order, true );
1841 1761
1842 - $i = 0;
1843 - foreach ( $sort_order as $topic ) {
1844 - ++$i;
1845 - $wpdb->update(
1846 - $wpdb->posts,
1847 - array( 'menu_order' => $i ),
1848 - array( 'ID' => $topic['topic_id'] )
1849 - );
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 + );
1850 1771
1851 - /**
1852 - * Removing All lesson with topic
1853 - */
1854 - $wpdb->update(
1855 - $wpdb->posts,
1856 - array( 'post_parent' => 0 ),
1857 - array( 'post_parent' => $topic['topic_id'] )
1858 - );
1772 + /**
1773 + * Removing All lesson with topic
1774 + */
1859 1775
1860 - /**
1861 - * Lesson Attaching with topic ID
1862 - * Sorting lesson
1863 - */
1864 - if ( isset( $topic['lesson_ids'] ) ) {
1865 - $lesson_ids = $topic['lesson_ids'];
1866 - } else {
1867 - $lesson_ids = array();
1868 - }
1869 - if ( count( $lesson_ids ) ) {
1870 - foreach ( $lesson_ids as $lesson_key => $lesson_id ) {
1871 1776 $wpdb->update(
1872 1777 $wpdb->posts,
1873 - array(
1874 - 'post_parent' => $topic['topic_id'],
1875 - 'menu_order' => $lesson_key,
1876 - ),
1877 - array( 'ID' => $lesson_id )
1778 + array( 'post_parent' => 0 ),
1779 + array( 'post_parent' => $topic['topic_id'] )
1878 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 + }
1879 1803 }
1880 1804 }
1881 1805 }
1882 1806 }
@@ -1947,14 +1871,12 @@
1947 1871 }
1948 1872 //phpcs:enable WordPress.Security.NonceVerification.Missing
1949 1873 }
1950 1874
1951 - $sorting_order = Input::post( 'tutor_topics_lessons_sorting', '' );
1952 - $sorting_order = json_decode( $sorting_order, true ) ?? array();
1953 1875 /**
1954 1876 * Sorting Topics and lesson
1955 1877 */
1956 - $this->save_course_content_order( $sorting_order );
1878 + $this->save_course_content_order();
1957 1879
1958 1880 // Additional data like course intro video.
1959 1881 if ( $additional_data_edit ) {
1960 1882 // Sanitize data through helper method.
@@ -1976,23 +1898,12 @@
1976 1898
1977 1899 /**
1978 1900 * Adding author to instructor automatically
1979 1901 */
1980 - $requested_author_id = Input::post( 'post_author_override', 0, Input::TYPE_INT );
1981 1902
1982 - /**
1983 - * Only accept the requested author override if it targets a real,approved instructor or admin
1984 - *
1985 - * @since 4.0.4
1986 - */
1987 - $author_id = $post->post_author;
1988 - if ( $requested_author_id && ( User::is_admin() || User::is_instructor() ) ) {
1989 - if ( User::is_admin( $requested_author_id ) || User::is_instructor( $requested_author_id, true ) ) {
1990 - $author_id = $requested_author_id;
1991 - }
1992 - }
1993 -
1994 - $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(
1995 1906 $wpdb->prepare(
1996 1907 "SELECT COUNT(umeta_id) FROM {$wpdb->usermeta}
1997 1908 WHERE user_id = %d
1998 1909 AND meta_key = '_tutor_instructor_course_id'
@@ -2017,20 +1928,8 @@
2017 1928 update_post_meta( $post_ID, $key, ( isset( $_POST[ $key ] ) ? 'yes' : 'no' ) );
2018 1929 }
2019 1930 }
2020 1931
2021 - /**
2022 - * Update course content if main author is changed and multi-instructor addon is disabled.
2023 - *
2024 - * @since 4.0.4
2025 - */
2026 - if ( ! $attached && ! tutor_utils()->is_addon_enabled( 'tutor-multi-instructors' ) ) {
2027 - CourseModel::update_course_content_author( $post_ID, (int) $author_id );
2028 - // Remove all existing instructors from the course and add the new one.
2029 - delete_metadata( 'user', 0, '_tutor_instructor_course_id', $post_ID, true );
2030 - add_user_meta( $author_id, '_tutor_instructor_course_id', $post_ID );
2031 - }
2032 -
2033 1932 do_action( 'tutor_save_course_after', $post_ID, $post );
2034 1933 }
2035 1934
2036 1935 /**
@@ -2172,14 +2071,8 @@
2172 2071 */
2173 2072
2174 2073 $is_purchasable = tutor_utils()->is_course_purchasable( $course_id );
2175 2074
2176 - $course = get_post( $course_id );
2177 -
2178 - if ( 'private' === $course->post_status && ! current_user_can( 'read_private_tutor_courses' ) ) {
2179 - wp_send_json_error( __( 'You do not have permission to enroll in this course', 'tutor' ) );
2180 - }
2181 -
2182 2075 /**
2183 2076 * If is is not purchasable, it's free, and enroll right now
2184 2077 * If purchasable, then process purchase.
2185 2078 *
@@ -2189,9 +2082,9 @@
2189 2082 // Process purchase.
2190 2083
2191 2084 } else {
2192 2085 // Free enroll.
2193 - EnrollmentModel::do_enroll( $course_id );
2086 + tutor_utils()->do_enroll( $course_id );
2194 2087 }
2195 2088
2196 2089 $referer_url = wp_get_referer();
2197 2090 wp_safe_redirect( tutor_utils()->get_nocache_url( $referer_url ) );
@@ -2225,27 +2118,20 @@
2225 2118 if ( ! $user_id ) {
2226 2119 die( esc_html__( 'Please Sign-In', 'tutor' ) );
2227 2120 }
2228 2121
2229 - if ( ! EnrollmentModel::is_enrolled( $course_id, $user_id ) ) {
2230 - die( esc_html__( 'User is not enrolled in course', 'tutor' ) );
2231 - }
2232 -
2233 2122 /**
2234 2123 * Filter hook provided to restrict course completion. This is useful
2235 2124 * for specific cases like prerequisites. WP_Error should be returned
2236 2125 * from the filter value to prevent the completion.
2237 2126 */
2238 - $can_complete = apply_filters( 'tutor_user_can_complete_course', CourseModel::can_complete_course( $course_id, $user_id ), $user_id, $course_id );
2239 -
2127 + $can_complete = apply_filters( 'tutor_user_can_complete_course', true, $user_id, $course_id );
2240 2128 if ( is_wp_error( $can_complete ) ) {
2241 2129 tutor_utils()->redirect_to( $permalink, $can_complete->get_error_message(), 'error' );
2242 - } elseif ( ! $can_complete ) {
2243 - tutor_utils()->redirect_to( $permalink, __( 'You do not have permission to complete this course.', 'tutor' ), 'error' );
2244 2130 } else {
2245 2131 CourseModel::mark_course_as_completed( $course_id, $user_id );
2246 2132 // Set temporary identifier to show review pop up.
2247 - self::set_review_popup_data( $user_id, $course_id );
2133 + self::set_review_popup_data( $user_id, $course_id, $permalink );
2248 2134
2249 2135 wp_safe_redirect( $permalink );
2250 2136 exit;
2251 2137 }
@@ -2251,52 +2137,8 @@
2251 2137 }
2252 2138 }
2253 2139
2254 2140 /**
2255 - * Ajax course complete handler
2256 - *
2257 - * @since 4.0.0
2258 - *
2259 - * @return void
2260 - */
2261 - public function ajax_tutor_complete_course() {
2262 - $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
2263 -
2264 - // Checking nonce.
2265 - if ( ! tutor_utils()->is_nonce_verified() ) {
2266 - $this->response_bad_request( tutor_utils()->error_message( 'nonce' ) );
2267 - }
2268 -
2269 - if ( ! $course_id ) {
2270 - $this->response_bad_request( __( 'Invalid course', 'tutor' ) );
2271 - }
2272 -
2273 - $user_id = get_current_user_id();
2274 - if ( ! EnrollmentModel::is_enrolled( $course_id, $user_id ) ) {
2275 - $this->response_bad_request( __( 'You are not enrolled in this course', 'tutor' ) );
2276 - }
2277 -
2278 - /**
2279 - * Filter hook provided to restrict course completion. This is useful
2280 - * for specific cases like prerequisites. WP_Error should be returned
2281 - * from the filter value to prevent the completion.
2282 - */
2283 - $can_complete = apply_filters( 'tutor_user_can_complete_course', CourseModel::can_complete_course( $course_id, $user_id ), $user_id, $course_id );
2284 -
2285 - if ( is_wp_error( $can_complete ) ) {
2286 - $this->response_bad_request( $can_complete->get_error_message() );
2287 - } elseif ( ! $can_complete ) {
2288 - $this->response_bad_request( __( 'You do not have permission to complete this course.', 'tutor' ) );
2289 - } else {
2290 - CourseModel::mark_course_as_completed( $course_id, $user_id );
2291 - // Set temporary identifier to show review pop up.
2292 - self::set_review_popup_data( $user_id, $course_id );
2293 -
2294 - $this->response_success( __( 'Course completed successfully', 'tutor' ) );
2295 - }
2296 - }
2297 -
2298 - /**
2299 2141 * Set data for review popup.
2300 2142 *
2301 2143 * @since 2.2.5
2302 2144 * @since 2.4.0 removed $permalink param. store user meta instead of option data.
@@ -2322,34 +2164,18 @@
2322 2164 * @since 1.0.0
2323 2165 * @return void
2324 2166 */
2325 2167 public function popup_review_form() {
2326 - if ( ! is_user_logged_in() ) {
2327 - return;
2328 - }
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 );
2329 2173
2330 - $is_learning_area = tutor_utils()->is_learning_area();
2331 - $is_legacy_learning = tutor_utils()->is_legacy_learning_mode();
2332 - if ( $is_legacy_learning && $is_learning_area ) {
2333 - return;
2174 + if ( is_single() && $course_id === $review_course_id ) {
2175 + include tutor()->path . 'views/modal/review.php';
2176 + }
2334 2177 }
2335 -
2336 - $course_id = get_the_ID();
2337 -
2338 - if ( $is_learning_area && ! Input::has( 'subpage' ) ) {
2339 - $course_id = wp_get_post_parent_id( wp_get_post_parent_id( get_the_ID() ) );
2340 - }
2341 -
2342 - if ( empty( $course_id ) ) {
2343 - return;
2344 - }
2345 -
2346 - $user_id = get_current_user_id();
2347 - $meta_key = User::get_review_popup_meta( $course_id );
2348 - $review_course_id = (int) get_user_meta( $user_id, $meta_key, true );
2349 - if ( is_single() && $course_id === $review_course_id ) {
2350 - include tutor()->path . 'views/modal/review.php';
2351 - }
2352 2178 }
2353 2179
2354 2180 /**
2355 2181 * Review popup data clear
@@ -2377,9 +2203,8 @@
2377 2203 /**
2378 2204 * Delete course delete from frontend dashboard
2379 2205 *
2380 2206 * @since 2.0.0
2381 - *
2382 2207 * @return void
2383 2208 */
2384 2209 public function tutor_delete_dashboard_course() {
2385 2210 tutor_utils()->checking_nonce();
@@ -2398,9 +2223,9 @@
2398 2223 wp_send_json_error( array( 'message' => __( 'Only main instructor can delete this course', 'tutor' ) ) );
2399 2224 }
2400 2225
2401 2226 // Check if user is only an instructor.
2402 - if ( ! User::is_admin() ) {
2227 + if ( ! current_user_can( 'administrator' ) ) {
2403 2228 // Check if instructor can trash course.
2404 2229 $can_trash_post = tutor_utils()->get_option( 'instructor_can_delete_course' );
2405 2230
2406 2231 if ( ! $can_trash_post ) {
@@ -2410,9 +2235,9 @@
2410 2235
2411 2236 $trash_course = wp_update_post(
2412 2237 array(
2413 2238 'ID' => $course_id,
2414 - 'post_status' => CourseModel::STATUS_TRASH,
2239 + 'post_status' => 'trash',
2415 2240 )
2416 2241 );
2417 2242
2418 2243 if ( $trash_course ) {
@@ -2527,19 +2352,23 @@
2527 2352 update_post_meta( $product_id, '_subscription_price', $course_price );
2528 2353 }
2529 2354
2530 2355 // Set course regular & sale price.
2531 - 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() );
2532 2358 } else {
2533 2359 // Create new WC product name with course title.
2534 2360 $product_id = self::create_wc_product( $course->post_title, $course_price, $sale_price );
2535 2361 if ( $product_id ) {
2536 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' );
2537 2367
2538 - self::sync_course_with_wc_product( $post_ID, $product_id );
2539 -
2540 2368 // Set course regular & sale price.
2541 - 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() );
2542 2371 }
2543 2372 }
2544 2373
2545 2374 $course_post_thumbnail = Input::post( 'thumbnail_id', 0, Input::TYPE_INT );
@@ -2587,8 +2416,33 @@
2587 2416 }
2588 2417 }
2589 2418
2590 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 + /**
2591 2445 * Check if course starting
2592 2446 *
2593 2447 * @since 1.4.8
2594 2448 * @return void
@@ -2608,9 +2462,8 @@
2608 2462 /**
2609 2463 * Add Course level to course settings
2610 2464 *
2611 2465 * @since 1.4.8
2612 - *
2613 2466 * @return void
2614 2467 */
2615 2468 public function course_elements_enable_disable() {
2616 2469 add_filter( 'tutor_course/single/completing-progress-bar', array( $this, 'enable_disable_course_progress_bar' ) );
@@ -2643,13 +2496,12 @@
2643 2496 *
2644 2497 * @since 1.4.8
2645 2498 *
2646 2499 * @param string $html HTML string.
2647 - *
2648 2500 * @return string
2649 2501 */
2650 2502 public function enable_disable_material_includes( $html ) {
2651 - $disable_option = ! (bool) get_tutor_option( 'enable_course_material', true );
2503 + $disable_option = ! (bool) get_tutor_option( 'enable_course_material', true, true );
2652 2504 if ( $disable_option ) {
2653 2505 return '';
2654 2506 }
2655 2507 return $html;
@@ -2660,9 +2512,8 @@
2660 2512 *
2661 2513 * @since 1.4.8
2662 2514 *
2663 2515 * @param string $html HTML string.
2664 - *
2665 2516 * @return string
2666 2517 */
2667 2518 public function enable_disable_course_content( $html ) {
2668 2519 $disable_option = ! (bool) tutor_utils()->get_option( 'enable_course_description', true, true );
@@ -2677,9 +2528,8 @@
2677 2528 *
2678 2529 * @since 1.4.8
2679 2530 *
2680 2531 * @param string $html HTML string.
2681 - *
2682 2532 * @return string
2683 2533 */
2684 2534 public function enable_disable_course_benefits( $html ) {
2685 2535 $disable_option = ! (bool) tutor_utils()->get_option( 'enable_course_benefits', true, true );
@@ -2694,9 +2544,8 @@
2694 2544 *
2695 2545 * @since 1.4.8
2696 2546 *
2697 2547 * @param string $html HTML string.
2698 - *
2699 2548 * @return string
2700 2549 */
2701 2550 public function enable_disable_course_requirements( $html ) {
2702 2551 $disable_option = ! (bool) tutor_utils()->get_option( 'enable_course_requirements', true, true );
@@ -2711,9 +2560,8 @@
2711 2560 *
2712 2561 * @since 1.4.8
2713 2562 *
2714 2563 * @param string $html HTML string.
2715 - *
2716 2564 * @return string
2717 2565 */
2718 2566 public function enable_disable_course_target_audience( $html ) {
2719 2567 $disable_option = ! (bool) tutor_utils()->get_option( 'enable_course_target_audience', true, true );
@@ -2758,9 +2606,9 @@
2758 2606 unset( $items['reviews'] );
2759 2607 }
2760 2608
2761 2609 // Whether enrollment require.
2762 - $is_enrolled = EnrollmentModel::is_enrolled();
2610 + $is_enrolled = tutor_utils()->is_enrolled();
2763 2611
2764 2612 return array_filter(
2765 2613 $items,
2766 2614 function ( $item ) use ( $is_enrolled ) {
@@ -2775,9 +2623,8 @@
2775 2623 /**
2776 2624 * Filter product in shop page
2777 2625 *
2778 2626 * @since 1.4.9
2779 - *
2780 2627 * @return void|null
2781 2628 */
2782 2629 public function filter_product_in_shop_page() {
2783 2630 $hide_course_from_shop_page = (bool) get_tutor_option( 'hide_course_from_shop_page' );
@@ -2783,11 +2630,11 @@
2783 2630 $hide_course_from_shop_page = (bool) get_tutor_option( 'hide_course_from_shop_page' );
2784 2631 if ( ! $hide_course_from_shop_page ) {
2785 2632 return;
2786 2633 }
2787 - add_filter( 'woocommerce_product_query', array( $this, 'filter_woocommerce_product_query' ) );
2788 - add_filter( 'edd_downloads_query', array( $this, 'filter_edd_downloads_query' ), 10 );
2789 - 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 );
2790 2637 }
2791 2638
2792 2639
2793 2640 /**
@@ -2793,9 +2640,8 @@
2793 2640 /**
2794 2641 * Tutor product meta query
2795 2642 *
2796 2643 * @since 1.4.9
2797 - *
2798 2644 * @return array
2799 2645 */
2800 2646 public function tutor_product_meta_query() {
2801 2647 $meta_query = array(
@@ -2810,9 +2656,8 @@
2810 2656 *
2811 2657 * @since 1.4.9
2812 2658 *
2813 2659 * @param \WP_Query $wp_query WP Query instance.
2814 - *
2815 2660 * @return \WP_Query
2816 2661 */
2817 2662 public function filter_woocommerce_product_query( $wp_query ) {
2818 2663 $product_ids = $this->get_connected_wc_product_ids();
@@ -2854,9 +2699,8 @@
2854 2699 *
2855 2700 * @since 1.4.9
2856 2701 *
2857 2702 * @param \WP_Query $query WP Query instance.
2858 - *
2859 2703 * @return \WP_Query
2860 2704 */
2861 2705 public function filter_edd_downloads_query( $query ) {
2862 2706 $query['meta_query'][] = $this->tutor_product_meta_query();
@@ -2868,9 +2712,8 @@
2868 2712 *
2869 2713 * @since 1.4.9
2870 2714 *
2871 2715 * @param \WP_Query $wp_query WP Query instance.
2872 - *
2873 2716 * @return \WP_Query
2874 2717 */
2875 2718 public function filter_archive_meta_query( $wp_query ) {
2876 2719 if ( ! is_admin() && $wp_query->is_archive && $wp_query->get( 'post_type' ) === 'download' ) {
@@ -2884,9 +2727,8 @@
2884 2727 *
2885 2728 * @since 1.5.8
2886 2729 *
2887 2730 * @param string $html HTML string.
2888 - *
2889 2731 * @return string
2890 2732 */
2891 2733 public function remove_price_if_enrolled( $html ) {
2892 2734 $should_removed = apply_filters( 'should_remove_price_if_enrolled', true );
@@ -2892,9 +2734,9 @@
2892 2734 $should_removed = apply_filters( 'should_remove_price_if_enrolled', true );
2893 2735
2894 2736 if ( $should_removed ) {
2895 2737 $course_id = get_the_ID();
2896 - $enrolled = EnrollmentModel::is_enrolled( $course_id );
2738 + $enrolled = tutor_utils()->is_enrolled( $course_id );
2897 2739 if ( $enrolled ) {
2898 2740 $html = '';
2899 2741 }
2900 2742 }
@@ -2901,119 +2743,112 @@
2901 2743 return $html;
2902 2744 }
2903 2745
2904 2746 /**
2905 - * Get course completion missing requirements message.
2747 + * Check if all lessons and quizzes done before mark course complete.
2906 2748 *
2907 - * @since 4.0.0
2749 + * @since 1.5.8
2908 2750 *
2909 - * @param int $course_id Course ID.
2910 - * @param int $user_id User ID.
2911 - *
2912 - * @return string|null
2751 + * @param string $html HTML string.
2752 + * @return string
2913 2753 */
2914 - public static function get_course_completion_restrict_msg( $course_id = 0, $user_id = 0 ) {
2915 - $course_id = tutor_utils()->get_post_id( $course_id );
2916 - $user_id = tutor_utils()->get_user_id( $user_id );
2754 + public function tutor_lms_hide_course_complete_btn( $html ) {
2917 2755
2918 - if ( 'strict' !== tutor_utils()->get_option( 'course_completion_process' ) ) {
2919 - return null;
2756 + $completion_mode = tutor_utils()->get_option( 'course_completion_process' );
2757 + if ( 'strict' !== $completion_mode ) {
2758 + return $html;
2920 2759 }
2921 2760
2922 - $completed_lessons = tutor_utils()->get_completed_lesson_count_by_course( $course_id, $user_id );
2923 - $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();
2924 2763
2925 - if ( $completed_lessons < $total_lessons ) {
2926 - 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>';
2927 2771 }
2928 2772
2929 - $course_contents = tutor_utils()->get_course_contents_by_id( $course_id );
2773 + $quizzes = array();
2774 + $assignments = array();
2930 2775
2931 - $required_quiz_pass_count = 0;
2932 - $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 + }
2933 2787
2934 - $is_assignment_addon_enabled = tutor_utils()->is_addon_enabled( 'tutor-assignments' );
2788 + $required_assignment_pass = 0;
2935 2789
2936 - foreach ( $course_contents as $content ) {
2790 + foreach ( $assignments as $row ) {
2937 2791
2938 - if ( tutor()->quiz_post_type === $content->post_type ) {
2939 - if ( ! QuizModel::is_quiz_passed( $content->ID, $user_id ) ) {
2940 - ++$required_quiz_pass_count;
2941 - }
2942 - }
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 );
2943 2796
2944 - if ( $is_assignment_addon_enabled && tutor()->assignment_post_type === $content->post_type ) {
2945 - if ( ! Assignments::is_assignment_passed( $content->ID, $user_id ) ) {
2946 - ++$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++;
2947 2803 }
2804 + } else {
2805 + $required_assignment_pass++;
2948 2806 }
2949 2807 }
2950 2808
2951 - if ( ! $required_quiz_pass_count && ! $required_assignment_pass_count ) {
2952 - return null;
2953 - }
2809 + $is_quiz_pass = true;
2810 + $required_quiz_pass = 0;
2954 2811
2955 - return self::get_course_completion_requirement_message(
2956 - $required_quiz_pass_count,
2957 - $required_assignment_pass_count
2958 - );
2959 - }
2812 + if ( tutor_utils()->count( $quizzes ) ) {
2813 + foreach ( $quizzes as $quiz ) {
2960 2814
2961 - /**
2962 - * Build missing course completion requirements message.
2963 - *
2964 - * @since 4.0.0
2965 - *
2966 - * @param int $quiz_count Total quiz count.
2967 - * @param int $assignment_count Total assignment count.
2968 - *
2969 - * @return string
2970 - */
2971 - private static function get_course_completion_requirement_message( $quiz_count, $assignment_count ) {
2972 - $quiz_label = _n( 'quiz', 'quizzes', $quiz_count, 'tutor' );
2973 - $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 );
2974 2819
2975 - if ( $quiz_count && ! $assignment_count ) {
2976 - return sprintf(
2977 - /* translators: %1$s: item count; %2$s: item label */
2978 - __( 'You have to pass %1$s %2$s to complete this course.', 'tutor' ),
2979 - $quiz_count,
2980 - $quiz_label
2981 - );
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 + }
2982 2829 }
2983 2830
2984 - if ( ! $quiz_count && $assignment_count ) {
2985 - return sprintf(
2986 - /* translators: %1$s: item count; %2$s: item label */
2987 - __( 'You have to pass %1$s %2$s to complete this course.', 'tutor' ),
2988 - $assignment_count,
2989 - $assignment_label
2990 - );
2991 - }
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' );
2992 2835
2993 - return sprintf(
2994 - /* translators: %1$s: quiz count; %2$s: quiz label; %3$s: assignment count; %4$s: assignment label */
2995 - __( 'You have to pass %1$s %2$s and %3$s %4$s to complete this course.', 'tutor' ),
2996 - $quiz_count,
2997 - $quiz_label,
2998 - $assignment_count,
2999 - $assignment_label
3000 - );
3001 - }
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 + }
3002 2840
3003 - /**
3004 - * Check if all lessons and quizzes done before mark course complete.
3005 - *
3006 - * @since 1.5.8
3007 - *
3008 - * @param string $html HTML string.
3009 - *
3010 - * @return string
3011 - */
3012 - public function tutor_lms_hide_course_complete_btn( $html ) {
3013 - $_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 + }
3014 2845
3015 - 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 +
3016 2851 return '<div class="tutor-alert tutor-warning tutor-mt-28">
3017 2852 <div class="tutor-alert-text">
3018 2853 <span class="tutor-alert-icon tutor-fs-4 tutor-icon-circle-info tutor-mr-12"></span>
3019 2854 <span>' . $_msg . '</span>
@@ -3029,9 +2864,8 @@
3029 2864 *
3030 2865 * @since 1.5.8
3031 2866 *
3032 2867 * @param string $html HTML string.
3033 - *
3034 2868 * @return string
3035 2869 */
3036 2870 public function get_generate_greadbook( $html ) {
3037 2871 if ( ! tutor_utils()->is_completed_course() ) {
@@ -3043,9 +2877,8 @@
3043 2877 /**
3044 2878 * Add social share content in header
3045 2879 *
3046 2880 * @since 1.6.3
3047 - *
3048 2881 * @return void
3049 2882 */
3050 2883 public function social_share_content() {
3051 2884 global $wp_query, $post;
@@ -3069,9 +2902,8 @@
3069 2902 *
3070 2903 * @since 1.8.2
3071 2904 *
3072 2905 * @param integer $post_id post ID.
3073 - *
3074 2906 * @return void
3075 2907 */
3076 2908 public function delete_associated_enrollment( $post_id ) {
3077 2909 global $wpdb;
@@ -3082,12 +2914,11 @@
3082 2914 post_id
3083 2915 FROM
3084 2916 {$wpdb->postmeta}
3085 2917 WHERE
3086 - meta_key=%s
2918 + meta_key='_tutor_enrolled_by_order_id'
3087 2919 AND meta_value = %d
3088 2920 ",
3089 - EnrollmentModel::ENROLLMENT_ORDER_ID_META,
3090 2921 $post_id
3091 2922 )
3092 2923 );
3093 2924
@@ -3103,35 +2934,21 @@
3103 2934 /**
3104 2935 * Reset course progress.
3105 2936 *
3106 2937 * @since 1.5.8
3107 - *
3108 2938 * @return void
3109 2939 */
3110 2940 public function tutor_reset_course_progress() {
3111 2941 tutor_utils()->checking_nonce();
3112 - $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
3113 - $context = Input::post( 'context', '' );
3114 - $course_reset_progress = tutor_utils()->get_option( 'course_reset_progress', false );
3115 - $course_retake_feature = tutor_utils()->get_option( 'course_retake_feature', false );
2942 + $course_id = Input::post( 'course_id' );
3116 2943
3117 - if ( ! $course_reset_progress && 'learning-area-sidebar' === $context ) {
3118 - $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' ) ) );
3119 2946 return;
3120 2947 }
3121 2948
3122 - if ( ! $course_retake_feature && ( 'course-landing' === $context || 'learning-area' === $context ) ) {
3123 - $this->response_bad_request( __( 'You do not have permission to retake this course.', 'tutor' ) );
3124 - return;
3125 - }
3126 -
3127 - if ( ! $course_id || ! is_numeric( $course_id ) || ! EnrollmentModel::is_enrolled( $course_id ) ) {
3128 - $this->response_bad_request( __( 'Invalid Course ID or Access Denied.', 'tutor' ) );
3129 - return;
3130 - }
3131 -
3132 2949 tutor_utils()->delete_course_progress( $course_id );
3133 - $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 ) ) );
3134 2951 }
3135 2952
3136 2953 /**
3137 2954 * Do enroll if guest attempt to enroll and course is free
@@ -3139,9 +2956,9 @@
3139 2956 * @since 1.9.8
3140 2957 *
3141 2958 * @param integer $course_id course ID.
3142 2959 * @param integer $user_id user ID.
3143 - *
2960 +
3144 2961 * @return void
3145 2962 */
3146 2963 public function enroll_after_login_if_attempt( int $course_id, int $user_id ) {
3147 2964 $course_id = sanitize_text_field( $course_id );
@@ -3149,9 +2966,9 @@
3149 2966
3150 2967 if ( $course_id && $is_allowed ) {
3151 2968 $is_purchasable = tutor_utils()->is_course_purchasable( $course_id );
3152 2969 if ( ! $is_purchasable ) {
3153 - EnrollmentModel::do_enroll( $course_id, $order_id = 0, $user_id );
2970 + tutor_utils()->do_enroll( $course_id, $order_id = 0, $user_id );
3154 2971 do_action( 'guest_attempt_after_enrollment', $course_id );
3155 2972 }
3156 2973 }
3157 2974 }
@@ -3159,9 +2976,8 @@
3159 2976 /**
3160 2977 * Handle course enrollment
3161 2978 *
3162 2979 * @since 2.1.0
3163 - *
3164 2980 * @return void
3165 2981 */
3166 2982 public function course_enrollment() {
3167 2983 tutor_utils()->checking_nonce();
@@ -3168,46 +2984,21 @@
3168 2984
3169 2985 $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
3170 2986 $user_id = get_current_user_id();
3171 2987
3172 - if ( ! $course_id || ! $user_id ) {
3173 - wp_send_json_error( tutor_utils()->error_message( 'invalid_req' ) );
3174 - }
3175 -
3176 - $password_protected = post_password_required( $course_id );
3177 - if ( $password_protected ) {
3178 - wp_send_json_error( __( 'This course is password protected', 'tutor' ) );
3179 - }
3180 -
3181 - $course = get_post( $course_id );
3182 -
3183 - if ( CourseModel::STATUS_PRIVATE === $course->post_status && ! current_user_can( 'read_private_tutor_courses' ) ) {
3184 - wp_send_json_error( __( 'You do not have permission to enroll in this course', 'tutor' ) );
3185 - }
3186 -
3187 - /**
3188 - * This check was added to address a security issue where users could
3189 - * enroll in a course via an AJAX call without purchasing it.
3190 - *
3191 - * To prevent this, we now verify whether the course is paid.
3192 - * Additionally, we check if the user is already enrolled, since
3193 - * Tutor's default behavior enrolls users automatically upon purchase.
3194 - *
3195 - * @since 3.9.4
3196 - */
3197 - if ( tutor_utils()->is_course_purchasable( $course_id ) ) {
3198 - $is_enrolled = (bool) EnrollmentModel::is_enrolled( $course_id, $user_id );
3199 -
3200 - if ( ! $is_enrolled ) {
3201 - 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' ) );
3202 2992 }
3203 - }
3204 -
3205 - $enroll = EnrollmentModel::do_enroll( $course_id, 0, $user_id );
3206 - if ( $enroll ) {
3207 - 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 + }
3208 2999 } else {
3209 - wp_send_json_error( __( 'Enrollment failed, please try again!', 'tutor' ) );
3000 + wp_send_json_error( __( 'Invalid course ID', 'tutor' ) );
3210 3001 }
3211 3002 }
3212 3003
3213 3004 /**
@@ -3294,10 +3085,14 @@
3294 3085 'regular_price' => $regular_price,
3295 3086 'sale_price' => $sale_price,
3296 3087 );
3297 3088
3298 - $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 + }
3299 3092
3093 + $card_data = apply_filters( 'tutor_add_course_plan_info', $info, $post );
3094 +
3300 3095 return $card_data;
3301 3096 }
3302 3097
3303 3098 /**
@@ -3318,9 +3113,9 @@
3318 3113 $info['last_updated'] = tutor_i18n_get_formated_date( $post->post_modified_at );
3319 3114 $info['course_duration'] = tutor_utils()->get_course_duration( $post->ID, false );
3320 3115 $info['total_enrolled'] = tutor_utils()->count_enrolled_users_by_course( $post->ID );
3321 3116
3322 - $card_data = apply_filters( 'tutor_course_card_data', $info, $post );
3117 + $card_data = apply_filters( 'tutor_add_course_plan_info', $info, $post );
3323 3118
3324 3119 return $card_data;
3325 3120 }
3326 3121
@@ -3377,199 +3172,6 @@
3377 3172 'trash',
3378 3173 'pending',
3379 3174 'future',
3380 3175 );
3381 - }
3382 -
3383 - /**
3384 - * Link a course/bundle post to a WooCommerce product.
3385 - *
3386 - * @since 3.8.2
3387 - *
3388 - * @param int $post_ID The WordPress post ID of the course.
3389 - * @param int $product_id The WooCommerce product ID to associate with the course.
3390 - *
3391 - * @return void
3392 - */
3393 - public static function sync_course_with_wc_product( $post_ID, $product_id ) {
3394 -
3395 - update_post_meta( $post_ID, self::COURSE_PRODUCT_ID_META, $product_id );
3396 -
3397 - // Mark product for woocommerce.
3398 - update_post_meta( $product_id, '_virtual', 'yes' );
3399 - update_post_meta( $product_id, '_tutor_product', 'yes' );
3400 - }
3401 -
3402 - /**
3403 - * Map Tutor's course prices to WooCommerce.
3404 - *
3405 - * @since 3.8.2
3406 - *
3407 - * @param int $post_ID The WordPress post ID of the course.
3408 - * @param string|int|float $regular_price The regular price.
3409 - * @param string|int|float $sale_price The sale price.
3410 - * @return void
3411 - */
3412 - private static function set_course_regular_and_sale_price( $post_ID, $regular_price, $sale_price ) {
3413 -
3414 - // Set course regular & sale price.
3415 - update_post_meta( $post_ID, self::COURSE_PRICE_META, $regular_price );
3416 - update_post_meta( $post_ID, self::COURSE_SALE_PRICE_META, $sale_price );
3417 - }
3418 -
3419 - /**
3420 - * Render start/resume button in enrolled course action btn.
3421 - *
3422 - * @since 4.0.0
3423 - *
3424 - * @param int $course_id course id.
3425 - *
3426 - * @return void
3427 - */
3428 - public function render_course_action_btn( int $course_id ) {
3429 - $is_completed = tutor_utils()->is_completed_course( $course_id );
3430 - if ( $is_completed ) {
3431 - $certificate_addon_active = tutor_utils()->is_addon_enabled( 'tutor-certificate' );
3432 - if ( ! $certificate_addon_active ) {
3433 - Button::make()
3434 - ->tag( 'a' )
3435 - ->label( __( 'Completed', 'tutor' ) )
3436 - ->icon( Icon::COMPLETED_CIRCLE )
3437 - ->variant( Variant::PRIMARY )
3438 - ->size( Size::X_SMALL )
3439 - ->attr( 'href', esc_url( get_the_permalink( $course_id ) ) )
3440 - ->render();
3441 - }
3442 - return;
3443 - }
3444 -
3445 - $course_progress = tutor_utils()->get_course_completed_percent( $course_id, 0, true );
3446 - $button_text = $course_progress['completed_percent'] > 0 ? __( 'Resume', 'tutor' ) : __( 'Start', 'tutor' );
3447 - $button_url = tutor_utils()->get_course_first_lesson( $course_id );
3448 -
3449 - if ( ! $button_url ) {
3450 - $button_url = get_the_permalink( $course_id );
3451 - }
3452 -
3453 - Button::make()
3454 - ->tag( 'a' )
3455 - ->label( $button_text )
3456 - ->icon( Icon::PLAY_2 )
3457 - ->variant( Variant::PRIMARY )
3458 - ->size( Size::X_SMALL )
3459 - ->attr( 'href', esc_url( $button_url ) )
3460 - ->render();
3461 - }
3462 -
3463 - /**
3464 - * Get the content for the course completion modal.
3465 - *
3466 - * This method returns HTML for displaying a modal that informs the user
3467 - * that they have not completed all required lessons and assessments. It includes
3468 - * the user's current progress shown as a percentage and a progress bar.
3469 - *
3470 - * @since 4.0.0
3471 - *
3472 - * @param float $course_progress The completion percentage of the course.
3473 - *
3474 - * @return string The rendered HTML content for the modal.
3475 - */
3476 - public static function get_complete_modal_content( float $course_progress = 0 ): string {
3477 - ob_start();
3478 - ?>
3479 - <div>
3480 - <p class="tutor-p3 tutor-text-secondary tutor-text-center tutor-mb-7 tutor-px-11">
3481 - <?php esc_html_e( 'You have not completed all required lessons and assessments. ', 'tutor' ); ?>
3482 - </p>
3483 - <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">
3484 - <div class="tutor-flex tutor-items-center tutor-justify-between">
3485 - <div class="tutor-p3 tutor-text-secondary">
3486 - <?php esc_html_e( 'Your Progress', 'tutor' ); ?>
3487 - </div>
3488 - <div class="tutor-p1 tutor-font-bold">
3489 - <?php echo esc_html( number_format_i18n( $course_progress ) . '%' ); ?>
3490 - </div>
3491 - </div>
3492 - <?php
3493 - Progress::make()->type( 'bar' )->value( $course_progress )->render();
3494 - ?>
3495 - </div>
3496 - </div>
3497 - <?php
3498 - return ob_get_clean();
3499 - }
3500 -
3501 - /**
3502 - * Render the course complete button.
3503 - *
3504 - * Displays a button that allows the user to complete the course. If the course
3505 - * progress is less than 100%, clicking the button shows a modal informing the user
3506 - * that not all requirements are fulfilled. If the course progress is 100%,
3507 - * clicking the button attempts to complete the course.
3508 - *
3509 - * @since 4.0.0
3510 - *
3511 - * @param string $modal_id The HTML ID of the modal to display if not complete.
3512 - * @param int $course_id The ID of the course.
3513 - * @param float $course_progress The current completion percentage of the course.
3514 - * @param string $size The button size.
3515 - * @param string $tooltip Optional. Tooltip message.
3516 - * @param bool $block Optional. Whether the button is full-width.
3517 - *
3518 - * @return void
3519 - */
3520 - 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 {
3521 - $button = Button::make()
3522 - ->variant( Variant::SECONDARY )
3523 - ->label( __( 'Complete the Course', 'tutor' ) )
3524 - ->icon( Icon::TICK_MARK )
3525 - ->size( $size )
3526 - ->block( $block )
3527 - ->attr( 'type', 'button' );
3528 -
3529 - if ( ! empty( $tooltip ) ) {
3530 - $button->disabled();
3531 - }
3532 -
3533 - if ( $course_progress < 100 ) {
3534 - $button->attr( '@click', "TutorCore.modal.showModal('{$modal_id}')" );
3535 - } else {
3536 - $button->attr( '@click', "handleCourseComplete({$course_id})" );
3537 - $button->attr( ':class', "courseCompleteMutation?.isPending ? 'tutor-btn-loading' : ''" );
3538 - $button->attr( ':disabled', 'courseCompleteMutation?.isPending' );
3539 - }
3540 -
3541 - if ( ! empty( $tooltip ) ) {
3542 - Tooltip::make()
3543 - ->content( $tooltip )
3544 - ->placement( Tooltip::PLACEMENT_BOTTOM )
3545 - ->arrow( Tooltip::ARROW_CENTER )
3546 - ->trigger_element( $button->get() )
3547 - ->render();
3548 - } else {
3549 - $button->render();
3550 - }
3551 - }
3552 -
3553 - /**
3554 - * Render course retake button
3555 - *
3556 - * @since 4.0.0
3557 - *
3558 - * @param string $modal_id Modal id.
3559 - * @param string $size The button size.
3560 - * @param bool $block Optional. Whether the button is full-width.
3561 - *
3562 - * @return void
3563 - */
3564 - public static function render_course_retake_btn( string $modal_id, string $size = Size::MEDIUM, bool $block = false ): void {
3565 - Button::make()
3566 - ->variant( Variant::SECONDARY )
3567 - ->label( __( 'Retake this Course', 'tutor' ) )
3568 - ->icon( Icon::RELOAD_4 )
3569 - ->size( $size )
3570 - ->block( $block )
3571 - ->attr( 'type', 'button' )
3572 - ->attr( '@click', "TutorCore.modal.showModal('{$modal_id}')" )
3573 - ->render();
3574 3176 }
3575 3177 }