| @@ -60,12 +60,14 @@ | ||
| 60 | 60 | * @since 4.0.0 |
| 61 | 61 | */ |
| 62 | 62 | protected $checkout_action = ''; |
| 63 | 63 | |
| 64 | - public function init() { | |
| 64 | + public function init(): void { | |
| 65 | 65 | if ( ! is_null( LearnPress::instance()->session ) ) { |
| 66 | 66 | $this->_checkout_email = LearnPress::instance()->session->get( 'checkout-email' ); |
| 67 | 67 | } |
| 68 | + | |
| 69 | + add_action( 'set_logged_in_cookie', [ $this, 'set_logged_in_cookie' ] ); | |
| 68 | 70 | } |
| 69 | 71 | |
| 70 | 72 | /** |
| 71 | 73 | * Create account when checking out with user guest and tick create account. |
| @@ -95,11 +97,18 @@ | ||
| 95 | 97 | */ |
| 96 | 98 | public function check_validate_fields() { |
| 97 | 99 | $session = LearnPress::instance()->session; |
| 98 | 100 | $data_session_before_user_login = $session->get_session_data(); |
| 99 | - $checkout_account_type = LP_Request::get_param( 'checkout-account-switch-form' ); | |
| 100 | - $this->checkout_action = $checkout_account_type; | |
| 101 | + $this->checkout_action = null; | |
| 101 | 102 | |
| 103 | + if ( isset( $_POST['reg_email'] ) ) { | |
| 104 | + $this->checkout_action = 'register'; | |
| 105 | + } elseif ( isset( $_POST['username'] ) ) { | |
| 106 | + $this->checkout_action = 'login'; | |
| 107 | + } elseif ( isset( $_POST['guest_email'] ) ) { | |
| 108 | + $this->checkout_action = 'guest'; | |
| 109 | + } | |
| 110 | + | |
| 102 | 111 | switch ( $this->checkout_action ) { |
| 103 | 112 | case 'login': |
| 104 | 113 | $user = wp_signon( |
| 105 | 114 | array( |
| @@ -149,22 +158,23 @@ | ||
| 149 | 158 | |
| 150 | 159 | if ( is_wp_error( $user_id ) ) { |
| 151 | 160 | throw new Exception( $user_id->get_error_message() ); |
| 152 | 161 | } else { |
| 153 | - $user = wp_signon( | |
| 154 | - array( | |
| 155 | - 'user_login' => $default_fields['reg_email'], | |
| 156 | - 'user_password' => $default_fields['reg_password'], | |
| 157 | - 'remember' => 1, | |
| 158 | - ), | |
| 159 | - is_ssl() | |
| 160 | - ); | |
| 162 | + // Directly authenticate the newly created user to bypass captcha plugins | |
| 163 | + // that hook into 'authenticate'. We already verified the password during registration. | |
| 164 | + $user = get_user_by( 'id', $user_id ); | |
| 161 | 165 | |
| 162 | - if ( is_wp_error( $user ) ) { | |
| 163 | - throw new Exception( $user->get_error_message() ); | |
| 164 | - } else { | |
| 165 | - wp_set_current_user( $user->ID ); | |
| 166 | + if ( ! $user instanceof WP_User ) { | |
| 167 | + throw new Exception( __( 'User registration succeeded but the user cannot be loaded.', 'learnpress' ) ); | |
| 166 | 168 | } |
| 169 | + | |
| 170 | + /*if ( ! wp_check_password( $default_fields['reg_password'], $user->user_pass, $user->ID ) ) { | |
| 171 | + throw new Exception( __( 'Incorrect password.', 'learnpress' ) ); | |
| 172 | + }*/ | |
| 173 | + | |
| 174 | + wp_set_current_user( $user->ID ); | |
| 175 | + wp_set_auth_cookie( $user->ID, true, is_ssl() ); | |
| 176 | + do_action( 'wp_login', $user->user_login, $user ); | |
| 167 | 177 | } |
| 168 | 178 | break; |
| 169 | 179 | case 'guest': |
| 170 | 180 | $email_guest = LP_Request::get_param( 'guest_email' ); |
| @@ -254,9 +264,13 @@ | ||
| 254 | 264 | |
| 255 | 265 | $user_id = $this->create_account(); |
| 256 | 266 | if ( $user_id ) { |
| 257 | 267 | // Notify mail create user success |
| 258 | - wp_new_user_notification( $user_id, null, apply_filters( 'learn-press/email-create-new-user-when-checkout', 'user' ) ); | |
| 268 | + wp_new_user_notification( | |
| 269 | + $user_id, | |
| 270 | + null, | |
| 271 | + apply_filters( 'learn-press/email-create-new-user-when-checkout', 'user' ) | |
| 272 | + ); | |
| 259 | 273 | } else { |
| 260 | 274 | throw new Exception( __( 'Create account failed', 'learnpress' ), 0 ); |
| 261 | 275 | } |
| 262 | 276 | } |
| @@ -568,6 +582,27 @@ | ||
| 568 | 582 | } |
| 569 | 583 | |
| 570 | 584 | ini_set( 'max_execution_time', LearnPress::$time_limit_default_of_sever ); |
| 571 | 585 | learn_press_send_json( $result ); |
| 586 | + } | |
| 587 | + | |
| 588 | + /** | |
| 589 | + * Sync $_COOKIE[LOGGED_IN_COOKIE] within the current checkout request. | |
| 590 | + * | |
| 591 | + * Checkout runs over AJAX. When login/register succeeds, wp_signon() only | |
| 592 | + * sends a Set-Cookie header to the browser (effective on the next request) | |
| 593 | + * and does not update $_COOKIE for the current request. Hooking into | |
| 594 | + * 'set_logged_in_cookie' lets us grab the newly generated cookie value and | |
| 595 | + * assign it to $_COOKIE manually, so subsequent code in the same request | |
| 596 | + * (e.g. creating the order) correctly recognizes the logged-in user. | |
| 597 | + * LPBackgroundAjax use $_COOKIE to send mails. | |
| 598 | + * | |
| 599 | + * @param string $logged_in_cookie The newly generated logged_in cookie value. | |
| 600 | + */ | |
| 601 | + public function set_logged_in_cookie( $logged_in_cookie ) { | |
| 602 | + if ( ! isset( $_POST['learn-press-checkout-nonce'] ) ) { | |
| 603 | + return; | |
| 604 | + } | |
| 605 | + | |
| 606 | + $_COOKIE[ LOGGED_IN_COOKIE ] = $logged_in_cookie; | |
| 572 | 607 | } |
| 573 | 608 | } |