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