(int) ($args['per_page'] ?? 20), 'paged' => (int) ($args['page'] ?? 1)]; if (!empty($args['role'])) $qArgs['role'] = $args['role']; if (!empty($args['search'])) $qArgs['search'] = '*' . $args['search'] . '*'; $query = new \WP_User_Query($qArgs); $users = []; foreach ($query->get_results() as $u) { $users[] = ['id' => $u->ID, 'username' => $u->user_login, 'email' => $u->user_email, 'name' => $u->display_name, 'roles' => $u->roles, 'registered' => $u->user_registered]; } return ['users' => $users, 'total' => $query->get_total()]; } public static function getUser(array $args): array { $user = get_userdata((int) $args['user_id']); if (!$user) throw new \RuntimeException("User not found: {$args['user_id']}"); return ['id' => $user->ID, 'username' => $user->user_login, 'email' => $user->user_email, 'first_name' => $user->first_name, 'last_name' => $user->last_name, 'display_name' => $user->display_name, 'roles' => $user->roles, 'registered' => $user->user_registered, 'capabilities' => array_keys(array_filter($user->allcaps))]; } public static function createUser(array $args): array { $userId = wp_create_user($args['username'], $args['password'], $args['email']); if (is_wp_error($userId)) throw new \RuntimeException($userId->get_error_message()); $user = new \WP_User($userId); if (!empty($args['role'])) $user->set_role($args['role']); if (!empty($args['first_name'])) update_user_meta($userId, 'first_name', $args['first_name']); if (!empty($args['last_name'])) update_user_meta($userId, 'last_name', $args['last_name']); return ['id' => $userId, 'username' => $args['username'], 'email' => $args['email']]; } public static function updateUser(array $args): array { $userId = (int) $args['user_id']; $data = ['ID' => $userId]; if (isset($args['email'])) $data['user_email'] = $args['email']; if (isset($args['password'])) $data['user_pass'] = $args['password']; $result = wp_update_user($data); if (is_wp_error($result)) throw new \RuntimeException($result->get_error_message()); if (!empty($args['first_name'])) update_user_meta($userId, 'first_name', $args['first_name']); if (!empty($args['last_name'])) update_user_meta($userId, 'last_name', $args['last_name']); if (!empty($args['role'])) { $user = new \WP_User($userId); $user->set_role($args['role']); } if (!empty($args['meta'])) foreach ($args['meta'] as $k => $v) update_user_meta($userId, $k, $v); return ['id' => $userId, 'updated' => true]; } public static function deleteUser(array $args): array { require_once ABSPATH . 'wp-admin/includes/user.php'; $userId = (int) $args['user_id']; $reassign = isset($args['reassign']) ? (int) $args['reassign'] : null; $result = wp_delete_user($userId, $reassign); if (!$result) throw new \RuntimeException("Failed to delete user: {$userId}"); return ['id' => $userId, 'deleted' => true]; } }