245 lines
9.5 KiB
PHP
245 lines
9.5 KiB
PHP
<?php
|
||
/**
|
||
* @package AKSRT
|
||
*/
|
||
|
||
namespace AKSRT\Settings;
|
||
|
||
defined('ABSPATH') || exit;
|
||
|
||
trait AuthReader {
|
||
|
||
/** 是否要求注册用户才能评论 */
|
||
public static function comment_requires_registration(): bool {
|
||
$user_id = get_current_user_id();
|
||
return !class_exists(Capabilities::class) || !Capabilities::user_can('comment_add', $user_id);
|
||
}
|
||
|
||
/** 是否允许前台注册。主题设置是唯一真值。 */
|
||
public static function registration_enabled(): bool {
|
||
return !self::close_signup();
|
||
}
|
||
|
||
/** 主题内置默认图片 URL */
|
||
private static function default_asset_url(string $filename): string {
|
||
return get_template_directory_uri() . '/assets/img/' . $filename;
|
||
}
|
||
|
||
/** 获取 Logo URL(留空时使用内置默认) */
|
||
public static function logo_url(): string {
|
||
$url = trim((string) self::get_value('site_logo', ''));
|
||
return $url !== '' ? $url : self::default_asset_url('default-logo.svg');
|
||
}
|
||
|
||
/** 获取暗色模式 Logo URL(留空时跟随浅色模式) */
|
||
public static function logo_dark_url(): string {
|
||
$url = trim((string) self::get_value('site_logo_dark', ''));
|
||
return $url !== '' ? $url : self::logo_url();
|
||
}
|
||
|
||
/** 获取站点品牌文字 */
|
||
public static function logo_text(): string {
|
||
$text = trim((string) self::get_value('logo_text', ''));
|
||
return $text !== '' ? $text : get_bloginfo('name');
|
||
}
|
||
|
||
// ---- 注册登录 auth 段 getter ----
|
||
|
||
/** 一键禁用登录注册总开关(默认 false;含社交登录,不影响后台) */
|
||
public static function close_sign(): bool {
|
||
return (bool) self::get_value('close_sign', false);
|
||
}
|
||
|
||
/** 是否关闭注册。禁用账密登录时同时关闭账密注册。 */
|
||
public static function close_signup(): bool {
|
||
if (self::close_sign()) return true;
|
||
return (bool) self::get_value('close_signup', false) || self::close_signin();
|
||
}
|
||
|
||
/** 是否关闭账密登录(仅允许社交登录) */
|
||
public static function close_signin(): bool {
|
||
if (self::close_sign()) return true;
|
||
return (bool) self::get_value('close_signin', false);
|
||
}
|
||
|
||
/** 登录注册样式: modal | page。 */
|
||
public static function user_sign_type(): string {
|
||
$type = (string) self::get_value('user_sign_type', 'modal');
|
||
if (!in_array($type, ['modal', 'page'], true)) $type = 'modal';
|
||
return $type;
|
||
}
|
||
|
||
/** 应急登录 Key(空=禁用;管理员通过 ?aksrt_bypass=<key> 绕过 wp-login 替换) */
|
||
public static function admin_sign_key(): string {
|
||
return trim((string) self::get_value('admin_sign_key', ''));
|
||
}
|
||
|
||
/** Whether wp-login.php is replaced by /sign/. */
|
||
public static function replace_wp_login(): bool {
|
||
return (bool) self::get_value('replace_wp_login', true);
|
||
}
|
||
|
||
/** 登录页 UI 配置数组 */
|
||
public static function sign_page_ui(): array {
|
||
$theme_mode = (string) self::get_value('sign_theme_mode', 'no');
|
||
if (!in_array($theme_mode, ['no', 'white-theme', 'dark-theme'], true)) {
|
||
$theme_mode = 'no';
|
||
}
|
||
return [
|
||
'background' => self::auth_gallery_ids('sign_page_background'),
|
||
'theme_mode' => $theme_mode,
|
||
'show_header' => (bool) self::get_value('sign_show_header', true),
|
||
'card_position' => in_array((string) self::get_value('sign_card_position', 'right'), ['left', 'center', 'right'], true)
|
||
? (string) self::get_value('sign_card_position', 'right') : 'right',
|
||
'show_logo' => (bool) self::get_value('sign_show_logo', false),
|
||
'footer' => (string) self::get_value('sign_page_footer', ''),
|
||
];
|
||
}
|
||
|
||
/** 弹窗 UI 配置数组 */
|
||
public static function modal_ui(): array {
|
||
return [
|
||
'background' => self::auth_gallery_ids('sign_modal_background'),
|
||
'show_logo' => (bool) self::get_value('sign_modal_show_logo', false),
|
||
];
|
||
}
|
||
|
||
/** 认证图片库的唯一存储格式:正整数附件 ID 数组。 */
|
||
private static function auth_gallery_ids(string $key): array {
|
||
$value = self::get_value($key, []);
|
||
if (!is_array($value)) {
|
||
return [];
|
||
}
|
||
|
||
$ids = array_values(array_filter(array_map('absint', $value)));
|
||
return array_values(array_unique($ids));
|
||
}
|
||
|
||
/** 登录框日间 LOGO URL(空时回退全局 logo_url) */
|
||
public static function user_logo_url(): string {
|
||
$ids = self::auth_gallery_ids('user_logo');
|
||
if ($ids) {
|
||
$url = wp_get_attachment_image_url($ids[0], 'full');
|
||
if ($url) return $url;
|
||
}
|
||
return self::logo_url();
|
||
}
|
||
|
||
/** 登录框夜间 LOGO URL(空时回退全局 logo_dark_url) */
|
||
public static function user_logo_dark_url(): string {
|
||
$ids = self::auth_gallery_ids('user_logo_dark');
|
||
if ($ids) {
|
||
$url = wp_get_attachment_image_url($ids[0], 'full');
|
||
if ($url) return $url;
|
||
}
|
||
return self::logo_dark_url();
|
||
}
|
||
|
||
/** 是否允许手机号登录 */
|
||
public static function signin_phone_enabled(): bool {
|
||
return (bool) self::get_value('user_signin_phone_s', false);
|
||
}
|
||
|
||
/** 是否启用验证码免密登录。 */
|
||
public static function passwordless_login_enabled(): bool {
|
||
return (bool) self::get_value('user_signin_nopas_s', false);
|
||
}
|
||
|
||
/** 免密登录验证渠道。 */
|
||
public static function passwordless_login_type(): string {
|
||
$type = (string) self::get_value('user_signin_nopas_type', 'email');
|
||
return in_array($type, ['email', 'phone', 'email_phone'], true) ? $type : 'email';
|
||
}
|
||
|
||
/** 免密登录显示模式 */
|
||
public static function nopas_active_mode(): string {
|
||
$mode = (string) self::get_value('user_signin_nopas_active', 'nopas');
|
||
return in_array($mode, ['nopas', 'pas', 'only_nopas'], true) ? $mode : 'nopas';
|
||
}
|
||
|
||
/** 是否在注册前验证邮箱或手机号。 */
|
||
public static function signup_verification_enabled(): bool {
|
||
return (bool) self::get_value('user_signup_captch', false);
|
||
}
|
||
|
||
/** 注册验证渠道。 */
|
||
public static function signup_verification_type(): string {
|
||
$type = (string) self::get_value('captch_type', 'email');
|
||
return in_array($type, ['email', 'phone', 'email_phone'], true) ? $type : 'email';
|
||
}
|
||
|
||
/** 找回密码验证渠道。 */
|
||
public static function reset_password_verification_type(): string {
|
||
$type = (string) self::get_value('user_repas_captch_type', 'email');
|
||
return in_array($type, ['email', 'phone', 'email_phone'], true) ? $type : 'email';
|
||
}
|
||
|
||
/** 注册时是否省略重复密码。 */
|
||
public static function signup_omit_password_confirmation(): bool {
|
||
return (bool) self::get_value('user_signup_no_repas', true);
|
||
}
|
||
|
||
/** 推广码在注册表单中的唯一显示模式。 */
|
||
public static function invitation_mode(): string {
|
||
$mode = (string) self::get_value('invit_code_s', 'close');
|
||
return in_array($mode, ['close', 'open', 'must'], true) ? $mode : 'close';
|
||
}
|
||
|
||
/** 自动弹窗配置。 */
|
||
public static function sign_modal_auto(): array {
|
||
return [
|
||
'enabled' => self::user_sign_type() === 'modal' && (bool) self::get_value('sign_modal_auto_s', false),
|
||
'waiting' => max(1, min(100, (int) self::get_value('sign_modal_auto_waiting_time', 5))),
|
||
'expires' => max(0, min(2000, (float) self::get_value('sign_modal_auto_expires', 1))),
|
||
];
|
||
}
|
||
|
||
/** 绑定提醒类型 */
|
||
public static function bind_reminder_type(): string {
|
||
$val = self::get_value('bind_reminder', 'close');
|
||
$val = (string) $val;
|
||
return in_array($val, ['email', 'phone', 'email_phone', 'close'], true) ? $val : 'close';
|
||
}
|
||
|
||
/** 强制绑定类型 */
|
||
public static function mandatory_bind_type(): string {
|
||
$val = self::get_value('mandatory_bind', 'close');
|
||
$val = (string) $val;
|
||
return in_array($val, ['email', 'phone', 'email_phone', 'close'], true) ? $val : 'close';
|
||
}
|
||
|
||
/** 昵称黑名单关键词数组 */
|
||
public static function nickname_blacklist(): array {
|
||
$raw = (string) self::get_value('user_nickname_out', '');
|
||
if ($raw === '') return [];
|
||
// 支持逗号分隔和换行分隔
|
||
$items = preg_split('/[,,\r\n]+/', $raw);
|
||
return array_values(array_filter(array_map('trim', $items), 'strlen'));
|
||
}
|
||
|
||
/** 邮箱限制配置:['type' => 'not'|'allow', 'domains' => [...]] */
|
||
public static function email_limit(): array {
|
||
$type = (string) self::get_value('user_email_limit_type', 'not');
|
||
if (!in_array($type, ['not', 'allow'], true)) $type = 'not';
|
||
$raw = (string) self::get_value('user_email_out', '');
|
||
$domains = [];
|
||
if ($raw !== '') {
|
||
$items = preg_split('/[\r\n]+/', $raw);
|
||
$domains = array_values(array_filter(array_map('trim', $items), 'strlen'));
|
||
}
|
||
return ['type' => $type, 'domains' => $domains];
|
||
}
|
||
|
||
|
||
/** 用户协议及隐私协议前台配置。 */
|
||
public static function user_agreement_settings(): array {
|
||
return [
|
||
'enabled' => (bool) self::get_value('user_agreement_s', false),
|
||
'privacy' => (bool) self::get_value('user_privacy_s', false),
|
||
'required' => (bool) self::get_value('user_agreement_check', false),
|
||
'page' => trim((string) self::get_value('user_agreement_page', '')),
|
||
'privacy_page'=> trim((string) self::get_value('user_privacy_page', '')),
|
||
];
|
||
}
|
||
}
|