# simpleanalytics/1.46/src/TrackingPolicy.php

Simple Analytics, version 1.46. 34 lines.

- Page: https://pluginprobe.com/plugins/simpleanalytics/1.46/code/src/TrackingPolicy.php
- Raw: https://pluginprobe.com/plugins/simpleanalytics/1.46/raw/src/TrackingPolicy.php
- Modified: 2024-11-18T08:04:32+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/simpleanalytics/1.46/code/src/TrackingPolicy.php#L10-L20`.

```php
<?php

namespace SimpleAnalytics;

class TrackingPolicy
{
    public function shouldCollectAnalytics(): bool
    {
        if (Setting::boolean(SettingName::ENABLED, true) === false) {
            return false;
        }

        if ($this->clientIpExcluded($_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'])) {
            return false;
        }

        if (! is_user_logged_in()) {
            return true;
        }

        return ! $this->containsExcludedRole(wp_get_current_user()->roles);
    }

    protected function clientIpExcluded(string $ip): bool
    {
        return in_array($ip, Setting::array(SettingName::EXCLUDED_IP_ADDRESSES));
    }

    protected function containsExcludedRole(array $roles): bool
    {
        return array_intersect(Setting::array(SettingName::EXCLUDED_ROLES), $roles) !== [];
    }
}

```
