diff options
author | Stefan Giehl <stefan@matomo.org> | 2020-02-27 10:23:17 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-27 10:23:17 +0300 |
commit | f28c7fa6cb6c63c8f459206448c7dcb93568099e (patch) | |
tree | 55168edfd9c5e2802bf6c0ace2fed4584cb79f52 /plugins/UserCountry/functions.php | |
parent | bcbdf42154ff40af6ce8276234ffbae19e0370d4 (diff) |
Removes GeoIp Legacy support (#15521)
* Removes GeoIp Legacy support
* move rendering provider configuration to the provider, to allow other providers to define an own one
* move related translations to GeoIp2 plugin
* Adds some UI tests
* Apply review feedback
Diffstat (limited to 'plugins/UserCountry/functions.php')
-rw-r--r-- | plugins/UserCountry/functions.php | 58 |
1 files changed, 49 insertions, 9 deletions
diff --git a/plugins/UserCountry/functions.php b/plugins/UserCountry/functions.php index 6c24afb44c..0061a4e518 100644 --- a/plugins/UserCountry/functions.php +++ b/plugins/UserCountry/functions.php @@ -10,11 +10,9 @@ namespace Piwik\Plugins\UserCountry; use Piwik\DataTable; -use Piwik\Option; use Piwik\Piwik; use Piwik\Plugin\Manager; use Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2; -use Piwik\Plugins\UserCountry\LocationProvider\GeoIp; use Piwik\Tracker\Visit; /** @@ -118,21 +116,63 @@ function getElementFromStringArray($label, $separator, $index, $emptyValue = fal */ function getRegionNameFromCodes($countryCode, $regionCode) { - if (!Manager::getInstance()->isPluginActivated('GeoIp2') || - LocationProvider::getCurrentProvider() instanceof GeoIp) { - return GeoIp::getRegionNameFromCodes($countryCode, $regionCode); - } - $name = GeoIp2::getRegionNameFromCodes($countryCode, $regionCode); - // fallback if no translation for with GeoIP2 + // fallback if no translation with GeoIP2 if ($name == Piwik::translate('General_Unknown')) { - $name = GeoIp::getRegionNameFromCodes($countryCode, $regionCode); + $name = getLegacyRegionNameFromCodes($countryCode, $regionCode); } return $name; } + +/** + * Returns a region name for a country code + region code. + * + * @param string $countryCode + * @param string $regionCode + * @return string The region name or 'Unknown' (translated). + */ +function getLegacyRegionNameFromCodes($countryCode, $regionCode) +{ + $regionNames = getRegionNames(); + + $countryCode = strtoupper($countryCode); + $regionCode = strtoupper($regionCode); + + // ensure tibet is shown as region of china + if ($countryCode == 'TI' && $regionCode == '1') { + $regionCode = '14'; + $countryCode = 'CN'; + } + + if (isset($regionNames[$countryCode][$regionCode])) { + return $regionNames[$countryCode][$regionCode]; + } else { + return Piwik::translate('General_Unknown'); + } +} + +/** + * Returns an array of region names mapped by country code & region code. + * + * @return array + */ +function getRegionNames() +{ + static $regionNames; + + if (is_null($regionNames)) { + $GEOIP_REGION_NAME = array(); + require_once PIWIK_INCLUDE_PATH . '/libs/MaxMindGeoIP/geoipregionvars.php'; + $regionNames = $GEOIP_REGION_NAME; + } + + return $regionNames; +} + + /** * Returns the region name using the label of a Visits by Region report. * |