152 lines
5.4 KiB
PHP
152 lines
5.4 KiB
PHP
<?php
|
|
/**
|
|
* AKSRT Site Notice — 全站弹窗公告
|
|
*
|
|
* Configurable popup notice with cookie-based frequency control.
|
|
* Reads system_notice_* keys from AKSRT_Settings (schema-driven).
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class AKSRT_Site_Notice {
|
|
|
|
private static $instance = null;
|
|
|
|
private function __construct() {}
|
|
|
|
public static function get_instance(): self {
|
|
if (null === self::$instance) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public function init(): void {
|
|
add_action('wp_footer', [$this, 'render_notice']);
|
|
}
|
|
|
|
/**
|
|
* Check if site notice is enabled, has content, not expired,
|
|
* and current user matches the target audience (policy).
|
|
*/
|
|
private function is_active(): bool {
|
|
if (!class_exists('AKSRT_Settings')) return false;
|
|
|
|
$cfg = AKSRT_Settings::get_system_notice_config();
|
|
if (!$cfg['enabled'] || $cfg['content'] === '') {
|
|
return false;
|
|
}
|
|
|
|
// 身份策略检查
|
|
$policy = $cfg['policy'];
|
|
if ($policy !== '') {
|
|
$user_id = get_current_user_id();
|
|
|
|
switch ($policy) {
|
|
case 'signin':
|
|
if ($user_id <= 0) return false;
|
|
break;
|
|
|
|
case 'vip':
|
|
if (!function_exists('aksrt_is_vip') || !aksrt_is_vip($user_id)) return false;
|
|
break;
|
|
|
|
case 'free':
|
|
if (function_exists('aksrt_is_vip') && aksrt_is_vip($user_id)) return false;
|
|
break;
|
|
|
|
case 'auth':
|
|
if ($user_id <= 0) return false;
|
|
$verified = get_user_meta($user_id, 'email_verified', true);
|
|
if (!$verified) {
|
|
$verified = get_user_meta($user_id, 'phone_verified', true);
|
|
}
|
|
if (!$verified) return false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 过期检查:expires 为小时数,0=永不过期
|
|
$expires_hours = (int) $cfg['expires'];
|
|
if ($expires_hours > 0 && isset($_COOKIE['aksrt_notice_seen'])) {
|
|
$seen_ts = absint($_COOKIE['aksrt_notice_seen']);
|
|
if ($seen_ts > 0 && (time() - $seen_ts) < ($expires_hours * 3600)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Render the notice popup in footer
|
|
*/
|
|
public function render_notice(): void {
|
|
if (!$this->is_active()) return;
|
|
|
|
$cfg = AKSRT_Settings::get_system_notice_config();
|
|
$content = wp_kses_post($cfg['content']);
|
|
$title = esc_html($cfg['title'] ?: __('站点公告', 'aksrt'));
|
|
$extra_cls = $cfg['class'] ? ' ' . esc_attr($cfg['class']) : '';
|
|
$radius = absint($cfg['radius']);
|
|
$size_cls = $cfg['size'];
|
|
|
|
// 标题图标 / 样式
|
|
$title_icon = $cfg['title_icon'];
|
|
$title_cls = $cfg['title_class'] ? ' ' . esc_attr($cfg['title_class']) : '';
|
|
$title_style = $cfg['title_style'] ? ' style="' . esc_attr($cfg['title_style']) . '"' : '';
|
|
|
|
// 弹窗尺寸 class
|
|
$dialog_cls = 'aksrt-site-notice__dialog';
|
|
if ($size_cls !== '') {
|
|
$dialog_cls .= ' ' . esc_attr($size_cls);
|
|
}
|
|
|
|
// 内联样式:圆角
|
|
$dialog_style = 'display:none;';
|
|
if ($radius > 0) {
|
|
$dialog_style .= 'border-radius:' . $radius . 'px;';
|
|
}
|
|
|
|
$buttons = $cfg['buttons'];
|
|
?>
|
|
<div id="aksrt-site-notice"
|
|
class="aksrt-site-notice<?php echo $extra_cls; ?>"
|
|
data-expires="<?php echo esc_attr($cfg['expires']); ?>"
|
|
style="<?php echo esc_attr($dialog_style); ?>">
|
|
<div class="aksrt-site-notice__overlay"></div>
|
|
<div class="<?php echo $dialog_cls; ?>">
|
|
<div class="aksrt-site-notice__head">
|
|
<h3 class="aksrt-site-notice__title<?php echo $title_cls; ?>"<?php echo $title_style; ?>>
|
|
<?php if ($title_icon): ?><i class="<?php echo esc_attr($title_icon); ?>"></i> <?php endif; ?>
|
|
<?php echo $title; ?>
|
|
</h3>
|
|
<button type="button" class="aksrt-site-notice__close" aria-label="<?php esc_attr_e('关闭', 'aksrt'); ?>">
|
|
<i class="fas fa-times"></i>
|
|
</button>
|
|
</div>
|
|
<div class="aksrt-site-notice__body">
|
|
<?php echo $content; ?>
|
|
</div>
|
|
<?php if (!empty($buttons)): ?>
|
|
<div class="aksrt-site-notice__foot">
|
|
<?php foreach ($buttons as $btn): ?>
|
|
<?php
|
|
$btn_cls = 'aksrt-site-notice__btn aksrt-site-notice__btn--' . esc_attr($btn['color']);
|
|
if ($btn['link']):
|
|
?>
|
|
<a href="<?php echo esc_url($btn['link']); ?>" class="<?php echo $btn_cls; ?>"><?php echo esc_html($btn['text']); ?></a>
|
|
<?php else: ?>
|
|
<button type="button" class="<?php echo $btn_cls; ?>" data-dismiss="notice"><?php echo esc_html($btn['text']); ?></button>
|
|
<?php endif; ?>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
}
|
|
}
|