1
0
Fork 0
mirror of https://github.com/Findus23/plugin-ExcludeCountries.git synced 2024-09-19 15:53:47 +02:00
plugin-ExcludeCountries/ExcludeCountries.php

80 lines
2.6 KiB
PHP
Raw Normal View History

2020-08-30 10:57:30 +02:00
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\ExcludeCountries;
use Piwik\Common;
use Piwik\Container\StaticContainer;
2020-08-30 12:29:03 +02:00
use Piwik\Piwik;
use Piwik\Plugin;
2020-08-30 10:57:30 +02:00
use Piwik\Plugins\UserCountry\LocationProvider;
2020-08-30 12:29:03 +02:00
use Piwik\Settings\Setting;
2020-08-30 10:57:30 +02:00
use Piwik\Tracker\Request;
2020-08-30 12:29:03 +02:00
class ExcludeCountries extends Plugin
2020-08-30 10:57:30 +02:00
{
2020-08-30 12:29:03 +02:00
/**
* inspired by plugins/UserCountry/Columns/Country.php
*/
public static function listCountries() {
$regionDataProvider = StaticContainer::get('Piwik\Intl\Data\Provider\RegionDataProvider');
$countryList = $regionDataProvider->getCountryList();
array_walk($countryList, function (&$item, $key) {
$item = Piwik::translate('Intl_Country_' . strtoupper($key));
});
asort($countryList); //order by localized name
return $countryList;
}
2020-08-30 10:57:30 +02:00
public function registerEvents() {
return [
"Tracker.isExcludedVisit" => "checkExcludedCountry",
];
}
public function checkExcludedCountry(&$excluded, Request $request) {
$logger = StaticContainer::getContainer()->get("Psr\Log\LoggerInterface");
$provider = LocationProvider::getProviderById(Common::getCurrentLocationProviderId());
2020-08-30 12:29:03 +02:00
$settings = new SystemSettings();
2020-08-30 10:57:30 +02:00
2020-09-04 13:35:42 +02:00
$location = $provider->getLocation(["ip" => $request->getIpString()]);
2020-08-30 12:29:03 +02:00
$countryCode = strtolower($location[LocationProvider::COUNTRY_CODE_KEY]);
if (!$location || empty($countryCode)) {
$logger->debug("ExcludeCountries could not detect a location");
return;
}
2020-08-30 12:29:03 +02:00
$excludeBool = $settings->excludeBool->getValue();
if ($excludeBool) {
$countries = $this->settingToCountryCodes($settings->excludedCountries);
if (in_array($countryCode, $countries)) {
$logger->debug("request excluded by ExcludeCountries plugin (" . $countryCode . ")");
$excluded = true;
}
} else {
$countries = $this->settingToCountryCodes($settings->includedCountries);
if (!in_array($countryCode, $countries)) {
$logger->debug("request excluded by ExcludeCountries plugin (" . $countryCode . ")");
$excluded = true;
}
}
}
private function settingToCountryCodes(Setting $setting) {
$codes = [];
foreach ($setting->getValue() as $value) {
if (!empty($value["country"])) {
$codes[] = $value["country"];
2020-08-30 21:07:13 +02:00
}
2020-08-30 10:57:30 +02:00
}
2020-08-30 12:29:03 +02:00
return $codes;
2020-08-30 10:57:30 +02:00
}
}