get_forum_service(); if (!$forum) { return $this->error(__('论坛系统未启用', 'aksrt')); } $application_id = (string) $request->get_param('id'); $params = $this->get_json_params($request); $status = sanitize_key((string) ($params['status'] ?? '')); $note = sanitize_textarea_field((string) ($params['note'] ?? '')); $result = $forum->review_moderator_application($application_id, $status, $note, get_current_user_id()); if (is_wp_error($result)) { return $this->error($result->get_error_message()); } return $this->success([ 'message' => $result['message'] ?? __('申请状态已更新', 'aksrt'), 'data' => $result, ]); } public function toggle_favorite(\WP_REST_Request $request): \WP_REST_Response { $forum = $this->get_forum_service(); if (!$forum) { return $this->error(__('论坛系统未启用', 'aksrt')); } $topic_id = (int) $request->get_param('id'); $result = $forum->toggle_favorite(get_current_user_id(), $topic_id); if (is_wp_error($result)) { $status = $result->get_error_code() === 'not_logged_in' ? 401 : 400; return $this->error($result->get_error_message(), $status); } return $this->success($result); } public function score_topic(\WP_REST_Request $request): \WP_REST_Response { $forum = $this->get_forum_service(); if (!$forum) { return $this->error(__('论坛系统未启用', 'aksrt')); } $topic_id = (int) $request->get_param('id'); $params = $this->get_json_params($request); $amount = (int) ($params['amount'] ?? 0); $result = $forum->score_topic(get_current_user_id(), $topic_id, $amount); if (is_wp_error($result)) { $status = $result->get_error_code() === 'not_logged_in' ? 401 : 400; return $this->error($result->get_error_message(), $status); } return $this->success($result); } public function adopt_answer(\WP_REST_Request $request): \WP_REST_Response { $forum = $this->get_forum_service(); if (!$forum) { return $this->error(__('论坛系统未启用', 'aksrt')); } $topic_id = (int) $request->get_param('id'); $params = $this->get_json_params($request); $comment_id = (int) ($params['comment_id'] ?? 0); $result = $forum->adopt_answer($topic_id, $comment_id, get_current_user_id()); if (is_wp_error($result)) { $status = $result->get_error_code() === 'forbidden' ? 403 : 400; return $this->error($result->get_error_message(), $status); } return $this->success($result); } public function check_logged_in_permission(): bool { return is_user_logged_in(); } private function get_forum_service(): ?\AKSRT\Forum\Forum { return class_exists('\AKSRT\Forum\Forum') ? \AKSRT\Forum\Forum::get_instance() : null; } public function frontend_create_plate(\WP_REST_Request $request): \WP_REST_Response { $forum = $this->get_forum_service(); if (!$forum) { return $this->error(__('论坛系统未启用', 'aksrt')); } $user_id = get_current_user_id(); if (!$forum->is_forum_admin($user_id)) { return $this->error(__('仅论坛管理员可创建版块', 'aksrt'), 403); } $params = $this->get_json_params($request); $data = [ 'name' => sanitize_text_field((string) ($params['name'] ?? '')), 'description' => sanitize_textarea_field((string) ($params['description'] ?? '')), 'icon' => sanitize_text_field((string) ($params['icon'] ?? '')), 'cover_url' => esc_url_raw((string) ($params['cover_url'] ?? '')), 'parent_id' => (int) ($params['parent_id'] ?? 0), 'plate_cat' => (int) ($params['plate_cat'] ?? 0), ]; if ($data['name'] === '') { return $this->error(__('版块名称不能为空', 'aksrt')); } $result = $forum->create_plate($data); if (is_wp_error($result)) { return $this->error($result->get_error_message()); } return $this->success($result); } public function frontend_update_plate(\WP_REST_Request $request): \WP_REST_Response { $forum = $this->get_forum_service(); if (!$forum) { return $this->error(__('论坛系统未启用', 'aksrt')); } $user_id = get_current_user_id(); $plate_id = (int) $request->get_param('id'); $post = get_post($plate_id); if (!$post || $post->post_type !== 'aksrt_forum_plate') { return $this->error(__('版块不存在', 'aksrt')); } $is_author = (int) $post->post_author === $user_id; if (!$is_author && !$forum->is_forum_admin($user_id)) { return $this->error(__('无权编辑此版块', 'aksrt'), 403); } $params = $this->get_json_params($request); $data = [ 'name' => sanitize_text_field((string) ($params['name'] ?? '')), 'description' => sanitize_textarea_field((string) ($params['description'] ?? '')), 'icon' => sanitize_text_field((string) ($params['icon'] ?? '')), 'cover_url' => esc_url_raw((string) ($params['cover_url'] ?? '')), ]; $result = $forum->update_plate($plate_id, $data); if (is_wp_error($result)) { return $this->error($result->get_error_message()); } return $this->success($result); } public function frontend_delete_plate(\WP_REST_Request $request): \WP_REST_Response { $forum = $this->get_forum_service(); if (!$forum) { return $this->error(__('论坛系统未启用', 'aksrt')); } $user_id = get_current_user_id(); if (!$forum->is_forum_admin($user_id)) { return $this->error(__('仅论坛管理员可删除版块', 'aksrt'), 403); } $plate_id = (int) $request->get_param('id'); $params = $this->get_json_params($request); $cascade = sanitize_key((string) ($params['cascade'] ?? 'trash')); $move_to = (int) ($params['move_to'] ?? 0); $result = $forum->delete_plate($plate_id, $cascade, $move_to); if (!$result) { return $this->error(__('删除版块失败', 'aksrt')); } return $this->success(['message' => __('版块已删除', 'aksrt')]); } }