Implement AJAX forms and dashboard modal workflow

- Added dashboard modal with multi-step entity creation
- Updated form pages to use AJAX submission
- Improved validation and user feedback on form actions

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Ruben Ramirez 2025-03-07 07:33:32 -06:00
parent e8e17737fc
commit efc50f3b4e
5 changed files with 206 additions and 579 deletions

View file

@ -122,6 +122,14 @@ if ($campaign_count > 0) {
$onboard_steps[3] = true;
}
// Enqueue styles and scripts for modal
wp_enqueue_style('mailferno-modal-style', '/wp-content/plugins/rl-mailwarmer/css/modal.css', array(), '1.0.0');
wp_enqueue_script('dashboard-modal', '/wp-content/plugins/rl-mailwarmer/js/dashboard-modal.js', array('jquery'), '1.0.1', true);
wp_localize_script('dashboard-modal', 'dashboard_modal_vars', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('dashboard_modal_nonce'),
));
get_header(); ?>
<div <?php generate_do_attr( 'content' ); ?>>
@ -278,6 +286,50 @@ get_header(); ?>
<!-- <div id="onboarding_frame" class="dash-card">
</div> -->
<!-- Floating Action Button -->
<div class="mf-floating-action-button">+</div>
<!-- Modal -->
<div class="mf-modal-overlay">
<div class="mf-modal-container">
<div class="mf-modal-header">
<h2>Create New</h2>
<button type="button" class="mf-modal-close">&times;</button>
</div>
<div class="mf-modal-body">
<!-- Step indicators -->
<div class="mf-steps-indicator">
<div class="mf-step-dot active"></div>
</div>
<!-- Step 1: Selection -->
<div class="mf-step active">
<h3>What would you like to create?</h3>
<div class="mf-selection-buttons">
<div class="mf-selection-button" data-type="domain">
<i class="dashicons dashicons-admin-site"></i>
<span>Domain</span>
</div>
<div class="mf-selection-button" data-type="email">
<i class="dashicons dashicons-email"></i>
<span>Email Account</span>
</div>
<div class="mf-selection-button" data-type="campaign">
<i class="dashicons dashicons-megaphone"></i>
<span>Campaign</span>
</div>
</div>
</div>
<!-- Additional steps will be added dynamically -->
</div>
<div class="mf-modal-footer">
<button type="button" class="mf-back-btn" style="display: none;">Back</button>
<button type="button" class="mf-next-btn disabled">Next</button>
</div>
</div>
</div>
</div>
@ -326,4 +378,4 @@ get_header(); ?>
generate_construct_sidebars();
get_footer();
get_footer();

View file

