mailferno-theme/single-campaign.php

323 lines
No EOL
9.6 KiB
PHP

<?php
/**
* The Template for displaying all single campaign posts.
*
* @package GeneratePress
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
get_header(); ?>
<div <?php generate_do_attr( 'content' ); ?>>
<main <?php generate_do_attr( 'main' ); ?>>
<?php
/**
* generate_before_main_content hook.
*
* @since 0.1
*/
do_action( 'generate_before_main_content' );
if ( generate_has_default_loop() ) {
while ( have_posts() ) :
the_post();
$post_id = get_the_ID();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> <?php generate_do_microdata( 'article' ); ?>>
<div class="inside-article">
<?php
/**
* generate_before_content hook.
*
* @since 0.1
*
* @hooked generate_featured_page_header_inside_single - 10
*/
do_action( 'generate_before_content' );
if ( generate_show_entry_header() ) :
?>
<header <?php generate_do_attr( 'entry-header' ); ?>>
<?php
/**
* generate_before_entry_title hook.
*
* @since 0.1
*/
do_action( 'generate_before_entry_title' );
if ( generate_show_title() ) {
$params = generate_get_the_title_parameters();
$post_id = get_the_ID(); // Get the current post ID
$edit_url = "/dashboard/campaigns/edit-campaign?edit={$post_id}"; // Construct the edit URL
echo $params['before']; // Output the title before markup
echo '<a href="' . esc_url( $edit_url ) . '">'; // Begin the link
the_title(); // Display the title
echo '</a>'; // Close the link
echo $params['after']; // Output the title after markup
}
/**
* generate_after_entry_title hook.
*
* @since 0.1
*
* @hooked generate_post_meta - 10
*/
do_action( 'generate_after_entry_title' );
$campaign_tracking_id = get_field('campaign_tracking_id', $post_id);
?>
<span class="tracking_id"><?php echo $campaign_tracking_id; ?></span>
</header>
<?php
endif;
/**
* generate_after_entry_header hook.
*
* @since 0.1
*
* @hooked generate_post_image - 10
*/
do_action( 'generate_after_entry_header' );
$itemprop = '';
if ( 'microdata' === generate_get_schema_type() ) {
$itemprop = ' itemprop="text"';
}
?>
<div class="entry-content"<?php echo $itemprop; // phpcs:ignore -- No escaping needed. ?>>
<?php
$post = get_post();
$post_id = get_the_ID();
rl_mailwarmer_display_campaign_timeline($post);
// Get campaign stats
global $wpdb;
$messages_table = $wpdb->prefix . 'rl_mailwarmer_messages';
$conversations_table = $wpdb->prefix . 'rl_mailwarmer_conversations';
// Total stats
$total_messages = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM $messages_table WHERE campaign_id = %d",
$post_id
));
$sent_messages = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM $messages_table WHERE campaign_id = %d AND status = 'sent'",
$post_id
));
$failed_messages = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM $messages_table WHERE campaign_id = %d AND status = 'failed'",
$post_id
));
$pending_messages = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM $messages_table WHERE campaign_id = %d AND status = 'new'",
$post_id
));
$total_conversations = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM $conversations_table WHERE campaign_id = %d",
$post_id
));
// Email accounts used in this campaign
$email_accounts = $wpdb->get_results($wpdb->prepare(
"SELECT DISTINCT email_account_id FROM $conversations_table WHERE campaign_id = %d",
$post_id
));
// Upcoming messages (next 7 days)
$today = current_time('Y-m-d');
$next_week = date('Y-m-d', strtotime('+7 days'));
$upcoming_messages = $wpdb->get_results($wpdb->prepare(
// "SELECT COUNT(*)
"SELECT id, scheduled_for_timestamp, from_email, to_email, subject
FROM $messages_table
WHERE campaign_id = %d
AND (status = 'new' OR status = 'scheduled')
AND scheduled_for_timestamp BETWEEN %s AND %s
ORDER BY scheduled_for_timestamp ASC
LIMIT 20",
$post_id, $today, $next_week
));
// Recently sent messages
$sent_message_list = $wpdb->get_results($wpdb->prepare(
"SELECT id, scheduled_for_timestamp, from_email, to_email, subject, status
FROM $messages_table
WHERE campaign_id = %d
AND (status = 'sent' OR status = 'failed')
ORDER BY scheduled_for_timestamp DESC
LIMIT 20",
$post_id
));
?>
<div class="campaign-stats">
<h3>Campaign Stats</h3>
<table class="wp-list-table widefat fixed striped">
<tr>
<th>Total Messages</th>
<td><?php echo esc_html($total_messages); ?></td>
</tr>
<tr>
<th>Sent Messages</th>
<td><?php echo esc_html($sent_messages); ?></td>
</tr>
<tr>
<th>Failed Messages</th>
<td><?php echo esc_html($failed_messages); ?></td>
</tr>
<tr>
<th>Pending Messages</th>
<td><?php echo esc_html($pending_messages); ?></td>
</tr>
<tr>
<th>Total Conversations</th>
<td><?php echo esc_html($total_conversations); ?></td>
</tr>
</table>
<h3>Email Accounts</h3>
<?php if (empty($email_accounts)) : ?>
<p>No email accounts associated with this campaign.</p>
<?php else : ?>
<ul>
<?php foreach ($email_accounts as $account) :
$email = get_the_title($account->email_account_id);
if (!empty($email)) :
?>
<li><?php echo esc_html($email); ?></li>
<?php endif; endforeach; ?>
</ul>
<?php endif; ?>
<h3>Upcoming Messages (Next 7 Days)</h3>
<?php if (empty($upcoming_messages)) : ?>
<p>No upcoming messages scheduled for the next 7 days.</p>
<?php else : ?>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th>Scheduled For</th>
<th>From</th>
<th>To</th>
<th>Subject</th>
</tr>
</thead>
<tbody>
<?php foreach ($upcoming_messages as $message) : ?>
<?php
log_to_file("Message: ", $message);
?>
<tr>
<td><?php echo $message->scheduled_for_timestamp ? esc_html(date('Y-m-d H:i', strtotime($message->scheduled_for_timestamp))) : ''; ?></td>
<td><?php echo $message->from_email ? esc_html($message->from_email) : '[Not Set Yet]'; ?></td>
<td><?php echo $message->to_email ? esc_html($message->to_email) : '[Not Set Yet]'; ?></td>
<td><?php echo $message->subject ? esc_html($message->subject) : '[Not Set Yet]'; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<h3>Recent Messages</h3>
<?php if (empty($sent_message_list)) : ?>
<p>No messages have been sent yet.</p>
<?php else : ?>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th>Sent At</th>
<th>From</th>
<th>To</th>
<th>Subject</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach ($sent_message_list as $message) : ?>
<tr>
<td><?php echo esc_html(date('Y-m-d H:i', strtotime($message->scheduled_for_timestamp))); ?></td>
<td><?php echo esc_html($message->from_email); ?></td>
<td><?php echo esc_html($message->to_email); ?></td>
<td><?php echo esc_html($message->subject); ?></td>
<td><?php echo esc_html(ucfirst($message->status)); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
<?php the_content();
wp_link_pages(
array(
'before' => '<div class="page-links">' . __( 'Pages:', 'generatepress' ),
'after' => '</div>',
)
);
?>
</div>
<?php
/**
* generate_after_entry_content hook.
*
* @since 0.1
*
* @hooked generate_footer_meta - 10
*/
do_action( 'generate_after_entry_content' );
/**
* generate_after_content hook.
*
* @since 0.1
*/
do_action( 'generate_after_content' );
?>
</div>
</article>
<?php
endwhile;
}
/**
* generate_after_main_content hook.
*
* @since 0.1
*/
do_action( 'generate_after_main_content' );
?>
</main>
</div>
<?php
/**
* generate_after_primary_content_area hook.
*
* @since 2.0
*/
do_action( 'generate_after_primary_content_area' );
generate_construct_sidebars();
get_footer();