Create a gist now

Instantly share code, notes, and snippets.

Cron_Pixie::_get_schedules()
<?php
/**
* Returns list of cron schedules.
*
* @return array
*/
private function _get_schedules() {
// Get list of schedules.
$schedules = wp_get_schedules();
// Append a "Once Only" schedule.
$schedules['once'] = array(
'display' => __( 'Once Only', 'wp-cron-pixie' ),
);
// Get list of jobs assigned to schedules.
// Using "private" function is really naughty, but it's the best option compared to querying db/options.
$cron_array = _get_cron_array();
// Consistent timestamp for seconds until due.
$now = time();
// Add child cron events to schedules.
foreach ( $cron_array as $timestamp => $jobs ) {
foreach ( $jobs as $hook => $events ) {
foreach ( $events as $key => $event ) {
$event['hook'] = $hook;
$event['timestamp'] = $timestamp;
$event['seconds_due'] = $timestamp - $now;
// The cron array also includes events without a recurring schedule.
$scheduled = empty( $event['schedule'] ) ? 'once' : $event['schedule'];
$schedules[ $scheduled ]['events'][] = $event;
}
}
}
// We need to change the associative array (map) into an indexed one (set) for easier use in collection.
$set = array();
foreach ( $schedules as $name => $schedule ) {
$schedule['name'] = $name;
$set[] = $schedule;
}
return $set;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment