API Reference
Schedule
Central task registry that manages all scheduled tasks.
Namespace: WPZylos\Framework\Scheduler\Schedule
Methods
call(callable $callback): ScheduledTask
Schedule a callable task (closure or invokable).
$task = $schedule->call(function () {
// Task logic
});
Parameters:
$callback— The callable to execute on schedule
Returns: ScheduledTask — Fluent task instance for chaining
command(string $command): ScheduledTask
Schedule a WP-CLI command.
$task = $schedule->command('cache flush');
Parameters:
$command— The WP-CLI command string (withoutwpprefix)
Returns: ScheduledTask
job(string $jobClass): ScheduledTask
Schedule a queue job class for dispatching.
$task = $schedule->job(ProcessOrders::class);
Parameters:
$jobClass— Fully qualified class name of the job
Returns: ScheduledTask
getTasks(): array
Get all registered tasks.
$tasks = $schedule->getTasks(); // ScheduledTask[]
Returns: ScheduledTask[]
getDueTasks(): array
Get only tasks that are currently due to run.
$dueTasks = $schedule->getDueTasks(); // ScheduledTask[]
Returns: ScheduledTask[]
run(): int
Run all due tasks. Catches exceptions per-task and fires wpzylos_scheduler_task_failed on failure.
$executed = $schedule->run();
Returns: int — Number of tasks that executed successfully
hasTasks(): bool
Check if any tasks are registered.
if ($schedule->hasTasks()) { /* ... */ }
Returns: bool
ScheduledTask
Individual scheduled task with frequency, constraint, and lock configuration.
Namespace: WPZylos\Framework\Scheduler\ScheduledTask
Uses traits: Frequencies, ManagesLocks
Constructor
public function __construct(mixed $callback, string $type = 'callback')
Parameters:
$callback— The callback, command string, or job class name$type— Task type:'callback','command', or'job'
Methods
name(string $name): static
Set a unique name for this task.
$task->name('my-unique-task');
when(callable $callback): static
Only run the task when the callback returns true.
$task->when(fn() => get_option('enabled') === '1');
skip(callable $callback): static
Skip the task when the callback returns true.
$task->skip(fn() => is_admin());
between(string $start, string $end): static
Only run between specific times (HH:MM format).
$task->between('09:00', '17:00');
onOneServer(): static
Only run on one server in a multisite setup.
$task->onOneServer();
isDue(): bool
Check if this task is due to run based on current time and all constraints.
if ($task->isDue()) { /* ... */ }
run(): void
Execute the task. Handles lock acquisition/release and dispatches based on type.
$task->run();
getName(): string
Get the task name (or auto-generated key if unnamed).
$name = $task->getName();
getType(): string
Get the task type ('callback', 'command', or 'job').
$type = $task->getType();
Frequencies (Trait)
All frequency methods available on ScheduledTask.
Namespace: WPZylos\Framework\Scheduler\Frequencies
Properties
| Property | Type | Default | Description |
|---|---|---|---|
$intervalSeconds | int | 3600 | Cron interval in seconds |
$atTime | ?string | null | Specific time (HH:MM) |
$dayOfWeek | ?int | null | Day of week (0=Sun, 6=Sat) |
$dayOfMonth | ?int | null | Day of month (1-31) |
$atMinute | ?int | null | Minute of hour (0-59) |
Methods
| Method | Interval | Description |
|---|---|---|
everyMinute() | 60s | Run every minute |
everyFiveMinutes() | 300s | Run every 5 minutes |
everyTenMinutes() | 600s | Run every 10 minutes |
everyFifteenMinutes() | 900s | Run every 15 minutes |
everyThirtyMinutes() | 1800s | Run every 30 minutes |
hourly() | 3600s | Run every hour |
hourlyAt(int $minute) | 3600s | Run hourly at specific minute |
daily() | 86400s | Run daily at midnight |
dailyAt(string $time) | 86400s | Run daily at HH:MM |
weekly() | 604800s | Run weekly (Sunday midnight) |
weeklyOn(int $day, string $time) | 604800s | Run weekly on day at time |
monthly() | 2592000s | Run monthly (1st at midnight) |
monthlyOn(int $day, string $time) | 2592000s | Run monthly on day at time |
getIntervalSeconds() | — | Get current interval |
ManagesLocks (Trait)
Transient-based overlap prevention.
Namespace: WPZylos\Framework\Scheduler\ManagesLocks
Properties
| Property | Type | Default | Description |
|---|---|---|---|
$preventOverlap | bool | false | Whether overlap prevention is enabled |
$lockExpiresAt | int | 1440 | Lock expiration in minutes (24 hours) |
Methods
withoutOverlapping(int $expiresAt = 1440): static
Enable overlap prevention with optional lock expiration.
$task->withoutOverlapping(); // 24-hour lock
$task->withoutOverlapping(60); // 1-hour lock
acquireLock(string $key): bool (protected)
Attempt to acquire a transient lock. Returns false if lock already exists.
releaseLock(string $key): void (protected)
Release a transient lock.
SchedulerServiceProvider
Service provider that registers the scheduler and integrates with WP-Cron.
Namespace: WPZylos\Framework\Scheduler\SchedulerServiceProvider
Implements: ServiceProviderInterface
Methods
register(ApplicationInterface $app): void
Registers Schedule::class and 'scheduler' as singletons in the container.
boot(ApplicationInterface $app): void
- Adds
every_minuteto WordPress cron schedules - Registers the cron action to run due tasks
- Schedules the recurring cron event on
init