Geolocation IP detection and API speed
-
Hi @joeleem0n,
I need this plugin to be able to use geo-localization with a CDN, a library or an API (but that is well cached). I’ve checked multiple times your implementation with NewRelic to understand what was slowing down the site and as you can see from my previous posts the APIs of this plugin were to blame.
The profiling requires tests on a production site and it’s time consuming.
I think that if you want to re-invent the wheel you need to know what you’re doing (even if you’re using a fast api it still needs to be properly cached, etc) and it’s better to leave functionalities separate and concentrate on your product.
I saw that now it’s using api.ipstack.com but it would be best to have the possibility to choose.
The implementation that I’m proposing is about changing two functions and it’s not disruptive in case you’re choosing to not use geoip-detect plugin.
To have it published in your plugin would avoid me each time to make a manual changes in the code on each update of the plugin.
Here we go, it’s on the file
public/class-warehouse-popups-woocommerce-public.php
and this is the first function:private static function auto_warehouse_switch() { global $SHIPPING_ZONES_ENABLED; $found_warehouse = false; if (isset($_SERVER['HTTP_GEOIP_COUNTRY_CODE'])) { $geo_country_code = trim(strtoupper($_SERVER['HTTP_GEOIP_COUNTRY_CODE'])); } else if (function_exists('geoip_detect2_get_info_from_current_ip')) { $geoInfo = geoip_detect2_get_info_from_current_ip(); $geo_country_code = ($geoInfo->country->isoCode); //$geo_postal_code=($geoInfo->postal->code); } else { $client_data = self::client_data(); $geo_country_code = $client_data['country_code1']; /*if (function_exists('geoip_detect2_get_info_from_current_ip')) { $geoInfo = geoip_detect2_get_info_from_current_ip(); $geo_country_code = ($geoInfo->country->isoCode); //$geo_postal_code=($geoInfo->postal->code); } else { $geo_url = 'https://www.geoplugin.net/json.gp?ip=' . $_SERVER['REMOTE_ADDR']; $geo_json = self::curl_get_contents($geo_url); $geo_object = json_decode($geo_json, true); $geo_country_code = trim(strtoupper($geo_object['geoplugin_countryCode'])); }*/ } (code continues..)
As you can see by comparing with the original function I’m just adding this:
else if (function_exists('geoip_detect2_get_info_from_current_ip')) { $geoInfo = geoip_detect2_get_info_from_current_ip(); $geo_country_code = ($geoInfo->country->isoCode); //$geo_postal_code=($geoInfo->postal->code); }
The other function is:
function get_geoip_data(){ if (is_admin()) { return false; } define("GEOIP_DETECT_IPV6_SUPPORTED", ""); // Just to remove annoying php warning if (function_exists('geoip_detect2_get_info_from_current_ip')) { $geoInfo = geoip_detect2_get_info_from_current_ip(); $geo_country_code = $geoInfo->country->isoCode; $geo_country_name = $geoInfo->country->name; //ChromePhp::log("Country name: $geo_country_name, country code: $geo_country_code"); $obj['name'] = $geo_country_name; $obj['alpha2'] = $geo_country_code; } else { $ip = self::get_client_ip(); $cache_data = self::_wmw_get_data_from_cache( $ip ); if( $cache_data ){ $obj = json_decode( $cache_data, true ); }else{ $geoip_url = "https://api.ipgeolocationapi.com/geolocate/" . $ip; $json = self::curl_get_contents($geoip_url); $obj = json_decode( $json, true ); self::_wmw_add_data_to_cache( $json, $ip ); } } $return_arr = array( 'name' => $obj['name'], 'country_code1' => $obj['alpha2'], 'country_code2' => $obj['alpha3'] ); return $return_arr; }
Where I’m just adding the call to the geoip-detect plugin in case it exist:
if (function_exists('geoip_detect2_get_info_from_current_ip')) { $geoInfo = geoip_detect2_get_info_from_current_ip(); $geo_country_code = $geoInfo->country->isoCode; $geo_country_name = $geoInfo->country->name; //ChromePhp::log("Country name: $geo_country_name, country code: $geo_country_code"); $obj['name'] = $geo_country_name; $obj['alpha2'] = $geo_country_code; }
This would make the plugin more professional as sites that are served with a Content Delivery Network (CDN) won’t need to do any extra API calls.
When can we expect to have this pushed in the code?
- The topic ‘Geolocation IP detection and API speed’ is closed to new replies.