Files
Vladimir Bryzgalov f510ac484a 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>
2026-05-01 20:19:34 +05:00

89 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Cf7stg;
final class Plugin
{
private static ?Plugin $instance = null;
public static function instance(): Plugin
{
if (self::$instance === null) self::$instance = new self();
return self::$instance;
}
public static function register_autoloader(): void
{
spl_autoload_register(static function (string $class): void {
if (strpos($class, 'Cf7stg\\') !== 0) return;
$relative = substr($class, strlen('Cf7stg\\'));
$slug = strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1-$2', $relative));
// Class names may use underscores (Settings_Page, Interface_Spam_Source);
// normalize both word-boundary conventions to hyphens.
$slug = str_replace('_', '-', $slug);
$candidates = [
dirname(__DIR__) . '/includes/class-' . $slug . '.php',
dirname(__DIR__) . '/includes/sources/class-' . $slug . '.php',
dirname(__DIR__) . '/admin/class-' . $slug . '.php',
// Fallback: treat slug as full filename (for Interface_* classes
// whose slug already becomes 'interface-xxx').
dirname(__DIR__) . '/includes/' . $slug . '.php',
dirname(__DIR__) . '/includes/sources/' . $slug . '.php',
dirname(__DIR__) . '/admin/' . $slug . '.php',
];
foreach ($candidates as $path) {
if (is_file($path)) {
require_once $path;
return;
}
}
});
}
public function boot(): void
{
// Inject silent-drop options into Classifier filter chain
Settings::register_classifier_filters();
// Register runtime hooks
Dispatcher::register_hooks();
(new CF7Source())->register();
// Self-heal cron events on every boot. Recovers from PUC self-update
// (which doesn't run register_activation_hook) and any other cause of
// cron event loss. Idempotent — wp_next_scheduled() short-circuits
// when events are already queued.
Activator::ensure_scheduled();
// Re-run install steps after a plugin upgrade (PUC or wp-admin upload).
add_action('upgrader_process_complete', ['\\Cf7stg\\Activator', 'on_upgrade_complete'], 10, 2);
self::register_update_checker();
// Admin
if (is_admin()) {
(new Settings_Page())->register();
(new Dashboard_Widget())->register();
}
}
private static function register_update_checker(): void
{
$loader = CF7STG_PLUGIN_DIR . 'includes/lib/plugin-update-checker/plugin-update-checker.php';
if (!is_file($loader)) {
return;
}
require_once $loader;
if (!class_exists('YahnisElsts\\PluginUpdateChecker\\v5\\PucFactory')) {
return;
}
\YahnisElsts\PluginUpdateChecker\v5\PucFactory::buildUpdateChecker(
'https://git.netranking.ru/bryzgalov/cf7-spam-to-telegram/raw/branch/main/wp-plugin-info.json',
CF7STG_PLUGIN_FILE,
'cf7-spam-to-telegram'
);
}
}