Files
wordpress-theme-aksrt/inc/classes/admin/class-aksrt-admin-user-controller-restriction-operations.php
T

83 lines
2.8 KiB
PHP

<?php
/**
* AKSRT admin user restriction operations support.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
trait AKSRT_Admin_User_Controller_Restriction_Operations {
public function get_user_restrictions(WP_REST_Request $request): WP_REST_Response {
$user_id = (int) $request->get_param('user_id');
$user = get_userdata($user_id);
if (!$user) {
return $this->error('用户不存在', 404);
}
if (!class_exists('AKSRT_User_Restriction')) {
return $this->success(['data' => ['restrictions' => []]]);
}
$restrictions = AKSRT_User_Restriction::get_instance()->get_user_restrictions($user_id, false);
return $this->success(['data' => ['restrictions' => $restrictions]]);
}
public function add_user_restriction(WP_REST_Request $request): WP_REST_Response {
$user_id = (int) $request->get_param('user_id');
$user = get_userdata($user_id);
if (!$user) {
return $this->error('用户不存在', 404);
}
$params = $request->get_json_params() ?? [];
$type = sanitize_key((string) ($params['type'] ?? ''));
$reason = sanitize_textarea_field((string) ($params['reason'] ?? ''));
$scope = sanitize_key((string) ($params['scope'] ?? 'global'));
$expires_at = sanitize_text_field((string) ($params['expires_at'] ?? ''));
if (!$type) {
return $this->error('请选择限制类型');
}
if (!class_exists('AKSRT_User_Restriction')) {
return $this->error('限制系统未启用', 501);
}
$result = AKSRT_User_Restriction::get_instance()->restrict(
$user_id, $type, $reason, $scope, $expires_at, get_current_user_id()
);
if (!$result['success']) {
return $this->error($result['message']);
}
return $this->success(['message' => '限制已添加', 'data' => $result]);
}
public function remove_user_restriction(WP_REST_Request $request): WP_REST_Response {
$restriction_id = (int) $request->get_param('id');
if (!class_exists('AKSRT_User_Restriction')) {
return $this->error('限制系统未启用', 501);
}
$result = AKSRT_User_Restriction::get_instance()->remove_restriction($restriction_id);
if (!$result) {
return $this->error('移除失败', 500);
}
return $this->success(['message' => '限制已移除']);
}
public function get_restriction_stats(WP_REST_Request $request): WP_REST_Response {
if (!class_exists('AKSRT_User_Restriction')) {
return $this->success(['data' => ['total' => 0, 'active' => 0, 'banned' => 0]]);
}
$stats = AKSRT_User_Restriction::get_instance()->get_stats();
return $this->success(['data' => $stats]);
}
}