| @@ -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 ); |
| @@ -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 | |
| @@ -188,25 +212,31 @@ | ||
| 188 | 212 | if ( $is_become_a_teacher ) { |
| 189 | 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,8 +247,13 @@ | ||
| 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 | 257 | throw new Exception( __( 'Please provide a valid email address.', 'learnpress' ), 101 ); |
| 223 | 258 | } |
| 224 | 259 | |
| @@ -246,10 +281,10 @@ | ||
| 246 | 281 | if ( empty( $password ) ) { |
| 247 | 282 | throw new Exception( __( 'Please enter an account password.', 'learnpress' ), 105 ); |
| 248 | 283 | } |
| 249 | 284 | |
| 250 | - if ( strlen( $password ) < 6 ) { | |
| 251 | - throw new Exception( __( 'Password is too short!', 'learnpress' ), 106 ); | |
| 285 | + if ( strlen( $password ) <= 5 ) { | |
| 286 | + throw new Exception( __( 'Password must contain at least six characters.', 'learnpress' ), 106 ); | |
| 252 | 287 | } |
| 253 | 288 | |
| 254 | 289 | if ( preg_match( '#\s+#', $password ) ) { |
| 255 | 290 | throw new Exception( __( 'Password can not contain spaces!', 'learnpress' ), 107 ); |
| @@ -263,9 +298,9 @@ | ||
| 263 | 298 | throw new Exception( __( 'Password and Confirm Password does not match!', 'learnpress' ), 108 ); |
| 264 | 299 | } |
| 265 | 300 | |
| 266 | 301 | $custom_fields = LP_Settings::get_option( 'register_profile_fields', [] ); |
| 267 | - if ( $custom_fields && ! empty( $update_meta ) ) { | |
| 302 | + if ( ! empty( $custom_fields ) ) { | |
| 268 | 303 | foreach ( $custom_fields as $field ) { |
| 269 | 304 | if ( $field['required'] === 'yes' && empty( $update_meta[ $field['id'] ] ) ) { |
| 270 | 305 | throw new Exception( $field['name'] . __( ' is required field.', 'learnpress' ), 109 ); |
| 271 | 306 | } |
| @@ -285,8 +320,15 @@ | ||
| 285 | 320 | ) |
| 286 | 321 | ) |
| 287 | 322 | ); |
| 288 | 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 | + | |
| 289 | 331 | $customer_id = wp_insert_user( $new_customer_data ); |
| 290 | 332 | |
| 291 | 333 | do_action( 'lp/after_create_new_customer', $email, $username, $password, $confirm_password, $args, $update_meta ); |
| 292 | 334 | |
| @@ -329,11 +371,16 @@ | ||
| 329 | 371 | break; |
| 330 | 372 | case 109: |
| 331 | 373 | $code_str = 'registration-custom-required-field'; |
| 332 | 374 | break; |
| 375 | + case 110: | |
| 376 | + $code_str = 'registration-not-allow'; | |
| 377 | + break; | |
| 333 | 378 | default: |
| 379 | + $code_str = $e->getMessage(); | |
| 334 | 380 | break; |
| 335 | 381 | } |
| 382 | + | |
| 336 | 383 | return new WP_Error( $code_str, $e->getMessage() ); |
| 337 | 384 | } |
| 338 | 385 | |
| 339 | 386 | return $customer_id; |
| @@ -360,13 +407,18 @@ | ||
| 360 | 407 | } elseif ( email_exists( $update_data['user_email'] ) && $update_data['user_email'] !== $current_user->user_email ) { |
| 361 | 408 | return new WP_Error( 'error_email', esc_html__( 'This email address is already registered.', 'learnpress' ) ); |
| 362 | 409 | } |
| 363 | 410 | |
| 364 | - $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' ); | |
| 365 | - | |
| 411 | + $custom_fields = LP_Profile::get_register_fields_custom(); | |
| 366 | 412 | if ( $custom_fields && ! empty( $update_meta ) ) { |
| 367 | 413 | foreach ( $custom_fields as $field ) { |
| 368 | - 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 ) { | |
| 369 | 421 | return new WP_Error( 'registration-custom-exists', $field['name'] . __( ' is required field.', 'learnpress' ) ); |
| 370 | 422 | } |
| 371 | 423 | } |
| 372 | 424 | } |
| @@ -450,5 +502,4 @@ | ||
| 450 | 502 | self::process_login(); |
| 451 | 503 | self::process_register(); |
| 452 | 504 | } |
| 453 | 505 | } |
| 454 | - | |