options = get_option( 'ip-blocker-pro_settings', [] ); } public function fetch_location( $ip ) { $provider = $this->options['geolocation_provider'] ?? 'ip-api.com'; switch ($provider) { case 'ipinfo.io': return $this->fetch_from_ipinfo($ip); case 'ipapi.com': return $this->fetch_from_ipapi($ip); case 'ipstack.com': return $this->fetch_from_ipstack($ip); case 'geoiplookup.net': return $this->fetch_from_geoiplookup($ip); default: return $this->fetch_from_ip_api($ip); } } private function fetch_from_ip_api( $ip ) { $api_key = $this->options['api_key_ip_apicom'] ?? ''; $protocol = ! empty( $api_key ) ? 'https' : 'http'; $url = "{$protocol}://ip-api.com/json/{$ip}?fields=status,message,country,countryCode,regionName,city,lat,lon,isp,org"; if ( $protocol === 'https' && ! empty( $api_key ) ) { $url .= '&key=' . esc_attr( $api_key ); } $response = wp_remote_get( $url ); if ( is_wp_error( $response ) ) { return null; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( isset( $body['status'] ) && $body['status'] === 'success' ) { return [ 'country' => $body['country'] ?? '', 'country_code' => $body['countryCode'] ?? '', 'region' => $body['regionName'] ?? '', 'city' => $body['city'] ?? '', 'lat' => $body['lat'] ?? '', 'lon' => $body['lon'] ?? '', 'isp' => $body['isp'] ?? ($body['org'] ?? ''), ]; } return null; } private function fetch_from_geoiplookup($ip) { $url = "https://json.geoiplookup.io/{$ip}"; $response = wp_remote_get($url); if (is_wp_error($response)) { return null; } $body = json_decode(wp_remote_retrieve_body($response), true); if (!empty($body['country_name'])) { return [ 'country' => $body['country_name'] ?? '', 'country_code' => $body['country_code'] ?? '', 'region' => $body['region'] ?? '', 'city' => $body['city'] ?? '', 'lat' => $body['latitude'] ?? '', 'lon' => $body['longitude'] ?? '', 'isp' => $body['isp'] ?? '', ]; } return null; } private function fetch_from_ipinfo($ip) { $api_key = $this->options['api_key_ipinfocom'] ?? ''; if(empty($api_key)) return null; $url = "https://ipinfo.io/{$ip}?token=" . esc_attr($api_key); $response = wp_remote_get($url); if (is_wp_error($response)) { return null; } $body = json_decode(wp_remote_retrieve_body($response), true); if (!empty($body['city']) && isset($body['loc'])) { list($lat, $lon) = explode(',', $body['loc']); return [ 'country' => $body['country'] ?? '', 'country_code' => $body['country'] ?? '', 'region' => $body['region'] ?? '', 'city' => $body['city'] ?? '', 'lat' => $lat, 'lon' => $lon, 'isp' => $body['org'] ?? '', ]; } return null; } private function fetch_from_ipapi($ip) { $api_key = $this->options['api_key_ipapicom'] ?? ''; if(empty($api_key)) return null; $url = "http://api.ipapi.com/api/{$ip}?access_key=" . esc_attr($api_key); $response = wp_remote_get($url); if (is_wp_error($response)) { return null; } $body = json_decode(wp_remote_retrieve_body($response), true); if (!isset($body['error'])) { return [ 'country' => $body['country_name'] ?? '', 'country_code' => $body['country_code'] ?? '', 'region' => $body['region_name'] ?? '', 'city' => $body['city'] ?? '', 'lat' => $body['latitude'] ?? '', 'lon' => $body['longitude'] ?? '', 'isp' => $body['connection']['isp'] ?? '', ]; } return null; } private function fetch_from_ipstack($ip) { $api_key = $this->options['api_key_ipstackcom'] ?? ''; if(empty($api_key)) return null; $url = "http://api.ipstack.com/{$ip}?access_key=" . esc_attr($api_key); $response = wp_remote_get($url); if (is_wp_error($response)) { return null; } $body = json_decode(wp_remote_retrieve_body($response), true); if (!isset($body['error'])) { return [ 'country' => $body['country_name'] ?? '', 'country_code' => $body['country_code'] ?? '', 'region' => $body['region_name'] ?? '', 'city' => $body['city'] ?? '', 'lat' => $body['latitude'] ?? '', 'lon' => $body['longitude'] ?? '', 'isp' => $body['connection']['isp'] ?? '', ]; } return null; } }