PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.2.0
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.2.0
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 4.2.1 All 138 releases
learnpress / inc / class-lp-forms-handler.php

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

470 lines 14.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class LP_Forms_Handler
4 *
5 * Process action for submitting forms
6 *
7 * @since 4.0.0
8 * @author ThimPress <nhamdv>
9 */
10 class LP_Forms_Handler {
11
12 /**
13 * Become a teacher form
14 */
15 public static function process_become_teacher() {
16 $args = array(
17 'bat_name' => LP_Helper::sanitize_params_submitted( $_POST['bat_name'] ?? '' ),
18 'bat_email' => LP_Helper::sanitize_params_submitted( $_POST['bat_email'] ?? '' ),
19 'bat_phone' => LP_Helper::sanitize_params_submitted( $_POST['bat_phone'] ?? '' ),
20 'bat_message' => LP_Helper::sanitize_params_submitted( $_POST['bat_message'] ?? '' ),
21 );
22
23 $result = array(
24 'message' => array(),
25 'result' => 'success',
26 );
27
28 if ( ( empty( $args['bat_name'] ) ) && $result['result'] !== 'error' ) {
29 $result = array(
30 'message' => learn_press_get_message( __( 'Please enter a valid account username.', 'learnpress' ), 'error' ),
31 'result' => 'error',
32 );
33 }
34
35 if ( ( empty( $args['bat_email'] ) || ! is_email( $args['bat_email'] ) ) && $result['result'] !== 'error' ) {
36 $result = array(
37 'message' => learn_press_get_message( __( 'Please provide a valid email address.', 'learnpress' ), 'error' ),
38 'result' => 'error',
39 );
40 }
41
42 if ( ( ! email_exists( $args['bat_email'] ) ) && $result['result'] !== 'error' ) {
43 $result = array(
44 'message' => learn_press_get_message( __( 'Your email does not exist!', 'learnpress' ), 'error' ),
45 'result' => 'error',
46 );
47 }
48
49 $result = apply_filters( 'learn-press/become-teacher-request-result', $result );
50
51 if ( $result['result'] === 'success' ) {
52 $result['message'][] = learn_press_get_message( __( 'Thank you! Your message has been sent.', 'learnpress' ), 'success' );
53 $user = get_user_by( 'email', $args['bat_email'] );
54
55 update_user_meta( $user->ID, '_requested_become_teacher', 'yes' );
56 do_action( 'learn-press/become-a-teacher-sent', $args );
57 }
58
59 learn_press_maybe_send_json( $result );
60 }
61
62 /**
63 * Process the login form.
64 *
65 * @throws Exception On login error.
66 * @author Thimpress <nhamdv>
67 */
68 public static function process_login() {
69 if ( ! LP_Request::verify_nonce( 'learn-press-login' ) ) {
70 return;
71 }
72
73 if ( isset( $_POST['username'], $_POST['password'] ) ) {
74 try {
75 $username = trim( LP_Helper::sanitize_params_submitted( $_POST['username'] ) );
76 $password = LP_Helper::sanitize_params_submitted( $_POST['password'] );
77 $remember = LP_Request::get_string( 'rememberme' );
78
79 if ( empty( $username ) ) {
80 throw new Exception( '<strong>' . __( 'Error:', 'learnpress' ) . '</strong> ' . __( 'A username is required', 'learnpress' ) );
81 }
82
83 // On multisite, ensure user exists on current site, if not add them before allowing login.
84 if ( is_multisite() ) {
85 $user_data = get_user_by( is_email( $username ) ? 'email' : 'login', $username );
86
87 if ( $user_data && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) {
88 add_user_to_blog( get_current_blog_id(), $user_data->ID, 'customer' );
89 }
90 }
91
92 $user = wp_signon(
93 apply_filters(
94 'learnpress_login_credentials',
95 array(
96 'user_login' => $username,
97 'user_password' => $password,
98 'remember' => $remember,
99 )
100 ),
101 is_ssl()
102 );
103
104 if ( is_wp_error( $user ) ) {
105 throw new Exception( $user->get_error_message() );
106 } else {
107 if ( ! empty( $_POST['redirect'] ) ) {
108 $redirect = LP_Helper::sanitize_params_submitted( $_POST['redirect'] );
109 } elseif ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
110 $redirect = LP_Helper::sanitize_params_submitted( $_REQUEST['_wp_http_referer'] );
111 } else {
112 $redirect = LP_Request::get_redirect( learn_press_get_page_link( 'profile' ) );
113 }
114
115 $message_data = [
116 'status' => 'success',
117 'content' => __( 'Login successfully!', 'learnpress' ),
118 ];
119 learn_press_set_message( $message_data );
120 wp_redirect( wp_validate_redirect( $redirect, learn_press_get_current_url() ) );
121 exit();
122 }
123 } catch ( Exception $e ) {
124 $message_data = [
125 'status' => 'error',
126 'content' => $e->getMessage(),
127 ];
128 learn_press_set_message( $message_data );
129 }
130 }
131 }
132
133 /**
134 * Process register form.
135 *
136 * @throws Exception On Error register.
137 * @author ThimPress <nhamdv>
138 */
139 public static function process_register() {
140 if ( ! LP_Request::verify_nonce( 'learn-press-register' ) ) {
141 return;
142 }
143
144 $username = LP_Helper::sanitize_params_submitted( $_POST['reg_username'] ?? '' );
145 $email = LP_Helper::sanitize_params_submitted( $_POST['reg_email'] ?? '' );
146 $password = LP_Helper::sanitize_params_submitted( $_POST['reg_password'] ?? '' );
147 $confirm_password = LP_Helper::sanitize_params_submitted( $_POST['reg_password2'] ?? '' );
148 $first_name = LP_Helper::sanitize_params_submitted( $_POST['reg_first_name'] ?? '' );
149 $last_name = LP_Helper::sanitize_params_submitted( $_POST['reg_last_name'] ?? '' );
150 $display_name = LP_Helper::sanitize_params_submitted( $_POST['reg_display_name'] ?? '' );
151 $update_meta = LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register_form'] ?? '' );
152
153 try {
154 $new_customer = self::learnpress_create_new_customer(
155 sanitize_email( $email ),
156 $username,
157 $password,
158 $confirm_password,
159 array(
160 'first_name' => $first_name,
161 'last_name' => $last_name,
162 'display_name' => $display_name,
163 ),
164 $update_meta
165 );
166
167 if ( is_wp_error( $new_customer ) ) {
168 throw new Exception( $new_customer->get_error_message() );
169 }
170
171 // Send email become a teacher.
172 $is_become_a_teacher = false;
173 if ( LP_Settings::get_option( 'instructor_registration', 'no' ) == 'yes' && isset( $_POST['become_teacher'] ) ) {
174 update_user_meta( $new_customer, '_requested_become_teacher', 'yes' );
175 do_action(
176 'learn-press/become-a-teacher-sent',
177 array(
178 'bat_email' => $email,
179 'bat_phone' => '',
180 'bat_message' => apply_filters( 'learnpress_become_instructor_message', esc_html__( 'I need to become an instructor', 'learnpress' ) ),
181 )
182 );
183
184 $is_become_a_teacher = true;
185 }
186
187 /**
188 * Auto login user
189 * Must set code below after Send email become a teacher
190 * because 'none' check by "check_ajax_referer" will not valid for send mail background on WP_Async_Request
191 */
192 wp_set_current_user( $new_customer );
193 wp_set_auth_cookie( $new_customer, true );
194
195 $message_success = $username . __( ' was successfully created!', 'learnpress' );
196
197 if ( $is_become_a_teacher ) {
198 $message_success .= '<br/>' . __( 'Your request to become an instructor has been sent. We will get back to you soon!', 'learnpress' );
199 }
200
201 $message_data = [
202 'status' => 'success',
203 'content' => $message_success,
204 ];
205 learn_press_set_message( $message_data );
206
207 if ( ! empty( $_POST['redirect'] ) ) {
208 $redirect = wp_sanitize_redirect( wp_unslash( $_POST['redirect'] ) );
209 } elseif ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
210 $redirect = wp_unslash( $_REQUEST['_wp_http_referer'] );
211 } else {
212 $redirect = LP_Request::get_redirect( learn_press_get_page_link( 'profile' ) );
213 }
214
215 wp_redirect( wp_validate_redirect( $redirect, learn_press_get_current_url() ) );
216 exit();
217
218 } catch ( Throwable $e ) {
219 $message_data = [
220 'status' => 'error',
221 'content' => $e->getMessage(),
222 ];
223 learn_press_set_message( $message_data );
224 }
225 }
226
227 /**
228 * New create customer.
229 *
230 * @author ThimPress <nhamdv>
231 * @version 1.0.1
232 * @since 4.0.0
233 */
234 public static function learnpress_create_new_customer( $email = '', $username = '', $password = '', $confirm_password = '', $args = array(), $update_meta = array() ) {
235 try {
236 if ( empty( $email ) || ! is_email( $email ) ) {
237 throw new Exception( __( 'Please provide a valid email address.', 'learnpress' ), 101 );
238 }
239
240 if ( email_exists( $email ) ) {
241 throw new Exception( __( 'An account is already registered with your email address.', 'learnpress' ), 102 );
242 }
243
244 $username = sanitize_user( $username );
245
246 if ( empty( $username ) || ! validate_username( $username ) ) {
247 throw new Exception( __( 'Please enter a valid account username.', 'learnpress' ), 103 );
248 }
249
250 if ( username_exists( $username ) ) {
251 throw new Exception(
252 __( 'An account is already registered with that username. Please choose another one.', 'learnpress' ),
253 104
254 );
255 }
256
257 if ( apply_filters( 'learnpress_registration_generate_password', false ) ) {
258 $password = wp_generate_password();
259 }
260
261 if ( empty( $password ) ) {
262 throw new Exception( __( 'Please enter an account password.', 'learnpress' ), 105 );
263 }
264
265 if ( strlen( $password ) <= 5 ) {
266 throw new Exception( __( 'Password must contain at least six characters.', 'learnpress' ), 106 );
267 }
268
269 if ( preg_match( '#\s+#', $password ) ) {
270 throw new Exception( __( 'Password can not contain spaces!', 'learnpress' ), 107 );
271 }
272
273 if ( empty( $confirm_password ) ) {
274 throw new Exception( __( 'Please enter confirm password.', 'learnpress' ), 108 );
275 }
276
277 if ( $password !== $confirm_password ) {
278 throw new Exception( __( 'Password and Confirm Password does not match!', 'learnpress' ), 108 );
279 }
280
281 $custom_fields = LP_Settings::get_option( 'register_profile_fields', [] );
282 if ( $custom_fields && ! empty( $update_meta ) ) {
283 foreach ( $custom_fields as $field ) {
284 if ( $field['required'] === 'yes' && empty( $update_meta[ $field['id'] ] ) ) {
285 throw new Exception( $field['name'] . __( ' is required field.', 'learnpress' ), 109 );
286 }
287 }
288 }
289
290 do_action( 'lp/before_create_new_customer', $email, $username, $password, $confirm_password, $args, $update_meta );
291
292 $new_customer_data = apply_filters(
293 'learnpress_new_customer_data',
294 array_merge(
295 $args,
296 array(
297 'user_login' => $username,
298 'user_pass' => $password,
299 'user_email' => $email,
300 )
301 )
302 );
303
304 $customer_id = wp_insert_user( $new_customer_data );
305
306 do_action( 'lp/after_create_new_customer', $email, $username, $password, $confirm_password, $args, $update_meta );
307
308 if ( is_wp_error( $customer_id ) ) {
309 throw new Exception( $customer_id->get_error_message() );
310 } else {
311 if ( ! empty( $update_meta ) ) {
312 lp_user_custom_register_fields( $customer_id, $update_meta );
313 }
314
315 // Send mail.
316 wp_new_user_notification( $customer_id, null, 'both' );
317 }
318 } catch ( Throwable $e ) {
319 $code_str = '';
320 switch ( $e->getCode() ) {
321 case 101:
322 $code_str = 'registration-error-invalid-email';
323 break;
324 case 102:
325 $code_str = 'registration-error-email-exists';
326 break;
327 case 103:
328 $code_str = 'registration-error-invalid-username';
329 break;
330 case 104:
331 $code_str = 'registration-error-username-exists';
332 break;
333 case 105:
334 $code_str = 'registration-error-missing-password';
335 break;
336 case 106:
337 $code_str = 'registration-error-short-password';
338 break;
339 case 107:
340 $code_str = 'registration-error-spacing-password';
341 break;
342 case 108:
343 $code_str = 'registration-error-confirm-password';
344 break;
345 case 109:
346 $code_str = 'registration-custom-required-field';
347 break;
348 default:
349 break;
350 }
351 return new WP_Error( $code_str, $e->getMessage() );
352 }
353
354 return $customer_id;
355 }
356
357 public static function update_user_data( $update_data, $update_meta ) {
358 $user_id = get_current_user_id();
359 $current_user = get_user_by( 'id', $user_id );
360
361 if ( empty( $update_data['user_email'] ) ) {
362 return new WP_Error( 'exist_email', esc_html__( 'Email is required', 'learnpress' ) );
363 }
364
365 if ( empty( $update_data['display_name'] ) ) {
366 return new WP_Error( 'exist_display_name', esc_html__( 'Display name is required', 'learnpress' ) );
367 }
368
369 if ( is_email( $update_data['display_name'] ) ) {
370 return new WP_Error( 'error_display_name', esc_html__( 'Due to privacy concerns, the display name cannot be changed to an email address.', 'learnpress' ) );
371 }
372
373 if ( ! is_email( $update_data['user_email'] ) ) {
374 return new WP_Error( 'error_email', esc_html__( 'Due to privacy concerns, the display name cannot be changed to an email address.', 'learnpress' ) );
375 } elseif ( email_exists( $update_data['user_email'] ) && $update_data['user_email'] !== $current_user->user_email ) {
376 return new WP_Error( 'error_email', esc_html__( 'This email address is already registered.', 'learnpress' ) );
377 }
378
379 $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' );
380
381 if ( $custom_fields && ! empty( $update_meta ) ) {
382 foreach ( $custom_fields as $field ) {
383 if ( $field['required'] === 'yes' && empty( $update_meta[ $field['id'] ] ) ) {
384 return new WP_Error( 'registration-custom-exists', $field['name'] . __( ' is required field.', 'learnpress' ) );
385 }
386 }
387 }
388
389 $update_data = apply_filters( 'learn-press/update-profile-basic-information-data', $update_data );
390
391 $return = wp_update_user( $update_data );
392
393 if ( ! empty( $update_meta ) ) {
394 lp_user_custom_register_fields( $user_id, $update_meta );
395 }
396
397 if ( is_wp_error( $return ) ) {
398 return $return;
399 }
400
401 return $return;
402 }
403
404 public static function retrieve_password( $user_login ) {
405 $login = isset( $user_login ) ? sanitize_user( wp_unslash( $user_login ) ) : '';
406
407 if ( empty( $login ) ) {
408 return new WP_Error( 'error_santize_login', esc_html__( 'Enter a username or email address.', 'learnpress' ) );
409 } else {
410 // Check on username first, as customers can use emails as usernames.
411 $user_data = get_user_by( 'login', $login );
412 }
413
414 // If no user found, check if it login is email and lookup user based on email.
415 if ( ! $user_data && is_email( $login ) && apply_filters( 'learnpress_get_username_from_email', true ) ) {
416 $user_data = get_user_by( 'email', $login );
417 }
418
419 $errors = new WP_Error();
420
421 do_action( 'lostpassword_post', $errors, $user_data );
422
423 if ( $errors->get_error_code() ) {
424 return $errors;
425 }
426
427 if ( ! $user_data ) {
428 return new WP_Error( 'error_not_user', esc_html__( 'Invalid username or email.', 'learnpress' ) );
429 }
430
431 if ( is_multisite() && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) {
432 return new WP_Error( 'error_not_user', esc_html__( 'Invalid username or email.', 'learnpress' ) );
433 }
434
435 // Redefining user_login ensures we return the right case in the email.
436 $user_login = $user_data->user_login;
437
438 do_action( 'retrieve_password', $user_login );
439
440 $allow = apply_filters( 'allow_password_reset', true, $user_data->ID );
441
442 if ( ! $allow ) {
443 return new WP_Error( 'error_not_allow', esc_html__( 'Password reset is not allowed for this user.', 'learnpress' ) );
444 } elseif ( is_wp_error( $allow ) ) {
445 return $allow;
446 }
447
448 $key = get_password_reset_key( $user_data );
449
450 if ( class_exists( 'LP_Email_Reset_Password' ) ) {
451 $email = new LP_Email_Reset_Password();
452
453 $email->handle(
454 array(
455 'reset_key' => $key,
456 'user_login' => $user_login,
457 )
458 );
459 }
460
461 return true;
462 }
463
464 public static function init() {
465 self::process_login();
466 self::process_register();
467 }
468 }
469
470