| | |
| | | |
| | | use JetBrains\PhpStorm\NoReturn; |
| | | use JVBase\base\Site; |
| | | use JVBase\inc\managers\ErrorHelper; |
| | | use JVBase\Instance; |
| | | use JVBase\integrations\services\Cloudflare; |
| | | use JVBase\meta\Form; |
| | | use JVBase\rest\routes\LoginRoutes; |
| | | use JVBase\ui\Tabs; |
| | | use WP_User; |
| | | use WP_Error; |
| | |
| | | */ |
| | | class MagicLinks |
| | | { |
| | | use Instance; |
| | | use Instance, ErrorHelper; |
| | | protected Cache $cache; |
| | | protected Cache $referral_cache; |
| | | protected Cache $rateLimitCache; |
| | |
| | | /** |
| | | * Process login via magic link |
| | | */ |
| | | #[NoReturn] |
| | | protected function processLogin(array $token_data): void |
| | | { |
| | | $user = get_user_by('ID', $token_data['user_id']); |
| | |
| | | #[NoReturn] protected function processSignup(array $token_data): void |
| | | { |
| | | if (!array_key_exists('email', $token_data) || !array_key_exists('name', $token_data)) { |
| | | JVB()->error()->log('MagicLinkManager', 'Could not process Signup'); |
| | | $this->logError('Could not process signup', 'processSignup', ['token_data' => $token_data]); |
| | | |
| | | $this->cleanURL(); |
| | | } |
| | | $user_id = wp_create_user( |
| | |
| | | protected function processReferralSignup(array $token_data): void |
| | | { |
| | | if (!array_key_exists('email', $token_data) || !array_key_exists('name', $token_data)) { |
| | | JVB()->error()->log('[MagicLinkManager]Could not process Referral Signup'); |
| | | $this->logError('Could not process referral signup', 'processReferralSignup', ['token_data' => $token_data]); |
| | | return; |
| | | } |
| | | |
| | |
| | | 'role' => $role |
| | | ]); |
| | | if (!is_wp_error($user_id)) { |
| | | $response = JVB()->routes('login')->login($email, $pass, true); |
| | | if ($response) { |
| | | $response = LoginRoutes::getInstance()->login($email, $pass, true); |
| | | if (!is_wp_error($response)) { |
| | | wp_safe_redirect(home_url('/dash?welcome=1&referral=1')); |
| | | exit; |
| | | } |
| | | } else { |
| | | JVB()->error()->log( |
| | | '[MagicLinkManager]', |
| | | $user_id->get_error_message(), |
| | | $token_data |
| | | ); |
| | | $this->logError($user_id->get_error_message(), 'processReferralSignup'); |
| | | |
| | | $this->cleanURL(); |
| | | } |
| | | } |
| | |
| | | /** |
| | | * Process password reset via magic link |
| | | */ |
| | | #[NoReturn] |
| | | protected function processPasswordReset(array $token_data): void |
| | | { |
| | | $user = get_user_by('ID', $token_data['user_id']); |
| | | |
| | | if (!$user) { |
| | | JVB()->error()->log( |
| | | 'MagicLinkManager', |
| | | 'Invalid user, could not reset password', |
| | | ); |
| | | $this->logError('Invalid user - could not reset password', 'processPasswordReset', ['token_data' => $token_data]); |
| | | $this->cleanURL(); |
| | | } |
| | | |