| @@ -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 | |
| @@ -156,10 +180,8 @@ | ||
| 156 | 180 | ); |
| 157 | 181 | |
| 158 | 182 | if ( is_wp_error( $new_customer ) ) { |
| 159 | 183 | throw new Exception( $new_customer->get_error_message() ); |
| 160 | - } else { | |
| 161 | - wp_new_user_notification( $new_customer ); | |
| 162 | 184 | } |
| 163 | 185 | |
| 164 | 186 | // Send email become a teacher. |
| 165 | 187 | $is_become_a_teacher = false; |
| @@ -169,9 +191,9 @@ | ||
| 169 | 191 | 'learn-press/become-a-teacher-sent', |
| 170 | 192 | array( |
| 171 | 193 | 'bat_email' => $email, |
| 172 | 194 | 'bat_phone' => '', |
| 173 | - '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' ) ), | |
| 174 | 196 | ) |
| 175 | 197 | ); |
| 176 | 198 | |
| 177 | 199 | $is_become_a_teacher = true; |
| @@ -187,28 +209,34 @@ | ||
| 187 | 209 | |
| 188 | 210 | $message_success = $username . __( ' was successfully created!', 'learnpress' ); |
| 189 | 211 | |
| 190 | 212 | if ( $is_become_a_teacher ) { |
| 191 | - $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' ); | |
| 192 | 214 | } |
| 193 | 215 | |
| 194 | - 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 ); | |
| 195 | 221 | |
| 196 | 222 | if ( ! empty( $_POST['redirect'] ) ) { |
| 197 | - $redirect = wp_sanitize_redirect( wp_unslash( $_POST['redirect'] ) ); | |
| 223 | + $url_redirect = wp_sanitize_redirect( wp_unslash( $_POST['redirect'] ) ); | |
| 198 | 224 | } elseif ( ! empty( $_REQUEST['_wp_http_referer'] ) ) { |
| 199 | - $redirect = wp_unslash( $_REQUEST['_wp_http_referer'] ); | |
| 225 | + $url_redirect = wp_unslash( $_REQUEST['_wp_http_referer'] ); | |
| 200 | 226 | } else { |
| 201 | - $redirect = LP_Request::get_redirect( learn_press_get_page_link( 'profile' ) ); | |
| 227 | + $url_redirect = LP_Request::get_redirect( learn_press_get_page_link( 'profile' ) ); | |
| 202 | 228 | } |
| 203 | - | |
| 204 | - 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() ) ); | |
| 205 | 231 | exit(); |
| 206 | 232 | |
| 207 | - } catch ( Exception $e ) { | |
| 208 | - if ( $e->getMessage() ) { | |
| 209 | - learn_press_add_message( $e->getMessage(), 'error' ); | |
| 210 | - } | |
| 233 | + } catch ( Throwable $e ) { | |
| 234 | + $message_data = [ | |
| 235 | + 'status' => 'error', | |
| 236 | + 'content' => $e->getMessage(), | |
| 237 | + ]; | |
| 238 | + learn_press_set_message( $message_data ); | |
| 211 | 239 | } |
| 212 | 240 | } |
| 213 | 241 | |
| 214 | 242 | /** |
| @@ -214,92 +242,146 @@ | ||
| 214 | 242 | /** |
| 215 | 243 | * New create customer. |
| 216 | 244 | * |
| 217 | 245 | * @author ThimPress <nhamdv> |
| 246 | + * @version 1.0.1 | |
| 247 | + * @since 4.0.0 | |
| 218 | 248 | */ |
| 219 | - public static function learnpress_create_new_customer( $email, $username = '', $password = '', $confirm_password = '', $args = array(), $update_meta = array() ) { | |
| 220 | - if ( empty( $email ) || ! is_email( $email ) ) { | |
| 221 | - return new WP_Error( 'registration-error-invalid-email', __( 'Please provide a valid email address.', 'learnpress' ) ); | |
| 222 | - } | |
| 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 | + } | |
| 223 | 255 | |
| 224 | - if ( email_exists( $email ) ) { | |
| 225 | - return new WP_Error( 'registration-error-email-exists', apply_filters( 'learnpress_registration_error_email_exists', __( 'An account is already registered with your email address.', 'learnpress' ), $email ) ); | |
| 226 | - } | |
| 256 | + if ( empty( $email ) || ! is_email( $email ) ) { | |
| 257 | + throw new Exception( __( 'Please provide a valid email address.', 'learnpress' ), 101 ); | |
| 258 | + } | |
| 227 | 259 | |
| 228 | - $username = sanitize_user( $username ); | |
| 260 | + if ( email_exists( $email ) ) { | |
| 261 | + throw new Exception( __( 'An account is already registered with your email address.', 'learnpress' ), 102 ); | |
| 262 | + } | |
| 229 | 263 | |
| 230 | - if ( empty( $username ) || ! validate_username( $username ) ) { | |
| 231 | - return new WP_Error( 'registration-error-invalid-username', __( 'Please enter a valid account username.', 'learnpress' ) ); | |
| 232 | - } | |
| 264 | + $username = sanitize_user( $username ); | |
| 233 | 265 | |
| 234 | - if ( username_exists( $username ) ) { | |
| 235 | - return new WP_Error( 'registration-error-username-exists', __( 'An account is already registered with that username. Please choose another.', 'learnpress' ) ); | |
| 236 | - } | |
| 266 | + if ( empty( $username ) || ! validate_username( $username ) ) { | |
| 267 | + throw new Exception( __( 'Please enter a valid account username.', 'learnpress' ), 103 ); | |
| 268 | + } | |
| 237 | 269 | |
| 238 | - if ( apply_filters( 'learnpress_registration_generate_password', false ) ) { | |
| 239 | - $password = wp_generate_password(); | |
| 240 | - } | |
| 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 | + } | |
| 241 | 276 | |
| 242 | - if ( empty( $password ) ) { | |
| 243 | - return new WP_Error( 'registration-error-missing-password', __( 'Please enter an account password.', 'learnpress' ) ); | |
| 244 | - } | |
| 277 | + if ( apply_filters( 'learnpress_registration_generate_password', false ) ) { | |
| 278 | + $password = wp_generate_password(); | |
| 279 | + } | |
| 245 | 280 | |
| 246 | - if ( strlen( $password ) < 6 ) { | |
| 247 | - return new WP_Error( 'registration-error-short-password', __( 'Password is too short!', 'learnpress' ) ); | |
| 248 | - } | |
| 281 | + if ( empty( $password ) ) { | |
| 282 | + throw new Exception( __( 'Please enter an account password.', 'learnpress' ), 105 ); | |
| 283 | + } | |
| 249 | 284 | |
| 250 | - if ( preg_match( '#\s+#', $password ) ) { | |
| 251 | - return new WP_Error( 'registration-error-spacing-password', __( 'Password can not have spacing!', 'learnpress' ) ); | |
| 252 | - } | |
| 285 | + if ( strlen( $password ) <= 5 ) { | |
| 286 | + throw new Exception( __( 'Password must contain at least six characters.', 'learnpress' ), 106 ); | |
| 287 | + } | |
| 253 | 288 | |
| 254 | - if ( empty( $confirm_password ) ) { | |
| 255 | - return new WP_Error( 'registration-error-missing-confirm-password', __( 'Please enter confirm password.', 'learnpress' ) ); | |
| 256 | - } | |
| 289 | + if ( preg_match( '#\s+#', $password ) ) { | |
| 290 | + throw new Exception( __( 'Password can not contain spaces!', 'learnpress' ), 107 ); | |
| 291 | + } | |
| 257 | 292 | |
| 258 | - if ( $password !== $confirm_password ) { | |
| 259 | - return new WP_Error( 'registration-error-confirm-password', __( 'Confirmation password incorrect!.', 'learnpress' ) ); | |
| 260 | - } | |
| 293 | + if ( empty( $confirm_password ) ) { | |
| 294 | + throw new Exception( __( 'Please enter confirm password.', 'learnpress' ), 108 ); | |
| 295 | + } | |
| 261 | 296 | |
| 262 | - $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' ); | |
| 297 | + if ( $password !== $confirm_password ) { | |
| 298 | + throw new Exception( __( 'Password and Confirm Password does not match!', 'learnpress' ), 108 ); | |
| 299 | + } | |
| 263 | 300 | |
| 264 | - if ( $custom_fields && ! empty( $update_meta ) ) { | |
| 265 | - foreach ( $custom_fields as $field ) { | |
| 266 | - if ( $field['required'] === 'yes' && empty( $update_meta[ $field['id'] ] ) ) { | |
| 267 | - return new WP_Error( 'registration-custom-exists', $field['name'] . __( ' is required field.', 'learnpress' ) ); | |
| 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 | + } | |
| 268 | 307 | } |
| 269 | 308 | } |
| 270 | - } | |
| 271 | 309 | |
| 272 | - $errors = new WP_Error(); | |
| 310 | + do_action( 'lp/before_create_new_customer', $email, $username, $password, $confirm_password, $args, $update_meta ); | |
| 273 | 311 | |
| 274 | - do_action( 'learnpress_register_post', $username, $email, $errors ); | |
| 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 | + ); | |
| 275 | 323 | |
| 276 | - $errors = apply_filters( 'learnpress_registration_errors', $errors, $username, $email ); | |
| 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 | + } | |
| 277 | 330 | |
| 278 | - if ( $errors->get_error_code() ) { | |
| 279 | - return $errors; | |
| 280 | - } | |
| 331 | + $customer_id = wp_insert_user( $new_customer_data ); | |
| 281 | 332 | |
| 282 | - $new_customer_data = apply_filters( | |
| 283 | - 'learnpress_new_customer_data', | |
| 284 | - array_merge( | |
| 285 | - $args, | |
| 286 | - array( | |
| 287 | - 'user_login' => $username, | |
| 288 | - 'user_pass' => $password, | |
| 289 | - 'user_email' => $email, | |
| 290 | - ) | |
| 291 | - ) | |
| 292 | - ); | |
| 333 | + do_action( 'lp/after_create_new_customer', $email, $username, $password, $confirm_password, $args, $update_meta ); | |
| 293 | 334 | |
| 294 | - $customer_id = wp_insert_user( $new_customer_data ); | |
| 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 | + } | |
| 295 | 341 | |
| 296 | - if ( ! empty( $update_meta ) ) { | |
| 297 | - lp_user_custom_register_fields( $customer_id, $update_meta ); | |
| 298 | - } | |
| 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 | + } | |
| 299 | 382 | |
| 300 | - if ( is_wp_error( $customer_id ) ) { | |
| 301 | - return $customer_id; | |
| 383 | + return new WP_Error( $code_str, $e->getMessage() ); | |
| 302 | 384 | } |
| 303 | 385 | |
| 304 | 386 | return $customer_id; |
| 305 | 387 | } |
| @@ -316,22 +398,27 @@ | ||
| 316 | 398 | return new WP_Error( 'exist_display_name', esc_html__( 'Display name is required', 'learnpress' ) ); |
| 317 | 399 | } |
| 318 | 400 | |
| 319 | 401 | if ( is_email( $update_data['display_name'] ) ) { |
| 320 | - 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' ) ); | |
| 321 | 403 | } |
| 322 | 404 | |
| 323 | 405 | if ( ! is_email( $update_data['user_email'] ) ) { |
| 324 | - 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' ) ); | |
| 325 | 407 | } elseif ( email_exists( $update_data['user_email'] ) && $update_data['user_email'] !== $current_user->user_email ) { |
| 326 | 408 | return new WP_Error( 'error_email', esc_html__( 'This email address is already registered.', 'learnpress' ) ); |
| 327 | 409 | } |
| 328 | 410 | |
| 329 | - $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' ); | |
| 330 | - | |
| 411 | + $custom_fields = LP_Profile::get_register_fields_custom(); | |
| 331 | 412 | if ( $custom_fields && ! empty( $update_meta ) ) { |
| 332 | 413 | foreach ( $custom_fields as $field ) { |
| 333 | - 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 ) { | |
| 334 | 421 | return new WP_Error( 'registration-custom-exists', $field['name'] . __( ' is required field.', 'learnpress' ) ); |
| 335 | 422 | } |
| 336 | 423 | } |
| 337 | 424 | } |
| @@ -415,5 +502,4 @@ | ||
| 415 | 502 | self::process_login(); |
| 416 | 503 | self::process_register(); |
| 417 | 504 | } |
| 418 | 505 | } |
| 419 | - | |