From c11bc80f75cb285e06c636e6597488f182690450 Mon Sep 17 00:00:00 2001 From: ruben Date: Fri, 7 Mar 2025 05:15:38 -0600 Subject: [PATCH] Added AJAX handling for creating/editing posts --- includes/rl-mailwarmer-ajax.php | 406 ++++++++++++++++++++++++++++++++ js/campaign-form.js | 195 +++++++++++++++ js/domain-form.js | 150 ++++++++++++ js/email-account-form.js | 169 +++++++++++++ 4 files changed, 920 insertions(+) create mode 100644 js/campaign-form.js create mode 100644 js/domain-form.js create mode 100644 js/email-account-form.js diff --git a/includes/rl-mailwarmer-ajax.php b/includes/rl-mailwarmer-ajax.php index 04dd5ea..c351942 100644 --- a/includes/rl-mailwarmer-ajax.php +++ b/includes/rl-mailwarmer-ajax.php @@ -1,5 +1,138 @@ 'domain', + 'post_status' => 'any', + 'title' => $post_title, + 'fields' => 'ids', + 'posts_per_page' => 1 + ]; + + $query = new WP_Query($args); + if ($query->have_posts()) { + wp_send_json_error(['message' => __('Domain already exists.', 'rl-mailwarmer')]); + return; + } + } + + // Prepare post data + $post_data = array( + 'post_title' => $post_title, + 'post_type' => 'domain', + 'post_status' => 'publish' + ); + + if ($post_id) { + $post_data['ID'] = $post_id; + $post_id = wp_update_post($post_data); + } else { + $post_id = wp_insert_post($post_data); + } + + if (is_wp_error($post_id)) { + wp_send_json_error(['message' => $post_id->get_error_message()]); + return; + } + + // Update meta fields + update_post_meta($post_id, 'cloudflare_api_email', $cloudflare_email); + update_post_meta($post_id, 'cloudflare_api_key', $cloudflare_key); + update_post_meta($post_id, 'domain_in_use', $domain_in_use); + + if ($new_post) { + // Set owner to current user for new domains + update_post_meta($post_id, 'owner_id', $current_user_id); + + // Run domain health check for new domains + RL_MailWarmer_Domain_Helper::save_domain_health_report($post_id); + } + + // Handle DNS updates if requested + if ($update_dns) { + $results = RL_MailWarmer_Domain_Helper::fix_deliverability_dns_issues($post_id); + } + + // Handle MX records based on domain usage + if (!$domain_in_use) { + $server = get_field('defaut_mailferno_mx', 'option'); + $update_MX_result = RL_MailWarmer_Domain_Helper::update_mx_record($post_id, $server->post_title, 0, $ttl = 3600); + } + + // Return success response with post ID and permalink + wp_send_json_success([ + 'message' => $new_post ? __('Domain added successfully.', 'rl-mailwarmer') : __('Domain updated successfully.', 'rl-mailwarmer'), + 'post_id' => $post_id, + 'permalink' => get_permalink($post_id) + ]); +}); + +/** + * Delete a domain via AJAX + */ +add_action('wp_ajax_rl_mailwarmer_delete_domain', function () { + // Verify nonce + if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'domain_delete_nonce')) { + wp_send_json_error(__('Security check failed', 'rl-mailwarmer')); + } + + $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; + + if (!$post_id) { + wp_send_json_error(__('Invalid domain ID', 'rl-mailwarmer')); + return; + } + + // Verify post type and user permissions + $post = get_post($post_id); + if (!$post || $post->post_type !== 'domain') { + wp_send_json_error(__('Invalid domain', 'rl-mailwarmer')); + return; + } + + // Verify ownership + $owner_id = get_post_meta($post_id, 'owner_id', true); + if ($owner_id != get_current_user_id() && !current_user_can('manage_options')) { + wp_send_json_error(__('Permission denied', 'rl-mailwarmer')); + return; + } + + // Delete the domain + $result = wp_delete_post($post_id, true); + + if (!$result) { + wp_send_json_error(__('Failed to delete domain', 'rl-mailwarmer')); + return; + } + + wp_send_json_success(['message' => __('Domain deleted successfully', 'rl-mailwarmer')]); +}); + add_action('wp_ajax_rl_mailwarmer_check_domain_health', function () { // log_to_file("wp_ajax_rl_mailwarmer_check_domain_health called"); // Verify nonce @@ -249,6 +382,279 @@ add_action('wp_ajax_rl_mailwarmer_modify_email_account', function () { } }); +// CAMPAIGN MANAGEMENT AJAX ENDPOINTS + +/** + * Create or update a campaign via AJAX + */ +add_action('wp_ajax_rl_mailwarmer_save_campaign', function () { + // Verify nonce + if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'campaign_form_nonce')) { + wp_send_json_error(__('Security check failed', 'rl-mailwarmer')); + } + + $current_user_id = get_current_user_id(); + $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; + $new_post = ($post_id == 0); + $calculate_timeline = isset($_POST['calculate_timeline']) ? true : false; + + // Prepare post data + $post_data = array( + 'post_title' => sanitize_text_field($_POST['campaign_name']), + 'post_type' => 'campaign', + 'post_status' => 'publish' + ); + + if ($post_id) { + $post_data['ID'] = $post_id; + $post_id = wp_update_post($post_data); + } else { + $post_id = wp_insert_post($post_data); + // Generate tracking ID for new campaigns + RL_MailWarmer_Campaign_Helper::generate_campaign_tracking_id($post_id); + } + + if (is_wp_error($post_id)) { + wp_send_json_error(['message' => $post_id->get_error_message()]); + return; + } + + // Update campaign enabled status + $campaign_enabled = isset($_POST['enabled']) ? true : false; + update_post_meta($post_id, 'enabled', $campaign_enabled); + + // Update ACF fields + $acf_fields = [ + 'domain_id', + 'email_accounts', + 'start_date', + 'warmup_period', + 'starting_volume', + 'target_volume', + 'weekend_reduction_factor', + 'target_profession', + 'target_profession_other', + 'campaign_conversation_topics' + ]; + + foreach ($acf_fields as $field) { + if (isset($_POST[$field])) { + switch ($field) { + case 'email_accounts': + $campaign_email_accounts = $_POST[$field]; + $is_limited = false; + + // Check if any email account has limited access + foreach ($campaign_email_accounts as $account_id) { + $limited_access = get_post_meta($account_id, 'limited_access', true); + if ($limited_access === true || $limited_access === '1') { + $is_limited = true; + break; + } + } + + // Update campaign_limited flag + update_post_meta($post_id, 'campaign_limited', $is_limited); + update_field($field, $campaign_email_accounts, $post_id); + break; + + case 'domain_id': + try { + update_field('domain', $_POST['domain_id'], $post_id); + } catch (Exception $e) { + wp_send_json_error(['message' => $e->getMessage()]); + return; + } + break; + + default: + update_field($field, sanitize_text_field($_POST[$field]), $post_id); + break; + } + } + } + + // Set campaign owner + update_post_meta($post_id, 'owner_id', $current_user_id); + + // Calculate timeline if needed + if ($new_post || $calculate_timeline) { + RL_MailWarmer_DB_Helper::delete_all_conversations_messages($post_id); + RL_MailWarmer_Campaign_Helper::calculate_campaign_timeline($post_id); + } + + // Return success response + wp_send_json_success([ + 'message' => $new_post ? __('Campaign added successfully.', 'rl-mailwarmer') : __('Campaign updated successfully.', 'rl-mailwarmer'), + 'post_id' => $post_id, + 'permalink' => get_permalink($post_id) + ]); +}); + +/** + * Delete a campaign via AJAX + */ +add_action('wp_ajax_rl_mailwarmer_delete_campaign', function () { + // Verify nonce + if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'campaign_delete_nonce')) { + wp_send_json_error(__('Security check failed', 'rl-mailwarmer')); + } + + $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; + + if (!$post_id) { + wp_send_json_error(__('Invalid campaign ID', 'rl-mailwarmer')); + return; + } + + // Verify post type + $post = get_post($post_id); + if (!$post || $post->post_type !== 'campaign') { + wp_send_json_error(__('Invalid campaign', 'rl-mailwarmer')); + return; + } + + // Verify ownership + $owner_id = get_post_meta($post_id, 'owner_id', true); + if ($owner_id != get_current_user_id() && !current_user_can('manage_options')) { + wp_send_json_error(__('Permission denied', 'rl-mailwarmer')); + return; + } + + // Delete campaign messages and timeline first + RL_MailWarmer_DB_Helper::delete_all_conversations_messages($post_id); + + // Delete the campaign + $result = wp_delete_post($post_id, true); + + if (!$result) { + wp_send_json_error(__('Failed to delete campaign', 'rl-mailwarmer')); + return; + } + + wp_send_json_success(['message' => __('Campaign deleted successfully', 'rl-mailwarmer')]); +}); + +// EMAIL ACCOUNT MANAGEMENT AJAX ENDPOINTS + +/** + * Create or update an email account via AJAX + */ +add_action('wp_ajax_rl_mailwarmer_save_email_account', function () { + // Verify nonce + if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'email_form_nonce')) { + wp_send_json_error(__('Security check failed', 'rl-mailwarmer')); + } + + $current_user_id = get_current_user_id(); + $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; + $post_title = strtolower(sanitize_email($_POST['email_address'])); + $new_post = ($post_id == 0); + + // Check for existing email account with same address for new posts + if ($new_post) { + $args = [ + 'post_type' => 'email-account', + 'post_status' => 'any', + 'title' => $post_title, + 'fields' => 'ids', + 'posts_per_page' => 1 + ]; + + $query = new WP_Query($args); + if ($query->have_posts()) { + wp_send_json_error(['message' => __('Email account already exists.', 'rl-mailwarmer')]); + return; + } + } + + // Prepare post data + $post_data = array( + 'post_title' => $post_title, + 'post_type' => 'email-account', + 'post_status' => 'publish' + ); + + if ($post_id) { + $post_data['ID'] = $post_id; + $post_id = wp_update_post($post_data); + } else { + $post_id = wp_insert_post($post_data); + } + + if (is_wp_error($post_id)) { + wp_send_json_error(['message' => $post_id->get_error_message()]); + return; + } + + // Update meta fields + $meta_fields = [ + 'domain_id', 'mail_password', 'full_name', 'email_signature', + 'email_provider', 'smtp_password', 'smtp_server', 'smtp_port', + 'imap_password', 'imap_server', 'imap_port' + ]; + + foreach ($meta_fields as $field) { + if (isset($_POST[$field])) { + update_post_meta($post_id, $field, sanitize_text_field($_POST[$field])); + } + } + + // For new accounts, set ownership and include in warmup pool + if ($new_post) { + update_post_meta($post_id, 'owner_id', $current_user_id); + update_post_meta($post_id, 'include_in_warmup_pool', $current_user_id); + } + + // Return success response + wp_send_json_success([ + 'message' => $new_post ? __('Email account added successfully.', 'rl-mailwarmer') : __('Email account updated successfully.', 'rl-mailwarmer'), + 'post_id' => $post_id, + 'permalink' => get_permalink($post_id) + ]); +}); + +/** + * Delete an email account via AJAX + */ +add_action('wp_ajax_rl_mailwarmer_delete_email_account', function () { + // Verify nonce + if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'email_delete_nonce')) { + wp_send_json_error(__('Security check failed', 'rl-mailwarmer')); + } + + $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; + + if (!$post_id) { + wp_send_json_error(__('Invalid email account ID', 'rl-mailwarmer')); + return; + } + + // Verify post type + $post = get_post($post_id); + if (!$post || $post->post_type !== 'email-account') { + wp_send_json_error(__('Invalid email account', 'rl-mailwarmer')); + return; + } + + // Verify ownership + $owner_id = get_post_meta($post_id, 'owner_id', true); + if ($owner_id != get_current_user_id() && !current_user_can('manage_options')) { + wp_send_json_error(__('Permission denied', 'rl-mailwarmer')); + return; + } + + // Delete the email account + $result = wp_delete_post($post_id, true); + + if (!$result) { + wp_send_json_error(__('Failed to delete email account', 'rl-mailwarmer')); + return; + } + + wp_send_json_success(['message' => __('Email account deleted successfully', 'rl-mailwarmer')]); +}); + add_action('wp_ajax_rl_mailwarmer_check_mail_login', function () { // Verify nonce if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'check_mail_login_nonce')) { diff --git a/js/campaign-form.js b/js/campaign-form.js new file mode 100644 index 0000000..9cb5dff --- /dev/null +++ b/js/campaign-form.js @@ -0,0 +1,195 @@ +/** + * Campaign form AJAX handling + */ +jQuery(document).ready(function($) { + // Handle campaign form submission + $('#campaign-form').on('submit', function(e) { + e.preventDefault(); + + // Show loading state + const $submitButton = $(this).find('input[type="submit"]'); + const originalText = $submitButton.val(); + $submitButton.val('Processing...').prop('disabled', true); + + // Get form data + const formData = new FormData(this); + formData.append('action', 'rl_mailwarmer_save_campaign'); + formData.append('security', campaign_form_vars.nonce); + + // Process checkbox fields + formData.set('enabled', $('#enabled').is(':checked') ? '1' : ''); + formData.set('calculate_timeline', $('#calculate_timeline').is(':checked') ? '1' : ''); + + // AJAX request + $.ajax({ + url: campaign_form_vars.ajax_url, + type: 'POST', + data: formData, + processData: false, + contentType: false, + success: function(response) { + if (response.success) { + // Display success message + $('#form-messages') + .removeClass('notice-error') + .addClass('notice notice-success is-dismissible') + .html('

' + response.data.message + '

') + .show(); + + // Add a dismiss button to the notice + addNoticeDismissButton(); + + // Reset form or redirect if not staying on page + if (!$('#stay_on_page').is(':checked') && response.data.permalink) { + window.location.href = response.data.permalink; + return; + } else if (!$('#stay_on_page').is(':checked')) { + window.location.href = campaign_form_vars.campaigns_page; + return; + } + + // Reset form for new submission if staying on page + if (!$('#post_id').val()) { + $('#campaign-form')[0].reset(); + } + } else { + // Display error message + $('#form-messages') + .removeClass('notice-success') + .addClass('notice notice-error is-dismissible') + .html('

' + response.data.message + '

') + .show(); + + // Add a dismiss button to the notice + addNoticeDismissButton(); + } + }, + error: function(xhr, status, error) { + // Display error message + $('#form-messages') + .removeClass('notice-success') + .addClass('notice notice-error is-dismissible') + .html('

An error occurred: ' + error + '

') + .show(); + + // Add a dismiss button to the notice + addNoticeDismissButton(); + }, + complete: function() { + // Restore button state + $submitButton.val(originalText).prop('disabled', false); + } + }); + }); + + // Handle delete campaign button click + $('.delete-campaign-btn').on('click', function(e) { + e.preventDefault(); + + if (!confirm('Are you sure you want to delete this campaign? This action cannot be undone.')) { + return; + } + + const postId = $(this).data('id'); + + // Show loading state + $(this).text('Deleting...').prop('disabled', true); + + // AJAX request to delete campaign + $.ajax({ + url: campaign_form_vars.ajax_url, + type: 'POST', + data: { + action: 'rl_mailwarmer_delete_campaign', + security: campaign_form_vars.delete_nonce, + post_id: postId + }, + success: function(response) { + if (response.success) { + // Redirect to campaigns list page + window.location.href = campaign_form_vars.campaigns_page; + } else { + alert(response.data.message || 'Error deleting campaign'); + } + }, + error: function() { + alert('An error occurred while deleting the campaign'); + } + }); + }); + + /** + * Filter email accounts to only display those matching the selected domain + */ + const $domainSelect = $('#domain_id'); + const $emailSelect = $('#email_accounts'); + const $emailOptions = $emailSelect.find('option'); + + function filterEmailAccounts() { + const selectedDomain = $domainSelect.val(); + + $emailOptions.hide(); + if (selectedDomain) { + $emailOptions.each(function() { + const $option = $(this); + const email = $option.text(); + const domain = $domainSelect.find('option:selected').text(); + + if (email.endsWith('@' + domain)) { + $option.show(); + } + }); + } + } + + $domainSelect.on('change', function() { + $emailSelect.val([]); // Clear selection + filterEmailAccounts(); + }); + + // Initial filter + filterEmailAccounts(); + + /** + * Show the Other Profession field when the Target Profession is set to "Other" + */ + const $professionSelect = $('#target_profession'); + const $otherField = $('#target_profession_other').closest('tr'); + + function toggleOtherField() { + if ($professionSelect.val() === 'Other') { + $otherField.show(); + } else { + $otherField.hide(); + $('#target_profession_other').val(''); + } + } + + $professionSelect.on('change', toggleOtherField); + + // Initial state + toggleOtherField(); + + // Helper function to add dismiss button to notices + function addNoticeDismissButton() { + $('.notice.is-dismissible').each(function() { + const $notice = $(this); + const $button = $(''); + + // Only add the button if it doesn't exist already + if ($notice.find('.notice-dismiss').length === 0) { + $notice.append($button); + + // Handle the dismiss button click + $button.on('click', function() { + $notice.slideUp('fast', function() { + $notice.remove(); + }); + }); + } + }); + } + + // Initialize dismiss buttons + addNoticeDismissButton(); +}); \ No newline at end of file diff --git a/js/domain-form.js b/js/domain-form.js new file mode 100644 index 0000000..b05d77b --- /dev/null +++ b/js/domain-form.js @@ -0,0 +1,150 @@ +/** + * Domain form AJAX handling + */ +jQuery(document).ready(function($) { + // console.log("Domain form loaded"); + // Handle domain form submission + $('#domain-form').on('submit', function(e) { + e.preventDefault(); + + // Show loading state + const $submitButton = $(this).find('input[type="submit"]'); + const originalText = $submitButton.val(); + $submitButton.val('Processing...').prop('disabled', true); + + // Get form data + const formData = new FormData(this); + formData.append('action', 'rl_mailwarmer_save_domain'); + formData.append('security', domain_form_vars.nonce); + + // Process checkbox fields + formData.set('domain_in_use', $('#domain_in_use').is(':checked') ? '1' : ''); + formData.set('update_dns', $('#update_dns').is(':checked') ? '1' : ''); + + // AJAX request + $.ajax({ + url: domain_form_vars.ajax_url, + type: 'POST', + data: formData, + processData: false, + contentType: false, + success: function(response) { + if (response.success) { + console.log("Domain added"); + // Display success message + $('#form-messages') + .removeClass('notice-error') + .addClass('notice notice-success is-dismissible') + .html('

