Cron
Introduction
Scheduler wires callbacks onto WP-Cron hooks and keeps the schedule declaration in one place. Added in wp-kit 2.4.0.
use YourPlugin\Deps\BitApps\WPKit\Cron\Scheduler;
$scheduler = new Scheduler();
$scheduler->job('yourplugin_hourly_sync', 'hourly', function () {
// runs every hour
});
$scheduler->boot();
boot() registers the hooks and schedules anything not already scheduled — call it on every request, not just activation.
Custom intervals
WordPress ships hourly, twicedaily, daily and weekly. Anything else has to be declared before a job can use it:
$scheduler->addSchedule('every_minute', 60, 'Every Minute');
$scheduler->job('yourplugin_sync', 'every_minute', function () {
// ...
});
addSchedule($name, $intervalSeconds, $display) — the display name is what appears in cron plugins.
Jobs
// recurring
$scheduler->job('yourplugin_sync', 'hourly', [$syncService, 'run']);
// with arguments
$scheduler->job('yourplugin_digest', 'daily', [$mailer, 'send'], ['weekly-digest']);
// one-off
$scheduler->once('yourplugin_one_time', time() + 3600, function () {
// fires once, an hour from now
});
Both take the hook name first, so a job is identified by that hook everywhere else.
Clearing
$scheduler->unschedule('yourplugin_sync'); // one hook
$scheduler->clearAll(); // every hook this scheduler knows
Call clearAll() from your deactivation hook. Events left behind keep firing on a hook nothing listens to any more.
WP-Cron is triggered by site traffic, not by the clock. A quiet site runs jobs late, and DISABLE_WP_CRON sites do not run them at all unless a real cron job calls wp-cron.php. Do not use it for anything time-critical without a server-side trigger.