From 02323bce7a4dc3b70b6f43a7568ce424bfb5e090 Mon Sep 17 00:00:00 2001 From: Andrei Igna Date: Tue, 23 Oct 2018 12:34:50 +0200 Subject: [PATCH] Added - helper function for visitor Geo info --- src/functions.php | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/functions.php b/src/functions.php index 4453aba..4265850 100644 --- a/src/functions.php +++ b/src/functions.php @@ -9,3 +9,40 @@ function mf() { } } + + + +// Get visitor location - Country + +if (!function_exists('getVisitorCountryCode')) { + function getVisitorCountryCode(): string { + return strtoupper(apply_filters('visitor_country_code', '')); + } +} + +add_filter('user_country_code', 'layered_visitor_country_code_woocommerce'); +add_filter('user_country_code', 'layered_visitor_country_code_headers'); + +function layered_visitor_country_code_woocommerce(string $countryCode = ''): string { + if (empty($countryCode) && class_exists('WC_Geolocation')) { + $location = WC_Geolocation::geolocate_ip(); + if ($location['country'] && !in_array($location['country'], array('A1', 'A2', 'EU', 'AP'))) { + $countryCode = $location['country']; + } + } + + return $countryCode; +} + +function if_menu_user_country_code_headers(string $countryCode = ''): string { + if (empty($countryCode)) { + foreach (['HTTP_CF_IPCOUNTRY', 'X-AppEngine-country', 'CloudFront-Viewer-Country', 'GEOIP_COUNTRY_CODE', 'HTTP_X_COUNTRY_CODE', 'HTTP_X_GEO_COUNTRY'] as $key) { + if (isset($_SERVER[$key]) && $_SERVER[$key] && !in_array($_SERVER[$key], ['XX', 'ZZ', 'A1', 'A2', 'EU', 'AP'])) { + return $_SERVER[$key]; + } + } + } + + return $countryCode; +} +