init_hooks(); } /** * Initialize hooks */ private function init_hooks() { add_action('wp_ajax_aha_mail_smtp_wizard_step', array($this, 'handle_wizard_step')); add_action('wp_ajax_aha_mail_smtp_wizard_complete', array($this, 'complete_wizard')); add_action('wp_ajax_aha_mail_smtp_wizard_skip', array($this, 'skip_wizard')); } /** * Check if wizard should be shown */ public static function should_show_wizard() { // Show wizard if SMTP is not configured and wizard hasn't been completed/skipped return !AHA_Mail_SMTP_Settings::is_smtp_configured() && !get_option('aha_mail_smtp_wizard_completed') && !get_option('aha_mail_smtp_wizard_skipped'); } /** * Get wizard steps */ public static function get_wizard_steps() { return array( 'welcome' => array( 'title' => __('Welcome', 'aha-mail-smtp'), 'description' => __('Welcome to AHA Mail SMTP setup wizard', 'aha-mail-smtp') ), 'provider' => array( 'title' => __('Choose Provider', 'aha-mail-smtp'), 'description' => __('Select your email service provider', 'aha-mail-smtp') ), 'settings' => array( 'title' => __('SMTP Settings', 'aha-mail-smtp'), 'description' => __('Configure your SMTP connection', 'aha-mail-smtp') ), 'authentication' => array( 'title' => __('Authentication', 'aha-mail-smtp'), 'description' => __('Set up email authentication', 'aha-mail-smtp') ), 'test' => array( 'title' => __('Test Email', 'aha-mail-smtp'), 'description' => __('Send a test email to verify setup', 'aha-mail-smtp') ), 'complete' => array( 'title' => __('Complete', 'aha-mail-smtp'), 'description' => __('Setup completed successfully', 'aha-mail-smtp') ) ); } /** * Render wizard */ public static function render_wizard() { $steps = self::get_wizard_steps(); $providers = AHA_Mail_SMTP_Core::get_smtp_providers(); ?>
handle_provider_step($data); break; case 'settings': $this->handle_settings_step($data); break; case 'authentication': $this->handle_authentication_step($data); break; case 'test': $this->handle_test_step($data); break; default: wp_send_json_success(); } } /** * Handle provider step */ private function handle_provider_step($data) { $provider = sanitize_text_field($data['provider'] ?? ''); if (empty($provider)) { wp_send_json_error(__('Please select a provider', 'aha-mail-smtp')); } // Save provider selection AHA_Mail_SMTP_Settings::set('provider', $provider); // Get provider settings $providers = AHA_Mail_SMTP_Core::get_smtp_providers(); $provider_config = $providers[$provider] ?? array(); wp_send_json_success(array( 'provider_config' => $provider_config, 'message' => sprintf(__('Selected %s as your email provider', 'aha-mail-smtp'), $provider_config['name'] ?? $provider) )); } /** * Handle settings step */ private function handle_settings_step($data) { $settings = array( 'smtp_host' => sanitize_text_field($data['smtp_host'] ?? ''), 'smtp_port' => (int) ($data['smtp_port'] ?? 587), 'smtp_encryption' => sanitize_text_field($data['smtp_encryption'] ?? 'tls'), 'from_email' => sanitize_email($data['from_email'] ?? ''), 'from_name' => sanitize_text_field($data['from_name'] ?? ''), 'smtp_auth' => true ); // Validate required fields if (empty($settings['smtp_host'])) { wp_send_json_error(__('SMTP host is required', 'aha-mail-smtp')); } if (empty($settings['from_email']) || !is_email($settings['from_email'])) { wp_send_json_error(__('Valid from email is required', 'aha-mail-smtp')); } // Save settings foreach ($settings as $key => $value) { AHA_Mail_SMTP_Settings::set($key, $value); } wp_send_json_success(__('SMTP settings saved successfully', 'aha-mail-smtp')); } /** * Handle authentication step */ private function handle_authentication_step($data) { $auth_type = sanitize_text_field($data['auth_type'] ?? 'password'); if ($auth_type === 'oauth') { // OAuth will be handled separately wp_send_json_success(__('OAuth authentication configured', 'aha-mail-smtp')); } else { // Password authentication $username = sanitize_text_field($data['smtp_username'] ?? ''); $password = $data['smtp_password'] ?? ''; if (empty($username) || empty($password)) { wp_send_json_error(__('Username and password are required', 'aha-mail-smtp')); } AHA_Mail_SMTP_Settings::set('smtp_username', $username); AHA_Mail_SMTP_Settings::set('smtp_password', $password); wp_send_json_success(__('Authentication configured successfully', 'aha-mail-smtp')); } } /** * Handle test step */ private function handle_test_step($data) { $test_email = sanitize_email($data['test_email'] ?? ''); if (empty($test_email) || !is_email($test_email)) { wp_send_json_error(__('Valid test email address is required', 'aha-mail-smtp')); } // Send test email $result = AHA_Mail_SMTP_Mailer::send_test_email( $test_email, __('AHA Mail SMTP Setup Test', 'aha-mail-smtp'), __('Congratulations! Your SMTP configuration is working correctly. This test email was sent during the setup wizard.', 'aha-mail-smtp') ); if ($result) { wp_send_json_success(__('Test email sent successfully!', 'aha-mail-smtp')); } else { wp_send_json_error(__('Failed to send test email. Please check your settings.', 'aha-mail-smtp')); } } /** * Complete wizard */ public function complete_wizard() { // Verify nonce if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'] ?? '')), 'aha_mail_smtp_admin')) { wp_send_json_error(__('Security check failed', 'aha-mail-smtp')); } // Check user capabilities if (!current_user_can('manage_options')) { wp_send_json_error(__('You do not have sufficient permissions', 'aha-mail-smtp')); } // Mark wizard as completed update_option('aha_mail_smtp_wizard_completed', true); wp_send_json_success(__('Setup wizard completed successfully!', 'aha-mail-smtp')); } /** * Skip wizard */ public function skip_wizard() { // Verify nonce if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'] ?? '')), 'aha_mail_smtp_admin')) { wp_send_json_error(__('Security check failed', 'aha-mail-smtp')); } // Check user capabilities if (!current_user_can('manage_options')) { wp_send_json_error(__('You do not have sufficient permissions', 'aha-mail-smtp')); } // Mark wizard as skipped update_option('aha_mail_smtp_wizard_skipped', true); wp_send_json_success(__('Setup wizard skipped', 'aha-mail-smtp')); } }