Search by

cosray / content-schedule

ernstla

Plan when a Cosray node goes online, goes offline or its draft goes live

Package info

github.com/cosraycms/plugin-content-schedule

Homepage

Type:cosray-plugin

pkg:composer/cosray/content-schedule

Statistics

Installs: 13

Dependents: 0

Suggesters: 0

Stars: 0

dev-main 2026-11-29 17:25 UTC

This package is auto-updated.

Last update: 2026-11-29 17:26:14 UTC


README

A Cosray plugin for scheduled publishing. In a node's editor, an editor plans when the node goes online and when it goes offline. Going online brings the changes saved by then live, so the same time plans an update of a node that is online. An overview lists what is planned across the site.

Nothing about a plan is checked when visitors read the site. At the planned time a job does what the editor would do by hand, so the node's history, webhooks, the API's sync and search all see an ordinary publication.

Installation

composer require cosray/content-schedule

Register the plugin in the app's bootstrap and apply its migration:

$app->plugin(\Cosray\ContentSchedule\Plugin::class);
php run db:migrations --apply

The plugin keeps one table, plans, in a database namespace of its own: the schema content_schedule on PostgreSQL, which Cosray creates, or the name prefix content_schedule_ where the site uses name prefixes. A site chooses another one with 'content-schedule.prefix' in db.placeholders. A plugin is PHP code with the site's full access; install it only if you trust its code.

To remove the plugin, unregister it, drop its schema with DROP SCHEMA content_schedule CASCADE; and delete its migration record with DELETE FROM migrations WHERE migration LIKE 'content-schedule:%';. Without a schema, drop the table content_schedule_plans instead. Wake-up jobs it left on the queue fail and can be pruned with queue:prune.

Scheduling a type

Add #[Schedule] to the node types editors may plan:

use Cosray\ContentSchedule\Schedule;

#[Label('Article'), Route('/articles/{title}'), Schedule]
final class Article implements Title {}

The attribute takes no arguments. A #[Record] has no publication, so it cannot carry it.

In the editor

A scheduled type's editor offers two entries in the menu beside the publish button: Plan publishing… and, for a node that is online or planned to go online, Plan taking offline…. Each opens a dialog with the time of its plan, worded by the state the plan meets:

  • Online from: for a node that is offline by then, the node goes online with the changes saved by then.
  • Publish changes on: for a node that is online by then, the changes saved by then are published.
  • Offline from: the node goes offline.

The state follows from the node's state now and its other plan: a node that is online, with an earlier plan to go offline, comes back online. Together the two times make a window or, for a node that is online, a pause.

Planning in a dialog plans its action, or moves its pending plan to the new time; Remove plan cancels it. Planned, the editor opens again: the status tab names what the plans will do, like "Planned: goes online on 30.11.2026, 08:00 · goes offline on 05.12.2026, 18:00", and the dialogs name who planned them. A refused time leaves the dialog as it was filled in and says why in a toast. Planning takes effect at once, apart from saving the node, and does not count as an edit of it. Times are entered and shown in the panel's time zone, and a time in the past is refused. A node that is offline has nothing to take offline, so its time to go offline needs an earlier time to go online. The two times may not fall in the same minute, which would leave their order open. Planning needs the permission to change the node's type.

What a plan does

  • Online from, Publish changes on: the node's draft, if it has one, is published with the node, as publishing in the editor does. The draft is taken as it is at that time, so editors can keep writing until then. Edits a user has not saved are not part of it.
  • Offline from: the node is taken offline. A draft becomes its main version, as taking a node offline in the editor does.

A node has at most one pending plan per action. The events a plan writes name the user who planned it, with {"source": "content-schedule", "planned": …} as their cause, which the node's history words as "planned". That user's permission is checked when the plan is made, not when it runs, so the plan of someone who has left still runs.

A plan that finds nothing to do, like bringing online a node that already is and has no draft, is done and says so. A plan that cannot run, because its node is gone, is locked with a draft, or its draft does not validate, fails with the reason and is not tried again; plan it anew once the cause is fixed.

Acting by hand supersedes a plan:

  • Bringing the node online drops its plan to go online. So does publishing the draft of an online node.
  • Taking it offline drops its plan to go offline and keeps the one to go online, which brings it back.
  • Deleting it drops both.
  • Discarding the draft keeps the plan, so a change can be planned before it is written.

The overview

The Schedule page lists the upcoming plans by day, and under Failed and Done what ran in the last 30 days, with filters for the action and the type. An upcoming plan to publish reads Goes online or Changes get published, by the state it meets, as in the editor. It shows the plans of the types a user may change. The plugin adds the page to the content rail in a section of its own, Planning, and a card to the dashboard that counts the plans of the next seven days. A site that places the link itself turns the section off:

'content-schedule.nav' => false,
$content = $app->section('Content');
$content->link('content-schedule:nav', '/panel/content-schedule')->icon('bi:calendar-event');

Running plans on time

Each plan pushes a job to Cosray's job queue, due at the plan's time, on the queue content-schedule. How punctual a plan is depends on what runs the queue:

  • queue:listen as a daemon runs it within moments.
  • queue:work from cron runs it within a minute.
  • Without either, requests run due jobs after their response. A plan then runs with the first request after its time, and that request still gets the old state. On a site with few visitors, or behind a cache, it can run late.

Use cron or the daemon where the time matters, as for an offer that must end. The overview warns when plans are overdue, which means that nothing runs the queue.

From code

Cosray\ContentSchedule\Plans is the service behind the panel. It takes the Context, the Cms and the node Types, as Cosray's own write services do, and checks no permissions:

use Cosray\Actor;
use Cosray\ContentSchedule\Action;
use Cosray\ContentSchedule\Plans;

$plans = new Plans($context, $cms, $types);

$plans->plan($uid, Action::Publish, new DateTimeImmutable('2027-01-01 08:00'), new Actor($userId));
$plans->cancel($uid, Action::Publish);
$plans->of($uid);   // the node's pending plans by action
$plans->run();      // runs what is due, as the job does

Development

The plugin is tested on Cosray's test kit against a database of its own:

createuser content_schedule_test --pwprompt  # password: content_schedule_test
createdb --owner content_schedule_test content_schedule_test
php run db:migrations --namespace=install --apply
php run db:migrations --apply
composer check

tests/app.php is the app the tests and run boot; DATABASE_URL overrides its database. Don't name the database role content_schedule: PostgreSQL's default search path looks in a schema named like the role first, which is the plugin's. php run i18n:status keeps the catalogs in lang/ in step with the code; add keys by hand.

License

MIT, see LICENSE.md.