mailferno-theme/page-edit-email-account.php
2024-12-26 12:33:40 -06:00

332 lines
No EOL
16 KiB
PHP

<?php
/**
* Template Name: Email Account Edit template
* Template Post Type: page
*/
if (!defined('ABSPATH')) {
exit;
}
$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_data = array(
'post_title' => sanitize_email($_POST['email_address']),
'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 {
$post_id = wp_insert_post($post_data);
$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;
} else {
$post_id = 0;
}
$email_data = [
'email_address' => '',
'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
]);
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'); ?>>
<?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; ?>
</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">
<?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); ?>">
<?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" name="email_submit" class="button button-primary"
value="<?php echo $post_id ? 'Update Email Account' : 'Add Email Account'; ?>">
</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>
<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');
generate_construct_sidebars();
get_footer();