From 654d6d6da81c77fbafe973b23b286f940ea88628 Mon Sep 17 00:00:00 2001 From: Alex P Date: Mon, 23 Jan 2023 11:44:45 +0200 Subject: [PATCH] Validate new user creation --- .../src/Validation/CheckoutFormValidator.php | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/modules/ppcp-button/src/Validation/CheckoutFormValidator.php b/modules/ppcp-button/src/Validation/CheckoutFormValidator.php index fd686b953..0fc579638 100644 --- a/modules/ppcp-button/src/Validation/CheckoutFormValidator.php +++ b/modules/ppcp-button/src/Validation/CheckoutFormValidator.php @@ -39,6 +39,45 @@ class CheckoutFormValidator extends WC_Checkout { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged @$this->validate_checkout( $data, $errors ); + if ( + apply_filters( 'woocommerce_paypal_payments_early_wc_checkout_account_creation_validation_enabled', true ) && + ! is_user_logged_in() && ( $this->is_registration_required() || ! empty( $data['createaccount'] ) ) + ) { + $username = ! empty( $data['account_username'] ) ? $data['account_username'] : ''; + $email = $data['billing_email'] ?? ''; + + if ( email_exists( $email ) ) { + $errors->add( + 'registration-error-email-exists', + apply_filters( + 'woocommerce_registration_error_email_exists', + // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch + __( 'An account is already registered with your email address. Please log in.', 'woocommerce' ), + $email + ) + ); + } + + if ( $username ) { // Empty username is already checked in validate_checkout, and it can be generated. + $username = sanitize_user( $username ); + if ( empty( $username ) || ! validate_username( $username ) ) { + $errors->add( + 'registration-error-invalid-username', + // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch + __( 'Please enter a valid account username.', 'woocommerce' ) + ); + } + + if ( username_exists( $username ) ) { + $errors->add( + 'registration-error-username-exists', + // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch + __( 'An account is already registered with that username. Please choose another.', 'woocommerce' ) + ); + } + } + } + if ( $errors->has_errors() ) { throw new ValidationException( $errors->get_error_messages() ); }