public function get_events()
{
$events = get_option('cron');
// That was easy
if (!is_array($events) || empty($events)) {
return array('events' => null);
}
// Simplify array format for further processing
$events = collapse_events_array($events);
// Select only those events to run in the next sixty seconds
// Will include missed events as well
$current_events = $internal_events = array();
$current_window = strtotime(sprintf('+%d seconds', JOB_QUEUE_WINDOW_IN_SECONDS));
foreach ($events as $event) {
// Skip events whose time hasn't come
if ($event['timestamp'] > $current_window) {
continue;
}
// Skip events that don't have any callbacks hooked to their actions, unless their execution is requested
if (!$this->action_has_callback_or_should_run_anyway($event)) {
continue;
}
// Necessary data to identify an individual event
// `$event['action']` is hashed to avoid information disclosure
// Core hashes `$event['instance']` for us
$event_data_public = array('timestamp' => $event['timestamp'], 'action' => md5($event['action']), 'instance' => $event['instance']);
// Queue internal events separately to avoid them being blocked
if (is_internal_event($event['action'])) {
$internal_events[] = $event_data_public;
} else {
$current_events[] = $event_data_public;
}
}
// Limit batch size to avoid resource exhaustion
if (count($current_events) > JOB_QUEUE_SIZE) {
$current_events = $this->reduce_queue($current_events);
}
// Combine with Internal Events and return necessary data to process the event queue
return array('events' => array_merge($current_events, $internal_events), 'endpoint' => get_rest_url(null, REST_API::API_NAMESPACE . '/' . REST_API::ENDPOINT_RUN));
}