Skip to content

Ignore script tags without an id in the attributes filter - #60

Merged
adriaandotcom merged 1 commit into
mainfrom
fix/script-registry-id-warning
Nov 16, 2026
Merged

adriaandotcom merged 1 commit into
mainfrom
fix/script-registry-id-warning

Conversation

@simpleanalytics-agent

@simpleanalytics-agent simpleanalytics-agent commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #59

wp_script_attributes runs for every script tag printed through wp_get_script_tag(), including tags from themes and other plugins that have no id. The filter now treats a missing id as "not our script" and returns those attributes unchanged.

 addAttributesFilter(attributes)
+  id = attributes['id'] ?? null
   for each script
-    if script has attributes and handle-js === attributes['id']
+    if script has attributes and handle-js === id
       return attributes + script attributes
   return attributes

Adds tests/Regression/script-attributes.php, which the existing phpRegression.spec.ts picks up:

register AnalyticsScript, turn PHP warnings into exceptions
wp_get_script_tag(src only)                  → no warning, no data-platform
wp_get_script_tag(id: "simpleanalytics-js")  → contains data-platform="wordpress"

Evidence

Ran the new regression file in WordPress Playground (latest WordPress, PHP 8.3). Playground has no Docker requirement, but its SQLite database can't create the MySQL temporary table that tests/Support/isolated-options.php uses. For this local run only, a stub of that helper ran the closure directly. CI runs the real helper on MySQL.

  • Before: FAIL: ErrorException: Undefined array key "id" @ src/ScriptRegistry.php:57
    After: Regression checks passed

PHP 7.4 wasn't tested locally; ?? works in PHP 7.0 and later, and CI's WordPress/PHP matrix covers 7.4.

Security implications

  • No security impact
  • Has security impact - described as:

Merge Danger

Door: two-way

Blast Radius: minimal

Only affects tags without an id, which previously failed the comparison anyway (null never equals simpleanalytics-js). The attributes on our own script are unchanged.

Summary by CodeRabbit

  • Bug Fixes
    • Prevented errors when processing script tags without an id attribute. Matching registered scripts continue to receive their configured attributes.

Themes and other plugins can print script tags without an id, which made
the wp_script_attributes filter emit an "Undefined array key" warning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

Walkthrough

The script attribute filter now handles script tags without an id attribute. A regression test checks that these tags do not receive data-platform and that the registered script tag still receives it.

Changes: Script attribute handling

Layer / File(s) Summary
Missing-id handling
src/ScriptRegistry.php, tests/Regression/script-attributes.php
addAttributesFilter treats a missing id as null before matching registered scripts. The regression test checks tags without an id and verifies the registered script receives data-platform="wordpress".

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: osbre

Merge Risk: Minimal · up to 1648d

This fixes the PHP warning when a theme or another plugin prints a script tag without an id. The change is small and covered by a regression test, so merge risk is minimal.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@github-actions github-actions Bot added the change: routine Routine or ordinary change that needs at most one human or AI-assisted review label Nov 16, 2026
@github-actions

Copy link
Copy Markdown

SDLC label: change: routine because The change stops a PHP notice when WordPress prints script tags without an id, so unrelated scripts no longer hit an undefined-index warning, and it does not touch access control, visitor data collection, or the tracking output.

@adriaandotcom
adriaandotcom merged commit 61bdd48 into main Nov 16, 2026
12 of 13 checks passed
@adriaandotcom
adriaandotcom deleted the fix/script-registry-id-warning branch November 16, 2026 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change: routine Routine or ordinary change that needs at most one human or AI-assisted review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PHP warning "Undefined array key id" from ScriptRegistry

2 participants