$email, 'password' => $password, 'client_id' => $client_id, ); $response = $this->post(self::API_BASE . '/woocomerce/login', $payload); if (is_wp_error($response)) { return $response; } if (!isset($response['token']) || empty($response['token'])) { return new WP_Error('threeplnext_invalid_response', __('Invalid response from 3plnext API.', '3plnext')); } return $response['token']; } public function register_store(string $token, array $store_data) { $headers = array( 'Authorization' => 'Bearer ' . $token, ); return $this->post(self::API_BASE . '/woocomerce/store_register', $store_data, $headers); } public function disconnect_store(string $token, string $store_url, string $client_id) { $headers = array( 'Authorization' => 'Bearer ' . $token, ); $payload = array( 'store_url' => $store_url, 'client_id' => $client_id, ); return $this->post(self::API_BASE . '/woocomerce/store_disconnect', $payload, $headers); } private function post(string $url, array $body, array $headers = array()) { $args = array( 'method' => 'POST', 'timeout' => 15, 'headers' => array_merge( array('Content-Type' => 'application/json'), $headers ), 'body' => wp_json_encode($body), 'data_format' => 'body', ); $response = wp_remote_post($url, $args); if (is_wp_error($response)) { return $response; } $code = wp_remote_retrieve_response_code($response); $raw = wp_remote_retrieve_body($response); if ($code < 200 || $code >= 300) { /* translators: %s: HTTP status code returned by the 3plnext API. */ return new WP_Error('threeplnext_http_error', sprintf(__('3plnext API error: %s', '3plnext'), (string) $code)); } $decoded = json_decode($raw, true); if (!is_array($decoded)) { return new WP_Error('threeplnext_invalid_json', __('3plnext API returned invalid JSON.', '3plnext')); } return $decoded; } }