fix(cron): self-heal scheduled events on every boot + on plugin upgrade

Pilot on washanyanya.ru lost the cf7stg_dispatch cron event somewhere
between 21.04 and 01.05 — wp_next_scheduled('cf7stg_dispatch') returned
NULL, queue had 6 pending rows, sent-24h=0, failed=0, no error trail.
Settings/token were intact. The most likely cause is the PUC self-update
to 1.2.0: WordPress plugin upgrades replace files but do NOT call
register_activation_hook, so Activator::activate() never re-ran and any
cron event Activator::deactivate() had cleared (or that was lost for any
other reason) stayed missing.

Two-part fix:

1. Activator::ensure_scheduled() — extracted from the old private
   schedule_events(), now public and idempotent. wp_next_scheduled()
   short-circuits when events are already queued, so it's safe to call
   on every request. Plugin::boot() invokes it on plugins_loaded — every
   admin or front-end hit auto-heals lost cron events.

2. Activator::on_upgrade_complete() bound to upgrader_process_complete.
   When WordPress finishes upgrading THIS plugin (matched via
   plugin_basename + $hook_extra['plugins']), we re-run install_table()
   (idempotent dbDelta), seed_silent_drop_options() (uses add_option,
   preserves user values), and ensure_scheduled(). guard_requirements
   is intentionally skipped — wp_die mid-upgrade would leave admin in
   a broken state, and the host obviously meets requirements since the
   old version is currently running.

Tests untouched (this is integration code wrapping WP-only APIs); full
suite still 126/126 green.

Changelog entry for 1.2.1 added to readme.txt.
Stable tag and CF7STG_VERSION will be bumped by release.sh.

Refs: cp-1lg

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Vladimir Bryzgalov
2026-05-01 20:19:34 +05:00
parent ad14d0e6e3
commit f510ac484a
3 changed files with 71 additions and 17 deletions
+4
View File
@@ -24,6 +24,10 @@ License: GPLv2 or later
== Changelog ==
= 1.2.1 =
* Fix: Cron-события `cf7stg_dispatch` / `cf7stg_cleanup` теперь самовосстанавливаются. `Plugin::boot()` на каждой загрузке вызывает `Activator::ensure_scheduled()` — если событие пропало (PUC self-update без `register_activation_hook`, ручной `wp_clear_scheduled_hook`, обрезанный wp_options.cron), оно немедленно пересоздаётся. До фикса очередь могла молча застревать на дни без признаков ошибки (sent-24h=0, failed=0).
* Fix: Подписан хук `upgrader_process_complete` — после plugin update WordPress повторно вызывает `Activator::install_table()`, `seed_silent_drop_options()` и `ensure_scheduled()`. Без guard_requirements (чтобы не убить admin при апгрейде).
= 1.2.0 =
* New: Self-hosted plugin updates from Gitea via Plugin Update Checker v5.6.
WordPress admin shows updates automatically (checked hourly). Source: