Download the PHP package pollora/schedule without Composer
On this page you can find all versions of the php package pollora/schedule. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pollora/schedule
More information about pollora/schedule
Files in pollora/schedule
Package schedule
Short Description Scheduled and deferred tasks for WordPress: one engine, a fluent API and #[Schedule] attributes, WP-Cron as the default driver. Runs with or without Laravel.
License MIT
Homepage https://pollora.dev/advanced/scheduling/
Informations about the package schedule
Scheduled tasks for WordPress, written as code you can read: a #[Schedule] attribute on a method, or a fluent API where the event depends on a condition or a computed value. One engine behind both, WP-Cron as the default driver.
Part of Pollora, the Laravel framework for WordPress, where it is installed and wired for you: see Scheduling. It runs without Laravel too, in a WordPress plugin, a Bedrock site or any PHP project.
Installation
Requires PHP 8.3+. It builds on pollora/attributes and pollora/discovery.
Recurring events
at('02:00') is the next time the clock of the site shows it, in the site's timezone. Declare the event on every request: the handler is attached each time, the event is scheduled only when none with the same hook and arguments exists.
Single events
call() takes an invokable class, [Class::class, 'method'], a closure, or the name of a WordPress hook to fire. The handler receives the arguments as one array, and nothing when there are none. Give the engine a PSR-11 container (Schedule::useContainer($container)) and the classes are built with their dependencies; without one they are built with new.
Attributes
ScheduleDiscovery finds them with pollora/discovery and hands them to the engine; the hook is named after the class and method when you give none.
Drivers
A driver is where events are kept and who runs them. WpCronDriver is the default: a thin layer over wp_schedule_event(), wp_schedule_single_event() and the other functions of WordPress, so every filter the site hangs on them keeps working. Choose the driver of the site with Schedule::driver('action-scheduler') or POLLORA_SCHEDULE_DRIVER=action-scheduler. Events then live in Action Scheduler (its queue, its retries, its Tools › Scheduled Actions screen), in a group of their own, and WordPress's own functions follow: wp_schedule_event() called by the core, a plugin or a theme reaches it too. Events already in the cron option stay on WP-Cron, as do Action Scheduler's own queue runner, the hooks you protect (ScheduleEngine::protect(), pollora/schedule/protected_hooks) and every event when Action Scheduler is not there.
Another driver implements Pollora\Schedule\Contracts\ScheduleDriver and is given to the engine with Schedule::useDriver().
A driver with no store of its own
A queue of deferred jobs (the Laravel queue, which the Pollora framework adds as the laravel driver) cannot be asked what it holds. It implements Pollora\Schedule\Contracts\Identifies as well: the engine gives each placement an identifier (Event::$id), writes it in the entry of the cron option and hands the event to placeIdentified(). When the job runs, it runs only if the entry still exists and carries its identifier, so cancelling an event is removing its entry, and a job that was lost can be sent again from the index.
Work another package queues
new ActionSchedulerDriver('my-group') keeps its actions in a group of its own, which the cron index below never follows. enqueue($hook, $timestamp, $args) stores an action as it is, with no duplicate check, and queues an async action when the time is now: for a caller that gives each event an identifier and queues many.
The cron option as an index
Whatever the driver, the cron option lists every scheduled event. An event another driver keeps carries a driver key next to schedule and args; an entry without it is WP-Cron's. WordPress reads the option without a query, so wp_next_scheduled(), Site Health, WP Crontrol, Query Monitor and wp cron event list all see the event, and WP-Cron never runs one it does not own.
A plugin that rewrites the whole array still works: removing or moving an entry reaches the driver that keeps it.
Changing driver
Setting Schedule::driver('action-scheduler') (or POLLORA_SCHEDULE_DRIVER) sends new events to Action Scheduler; what WP-Cron holds stays there until you move it:
An event is created in the target before it leaves the source, so a crash duplicates it instead of losing it, and running the command again does not create it twice. A recurring event keeps the interval its entry stores. Hooks the driver protects stay on WP-Cron.
wp cron event list shows the type and the driver of each event, and takes --type=once|recurring and --driver=<name> like its other filters.
Testing
Nothing is scheduled and no handler is attached while faking. Schedule::reset() brings the real driver back.
License
MIT. See LICENSE.
All versions of schedule with dependencies
pollora/attributes Version ^0.1 || ^1.0
pollora/discovery Version ^0.1 || ^0.2 || ^0.3 || ^1.0
psr/container Version ^1.1 || ^2.0
psr/log Version ^3.0