Skip to content

Commit

Permalink
check for excluded request if not using optimized endpoint. relates to
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Dec 3, 2024
1 parent f5af9c3 commit 701177d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 28 deletions.
31 changes: 3 additions & 28 deletions src/class-script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,12 @@ public function __construct()
public function maybe_enqueue_script(bool $echo = false)
{
$load_script = apply_filters('koko_analytics_load_tracking_script', true);
if (false === $load_script) {
if (! $load_script) {
return;
}

// Do not load script if excluding current user by role
$settings = get_settings();
if (count($settings['exclude_user_roles']) > 0) {
$user = wp_get_current_user();

if ($user instanceof WP_User && $user->exists() && $this->user_has_roles($user, $settings['exclude_user_roles'])) {
return;
}
}

// Do not load script if excluded by IP address
if (count($settings['exclude_ip_addresses']) > 0) {
$ip_address = get_client_ip();
if ($ip_address !== '' && in_array($ip_address, $settings['exclude_ip_addresses'], true)) {
return;
}
if (is_request_excluded()) {
return;
}

// TODO: Handle "term" requests so we track both terms and post types.
Expand Down Expand Up @@ -161,15 +147,4 @@ public function add_async_attribute($tag, $handle)

return str_replace(' src=', ' defer src=', $tag);
}

public function user_has_roles(WP_User $user, array $roles): bool
{
foreach ($user->roles as $user_role) {
if (in_array($user_role, $roles, true)) {
return true;
}
}

return false;
}
}
41 changes: 41 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ function collect_request()
return;
}

// if WordPress environment is loaded, check if request is excluded
// TODO: come up with a way to check for excluded request without WordPress
if (\defined('ABSPATH') && is_request_excluded()) {
return;
}

if (isset($_GET['e'])) {
$data = extract_event_data($_GET);
} else {
Expand Down Expand Up @@ -419,3 +425,38 @@ function percent_format_i18n($pct): string
$formatted = \number_format_i18n($pct * 100, 0);
return $prefix . $formatted . '%';
}

function is_request_excluded(): bool
{
$settings = get_settings();

// check if exclude by logged-in user role
if (count($settings['exclude_user_roles']) > 0) {
$user = wp_get_current_user();

if ($user instanceof \WP_User && $user->exists() && user_has_roles($user, $settings['exclude_user_roles'])) {
return true;
}
}

// check if excluded by IP address
if (count($settings['exclude_ip_addresses']) > 0) {
$ip_address = get_client_ip();
if ($ip_address !== '' && in_array($ip_address, $settings['exclude_ip_addresses'], true)) {
return true;
}
}

return false;
}

function user_has_roles(\WP_User $user, array $roles): bool
{
foreach ($user->roles as $user_role) {
if (in_array($user_role, $roles, true)) {
return true;
}
}

return false;
}

0 comments on commit 701177d

Please sign in to comment.