mailferno-theme/page-dashboard.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

381 lines
No EOL
12 KiB
PHP

<?php
/**
* The Template for displaying the dashboard.
*
* @package GeneratePress
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
$current_user_id = get_current_user_id();
$cloudflare_email = get_user_meta($current_user_id, 'cloudflare_api_email', true);
$cloudflare_key = get_user_meta($current_user_id, 'cloudflare_api_key', true);
$onboard_steps = [ false, false, false, false ];
// $message = "This is the notification area";
if (! ($cloudflare_email || $cloudflare_key) ) {
$message = '<a href="/membership-account/your-profile/">It looks like you haven\'t set up your CloudFlare API info yet. Click here to save that in your profile and get started!</a>';
}
$domain_count = count(get_posts([
'post_type' => 'domain',
'posts_per_page' => -1,
'status' => 'publish',
'meta_query' => [['key' => 'owner_id', 'value' => $current_user_id]]
]));
$campaign_count = count(get_posts([
'post_type' => 'campaign',
'posts_per_page' => -1,
'status' => 'publish',
'meta_query' => [['key' => 'owner_id', 'value' => $current_user_id]]
]));
$email_account_count = count(get_posts([
'post_type' => 'email-account',
'posts_per_page' => -1,
'status' => 'publish',
'meta_query' => [['key' => 'owner_id', 'value' => $current_user_id]]
]));
// Get campaigns for this user
$campaigns = get_posts([
"post_type" => "campaign",
"posts_per_page" => -1,
"status" => "publish",
"meta_query" => [['key' => 'owner_id', 'value' => $current_user_id]]
]);
// Initialize message counters and delivery stats
$messages_today = 0;
$messages_this_week = 0;
$total_sent = 0;
$total_failed = 0;
// Get message counts from database
global $wpdb;
$messages_table = $wpdb->prefix . 'rl_mailwarmer_messages';
// Get campaign IDs
$campaign_ids = array();
foreach ($campaigns as $campaign) {
$campaign_ids[] = $campaign->ID;
}
if (!empty($campaign_ids)) {
// Convert to comma-separated string for SQL
$campaign_ids_str = implode(",", $campaign_ids);
// Messages scheduled today
$today = date("Y-m-d");
$messages_today = $wpdb->get_var(
"SELECT COUNT(*) FROM $messages_table
WHERE campaign_id IN ($campaign_ids_str)
AND (status = 'new' OR status = 'scheduled')
AND DATE(scheduled_for_timestamp) = '$today'"
);
// Messages scheduled this week (next 7 days)
$week_end = date("Y-m-d", strtotime("+7 days"));
$messages_this_week = $wpdb->get_var(
"SELECT COUNT(*) FROM $messages_table
WHERE campaign_id IN ($campaign_ids_str)
AND (status = 'new' OR status = 'scheduled')
AND DATE(scheduled_for_timestamp) BETWEEN '$today' AND '$week_end'"
);
// Get total sent and failed messages
$total_sent = $wpdb->get_var(
"SELECT COUNT(*) FROM $messages_table
WHERE campaign_id IN ($campaign_ids_str)
AND status = 'sent'"
);
$total_failed = $wpdb->get_var(
"SELECT COUNT(*) FROM $messages_table
WHERE campaign_id IN ($campaign_ids_str)
AND status = 'failed'"
);
}
// Calculate delivery rate
$delivery_rate = 0;
if (($total_sent + $total_failed) > 0) {
$delivery_rate = round(($total_sent / ($total_sent + $total_failed)) * 100, 1);
}
if ( ($cloudflare_email || $cloudflare_key) ) {
$onboard_steps[0] = true;
}
if ($domain_count > 0) {
$onboard_steps[1] = true;
}
if ($email_account_count > 0) {
$onboard_steps[2] = true;
}
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' ); ?>>
<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();
?>
<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();
// the_title( $params['before'], $params['after'] );
// }
?>
<div class="dash_summary">
<?php if (isset($message) ) : ?>
<div class="message_area_wrapper">
<div class="message_area notice notice-success">
<?php echo $message; ?>
</div>
</div>
<?php endif; ?>
<!-- <div class="summary_table_wrapper dash-card">
<table class="summary-table">
<tr>
<th><a href="/dashboard/domains">Total Domains</a></th>
<th><a href="/dashboard/campaigns">Total Campaigns</a></th>
<th><a href="/dashboard/email-accounts">Total Email Accounts</a></th>
</tr>
<tr>
<td><a href="/dashboard/domains"><?php //echo $domain_count; ?></a></td>
<td><a href="/dashboard/campaigns"><?php //echo $campaign_count; ?></a></td>
<td><a href="/dashboard/email-accounts"><?php //echo $email_count; ?></a></td>
</tr>
</table>
</div> -->
</div>
<?php
/**
* generate_after_entry_title hook.
*
* @since 0.1
*
* @hooked generate_post_meta - 10
*/
do_action( 'generate_after_entry_title' );
?>
</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 mf-dashboard-page"<?php echo $itemprop; // phpcs:ignore -- No escaping needed. ?>>
<?php
the_content();
wp_link_pages(
array(
'before' => '<div class="page-links">' . __( 'Pages:', 'generatepress' ),
'after' => '</div>',
)
);
?>
<div class="dash-row-wrapper">
<div class="dash-card header-card"><h3>Campaigns</h3><span class="card_data"><?php echo $campaign_count; ?></span></div>
<div class="dash-card header-card"><h3>Email Accounts</h3><span class="card_data"><?php echo $email_account_count; ?></span></div>
<div class="dash-card header-card"><h3>Domains</h3><span class="card_data"><?php echo $domain_count; ?></span></div>
<div class="dash-card header-card"><h3>Messages Scheduled</h3>
<span class="card_data">
<span><span>Today: <?php echo esc_html($messages_today); ?><br>This Week: <?php echo esc_html($messages_this_week); ?></span></span>
</span></div>
<div class="dash-card header-card"><h3>Delivery Rate</h3><span class="card_data"><?php echo esc_html($delivery_rate); ?>%</span></div>
<div class="dash-card header-card"><h3>Message Status</h3><span class="card_data">Sent: <?php echo esc_html($total_sent); ?><br>Failed: <?php echo esc_html($total_failed); ?></span></div>
<!-- <div class="dash-card header-card"></div>
<div class="dash-card header-card"></div> -->
</div>
<div class="dash-row-wrapper">
<div class="dash-card">
<?php
if ( ($onboard_steps[0] = false || $onboard_steps[1] = false || $onboard_steps[2] = false || $onboard_steps[3] = false)) :
?>
<h2>Get Started:</h2>
<ol class="onboarding_list">
<li class="<?php echo $onboard_steps[0] ?'finished': ''; ?>" ><a href="/membership-account/your-profile/">Save your CloudFlare API Credentials</a></li>
<li class="<?php echo $onboard_steps[1] ?'finished': ''; ?>" ><a href="/dashboard/domains/edit-domain/">Add a Domain and verify DNS records</a></li>
<li class="<?php echo $onboard_steps[2] ?'finished': ''; ?>" ><a href="/dashboard/email-accounts/edit-email-account/">Add one or more Email Accounts</a></li>
<li class="<?php echo $onboard_steps[3] ?'finished': ''; ?>" ><a href="/dashboard/campaigns/edit-campaign/">Create a Campaign</a></li>
</ol>
<?php endif; ?>
</div>
<!-- <div class="dash-card"></div> -->
</div>
<!-- <div class="dash-row-wrapper">
<div class="dash-card"></div>
<div class="dash-card"></div>
<div class="dash-card"></div>
</div> -->
<!-- <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>
<?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();