| @@ -1,5 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 3 | +use LearnPress\Background\LPBackgroundAjax; | |
| 4 | + | |
| 2 | 5 | /** |
| 3 | 6 | * Class LP_Forms_Handler |
| 4 | 7 | * |
| 5 | 8 | * Process action for submitting forms |
| @@ -52,8 +55,19 @@ | ||
| 52 | 55 | $result['message'][] = learn_press_get_message( __( 'Thank you! Your message has been sent.', 'learnpress' ), 'success' ); |
| 53 | 56 | $user = get_user_by( 'email', $args['bat_email'] ); |
| 54 | 57 | |
| 55 | 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 | + | |
| 56 | 70 | do_action( 'learn-press/become-a-teacher-sent', $args ); |
| 57 | 71 | } |
| 58 | 72 | |
| 59 | 73 | learn_press_maybe_send_json( $result ); |
| @@ -76,9 +90,9 @@ | ||
| 76 | 90 | $password = LP_Helper::sanitize_params_submitted( $_POST['password'] ); |
| 77 | 91 | $remember = LP_Request::get_string( 'rememberme' ); |
| 78 | 92 | |
| 79 | 93 | if ( empty( $username ) ) { |
| 80 | - throw new Exception( '<strong>' . __( 'Error:', 'learnpress' ) . '</strong> ' . __( 'Username is required', 'learnpress' ) ); | |
| 94 | + throw new Exception( '<strong>' . __( 'Error:', 'learnpress' ) . '</strong> ' . __( 'A username is required', 'learnpress' ) ); | |
| 81 | 95 | } |
| 82 | 96 | |
| 83 | 97 | // On multisite, ensure user exists on current site, if not add them before allowing login. |
| 84 | 98 | if ( is_multisite() ) { |
| @@ -104,20 +118,30 @@ | ||
| 104 | 118 | if ( is_wp_error( $user ) ) { |
| 105 | 119 | throw new Exception( $user->get_error_message() ); |
| 106 | 120 | } else { |
| 107 | 121 | if ( ! empty( $_POST['redirect'] ) ) { |
| 108 | - $redirect = LP_Helper::sanitize_params_submitted( $_POST['redirect'] ); | |
| 122 | + $url_redirect = LP_Helper::sanitize_params_submitted( $_POST['redirect'] ); | |
| 109 | 123 | } elseif ( ! empty( $_REQUEST['_wp_http_referer'] ) ) { |
| 110 | - $redirect = LP_Helper::sanitize_params_submitted( $_REQUEST['_wp_http_referer'] ); | |
| 124 | + $url_redirect = LP_Request::get_param( '_wp_http_referer' ); | |
| 111 | 125 | } else { |
| 112 | - $redirect = LP_Request::get_redirect( learn_press_get_page_link( 'profile' ) ); | |
| 126 | + $url_redirect = LP_Request::get_redirect( learn_press_get_page_link( 'profile' ) ); | |
| 113 | 127 | } |
| 128 | + $url_redirect = apply_filters( 'learn-press/login-redirect', $url_redirect, $user ); | |
| 114 | 129 | |
| 115 | - wp_redirect( wp_validate_redirect( $redirect, learn_press_get_current_url() ) ); | |
| 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() ) ); | |
| 116 | 136 | exit(); |
| 117 | 137 | } |
| 118 | 138 | } catch ( Exception $e ) { |
| 119 | - learn_press_add_message( $e->getMessage(), 'error' ); | |
| 139 | + $message_data = [ | |
| 140 | + 'status' => 'error', | |
| 141 | + 'content' => $e->getMessage(), | |
| 142 | + ]; | |
| 143 | + learn_press_set_message( $message_data ); | |
| 120 | 144 | } |
| 121 | 145 | } |
| 122 | 146 | } |
| 123 | 147 | |
| @@ -167,9 +191,9 @@ | ||
| 167 | 191 | 'learn-press/become-a-teacher-sent', |
| 168 | 192 | array( |
| 169 | 193 | 'bat_email' => $email, |
| 170 | 194 | 'bat_phone' => '', |
| 171 | - 'bat_message' => apply_filters( 'learnpress_become_instructor_message', esc_html__( 'I need become an instructor', 'learnpress' ) ), | |
| 195 | + 'bat_message' => apply_filters( 'learnpress_become_instructor_message', esc_html__( 'I need to become an instructor', 'learnpress' ) ), | |
| 172 | 196 | ) |
| 173 | 197 | ); |
| 174 | 198 | |
| 175 | 199 | $is_become_a_teacher = true; |
| @@ -185,28 +209,34 @@ | ||
| 185 | 209 | |
| 186 | 210 | $message_success = $username . __( ' was successfully created!', 'learnpress' ); |
| 187 | 211 | |
| 188 | 212 | 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' ); | |
| 213 | + $message_success .= '<br/>' . __( 'Your request to become an instructor has been sent. We will get back to you soon!', 'learnpress' ); | |
| 190 | 214 | } |
| 191 | 215 | |
| 192 | - learn_press_add_message( $message_success, 'success' ); | |
| 216 | + $message_data = [ | |
| 217 | + 'status' => 'success', | |
| 218 | + 'content' => $message_success, | |
| 219 | + ]; | |
| 220 | + learn_press_set_message( $message_data ); | |
| 193 | 221 | |
| 194 | 222 | if ( ! empty( $_POST['redirect'] ) ) { |
| 195 | - $redirect = wp_sanitize_redirect( wp_unslash( $_POST['redirect'] ) ); | |
| 223 | + $url_redirect = wp_sanitize_redirect( wp_unslash( $_POST['redirect'] ) ); | |
| 196 | 224 | } elseif ( ! empty( $_REQUEST['_wp_http_referer'] ) ) { |
| 197 | - $redirect = wp_unslash( $_REQUEST['_wp_http_referer'] ); | |
| 225 | + $url_redirect = wp_unslash( $_REQUEST['_wp_http_referer'] ); | |
| 198 | 226 | } else { |
| 199 | - $redirect = LP_Request::get_redirect( learn_press_get_page_link( 'profile' ) ); | |
| 227 | + $url_redirect = LP_Request::get_redirect( learn_press_get_page_link( 'profile' ) ); | |
| 200 | 228 | } |
| 201 | - | |
| 202 | - wp_redirect( wp_validate_redirect( $redirect, learn_press_get_current_url() ) ); | |
| 229 | + $url_redirect = apply_filters( 'learn-press/register-redirect', $url_redirect, $new_customer ); | |
| 230 | + wp_redirect( wp_validate_redirect( $url_redirect, LP_Helper::getUrlCurrent() ) ); | |
| 203 | 231 | exit(); |
| 204 | 232 | |
| 205 | - } catch ( Exception $e ) { | |
| 206 | - if ( $e->getMessage() ) { | |
| 207 | - learn_press_add_message( $e->getMessage(), 'error' ); | |
| 208 | - } | |
| 233 | + } catch ( Throwable $e ) { | |
| 234 | + $message_data = [ | |
| 235 | + 'status' => 'error', | |
| 236 | + 'content' => $e->getMessage(), | |
| 237 | + ]; | |
| 238 | + learn_press_set_message( $message_data ); | |
| 209 | 239 | } |
| 210 | 240 | } |
| 211 | 241 | |
| 212 | 242 | /** |
| @@ -217,24 +247,32 @@ | ||
| 217 | 247 | * @since 4.0.0 |
| 218 | 248 | */ |
| 219 | 249 | public static function learnpress_create_new_customer( $email = '', $username = '', $password = '', $confirm_password = '', $args = array(), $update_meta = array() ) { |
| 220 | 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 | + | |
| 221 | 256 | if ( empty( $email ) || ! is_email( $email ) ) { |
| 222 | - throw new Exception( __( 'Please provide a valid email address.', 'learnpress' ) ); | |
| 257 | + throw new Exception( __( 'Please provide a valid email address.', 'learnpress' ), 101 ); | |
| 223 | 258 | } |
| 224 | 259 | |
| 225 | 260 | if ( email_exists( $email ) ) { |
| 226 | - throw new Exception( __( 'An account is already registered with your email address.', 'learnpress' ) ); | |
| 261 | + throw new Exception( __( 'An account is already registered with your email address.', 'learnpress' ), 102 ); | |
| 227 | 262 | } |
| 228 | 263 | |
| 229 | 264 | $username = sanitize_user( $username ); |
| 230 | 265 | |
| 231 | 266 | if ( empty( $username ) || ! validate_username( $username ) ) { |
| 232 | - throw new Exception( __( 'Please enter a valid account username.', 'learnpress' ) ); | |
| 267 | + throw new Exception( __( 'Please enter a valid account username.', 'learnpress' ), 103 ); | |
| 233 | 268 | } |
| 234 | 269 | |
| 235 | 270 | if ( username_exists( $username ) ) { |
| 236 | - throw new Exception( __( 'An account is already registered with that username. Please choose another.', 'learnpress' ) ); | |
| 271 | + throw new Exception( | |
| 272 | + __( 'An account is already registered with that username. Please choose another one.', 'learnpress' ), | |
| 273 | + 104 | |
| 274 | + ); | |
| 237 | 275 | } |
| 238 | 276 | |
| 239 | 277 | if ( apply_filters( 'learnpress_registration_generate_password', false ) ) { |
| 240 | 278 | $password = wp_generate_password(); |
| @@ -240,33 +278,32 @@ | ||
| 240 | 278 | $password = wp_generate_password(); |
| 241 | 279 | } |
| 242 | 280 | |
| 243 | 281 | if ( empty( $password ) ) { |
| 244 | - throw new Exception( __( 'Please enter an account password.', 'learnpress' ) ); | |
| 282 | + throw new Exception( __( 'Please enter an account password.', 'learnpress' ), 105 ); | |
| 245 | 283 | } |
| 246 | 284 | |
| 247 | - if ( strlen( $password ) < 6 ) { | |
| 248 | - throw new Exception( __( 'Password is too short!', 'learnpress' ) ); | |
| 285 | + if ( strlen( $password ) <= 5 ) { | |
| 286 | + throw new Exception( __( 'Password must contain at least six characters.', 'learnpress' ), 106 ); | |
| 249 | 287 | } |
| 250 | 288 | |
| 251 | 289 | if ( preg_match( '#\s+#', $password ) ) { |
| 252 | - throw new Exception( __( 'Password can not have spacing!', 'learnpress' ) ); | |
| 290 | + throw new Exception( __( 'Password can not contain spaces!', 'learnpress' ), 107 ); | |
| 253 | 291 | } |
| 254 | 292 | |
| 255 | 293 | if ( empty( $confirm_password ) ) { |
| 256 | - throw new Exception( __( 'Please enter confirm password.', 'learnpress' ) ); | |
| 294 | + throw new Exception( __( 'Please enter confirm password.', 'learnpress' ), 108 ); | |
| 257 | 295 | } |
| 258 | 296 | |
| 259 | 297 | if ( $password !== $confirm_password ) { |
| 260 | - throw new Exception( __( 'Confirmation password incorrect!.', 'learnpress' ) ); | |
| 298 | + throw new Exception( __( 'Password and Confirm Password does not match!', 'learnpress' ), 108 ); | |
| 261 | 299 | } |
| 262 | 300 | |
| 263 | 301 | $custom_fields = LP_Settings::get_option( 'register_profile_fields', [] ); |
| 264 | - | |
| 265 | - if ( $custom_fields && ! empty( $update_meta ) ) { | |
| 302 | + if ( ! empty( $custom_fields ) ) { | |
| 266 | 303 | foreach ( $custom_fields as $field ) { |
| 267 | 304 | if ( $field['required'] === 'yes' && empty( $update_meta[ $field['id'] ] ) ) { |
| 268 | - throw new Exception( $field['name'] . __( ' is required field.', 'learnpress' ) ); | |
| 305 | + throw new Exception( $field['name'] . __( ' is required field.', 'learnpress' ), 109 ); | |
| 269 | 306 | } |
| 270 | 307 | } |
| 271 | 308 | } |
| 272 | 309 | |
| @@ -283,8 +320,15 @@ | ||
| 283 | 320 | ) |
| 284 | 321 | ) |
| 285 | 322 | ); |
| 286 | 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 | + | |
| 287 | 331 | $customer_id = wp_insert_user( $new_customer_data ); |
| 288 | 332 | |
| 289 | 333 | do_action( 'lp/after_create_new_customer', $email, $username, $password, $confirm_password, $args, $update_meta ); |
| 290 | 334 | |
| @@ -298,9 +342,46 @@ | ||
| 298 | 342 | // Send mail. |
| 299 | 343 | wp_new_user_notification( $customer_id, null, 'both' ); |
| 300 | 344 | } |
| 301 | 345 | } catch ( Throwable $e ) { |
| 302 | - return new WP_Error( 'lp/create-new-customer/error', $e->getMessage() ); | |
| 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() ); | |
| 303 | 384 | } |
| 304 | 385 | |
| 305 | 386 | return $customer_id; |
| 306 | 387 | } |
| @@ -317,22 +398,27 @@ | ||
| 317 | 398 | return new WP_Error( 'exist_display_name', esc_html__( 'Display name is required', 'learnpress' ) ); |
| 318 | 399 | } |
| 319 | 400 | |
| 320 | 401 | 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' ) ); | |
| 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' ) ); | |
| 322 | 403 | } |
| 323 | 404 | |
| 324 | 405 | 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' ) ); | |
| 406 | + return new WP_Error( 'error_email', esc_html__( 'Due to privacy concerns, the display name cannot be changed to an email address.', 'learnpress' ) ); | |
| 326 | 407 | } elseif ( email_exists( $update_data['user_email'] ) && $update_data['user_email'] !== $current_user->user_email ) { |
| 327 | 408 | return new WP_Error( 'error_email', esc_html__( 'This email address is already registered.', 'learnpress' ) ); |
| 328 | 409 | } |
| 329 | 410 | |
| 330 | - $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' ); | |
| 331 | - | |
| 411 | + $custom_fields = LP_Profile::get_register_fields_custom(); | |
| 332 | 412 | if ( $custom_fields && ! empty( $update_meta ) ) { |
| 333 | 413 | foreach ( $custom_fields as $field ) { |
| 334 | - if ( $field['required'] === 'yes' && empty( $update_meta[ $field['id'] ] ) ) { | |
| 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 ) { | |
| 335 | 421 | return new WP_Error( 'registration-custom-exists', $field['name'] . __( ' is required field.', 'learnpress' ) ); |
| 336 | 422 | } |
| 337 | 423 | } |
| 338 | 424 | } |
| @@ -416,5 +502,4 @@ | ||
| 416 | 502 | self::process_login(); |
| 417 | 503 | self::process_register(); |
| 418 | 504 | } |
| 419 | 505 | } |
| 420 | - | |