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