/** * Execute a specific event */ public function run_event($request) { // Parse request for details needed to identify the event to execute // `$timestamp` is, unsurprisingly, the Unix timestamp the event is scheduled for // `$action` is the md5 hash of the action used when the event is registered // `$instance` is the md5 hash of the event's arguments array, which Core uses to index the `cron` option $event = $request->get_json_params(); $timestamp = isset($event['timestamp']) ? absint($event['timestamp']) : null; $action = isset($event['action']) ? trim(sanitize_text_field($event['action'])) : null; $instance = isset($event['instance']) ? trim(sanitize_text_field($event['instance'])) : null; return rest_ensure_response(Events::instance()->run_event($timestamp, $action, $instance)); }
} // 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']); } } Events::instance();