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

90 lines
2.8 KiB
PHP

<?php
/**
* AKSRT admin follow operations support.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
trait AKSRT_Admin_User_Controller_Follow_Operations {
/**
* POST /follow/toggle — 切换关注状态
*/
public function follow_toggle(WP_REST_Request $request): WP_REST_Response {
if (!class_exists('AKSRT_Follow')) {
return $this->error('关注系统未启用', 500);
}
$params = $this->get_json_params($request);
$target_user_id = (int) ($params['target_user_id'] ?? 0);
$current_user_id = get_current_user_id();
if ($target_user_id <= 0 || $target_user_id === $current_user_id) {
return $this->error('参数无效');
}
$target_user = get_userdata($target_user_id);
if (!$target_user) {
return $this->error('用户不存在', 404);
}
$is_following = AKSRT_Follow::get_instance()->toggle($current_user_id, $target_user_id);
return $this->success([
'data' => [
'is_following' => $is_following,
'follower_count' => AKSRT_Follow::get_instance()->get_follower_count($target_user_id),
],
]);
}
/**
* GET /follow/status — 查询关注状态
*/
public function follow_status(WP_REST_Request $request): WP_REST_Response {
if (!class_exists('AKSRT_Follow')) {
return $this->success(['data' => ['is_following' => false]]);
}
$target_user_id = (int) $request->get_param('target_user_id');
$current_user_id = get_current_user_id();
if ($target_user_id <= 0) {
return $this->error('参数无效');
}
$is_following = AKSRT_Follow::get_instance()->is_following($current_user_id, $target_user_id);
return $this->success([
'data' => [
'is_following' => $is_following,
'follower_count' => AKSRT_Follow::get_instance()->get_follower_count($target_user_id),
'following_count' => AKSRT_Follow::get_instance()->get_following_count($target_user_id),
],
]);
}
/**
* GET /follow/counts — 获取关注/粉丝数
*/
public function follow_counts(WP_REST_Request $request): WP_REST_Response {
if (!class_exists('AKSRT_Follow')) {
return $this->success(['data' => ['followers' => 0, 'following' => 0]]);
}
$user_id = (int) $request->get_param('user_id');
if ($user_id <= 0) {
return $this->error('参数无效');
}
return $this->success([
'data' => [
'followers' => AKSRT_Follow::get_instance()->get_follower_count($user_id),
'following' => AKSRT_Follow::get_instance()->get_following_count($user_id),
],
]);
}
}