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

421 lines 13.7 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 }
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 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 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' ) );
223 }
224
225 if ( email_exists( $email ) ) {
226 throw new Exception( __( 'An account is already registered with your email address.', 'learnpress' ) );
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' ) );
233 }
234
235 if ( username_exists( $username ) ) {
236 throw new Exception( __( 'An account is already registered with that username. Please choose another.', 'learnpress' ) );
237 }
238
239 if ( apply_filters( 'learnpress_registration_generate_password', false ) ) {
240 $password = wp_generate_password();
241 }
242
243 if ( empty( $password ) ) {
244 throw new Exception( __( 'Please enter an account password.', 'learnpress' ) );
245 }
246
247 if ( strlen( $password ) < 6 ) {
248 throw new Exception( __( 'Password is too short!', 'learnpress' ) );
249 }
250
251 if ( preg_match( '#\s+#', $password ) ) {
252 throw new Exception( __( 'Password can not have spacing!', 'learnpress' ) );
253 }
254
255 if ( empty( $confirm_password ) ) {
256 throw new Exception( __( 'Please enter confirm password.', 'learnpress' ) );
257 }
258
259 if ( $password !== $confirm_password ) {
260 throw new Exception( __( 'Confirmation password incorrect!.', 'learnpress' ) );
261 }
262
263 $custom_fields = LP_Settings::get_option( 'register_profile_fields', [] );
264
265 if ( $custom_fields && ! empty( $update_meta ) ) {
266 foreach ( $custom_fields as $field ) {
267 if ( $field['required'] === 'yes' && empty( $update_meta[ $field['id'] ] ) ) {
268 throw new Exception( $field['name'] . __( ' is required field.', 'learnpress' ) );
269 }
270 }
271 }
272
273 do_action( 'lp/before_create_new_customer', $email, $username, $password, $confirm_password, $args, $update_meta );
274
275 $new_customer_data = apply_filters(
276 'learnpress_new_customer_data',
277 array_merge(
278 $args,
279 array(
280 'user_login' => $username,
281 'user_pass' => $password,
282 'user_email' => $email,
283 )
284 )
285 );
286
287 $customer_id = wp_insert_user( $new_customer_data );
288
289 do_action( 'lp/after_create_new_customer', $email, $username, $password, $confirm_password, $args, $update_meta );
290
291 if ( is_wp_error( $customer_id ) ) {
292 throw new Exception( $customer_id->get_error_message() );
293 } else {
294 if ( ! empty( $update_meta ) ) {
295 lp_user_custom_register_fields( $customer_id, $update_meta );
296 }
297
298 // Send mail.
299 wp_new_user_notification( $customer_id, null, 'both' );
300 }
301 } catch ( Throwable $e ) {
302 return new WP_Error( 'lp/create-new-customer/error', $e->getMessage() );
303 }
304
305 return $customer_id;
306 }
307
308 public static function update_user_data( $update_data, $update_meta ) {
309 $user_id = get_current_user_id();
310 $current_user = get_user_by( 'id', $user_id );
311
312 if ( empty( $update_data['user_email'] ) ) {
313 return new WP_Error( 'exist_email', esc_html__( 'Email is required', 'learnpress' ) );
314 }
315
316 if ( empty( $update_data['display_name'] ) ) {
317 return new WP_Error( 'exist_display_name', esc_html__( 'Display name is required', 'learnpress' ) );
318 }
319
320 if ( is_email( $update_data['display_name'] ) ) {
321 return new WP_Error( 'error_display_name', esc_html__( 'Display name cannot be changed to email address due to privacy concern.', 'learnpress' ) );
322 }
323
324 if ( ! is_email( $update_data['user_email'] ) ) {
325 return new WP_Error( 'error_email', esc_html__( 'Display name cannot be changed to email address due to privacy concern.', 'learnpress' ) );
326 } elseif ( email_exists( $update_data['user_email'] ) && $update_data['user_email'] !== $current_user->user_email ) {
327 return new WP_Error( 'error_email', esc_html__( 'This email address is already registered.', 'learnpress' ) );
328 }
329
330 $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' );
331
332 if ( $custom_fields && ! empty( $update_meta ) ) {
333 foreach ( $custom_fields as $field ) {
334 if ( $field['required'] === 'yes' && empty( $update_meta[ $field['id'] ] ) ) {
335 return new WP_Error( 'registration-custom-exists', $field['name'] . __( ' is required field.', 'learnpress' ) );
336 }
337 }
338 }
339
340 $update_data = apply_filters( 'learn-press/update-profile-basic-information-data', $update_data );
341
342 $return = wp_update_user( $update_data );
343
344 if ( ! empty( $update_meta ) ) {
345 lp_user_custom_register_fields( $user_id, $update_meta );
346 }
347
348 if ( is_wp_error( $return ) ) {
349 return $return;
350 }
351
352 return $return;
353 }
354
355 public static function retrieve_password( $user_login ) {
356 $login = isset( $user_login ) ? sanitize_user( wp_unslash( $user_login ) ) : '';
357
358 if ( empty( $login ) ) {
359 return new WP_Error( 'error_santize_login', esc_html__( 'Enter a username or email address.', 'learnpress' ) );
360 } else {
361 // Check on username first, as customers can use emails as usernames.
362 $user_data = get_user_by( 'login', $login );
363 }
364
365 // If no user found, check if it login is email and lookup user based on email.
366 if ( ! $user_data && is_email( $login ) && apply_filters( 'learnpress_get_username_from_email', true ) ) {
367 $user_data = get_user_by( 'email', $login );
368 }
369
370 $errors = new WP_Error();
371
372 do_action( 'lostpassword_post', $errors, $user_data );
373
374 if ( $errors->get_error_code() ) {
375 return $errors;
376 }
377
378 if ( ! $user_data ) {
379 return new WP_Error( 'error_not_user', esc_html__( 'Invalid username or email.', 'learnpress' ) );
380 }
381
382 if ( is_multisite() && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) {
383 return new WP_Error( 'error_not_user', esc_html__( 'Invalid username or email.', 'learnpress' ) );
384 }
385
386 // Redefining user_login ensures we return the right case in the email.
387 $user_login = $user_data->user_login;
388
389 do_action( 'retrieve_password', $user_login );
390
391 $allow = apply_filters( 'allow_password_reset', true, $user_data->ID );
392
393 if ( ! $allow ) {
394 return new WP_Error( 'error_not_allow', esc_html__( 'Password reset is not allowed for this user.', 'learnpress' ) );
395 } elseif ( is_wp_error( $allow ) ) {
396 return $allow;
397 }
398
399 $key = get_password_reset_key( $user_data );
400
401 if ( class_exists( 'LP_Email_Reset_Password' ) ) {
402 $email = new LP_Email_Reset_Password();
403
404 $email->handle(
405 array(
406 'reset_key' => $key,
407 'user_login' => $user_login,
408 )
409 );
410 }
411
412 return true;
413 }
414
415 public static function init() {
416 self::process_login();
417 self::process_register();
418 }
419 }
420
421