Normalize fixed profile fields, profile actions, and the five-column capability matrix for comments, uploads, and downloads.
231 lines
12 KiB
PHP
231 lines
12 KiB
PHP
<?php
|
|
/**
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Settings;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class Capabilities {
|
|
|
|
/**
|
|
* Capability definitions registry.
|
|
*
|
|
* @return array<string, array{label:string, exclude_roles?:string[], default?:array<string,mixed>}>
|
|
*/
|
|
public static function cap_definitions(): array {
|
|
return [
|
|
'user_report' => ['label' => __('举报其它用户', 'aksrt'), 'default' => ['logged' => true]],
|
|
'balance_transfer' => ['label' => __('余额转账给其他用户', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'points_transfer' => ['label' => __('积分转账给其他用户', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'balance_withdraw' => ['label' => __('余额提现', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'new_post_add' => ['label' => __('发布新文章', 'aksrt'), 'default' => ['logged' => true]],
|
|
'new_post_audit_no' => ['label' => __('投稿无需审核直接发布', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'new_post_audit_no_manual' => ['label' => __('无需人工审核直接发布', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'new_post_upload_video' => ['label' => __('投稿允许上传视频', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'new_post_iframe_video' => ['label' => __('投稿允许插入 iframe 视频', 'aksrt'), 'exclude_roles' => ['all'], 'default' => ['logged' => true]],
|
|
'new_post_hide' => ['label' => __('投稿允许发布隐藏内容', 'aksrt'), 'default' => ['logged' => true]],
|
|
'new_post_image_cover' => ['label' => __('发帖允许设置图片封面', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'new_post_slide_cover' => ['label' => __('允许设置幻灯片封面', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'new_post_video_cover' => ['label' => __('允许设置视频封面', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'new_post_pay' => ['label' => __('投稿允许设置付费内容', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'new_post_pay_download' => ['label' => __('投稿允许设置付费下载', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'new_post_edit' => ['label' => __('修改自己发布的投稿', 'aksrt'), 'exclude_roles' => ['all'], 'default' => ['logged' => true]],
|
|
'new_post_delete' => ['label' => __('删除自己发布的投稿', 'aksrt'), 'exclude_roles' => ['all']],
|
|
|
|
'comment_view' => ['label' => __('查看评论', 'aksrt'), 'default' => ['all' => true]],
|
|
'comment_add' => ['label' => __('发表评论', 'aksrt'), 'default' => ['logged' => true]],
|
|
'comment_edit' => ['label' => __('修改自己发布的评论', 'aksrt'), 'exclude_roles' => ['all'], 'default' => ['logged' => true]],
|
|
'comment_delete' => ['label' => __('删除自己发布的评论', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_audit_no' => ['label' => __('评论无需审核直接发布', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_audit_no_manual' => ['label' => __('评论无需人工审核', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_set_topping_my_post' => ['label' => __('置顶自己文章下的评论', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_set_topping_other' => ['label' => __('置顶其他文章下的评论', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_edit_my_post' => ['label' => __('修改自己文章下的评论', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_edit_other' => ['label' => __('修改其他文章下的评论', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_audit_my_post' => ['label' => __('审核自己文章下的评论', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_audit_other' => ['label' => __('审核其他文章下的评论', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_delete_my_post' => ['label' => __('删除自己文章下的评论', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_delete_other' => ['label' => __('删除其他文章下的评论', 'aksrt'), 'exclude_roles' => ['all']],
|
|
|
|
'upload_avatar' => ['label' => __('上传头像', 'aksrt'), 'default' => ['logged' => true]],
|
|
'upload_profile_background' => ['label' => __('上传用户资料卡背景图', 'aksrt'), 'default' => ['logged' => true]],
|
|
'upload_submit_image' => ['label' => __('投稿图片上传', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'upload_comment_image' => ['label' => __('评论图片上传', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'upload_resource_attachment'=> ['label' => __('资源附件上传', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'resource_download' => ['label' => __('资源下载', 'aksrt'), 'default' => ['logged' => true]],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* All role columns in the capability matrix.
|
|
*/
|
|
public static function cap_role_columns(): array {
|
|
return ['all', 'logged', 'level', 'vip', 'auth'];
|
|
}
|
|
|
|
/**
|
|
* Read the user_caps matrix from settings.
|
|
*
|
|
* @return array<string, array<string, mixed>>
|
|
*/
|
|
public static function get_user_capabilities(): array {
|
|
$s = Settings::get_all();
|
|
$stored = is_array($s['user_caps'] ?? null) ? $s['user_caps'] : [];
|
|
$defs = self::cap_definitions();
|
|
$cols = self::cap_role_columns();
|
|
|
|
$result = [];
|
|
foreach ($defs as $cap_id => $def) {
|
|
$defaults = $def['default'] ?? [];
|
|
$excludes = $def['exclude_roles'] ?? [];
|
|
|
|
$row = [];
|
|
foreach ($cols as $col) {
|
|
if (in_array($col, $excludes, true)) {
|
|
$row[$col] = false;
|
|
continue;
|
|
}
|
|
|
|
if (isset($stored[$cap_id][$col])) {
|
|
$val = $stored[$cap_id][$col];
|
|
$row[$col] = in_array($col, ['level', 'vip'], true) ? (int) $val : (bool) $val;
|
|
} else {
|
|
$row[$col] = $defaults[$col] ?? (in_array($col, ['level', 'vip'], true) ? -1 : false);
|
|
}
|
|
}
|
|
$result[$cap_id] = $row;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Resolve whether a user has a specific capability.
|
|
*/
|
|
public static function user_can(string $cap, int $user_id = 0, ?int $context = null): bool {
|
|
if ($user_id > 0 && user_can($user_id, 'manage_options')) {
|
|
return true;
|
|
}
|
|
|
|
$caps = self::get_user_capabilities();
|
|
if (!isset($caps[$cap])) {
|
|
return false;
|
|
}
|
|
|
|
$row = $caps[$cap];
|
|
|
|
if (!empty($row['all'])) {
|
|
return true;
|
|
}
|
|
|
|
if ($user_id <= 0) {
|
|
return false;
|
|
}
|
|
|
|
if (!empty($row['logged'])) {
|
|
return true;
|
|
}
|
|
|
|
$level_threshold = (int) ($row['level'] ?? -1);
|
|
if ($level_threshold >= 0) {
|
|
$user_level = class_exists('\AKSRT\User\Level') ? \AKSRT\User\Level::get_level($user_id) : 0;
|
|
if ($user_level >= $level_threshold) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
$vip_threshold = (int) ($row['vip'] ?? -1);
|
|
if ($vip_threshold >= 0) {
|
|
$user_vip = function_exists('aksrt_get_user_vip_level') ? aksrt_get_user_vip_level($user_id) : 0;
|
|
if ($user_vip >= $vip_threshold) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
if (!empty($row['auth'])) {
|
|
if (class_exists('\\AKSRT\\User\\AuthApply') && \AKSRT\User\AuthApply::is_verified($user_id)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 获取"网站安全"设置(人机验证见 aksrt_get_captcha_status_summary)。
|
|
*/
|
|
public static function get_security_settings(): array {
|
|
$s = Settings::get_all();
|
|
|
|
$captcha = function_exists('aksrt_get_captcha_status_summary')
|
|
? aksrt_get_captcha_status_summary()
|
|
: ['enabled' => false, 'provider' => '', 'configured' => false, 'scenes' => []];
|
|
|
|
return [
|
|
'captcha' => $captcha,
|
|
'brush_limit_post' => [
|
|
'enabled' => !empty($s['brush_limit_post_enabled']),
|
|
'minutes_10' => (int) ($s['brush_limit_post_minutes_10'] ?? 20),
|
|
'day_1' => (int) ($s['brush_limit_post_day_1'] ?? 50),
|
|
],
|
|
'brush_limit_comment' => [
|
|
'enabled' => !empty($s['brush_limit_comment_enabled']),
|
|
'minutes_10' => (int) ($s['brush_limit_comment_minutes_10'] ?? 20),
|
|
'day_1' => (int) ($s['brush_limit_comment_day_1'] ?? 100),
|
|
],
|
|
'brush_limit_upload' => [
|
|
'enabled' => !empty($s['brush_limit_upload_enabled']),
|
|
'minutes_10' => (int) ($s['brush_limit_upload_minutes_10'] ?? 50),
|
|
'day_1' => (int) ($s['brush_limit_upload_day_1'] ?? 200),
|
|
],
|
|
'brush_limit_register' => [
|
|
'enabled' => !empty($s['brush_limit_register_enabled']),
|
|
'minutes_10' => (int) ($s['brush_limit_register_minutes_10'] ?? 20),
|
|
'day_1' => (int) ($s['brush_limit_register_day_1'] ?? 100),
|
|
],
|
|
'brush_limit_order' => [
|
|
'enabled' => !empty($s['brush_limit_order_enabled']),
|
|
'minutes_10' => (int) ($s['brush_limit_order_minutes_10'] ?? 30),
|
|
'day_1' => (int) ($s['brush_limit_order_day_1'] ?? 100),
|
|
],
|
|
'error_frequency_limit' => !empty($s['error_frequency_limit'] ?? true),
|
|
];
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取"系统工具"设置
|
|
*/
|
|
public static function get_system_tools_settings(): array {
|
|
$s = Settings::get_all();
|
|
$time_format = sanitize_text_field((string) ($s['time_format'] ?? 'n月j日 H:i'));
|
|
|
|
return [
|
|
'remove_emoji' => !empty($s['remove_emoji'] ?? true),
|
|
'remove_open_sans' => !empty($s['remove_open_sans'] ?? true),
|
|
'remove_more_wp_head' => !empty($s['remove_more_wp_head'] ?? true),
|
|
'hide_admin_bar' => !empty($s['hide_admin_bar'] ?? true),
|
|
'remove_dashboard_widgets' => !empty($s['remove_dashboard_widgets'] ?? true),
|
|
'admin_frontend_set' => !empty($s['admin_frontend_set'] ?? true),
|
|
'display_wp_update' => !empty($s['display_wp_update'] ?? true),
|
|
'close_gutenberg' => !empty($s['close_gutenberg']),
|
|
'admin_user_del_fields' => !empty($s['admin_user_del_fields'] ?? true),
|
|
'split_upload_s' => !empty($s['split_upload_s'] ?? true),
|
|
'split_minimum_size' => max(4, min(100, (int) ($s['split_minimum_size'] ?? 20))),
|
|
'newfilename' => !empty($s['newfilename']),
|
|
'newfilename_type' => in_array($s['newfilename_type'] ?? 'time', ['random', 'time'], true)
|
|
? $s['newfilename_type'] : 'time',
|
|
'disabled_large_thumbnail' => !empty($s['disabled_large_thumbnail'] ?? true),
|
|
'disabled_autoembed' => !empty($s['disabled_autoembed'] ?? true),
|
|
'mce_float_toolbar' => [
|
|
'pc_s' => !empty($s['mce_float_toolbar_pc_s'] ?? true),
|
|
'm_s' => !empty($s['mce_float_toolbar_m_s'] ?? true),
|
|
],
|
|
'time_ago_s' => !empty($s['time_ago_s'] ?? true),
|
|
'time_format' => $time_format,
|
|
];
|
|
}
|
|
}
|