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 (without wp prefix)

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

PropertyTypeDefaultDescription
$intervalSecondsint3600Cron interval in seconds
$atTime?stringnullSpecific time (HH:MM)
$dayOfWeek?intnullDay of week (0=Sun, 6=Sat)
$dayOfMonth?intnullDay of month (1-31)
$atMinute?intnullMinute of hour (0-59)

Methods

MethodIntervalDescription
everyMinute()60sRun every minute
everyFiveMinutes()300sRun every 5 minutes
everyTenMinutes()600sRun every 10 minutes
everyFifteenMinutes()900sRun every 15 minutes
everyThirtyMinutes()1800sRun every 30 minutes
hourly()3600sRun every hour
hourlyAt(int $minute)3600sRun hourly at specific minute
daily()86400sRun daily at midnight
dailyAt(string $time)86400sRun daily at HH:MM
weekly()604800sRun weekly (Sunday midnight)
weeklyOn(int $day, string $time)604800sRun weekly on day at time
monthly()2592000sRun monthly (1st at midnight)
monthlyOn(int $day, string $time)2592000sRun monthly on day at time
getIntervalSeconds()Get current interval

ManagesLocks (Trait)

Transient-based overlap prevention.

Namespace: WPZylos\Framework\Scheduler\ManagesLocks

Properties

PropertyTypeDefaultDescription
$preventOverlapboolfalseWhether overlap prevention is enabled
$lockExpiresAtint1440Lock 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_minute to WordPress cron schedules
  • Registers the cron action to run due tasks
  • Schedules the recurring cron event on init