@ -8,166 +8,21 @@ if (!defined('ABSPATH')) {
exit;
}
// log_to_file("Campaign Edit template - Loaded");
$current_user_id = get_current_user_id();
$calculate_timeline = true;
$message = false;
$new_post = true;
$campaign_enabled = true;
$professions = [];
// log_to_file("Campaign Edit template - Current user ID: $current_user_id");
// log_to_file("Campaign Edit template - Server Request Method: ", $_SERVER['REQUEST_METHOD']);
// log_to_file("Campaign Edit template - Post Data: ", $_POST);
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['campaign_submit'])) {
// log_to_file("Campaign Edit template - Handling post request");
$post_data = array(
'post_title' => sanitize_text_field($_POST['campaign_name']),
'post_type' => 'campaign',
'post_status' => 'publish'
);
$calculate_timeline = isset($_POST['calculate_timeline']) ? true : false;
if (isset($_POST['post_id']) && !empty($_POST['post_id'])) {
// log_to_file("Campaign Edit template - Updating campaign");
$post_data['ID'] = intval($_POST['post_id']);
$post_id = wp_update_post($post_data);
$new_post = false;
$message = ['status' => 'success', 'message' => 'Campaign updated successfully.'];
$campaign_tracking_id = get_post_meta($post_id, 'campaign_tracking_id', true);
// log_to_file("Campaign Edit template - Campaign Tracking ID: $campaign_tracking_id");
if (!$campaign_tracking_id) {
RL_MailWarmer_Campaign_Helper::generate_campaign_tracking_id($post_id);
}
} else {
// log_to_file("Campaign Edit template - Creating new campaign");
$post_id = wp_insert_post($post_data);
RL_MailWarmer_Campaign_Helper::generate_campaign_tracking_id($post_id);
$message = ['status' => 'success', 'message' => 'Campaign added successfully.'];
}
if (!is_wp_error($post_id)) {
// log_to_file("Campaign Edit template - Create/update successful!");
$campaign_enabled = isset($_POST['enabled']) ? true : false;
// log_to_file("Campaign Edit template - 1 Campaign Enabled: {$campaign_enabled} New Post: {$new_post}");
// var_dump($campaign_enabled);
update_post_meta($post_id, 'enabled', $campaign_enabled);
$acf_fields = [
'domain_id',
// 'enabled',
'email_accounts',
// 'num_additional_emails',
'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])) {
// log_to_file("Campaign Edit template - Checking for $field in POST: ", $_POST[$field]);
switch ($field) {
case 'email_accounts':
$campaign_email_accounts = $_POST[$field];
$is_limited = false;
// Iterate through each email account ID
foreach ($campaign_email_accounts as $account_id) {
// Retrieve the 'limited_access' meta value for the current account ID
$limited_access = get_post_meta($account_id, 'limited_access', true);
// Check if 'limited_access' is true
if ($limited_access === true || $limited_access === '1') {
$is_limited = true;
break; // Exit the loop early as we only need one true value
}
}
// Update 'campaign_limited' for $post_id if any account is limited
if ($is_limited) {
update_post_meta($post_id, 'campaign_limited', true);
} else {
update_post_meta($post_id, 'campaign_limited', false);
}
// Update the field as usual
update_field($field, $campaign_email_accounts, $post_id);
break;
case 'domain_id':
// log_to_file("Campaign Edit template - Found domain in POST", $_POST['domain_id']);
try {
update_field('domain', $_POST['domain_id'], $post_id);
} catch (Exception $e) {
throw new Exception(__('fetch_dns_records - Campaign Edit template: Unable to update domain field for Campaign', 'rl-mailwarmer') . $e->getMessage());
}
break;
default:
update_field($field, sanitize_text_field($_POST[$field]), $post_id);
break;
}
}
}
// log_to_file("Campaign Edit template - Setting campaign owner_id to $current_user_id");
update_post_meta($post_id, 'owner_id', $current_user_id);
// Calculate a timeline
if ($new_post || $calculate_timeline) {
// log_to_file("Campaign Edit template - Recalculating timeline");
RL_MailWarmer_DB_Helper::delete_all_conversations_messages($post_id);
RL_MailWarmer_Campaign_Helper::calculate_campaign_timeline($post_id);
// RL_MailWarmer_Campaign_Helper::fill_campaign_timeline($post_id);
// log_to_file("Campaign Edit template - Campaign timeline: ", $campaign_timeline);
}
} else {
$message = ['status' => 'error', 'message' => 'Error: ' . $post_id->get_error_message()];
}
if ( !(isset($_POST['stay_on_page']) && ($_POST['stay_on_page'] === 'on')) ) {
// log_to_file("Domain Edit template - stay_on_page not set; redirecting!");
wp_redirect( get_permalink( $post_id ) );
exit;
}
} else {
// log_to_file("Campaign Edit template - Post request not set");
}
// Get existing post data if editing
if (isset($_GET['edit'])) {
$post_id = intval($_GET['edit']);
$new_post = false;
$campaign_enabled = get_post_meta($post_id, 'enabled') ? get_post_meta($post_id, 'enabled', true) : false;
// log_to_file("Campaign Edit template - 2 Campaign Enabled: {$campaign_enabled} New Post: {$new_post}");
// var_dump($campaign_enabled);
// update_post_meta($post_id, 'enabled', $campaign_enabled);
} else {
$post_id = 0;
}
$campaign_data = [
'campaign_name' => '',
// 'enabled' => '',
'domain_id' => '',
'email_accounts' => [],
'num_additional_emails' => '',
@ -187,7 +42,6 @@ if ($post_id > 0) {
$campaign_data['campaign_name'] = $post_item->post_title;
foreach ($campaign_data as $key => $value) {
if ($key !== 'campaign_name') {
switch ($key) {
case 'domain_id':
$domain_id = get_field('domain', $post_id) ? get_field('domain', $post_id) : '';
@ -198,7 +52,6 @@ if ($post_id > 0) {
case 'email_accounts':
$email_accounts = get_field($key, $post_id) ? get_field($key, $post_id) : '';
// log_to_file("Campaign Edit template - Email accounts: ", $email_accounts);
$campaign_data[$key] = $email_accounts;
break;
@ -211,20 +64,11 @@ if ($post_id > 0) {
$campaign_data[$key] = get_field($key, $post_id);
break;
}
// if ($key === 'domain_id') {
// $domain_id = get_field('domain', $post_id);
// $campaign_data[$key] = $domain_id->ID;
// } else {
// $campaign_data[$key] = get_field($key, $post_id);
// }
}
}
}
}
// log_to_file("Campaign Edit template - Campaign data: ", $campaign_data);
// Get domains for current user
$domains = get_posts([
'orderby' => 'title',
@ -238,16 +82,18 @@ $domains = get_posts([
]
]
]);
// log_to_file("Campaign Edit template - Domains: ", $domains);
// Get the list of default professions
$professions = rl_get_textarea_meta_as_array('option', 'default_profession_pool');
// log_to_file("Campaign Edit template - Professions: ", $professions);
// log_to_file("Campaign Edit template - 3 Campaign Enabled: {$campaign_enabled} New Post: {$new_post}");
// Enqueue Scripts
wp_enqueue_script('campaign-form', '/wp-content/plugins/rl-mailwarmer/js/campaign-form.js', array('jquery'), '1.0', true);
wp_localize_script('campaign-form', 'campaign_form_vars', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('campaign_form_nonce'),
'delete_nonce' => wp_create_nonce('campaign_delete_nonce'),
'campaigns_page' => site_url('/campaigns/'),
));
get_header(); ?>
@ -262,14 +108,8 @@ get_header(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> <?php generate_do_microdata('article'); ?>>
<div class="inside-article mf-dashboard-page">
<header <?php generate_do_attr('entry-header'); ?>>
<?php if ($message): ?>
<div class="notice notice-<?php echo $message['status']; ?> is-dismissible">
<p><?php echo esc_html($message['message']); ?></p>
<button type="button" class="notice-dismiss">
<span class="screen-reader-text">Dismiss this notice.</span>
</button>
</div>
<?php endif; ?>
<!-- Form Messages Container -->
<div id="form-messages" style="display: none;"></div>
</header>
<div class="entry-content">
@ -280,11 +120,11 @@ get_header(); ?>
<?php endif; ?>
<form method="post" action="" class="mf-dashboard-form">
<form id="campaign-form" class="mf-dashboard-form">
<?php wp_nonce_field('campaign_form_nonce', 'campaign_nonce'); ?>
<?php if ($post_id) : ?>
<input type="hidden" name="post_id" value="<?php echo esc_attr($post_id); ?>">
<input type="hidden" id="post_id" name="post_id" value="<?php echo esc_attr($post_id); ?>">
<?php endif; ?>
<div class="mf-form-field-block">
@ -338,9 +178,6 @@ get_header(); ?>
]);
// log_to_file("Selected email accounts: ", $selected_accounts );
// log_to_file("User email accounts: ", $user_email_accounts);
if ($user_email_accounts) :
foreach ($user_email_accounts as $account) :
$account_id = $account->ID;
@ -351,14 +188,6 @@ get_header(); ?>
</select>
</td>
</tr>
<!-- <tr>
<th><label for="num_additional_emails">Number of Additional Emails</label></th>
<td>
<input type="number" id="num_additional_emails" name="num_additional_emails"
value="<?php //echo esc_attr($campaign_data['num_additional_emails']); ?>"
class="small-text" required>
</td>
</tr> -->
<tr>
<th><label for="start_date">Start Date</label></th>
<td>
@ -448,11 +277,13 @@ get_header(); ?>
</div>
<p class="submit">
<input type="submit" name="campaign_submit" class="button button-primary"
<input type="submit" class="button button-primary"
value="<?php echo $post_id ? 'Update Campaign' : 'Add Campaign'; ?>">
<?php if ($post_id) : ?>
<a href="#" class="button delete-campaign-btn" data-id="<?php echo esc_attr($post_id); ?>" style="margin-left: 10px; color: #a00;">Delete Campaign</a>
<?php endif; ?>
</p>
<?php //if ($new_post) : ?>
<?php if (true) : ?>
<div class="mf-form-field-block">
@ -476,73 +307,7 @@ get_header(); ?>
do_action('generate_after_main_content'); ?>
</main>
</div>
<script>
jQuery(document).ready(function($) {
/*
* 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();
console.log("Filtering for " + selectedDomain);
$emailOptions.hide();
if (selectedDomain) {
$emailOptions.each(function() {
const $option = $(this);
const email = $option.text();
// console.log("Checking " + email);
const domain = $domainSelect.find('option:selected').text();
// console.log("Domain " + domain);
if (email.endsWith('@' + domain)) {
// console.log("Showing this option");
$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();
});
</script>
<?php
do_action('generate_after_primary_content_area');
generate_construct_sidebars();
get_footer();
get_footer();

View file

@ -10,98 +10,12 @@ if ( ! defined( 'ABSPATH' ) ) {
}
$current_user_id = get_current_user_id();
$message = false;
$new_post = true;
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['domain_submit'])) {
$post_title = strtolower(sanitize_text_field($_POST['domain_name']));
$cloudflare_email = isset($_POST['cloudflare_api_email']) ? sanitize_email($_POST['cloudflare_api_email']) : '';
$cloudflare_key = isset($_POST['cloudflare_api_key']) ? sanitize_text_field($_POST['cloudflare_api_key']) : '';
$domain_in_use = isset($_POST['domain_in_use']) ? $_POST['domain_in_use'] : '';
$post_data = array(
'post_title' => $post_title,
'post_type' => 'domain',
'post_status' => 'publish'
);
// Check if editing existing post
if (isset($_POST['post_id']) && !empty($_POST['post_id'])) {
$post_data['ID'] = intval($_POST['post_id']);
$post_id = wp_update_post($post_data);
$new_post = false;
$message = ['status' => 'success', 'message' => 'Domain updated successfully.'];
} else {
// Prepare WP_Query to check for an existing post with the same title
$args = [
'post_type' => 'domain', // Replace with your post type
'post_status' => 'any',
'title' => $post_title,
'fields' => 'id',
'posts_per_page' => 1
];
$query = new WP_Query($args);
if ($query->have_posts()) {
log_to_file("Domain Edit template - Domain {$post_title} already exists: ");
$message = ['status' => 'error', 'message' => 'Domain already exists.'];
} else {
$post_id = wp_insert_post($post_data);
$new_post = true;
$message = ['status' => 'success', 'message' => 'Domain added successfully.'];
if (!is_wp_error($post_id)) {
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 the owner_id to the creator of the post if this is a new domain
update_post_meta($post_id, 'owner_id', $current_user_id);
// Run a domain health check
$domain_report_id = RL_MailWarmer_Domain_Helper::save_domain_health_report($post_id);
}
} else {
$message = ['status' => 'error', 'message' => 'Error: ' . $post_id->get_error_message()];
// $message = 'Error: ' . $post_id->get_error_message();
}
if (isset($_POST['update_dns']) && !empty($_POST['update_dns'])) {
log_to_file("Domain Edit template - Running Fix Domain");
$results = RL_MailWarmer_Domain_Helper::fix_deliverability_dns_issues($post_id);
log_to_file("Domain Edit template - Results: ", $results);
}
if ($domain_in_use) {
log_to_file("Domain Edit template - Domain in use: {$post_title}. Not modifying MX records");
} else {
log_to_file("Domain Edit template - Domain not in use: {$post_title}. Setting MX to Mailferno server ");
// Set MX to 'server' of $post_id
$server = get_field('defaut_mailferno_mx', 'option');
log_to_file("Domain Edit template - Default Mailferno MX Server: ", $server);
$update_MX_result = RL_MailWarmer_Domain_Helper::update_mx_record($post_id, $server->post_title, 0, $ttl = 3600);
log_to_file("Domain Edit template - CloudFlare Response: {$update_MX_result}");
}
if ( !(isset($_POST['stay_on_page']) && ($_POST['stay_on_page'] === 'on')) ) {
// log_to_file("Domain Edit template - stay_on_page not set; redirecting!");
wp_redirect( get_permalink( $post_id ) );
exit;
}
}
// Reset post data
wp_reset_postdata();
}
}
// Get existing post data if editing
if (isset($_GET['edit'])) {
$post_id = intval($_GET['edit']);
$new_post = false;
$new_post = false;
} else {
$post_id = 0;
}
@ -120,6 +34,15 @@ if ($post_id > 0) {
}
}
// Enqueue Scripts
wp_enqueue_script('domain-form', '/wp-content/plugins/rl-mailwarmer/js/domain-form.js', array('jquery'), '1.0', true);
wp_localize_script('domain-form', 'domain_form_vars', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('domain_form_nonce'),
'delete_nonce' => wp_create_nonce('domain_delete_nonce'),
'domains_page' => site_url('/domains/'),
));
get_header(); ?>
<div <?php generate_do_attr( 'content' ); ?>>
@ -161,21 +84,6 @@ get_header(); ?>
*/
do_action( 'generate_before_entry_title' );
if ($message): ?>
<div class="notice notice-<?php echo $message['status']; ?> is-dismissible">
<p><?php echo esc_html($message['message']); ?></p>
<button type="button" class="notice-dismiss">
<span class="screen-reader-text">Dismiss this notice.</span>
</button>
</div>
<?php endif;
// if ( generate_show_title() ) {
// $params = generate_get_the_title_parameters();
// the_title( $params['before'], $params['after'] );
// }
/**
* generate_after_entry_title hook.
*
@ -207,118 +115,112 @@ get_header(); ?>
<div class="entry-content"<?php echo $itemprop; // phpcs:ignore -- No escaping needed. ?>>
<div class="wrap">
<h1><?php echo $post_id ? 'Edit Domain' : 'Add New Domain'; ?></h1>
<form method="post" action="" class="mf-dashboard-form">
<?php wp_nonce_field('domain_form_nonce', 'domain_nonce'); ?>
<?php if ($post_id) : ?>
<input type="hidden" name="post_id" value="<?php echo esc_attr($post_id); ?>">
<?php endif; ?>
<div class="mf-form-field-block">
<table class="mf-form-table">
<tr>
<th><label for="domain_name">Domain Name</label></th>
<td>
<input type="text"
id="domain_name"
name="domain_name"
value="<?php echo esc_attr($domain_name); ?>"
class="regular-text"
required>
</td>
</tr>
<tr>
<th><label for="update_dns">Update DNS Settings?</label></th>
<td>
<input type="checkbox"
id="update_dns"
name="update_dns"
<?php echo $new_post ? 'checked' : ''; ?>
class="">
<span>Warning! This will attempt to update your DNS records!</span>
</td>
</tr>
<tr>
<th><label for="domain_in_use">Is this domain in use (already sending & receiving email)?</label></th>
<td>
<input type="checkbox"
id="domain_in_use"
name="domain_in_use"
<?php echo $domain_in_use || $new_post ? 'checked' : ''; ?>
class="">
<span>If the domain is already in use we won't change the MX records</span>
</td>
</tr>
</table>
</div>
<h1><?php echo $post_id ? 'Edit Domain' : 'Add New Domain'; ?></h1>
<!-- Form Messages Container -->
<div id="form-messages" style="display: none;"></div>
<form id="domain-form" class="mf-dashboard-form">
<?php wp_nonce_field('domain_form_nonce', 'domain_nonce'); ?>
<?php if ($post_id) : ?>
<input type="hidden" id="post_id" name="post_id" value="<?php echo esc_attr($post_id); ?>">
<?php endif; ?>
<div class="mf-form-field-block">
<table class="mf-form-table">
<tr>
<th><label for="domain_name">Domain Name</label></th>
<td>
<input type="text"
id="domain_name"
name="domain_name"
value="<?php echo esc_attr($domain_name); ?>"
class="regular-text"
required>
</td>
</tr>
<tr>
<th><label for="update_dns">Update DNS Settings?</label></th>
<td>
<input type="checkbox"
id="update_dns"
name="update_dns"
<?php echo $new_post ? 'checked' : ''; ?>
class="">
<span>Warning! This will attempt to update your DNS records!</span>
</td>
</tr>
<tr>
<th><label for="domain_in_use">Is this domain in use (already sending & receiving email)?</label></th>
<td>
<input type="checkbox"
id="domain_in_use"
name="domain_in_use"
<?php echo $domain_in_use || $new_post ? 'checked' : ''; ?>
class="">
<span>If the domain is already in use we won't change the MX records</span>
</td>
</tr>
</table>
</div>
<div class="mf-form-field-block advanced-settings">
<h3 class="advanced-toggle">CloudFlare Settings</h3>
<p>Only if different than the credentials in your profile</p>
<div class="advanced-content" style="display: none;">
<table class="mf-form-table">
<tr>
<th><label for="cloudflare_api_email">Cloudflare API Email</label></th>
<td>
<input type="email"
id="cloudflare_api_email"
name="cloudflare_api_email"
value="<?php echo esc_attr($cloudflare_email); ?>"
class="regular-text">
</td>
</tr>
<tr>
<th><label for="cloudflare_api_key">Cloudflare API Key</label></th>
<td>
<input type="password"
id="cloudflare_api_key"
name="cloudflare_api_key"
value="<?php echo esc_attr($cloudflare_key); ?>"
class="regular-text">
</td>
</tr>
</table>
</div>
</div>
<p class="submit">
<input type="submit"
name="domain_submit"
class="button button-primary"
value="<?php echo $post_id ? 'Update Domain' : 'Add Domain'; ?>">
</p>
<div class="mf-form-field-block advanced-settings">
<h3 class="advanced-toggle">CloudFlare Settings</h3>
<p>Only if different than the credentials in your profile</p>
<div class="advanced-content" style="display: none;">
<table class="mf-form-table">
<tr>
<th><label for="cloudflare_api_email">Cloudflare API Email</label></th>
<td>
<input type="email"
id="cloudflare_api_email"
name="cloudflare_api_email"
value="<?php echo esc_attr($cloudflare_email); ?>"
class="regular-text">
</td>
</tr>
<tr>
<th><label for="cloudflare_api_key">Cloudflare API Key</label></th>
<td>
<input type="password"
id="cloudflare_api_key"
name="cloudflare_api_key"
value="<?php echo esc_attr($cloudflare_key); ?>"
class="regular-text">
</td>
</tr>
</table>
</div>
</div>
<p class="submit">
<input type="submit"
class="button button-primary"
value="<?php echo $post_id ? 'Update Domain' : 'Add Domain'; ?>">
<?php if ($post_id) : ?>
<a href="#" class="button delete-domain-btn" data-id="<?php echo esc_attr($post_id); ?>" style="margin-left: 10px; color: #a00;">Delete Domain</a>
<?php endif; ?>
</p>
<?php if ($new_post) : ?>
<?php if ($new_post) : ?>
<div class="mf-form-field-block">
<label for="stay_on_page">Stay on this page to create another?</label>
<input type="checkbox"
id="stay_on_page"
name="stay_on_page"
class="">
</div>
<div class="mf-form-field-block">
<label for="stay_on_page">Stay on this page to create another?</label>
<input type="checkbox"
id="stay_on_page"
name="stay_on_page"
class="">
</div>
<?php endif; ?>
<?php endif; ?>
</form>
</form>
</div>
<?php
// the_content();
// wp_link_pages(
// array(
// 'before' => '<div class="page-links">' . __( 'Pages:', 'generatepress' ),
// 'after' => '</div>',
// )
// );
?>
</div>
<?php
@ -367,4 +269,4 @@ get_header(); ?>
generate_construct_sidebars();
get_footer();
get_footer();

View file

@ -9,87 +9,23 @@ if (!defined('ABSPATH')) {
}
$current_user_id = get_current_user_id();
$message = false;
$new_post = true;
log_to_file("Email Account Edit template - Current user ID: $current_user_id");
log_to_file("Email Account Edit template - Server Request Method: ", $_SERVER['REQUEST_METHOD']);
log_to_file("Email Account Edit template - Post Data: ", $_POST);
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['email_submit']) && isset($_POST['email_address'])) {
$post_title = strtolower(sanitize_email($_POST['email_address']));
$post_data = array(
'post_title' => $post_title,
'post_type' => 'email-account',
'post_status' => 'publish'
);
$meta_fields = [
'domain_id', 'mail_password', 'full_name', 'email_signature',
'email_provider', 'smtp_password', 'smtp_server', 'smtp_port',
'imap_password', 'imap_server', 'imap_port'
];
if (isset($_POST['post_id']) && !empty($_POST['post_id'])) {
$post_data['ID'] = intval($_POST['post_id']);
$post_id = wp_update_post($post_data);
$new_post = false;
$message = ['status' => 'success', 'message' => 'Email account updated successfully.'];
} else {
// Prepare WP_Query to check for an existing post with the same title
$args = [
'post_type' => 'email-account', // Replace with your post type
'post_status' => 'any',
'title' => $post_title,
'fields' => 'id',
'posts_per_page' => 1
];
$query = new WP_Query($args);
if ($query->have_posts()) {
log_to_file("Email Account Edit template - email account {$post_title} already exists: ");
$message = ['status' => 'error', 'message' => 'Email account already exists.'];
} else {
$post_id = wp_insert_post($post_data);
$new_post = true;
$message = ['status' => 'success', 'message' => 'Email account added successfully.'];
if (!is_wp_error($post_id)) {
foreach ($meta_fields as $field) {
if (isset($_POST[$field])) {
update_post_meta($post_id, $field, sanitize_text_field($_POST[$field]));
}
}
// include new accounts in the warmup pool and set the owner_id to the creator of the post
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);
}
} else {
$message = ['status' => 'error', 'message' => 'Error: ' . $post_id->get_error_message()];
}
if ( !(isset($_POST['stay_on_page']) && ($_POST['stay_on_page'] === 'on')) ) {
// log_to_file("Email Account Edit template - stay_on_page not set; redirecting!");
wp_redirect( get_permalink( $post_id ) );
exit;
}
}
}
}
// Get existing post data if editing
if (isset($_GET['edit'])) {
$post_id = intval($_GET['edit']);
$new_post = false;
$new_post = false;
} else {
$post_id = 0;
}
if (isset($_GET['domain_id'])) {
$domain_id = intval($_GET['domain_id']);
} else {
$domain_id = '';
}
$email_data = [
'email_address' => '',
'domain_id' => '',
'domain_id' => $domain_id,
'mail_password' => '',
'full_name' => '',
'email_signature' => '',
@ -136,6 +72,15 @@ $providers = get_posts([
'posts_per_page' => -1
]);
// Enqueue Scripts
wp_enqueue_script('email-account-form', '/wp-content/plugins/rl-mailwarmer/js/email-account-form.js', array('jquery'), '1.0', true);
wp_localize_script('email-account-form', 'email_form_vars', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('email_form_nonce'),
'delete_nonce' => wp_create_nonce('email_delete_nonce'),
'email_accounts_page' => site_url('/email-accounts/'),
));
get_header(); ?>
<div <?php generate_do_attr('content'); ?>>
@ -148,25 +93,19 @@ get_header(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> <?php generate_do_microdata('article'); ?>>
<div class="inside-article mf-dashboard-page">
<header <?php generate_do_attr('entry-header'); ?>>
<?php if ($message): ?>
<div class="notice notice-<?php echo $message['status']; ?> is-dismissible">
<p><?php echo esc_html($message['message']); ?></p>
<button type="button" class="notice-dismiss">
<span class="screen-reader-text">Dismiss this notice.</span>
</button>
</div>
<?php endif; ?>
<!-- Form Messages Container -->
<div id="form-messages" style="display: none;"></div>
</header>
<div class="entry-content">
<div class="wrap">
<h1><?php echo $post_id ? 'Edit Email Account' : 'Add New Email Account'; ?></h1>
<form method="post" action="" class="mf-dashboard-form">
<form id="email-account-form" class="mf-dashboard-form">
<?php wp_nonce_field('email_form_nonce', 'email_nonce'); ?>
<?php if ($post_id) : ?>
<input type="hidden" name="post_id" value="<?php echo esc_attr($post_id); ?>">
<input type="hidden" id="post_id" name="post_id" value="<?php echo esc_attr($post_id); ?>">
<?php endif; ?>
<div class="mf-form-field-block">
@ -284,12 +223,14 @@ get_header(); ?>
</div>
<p class="submit">
<input type="submit" name="email_submit" class="button button-primary"
<input type="submit" class="button button-primary"
value="<?php echo $post_id ? 'Update Email Account' : 'Add Email Account'; ?>">
<?php if ($post_id) : ?>
<a href="#" class="button delete-email-account-btn" data-id="<?php echo esc_attr($post_id); ?>" style="margin-left: 10px; color: #a00;">Delete Email Account</a>
<?php endif; ?>
</p>
<?php if ($new_post) : ?>
<div class="mf-form-field-block">
<label for="stay_on_page">Stay on this page to create another?</label>
<input type="checkbox"
@ -297,7 +238,6 @@ get_header(); ?>
name="stay_on_page"
class="">
</div>
<?php endif; ?>
</form>
</div>
@ -310,38 +250,6 @@ get_header(); ?>
do_action('generate_after_main_content'); ?>
</main>
</div>
<script>
jQuery(document).ready(function($) {
// Email validation function
function validateEmail() {
var domain_id = $("#domain_id").val();
var email = $("#email_address").val();
if (!domain_id || !email) return true;
var domain_text = $("#domain_id option:selected").text();
var 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
$("form").on("submit", function(e) {
if (!validateEmail()) {
e.preventDefault();
}
});
// Optional: Real-time validation as user types
$("#email_address, #domain_id").on("change", validateEmail);
});
</script>
<?php
do_action('generate_after_primary_content_area');

View file

@ -167,7 +167,7 @@ get_header(); ?>
echo '<p>No email accounts found for this domain.</p>';
}
echo '<p><a href="/dashboard/email-accounts/edit-email-account?domain=' . esc_attr($post_id) . '" class="button button-primary">Create New Email Account</a></p>';
echo '<p><a href="/dashboard/email-accounts/edit-email-account?domain_id=' . esc_attr($post_id) . '" class="button button-primary">Create New Email Account</a></p>';
?>
</div>
<div id="campaign_tab">