'GET', 'callback' => [$this, 'get_capabilities'], 'permission_callback' => [$this, 'check_permission'], ]); register_rest_route(self::API_NAMESPACE, '/permissions/capabilities', [ 'methods' => 'POST', 'callback' => [$this, 'save_capabilities'], 'permission_callback' => [$this, 'check_permission'], ]); } public function get_capabilities(): \WP_REST_Response { if (!class_exists('\AKSRT\Settings\Settings')) { return $this->error(__('能力矩阵模块不可用', 'aksrt'), 500); } $caps = \AKSRT\Settings\Settings::get_user_capabilities(); $defs = \AKSRT\Settings\Settings::cap_definitions(); $columns = \AKSRT\Settings\Settings::cap_role_columns(); return $this->success([ 'data' => [ 'columns' => $this->get_capability_column_labels(), 'groups' => $this->build_capability_groups($defs, $caps, $columns), 'summary' => [ 'cap_count' => count($defs), 'group_count' => 5, 'column_count' => count($columns), ], ], ]); } public function save_capabilities(\WP_REST_Request $request): \WP_REST_Response { if (!class_exists('\AKSRT\Settings\Settings')) { return $this->error(__('能力矩阵模块不可用', 'aksrt'), 500); } $params = $this->get_json_params($request); $input = $params['user_caps'] ?? null; if (!is_array($input)) { return $this->error(__('无效的能力矩阵数据', 'aksrt'), 422); } $defs = \AKSRT\Settings\Settings::cap_definitions(); $columns = \AKSRT\Settings\Settings::cap_role_columns(); $clean = []; foreach ($defs as $cap_id => $def) { $row = is_array($input[$cap_id] ?? null) ? $input[$cap_id] : []; $clean[$cap_id] = []; foreach ($columns as $column) { if (in_array($column, ['level', 'vip'], true)) { $value = isset($row[$column]) ? (int) $row[$column] : -1; $max = $column === 'level' ? 10 : 2; $clean[$cap_id][$column] = max(-1, min($max, $value)); } else { $clean[$cap_id][$column] = !empty($row[$column]); } } } $all = \AKSRT\Settings\Settings::get_all(); $all['user_caps'] = $clean; update_option('aksrt_settings', $all); \AKSRT\Settings\Settings::flush_settings_cache(); return $this->success([ 'message' => __('能力矩阵已保存', 'aksrt'), 'data' => [ 'user_caps' => $clean, ], ]); } private function build_capability_groups(array $defs, array $caps, array $columns): array { $buckets = [ 'user' => [ 'key' => 'user', 'title' => __('用户治理', 'aksrt'), 'items' => [], ], 'post' => [ 'key' => 'post', 'title' => __('投稿权限', 'aksrt'), 'items' => [], ], 'comment' => [ 'key' => 'comment', 'title' => __('评论权限', 'aksrt'), 'items' => [], ], 'upload' => [ 'key' => 'upload', 'title' => __('上传权限', 'aksrt'), 'items' => [], ], 'download' => [ 'key' => 'download', 'title' => __('下载权限', 'aksrt'), 'items' => [], ], ]; foreach ($defs as $cap_id => $def) { $bucket_key = 'user'; if (str_starts_with($cap_id, 'comment_')) { $bucket_key = 'comment'; } elseif (str_starts_with($cap_id, 'new_post_')) { $bucket_key = 'post'; } elseif (str_starts_with($cap_id, 'upload_')) { $bucket_key = 'upload'; } elseif (str_starts_with($cap_id, 'resource_')) { $bucket_key = 'download'; } $excludes = $def['exclude_roles'] ?? []; $values = $caps[$cap_id] ?? []; $visible_columns = []; foreach ($columns as $column) { if (in_array($column, $excludes, true)) { continue; } $visible_columns[] = $column; } $buckets[$bucket_key]['items'][] = [ 'cap_id' => $cap_id, 'label' => $def['label'], 'visible_columns' => $visible_columns, 'values' => $values, ]; } return array_values($buckets); } private function get_capability_column_labels(): array { return [ 'all' => __('所有人', 'aksrt'), 'logged' => __('已登录', 'aksrt'), 'level' => __('等级≥', 'aksrt'), 'vip' => __('VIP≥', 'aksrt'), 'auth' => __('已认证', 'aksrt'), ]; } }