mirror of
https://github.com/Findus23/plugin-ExcludeCountries.git
synced 2024-09-19 15:53:47 +02:00
add Settings
This commit is contained in:
parent
15e0aa2884
commit
09f5f09818
2 changed files with 116 additions and 5 deletions
|
@ -10,11 +10,27 @@ namespace Piwik\Plugins\ExcludeCountries;
|
|||
|
||||
use Piwik\Common;
|
||||
use Piwik\Container\StaticContainer;
|
||||
use Piwik\Piwik;
|
||||
use Piwik\Plugin;
|
||||
use Piwik\Plugins\UserCountry\LocationProvider;
|
||||
use Piwik\Settings\Setting;
|
||||
use Piwik\Tracker\Request;
|
||||
|
||||
class ExcludeCountries extends \Piwik\Plugin
|
||||
class ExcludeCountries extends Plugin
|
||||
{
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
public function registerEvents() {
|
||||
return [
|
||||
"Tracker.isExcludedVisit" => "checkExcludedCountry",
|
||||
|
@ -24,13 +40,35 @@ class ExcludeCountries extends \Piwik\Plugin
|
|||
public function checkExcludedCountry(&$excluded, Request $request) {
|
||||
$logger = StaticContainer::getContainer()->get("Psr\Log\LoggerInterface");
|
||||
$provider = LocationProvider::getProviderById(Common::getCurrentLocationProviderId());
|
||||
$settings = new SystemSettings();
|
||||
|
||||
|
||||
$location = $provider->getLocation(["ip" => $request->getIp()]);
|
||||
$countryCode = $location[LocationProvider::COUNTRY_CODE_KEY];
|
||||
if (strtolower($countryCode) !== "de") {
|
||||
$logger->debug("request excluded by ExcludeCountries plugin (" . $countryCode . ")");
|
||||
$excluded = true;
|
||||
$countryCode = strtolower($location[LocationProvider::COUNTRY_CODE_KEY]);
|
||||
|
||||
$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) {
|
||||
$codes[] = $value["languageCode"];
|
||||
}
|
||||
return $codes;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
73
SystemSettings.php
Normal file
73
SystemSettings.php
Normal file
|
@ -0,0 +1,73 @@
|
|||
<?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\Settings\FieldConfig;
|
||||
use Piwik\Settings\Setting;
|
||||
|
||||
/**
|
||||
* Defines Settings for ExcludeCountries.
|
||||
*
|
||||
* Usage like this:
|
||||
* $settings = new SystemSettings();
|
||||
* $settings->metric->getValue();
|
||||
* $settings->description->getValue();
|
||||
*/
|
||||
class SystemSettings extends \Piwik\Settings\Plugin\SystemSettings
|
||||
{
|
||||
/** @var Setting */
|
||||
public $excludeBool;
|
||||
|
||||
/** @var Setting */
|
||||
public $excludedCountries;
|
||||
|
||||
/** @var Setting */
|
||||
public $includedCountries;
|
||||
|
||||
protected function init() {
|
||||
$this->excludeBool = $this->createExcludeBoolSetting();
|
||||
$this->excludedCountries = $this->createExcludedCountriesSetting();
|
||||
$this->includedCountries = $this->createIncludedCountriesSetting();
|
||||
|
||||
}
|
||||
|
||||
private function createExcludeBoolSetting() {
|
||||
return $this->makeSetting('excludeBool', $default = true, FieldConfig::TYPE_BOOL, function (FieldConfig $field) {
|
||||
$field->title = 'Exclude Countries';
|
||||
$field->uiControl = FieldConfig::UI_CONTROL_CHECKBOX;
|
||||
$field->description = 'If enabled, visitors from the selected countries are not tracked. If disabled, only visitors from the selected countries are tracked';
|
||||
});
|
||||
}
|
||||
|
||||
private function createExcludedCountriesSetting() {
|
||||
return $this->makeSetting('excludedCountries', [], FieldConfig::TYPE_ARRAY, function (FieldConfig $field) {
|
||||
$countries = ExcludeCountries::listCountries();
|
||||
$field->title = 'Excluded Countries';
|
||||
$field->description = "Don't track users from these countries.";
|
||||
$field->uiControl = FieldConfig::UI_CONTROL_MULTI_TUPLE;
|
||||
$field1 = new FieldConfig\MultiPair("Country", 'country', FieldConfig::UI_CONTROL_SINGLE_SELECT);
|
||||
$field1->availableValues = $countries;
|
||||
$field->uiControlAttributes['field1'] = $field1->toArray();
|
||||
$field->condition = "excludeBool == true";
|
||||
});
|
||||
}
|
||||
|
||||
private function createIncludedCountriesSetting() {
|
||||
return $this->makeSetting('includedCountries', [], FieldConfig::TYPE_ARRAY, function (FieldConfig $field) {
|
||||
$countries = ExcludeCountries::listCountries();
|
||||
$field->title = 'Included Countries';
|
||||
$field->description = "Only track users from these countries.";
|
||||
$field->uiControl = FieldConfig::UI_CONTROL_MULTI_TUPLE;
|
||||
$field1 = new FieldConfig\MultiPair("Country", 'country', FieldConfig::UI_CONTROL_SINGLE_SELECT);
|
||||
$field1->availableValues = $countries;
|
||||
$field->uiControlAttributes['field1'] = $field1->toArray();
|
||||
$field->condition = "excludeBool == false";
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue