* @implements MeasurableSettingInterface */ abstract class CompliancePolicy implements SystemSettingInterface, MeasurableSettingInterface, ConfigSettingInterface { /** * @use SystemSetterTrait */ use SystemSetterTrait; /** * @use MeasurableSetterTrait */ use MeasurableSetterTrait; /** * @use ConfigGetterTrait */ use ConfigGetterTrait; abstract public static function getName(): string; abstract public static function getTitle(): string; abstract protected static function generateDescription(): string; abstract protected static function generateWarnings(): string; public static function getDescription(): string { $description = static::generateDescription(); /** * This event is triggered while the description of a compliance policy is * being generated. The policy description can be modified via this event. * * @param string &$description of the policy. */ Piwik::postEvent('CompliancePolicy.updatePolicyDescription', [&$description, static::class]); $shouldShowWarnings = true; /** * This event is triggered while the description of a compliance policy is * being generated, and controls whether any warnings specific to the policy * are displayed at the end of the description. * * @param bool &$shouldShowWarnings set to false if the warnings should be hidden */ Piwik::postEvent('CompliancePolicy.shouldShowWarnings', [&$shouldShowWarnings, static::class]); if ($shouldShowWarnings) { $warnings = static::generateWarnings(); if (!empty($warnings)) { $description .= '
' . static::generateWarnings(); } } return $description; } /** * @return array> of [['title' => (string) 'TITLE', 'note' => (string) 'NOTE']] */ abstract public static function getUnknownSettings(): array; /** * @return array */ public static function getDetails(): array { return [ 'id' => static::getName(), 'title' => static::getTitle(), 'description' => static::getDescription(), ]; } protected static function getPluginManagerInstance(): Manager { return Manager::getInstance(); } protected static function getSystemDefaultValue() { return false; } protected static function getSystemName(): string { return preg_replace('/\s+/', '', static::getName()) . '_policy_enabled'; } protected static function getSystemType(): string { return FieldConfig::TYPE_BOOL; } protected static function getMeasurableDefaultValue() { return false; } protected static function getMeasurableName(): string { return preg_replace('/\s+/', '', static::getName()) . '_policy_enabled'; } protected static function getMeasurableType(): string { return FieldConfig::TYPE_BOOL; } protected static function getConfigSection(): string { return Piwik::getPluginNameOfMatomoClass(static::class); } protected static function getConfigSettingName(): string { return static::getSystemName(); } /** * If the policy is active at the instance level, * disabling the policy for a site will also disable it * for the instance. */ public static function setActiveStatus(?int $idSite, bool $isActive): void { if (isset($idSite)) { static::setMeasurableValue($idSite, $isActive); if (static::getSystemValue() && !$isActive) { static::setSystemValue($isActive); } } else { static::setSystemValue($isActive); } /** * This event is triggered when the status of a compliance policy changes, and * is to be used to perform extra actions when a policy is activated/deactivated. * * The status of a policy cannot be changed via this event. * * @param bool $isActive Whether the policy is being activated or deactivated * @param int|null $idSite * @param class-string The compliance policy in question */ Piwik::postEvent('CompliancePolicy.setActiveStatus', [$isActive, $idSite, static::class]); } /** * If the policy is active at the instance level, then * this function will return true for all sites. */ public static function isActive(?int $idSite): bool { $instanceLevel = static::getSystemValue(); if (!$instanceLevel && isset($idSite)) { return static::getMeasurableValue($idSite); } return $instanceLevel; } public static function isConfigControlled() { return !is_null(static::getConfigValue()); } }