' + response.data.message + '

') + .show(); + + // Add a dismiss button to the notice + addNoticeDismissButton(); + + // Reset form or redirect if not staying on page + if (!$('#stay_on_page').is(':checked') && response.data.permalink) { + // window.location.href = response.data.permalink; + return; + } else if (!$('#stay_on_page').is(':checked')) { + window.location.href = domain_form_vars.domains_page; + return; + } + + // Reset form for new submission if staying on page + if (!$('#post_id').val()) { + $('#domain-form')[0].reset(); + } + } else { + // Display error message + $('#form-messages') + .removeClass('notice-success') + .addClass('notice notice-error is-dismissible') + .html('

' + response.data.message + '

') + .show(); + + // Add a dismiss button to the notice + addNoticeDismissButton(); + } + }, + error: function(xhr, status, error) { + // Display error message + $('#form-messages') + .removeClass('notice-success') + .addClass('notice notice-error is-dismissible') + .html('

An error occurred: ' + error + '

') + .show(); + + // Add a dismiss button to the notice + addNoticeDismissButton(); + }, + complete: function() { + // Restore button state + $submitButton.val(originalText).prop('disabled', false); + } + }); + }); + + // Handle delete domain button click + $('.delete-domain-btn').on('click', function(e) { + e.preventDefault(); + + if (!confirm('Are you sure you want to delete this domain? This action cannot be undone.')) { + return; + } + + const postId = $(this).data('id'); + + // Show loading state + $(this).text('Deleting...').prop('disabled', true); + + // AJAX request to delete domain + $.ajax({ + url: domain_form_vars.ajax_url, + type: 'POST', + data: { + action: 'rl_mailwarmer_delete_domain', + security: domain_form_vars.delete_nonce, + post_id: postId + }, + success: function(response) { + if (response.success) { + // Redirect to domains list page + window.location.href = domain_form_vars.domains_page; + } else { + alert(response.data.message || 'Error deleting domain'); + } + }, + error: function() { + alert('An error occurred while deleting the domain'); + } + }); + }); + + // Toggle advanced settings + $('.advanced-toggle').on('click', function() { + $(this).parent().find('.advanced-content').slideToggle(); + }); + + // Helper function to add dismiss button to notices + function addNoticeDismissButton() { + $('.notice.is-dismissible').each(function() { + const $notice = $(this); + const $button = $(''); + + // Only add the button if it doesn't exist already + if ($notice.find('.notice-dismiss').length === 0) { + $notice.append($button); + + // Handle the dismiss button click + $button.on('click', function() { + $notice.slideUp('fast', function() { + $notice.remove(); + }); + }); + } + }); + } + + // Initialize dismiss buttons + addNoticeDismissButton(); +}); \ No newline at end of file diff --git a/js/email-account-form.js b/js/email-account-form.js new file mode 100644 index 0000000..0ee55a7 --- /dev/null +++ b/js/email-account-form.js @@ -0,0 +1,169 @@ +/** + * Email account form AJAX handling + */ +jQuery(document).ready(function($) { + // Handle email account form submission + $('#email-account-form').on('submit', function(e) { + e.preventDefault(); + + // Email validation + if (!validateEmail()) { + return; + } + + // Show loading state + const $submitButton = $(this).find('input[type="submit"]'); + const originalText = $submitButton.val(); + $submitButton.val('Processing...').prop('disabled', true); + + // Get form data + const formData = new FormData(this); + formData.append('action', 'rl_mailwarmer_save_email_account'); + formData.append('security', email_form_vars.nonce); + + // AJAX request + $.ajax({ + url: email_form_vars.ajax_url, + type: 'POST', + data: formData, + processData: false, + contentType: false, + success: function(response) { + if (response.success) { + // Display success message + $('#form-messages') + .removeClass('notice-error') + .addClass('notice notice-success is-dismissible') + .html('

' + response.data.message + '

') + .show(); + + // Add a dismiss button to the notice + addNoticeDismissButton(); + + // Reset form or redirect if not staying on page + if (!$('#stay_on_page').is(':checked') && response.data.permalink) { + // window.location.href = response.data.permalink; + return; + } else if (!$('#stay_on_page').is(':checked')) { + window.location.href = email_form_vars.email_accounts_page; + return; + } + + // Reset form for new submission if staying on page + if (!$('#post_id').val()) { + $('#email-account-form')[0].reset(); + } + } else { + // Display error message + $('#form-messages') + .removeClass('notice-success') + .addClass('notice notice-error is-dismissible') + .html('

' + response.data.message + '

') + .show(); + + // Add a dismiss button to the notice + addNoticeDismissButton(); + } + }, + error: function(xhr, status, error) { + // Display error message + $('#form-messages') + .removeClass('notice-success') + .addClass('notice notice-error is-dismissible') + .html('

An error occurred: ' + error + '

') + .show(); + + // Add a dismiss button to the notice + addNoticeDismissButton(); + }, + complete: function() { + // Restore button state + $submitButton.val(originalText).prop('disabled', false); + } + }); + }); + + // Handle delete email account button click + $('.delete-email-account-btn').on('click', function(e) { + e.preventDefault(); + + if (!confirm('Are you sure you want to delete this email account? This action cannot be undone.')) { + return; + } + + const postId = $(this).data('id'); + + // Show loading state + $(this).text('Deleting...').prop('disabled', true); + + // AJAX request to delete email account + $.ajax({ + url: email_form_vars.ajax_url, + type: 'POST', + data: { + action: 'rl_mailwarmer_delete_email_account', + security: email_form_vars.delete_nonce, + post_id: postId + }, + success: function(response) { + if (response.success) { + // Redirect to email accounts list page + window.location.href = email_form_vars.email_accounts_page; + } else { + alert(response.data.message || 'Error deleting email account'); + } + }, + error: function() { + alert('An error occurred while deleting the email account'); + } + }); + }); + + // Email validation function + function validateEmail() { + const domain_id = $("#domain_id").val(); + const email = $("#email_address").val(); + + if (!domain_id || !email) return true; + + const domain_text = $("#domain_id option:selected").text(); + const emailDomain = email.split("@")[1]; + + if (emailDomain !== domain_text) { + alert("Email address must match the selected domain: " + domain_text); + return false; + } + return true; + } + + // Add validation to form submission + $("#email_address, #domain_id").on("change", validateEmail); + + // Toggle advanced settings + $('.advanced-toggle').on('click', function() { + $(this).parent().find('.advanced-content').slideToggle(); + }); + + // Helper function to add dismiss button to notices + function addNoticeDismissButton() { + $('.notice.is-dismissible').each(function() { + const $notice = $(this); + const $button = $(''); + + // Only add the button if it doesn't exist already + if ($notice.find('.notice-dismiss').length === 0) { + $notice.append($button); + + // Handle the dismiss button click + $button.on('click', function() { + $notice.slideUp('fast', function() { + $notice.remove(); + }); + }); + } + }); + } + + // Initialize dismiss buttons + addNoticeDismissButton(); +}); \ No newline at end of file