Files
wordpress-theme-aksrt/src/AKSRT/Admin/UserControllerFollowOperations.php
T
kate522 aaf458af1c refactor(autoload): migrate theme classes to Composer PSR-4
Move 288 types from global AKSRT_* + inc/classes manual requires into
namespace AKSRT\{Domain} under src/AKSRT/, wire composer.json psr-4,
and slim bootstrap to helpers/modules/hooks only. Split multi-class
widget/gateway files, update admin contract audit paths, and document
the layout in ADR/README.
2026-07-17 13:29:22 +08:00

90 lines
2.8 KiB
PHP

<?php
/**
* @package AKSRT
*/
namespace AKSRT\Admin;
defined('ABSPATH') || exit;
trait UserControllerFollowOperations {
/**
* POST /follow/toggle — 切换关注状态
*/
public function follow_toggle(\WP_REST_Request $request): \WP_REST_Response {
if (!class_exists('\AKSRT\User\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\User\Follow::get_instance()->toggle($current_user_id, $target_user_id);
return $this->success([
'data' => [
'is_following' => $is_following,
'follower_count' => \AKSRT\User\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\User\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\User\Follow::get_instance()->is_following($current_user_id, $target_user_id);
return $this->success([
'data' => [
'is_following' => $is_following,
'follower_count' => \AKSRT\User\Follow::get_instance()->get_follower_count($target_user_id),
'following_count' => \AKSRT\User\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\User\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\User\Follow::get_instance()->get_follower_count($user_id),
'following' => \AKSRT\User\Follow::get_instance()->get_following_count($user_id),
],
]);
}
}