api_key = get_option('geoscale_api_key'); } public function search($country, $query, $lang = '') { $args = [ 'country' => $country, 'q' => $query, ]; if ($lang) { $args['lang'] = $lang; } $url = add_query_arg($args, self::BASE_URL); return $this->request($url); } public function select($id, $lang = '') { $url = trailingslashit(self::BASE_URL) . rawurlencode($id); if ($lang) { $url = add_query_arg('lang', $lang, $url); } return $this->request($url); } private function request($url) { if (!$this->api_key) { return new \WP_Error( 'geoscale_missing_api_key', __('GeoScale API key is not configured.', 'address-autocomplete-geoscale'), ['status' => 500] ); } $response = wp_remote_get($url, [ 'timeout' => 3, 'headers' => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $this->api_key, ], ]); if (is_wp_error($response) || 200 !== wp_remote_retrieve_response_code($response)) { return new \WP_Error( 'geoscale_api_error', __('The GeoScale Address API request failed.', 'address-autocomplete-geoscale'), ['status' => 502] ); } return json_decode( wp_remote_retrieve_body($response), true ); } }