PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.2
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / class-lp-ajax.php

class-lp-ajax.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.2, at inc/class-lp-ajax.php

279 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use LearnPress\Helpers\Template;
4 use LearnPress\Models\CourseModel;
5 use LearnPress\Models\UserItems\UserCourseModel;
6 use LearnPress\Models\UserItems\UserItemModel;
7 use LearnPress\Models\UserModel;
8
9 defined( 'ABSPATH' ) || exit;
10
11 if ( ! class_exists( 'LP_AJAX' ) ) {
12 class LP_AJAX {
13 /**
14 * Init common ajax events
15 */
16 public static function init() {
17 $ajax_events = array(
18 'checkout-user-email-exists:nopriv',
19 'recover-order',
20 'request-become-a-teacher:nonce',
21 'checkout:nopriv',
22 //'complete-lesson',
23 'finish-course', // finish_course.
24 'external-link:nopriv',
25 );
26
27 $ajax_events = apply_filters( 'learn-press/ajax/events', $ajax_events );
28
29 foreach ( $ajax_events as $action => $callback ) {
30
31 if ( is_numeric( $action ) ) {
32 $action = $callback;
33 }
34
35 $actions = LP_Request::parse_action( $action );
36 $method = $actions['action'];
37
38 if ( ! is_callable( $callback ) ) {
39 $method = preg_replace( '/-/', '_', $method );
40 $callback = array( __CLASS__, $method );
41 }
42
43 LP_Request::register_ajax( $action, $callback );
44 }
45 }
46
47 public static function external_link() {
48 $nonce = LP_Request::get( 'nonce' );
49 $id = LP_Request::get( 'id' );
50 $course = learn_press_get_course( $id );
51
52 if ( ! $course ) {
53 return;
54 }
55
56 $link = $course->get_external_link();
57
58 if ( ! wp_verify_nonce( $nonce, 'external-link-' . $link ) ) {
59 return;
60 }
61
62 if ( apply_filters( 'learn-press/course-redirect-external-link', $id ) ) {
63 wp_redirect( $link );
64 exit();
65 }
66 }
67
68 public static function checkout() {
69 LearnPress::instance()->checkout()->process_checkout_handler();
70 }
71
72 public static function request_become_a_teacher() {
73 LP_Forms_Handler::process_become_teacher();
74 }
75
76 /**
77 * Recover order
78 *
79 * @since 4.0.0
80 * @version 4.0.1
81 * @return void
82 */
83 public static function recover_order() {
84 $response = new LP_REST_Response();
85
86 if ( ! LP_Request::verify_nonce( 'recover-order' ) ) {
87 return;
88 }
89
90 $factory = new LP_Order_CURD();
91 $user_id = get_current_user_id();
92 $order_key = LP_Request::get_string( 'order-key' );
93 $order = $factory->recover( $order_key, $user_id );
94
95 if ( is_wp_error( $order ) ) {
96 $response->message = $order->get_error_message();
97 } else {
98 $response->status = 'success';
99 $response->message = sprintf(
100 __( 'The order %s has been successfully recovered.', 'learnpress' ),
101 $order_key
102 );
103 $response->data->redirect = $order->get_view_order_url();
104 }
105
106 wp_send_json( $response );
107 }
108
109 public static function checkout_user_email_exists() {
110 $response = new LP_REST_Response();
111
112 try {
113 $email = LP_Request::get_email( 'email' );
114 $user_can_register = get_option( 'users_can_register' );
115 $html_wrapper = [
116 '<label class="lp-guest-checkout-output">' => '</label>',
117 ];
118
119 if ( email_exists( $email ) ) {
120 $output = __(
121 'Your email already exists. Do you want to continue with this email?',
122 'learnpress'
123 );
124 } elseif ( $user_can_register ) {
125 $output = sprintf(
126 '<input type="checkbox" name="checkout-email-option" value="new-account"> <span>%s</span>',
127 __(
128 'Create a new account with this email. The account information will be sent with this email.',
129 'learnpress'
130 )
131 );
132 } else {
133 $output = __( 'The system does not allow the creation of a new account, you must enter an existing account.', 'learnpress' );
134 }
135
136 $response->status = 'success';
137 $response->data->content = Template::instance()->nest_elements( $html_wrapper, $output );
138 } catch ( Throwable $e ) {
139 $response->message = $e->getMessage();
140 }
141
142 wp_send_json( $response );
143 }
144
145 /**
146 * Request finish course
147 *
148 * TODO: should move this function to api - tungnx
149 */
150 public static function finish_course() {
151 $link_redirect = '';
152 $message_data = [
153 'status' => 'error',
154 'content' => '',
155 ];
156
157 try {
158 $nonce = LP_Request::get_param( 'finish-course-nonce', '', 'text', 'post' );
159 $course_id = LP_Request::get_param( 'course-id', 0, 'int', 'post' );
160 $courseModel = CourseModel::find( $course_id, true );
161 $user_id = get_current_user_id();
162 $userModel = UserModel::find( $user_id, true );
163
164 $nonce_action = sprintf( 'finish-course-%d-%d', $course_id, $user_id );
165 if ( ! $courseModel || ! $userModel || ! wp_verify_nonce( $nonce, $nonce_action ) ) {
166 throw new Exception( __( 'Request is invalid!', 'learnpress' ) );
167 }
168
169 $userCourseModel = UserCourseModel::find( $user_id, $course_id, true );
170 if ( ! $userCourseModel instanceof UserCourseModel ) {
171 throw new Exception( __( 'You have not enrolled in this course.', 'learnpress' ) );
172 }
173
174 $can_finish = $userCourseModel->can_finish();
175 if ( is_wp_error( $can_finish ) ) {
176 throw new Exception( $can_finish->get_error_message() );
177 }
178
179 $userCourseModel->handle_finish();
180 $lp_redirect = LP_Settings::get_option( 'course_finish_redirect' );
181 $link_redirect = ! empty( $lp_redirect ) ? $lp_redirect : $courseModel->get_permalink();
182 $link_redirect = esc_url_raw( $link_redirect );
183 $message_data['status'] = 'success';
184 $message_data['content'] = __( 'Course has been finished successfully.', 'learnpress' );
185 } catch ( Throwable $e ) {
186 $message_data['content'] = $e->getMessage();
187 }
188
189 learn_press_set_message( $message_data );
190 if ( ! empty( $link_redirect ) ) {
191 wp_redirect( $link_redirect );
192 die();
193 }
194 }
195
196 /**
197 * Complete lesson
198 * @deprecated 4.2.7.6
199 */
200 /*public static function complete_lesson() {
201 $response = array(
202 'result' => 'error',
203 'redirect' => '',
204 );
205
206 try {
207 $nonce = LP_Request::get_param( 'complete-lesson-nonce' );
208 $lesson_id = LP_Request::get_param( 'id', 0, 'int' );
209 $course_id = LP_Request::get_param( 'course_id', 0, 'int' );
210 $item_id_next = LP_Request::get_param( 'item_id_next', 0, 'int' );
211
212 if ( ! wp_verify_nonce( $nonce, 'lesson-complete' ) ) {
213 throw new Exception( __( 'Error! Invalid lesson or failed security check.', 'learnpress' ) );
214 }
215
216 $lesson = get_post( $lesson_id );
217 if ( ! $lesson || $lesson->post_type !== LP_LESSON_CPT ) {
218 throw new Exception( __( 'Error! Invalid lesson.', 'learnpress' ) );
219 }
220
221 $user = learn_press_get_current_user();
222 if ( $user instanceof LP_User_Guest ) {
223 throw new Exception( __( 'Please login.', 'learnpress' ) );
224 }
225
226 $course = learn_press_get_course( $course_id );
227 if ( ! $course ) {
228 throw new Exception( __( 'Course is invalid!.', 'learnpress' ) );
229 }
230
231 $item = $course->get_item( $lesson_id );
232 if ( ! $item instanceof LP_Course_Item ) {
233 throw new Exception( 'Item is invalid!', 'learnpress' );
234 }
235
236 $result = $user->complete_lesson( $lesson_id, $course_id );
237 if ( $item_id_next ) {
238 $response['redirect'] = $course->get_item_link( $item_id_next );
239 } else {
240 $response['redirect'] = $item->get_permalink();
241 }
242
243 if ( ! is_wp_error( $result ) ) {
244 if ( $course->get_next_item() ) {
245 $next = $course->get_next_item();
246 $response['redirect'] = $course->get_item_link( $next );
247 }
248
249 $message_data = [
250 'status' => 'success',
251 'content' => sprintf( __( 'Congrats! You have completed "%s".', 'learnpress' ), $item->get_title() ),
252 ];
253 learn_press_set_message( $message_data );
254 $response['result'] = 'success';
255 } else {
256 throw new Exception( $result->get_error_message() );
257 }
258
259 $response = apply_filters( 'learn-press/user-completed-lesson-result', $response, $lesson_id, $course_id, $user->get_id() );
260 } catch ( Throwable $e ) {
261 $message_data = [
262 'status' => 'error',
263 'content' => $e->getMessage(),
264 ];
265 learn_press_set_message( $message_data );
266 }
267
268 //learn_press_maybe_send_json( $response );
269
270 if ( ! empty( $response['redirect'] ) ) {
271 wp_redirect( $response['redirect'] );
272 exit();
273 }
274 }*/
275 }
276 }
277
278 LP_AJAX::init();
279