Skip to content

Improved: Updated JS snippet to make sure it's skipped by JS optimization plugins#289

Merged
Dan0sz merged 4 commits intodevelopfrom
update_js_snippet
Mar 25, 2026
Merged

Improved: Updated JS snippet to make sure it's skipped by JS optimization plugins#289
Dan0sz merged 4 commits intodevelopfrom
update_js_snippet

Conversation

@Dan0sz
Copy link
Collaborator

@Dan0sz Dan0sz commented Mar 25, 2026

Summary by CodeRabbit

  • New Features

    • Added support for excluding the Plausible script from minification and optimization processes.
  • Bug Fixes

    • Improved script registration and initialization mechanism for better performance.
  • Documentation

    • Updated method documentation with improved clarity and exception handling annotations.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 25, 2026

📝 Walkthrough

Walkthrough

The pull request modifies script registration and compatibility handling. The Assets class now registers the Plausible analytics script with a proper JS source URL instead of an empty source, streamlines the enqueue call, and adjusts the bootstrap logic. The Compatibility class adds a filter hook for minification exclusion and introduces a new method to append optimization-related attributes to the script tag.

Changes

Cohort / File(s) Summary
Script Registration
src/Assets.php
Modified maybe_enqueue_main_script() to register script with non-empty JS source URL via get_js_url(true) and simplified enqueue call. Adjusted inline bootstrap logic to define global plausible queue and initialization without dynamically inserting external script. Reordered PHPDoc annotations.
Compatibility Filters & Exclusions
src/Compatibility.php
Constructor now registers plausible_analytics_script_params filter. Added exclude_from_minification() method to append minification/optimization exclusion attributes (data-no-minify, data-no-optimize, data-noptimize). Replaced strpos() with str_contains() for REST endpoint detection. Updated multiple method docblocks with @throws Exception annotations and improved parameter documentation.

Poem

🐰 Scripts now register with proper URLs, so clean!
Minification filters keep optimizers serene,
Bootstrap bootstraps without extra load,
Plausible analytics smooths the code road!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: ensuring the JS snippet is skipped by optimization plugins through modifications to handle minification/optimization attributes.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ 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 update_js_snippet

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.

@codecov
Copy link

codecov bot commented Mar 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

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 the current code and only fix it if needed.

Inline comments:
In `@src/Assets.php`:
- Around line 44-47: Update the outdated docblock above the wp_register_script
call: replace the "dummy script" wording with a brief, accurate description that
this registers the Plausible analytics script with an actual URL (via
$this->get_js_url(true)) under the handle 'plausible-analytics' and respects the
plausible_load_js_in_footer filter for footer loading; reference the
wp_register_script invocation and the get_js_url(true) usage so future readers
understand this is a real external script registration, not a placeholder.

In `@src/Compatibility.php`:
- Line 271: Replace all uses of str_contains (e.g., str_contains($url,
$rest_endpoint)) with a PHP 7.2-compatible check using strpos !== false inside
the Compatibility class (or wherever those calls appear), e.g., change to
(strpos($url, $rest_endpoint) !== false); alternatively, if you decide to
require PHP 8.0+, update the plugin's declared minimum PHP version instead of
changing code—pick one approach and apply it consistently across all occurrences
of str_contains.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 15deff52-bbbe-4714-82b3-d02e991c13a9

📥 Commits

Reviewing files that changed from the base of the PR and between 9f7a7bb and a137b76.

📒 Files selected for processing (2)
  • src/Assets.php
  • src/Compatibility.php

@Dan0sz Dan0sz merged commit 6a29e39 into develop Mar 25, 2026
7 checks passed
@Dan0sz Dan0sz deleted the update_js_snippet branch March 25, 2026 12:57
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.

1 participant