Files
cf7-spam-to-telegram/includes/class-settings.php
T

80 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Cf7stg;
final class Settings
{
public const OPT_BOT_TOKEN = 'cf7stg_bot_token_enc';
public const OPT_CHAT_ID = 'cf7stg_chat_id';
public const OPT_SITE_TITLE = 'cf7stg_site_title';
public const OPT_LAST_TEST = 'cf7stg_last_test';
public static function get_bot_token(): string
{
if (defined('SPAM2TG_BOT_TOKEN') && SPAM2TG_BOT_TOKEN !== '') {
return (string)SPAM2TG_BOT_TOKEN;
}
$enc = (string)get_option(self::OPT_BOT_TOKEN, '');
$dec = Crypto::decrypt($enc);
return $dec ?? '';
}
public static function is_bot_token_constant(): bool
{
return defined('SPAM2TG_BOT_TOKEN') && SPAM2TG_BOT_TOKEN !== '';
}
public static function set_bot_token(string $plaintext): void
{
update_option(self::OPT_BOT_TOKEN, Crypto::encrypt($plaintext));
}
public static function get_chat_id(): string
{
if (defined('SPAM2TG_CHAT_ID') && SPAM2TG_CHAT_ID !== '') {
return (string)SPAM2TG_CHAT_ID;
}
return (string)get_option(self::OPT_CHAT_ID, '');
}
public static function is_chat_id_constant(): bool
{
return defined('SPAM2TG_CHAT_ID') && SPAM2TG_CHAT_ID !== '';
}
public static function set_chat_id(string $chat_id): void
{
update_option(self::OPT_CHAT_ID, $chat_id);
}
public static function get_site_title(): string
{
$override = trim((string)get_option(self::OPT_SITE_TITLE, ''));
if ($override !== '') return $override;
$host = parse_url(home_url(), PHP_URL_HOST) ?: '';
return DomainFormatter::humanize((string)$host);
}
public static function set_site_title(string $title): void
{
update_option(self::OPT_SITE_TITLE, trim($title));
}
public static function get_last_test(): array
{
$v = get_option(self::OPT_LAST_TEST, []);
return is_array($v) ? $v : [];
}
public static function set_last_test(array $v): void
{
update_option(self::OPT_LAST_TEST, $v);
}
public static function is_configured(): bool
{
return self::get_bot_token() !== '' && self::get_chat_id() !== '';
}
}