Skip to content

Fixed: add missing $in_footer parameter to wp_enqueue/register_script calls#308

Merged
Dan0sz merged 7 commits into
developfrom
not_in_footer
Jun 10, 2026
Merged

Fixed: add missing $in_footer parameter to wp_enqueue/register_script calls#308
Dan0sz merged 7 commits into
developfrom
not_in_footer

Conversation

@Dan0sz

@Dan0sz Dan0sz commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Performance Improvements
    • Scripts for affiliate links, form submissions, and WooCommerce now load later in the page, reducing render-blocking and improving page responsiveness.
  • Tests
    • Integration tests updated and reordered to validate footer-loading behavior and ensure cleanup after assertions.
  • Chores
    • CI adjusted so coverage upload issues no longer fail the workflow.

@coderabbitai

coderabbitai Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 676b5ce8-fafe-4d98-90a9-4edacc5909f7

📥 Commits

Reviewing files that changed from the base of the PR and between 833bffa and 0cc6432.

📒 Files selected for processing (2)
  • .github/workflows/push.yml
  • tests/integration/AssetsTest.php

📝 Walkthrough

Walkthrough

Adds in_footer => true to three WordPress script enqueue/register calls (affiliate links, form submit, WooCommerce) and updates integration tests to assert footer output; CI Codecov upload step set to continue on error.

Changes

Script Loading Optimization

Layer / File(s) Summary
Defer integration scripts to footer
src/Assets.php, src/Integrations/FormSubmit.php, src/Integrations/WooCommerce.php
Affiliate links, form submission, and WooCommerce integration scripts now include in_footer => true in their wp_enqueue_script() / wp_register_script() calls, deferring them to the page footer.
Integration tests: capture footer output
tests/integration/AssetsTest.php
Reorder and adjust tests to buffer and fire wp_footer, assert affiliate-link script and related markers, and improve finally-block cleanup (remove filters, reset current user).

CI Cache Update

Layer / File(s) Summary
Allow Codecov upload failures
.github/workflows/push.yml
Codecov upload step now includes continue-on-error: true so coverage upload failures do not fail the CI job.

"A rabbit taps the keys with cheer,
scripts hop down to the footer near,
tests peek where the footers play,
CI shrugs and carries on its way,
I nibble bugs and twirl my ear."

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: adding the $in_footer parameter to wp_enqueue/register_script calls across multiple files, which is exactly what the changeset implements.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch not_in_footer

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/Assets.php`:
- Line 43: Two integration scripts (plausible-affiliate-links and
plausible-form-submit-integration) declare 'plausible-analytics' as a dependency
with in_footer => true which forces the main plausible-analytics script to the
footer and breaks inline DOMContentLoaded listeners; fix by either removing
in_footer => true from those integrations so they enqueue in the header (keep
plausible-analytics header behavior controlled by the
plausible_load_js_in_footer filter), or update the inline handlers (the
DOMContentLoaded listeners registered near the inline event handler blocks and
in the code that depends on plausible-analytics) to run immediately if
document.readyState is already interactive/complete (or use an immediate
execution guard) so they work when scripts load after DOMContentLoaded.
- Line 43: The test asserts the affiliate links script is printed during wp_head
but Assets enqueues 'plausible-affiliate-links' with in_footer => true, so
update the integration test (AssetsTest) to trigger and capture
do_action('wp_footer') instead of (or in addition to) do_action('wp_head') and
assert that the output contains plausible-affiliate-links.js and the
plausibleAffiliateLinks const; locate the enqueue call for
'plausible-affiliate-links' in the Assets class and the assertion/do_action
calls in AssetsTest to make the matching change.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0c6b5425-4836-4256-9fe1-8ff08fef3c92

📥 Commits

Reviewing files that changed from the base of the PR and between b33b395 and 833bffa.

📒 Files selected for processing (3)
  • src/Assets.php
  • src/Integrations/FormSubmit.php
  • src/Integrations/WooCommerce.php

Comment thread src/Assets.php
PLAUSIBLE_ANALYTICS_PLUGIN_URL . 'assets/dist/js/plausible-affiliate-links.js',
[ 'plausible-analytics' ],
filemtime( PLAUSIBLE_ANALYTICS_PLUGIN_DIR . 'assets/dist/js/plausible-affiliate-links.js' ),
[ 'in_footer' => true ],

@coderabbitai coderabbitai Bot Jun 10, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Cross-file root cause: Two integration scripts force the main analytics script to footer.

Both plausible-affiliate-links (src/Assets.php line 43) and plausible-form-submit-integration (src/Integrations/FormSubmit.php lines 61-62) declare 'plausible-analytics' as a dependency and set in_footer => true. WordPress dependency resolution moves the parent 'plausible-analytics' script to the footer when any dependent child is marked for footer placement.

This creates a timing hazard: the main script at src/Assets.php:104 defaults to header placement via the filter plausible_load_js_in_footer (default false), and several inline event handlers at lines 77, 172, and 204 register DOMContentLoaded listeners. When scripts execute in the footer (after DOM parsing), DOMContentLoaded has already fired, so these listeners never run.

Recommended fix: Either (1) keep integration scripts in the header to preserve current behavior, or (2) replace DOMContentLoaded listeners with immediate execution guards or alternative ready-state checks that work when scripts load after DOM is ready.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/Assets.php` at line 43, Two integration scripts
(plausible-affiliate-links and plausible-form-submit-integration) declare
'plausible-analytics' as a dependency with in_footer => true which forces the
main plausible-analytics script to the footer and breaks inline DOMContentLoaded
listeners; fix by either removing in_footer => true from those integrations so
they enqueue in the header (keep plausible-analytics header behavior controlled
by the plausible_load_js_in_footer filter), or update the inline handlers (the
DOMContentLoaded listeners registered near the inline event handler blocks and
in the code that depends on plausible-analytics) to run immediately if
document.readyState is already interactive/complete (or use an immediate
execution guard) so they work when scripts load after DOMContentLoaded.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check test expectations for script placement

