Skip to content

feat: move inline heartbeat JS to a standalone enqueued script - #169

Open
i-am-chitti wants to merge 3 commits into
WordPress:mainfrom
i-am-chitti:issue-135
Open

feat: move inline heartbeat JS to a standalone enqueued script#169
i-am-chitti wants to merge 3 commits into
WordPress:mainfrom
i-am-chitti:issue-135

Conversation

@i-am-chitti

Copy link
Copy Markdown
Contributor

Description

Currently, the script handling heartbeat pings is outputted directly as a multi-line HEREDOC string inside wp_presence_enqueue_heartbeat_ping() via wp_add_inline_script(). This brings a few challenges:

  1. It shows up as an anonymous inline script in browser DevTools, making setting breakpoints or debugging in the Sources panel extremely difficult.
  2. The script cannot be cached independently by browsers.
  3. Code linters and formatters cannot easily lint or clean the script.

This PR addresses this by:

  • Moving the core JavaScript out of includes/heartbeat.php to a new standalone file assets/js/presence-ping.js.
  • Registering and enqueuing the script using wp_enqueue_script() with jquery and heartbeat as dependencies.
  • Passing the dynamic PHP-side configuration object wpPresenceConfig via wp_add_inline_script() attached to the script as a before dependency.

Fixes

Proposed Changes

  • assets/js/presence-ping.js
    • Created a new JS file containing the heartbeat presence check-in script logic.
  • includes/heartbeat.php
    • Replaced the multi-line HEREDOC wp_add_inline_script() block with wp_enqueue_script() pointing to the new standalone assets file.
    • Attached the configuration wpPresenceConfig to the script handle instead.
  • tests/test-heartbeat.php
    • Added test_wp_presence_enqueue_heartbeat_ping() unit test to verify that the script is enqueued and the inline configuration data is attached properly.

How to Test / Verification

Run the test suite in wp-env: npm run test
Verify that all unit tests pass successfully.

Use of AI Tools

AI assistance: Yes
Tool(s): Antigravity
Model(s): Gemini 3.5 Flash
Used for: Extracting inline HEREDOC JavaScript into assets/js/presence-ping.js, and writing unit tests to cover script enqueuing.

@i-am-chitti
i-am-chitti requested a review from josephfusco as a code owner July 30, 2026 17:06
@github-actions

Copy link
Copy Markdown
Contributor

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props iamchitti.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown
Contributor

▶ Preview in WordPress Playground

Open in WordPress Playground

Boots a fresh WordPress with this PR's presence-api build, seeds 5 demo users, and drops you on the dashboard.

Stress-test variant: 40 demo users · Built from aab2d3c085602a427c232df6dca344fe3567e962. Auto-updates when you push.

@codecov

codecov Bot commented Jul 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 31.08%. Comparing base (3a2ae99) to head (aab2d3c).
⚠️ Report is 26 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main     #169      +/-   ##
============================================
+ Coverage     28.56%   31.08%   +2.51%     
  Complexity      161      161              
============================================
  Files            15       15              
  Lines          1817     1821       +4     
============================================
+ Hits            519      566      +47     
+ Misses         1298     1255      -43     
Flag Coverage Δ
phpunit 31.08% <100.00%> (+2.51%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@josephfusco josephfusco added [Area] Heartbeat Issues for the heartbeat subsystem javascript Pull requests that update javascript code labels Jul 31, 2026
@josephfusco josephfusco changed the title Build: Move inline heartbeat JS to a standalone enqueued script refactor: move inline heartbeat JS to a standalone enqueued script Jul 31, 2026
@josephfusco josephfusco added the [Type] Feature A new feature within an existing module label Jul 31, 2026
@josephfusco josephfusco changed the title refactor: move inline heartbeat JS to a standalone enqueued script feat: move inline heartbeat JS to a standalone enqueued script Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Area] Heartbeat Issues for the heartbeat subsystem javascript Pull requests that update javascript code [Type] Feature A new feature within an existing module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants