baseUrl . $endpoint; if ($params) { $url .= '?' . http_build_query($params); } return $this->request('GET', $url); } public function post(string $endpoint, array $data = []): array { return $this->request('POST', $this->baseUrl . $endpoint, $data); } public function put(string $endpoint, array $data = []): array { return $this->request('PUT', $this->baseUrl . $endpoint, $data); } public function patch(string $endpoint, array $data = []): array { return $this->request('PATCH', $this->baseUrl . $endpoint, $data); } public function delete(string $endpoint): array { return $this->request('DELETE', $this->baseUrl . $endpoint); } private function request(string $method, string $url, ?array $data = null): array { $args = [ 'method' => $method, 'timeout' => 30, 'headers' => [ 'Authorization' => 'Bearer ' . $this->token, 'Accept' => 'application/json', 'Content-Type' => 'application/json', ], ]; if ($data !== null) { $args['body'] = wp_json_encode($data); } $response = wp_remote_request($url, $args); if (is_wp_error($response)) { throw new \RuntimeException(esc_html($response->get_error_message())); } $code = wp_remote_retrieve_response_code($response); $body = json_decode(wp_remote_retrieve_body($response), true) ?? []; if ($code >= 400) { $message = $body['message'] ?? "HTTP {$code}"; throw new \RuntimeException(esc_html($message), (int) $code); } return $body; } public function setBaseUrl(string $url): void { $this->baseUrl = rtrim($url, '/'); } }