echo "=== Checking test expectations for affiliate links script ==="
rg -n "plausible-affiliate-links" tests/ -B 3 -A 3

echo -e "\n=== Checking for wp_head vs wp_footer in tests ==="
rg -n "do_action.*wp_(head|footer)" tests/ -B 2 -A 2

Repository: plausible/wordpress

Length of output: 1028


Fix integration test to assert wp_footer output for in_footer enqueued affiliate links script.

src/Assets.php enqueues plausible-affiliate-links with ['in_footer' => true], but tests/integration/AssetsTest.php only calls do_action( 'wp_head' ) and asserts the output contains plausible-affiliate-links.js (and const plausibleAffiliateLinks). With in_footer => true, the script should be emitted during wp_footer, so update the test to trigger/capture do_action( 'wp_footer' ) (or adjust the assertions to match the new placement).

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/Assets.php` at line 43, The test asserts the affiliate links script is
printed during wp_head but Assets enqueues 'plausible-affiliate-links' with
in_footer => true, so update the integration test (AssetsTest) to trigger and
capture do_action('wp_footer') instead of (or in addition to)
do_action('wp_head') and assert that the output contains
plausible-affiliate-links.js and the plausibleAffiliateLinks const; locate the
enqueue call for 'plausible-affiliate-links' in the Assets class and the
assertion/do_action calls in AssetsTest to make the matching change.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're wrong there. Maybe that has changed in recent WP versions, but it's perfectly possible now to enqueue dependend script in the footer while the parent script remains in head.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧩 Analysis chain

🌐 Web query:

WordPress wp_enqueue_script in_footer dependency resolution: can a child script be in footer while parent script remains in header? WordPress version change

Length of output: 18679



@Dan0sz, you're absolutely right, and I apologize for the incorrect analysis. My understanding of WordPress dependency resolution was backwards.

WordPress promotes dependencies upward (from footer → header) to ensure they load before the script that depends on them — not downward. So when plausible-affiliate-links (child, in_footer => true) lists plausible-analytics as a dependency, WordPress leaves plausible-analytics in the header where it was registered and simply emits the child script in the footer afterward. The parent loads first (in <head>), the child loads later (in footer) — perfectly valid ordering.

My concern about DOMContentLoaded listeners breaking was also unfounded: those inline scripts are attached to plausible-analytics which remains in the header and fires normally.

Would you like me to resolve my two comments on this PR since they were based on an incorrect assumption?


✏️ Learnings added
Learnt from: Dan0sz
Repo: plausible/wordpress PR: 308
File: src/Assets.php:43-43
Timestamp: 2026-06-10T12:47:59.014Z
Learning: In WordPress, when a child script sets `in_footer => true` but its dependency (parent) is registered for the header, WordPress does NOT move the parent to the footer. WordPress dependency resolution only promotes dependencies upward (footer → header) to ensure they load before the dependent script. So a footer-placed child script can safely depend on a header-placed parent script — the parent stays in the header and the child loads in the footer afterward. Flagging this pattern as a "timing hazard" in the plausible/wordpress plugin (or any WordPress plugin) is incorrect.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


🧠 Learnings used
Learnt from: Dan0sz
Repo: plausible/wordpress PR: 289
File: src/Compatibility.php:271-271
Timestamp: 2026-03-25T12:57:52.477Z
Learning: In the plausible/wordpress plugin, it is acceptable to use `str_contains()`, `str_starts_with()`, and `str_ends_with()` even if this project declares a minimum PHP version below 8.0. The plugin’s WordPress requirement (WordPress 5.9+) provides polyfills for these functions on PHP < 8.0, so review should not flag these calls as incompatible with older PHP versions as long as the WordPress 5.9+ assumption remains true.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please. You were right about the test breaking, though. It should be fixed in #0cc6432.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, something went wrong! Please try again later. 🐰 💔

@codecov

codecov Bot commented Jun 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@Dan0sz Dan0sz merged commit 035dc70 into develop Jun 10, 2026
6 of 7 checks passed
@Dan0sz Dan0sz deleted the not_in_footer branch June 10, 2026 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants