'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\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'], ]); } // ── 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\Messages\Message')) { return $this->success(['data' => [], 'total' => 0]); } $msg = \AKSRT\Messages\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\Messages\Message')) { return $this->success(['data' => ['unread' => 0]]); } $count = \AKSRT\Messages\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\Messages\Message')) { return $this->error('not_available', '消息系统未启用。', 501); } $result = \AKSRT\Messages\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\Messages\Message')) { return $this->error('not_available', '消息系统未启用。', 501); } $result = \AKSRT\Messages\Message::get_instance()->mark_all_read($user_id); return $this->success(['data' => ['success' => $result]]); } public function check_logged_in_permission(): bool { return is_user_logged_in(); } }