'Amazon AWS CloudFront', 'cloudflare' => 'Cloudflare', ); public function country($ip) { $r = array(); $isoCode = ''; switch ($this->options['provider']) { case 'aws': if (isset($_SERVER['CloudFront-Viewer-Country'])) { $isoCode = $_SERVER['CloudFront-Viewer-Country']; } break; case 'cloudflare'; if (isset($_SERVER["HTTP_CF_IPCOUNTRY"])) { $isoCode = $_SERVER["HTTP_CF_IPCOUNTRY"]; if ($isoCode == 'xx' /* not a country / unknown */) $isoCode = ''; } break; } $country = ''; if (!$isoCode) { return null; } $r['country']['iso_code'] = strtoupper($isoCode); $r['traits']['ip_address'] = $ip; $record = new \GeoIp2\Model\City($r, array('en')); return $record; } } class HeaderDataSource extends AbstractDataSource { public function getId() { return 'header'; } public function getLabel() { return __('Special Hosting Providers (Cloudflare, Amazon AWS CloudFront)', 'geoip-detect'); } public function getDescriptionHTML() { return __('These servers already do geodetection, but only of the visitor\'s country.', 'geoip-detect'); } public function getStatusInformationHTML() { $provider = get_option('geoip-detect-header-provider'); $html = ''; $link = ''; if ($provider == 'cloudflare') { $link = 'https://support.cloudflare.com/hc/en-us/articles/200168236-What-does-CloudFlare-IP-Geolocation-do-'; } elseif ($provider == 'aws') { $link = 'https://aws.amazon.com/blogs/aws/enhanced-cloudfront-customization/'; } if ($link) $html = sprintf(__('This needs to be enabled in the admin panel: see Help.', 'geoip-detect'), $link); return $html; } public function getParameterHTML() { $provider = get_option('geoip-detect-header-provider'); $checked_cloudflare = $provider == 'cloudflare' ? 'checked' : ''; $checked_aws = $provider == 'aws' ? 'checked' : ''; $label = __('Which Hosting Provider:', 'geoip-detect'); $html = <<$label


HTML; return $html; } public function saveParameters($post) { $message = ''; $value = isset($post['options_header']['provider']) ? $post['options_header']['provider'] : ''; if (!empty($value)) { update_option('geoip-detect-header-provider', $value); } return $message; } public function getShortLabel() { $provider = get_option('geoip-detect-header-provider'); $labels = array( '' => __('None', 'geoip-detect'), 'aws' => 'Amazon AWS CloudFront', 'cloudflare' => 'Cloudflare', ); if (!isset($labels[$provider])) $provider = ''; $html = __('Hosting Provider:', 'geoip-detect') . ' ' . $labels[$provider]; return $html; } public function getReader($locales = array('en'), $options = array()) { $reader = null; $provider = get_option('geoip-detect-header-provider'); if ($provider) { try { $reader = new HeaderReader( array( 'provider' => $provider, ) ); } catch ( \Exception $e ) { if (WP_DEBUG) echo printf(__('Error while creating reader for "%s": %s', 'geoip-detect'), $filename, $e->getMessage ()); } } return $reader; } public function isWorking() { $working = (bool) get_option('geoip-detect-header-provider'); return $working; } } geoip_detect2_register_source(new HeaderDataSource());