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