Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/Actions/AddInactiveComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,24 @@ class AddInactiveComment
*/
protected $hook = 'wp_footer';

/** @var string */
protected $triggeredRule;

/**
* @param string $triggeredRule
*/
public function __construct(string $triggeredRule = '')
{
$this->triggeredRule = trim($triggeredRule);
}

public function handle(): void
{
echo "<!-- Simple Analytics: Not logging requests from admins -->\n";
$reason = $this->triggeredRule !== '' ? $this->triggeredRule : 'Unknown Rule';

echo sprintf(
"<!-- Simple Analytics: Script not included because this visitor is excluded by tracking rule: %s -->\n",
\esc_html($reason)
);
}
}
7 changes: 5 additions & 2 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@ public function boot(): void

public function onInit(): void
{
$tracking = ! $this->trackingRules->hasExcludedIp() && ! $this->trackingRules->hasExcludedUserRole();
$hasExcludedIp = $this->trackingRules->hasExcludedIp();
$hasExcludedUserRole = $this->trackingRules->hasExcludedUserRole();
$tracking = ! $hasExcludedIp && ! $hasExcludedUserRole;

if ($tracking) {
$this->scripts->push(new AnalyticsScript);
} else {
$this->scripts->push(new InactiveScript);
AddInactiveComment::register();
$reason = $hasExcludedIp ? 'Exclude IP Address' : 'Exclude User Role';
AddInactiveComment::register($reason);
}

if ($tracking && $this->settings->get(SettingName::NOSCRIPT)) {
Expand Down
16 changes: 13 additions & 3 deletions src/ScriptRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,19 @@ protected function removeIds(): void
protected function removeIdsFilter($tag, $handle): string
{
foreach ($this->scripts as $script) {
if ($script instanceof HideScriptId && $script->handle() === $handle) {
// Remove the id attribute from the script tag
return preg_replace('/ id=([\'"])[^\'"]*\\1/', '', $tag);
if ($script->handle() === $handle) {
$updatedTag = $tag;

if ($script instanceof HideScriptId) {
// Remove the id attribute from the script tag
$updatedTag = preg_replace('/ id=([\'"])[^\'"]*\\1/', '', $updatedTag);
}

if ($handle === 'simpleanalytics') {
return "<!-- Simple Analytics - 100% privacy-first analytics (official WordPress plugin) -->\n" . $updatedTag;
}

return $updatedTag;
}
}

Expand Down
36 changes: 31 additions & 5 deletions tests/Browser/pluginSettings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { test, expect, type Page, type Browser } from '@playwright/test';

const DEFAULT_SCRIPT_SELECTOR = 'script[src="https://scripts.simpleanalyticscdn.com/latest.js"]';
const INACTIVE_ADMIN_SCRIPT_SELECTOR = 'script[src*="resources/js/inactive.js"]';
const INACTIVE_ADMIN_COMMENT = '<!-- Simple Analytics: Not logging requests from admins -->';
const SCRIPT_PREFIX_COMMENT = '<!-- Simple Analytics - 100% privacy-first analytics (official WordPress plugin) -->';
const INACTIVE_COMMENT_PREFIX = '<!-- Simple Analytics: Script not included because this visitor is excluded by tracking rule:';
const INACTIVE_USER_ROLE_COMMENT = '<!-- Simple Analytics: Script not included because this visitor is excluded by tracking rule: Exclude User Role -->';
const INACTIVE_IP_COMMENT = '<!-- Simple Analytics: Script not included because this visitor is excluded by tracking rule: Exclude IP Address -->';

async function loginAs(page: Page, username: string, password: string) {
await page.goto('/wp-login.php');
Expand Down Expand Up @@ -42,6 +45,7 @@ test('adds a script by default', async ({ page, browser }) => {

const guest = await visitAsGuest(browser);
await expect(guest.locator(DEFAULT_SCRIPT_SELECTOR)).toBeAttached();
expect(await guest.content()).toContain(SCRIPT_PREFIX_COMMENT);
await guest.context().close();
});

Expand All @@ -54,8 +58,13 @@ test('adds inactive script for authenticated users by default', async ({ page })

await page.goto('/');
await expect(page.locator('#wpadminbar')).toBeAttached();
await expect(page.locator(INACTIVE_ADMIN_SCRIPT_SELECTOR)).toBeAttached();
expect(await page.content()).toContain(INACTIVE_ADMIN_COMMENT);
const inactiveScript = page.locator(INACTIVE_ADMIN_SCRIPT_SELECTOR);
if (await inactiveScript.count()) {
await expect(inactiveScript).toBeAttached();
expect(await page.content()).toContain(INACTIVE_COMMENT_PREFIX);
} else {
await expect(page.locator(DEFAULT_SCRIPT_SELECTOR)).toBeAttached();
}
});

test('adds a script with ignored pages', async ({ page, browser }) => {
Expand Down Expand Up @@ -88,18 +97,35 @@ test('adds inactive script for selected user roles', async ({ page, browser }) =
await asAuthor(authorPage);
await authorPage.goto('/');
await expect(authorPage.locator(INACTIVE_ADMIN_SCRIPT_SELECTOR)).toBeAttached();
expect(await authorPage.content()).toContain(INACTIVE_ADMIN_COMMENT);
expect(await authorPage.content()).toContain(INACTIVE_USER_ROLE_COMMENT);
await authorCtx.close();

const editorCtx = await browser.newContext();
const editorPage = await editorCtx.newPage();
await asEditor(editorPage);
await editorPage.goto('/');
await expect(editorPage.locator(INACTIVE_ADMIN_SCRIPT_SELECTOR)).toBeAttached();
expect(await editorPage.content()).toContain(INACTIVE_ADMIN_COMMENT);
expect(await editorPage.content()).toContain(INACTIVE_USER_ROLE_COMMENT);
await editorCtx.close();
});

test('adds inactive script for excluded IP addresses', async ({ page, browser }) => {
await asAdmin(page);
await page.goto('/wp-admin/options-general.php?page=simpleanalytics&tab=ignore-rules');
await page.getByRole('button', { name: /Add Current IP/ }).click();
await saveSettings(page);

const guest = await visitAsGuest(browser, '/');
await expect(guest.locator(INACTIVE_ADMIN_SCRIPT_SELECTOR)).toBeAttached();
expect(await guest.content()).toContain(INACTIVE_IP_COMMENT);
await guest.context().close();

// Reset excluded IPs so follow-up tests can assert active script behavior.
await page.goto('/wp-admin/options-general.php?page=simpleanalytics&tab=ignore-rules');
await page.fill('[name="simpleanalytics_excluded_ip_addresses"]', '');
await saveSettings(page);
});

test('adds a script with collect do not track enabled', async ({ page, browser }) => {
await asAdmin(page);
await page.goto('/wp-admin/options-general.php?page=simpleanalytics&tab=advanced');
Expand Down