Files
cf7-spam-to-telegram/includes/class-activator.php
T
Vladimir Bryzgalov 22239321d0 fix(activator): register cron_schedules filter during activation
Dispatcher::register_hooks() runs on plugins_loaded, which is AFTER
register_activation_hook fires. The custom recurrence 'cf7stg_every_minute'
was therefore unknown to wp_schedule_event during activation, and the
'cf7stg_dispatch' event silently failed to schedule — queue items never
got dispatched.

Pilot caught this on washanyanya.ru (23 items queued, 0 sent, 0 failed —
no cron hook firing at all).

Adding the filter directly inside Activator::schedule_events() makes the
custom schedule available in the same request as the wp_schedule_event
call, regardless of bootstrap order.

After updating the plugin, deactivate + reactivate is required to
re-trigger Activator::activate() so events get (re)scheduled.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-17 23:26:06 +05:00

84 lines
3.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Cf7stg;
final class Activator
{
public const DB_VERSION = '1';
public const DB_VERSION_OPTION = 'cf7stg_db_version';
public static function activate(): void
{
self::guard_requirements();
self::install_table();
self::schedule_events();
}
public static function deactivate(): void
{
wp_clear_scheduled_hook('cf7stg_dispatch');
wp_clear_scheduled_hook('cf7stg_cleanup');
}
private static function guard_requirements(): void
{
if (version_compare(PHP_VERSION, '7.4', '<')) {
deactivate_plugins(plugin_basename(CF7STG_PLUGIN_FILE));
wp_die('Плагин CF7 Spam → Telegram требует PHP 7.4 или выше. Текущая: ' . PHP_VERSION);
}
global $wp_version;
if (version_compare($wp_version, '6.0', '<')) {
deactivate_plugins(plugin_basename(CF7STG_PLUGIN_FILE));
wp_die('Плагин CF7 Spam → Telegram требует WordPress 6.0 или выше.');
}
}
private static function install_table(): void
{
global $wpdb;
$table = $wpdb->prefix . 'cf7stg_queue';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE {$table} (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
flamingo_post_id BIGINT UNSIGNED NULL,
form_id BIGINT UNSIGNED NULL,
source_key VARCHAR(40) NOT NULL DEFAULT 'cf7',
payload_json LONGTEXT NOT NULL,
label VARCHAR(10) NOT NULL DEFAULT 'unclear',
is_retro TINYINT(1) NOT NULL DEFAULT 0,
status VARCHAR(20) NOT NULL DEFAULT 'pending',
attempts SMALLINT UNSIGNED NOT NULL DEFAULT 0,
last_error TEXT NULL,
next_try_at DATETIME NULL,
created_at DATETIME NOT NULL,
sent_at DATETIME NULL,
PRIMARY KEY (id),
KEY idx_status_next (status, next_try_at),
UNIQUE KEY uq_flamingo (flamingo_post_id, is_retro)
) {$charset_collate};";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta($sql);
update_option(self::DB_VERSION_OPTION, self::DB_VERSION);
}
private static function schedule_events(): void
{
// Register the custom 60-second schedule here — Dispatcher::register_hooks()
// runs on plugins_loaded, which is AFTER activation. Without this filter
// being in place during activate(), wp_schedule_event with an unknown
// recurrence ('cf7stg_every_minute') silently fails and the event never
// fires.
add_filter('cron_schedules', ['\\Cf7stg\\Dispatcher', 'register_schedule']);
if (!wp_next_scheduled('cf7stg_dispatch')) {
wp_schedule_event(time() + 60, 'cf7stg_every_minute', 'cf7stg_dispatch');
}
if (!wp_next_scheduled('cf7stg_cleanup')) {
wp_schedule_event(time() + 3600, 'daily', 'cf7stg_cleanup');
}
}
}