| @@ -7,10 +7,8 @@ | ||
| 7 | 7 | * |
| 8 | 8 | * @package Forumax |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | -defined( 'ABSPATH' ) || exit; | |
| 12 | - | |
| 13 | 11 | /** |
| 14 | 12 | * Render the tabbed login and registration form. |
| 15 | 13 | * |
| 16 | 14 | * @param array $atts Shortcode attributes. |
| @@ -40,15 +38,8 @@ | ||
| 40 | 38 | |
| 41 | 39 | $can_register = get_option( 'users_can_register' ); |
| 42 | 40 | $wrapper_id = $uid; |
| 43 | 41 | |
| 44 | - // Errors passed back by forumax_login_failed_redirect() / forumax_handle_registration(). | |
| 45 | - // phpcs:disable WordPress.Security.NonceVerification.Recommended -- read-only display flags. | |
| 46 | - $login_failed = isset( $_GET['login'] ) && 'failed' === sanitize_key( wp_unslash( $_GET['login'] ) ); | |
| 47 | - $reg_errors = isset( $_GET['reg_error'] ) ? array_filter( array_map( 'sanitize_key', explode( ',', sanitize_text_field( wp_unslash( $_GET['reg_error'] ) ) ) ) ) : array(); | |
| 48 | - // phpcs:enable | |
| 49 | - $active_tab = ( $can_register && ! empty( $reg_errors ) ) ? 'register' : 'login'; | |
| 50 | - | |
| 51 | 42 | ob_start(); |
| 52 | 43 | ?> |
| 53 | 44 | <div class="frmx-auth-tabs" id="<?php echo esc_attr( $wrapper_id ); ?>"> |
| 54 | 45 | |
| @@ -53,12 +44,12 @@ | ||
| 53 | 44 | <div class="frmx-auth-tabs" id="<?php echo esc_attr( $wrapper_id ); ?>"> |
| 54 | 45 | |
| 55 | 46 | <?php if ( $can_register ) : ?> |
| 56 | 47 | <div class="frmx-auth-tabs-header"> |
| 57 | - <button type="button" class="frmx-auth-tab<?php echo 'login' === $active_tab ? ' frmx-auth-tab-active' : ''; ?>" data-frmx-tab="login"> | |
| 48 | + <button type="button" class="frmx-auth-tab frmx-auth-tab-active" data-frmx-tab="login"> | |
| 58 | 49 | <?php esc_html_e( 'Sign In', 'forumax' ); ?> |
| 59 | 50 | </button> |
| 60 | - <button type="button" class="frmx-auth-tab<?php echo 'register' === $active_tab ? ' frmx-auth-tab-active' : ''; ?>" data-frmx-tab="register"> | |
| 51 | + <button type="button" class="frmx-auth-tab" data-frmx-tab="register"> | |
| 61 | 52 | <?php esc_html_e( 'Create Account', 'forumax' ); ?> |
| 62 | 53 | </button> |
| 63 | 54 | </div> |
| 64 | 55 | <?php endif; ?> |
| @@ -63,14 +54,9 @@ | ||
| 63 | 54 | </div> |
| 64 | 55 | <?php endif; ?> |
| 65 | 56 | |
| 66 | 57 | <!-- Login Tab --> |
| 67 | - <div class="frmx-auth-panel<?php echo 'login' === $active_tab ? ' frmx-auth-panel-active' : ''; ?>" data-frmx-panel="login"> | |
| 68 | - <?php if ( $login_failed ) : ?> | |
| 69 | - <div class="frmx-auth-error bbp-template-notice error" role="alert"> | |
| 70 | - <p><?php esc_html_e( 'Invalid username, email address or password. Please try again.', 'forumax' ); ?></p> | |
| 71 | - </div> | |
| 72 | - <?php endif; ?> | |
| 58 | + <div class="frmx-auth-panel frmx-auth-panel-active" data-frmx-panel="login"> | |
| 73 | 59 | <?php |
| 74 | 60 | $form_html = wp_login_form( array( |
| 75 | 61 | 'redirect' => $redirect_url, |
| 76 | 62 | 'label_username' => esc_html__( 'Username or Email', 'forumax' ), |
| @@ -108,11 +94,8 @@ | ||
| 108 | 94 | '$1 placeholder="' . $password_placeholder . '"$2', |
| 109 | 95 | $form_html |
| 110 | 96 | ); |
| 111 | 97 | |
| 112 | - // Marks submissions from this form so only they are redirected back on failure. | |
| 113 | - $form_html = preg_replace( '/<\/form>/i', '<input type="hidden" name="forumax_login" value="1" /></form>', $form_html, 1 ); | |
| 114 | - | |
| 115 | 98 | echo $form_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 116 | 99 | ?> |
| 117 | 100 | |
| 118 | 101 | <?php |
| @@ -124,16 +107,9 @@ | ||
| 124 | 107 | </div> |
| 125 | 108 | |
| 126 | 109 | <?php if ( $can_register ) : ?> |
| 127 | 110 | <!-- Registration Tab --> |
| 128 | - <div class="frmx-auth-panel<?php echo 'register' === $active_tab ? ' frmx-auth-panel-active' : ''; ?>" data-frmx-panel="register"> | |
| 129 | - <?php if ( ! empty( $reg_errors ) ) : ?> | |
| 130 | - <div class="frmx-auth-error bbp-template-notice error" role="alert"> | |
| 131 | - <?php foreach ( $reg_errors as $reg_error ) : ?> | |
| 132 | - <p><?php echo esc_html( forumax_registration_error_message( $reg_error ) ); ?></p> | |
| 133 | - <?php endforeach; ?> | |
| 134 | - </div> | |
| 135 | - <?php endif; ?> | |
| 111 | + <div class="frmx-auth-panel" data-frmx-panel="register"> | |
| 136 | 112 | <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="frmx-auth-register-form"> |
| 137 | 113 | <input type="hidden" name="action" value="forumax_register" /> |
| 138 | 114 | <input type="hidden" name="redirect_to" value="<?php echo esc_url( $redirect_url ); ?>" /> |
| 139 | 115 | |
| @@ -249,18 +225,11 @@ | ||
| 249 | 225 | * |
| 250 | 226 | * @return void |
| 251 | 227 | */ |
| 252 | 228 | function forumax_login_failed_redirect( $username ) { |
| 253 | - // Only handle the Forumax form; other login forms (WooCommerce, REST, | |
| 254 | - // XML-RPC, wp-login.php) keep their own error handling. | |
| 255 | - // phpcs:ignore WordPress.Security.NonceVerification.Missing -- core login has no nonce; this only picks the redirect target. | |
| 256 | - if ( empty( $_POST['forumax_login'] ) ) { | |
| 257 | - return; | |
| 258 | - } | |
| 259 | - | |
| 260 | 229 | $referrer = wp_get_referer(); |
| 261 | 230 | if ( ! empty( $referrer ) && ! str_contains( $referrer, 'wp-login.php' ) ) { |
| 262 | - wp_safe_redirect( add_query_arg( 'login', 'failed', remove_query_arg( array( 'login', 'reg_error' ), $referrer ) ) ); | |
| 231 | + wp_redirect( add_query_arg( 'login', 'failed', $referrer ) ); | |
| 263 | 232 | exit; |
| 264 | 233 | } |
| 265 | 234 | } |
| 266 | 235 | |
| @@ -266,29 +235,8 @@ | ||
| 266 | 235 | |
| 267 | 236 | add_action( 'wp_login_failed', 'forumax_login_failed_redirect' ); |
| 268 | 237 | |
| 269 | 238 | /** |
| 270 | - * Human readable message for a registration error code. | |
| 271 | - * | |
| 272 | - * @param string $code Error code. | |
| 273 | - * @return string | |
| 274 | - */ | |
| 275 | -function forumax_registration_error_message( $code ) { | |
| 276 | - $messages = array( | |
| 277 | - 'empty_username' => __( 'Please enter a username.', 'forumax' ), | |
| 278 | - 'invalid_username' => __( 'This username is invalid. Please use letters, numbers and - _ . @ only.', 'forumax' ), | |
| 279 | - 'existing_user_login' => __( 'This username is already registered. Please choose another one.', 'forumax' ), | |
| 280 | - 'invalid_email' => __( 'Please enter a valid email address.', 'forumax' ), | |
| 281 | - 'email_exists' => __( 'This email address is already registered.', 'forumax' ), | |
| 282 | - 'existing_user_email' => __( 'This email address is already registered.', 'forumax' ), | |
| 283 | - 'empty_password' => __( 'Please enter a password.', 'forumax' ), | |
| 284 | - 'password_mismatch' => __( 'The passwords do not match.', 'forumax' ), | |
| 285 | - ); | |
| 286 | - | |
| 287 | - return $messages[ $code ] ?? __( 'Registration failed. Please check the form and try again.', 'forumax' ); | |
| 288 | -} | |
| 289 | - | |
| 290 | -/** | |
| 291 | 239 | * Handle the custom registration form submission. |
| 292 | 240 | * |
| 293 | 241 | * Creates a new user with the provided username, email, and password, |
| 294 | 242 | * auto-logs them in, and redirects to the referring page. |
| @@ -335,34 +283,10 @@ | ||
| 335 | 283 | if ( $user_pass !== $user_pass_confirm ) { |
| 336 | 284 | $errors[] = 'password_mismatch'; |
| 337 | 285 | } |
| 338 | 286 | |
| 339 | - if ( empty( $errors ) ) { | |
| 340 | - if ( ! validate_username( $user_login ) ) { | |
| 341 | - $errors[] = 'invalid_username'; | |
| 342 | - } elseif ( username_exists( $user_login ) ) { | |
| 343 | - $errors[] = 'existing_user_login'; | |
| 344 | - } | |
| 345 | - | |
| 346 | - if ( email_exists( $user_email ) ) { | |
| 347 | - $errors[] = 'email_exists'; | |
| 348 | - } | |
| 349 | - } | |
| 350 | - | |
| 351 | - // Let CAPTCHA / anti-spam plugins that print into `register_form` | |
| 352 | - // validate the submission too, as core registration does. | |
| 353 | - if ( empty( $errors ) ) { | |
| 354 | - $wp_errors = new WP_Error(); | |
| 355 | - do_action( 'register_post', $user_login, $user_email, $wp_errors ); | |
| 356 | - $wp_errors = apply_filters( 'registration_errors', $wp_errors, $user_login, $user_email ); | |
| 357 | - | |
| 358 | - if ( is_wp_error( $wp_errors ) && $wp_errors->has_errors() ) { | |
| 359 | - $errors = array_merge( $errors, $wp_errors->get_error_codes() ); | |
| 360 | - } | |
| 361 | - } | |
| 362 | - | |
| 363 | 287 | if ( ! empty( $errors ) ) { |
| 364 | - wp_safe_redirect( add_query_arg( 'reg_error', implode( ',', array_unique( $errors ) ), remove_query_arg( array( 'login', 'reg_error' ), $referrer ) ) ); | |
| 288 | + wp_safe_redirect( add_query_arg( 'reg_error', implode( ',', $errors ), $referrer ) ); | |
| 365 | 289 | exit; |
| 366 | 290 | } |
| 367 | 291 | |
| 368 | 292 | // Let external plugins validate registration (e.g. Envato license check). |
| @@ -379,8 +303,11 @@ | ||
| 379 | 303 | $error_code = $user_id->get_error_code(); |
| 380 | 304 | wp_safe_redirect( add_query_arg( 'reg_error', $error_code, $referrer ) ); |
| 381 | 305 | exit; |
| 382 | 306 | } |
| 307 | + | |
| 308 | + // Fire standard WordPress registration action. | |
| 309 | + do_action( 'user_register', $user_id ); | |
| 383 | 310 | |
| 384 | 311 | // Auto-login the new user. |
| 385 | 312 | wp_set_current_user( $user_id ); |
| 386 | 313 | wp_set_auth_cookie( $user_id, true ); |