Skip to content

Commit ed0372b

Browse files
authored
Merge pull request #2676 from themeum/validation_fixes
Added backend validation for student activities in course
2 parents b207794 + 2daab92 commit ed0372b

8 files changed

Lines changed: 73 additions & 9 deletions

File tree

assets/src/js/front/course/_wishlist.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ window.jQuery(document).ready(($) => {
3030
$that.blur();
3131
}
3232
} else {
33-
tutor_toast(__('Error', 'tutor-pro'), data?.data?.message ?? 'Something went wrong!!', 'error');
33+
if ( data?.data?.message ) {
34+
tutor_toast(__('Error', 'tutor-pro'), data?.data?.message, 'error');
35+
}
3436
$('.tutor-login-modal').addClass('tutor-is-active');
3537
$that.blur();
3638
}

assets/src/js/front/pages/course-landing.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ window.jQuery(document).ready(($) => {
3030
button.prop('disabled', true).addClass('is-loading');
3131
},
3232
success: function (response) {
33-
if (response.success) {
33+
if (response.status_code === 200 ) {
3434
window.location.assign(response.data.redirect_to);
35-
} else {
36-
alert((response.data || {}).message || __('Something went wrong', 'tutor'));
3735
}
3836
},
37+
error: function(response) {
38+
tutor_toast(__( 'Error', 'tutor' ),(response.responseJSON || {}).message ||__('Something went wrong', 'tutor'));
39+
},
3940
complete: function () {
4041
button.prop('disabled', false).removeClass('is-loading');
4142
}

assets/src/js/frontend/learning-area/common.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { wpAjaxInstance } from '@TutorShared/utils/api';
33
import endpoints from '@TutorShared/utils/endpoints';
44
import { convertToErrorMessage } from '@TutorShared/utils/util';
55
import { __ } from '@wordpress/i18n';
6+
import { type AxiosError } from 'axios';
67

78
interface CourseCompletePayload {
89
course_id: number;
@@ -32,8 +33,11 @@ export const courseCompleteHandler = () => {
3233
modal.closeModal('tutor-course-complete-modal');
3334
window.location.reload();
3435
},
35-
onError: (error: Error) => {
36+
onError: (error: AxiosError) => {
3637
toast.error(convertToErrorMessage(error));
38+
if (!error || !error.response || !error.response.data) {
39+
window.location.reload();
40+
}
3741
},
3842
});
3943

assets/src/js/frontend/learning-area/sidebar.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { type MutationState } from '@Core/ts/services/Query';
88
import { wpAjaxInstance } from '@TutorShared/utils/api';
99
import endpoints from '@TutorShared/utils/endpoints';
1010
import { convertToErrorMessage } from '@TutorShared/utils/util';
11+
import { type AxiosError } from 'axios';
1112

1213
interface ResetProgressPayload {
1314
course_id: number;
@@ -57,8 +58,11 @@ export const sidebarComponent = ({
5758
window.location.href = response.data.redirect_to;
5859
}
5960
},
60-
onError: (error) => {
61+
onError: (error: AxiosError) => {
6162
toast.error(convertToErrorMessage(error));
63+
if (!error || !error.response || !error.response.data) {
64+
window.location.reload();
65+
}
6266
},
6367
},
6468
);

classes/Course.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Tutor\Helpers\HttpHelper;
2424
use Tutor\Models\CourseModel;
2525
use Tutor\Ecommerce\Ecommerce;
26+
use Tutor\Helpers\DateTimeHelper;
2627
use Tutor\Traits\JsonResponse;
2728
use Tutor\Helpers\ValidationHelper;
2829
use Tutor\Models\EnrollmentModel;
@@ -3079,15 +3080,22 @@ public function delete_associated_enrollment( $post_id ) {
30793080
*/
30803081
public function tutor_reset_course_progress() {
30813082
tutor_utils()->checking_nonce();
3082-
$course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
3083+
$course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
3084+
$course_reset_progress = tutor_utils()->get_option( 'course_reset_progress', false );
3085+
$course_retake_feature = tutor_utils()->get_option( 'course_retake_feature', false );
3086+
3087+
if ( ! $course_reset_progress || ! $course_retake_feature ) {
3088+
$this->response_bad_request( __( 'You are not allowed to reset course progress.', 'tutor' ) );
3089+
return;
3090+
}
30833091

30843092
if ( ! $course_id || ! is_numeric( $course_id ) || ! EnrollmentModel::is_enrolled( $course_id ) ) {
3085-
wp_send_json_error( array( 'message' => __( 'Invalid Course ID or Access Denied.', 'tutor' ) ) );
3093+
$this->response_bad_request( __( 'Invalid Course ID or Access Denied.', 'tutor' ) );
30863094
return;
30873095
}
30883096

30893097
tutor_utils()->delete_course_progress( $course_id );
3090-
wp_send_json_success( array( 'redirect_to' => tutor_utils()->get_course_first_lesson( $course_id ) ) );
3098+
$this->json_response( '', array( 'redirect_to' => tutor_utils()->get_course_first_lesson( $course_id ) ) );
30913099
}
30923100

30933101
/**

ecommerce/CartController.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ public function add_course_to_cart() {
231231
);
232232
}
233233

234+
$can_buy = apply_filters( 'tutor_allow_course_enrollment', true, $course_id );
235+
if ( ! $can_buy ) {
236+
$this->json_response(
237+
__( 'Failed to add to cart.', 'tutor' ),
238+
null,
239+
HttpHelper::STATUS_BAD_REQUEST
240+
);
241+
}
242+
234243
// Check if the course already exists in the cart or not.
235244
$is_course_in_user_cart = $this->model->is_course_in_user_cart( $user_id, $course_id );
236245
if ( $is_course_in_user_cart ) {

ecommerce/CheckoutController.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,26 @@ public function pay_now() {
624624
}
625625
}
626626

627+
if ( isset( $request['object_ids'] ) ) {
628+
$course_ids = explode( ',', $request['object_ids'] );
629+
630+
if ( tutor_utils()->count( $course_ids ) ) {
631+
foreach ( $course_ids as $course_id ) {
632+
$can_buy = apply_filters( 'tutor_allow_course_enrollment', true, $course_id );
633+
if ( ! $can_buy ) {
634+
array_push(
635+
$errors,
636+
sprintf(
637+
// Translators: %s course name.
638+
__( ' Course %s cannot be enrolled right now.', 'tutor' )
639+
),
640+
get_the_title( $course_id ) ?? ''
641+
);
642+
}
643+
}
644+
}
645+
}
646+
627647
$validate_consent = LegalConsent::validate_consent( LegalConsent::DISPLAY_ON_CHECKOUT, $_POST );
628648
if ( is_wp_error( $validate_consent ) ) {
629649
array_push( $errors, $validate_consent->get_error_message() );
@@ -1040,6 +1060,17 @@ public function restrict_checkout_page() {
10401060
return;
10411061
}
10421062

1063+
$course_id = Input::get( 'course_id', 0, Input::TYPE_INT );
1064+
1065+
if ( $course_id ) {
1066+
$can_buy = apply_filters( 'tutor_allow_course_enrollment', true, $course_id );
1067+
$course_url = get_post_permalink( $course_id );
1068+
if ( ! $can_buy ) {
1069+
wp_safe_redirect( tutor_utils()->get_nocache_url( $course_url ) );
1070+
exit;
1071+
}
1072+
}
1073+
10431074
$cart_page_url = CartController::get_page_url();
10441075

10451076
if ( ! is_user_logged_in() && ! apply_filters( 'tutor_is_guest_checkout_enabled', false ) ) {

models/EnrollmentModel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public static function do_enroll( $course_id = 0, $order_id = 0, $user_id = 0, $
7272
return $enrolled_id;
7373
}
7474

75+
$can_enroll = apply_filters( 'tutor_allow_course_enrollment', true, $course_id );
76+
if ( ! $can_enroll ) {
77+
return $enrolled_id;
78+
}
79+
7580
$fire_hook ? do_action( 'tutor_before_enroll', $course_id ) : null;
7681
$user_id = tutor_utils()->get_user_id( $user_id );
7782
$title = __( 'Course Enrolled', 'tutor' ) . ' – ' . gmdate( get_option( 'date_format' ) ) . ' @ ' . gmdate( get_option( 'time_format' ) );

0 commit comments

Comments
 (0)