385 lines
14 KiB
PHP
385 lines
14 KiB
PHP
<?php
|
|
defined('ABSPATH') || exit;
|
|
|
|
trait AKSRT_Admin_Forum_Controller_Part_1 {
|
|
|
|
public function update_plate(WP_REST_Request $request): WP_REST_Response {
|
|
$forum = $this->get_forum_service();
|
|
if (!$forum) {
|
|
return $this->error(__('论坛系统未启用', 'aksrt'));
|
|
}
|
|
|
|
$plate_id = (int) $request->get_param('id');
|
|
$params = $this->get_json_params($request);
|
|
|
|
$result = $forum->update_plate($plate_id, $params);
|
|
if (!$result) {
|
|
return $this->error(__('更新版块失败', 'aksrt'));
|
|
}
|
|
|
|
return $this->success(['message' => __('版块已更新', 'aksrt')]);
|
|
}
|
|
|
|
public function delete_plate(WP_REST_Request $request): WP_REST_Response {
|
|
$forum = $this->get_forum_service();
|
|
if (!$forum) {
|
|
return $this->error(__('论坛系统未启用', 'aksrt'));
|
|
}
|
|
|
|
$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')]);
|
|
}
|
|
|
|
public function reorder_plates(WP_REST_Request $request): WP_REST_Response {
|
|
$forum = $this->get_forum_service();
|
|
if (!$forum) {
|
|
return $this->error(__('论坛系统未启用', 'aksrt'));
|
|
}
|
|
|
|
$params = $this->get_json_params($request);
|
|
$order = $params['order'] ?? [];
|
|
if (!is_array($order) || empty($order)) {
|
|
return $this->error(__('排序参数无效', 'aksrt'));
|
|
}
|
|
|
|
$forum->reorder_plates($order);
|
|
|
|
return $this->success(['message' => __('排序已更新', 'aksrt')]);
|
|
}
|
|
|
|
public function toggle_follow_plate(WP_REST_Request $request): WP_REST_Response {
|
|
$forum = $this->get_forum_service();
|
|
if (!$forum) {
|
|
return $this->error(__('论坛系统未启用', 'aksrt'));
|
|
}
|
|
|
|
$plate_id = (int) $request->get_param('id');
|
|
$user_id = get_current_user_id();
|
|
|
|
$result = $forum->toggle_follow_plate($user_id, $plate_id);
|
|
return $this->success($result);
|
|
}
|
|
|
|
public function get_plate_categories(): WP_REST_Response {
|
|
$forum = $this->get_forum_service();
|
|
if (!$forum) {
|
|
return $this->success(['data' => []]);
|
|
}
|
|
|
|
return $this->success(['data' => $forum->get_plate_categories()]);
|
|
}
|
|
|
|
public function get_sorted_plates(WP_REST_Request $request): WP_REST_Response {
|
|
$forum = $this->get_forum_service();
|
|
if (!$forum) {
|
|
return $this->success(['data' => []]);
|
|
}
|
|
|
|
$orderby = sanitize_key((string) ($request->get_param('orderby') ?: 'menu_order'));
|
|
$order = sanitize_key((string) ($request->get_param('order') ?: 'ASC'));
|
|
$limit = max(1, min(100, (int) ($request->get_param('limit') ?: 50)));
|
|
|
|
return $this->success(['data' => $forum->get_sorted_plates($orderby, $order, $limit)]);
|
|
}
|
|
|
|
public function get_topics(WP_REST_Request $request): WP_REST_Response {
|
|
$forum = $this->get_forum_service();
|
|
if (!$forum) {
|
|
return $this->success(['data' => ['items' => [], 'total' => 0, 'page' => 1, 'total_pages' => 1]]);
|
|
}
|
|
|
|
$args = [
|
|
'page' => max(1, (int) ($request->get_param('page') ?: 1)),
|
|
'per_page' => max(1, min(100, (int) ($request->get_param('per_page') ?: 20))),
|
|
'status' => sanitize_key((string) ($request->get_param('status') ?: 'all')),
|
|
'board_id' => (int) ($request->get_param('board_id') ?: 0),
|
|
'visibility' => sanitize_key((string) ($request->get_param('visibility') ?: '')),
|
|
'review_status' => sanitize_key((string) ($request->get_param('review_status') ?: '')),
|
|
'keyword' => sanitize_text_field((string) ($request->get_param('keyword') ?: '')),
|
|
'orderby' => sanitize_key((string) ($request->get_param('orderby') ?: 'date')),
|
|
'viewer_user_id' => get_current_user_id(),
|
|
'include_hidden' => true,
|
|
];
|
|
|
|
$view = $forum->get_topic_list($args);
|
|
|
|
return $this->success([
|
|
'data' => [
|
|
'items' => $view['items'],
|
|
'total' => $view['total'],
|
|
'page' => $view['page'],
|
|
'total_pages' => $view['max_pages'],
|
|
'options' => [
|
|
'boards' => $forum->get_boards(),
|
|
'visibility' => $forum->get_visibility_options(),
|
|
'review' => $forum->get_review_options(),
|
|
'status' => [
|
|
['label' => __('全部', 'aksrt'), 'value' => 'all'],
|
|
['label' => __('已发布', 'aksrt'), 'value' => 'publish'],
|
|
['label' => __('待审核', 'aksrt'), 'value' => 'pending'],
|
|
['label' => __('草稿/拒绝', 'aksrt'), 'value' => 'draft'],
|
|
],
|
|
],
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function get_topic_detail(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');
|
|
if ($topic_id <= 0) {
|
|
return $this->error(__('参数无效', 'aksrt'));
|
|
}
|
|
|
|
return $this->success([
|
|
'data' => $forum->get_topic_detail($topic_id, get_current_user_id()),
|
|
]);
|
|
}
|
|
|
|
public function moderate_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);
|
|
$action = sanitize_key((string) ($params['action'] ?? ''));
|
|
|
|
if ($topic_id <= 0 || $action === '') {
|
|
return $this->error(__('参数无效', 'aksrt'));
|
|
}
|
|
|
|
$result = $forum->moderate_topic($topic_id, $action, $params, get_current_user_id());
|
|
if (is_wp_error($result)) {
|
|
return $this->error($result->get_error_message(), $result->get_error_code() === 'forbidden' ? 403 : 400);
|
|
}
|
|
|
|
return $this->success([
|
|
'message' => $result['message'] ?? __('治理动作已执行', 'aksrt'),
|
|
'data' => $result,
|
|
]);
|
|
}
|
|
|
|
public function vote_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);
|
|
$option_ids = $params['option_ids'] ?? [];
|
|
if (!is_array($option_ids)) {
|
|
$option_ids = [];
|
|
}
|
|
|
|
$result = $forum->submit_vote($topic_id, get_current_user_id(), $option_ids);
|
|
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([
|
|
'message' => $result['message'] ?? __('投票成功', 'aksrt'),
|
|
'data' => $result,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Legacy admin moderation endpoint kept for compatibility.
|
|
*/
|
|
public function update_topic_legacy(WP_REST_Request $request): WP_REST_Response {
|
|
$params = $this->get_json_params($request);
|
|
$topic_id = (int) ($params['topic_id'] ?? 0);
|
|
if ($topic_id <= 0) {
|
|
return $this->error(__('参数无效', 'aksrt'));
|
|
}
|
|
|
|
$forum = $this->get_forum_service();
|
|
if (!$forum) {
|
|
return $this->error(__('论坛系统未启用', 'aksrt'));
|
|
}
|
|
|
|
$actions = [];
|
|
if (isset($params['sticky'])) {
|
|
$actions[] = ['action' => 'sticky', 'value' => !empty($params['sticky'])];
|
|
}
|
|
if (isset($params['featured'])) {
|
|
$actions[] = ['action' => 'featured', 'value' => !empty($params['featured'])];
|
|
}
|
|
if (isset($params['locked'])) {
|
|
$actions[] = ['action' => 'locked', 'value' => !empty($params['locked'])];
|
|
}
|
|
|
|
foreach ($actions as $action) {
|
|
$result = $forum->moderate_topic($topic_id, $action['action'], ['value' => $action['value']], get_current_user_id());
|
|
if (is_wp_error($result)) {
|
|
return $this->error($result->get_error_message());
|
|
}
|
|
}
|
|
|
|
return $this->success(['message' => __('治理状态已更新', 'aksrt')]);
|
|
}
|
|
|
|
public function get_moderators(): WP_REST_Response {
|
|
$forum = $this->get_forum_service();
|
|
if (!$forum) {
|
|
return $this->success(['data' => ['boards' => []]]);
|
|
}
|
|
|
|
$board_id = isset($_GET['board_id']) ? (int) $_GET['board_id'] : 0;
|
|
$result = [];
|
|
foreach ($forum->get_boards() as $board) {
|
|
$current_board_id = (int) ($board['id'] ?? 0);
|
|
if ($board_id > 0 && $current_board_id !== $board_id) {
|
|
continue;
|
|
}
|
|
|
|
$result[] = [
|
|
'board_id' => $current_board_id,
|
|
'board_name' => (string) ($board['name'] ?? ''),
|
|
'topic_count' => (int) ($board['post_count'] ?? 0),
|
|
'pending_application_count' => (int) ($board['pending_application_count'] ?? 0),
|
|
'moderators' => $forum->get_board_moderator_profiles_with_roles($current_board_id),
|
|
];
|
|
}
|
|
|
|
return $this->success(['data' => ['boards' => $result]]);
|
|
}
|
|
|
|
public function update_moderators(WP_REST_Request $request): WP_REST_Response {
|
|
$forum = $this->get_forum_service();
|
|
if (!$forum) {
|
|
return $this->error(__('论坛系统未启用', 'aksrt'));
|
|
}
|
|
|
|
$params = $this->get_json_params($request);
|
|
$board_id = (int) ($params['board_id'] ?? 0);
|
|
$user_id = (int) ($params['user_id'] ?? 0);
|
|
$action = sanitize_key((string) ($params['action'] ?? ''));
|
|
$role = sanitize_key((string) ($params['role'] ?? 'moderator'));
|
|
|
|
if ($board_id <= 0 || $user_id <= 0 || !in_array($action, ['add', 'remove'], true)) {
|
|
return $this->error(__('参数无效', 'aksrt'));
|
|
}
|
|
|
|
$valid_roles = ['super_moderator', 'moderator', 'junior_moderator'];
|
|
if (!in_array($role, $valid_roles, true)) {
|
|
$role = 'moderator';
|
|
}
|
|
|
|
$board = get_term($board_id, 'aksrt_forum_board');
|
|
if (!$board || is_wp_error($board)) {
|
|
return $this->error(__('版块不存在', 'aksrt'), 404);
|
|
}
|
|
|
|
$user = get_userdata($user_id);
|
|
if (!$user) {
|
|
return $this->error(__('用户不存在', 'aksrt'), 404);
|
|
}
|
|
|
|
if ($action === 'add') {
|
|
$forum->add_moderator($user_id, $board_id, $role);
|
|
} else {
|
|
$forum->remove_moderator($user_id, $board_id);
|
|
}
|
|
|
|
return $this->success([
|
|
'message' => $action === 'add' ? __('版主已添加', 'aksrt') : __('版主已移除', 'aksrt'),
|
|
]);
|
|
}
|
|
|
|
public function change_moderator_role(WP_REST_Request $request): WP_REST_Response {
|
|
$forum = $this->get_forum_service();
|
|
if (!$forum) {
|
|
return $this->error(__('论坛系统未启用', 'aksrt'));
|
|
}
|
|
|
|
$params = $this->get_json_params($request);
|
|
$board_id = (int) ($params['board_id'] ?? 0);
|
|
$user_id = (int) ($params['user_id'] ?? 0);
|
|
$role = sanitize_key((string) ($params['role'] ?? ''));
|
|
|
|
if ($board_id <= 0 || $user_id <= 0 || !$role) {
|
|
return $this->error(__('参数无效', 'aksrt'));
|
|
}
|
|
|
|
$valid_roles = ['super_moderator', 'moderator', 'junior_moderator'];
|
|
if (!in_array($role, $valid_roles, true)) {
|
|
return $this->error(__('无效的角色', 'aksrt'));
|
|
}
|
|
|
|
$result = $forum->set_moderator_role($user_id, $board_id, $role);
|
|
if (!$result) {
|
|
return $this->error(__('设置角色失败,用户可能不是该版块版主', 'aksrt'));
|
|
}
|
|
|
|
return $this->success(['message' => __('角色已更新', 'aksrt')]);
|
|
}
|
|
|
|
public function apply_moderator(WP_REST_Request $request): WP_REST_Response {
|
|
$forum = $this->get_forum_service();
|
|
if (!$forum) {
|
|
return $this->error(__('论坛系统未启用', 'aksrt'));
|
|
}
|
|
|
|
$params = $this->get_json_params($request);
|
|
$board_id = (int) ($params['board_id'] ?? 0);
|
|
$reason = sanitize_textarea_field((string) ($params['reason'] ?? ''));
|
|
|
|
$result = $forum->apply_moderator(get_current_user_id(), $board_id, $reason);
|
|
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([
|
|
'message' => $result['message'] ?? __('申请已提交', 'aksrt'),
|
|
'data' => $result,
|
|
]);
|
|
}
|
|
|
|
public function get_moderator_applications(WP_REST_Request $request): WP_REST_Response {
|
|
$forum = $this->get_forum_service();
|
|
if (!$forum) {
|
|
return $this->success(['data' => ['items' => [], 'total' => 0]]);
|
|
}
|
|
|
|
$items = $forum->get_moderator_application_list([
|
|
'status' => sanitize_key((string) ($request->get_param('status') ?: '')),
|
|
'board_id' => (int) ($request->get_param('board_id') ?: 0),
|
|
'keyword' => sanitize_text_field((string) ($request->get_param('keyword') ?: '')),
|
|
]);
|
|
|
|
return $this->success([
|
|
'data' => [
|
|
'items' => $items,
|
|
'total' => count($items),
|
|
'options' => [
|
|
'boards' => $forum->get_boards(),
|
|
'status' => [
|
|
['label' => __('全部', 'aksrt'), 'value' => ''],
|
|
['label' => __('待处理', 'aksrt'), 'value' => AKSRT_Forum::APPLICATION_PENDING],
|
|
['label' => __('已通过', 'aksrt'), 'value' => AKSRT_Forum::APPLICATION_APPROVED],
|
|
['label' => __('已拒绝', 'aksrt'), 'value' => AKSRT_Forum::APPLICATION_REJECTED],
|
|
],
|
|
],
|
|
],
|
|
]);
|
|
}
|
|
}
|