/** * 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(); });