mailferno-theme/page-edit-campaign.php

336 lines
No EOL
18 KiB
PHP

<?php
/**
* Template Name: Campaign Edit template
* Template Post Type: page
*/
if (!defined('ABSPATH')) {
exit;
}
$current_user_id = get_current_user_id();
$new_post = true;
$campaign_enabled = true;
$professions = [];
// 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;
} else {
$post_id = 0;
}
if (isset($_GET['Email_ID'])) {
$email_id = intval($_GET['Email_ID']);
$domain = get_field('domain', $email_id);
if (is_object($domain)) {
$domain_id = $domain->ID;
} else {
$domain_id = $domain;
}
$email_accounts[] = $email_id;
$campaign_name = get_the_title($email_id);
} else {
$email_accounts = [];
$domain_id = '';
$campaign_name = '';
}
$campaign_data = [
'campaign_name' => $campaign_name,
'domain_id' => $domain_id,
'email_accounts' => $email_accounts,
'num_additional_emails' => '',
'start_date' => date('Y-m-d', strtotime('today')),
'warmup_period' => 8,
'starting_volume' => 5,
'target_volume' => 200,
'weekend_reduction_factor' => 60,
'target_profession' => '',
'target_profession_other' => '',
'campaign_conversation_topics' => ''
];
if ($post_id > 0) {
$post_item = get_post($post_id);
if ($post_item && $post_item->post_type === 'campaign') {
$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) : '';
if ($domain_id) {
$campaign_data[$key] = $domain_id->ID;
}
break;
case 'email_accounts':
$email_accounts = get_field($key, $post_id) ? get_field($key, $post_id) : '';
$campaign_data[$key] = $email_accounts;
break;
case 'start_date':
$start_date = get_field($key, $post_id) ? get_field($key, $post_id) : '';
$campaign_data[$key] = date('Y-m-d', strtotime($start_date));
break;
default:
$campaign_data[$key] = get_field($key, $post_id);
break;
}
}
}
}
}
// 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
]
]
]);
log_to_file("page-edit-campaign.php - Domains: ", $domains);
// Get the list of default professions
$professions = rl_get_textarea_meta_as_array('option', 'default_profession_pool');
// 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(); ?>
<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 style="display:inline-block;"><?php echo $post_id ? 'Edit Campaign' : 'Add New Campaign'; ?></h1>
<?php if ($post_id) : ?>
<span id="view_post_link"><a href="<?php echo get_the_permalink($post_id); ?>">View Campaign</a></span>
<?php endif; ?>
<form id="campaign-form" class="mf-dashboard-form">
<?php wp_nonce_field('campaign_form_nonce', 'campaign_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="campaign_name">Campaign Name</label></th>
<td>
<input type="text" id="campaign_name" name="campaign_name"
value="<?php echo esc_attr($campaign_data['campaign_name']); ?>"
class="regular-text" required>
</td>
</tr>
<tr>
<th><label for="enabled">Enable this campaign?</label></th>
<td>
<input type="checkbox"
id="enabled"
name="enabled"
<?php echo $campaign_enabled || $new_post ? 'checked' : ''; ?>
class="">
<span>Disabled campaigns will not send any mail until they are re-enabled</span>
</td>
</tr>
<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 $domain->ID; ?>"<?php selected($campaign_data['domain_id'], $domain->ID); ?>><?php echo esc_html($domain->post_title); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr>
<th><label for="email_accounts">Email Accounts</label></th>
<td>
<select id="email_accounts" name="email_accounts[]" multiple required class="email-accounts-select">
<?php
$selected_accounts = get_post_meta($post_id, 'email_accounts') ? get_post_meta($post_id, 'email_accounts', true) : $campaign_data['email_accounts'];
$user_email_accounts = get_posts([
'post_type' => 'email-account',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => [
[
'key' => 'owner_id',
'value' => $current_user_id
]
]
]);
if ($user_email_accounts) :
foreach ($user_email_accounts as $account) :
$account_id = $account->ID;
$account_name = $account->post_title; ?>
<option value="<?php echo esc_attr($account_id); ?>"<?php echo in_array($account_id, $selected_accounts) ? 'selected' : ''; ?>><?php echo esc_html($account_name); ?></option>
<?php endforeach; ?>
<?php endif; ?>
</select>
</td>
</tr>
<tr>
<th><label for="start_date">Start Date</label></th>
<td>
<?php
$start_date = isset($campaign_data['start_date']) && !empty($campaign_data['start_date'])
? $campaign_data['start_date']
: date('Y-m-d');
?>
<input type="date" id="start_date" name="start_date"
value="<?php echo esc_attr($start_date); ?>" required>
</td>
</tr>
<tr>
<th><label for="warmup_period">Warmup Period (in weeks)</label></th>
<td>
<input type="number" id="warmup_period" name="warmup_period"
value="<?php echo esc_attr($campaign_data['warmup_period']); ?>"
class="small-text" min="1" max="52" placeholder="8" required>
</td>
</tr>
<tr>
<th><label for="starting_volume">Starting Daily Volume</label></th>
<td>
<input type="number" id="starting_volume" name="starting_volume"
value="<?php echo esc_attr($campaign_data['starting_volume']); ?>"
class="small-text" min="1" max="50" placeholder="5" required>
</td>
</tr>
<tr>
<th><label for="target_volume">Target Daily Volume</label></th>
<td>
<input type="number" id="target_volume" name="target_volume"
value="<?php echo esc_attr($campaign_data['target_volume']); ?>"
class="small-text" min="5" max="1000" placeholder="50" required>
</td>
</tr>
<tr>
<th><label for="weekend_reduction_factor">Weekend Reduction Factor</label></th>
<td>
<input type="number" id="weekend_reduction_factor" name="weekend_reduction_factor"
value="<?php echo esc_attr($campaign_data['weekend_reduction_factor']); ?>"
class="small-text" min="10" max="90" placeholder="25" step="5">%
<span>Reduce traffic on weekend & holidays by this percentage</span>
</td>
</tr>
<tr>
<th><label for="target_profession">Target Profession</label></th>
<td>
<select id="target_profession" name="target_profession">
<option value="">Select Profession</option>
<?php foreach ($professions as $profession) :
?>
<option value="<?php echo htmlentities($profession); ?>"<?php selected($campaign_data['target_profession'], $profession); ?>><?php echo htmlentities($profession); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr>
<th><label for="target_profession_other">Other Profession</label></th>
<td>
<input type="text" id="target_profession_other" name="target_profession_other"
value="<?php echo esc_attr($campaign_data['target_profession_other']); ?>"
class="regular-text">
</td>
</tr>
<tr>
<th><label for="campaign_conversation_topics">Conversation Topics</label></th>
<td>
<textarea id="campaign_conversation_topics" name="campaign_conversation_topics"
class="regular-text" rows="5"><?php echo esc_textarea($campaign_data['campaign_conversation_topics']); ?></textarea>
</td>
</tr>
<tr>
<th><label for="calculate_timeline">Calculate the campaign timeline?</label></th>
<td>
<input type="checkbox"
id="calculate_timeline"
name="calculate_timeline"
<?php echo $new_post ? 'checked' : ''; ?>
class="">
<span>This will delete any pending messages and recreate the timeline based on the above fields</span>
</td>
</tr>
</table>
</div>
<p class="submit">
<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 (true) : ?>
<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();