89 lines
2.2 KiB
PHP
89 lines
2.2 KiB
PHP
<?php
|
|
/**
|
|
* Handles WP-Cron scheduling for RL MailWarmer.
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class RL_MailWarmer_Scheduler
|
|
{
|
|
/**
|
|
* Initialize the scheduler.
|
|
*/
|
|
public static function init()
|
|
{
|
|
// Hook into WP-Cron for sending emails
|
|
add_action('rl_mailwarmer_send_emails', [__CLASS__, 'send_scheduled_emails']);
|
|
}
|
|
|
|
/**
|
|
* Schedule WP-Cron jobs.
|
|
*/
|
|
public static function schedule_cron_jobs()
|
|
{
|
|
if (!wp_next_scheduled('rl_mailwarmer_send_emails')) {
|
|
wp_schedule_event(time(), 'hourly', 'rl_mailwarmer_send_emails');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Clear WP-Cron jobs on deactivation.
|
|
*/
|
|
public static function clear_cron_jobs()
|
|
{
|
|
$timestamp = wp_next_scheduled('rl_mailwarmer_send_emails');
|
|
if ($timestamp) {
|
|
wp_unschedule_event($timestamp, 'rl_mailwarmer_send_emails');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Send scheduled emails for campaigns.
|
|
*/
|
|
public static function send_scheduled_emails()
|
|
{
|
|
// Fetch campaigns with active schedules
|
|
$campaigns = get_posts([
|
|
'post_type' => 'campaign',
|
|
'post_status' => 'publish',
|
|
'meta_query' => [
|
|
[
|
|
'key' => 'email_schedule',
|
|
'compare' => 'EXISTS',
|
|
],
|
|
],
|
|
]);
|
|
|
|
if (!$campaigns) {
|
|
return;
|
|
}
|
|
|
|
foreach ($campaigns as $campaign) {
|
|
// Get scheduled emails
|
|
$schedule = get_post_meta($campaign->ID, 'email_schedule', true);
|
|
|
|
if (!is_array($schedule)) {
|
|
continue;
|
|
}
|
|
|
|
foreach ($schedule as $email) {
|
|
// Check if the email is ready to be sent
|
|
$send_time = strtotime($email['send_time']);
|
|
if ($send_time > time()) {
|
|
continue;
|
|
}
|
|
|
|
// Send the email
|
|
RL_MailWarmer_Email_Handler::send_email($email);
|
|
|
|
// Mark as sent
|
|
$email['sent'] = true;
|
|
}
|
|
|
|
// Update the schedule
|
|
update_post_meta($campaign->ID, 'email_schedule', $schedule);
|
|
}
|
|
}
|
|
}
|