94 lines
3.5 KiB
PHP
94 lines
3.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Scheduler class for the RL MailWarmer plugin.
|
|
* Handles scheduling and execution of tasks, including fetching AI-generated content for conversations and sending pending messages
|
|
*/
|
|
class RL_MailWarmer_Scheduler {
|
|
|
|
/**
|
|
* Initialize the scheduler by setting up hooks.
|
|
*/
|
|
public static function init() {
|
|
add_filter('cron_schedules', [__CLASS__, 'add_cron_timings']);
|
|
|
|
// Call local functions to call the remote classes, so we can debug cron jobs
|
|
add_action('rl_mailwarmer_process_messages', [__CLASS__, 'cron_process_pending_messages']);
|
|
add_action('rl_mailwarmer_process_upcoming_conversations', [__CLASS__, 'cron_process_upcoming_conversations']);
|
|
// add_action('rl_mailwarmer_process_messages', [RL_MailWarmer_Message_Handler::class, 'process_pending_messages']);
|
|
// add_action('rl_mailwarmer_process_upcoming_conversations', [RL_MailWarmer_Conversation_Handler::class, 'process_upcoming_conversations']);
|
|
|
|
|
|
self::schedule_cron_jobs();
|
|
}
|
|
|
|
/**
|
|
* Add a custom schedule for every minute & every 5 minutes.
|
|
*
|
|
* @param array $schedules Existing cron schedules.
|
|
* @return array Modified cron schedules.
|
|
*/
|
|
public static function add_cron_timings($schedules) {
|
|
if (!isset($schedules['every_minute'])) {
|
|
$schedules['every_minute'] = [
|
|
'interval' => 60, // 60 seconds = 1 minute
|
|
'display' => __('Every Minute', 'rl-mailwarmer'),
|
|
];
|
|
$schedules['every_five_minutes'] = [
|
|
'interval' => 300,
|
|
'display' => __('Every 5 Minutes', 'rl-mailwarmer'),
|
|
];
|
|
}
|
|
return $schedules;
|
|
}
|
|
|
|
/**
|
|
* Schedule the cron task if not already scheduled.
|
|
*/
|
|
public static function schedule_cron_jobs() {
|
|
|
|
// Message handler
|
|
if (!wp_next_scheduled('rl_mailwarmer_process_messages')) {
|
|
wp_schedule_event(time(), 'every_minute', 'rl_mailwarmer_process_messages');
|
|
}
|
|
|
|
// Conversation handler
|
|
if (!wp_next_scheduled('rl_mailwarmer_process_upcoming_conversations')) {
|
|
wp_schedule_event(time(), 'every_minute', 'rl_mailwarmer_process_upcoming_conversations');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Clear the scheduled task.
|
|
*/
|
|
public static function clear_cron_jobs() {
|
|
|
|
// Message handler
|
|
$timestamp = wp_next_scheduled('rl_mailwarmer_process_messages');
|
|
if ($timestamp) {
|
|
wp_unschedule_event($timestamp, 'rl_mailwarmer_process_messages');
|
|
}
|
|
|
|
// Conversation handler
|
|
$timestamp = wp_next_scheduled('rl_mailwarmer_process_upcoming_conversations');
|
|
if ($timestamp) {
|
|
wp_unschedule_event($timestamp, 'rl_mailwarmer_process_upcoming_conversations');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Process pending messages by delegating to the Message Handler.
|
|
*/
|
|
public static function cron_process_pending_messages() {
|
|
// action_log("====================== Running Cron to process messages ========================");
|
|
RL_MailWarmer_Message_Handler::process_pending_messages();
|
|
}
|
|
|
|
/**
|
|
* Process pending conversations by delegating to the Message Handler.
|
|
*/
|
|
public static function cron_process_upcoming_conversations() {
|
|
// action_log("====================== Running Cron to process conversations ========================");
|
|
RL_MailWarmer_Conversation_Handler::process_upcoming_conversations();
|
|
}
|
|
}
|