Extend badge dictionary with optional icon_url, prefer aksrt_wear_badge when assigned, and render medal images in identity chips.
306 lines
13 KiB
PHP
306 lines
13 KiB
PHP
<?php
|
|
/**
|
|
* Settings user runtime helper.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class AKSRT_Settings_User_Runtime {
|
|
|
|
/**
|
|
* Public user-home display settings.
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function get_user_home_settings(): array {
|
|
$settings = AKSRT_Settings::get_all();
|
|
$allowed_tabs = ['posts', 'comments', 'favorites', 'credits', 'product', 'plate', 'forum', 'follow'];
|
|
|
|
$parse_csv = function (string $key, string $fallback) use ($settings, $allowed_tabs): array {
|
|
$raw = $settings[$key] ?? $fallback;
|
|
$items = is_array($raw) ? $raw : preg_split('/[\s,]+/', (string) $raw, -1, PREG_SPLIT_NO_EMPTY);
|
|
$clean = [];
|
|
foreach ((array) $items as $v) {
|
|
$v = sanitize_key((string) $v);
|
|
if (in_array($v, $allowed_tabs, true) && !in_array($v, $clean, true)) {
|
|
$clean[] = $v;
|
|
}
|
|
}
|
|
return $clean;
|
|
};
|
|
|
|
// Zibll author_main_tab_opt defaults: post, favorite, comment, plate, forum, follow.
|
|
$normalized_tabs = $parse_csv('user_home_tabs', 'posts,favorites,comments,follow');
|
|
if (empty($normalized_tabs)) {
|
|
$normalized_tabs = ['posts', 'favorites', 'comments', 'follow'];
|
|
}
|
|
|
|
$raw_order = $parse_csv('user_home_tabs_order', '');
|
|
$ordered = [];
|
|
foreach ($raw_order as $tab) {
|
|
if (in_array($tab, $normalized_tabs, true)) {
|
|
$ordered[] = $tab;
|
|
}
|
|
}
|
|
foreach ($normalized_tabs as $tab) {
|
|
if (!in_array($tab, $ordered, true)) {
|
|
$ordered[] = $tab;
|
|
}
|
|
}
|
|
|
|
$pc_hide = $parse_csv('user_home_tabs_pc_hide', '');
|
|
$mobile_hide = $parse_csv('user_home_tabs_mobile_hide', '');
|
|
|
|
return [
|
|
'tabs' => $normalized_tabs,
|
|
'tabs_order' => $ordered,
|
|
'tabs_pc_hide' => $pc_hide,
|
|
'tabs_mobile_hide' => $mobile_hide,
|
|
// Public author page is full-width (Zibll author.php). Sidebar option kept as no-op reserved key.
|
|
'sidebar' => false,
|
|
'cover' => !isset($settings['user_home_cover']) || !empty($settings['user_home_cover']),
|
|
'follow' => !isset($settings['user_home_follow']) || !empty($settings['user_home_follow']),
|
|
'posts_per_page' => max(5, min(50, (int) ($settings['user_home_posts_per_page'] ?? 10))),
|
|
'avatar' => !isset($settings['user_home_avatar']) || !empty($settings['user_home_avatar']),
|
|
'name' => !isset($settings['user_home_name']) || !empty($settings['user_home_name']),
|
|
'desc' => !isset($settings['user_home_desc']) || !empty($settings['user_home_desc']),
|
|
// Body stats row removed; counts live in tabs like Zibll.
|
|
'stats' => false,
|
|
'social' => !isset($settings['user_home_social']) || !empty($settings['user_home_social']),
|
|
'tab_swiper' => !empty($settings['author_main_tab_swiper']),
|
|
'message' => !isset($settings['user_home_message']) || !empty($settings['user_home_message']),
|
|
'private' => !isset($settings['user_home_private']) || !empty($settings['user_home_private']),
|
|
'paging_ajax' => !isset($settings['user_home_paging_ajax']) || !empty($settings['user_home_paging_ajax']),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Private user-center display settings.
|
|
*
|
|
* @return array{tabs:string[],avatar_upload:bool,cover_upload:bool,social_links:bool}
|
|
*/
|
|
public static function get_user_center_settings(): array {
|
|
$settings = AKSRT_Settings::get_all();
|
|
$raw_tabs = $settings['user_center_tabs'] ?? 'profile,posts,comments,credits,favorites';
|
|
$tabs = is_array($raw_tabs)
|
|
? $raw_tabs
|
|
: preg_split('/[\s,]+/', (string) $raw_tabs, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
|
$allowed_tabs = ['profile', 'posts', 'comments', 'favorites', 'credits'];
|
|
$normalized_tabs = ['profile'];
|
|
|
|
foreach ((array) $tabs as $tab) {
|
|
$tab = sanitize_key((string) $tab);
|
|
if ($tab === 'profile') {
|
|
continue;
|
|
}
|
|
|
|
if (in_array($tab, $allowed_tabs, true) && !in_array($tab, $normalized_tabs, true)) {
|
|
$normalized_tabs[] = $tab;
|
|
}
|
|
}
|
|
|
|
if (!in_array('posts', $normalized_tabs, true) && !in_array('comments', $normalized_tabs, true)) {
|
|
$normalized_tabs[] = 'posts';
|
|
}
|
|
|
|
return [
|
|
'tabs' => $normalized_tabs,
|
|
'avatar_upload' => !isset($settings['user_avatar_upload']) || !empty($settings['user_avatar_upload']),
|
|
'cover_upload' => !isset($settings['user_cover_upload']) || !empty($settings['user_cover_upload']),
|
|
'social_links' => !isset($settings['user_social_links']) || !empty($settings['user_social_links']),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* User identity, level, badge, and report governance settings.
|
|
*
|
|
* @return array{
|
|
* verify:array{enabled:bool,public_display:bool,badge_text:string,accept_email_verified:bool,require_manual_review:bool},
|
|
* level:array{enabled:bool,public_display:bool,source:string,label_prefix:string,thresholds:array<int,int>},
|
|
* badge:array{enabled:bool,public_display:bool,limit:int,dictionary:array<int,array{slug:string,label:string,tone:string,icon_url?:string}>},
|
|
* report:array{enabled:bool,public_entry:bool,public_notice:bool,ban_notice_text:string,categories:array<int,array{slug:string,label:string}>}
|
|
* }
|
|
*/
|
|
public static function get_user_identity_settings(): array {
|
|
$settings = AKSRT_Settings::get_all();
|
|
$verify_badge_text = trim((string) ($settings['user_verify_badge_text'] ?? ''));
|
|
$level_prefix = trim((string) ($settings['user_level_prefix'] ?? ''));
|
|
$ban_notice_text = trim((string) ($settings['user_report_ban_notice_text'] ?? ''));
|
|
|
|
return [
|
|
'verify' => [
|
|
'enabled' => !isset($settings['user_verify_enabled']) || !empty($settings['user_verify_enabled']),
|
|
'public_display' => !isset($settings['user_verify_public_display']) || !empty($settings['user_verify_public_display']),
|
|
'badge_text' => $verify_badge_text !== '' ? $verify_badge_text : __('已认证', 'aksrt'),
|
|
'accept_email_verified' => !isset($settings['user_verify_accept_email_verified']) || !empty($settings['user_verify_accept_email_verified']),
|
|
'require_manual_review' => !empty($settings['user_verify_require_manual_review']),
|
|
],
|
|
'level' => [
|
|
'enabled' => !empty($settings['user_level_enabled']),
|
|
'public_display' => !isset($settings['user_level_public_display']) || !empty($settings['user_level_public_display']),
|
|
'source' => (function() use ($settings) {
|
|
$v = $settings['user_level_source'] ?? 'credits_balance';
|
|
return in_array($v, ['credits_balance', 'manual_meta'], true) ? $v : 'credits_balance';
|
|
})(),
|
|
'label_prefix' => $level_prefix !== '' ? $level_prefix : 'Lv.',
|
|
'thresholds' => self::parse_level_thresholds((string) ($settings['user_level_thresholds'] ?? "1|0\n2|30\n3|100\n4|300")),
|
|
],
|
|
'badge' => [
|
|
'enabled' => !empty($settings['user_badge_enabled']),
|
|
'public_display' => !isset($settings['user_badge_public_display']) || !empty($settings['user_badge_public_display']),
|
|
'limit' => max(1, min(8, (int) ($settings['user_badge_limit'] ?? 3))),
|
|
'dictionary' => self::parse_badge_dictionary((string) ($settings['user_badge_dictionary'] ?? "founder|创始成员|warning\nstaff|站务团队|info")),
|
|
],
|
|
'report' => [
|
|
'enabled' => !empty($settings['user_report_enabled']),
|
|
'public_entry' => !isset($settings['user_report_public_entry']) || !empty($settings['user_report_public_entry']),
|
|
'public_notice' => !isset($settings['user_report_public_notice']) || !empty($settings['user_report_public_notice']),
|
|
'ban_notice_text' => $ban_notice_text !== '' ? $ban_notice_text : __('当前账号处于限制状态,部分互动能力已关闭。', 'aksrt'),
|
|
'categories' => self::parse_report_categories((string) ($settings['user_report_categories'] ?? "spam|垃圾信息\nabuse|人身攻击\ncopyright|版权投诉")),
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 禁封设置
|
|
*/
|
|
public static function ban_settings(): array {
|
|
$settings = AKSRT_Settings::get_all();
|
|
$reasons_raw = (string) ($settings['ban_preset_reason'] ?? "spam|垃圾信息\nabuse|人身攻击\nviolation|违规操作");
|
|
$reasons = [];
|
|
foreach (preg_split('/\r\n|\r|\n/', $reasons_raw) as $line) {
|
|
$line = trim($line);
|
|
if ($line === '') {
|
|
continue;
|
|
}
|
|
$parts = explode('|', $line, 2);
|
|
$reasons[] = [
|
|
'slug' => sanitize_key($parts[0]),
|
|
'name' => sanitize_text_field($parts[1] ?? $parts[0]),
|
|
];
|
|
}
|
|
$appeal_keys_raw = (string) ($settings['ban_appeal_keys'] ?? '');
|
|
$appeal_keys = array_filter(array_map('trim', explode(',', $appeal_keys_raw)));
|
|
|
|
return [
|
|
'reasons' => $reasons,
|
|
'appeal_desc' => wp_kses_post((string) ($settings['ban_appeal_desc'] ?? '')),
|
|
'appeal_keys' => array_values($appeal_keys),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 举报设置
|
|
*/
|
|
public static function report_settings(): array {
|
|
$settings = AKSRT_Settings::get_all();
|
|
return [
|
|
'enabled' => !empty($settings['user_report_enabled']),
|
|
'public_entry' => !empty($settings['user_report_public_entry']),
|
|
'public_notice' => !empty($settings['user_report_public_notice']),
|
|
'ban_notice_text' => sanitize_text_field((string) ($settings['user_report_ban_notice_text'] ?? '')),
|
|
'img_enabled' => !empty($settings['user_report_img_s']),
|
|
'desc' => wp_kses_post((string) ($settings['user_report_desc'] ?? '')),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<int,int>
|
|
*/
|
|
public static function parse_level_thresholds(string $raw): array {
|
|
$lines = preg_split('/[\r\n]+/', $raw, -1, PREG_SPLIT_NO_EMPTY);
|
|
$thresholds = [];
|
|
|
|
foreach ((array) $lines as $line) {
|
|
$parts = preg_split('/[|:,]/', trim($line), 2);
|
|
if (!is_array($parts) || count($parts) < 2) {
|
|
continue;
|
|
}
|
|
|
|
$level = (int) trim((string) $parts[0]);
|
|
$value = (int) trim((string) $parts[1]);
|
|
if ($level <= 0 || $value < 0) {
|
|
continue;
|
|
}
|
|
|
|
$thresholds[$level] = $value;
|
|
}
|
|
|
|
if (empty($thresholds)) {
|
|
$thresholds = [1 => 0, 2 => 30, 3 => 100, 4 => 300];
|
|
}
|
|
|
|
ksort($thresholds, SORT_NUMERIC);
|
|
return $thresholds;
|
|
}
|
|
|
|
/**
|
|
* @return array<int,array{slug:string,label:string,tone:string,icon_url:string}>
|
|
*/
|
|
public static function parse_badge_dictionary(string $raw): array {
|
|
$lines = preg_split('/[\r\n]+/', $raw, -1, PREG_SPLIT_NO_EMPTY);
|
|
$allowed_tones = ['neutral', 'info', 'success', 'warning', 'brand'];
|
|
$items = [];
|
|
|
|
foreach ((array) $lines as $line) {
|
|
$parts = array_map('trim', explode('|', (string) $line));
|
|
if (count($parts) < 2) {
|
|
continue;
|
|
}
|
|
|
|
$slug = sanitize_key((string) $parts[0]);
|
|
$label = sanitize_text_field((string) $parts[1]);
|
|
$tone = isset($parts[2]) ? sanitize_key((string) $parts[2]) : 'neutral';
|
|
$icon_url = isset($parts[3]) ? esc_url_raw((string) $parts[3]) : '';
|
|
|
|
if ($slug === '' || $label === '') {
|
|
continue;
|
|
}
|
|
|
|
if (!in_array($tone, $allowed_tones, true)) {
|
|
$tone = 'neutral';
|
|
}
|
|
|
|
$items[] = [
|
|
'slug' => $slug,
|
|
'label' => $label,
|
|
'tone' => $tone,
|
|
'icon_url' => $icon_url,
|
|
];
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
|
|
/**
|
|
* @return array<int,array{slug:string,label:string}>
|
|
*/
|
|
public static function parse_report_categories(string $raw): array {
|
|
$lines = preg_split('/[\r\n]+/', $raw, -1, PREG_SPLIT_NO_EMPTY);
|
|
$items = [];
|
|
|
|
foreach ((array) $lines as $line) {
|
|
$parts = array_map('trim', explode('|', (string) $line));
|
|
if (count($parts) < 2) {
|
|
continue;
|
|
}
|
|
|
|
$slug = sanitize_key((string) $parts[0]);
|
|
$label = sanitize_text_field((string) $parts[1]);
|
|
if ($slug === '' || $label === '') {
|
|
continue;
|
|
}
|
|
|
|
$items[] = [
|
|
'slug' => $slug,
|
|
'label' => $label,
|
|
];
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
}
|