CronScheduler

Class.

use Saij\WpAppFramework\Cron\CronScheduler;

Schedules one-time WP-Cron tasks of the theme or plugin. Inject it into a service:

$this->cron->schedule(new SendReminder($postId), new DateTimeImmutable('+1 hour'));

A task is an object of a value class with a #[CronTask] handler. Two tasks are the same if their values are equal: WP-Cron stores the values of the task as the argument of the event and finds, compares and unschedules events by their arguments.

Methods

isScheduled()

isScheduled(object $task): bool

Whether a task equal to the given one is scheduled.

ParameterTypeDefaultDescription
taskobjectrequiredThe task: an object of a value class that is the first parameter of a #[CronTask] handler of this theme or plugin. Equal to the task to look for: same class, equal values.

Returns bool

Throws LogicException: If the task has no #[CronTask] handler in this theme or plugin.

schedule()

schedule(object $task, DateTimeInterface $at): bool

Schedules the task to run at the given time (or on the next request after it).

ParameterTypeDefaultDescription
taskobjectrequiredThe task: an object of a value class that is the first parameter of a #[CronTask] handler of this theme or plugin.
atDateTimeInterfacerequiredThe time to run the task, in any time zone.

Returns bool: True if scheduled, false if WordPress already has the same task within 10 minutes of that time.

Throws RuntimeException: If WordPress refuses the task for another reason (e.g. a time before 1970, or a plugin).

Throws LogicException: If the task has no #[CronTask] handler in this theme or plugin.

unschedule()

unschedule(object $task): int

Removes every scheduled task equal to the given one.

ParameterTypeDefaultDescription
taskobjectrequiredThe task: an object of a value class that is the first parameter of a #[CronTask] handler of this theme or plugin. Equal to the task to remove: same class, equal values.

Returns int: The number of tasks removed.

Throws RuntimeException: If WordPress cannot save the cron events.

Throws LogicException: If the task has no #[CronTask] handler in this theme or plugin.