Last active
July 26, 2018 13:55
-
-
Save ddur/c350c864aa720fd2d57ac393acf6266c to your computer and use it in GitHub Desktop.
IP Geo API provider class for ip-geo-block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (class_exists ('IP_Geo_Block_API', FALSE)) { | |
/** Class for Nginx with Http GeoIP Module | |
* @see http://nginx.org/en/docs/http/ngx_http_geoip_module.html | |
* @see https://gist.github.com/gjuric/fc418ab81a56b63a633e | |
*/ | |
class IP_Geo_Block_API_NginxGeoIP extends IP_Geo_Block_API { | |
public function get_location ($ip, $args = array()) { | |
if ($ip !== $_SERVER['REMOTE_ADDR']) { | |
return array ( | |
'errorMessage' => 'Service not available. Please select another provider.', | |
); | |
} | |
return array( | |
'countryCode' => isset ($_SERVER['GEOIP_COUNTRY_CODE']) ? $_SERVER['GEOIP_COUNTRY_CODE'] : 'ZZ', | |
'countryName' => isset ($_SERVER['GEOIP_COUNTRY_NAME']) ? $_SERVER['GEOIP_COUNTRY_NAME'] : '', | |
'regionName' => isset ($_SERVER['GEOIP_REGION_NAME']) ? $_SERVER['GEOIP_REGION_NAME'] : '', | |
'cityName' => isset ($_SERVER['GEOIP_CITY']) ? $_SERVER['GEOIP_CITY'] : '', | |
'latitude' => isset ($_SERVER['GEOIP_LATITUDE']) ? $_SERVER['GEOIP_LATITUDE'] : '', | |
'longitude' => isset ($_SERVER['GEOIP_LONGITUDE']) ? $_SERVER['GEOIP_LONGITUDE'] : '', | |
); | |
} | |
public function get_attribution () { return NULL; } | |
public function download (&$db, $args) { return FALSE; } | |
public function add_settings_field ($field, $section, $option_slug, $option_name, $options, $callback, $str_path, $str_last) {} | |
} | |
/** Register API */ | |
IP_Geo_Block_Provider::register_addon ( | |
array ( | |
'Nginx+GeoIP' => array ( | |
'key' => NULL, | |
'type' => 'IPv4, IPv6', | |
'link' => '<a href="http://nginx.org/en/docs/http/ngx_http_geoip_module.html" title="Nginx ngx_http_geoip_module (Maxmind)" rel=noreferrer target=_blank>Nginx ngx_http_geoip_module (Maxmind)</a> (IPv4, IPv6)', | |
), | |
) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment