mailferno-theme/page-edit-email-account.php
ruben efc50f3b4e 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>
2025-03-07 07:33:32 -06:00

257 lines
No EOL
14 KiB
PHP

<?php
/**
* Template Name: Email Account Edit template
* Template Post Type: page
*/
if (!defined('ABSPATH')) {
exit;
}
$current_user_id = get_current_user_id();
$new_post = true;
// Get existing post data if editing
if (isset($_GET['edit'])) {
$post_id = intval($_GET['edit']);
$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,
'mail_password' => '',
'full_name' => '',
'email_signature' => '',
'email_provider' => '',
'smtp_password' => '',
'smtp_server' => '',
'smtp_port' => '',
'imap_password' => '',
'imap_server' => '',
'imap_port' => ''
];
if ($post_id > 0) {
$post_item = get_post($post_id);
if ($post_item && $post_item->post_type === 'email-account') {
$email_data['email_address'] = $post_item->post_title;
foreach ($email_data as $key => $value) {
if ($key !== 'email_address') {
$email_data[$key] = get_post_meta($post_id, $key, true);
}
}
}
}
// Get domains for current user
$domains = get_posts([
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'domain',
'posts_per_page' => -1,
'meta_query' => [
[
'key' => 'owner_id',
'value' => $current_user_id
]
]
]);
// Get email providers
$providers = get_posts([
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'email-provider',
'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'); ?>>
<main <?php generate_do_attr('main'); ?>>
<?php do_action('generate_before_main_content');
if (generate_has_default_loop()) {
while (have_posts()) :
the_post(); ?>
<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'); ?>>
<!-- 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 id="email-account-form" class="mf-dashboard-form">
<?php wp_nonce_field('email_form_nonce', 'email_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_id">Domain</label></th>
<td>
<select id="domain_id" name="domain_id" required>
<option value="">Select Domain</option>
<?php foreach ($domains as $domain) : ?>
<option value="<?php echo esc_attr($domain->ID); ?>"<?php selected($email_data['domain_id'], $domain->ID); ?>><?php echo esc_html($domain->post_title); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr>
<th><label for="email_address">Email Address</label></th>
<td>
<input type="email" id="email_address" name="email_address"
value="<?php echo esc_attr($email_data['email_address']); ?>"
class="regular-text" required>
</td>
</tr>
<tr>
<th><label for="mail_password">Mail Password</label></th>
<td>
<input type="password" id="mail_password" name="mail_password"
value="<?php echo esc_attr($email_data['mail_password']); ?>"
class="regular-text" required>
</td>
</tr>
<tr>
<th><label for="full_name">Full Name</label></th>
<td>
<input type="text" id="full_name" name="full_name"
value="<?php echo esc_attr($email_data['full_name']); ?>"
class="regular-text" required>
</td>
</tr>
<tr>
<th><label for="email_signature">Email Signature</label></th>
<td>
<textarea id="email_signature" name="email_signature"
class="regular-text"><?php echo esc_textarea($email_data['email_signature']); ?></textarea>
</td>
</tr>
<tr>
<th><label for="email_provider">Email Provider</label></th>
<td>
<select id="email_provider" name="email_provider" required>
<option value="">Select Provider</option>
<?php foreach ($providers as $provider) : ?>
<option value="<?php echo esc_attr($provider->ID); ?>"<?php selected($email_data['email_provider'], $provider->ID); ?>><?php echo esc_html($provider->post_title); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
</table>
</div>
<div class="mf-form-field-block advanced-settings">
<h3 class="advanced-toggle">Advanced Settings</h3>
<div class="advanced-content" style="display: none;">
<table class="mf-form-table">
<tr>
<th><label for="smtp_password">SMTP Password</label></th>
<td>
<input type="password" id="smtp_password" name="smtp_password"
value="<?php echo esc_attr($email_data['smtp_password']); ?>"
class="regular-text">
</td>
</tr>
<tr>
<th><label for="smtp_server">SMTP Server</label></th>
<td>
<input type="text" id="smtp_server" name="smtp_server"
value="<?php echo esc_attr($email_data['smtp_server']); ?>"
class="regular-text">
</td>
</tr>
<tr>
<th><label for="smtp_port">SMTP Port</label></th>
<td>
<input type="number" id="smtp_port" name="smtp_port"
value="<?php echo esc_attr($email_data['smtp_port']); ?>"
class="small-text">
</td>
</tr>
<tr>
<th><label for="imap_password">IMAP Password</label></th>
<td>
<input type="password" id="imap_password" name="imap_password"
value="<?php echo esc_attr($email_data['imap_password']); ?>"
class="regular-text">
</td>
</tr>
<tr>
<th><label for="imap_server">IMAP Server</label></th>
<td>
<input type="text" id="imap_server" name="imap_server"
value="<?php echo esc_attr($email_data['imap_server']); ?>"
class="regular-text">
</td>
</tr>
<tr>
<th><label for="imap_port">IMAP Port</label></th>
<td>
<input type="number" id="imap_port" name="imap_port"
value="<?php echo esc_attr($email_data['imap_port']); ?>"
class="small-text">
</td>
</tr>
</table>
</div>
</div>
<p class="submit">
<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"
id="stay_on_page"
name="stay_on_page"
class="">
</div>
<?php endif; ?>
</form>
</div>
</div>
</div>
</article>
<?php endwhile;
wp_reset_postdata();
}
do_action('generate_after_main_content'); ?>
</main>
</div>
<?php
do_action('generate_after_primary_content_area');
generate_construct_sidebars();
get_footer();