/** * Schedule internal jobs */ public function schedule_internal_events() { $when = strtotime(sprintf('+%d seconds', JOB_QUEUE_WINDOW_IN_SECONDS)); $schedules = wp_get_schedules(); foreach ($this->internal_jobs as $job_args) { if (!wp_next_scheduled($job_args['action'])) { $interval = array_key_exists($job_args['schedule'], $schedules) ? $schedules[$job_args['schedule']]['interval'] : 0; $args = array('schedule' => $job_args['schedule'], 'args' => array(), 'interval' => $interval); Cron_Options_CPT::instance()->create_or_update_job($when, $job_args['action'], $args); } } }
private function find_unscheduled_jobs($new, $old) { $differences = array(); $old = collapse_events_array($old); foreach ($old as $event) { $timestamp = $event['timestamp']; $action = $event['action']; $instance = $event['instance']; if (!isset($new[$timestamp][$action][$instance])) { $differences[] = array('timestamp' => $timestamp, 'action' => $action, 'instance' => $instance); } } return $differences; } /** * Generate a standardized post name from an event's arguments */ private function event_name($timestamp, $action, $instance) { return sprintf('%s-%s-%s', $timestamp, md5($action), $instance); } /** * Generate a standardized, human-readable post title from an event's arguments */ private function event_title($timestamp, $action, $instance) { return sprintf('%s | %s | %s', $timestamp, $action, $instance); } } Cron_Options_CPT::instance();
/** * Mark an event completed, and reschedule when requested */ private function update_event_record($event) { if (false !== $event['schedule']) { // Get the existing ID $job_id = Cron_Options_CPT::instance()->job_exists($event['timestamp'], $event['action'], $event['instance'], true); // Re-implements much of the logic from `wp_reschedule_event()` $schedules = wp_get_schedules(); $interval = 0; // First, we try to get it from the schedule if (isset($schedules[$event['schedule']])) { $interval = $schedules[$event['schedule']]['interval']; } // Now we try to get it from the saved interval, in case the schedule disappears if (0 == $interval) { $interval = $event['interval']; } // If we have an interval, update the existing event entry if (0 != $interval) { // Determine new timestamp, according to how `wp_reschedule_event()` does $now = time(); $new_timestamp = $event['timestamp']; if ($new_timestamp >= $now) { $new_timestamp = $now + $interval; } else { $new_timestamp = $now + ($interval - ($now - $new_timestamp) % $interval); } // Build the expected arguments format $event_args = array('schedule' => $event['schedule'], 'args' => $event['args'], 'interval' => $interval); // Update CPT store Cron_Options_CPT::instance()->create_or_update_job($new_timestamp, $event['action'], $event_args, $job_id); // If the event could be rescheduled, don't then delete it :) if (is_int($job_id) && $job_id > 0) { return; } } } // Either event doesn't recur, or the interval couldn't be determined Cron_Options_CPT::instance()->mark_job_completed($event['timestamp'], $event['action'], $event['instance']); }