| 1 |
<?php |
| 2 |
|
| 3 |
namespace SimpleAnalytics; |
| 4 |
|
| 5 |
class TrackingPolicy |
| 6 |
{ |
| 7 |
public function shouldCollectAnalytics(): bool |
| 8 |
{ |
| 9 |
if (Setting::boolean(SettingName::ENABLED, true) === false) { |
| 10 |
return false; |
| 11 |
} |
| 12 |
|
| 13 |
if ($this->clientIpExcluded($_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'])) { |
| 14 |
return false; |
| 15 |
} |
| 16 |
|
| 17 |
if (! is_user_logged_in()) { |
| 18 |
return true; |
| 19 |
} |
| 20 |
|
| 21 |
return ! $this->containsExcludedRole(wp_get_current_user()->roles); |
| 22 |
} |
| 23 |
|
| 24 |
protected function clientIpExcluded(string $ip): bool |
| 25 |
{ |
| 26 |
return in_array($ip, Setting::array(SettingName::EXCLUDED_IP_ADDRESSES)); |
| 27 |
} |
| 28 |
|
| 29 |
protected function containsExcludedRole(array $roles): bool |
| 30 |
{ |
| 31 |
return array_intersect(Setting::array(SettingName::EXCLUDED_ROLES), $roles) !== []; |
| 32 |
} |
| 33 |
} |
| 34 |
|