Files
wordpress-theme-aksrt/inc/classes/class-aksrt-user-center.php
T

141 lines
4.0 KiB
PHP

<?php
/**
* AKSRT User Center —本人中心协调类
*
* 负责本人中心页面的初始化、面板注册和资源加载协调。
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
class AKSRT_User_Center {
private static $instance = null;
/** @var array 面板注册表 */
private $panels = [];
private function __construct() {}
public static function get_instance(): self {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function init(): void {
$this->register_default_panels();
}
/**
* 注册默认面板
*/
private function register_default_panels(): void {
$settings = class_exists('AKSRT_Settings')
? AKSRT_Settings::get_user_center_settings()
: [
'tabs' => ['profile', 'posts', 'comments', 'favorites'],
'avatar_upload' => true,
'cover_upload' => true,
'social_links' => true,
];
$definitions = [
'profile' => [
'label' => __('个人信息', 'aksrt'),
'icon' => 'user',
'priority' => 10,
'type' => 'panel',
],
'posts' => [
'label' => __('我的文章', 'aksrt'),
'icon' => 'file-alt',
'priority' => 20,
'type' => 'panel',
],
'comments' => [
'label' => __('我的评论', 'aksrt'),
'icon' => 'comment',
'priority' => 30,
'type' => 'panel',
],
'favorites' => [
'label' => __('我的收藏', 'aksrt'),
'icon' => 'star',
'priority' => 40,
'type' => 'panel',
],
'credits' => [
'label' => class_exists('AKSRT_Settings')
? sprintf(__('我的%s', 'aksrt'), AKSRT_Settings::get_currency_name())
: __('我的积分', 'aksrt'),
'icon' => 'wallet',
'priority' => 50,
'type' => 'panel',
],
'notif' => [
'label' => __('消息通知', 'aksrt'),
'icon' => 'bell',
'priority' => 60,
'type' => 'link',
],
'settings' => [
'label' => __('账号设置', 'aksrt'),
'icon' => 'cog',
'priority' => 70,
'type' => 'panel',
],
];
$this->panels = [];
foreach ((array) ($settings['tabs'] ?? []) as $key) {
if ($key === 'credits' && (!class_exists('AKSRT_Settings') || !AKSRT_Settings::credits_enabled())) {
continue;
}
if (isset($definitions[$key])) {
$this->panels[$key] = $definitions[$key];
}
}
if (!isset($this->panels['settings'])) {
$this->panels['settings'] = $definitions['settings'];
}
$this->panels['notif'] = $definitions['notif'];
/**
* 允许插件/子主题注册额外面板
*
* @param array $panels 面板注册表
*/
$this->panels = apply_filters('aksrt_user_center_panels', $this->panels);
}
/**
* 获取面板注册表
*/
public function get_panels(): array {
return $this->panels;
}
/**
* 获取可切换的本地面板(不含外链入口)。
*/
public function get_dashboard_tabs(): array {
return array_filter($this->panels, static function (array $panel): bool {
return ($panel['type'] ?? 'panel') === 'panel';
});
}
/**
* 判断当前页面是否为本人中心
*/
public static function is_page(): bool {
return function_exists('aksrt_is_user_center_page') && aksrt_is_user_center_page();
}
}