导入 AKSRT 主题开发源码

This commit is contained in:
Claude Code
2026-07-03 22:50:34 +08:00
parent 9681dd3d48
commit 1268a55dd0
595 changed files with 127705 additions and 0 deletions
@@ -0,0 +1,254 @@
<?php
/**
* Admin REST Controller — Messages & Conversations
*
* Provides REST endpoints for notifications and private messages
* under aksrt/v1/messages and aksrt/v1/conversations.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
class AKSRT_Admin_Message_Controller extends AKSRT_Admin_Controller_Base {
private static $instance = null;
public static function get_instance(): self {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function register_routes(): void {
// ── Notifications ──
// List notifications
register_rest_route(self::API_NAMESPACE, '/messages', [
'methods' => 'GET',
'callback' => [$this, 'list_notifications'],
'permission_callback' => [$this, 'check_logged_in_permission'],
'args' => [
'type' => ['default' => '', 'sanitize_callback' => 'sanitize_text_field'],
'page' => ['default' => 1, 'sanitize_callback' => 'absint'],
'per_page' => ['default' => 20, 'sanitize_callback' => 'absint'],
],
]);
// Unread count
register_rest_route(self::API_NAMESPACE, '/messages/unread', [
'methods' => 'GET',
'callback' => [$this, 'get_unread_count'],
'permission_callback' => [$this, 'check_logged_in_permission'],
]);
// Mark one notification read
register_rest_route(self::API_NAMESPACE, '/messages/(?P<id>\d+)/read', [
'methods' => 'POST',
'callback' => [$this, 'mark_read'],
'permission_callback' => [$this, 'check_logged_in_permission'],
]);
// Mark all notifications read
register_rest_route(self::API_NAMESPACE, '/messages/read-all', [
'methods' => 'POST',
'callback' => [$this, 'mark_all_read'],
'permission_callback' => [$this, 'check_logged_in_permission'],
]);
// ── Private Messages / Conversations ──
// List conversations
register_rest_route(self::API_NAMESPACE, '/conversations', [
'methods' => 'GET',
'callback' => [$this, 'list_conversations'],
'permission_callback' => [$this, 'check_logged_in_permission'],
'args' => [
'limit' => ['default' => 20, 'sanitize_callback' => 'absint'],
],
]);
// Get thread messages
register_rest_route(self::API_NAMESPACE, '/conversations/(?P<id>\d+)/messages', [
'methods' => 'GET',
'callback' => [$this, 'get_thread'],
'permission_callback' => [$this, 'check_logged_in_permission'],
'args' => [
'page' => ['default' => 1, 'sanitize_callback' => 'absint'],
'per_page' => ['default' => 30, 'sanitize_callback' => 'absint'],
],
]);
// Send message in conversation
register_rest_route(self::API_NAMESPACE, '/conversations/(?P<id>\d+)/messages', [
'methods' => 'POST',
'callback' => [$this, 'send_message'],
'permission_callback' => [$this, 'check_logged_in_permission'],
'args' => [
'content' => ['required' => true, 'sanitize_callback' => 'sanitize_textarea_field'],
],
]);
// Create or get conversation with a user
register_rest_route(self::API_NAMESPACE, '/conversations/with/(?P<user_id>\d+)', [
'methods' => 'POST',
'callback' => [$this, 'get_or_create_conversation'],
'permission_callback' => [$this, 'check_logged_in_permission'],
]);
// Mark conversation read
register_rest_route(self::API_NAMESPACE, '/conversations/(?P<id>\d+)/read', [
'methods' => 'POST',
'callback' => [$this, 'mark_conversation_read'],
'permission_callback' => [$this, 'check_logged_in_permission'],
]);
}
// ── Notification handlers ──
public function list_notifications(WP_REST_Request $request): WP_REST_Response {
$user_id = get_current_user_id();
$type = $request->get_param('type');
$per_page = min(100, (int) $request->get_param('per_page'));
$page = max(1, (int) $request->get_param('page'));
if (!class_exists('AKSRT_Message')) {
return $this->success(['data' => [], 'total' => 0]);
}
$msg = AKSRT_Message::get_instance();
$args = [
'limit' => $per_page,
'offset' => ($page - 1) * $per_page,
];
if ($type) {
$args['type'] = $type;
}
$notifications = $msg->get_notifications($user_id, $args);
$unread = $msg->get_unread_count($user_id);
return $this->success([
'data' => $notifications,
'unread' => $unread,
]);
}
public function get_unread_count(WP_REST_Request $request): WP_REST_Response {
$user_id = get_current_user_id();
if (!class_exists('AKSRT_Message')) {
return $this->success(['data' => ['unread' => 0]]);
}
$count = AKSRT_Message::get_instance()->get_unread_count($user_id);
return $this->success(['data' => ['unread' => $count]]);
}
public function mark_read(WP_REST_Request $request): WP_REST_Response {
$user_id = get_current_user_id();
$id = (string) $request->get_param('id');
if (!class_exists('AKSRT_Message')) {
return $this->error('not_available', '消息系统未启用。', 501);
}
$result = AKSRT_Message::get_instance()->mark_read($user_id, $id);
return $this->success(['data' => ['success' => $result]]);
}
public function mark_all_read(WP_REST_Request $request): WP_REST_Response {
$user_id = get_current_user_id();
if (!class_exists('AKSRT_Message')) {
return $this->error('not_available', '消息系统未启用。', 501);
}
$result = AKSRT_Message::get_instance()->mark_all_read($user_id);
return $this->success(['data' => ['success' => $result]]);
}
// ── Conversation handlers ──
public function list_conversations(WP_REST_Request $request): WP_REST_Response {
$user_id = get_current_user_id();
$limit = min(50, (int) $request->get_param('limit'));
if (!class_exists('AKSRT_Private_Message')) {
return $this->success(['data' => []]);
}
$conversations = AKSRT_Private_Message::get_instance()->get_conversations($user_id, $limit);
return $this->success(['data' => $conversations]);
}
public function get_thread(WP_REST_Request $request): WP_REST_Response {
$user_id = get_current_user_id();
$conv_id = (int) $request->get_param('id');
$page = max(1, (int) $request->get_param('page'));
$per_page = min(50, (int) $request->get_param('per_page'));
if (!class_exists('AKSRT_Private_Message')) {
return $this->error('not_available', '私信系统未启用。', 501);
}
$result = AKSRT_Private_Message::get_instance()->get_thread($conv_id, $user_id, $page, $per_page);
if (is_wp_error($result)) {
return $this->error('thread_error', $result->get_error_message(), 403);
}
return $this->success(['data' => $result]);
}
public function send_message(WP_REST_Request $request): WP_REST_Response {
$user_id = get_current_user_id();
$conv_id = (int) $request->get_param('id');
$content = trim((string) $request->get_param('content'));
if (!$content) {
return $this->error('content_required', '消息内容不能为空。', 400);
}
if (!class_exists('AKSRT_Private_Message')) {
return $this->error('not_available', '私信系统未启用。', 501);
}
$result = AKSRT_Private_Message::get_instance()->send($conv_id, $user_id, $content);
if (!$result['success']) {
return $this->error('send_failed', $result['message'] ?? '发送失败。', 500);
}
return $this->success(['data' => $result]);
}
public function get_or_create_conversation(WP_REST_Request $request): WP_REST_Response {
$user_id = get_current_user_id();
$other_id = (int) $request->get_param('user_id');
if ($user_id === $other_id) {
return $this->error('self_message', '不能与自己创建对话。', 400);
}
if (!class_exists('AKSRT_Private_Message')) {
return $this->error('not_available', '私信系统未启用。', 501);
}
$conv_id = AKSRT_Private_Message::get_instance()->get_or_create_conversation($user_id, $other_id);
if (is_wp_error($conv_id)) {
return $this->error('create_failed', $conv_id->get_error_message(), 500);
}
return $this->success(['data' => ['conversation_id' => $conv_id]]);
}
public function mark_conversation_read(WP_REST_Request $request): WP_REST_Response {
$user_id = get_current_user_id();
$conv_id = (int) $request->get_param('id');
if (!class_exists('AKSRT_Private_Message')) {
return $this->error('not_available', '私信系统未启用。', 501);
}
AKSRT_Private_Message::get_instance()->mark_conversation_read($conv_id, $user_id);
return $this->success(['data' => ['success' => true]]);
}
public function check_logged_in_permission(): bool {
return is_user_logged_in();
}
}