verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ?? '' ) ), 'adsd_rcc_public_nonce' ) ) { wp_send_json_error( [ 'message' => __( 'Security check failed.', 'adsd-referral-couponcore' ) ] ); return; } $email = sanitize_email( wp_unslash( $_POST['adsd_rcc_email'] ?? '' ) ); $ref_code = sanitize_text_field( wp_unslash( $_POST['adsd_rcc_ref_code'] ?? '' ) ); // phpcs:enable WordPress.Security.NonceVerification.Missing if ( ! is_email( $email ) ) { wp_send_json_error( [ 'message' => __( 'Please enter a valid email address.', 'adsd-referral-couponcore' ) ] ); return; } $ip = ADSD_RCC_Crypto::get_client_ip(); $guard = ADSD_RCC_Fraud_Guard::instance(); $abuse = $guard->check_registration_abuse( $ip ); if ( is_wp_error( $abuse ) ) { wp_send_json_error( [ 'message' => $abuse->get_error_message() ] ); return; } if ( email_exists( $email ) ) { wp_send_json_error( [ 'message' => __( 'This email is already registered. Please log in.', 'adsd-referral-couponcore' ) ] ); return; } // Resolve referral $referral_id = null; if ( $ref_code ) { $db = ADSD_RCC_DB::instance(); $link = $db->get_link_by_token( $ref_code ); if ( ! $link ) { wp_send_json_error( [ 'message' => __( 'Invalid referral code.', 'adsd-referral-couponcore' ) ] ); return; } $engine = ADSD_RCC_Referral_Engine::instance(); $result = $engine->record_pending_referral( $email, $ref_code ); if ( is_wp_error( $result ) ) { wp_send_json_error( [ 'message' => $result->get_error_message() ] ); return; } $referral_id = $result; } // Generate verification token only — password is set by the user after verification $verify_token = ADSD_RCC_Crypto::token( 32 ); $expiry = gmdate( 'Y-m-d H:i:s', strtotime( '+' . ADSD_RCC_Settings::instance()->verification_expiry_hours() . ' hours' ) ); $db_result = ADSD_RCC_DB::instance()->insert_verification( [ 'email' => $email, 'token' => $verify_token, 'referral_id' => $referral_id, 'temp_password' => '', // No temp password — user sets password after verification 'expires_at' => $expiry, 'ip_address' => $ip, 'device_hash' => ADSD_RCC_Crypto::device_hash(), ] ); if ( ! $db_result ) { wp_send_json_error( [ 'message' => __( 'Registration failed. Please try again.', 'adsd-referral-couponcore' ) ] ); return; } // Build verification URL $verify_url = add_query_arg( [ 'adsd_rcc_action' => 'verify_email', 'token' => $verify_token, ], home_url( '/' ) ); // Send verification email (no password in email — user sets password after clicking verify) $mailer = ADSD_RCC_Mailer::instance(); $sent = $mailer->send( $email, 'verification', [ '{user_email}' => $email, '{user_password}' => '', // Intentionally blank — password is set after verification '{verification_link}' => $verify_url, '{button_url}' => $verify_url, ] ); if ( ! $sent ) { // Log the email failure for the admin, but don't expose SMTP errors to the user. ADSD_RCC_DB::instance()->log( 'registration_mail_failed', null, null, 'Verification email failed for: ' . $email, [ 'error' => $mailer->get_last_error() ] ); // Account record IS saved — show a green success message. // The admin can diagnose email issues via the plugin log; the user sees a clean message. wp_send_json_success( [ 'message' => __( 'Your account was created! Please check your email to verify your address. If you don\'t receive it, contact the site admin.', 'adsd-referral-couponcore' ), ] ); return; } wp_send_json_success( [ 'message' => __( 'Almost there! A verification email has been sent to your address. Please click the link inside to activate your account.', 'adsd-referral-couponcore' ), ] ); } // ─── Email Verification ─────────────────────────────────────────────────── public function maybe_verify_email_from_url(): void { // phpcs:disable WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput -- verification link from email; random token IS the security credential if ( ( $_GET['adsd_rcc_action'] ?? '' ) !== 'verify_email' ) { return; } $token = sanitize_text_field( wp_unslash( $_GET['token'] ?? '' ) ); // phpcs:enable WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput if ( ! $token ) { return; } $this->process_verification( $token ); } public function handle_verify_email(): void { // phpcs:disable WordPress.Security.NonceVerification.Recommended,WordPress.Security.NonceVerification.Missing // The random token in the URL is the security credential for email verification. // No nonce is needed: the token itself proves the user received the email. $token = sanitize_text_field( wp_unslash( $_GET['token'] ?? $_POST['token'] ?? '' ) ); // phpcs:enable WordPress.Security.NonceVerification.Recommended,WordPress.Security.NonceVerification.Missing $this->process_verification( $token ); } private function process_verification( string $token ): void { if ( ! $token ) { $this->verification_redirect( 'error', __( 'Invalid verification link.', 'adsd-referral-couponcore' ) ); return; } $db = ADSD_RCC_DB::instance(); $row = $db->get_verification_by_token( $token ); if ( ! $row ) { $this->verification_redirect( 'error', __( 'This verification link is invalid or has expired.', 'adsd-referral-couponcore' ) ); return; } // Check if user already exists (re-click prevention) if ( email_exists( $row->email ) ) { $this->verification_redirect( 'info', __( 'Your email has already been verified. Please log in.', 'adsd-referral-couponcore' ) ); return; } // Derive username from the email prefix (e.g. abcd123@mail.com → abcd123) $username_base = sanitize_user( strstr( $row->email, '@', true ), true ); $username = $username_base; $suffix = 1; while ( username_exists( $username ) ) { $username = $username_base . $suffix; $suffix++; } // Create WordPress user with a temporary random password. // The user will set their own password via the adsd_rcc_set_password form. // Use 'customer' role when WooCommerce is active; otherwise fall back to 'subscriber'. $user_role = ADSD_RCC_Plugin::is_woocommerce_active() ? 'customer' : 'subscriber'; $temp_password = ADSD_RCC_Crypto::random_password(); // Suppress WP's default new-user notification email before creating the user. // The plugin sends its own verification + set-password email chain. add_filter( 'wp_send_new_user_notifications', '__return_false' ); $user_id = wp_insert_user( [ 'user_login' => $username, 'user_email' => $row->email, 'user_pass' => $temp_password, 'role' => $user_role, ] ); // Restore default behaviour for any other user creation on the same request. remove_filter( 'wp_send_new_user_notifications', '__return_false' ); if ( is_wp_error( $user_id ) ) { $this->verification_redirect( 'error', $user_id->get_error_message() ); return; } // Mark verification complete $db->mark_verification_complete( (int) $row->id ); // Confirm referral if present if ( $row->referral_id ) { ADSD_RCC_Referral_Engine::instance()->confirm_referral( (int) $row->referral_id, $user_id ); } $db->log( 'email_verified', $user_id, (int) $row->id, 'Email verified and account created.' ); // Generate a WordPress password-reset key so the user can set their own password. // We redirect them to the plugin's own [adsd_rcc_set_password] page (configured in Settings → Section 6). $user_obj = get_userdata( $user_id ); $reset_key = get_password_reset_key( $user_obj ); if ( is_wp_error( $reset_key ) ) { // Fallback: send to login page and let them use "forgot password" $this->verification_redirect( 'info', __( 'Email verified! Please use the login page to set your password.', 'adsd-referral-couponcore' ) ); return; } // Build the link to the [adsd_rcc_set_password] page with key + login as query params $set_pw_page = ADSD_RCC_Settings::instance()->page_url( 'set_password' ); $redirect = add_query_arg( [ 'adsd_rcc_key' => rawurlencode( $reset_key ), 'adsd_rcc_login' => rawurlencode( $user_obj->user_login ), ], $set_pw_page ); wp_safe_redirect( $redirect ); exit; } private function verification_redirect( string $type, string $message ): void { $login_url = ADSD_RCC_Settings::instance()->page_url( 'login' ); $redirect = add_query_arg( [ 'adsd_rcc_notice' => $type, 'adsd_rcc_message' => urlencode( $message ), ], $login_url ); wp_safe_redirect( $redirect ); exit; } // ─── WooCommerce Registration Integration ───────────────────────────────── // phpcs:disable WordPress.Security.NonceVerification.Missing -- WooCommerce handles nonce for registration form public function woo_referral_field(): void { ?>

get_session_token(); } if ( ! $ref_code ) { return; } $email = $new_customer_data['user_email'] ?? ''; $engine = ADSD_RCC_Referral_Engine::instance(); $result = $engine->record_pending_referral( $email, $ref_code ); if ( ! is_wp_error( $result ) ) { // For WooCommerce, the email is already verified by WC — confirm immediately $engine->confirm_referral( (int) $result, $customer_id ); ADSD_RCC_Referral_Engine::instance()->clear_session_token(); } } }