PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.6.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.6.9
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.6.9, at inc/class-lp-forms-handler.php

420 lines 13.8 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> ' . __( '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 } else {
161 wp_new_user_notification( $new_customer );
162 }
163
164 // Send email become a teacher.
165 $is_become_a_teacher = false;
166 if ( LP_Settings::get_option( 'instructor_registration', 'no' ) == 'yes' && isset( $_POST['become_teacher'] ) ) {
167 update_user_meta( $new_customer, '_requested_become_teacher', 'yes' );
168 do_action(
169 'learn-press/become-a-teacher-sent',
170 array(
171 'bat_email' => $email,
172 'bat_phone' => '',
173 'bat_message' => apply_filters( 'learnpress_become_instructor_message', esc_html__( 'I need become an instructor', 'learnpress' ) ),
174 )
175 );
176
177 $is_become_a_teacher = true;
178 }
179
180 /**
181 * Auto login user
182 * Must set code below after Send email become a teacher
183 * because 'none' check by "check_ajax_referer" will not valid for send mail background on WP_Async_Request
184 */
185 wp_set_current_user( $new_customer );
186 wp_set_auth_cookie( $new_customer, true );
187
188 $message_success = $username . __( ' was successfully created!', 'learnpress' );
189
190 if ( $is_become_a_teacher ) {
191 $message_success .= '<br/>' . __( 'Your request become an instructor has been sent. We will get back to you soon!', 'learnpress' );
192 }
193
194 learn_press_add_message( $message_success, 'success' );
195
196 if ( ! empty( $_POST['redirect'] ) ) {
197 $redirect = wp_sanitize_redirect( wp_unslash( $_POST['redirect'] ) );
198 } elseif ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
199 $redirect = wp_unslash( $_REQUEST['_wp_http_referer'] );
200 } else {
201 $redirect = LP_Request::get_redirect( learn_press_get_page_link( 'profile' ) );
202 }
203
204 wp_redirect( wp_validate_redirect( $redirect, learn_press_get_current_url() ) );
205 exit();
206
207 } catch ( Exception $e ) {
208 if ( $e->getMessage() ) {
209 learn_press_add_message( $e->getMessage(), 'error' );
210 }
211 }
212 }
213
214 /**
215 * New create customer.
216 *
217 * @author ThimPress <nhamdv>
218 */
219 public static function learnpress_create_new_customer( $email, $username = '', $password = '', $confirm_password = '', $args = array(), $update_meta = array() ) {
220 if ( empty( $email ) || ! is_email( $email ) ) {
221 return new WP_Error( 'registration-error-invalid-email', __( 'Please provide a valid email address.', 'learnpress' ) );
222 }
223
224 if ( email_exists( $email ) ) {
225 return new WP_Error( 'registration-error-email-exists', apply_filters( 'learnpress_registration_error_email_exists', __( 'An account is already registered with your email address.', 'learnpress' ), $email ) );
226 }
227
228 $username = sanitize_user( $username );
229
230 if ( empty( $username ) || ! validate_username( $username ) ) {
231 return new WP_Error( 'registration-error-invalid-username', __( 'Please enter a valid account username.', 'learnpress' ) );
232 }
233
234 if ( username_exists( $username ) ) {
235 return new WP_Error( 'registration-error-username-exists', __( 'An account is already registered with that username. Please choose another.', 'learnpress' ) );
236 }
237
238 if ( apply_filters( 'learnpress_registration_generate_password', false ) ) {
239 $password = wp_generate_password();
240 }
241
242 if ( empty( $password ) ) {
243 return new WP_Error( 'registration-error-missing-password', __( 'Please enter an account password.', 'learnpress' ) );
244 }
245
246 if ( strlen( $password ) < 6 ) {
247 return new WP_Error( 'registration-error-short-password', __( 'Password is too short!', 'learnpress' ) );
248 }
249
250 if ( preg_match( '#\s+#', $password ) ) {
251 return new WP_Error( 'registration-error-spacing-password', __( 'Password can not have spacing!', 'learnpress' ) );
252 }
253
254 if ( empty( $confirm_password ) ) {
255 return new WP_Error( 'registration-error-missing-confirm-password', __( 'Please enter confirm password.', 'learnpress' ) );
256 }
257
258 if ( $password !== $confirm_password ) {
259 return new WP_Error( 'registration-error-confirm-password', __( 'Confirmation password incorrect!.', 'learnpress' ) );
260 }
261
262 $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' );
263
264 if ( $custom_fields && ! empty( $update_meta ) ) {
265 foreach ( $custom_fields as $field ) {
266 if ( $field['required'] === 'yes' && empty( $update_meta[ $field['id'] ] ) ) {
267 return new WP_Error( 'registration-custom-exists', $field['name'] . __( ' is required field.', 'learnpress' ) );
268 }
269 }
270 }
271
272 $errors = new WP_Error();
273
274 do_action( 'learnpress_register_post', $username, $email, $errors );
275
276 $errors = apply_filters( 'learnpress_registration_errors', $errors, $username, $email );
277
278 if ( $errors->get_error_code() ) {
279 return $errors;
280 }
281
282 $new_customer_data = apply_filters(
283 'learnpress_new_customer_data',
284 array_merge(
285 $args,
286 array(
287 'user_login' => $username,
288 'user_pass' => $password,
289 'user_email' => $email,
290 )
291 )
292 );
293
294 $customer_id = wp_insert_user( $new_customer_data );
295
296 if ( ! empty( $update_meta ) ) {
297 lp_user_custom_register_fields( $customer_id, $update_meta );
298 }
299
300 if ( is_wp_error( $customer_id ) ) {
301 return $customer_id;
302 }
303
304 return $customer_id;
305 }
306
307 public static function update_user_data( $update_data, $update_meta ) {
308 $user_id = get_current_user_id();
309 $current_user = get_user_by( 'id', $user_id );
310
311 if ( empty( $update_data['user_email'] ) ) {
312 return new WP_Error( 'exist_email', esc_html__( 'Email is required', 'learnpress' ) );
313 }
314
315 if ( empty( $update_data['display_name'] ) ) {
316 return new WP_Error( 'exist_display_name', esc_html__( 'Display name is required', 'learnpress' ) );
317 }
318
319 if ( is_email( $update_data['display_name'] ) ) {
320 return new WP_Error( 'error_display_name', esc_html__( 'Display name cannot be changed to email address due to privacy concern.', 'learnpress' ) );
321 }
322
323 if ( ! is_email( $update_data['user_email'] ) ) {
324 return new WP_Error( 'error_email', esc_html__( 'Display name cannot be changed to email address due to privacy concern.', 'learnpress' ) );
325 } elseif ( email_exists( $update_data['user_email'] ) && $update_data['user_email'] !== $current_user->user_email ) {
326 return new WP_Error( 'error_email', esc_html__( 'This email address is already registered.', 'learnpress' ) );
327 }
328
329 $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' );
330
331 if ( $custom_fields && ! empty( $update_meta ) ) {
332 foreach ( $custom_fields as $field ) {
333 if ( $field['required'] === 'yes' && empty( $update_meta[ $field['id'] ] ) ) {
334 return new WP_Error( 'registration-custom-exists', $field['name'] . __( ' is required field.', 'learnpress' ) );
335 }
336 }
337 }
338
339 $update_data = apply_filters( 'learn-press/update-profile-basic-information-data', $update_data );
340
341 $return = wp_update_user( $update_data );
342
343 if ( ! empty( $update_meta ) ) {
344 lp_user_custom_register_fields( $user_id, $update_meta );
345 }
346
347 if ( is_wp_error( $return ) ) {
348 return $return;
349 }
350
351 return $return;
352 }
353
354 public static function retrieve_password( $user_login ) {
355 $login = isset( $user_login ) ? sanitize_user( wp_unslash( $user_login ) ) : '';
356
357 if ( empty( $login ) ) {
358 return new WP_Error( 'error_santize_login', esc_html__( 'Enter a username or email address.', 'learnpress' ) );
359 } else {
360 // Check on username first, as customers can use emails as usernames.
361 $user_data = get_user_by( 'login', $login );
362 }
363
364 // If no user found, check if it login is email and lookup user based on email.
365 if ( ! $user_data && is_email( $login ) && apply_filters( 'learnpress_get_username_from_email', true ) ) {
366 $user_data = get_user_by( 'email', $login );
367 }
368
369 $errors = new WP_Error();
370
371 do_action( 'lostpassword_post', $errors, $user_data );
372
373 if ( $errors->get_error_code() ) {
374 return $errors;
375 }
376
377 if ( ! $user_data ) {
378 return new WP_Error( 'error_not_user', esc_html__( 'Invalid username or email.', 'learnpress' ) );
379 }
380
381 if ( is_multisite() && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) {
382 return new WP_Error( 'error_not_user', esc_html__( 'Invalid username or email.', 'learnpress' ) );
383 }
384
385 // Redefining user_login ensures we return the right case in the email.
386 $user_login = $user_data->user_login;
387
388 do_action( 'retrieve_password', $user_login );
389
390 $allow = apply_filters( 'allow_password_reset', true, $user_data->ID );
391
392 if ( ! $allow ) {
393 return new WP_Error( 'error_not_allow', esc_html__( 'Password reset is not allowed for this user.', 'learnpress' ) );
394 } elseif ( is_wp_error( $allow ) ) {
395 return $allow;
396 }
397
398 $key = get_password_reset_key( $user_data );
399
400 if ( class_exists( 'LP_Email_Reset_Password' ) ) {
401 $email = new LP_Email_Reset_Password();
402
403 $email->handle(
404 array(
405 'reset_key' => $key,
406 'user_login' => $user_login,
407 )
408 );
409 }
410
411 return true;
412 }
413
414 public static function init() {
415 self::process_login();
416 self::process_register();
417 }
418 }
419
420