Skip to content

Commit ad5ddfe

Browse files
osbreadriaandotcom
authored andcommitted
fix: Handle IP address absence
1 parent 9f45642 commit ad5ddfe

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/TrackingPolicy.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,31 @@ class TrackingPolicy
66
{
77
public function shouldCollectAnalytics(): bool
88
{
9-
if ($this->clientIpExcluded($_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'])) {
9+
if ($this->isClientIpExcluded()) {
1010
return false;
1111
}
1212

1313
if (! is_user_logged_in()) {
1414
return true;
1515
}
1616

17-
return ! $this->containsExcludedRole(wp_get_current_user()->roles);
17+
return ! $this->containsExcludedRole();
1818
}
1919

20-
protected function clientIpExcluded(string $ip): bool
20+
protected function isClientIpExcluded(): bool
2121
{
22+
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'];
23+
24+
if (empty($ip)) return false;
25+
2226
return in_array($ip, Setting::array(SettingName::EXCLUDED_IP_ADDRESSES));
2327
}
2428

25-
protected function containsExcludedRole(array $roles): bool
29+
protected function containsExcludedRole(): bool
2630
{
27-
return array_intersect(Setting::array(SettingName::EXCLUDED_ROLES), $roles) !== [];
31+
$currentRoles = wp_get_current_user()->roles;
32+
$excludedRoles = Setting::array(SettingName::EXCLUDED_ROLES);
33+
34+
return array_intersect($excludedRoles, $currentRoles) !== [];
2835
}
2936
}

0 commit comments

Comments
 (0)