dbdfc9afa9
- Dashboard widget is now registered unconditionally. Header + content adapt: shows '⚠️ ошибки доставки' only when failed > 0, otherwise plain title with queue state (pending / sent-24h / failed). Pilot feedback: the conditional widget felt like the plugin was missing — users expect a persistent status readout. - Retro default window changed from 7 days to 0 (no limit). Most pilot uses were 'dump everything since install', so 0 is the more useful default. User can still narrow to N days in the form. - Spec §10 + §14 updated. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
108 lines
3.6 KiB
PHP
108 lines
3.6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Cf7stg;
|
|
|
|
final class Settings_Page
|
|
{
|
|
private const PAGE_SLUG = 'cf7stg-settings';
|
|
private const NONCE = 'cf7stg_settings_nonce';
|
|
|
|
public function register(): void
|
|
{
|
|
add_action('admin_menu', [$this, 'add_menu']);
|
|
add_action('admin_post_cf7stg_save', [$this, 'handle_save']);
|
|
add_action('admin_post_cf7stg_test', [$this, 'handle_test']);
|
|
add_action('admin_post_cf7stg_retro', [$this, 'handle_retro']);
|
|
add_action('admin_post_cf7stg_retry_failed', [$this, 'handle_retry_failed']);
|
|
add_action('admin_post_cf7stg_purge_sent', [$this, 'handle_purge_sent']);
|
|
}
|
|
|
|
public function add_menu(): void
|
|
{
|
|
add_options_page(
|
|
'CF7 Spam → Telegram',
|
|
'CF7 Spam → TG',
|
|
'manage_options',
|
|
self::PAGE_SLUG,
|
|
[$this, 'render']
|
|
);
|
|
}
|
|
|
|
public function render(): void
|
|
{
|
|
if (!current_user_can('manage_options')) return;
|
|
$page_slug = self::PAGE_SLUG;
|
|
$nonce = self::NONCE;
|
|
include CF7STG_PLUGIN_DIR . 'admin/views/settings.php';
|
|
}
|
|
|
|
public function handle_save(): void
|
|
{
|
|
$this->guard('cf7stg_save');
|
|
if (!Settings::is_bot_token_constant()) {
|
|
$token = (string)($_POST['bot_token'] ?? '');
|
|
if ($token !== '') Settings::set_bot_token($token);
|
|
}
|
|
if (!Settings::is_chat_id_constant()) {
|
|
Settings::set_chat_id(sanitize_text_field((string)($_POST['chat_id'] ?? '')));
|
|
}
|
|
Settings::set_site_title(sanitize_text_field((string)($_POST['site_title'] ?? '')));
|
|
$this->redirect_back(['saved' => 1]);
|
|
}
|
|
|
|
public function handle_test(): void
|
|
{
|
|
$this->guard('cf7stg_test');
|
|
try {
|
|
TelegramClient::send_message(
|
|
Settings::get_chat_id(),
|
|
'✅ Тестовое сообщение из плагина CF7 Spam → Telegram ('
|
|
. esc_html(Settings::get_site_title()) . ')'
|
|
);
|
|
Settings::set_last_test(['ok' => true, 'at' => current_time('mysql'), 'err' => '']);
|
|
$this->redirect_back(['test' => 'ok']);
|
|
} catch (TelegramException $e) {
|
|
Settings::set_last_test(['ok' => false, 'at' => current_time('mysql'), 'err' => $e->getMessage()]);
|
|
$this->redirect_back(['test' => 'err']);
|
|
}
|
|
}
|
|
|
|
public function handle_retro(): void
|
|
{
|
|
$this->guard('cf7stg_retro');
|
|
$count = max(1, min(RetroImporter::MAX_COUNT, (int)($_POST['retro_count'] ?? 10)));
|
|
$days = max(0, (int)($_POST['retro_days'] ?? 0));
|
|
$repeat = !empty($_POST['retro_repeat']);
|
|
$result = RetroImporter::import($count, $days, $repeat);
|
|
$this->redirect_back(['retro_added' => (int)$result['added'], 'retro_skipped' => (int)$result['skipped']]);
|
|
}
|
|
|
|
public function handle_retry_failed(): void
|
|
{
|
|
$this->guard('cf7stg_retry_failed');
|
|
$n = Queue::retry_all_failed();
|
|
$this->redirect_back(['retried' => $n]);
|
|
}
|
|
|
|
public function handle_purge_sent(): void
|
|
{
|
|
$this->guard('cf7stg_purge_sent');
|
|
$n = Queue::purge_sent();
|
|
$this->redirect_back(['purged' => $n]);
|
|
}
|
|
|
|
private function guard(string $action): void
|
|
{
|
|
if (!current_user_can('manage_options')) wp_die('forbidden');
|
|
check_admin_referer($action, self::NONCE);
|
|
}
|
|
|
|
private function redirect_back(array $extra = []): void
|
|
{
|
|
$url = add_query_arg(array_merge(['page' => self::PAGE_SLUG], $extra), admin_url('options-general.php'));
|
|
wp_safe_redirect($url);
|
|
exit;
|
|
}
|
|
}
|