PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3
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-forms-handler.php

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

455 lines 14.5 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 wp_redirect( wp_validate_redirect( $redirect, learn_press_get_current_url() ) );
116 exit();
117 }
118 } catch ( Exception $e ) {
119 learn_press_add_message( $e->getMessage(), 'error' );
120 }
121 }
122 }
123
124 /**
125 * Process register form.
126 *
127 * @throws Exception On Error register.
128 * @author ThimPress <nhamdv>
129 */
130 public static function process_register() {
131 if ( ! LP_Request::verify_nonce( 'learn-press-register' ) ) {
132 return;
133 }
134
135 $username = LP_Helper::sanitize_params_submitted( $_POST['reg_username'] ?? '' );
136 $email = LP_Helper::sanitize_params_submitted( $_POST['reg_email'] ?? '' );
137 $password = LP_Helper::sanitize_params_submitted( $_POST['reg_password'] ?? '' );
138 $confirm_password = LP_Helper::sanitize_params_submitted( $_POST['reg_password2'] ?? '' );
139 $first_name = LP_Helper::sanitize_params_submitted( $_POST['reg_first_name'] ?? '' );
140 $last_name = LP_Helper::sanitize_params_submitted( $_POST['reg_last_name'] ?? '' );
141 $display_name = LP_Helper::sanitize_params_submitted( $_POST['reg_display_name'] ?? '' );
142 $update_meta = LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register_form'] ?? '' );
143
144 try {
145 $new_customer = self::learnpress_create_new_customer(
146 sanitize_email( $email ),
147 $username,
148 $password,
149 $confirm_password,
150 array(
151 'first_name' => $first_name,
152 'last_name' => $last_name,
153 'display_name' => $display_name,
154 ),
155 $update_meta
156 );
157
158 if ( is_wp_error( $new_customer ) ) {
159 throw new Exception( $new_customer->get_error_message() );
160 }
161
162 // Send email become a teacher.
163 $is_become_a_teacher = false;
164 if ( LP_Settings::get_option( 'instructor_registration', 'no' ) == 'yes' && isset( $_POST['become_teacher'] ) ) {
165 update_user_meta( $new_customer, '_requested_become_teacher', 'yes' );
166 do_action(
167 'learn-press/become-a-teacher-sent',
168 array(
169 'bat_email' => $email,
170 'bat_phone' => '',
171 'bat_message' => apply_filters( 'learnpress_become_instructor_message', esc_html__( 'I need to become an instructor', 'learnpress' ) ),
172 )
173 );
174
175 $is_become_a_teacher = true;
176 }
177
178 /**
179 * Auto login user
180 * Must set code below after Send email become a teacher
181 * because 'none' check by "check_ajax_referer" will not valid for send mail background on WP_Async_Request
182 */
183 wp_set_current_user( $new_customer );
184 wp_set_auth_cookie( $new_customer, true );
185
186 $message_success = $username . __( ' was successfully created!', 'learnpress' );
187
188 if ( $is_become_a_teacher ) {
189 $message_success .= '<br/>' . __( 'Your request to become an instructor has been sent. We will get back to you soon!', 'learnpress' );
190 }
191
192 learn_press_add_message( $message_success, 'success' );
193
194 if ( ! empty( $_POST['redirect'] ) ) {
195 $redirect = wp_sanitize_redirect( wp_unslash( $_POST['redirect'] ) );
196 } elseif ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
197 $redirect = wp_unslash( $_REQUEST['_wp_http_referer'] );
198 } else {
199 $redirect = LP_Request::get_redirect( learn_press_get_page_link( 'profile' ) );
200 }
201
202 wp_redirect( wp_validate_redirect( $redirect, learn_press_get_current_url() ) );
203 exit();
204
205 } catch ( Exception $e ) {
206 if ( $e->getMessage() ) {
207 learn_press_add_message( $e->getMessage(), 'error' );
208 }
209 }
210 }
211
212 /**
213 * New create customer.
214 *
215 * @author ThimPress <nhamdv>
216 * @version 1.0.1
217 * @since 4.0.0
218 */
219 public static function learnpress_create_new_customer( $email = '', $username = '', $password = '', $confirm_password = '', $args = array(), $update_meta = array() ) {
220 try {
221 if ( empty( $email ) || ! is_email( $email ) ) {
222 throw new Exception( __( 'Please provide a valid email address.', 'learnpress' ), 101 );
223 }
224
225 if ( email_exists( $email ) ) {
226 throw new Exception( __( 'An account is already registered with your email address.', 'learnpress' ), 102 );
227 }
228
229 $username = sanitize_user( $username );
230
231 if ( empty( $username ) || ! validate_username( $username ) ) {
232 throw new Exception( __( 'Please enter a valid account username.', 'learnpress' ), 103 );
233 }
234
235 if ( username_exists( $username ) ) {
236 throw new Exception(
237 __( 'An account is already registered with that username. Please choose another one.', 'learnpress' ),
238 104
239 );
240 }
241
242 if ( apply_filters( 'learnpress_registration_generate_password', false ) ) {
243 $password = wp_generate_password();
244 }
245
246 if ( empty( $password ) ) {
247 throw new Exception( __( 'Please enter an account password.', 'learnpress' ), 105 );
248 }
249
250 if ( strlen( $password ) < 6 ) {
251 throw new Exception( __( 'Password is too short!', 'learnpress' ), 106 );
252 }
253
254 if ( preg_match( '#\s+#', $password ) ) {
255 throw new Exception( __( 'Password can not contain spaces!', 'learnpress' ), 107 );
256 }
257
258 if ( empty( $confirm_password ) ) {
259 throw new Exception( __( 'Please enter confirm password.', 'learnpress' ), 108 );
260 }
261
262 if ( $password !== $confirm_password ) {
263 throw new Exception( __( 'Password and Confirm Password does not match!', 'learnpress' ), 108 );
264 }
265
266 $custom_fields = LP_Settings::get_option( 'register_profile_fields', [] );
267 if ( $custom_fields && ! empty( $update_meta ) ) {
268 foreach ( $custom_fields as $field ) {
269 if ( $field['required'] === 'yes' && empty( $update_meta[ $field['id'] ] ) ) {
270 throw new Exception( $field['name'] . __( ' is required field.', 'learnpress' ), 109 );
271 }
272 }
273 }
274
275 do_action( 'lp/before_create_new_customer', $email, $username, $password, $confirm_password, $args, $update_meta );
276
277 $new_customer_data = apply_filters(
278 'learnpress_new_customer_data',
279 array_merge(
280 $args,
281 array(
282 'user_login' => $username,
283 'user_pass' => $password,
284 'user_email' => $email,
285 )
286 )
287 );
288
289 $customer_id = wp_insert_user( $new_customer_data );
290
291 do_action( 'lp/after_create_new_customer', $email, $username, $password, $confirm_password, $args, $update_meta );
292
293 if ( is_wp_error( $customer_id ) ) {
294 throw new Exception( $customer_id->get_error_message() );
295 } else {
296 if ( ! empty( $update_meta ) ) {
297 lp_user_custom_register_fields( $customer_id, $update_meta );
298 }
299
300 // Send mail.
301 wp_new_user_notification( $customer_id, null, 'both' );
302 }
303 } catch ( Throwable $e ) {
304 $code_str = '';
305 switch ( $e->getCode() ) {
306 case 101:
307 $code_str = 'registration-error-invalid-email';
308 break;
309 case 102:
310 $code_str = 'registration-error-email-exists';
311 break;
312 case 103:
313 $code_str = 'registration-error-invalid-username';
314 break;
315 case 104:
316 $code_str = 'registration-error-username-exists';
317 break;
318 case 105:
319 $code_str = 'registration-error-missing-password';
320 break;
321 case 106:
322 $code_str = 'registration-error-short-password';
323 break;
324 case 107:
325 $code_str = 'registration-error-spacing-password';
326 break;
327 case 108:
328 $code_str = 'registration-error-confirm-password';
329 break;
330 case 109:
331 $code_str = 'registration-custom-required-field';
332 break;
333 default:
334 break;
335 }
336 return new WP_Error( $code_str, $e->getMessage() );
337 }
338
339 return $customer_id;
340 }
341
342 public static function update_user_data( $update_data, $update_meta ) {
343 $user_id = get_current_user_id();
344 $current_user = get_user_by( 'id', $user_id );
345
346 if ( empty( $update_data['user_email'] ) ) {
347 return new WP_Error( 'exist_email', esc_html__( 'Email is required', 'learnpress' ) );
348 }
349
350 if ( empty( $update_data['display_name'] ) ) {
351 return new WP_Error( 'exist_display_name', esc_html__( 'Display name is required', 'learnpress' ) );
352 }
353
354 if ( is_email( $update_data['display_name'] ) ) {
355 return new WP_Error( 'error_display_name', esc_html__( 'Due to privacy concerns, the display name cannot be changed to an email address.', 'learnpress' ) );
356 }
357
358 if ( ! is_email( $update_data['user_email'] ) ) {
359 return new WP_Error( 'error_email', esc_html__( 'Due to privacy concerns, the display name cannot be changed to an email address.', 'learnpress' ) );
360 } elseif ( email_exists( $update_data['user_email'] ) && $update_data['user_email'] !== $current_user->user_email ) {
361 return new WP_Error( 'error_email', esc_html__( 'This email address is already registered.', 'learnpress' ) );
362 }
363
364 $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' );
365
366 if ( $custom_fields && ! empty( $update_meta ) ) {
367 foreach ( $custom_fields as $field ) {
368 if ( $field['required'] === 'yes' && empty( $update_meta[ $field['id'] ] ) ) {
369 return new WP_Error( 'registration-custom-exists', $field['name'] . __( ' is required field.', 'learnpress' ) );
370 }
371 }
372 }
373
374 $update_data = apply_filters( 'learn-press/update-profile-basic-information-data', $update_data );
375
376 $return = wp_update_user( $update_data );
377
378 if ( ! empty( $update_meta ) ) {
379 lp_user_custom_register_fields( $user_id, $update_meta );
380 }
381
382 if ( is_wp_error( $return ) ) {
383 return $return;
384 }
385
386 return $return;
387 }
388
389 public static function retrieve_password( $user_login ) {
390 $login = isset( $user_login ) ? sanitize_user( wp_unslash( $user_login ) ) : '';
391
392 if ( empty( $login ) ) {
393 return new WP_Error( 'error_santize_login', esc_html__( 'Enter a username or email address.', 'learnpress' ) );
394 } else {
395 // Check on username first, as customers can use emails as usernames.
396 $user_data = get_user_by( 'login', $login );
397 }
398
399 // If no user found, check if it login is email and lookup user based on email.
400 if ( ! $user_data && is_email( $login ) && apply_filters( 'learnpress_get_username_from_email', true ) ) {
401 $user_data = get_user_by( 'email', $login );
402 }
403
404 $errors = new WP_Error();
405
406 do_action( 'lostpassword_post', $errors, $user_data );
407
408 if ( $errors->get_error_code() ) {
409 return $errors;
410 }
411
412 if ( ! $user_data ) {
413 return new WP_Error( 'error_not_user', esc_html__( 'Invalid username or email.', 'learnpress' ) );
414 }
415
416 if ( is_multisite() && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) {
417 return new WP_Error( 'error_not_user', esc_html__( 'Invalid username or email.', 'learnpress' ) );
418 }
419
420 // Redefining user_login ensures we return the right case in the email.
421 $user_login = $user_data->user_login;
422
423 do_action( 'retrieve_password', $user_login );
424
425 $allow = apply_filters( 'allow_password_reset', true, $user_data->ID );
426
427 if ( ! $allow ) {
428 return new WP_Error( 'error_not_allow', esc_html__( 'Password reset is not allowed for this user.', 'learnpress' ) );
429 } elseif ( is_wp_error( $allow ) ) {
430 return $allow;
431 }
432
433 $key = get_password_reset_key( $user_data );
434
435 if ( class_exists( 'LP_Email_Reset_Password' ) ) {
436 $email = new LP_Email_Reset_Password();
437
438 $email->handle(
439 array(
440 'reset_key' => $key,
441 'user_login' => $user_login,
442 )
443 );
444 }
445
446 return true;
447 }
448
449 public static function init() {
450 self::process_login();
451 self::process_register();
452 }
453 }
454
455