Skip to content

Add wp_parsely_tracker_url filter#4108

Merged
acicovic merged 6 commits intodevelopfrom
copilot/fix-issue-4107
Apr 6, 2026
Merged

Add wp_parsely_tracker_url filter#4108
acicovic merged 6 commits intodevelopfrom
copilot/fix-issue-4107

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 1, 2026

Description

Adds a wp_parsely_tracker_url filter to get_tracker_url(), allowing publishers to programmatically override the Parse.ly tracker script URL.

Changes

  • src/class-parsely.php: Updated get_tracker_url() to apply the wp_parsely_tracker_url filter before returning the tracker URL
  • tests/Integration/OtherTest.php: New integration test covering filter override

Usage

add_filter( 'wp_parsely_tracker_url', function( string $url ): string {
    return 'https://my-first-party-cdn.example.com/p.js';
} );

Motivation and context

The tracker script URL is hardcoded to https://cdn.parsely.com/keys/{site_id}/p.js. Publishers serving the tracker from a first-party domain had no clean override path — they had to suppress the built-in tracker via wp_parsely_load_js_tracker and re-enqueue manually (replicating data-parsely-site, id="parsely-cfg", and loader script), or use the fragile script_loader_src filter.

Fixes #4107.

How has this been tested?

  • PHP syntax validation passes on all modified files
  • New integration test (test_get_tracker_url_filter) verifies the filter override works correctly
  • Existing test_get_tracker_url and test_get_tracker_no_site_id remain unchanged and unaffected

Summary by CodeRabbit

  • New Features

    • Introduced a filter hook that allows customization of the tracker URL used by the plugin.
  • Tests

    • Added integration test validating the tracker URL filter override behavior.

Copilot AI assigned Copilot and jblz Apr 1, 2026
@jblz jblz marked this pull request as ready for review April 1, 2026 19:05
@jblz jblz requested a review from a team as a code owner April 1, 2026 19:05
@jblz
Copy link
Copy Markdown
Contributor

jblz commented Apr 2, 2026

@copilot let's only use the filter approach for now. Please remove the option mechanism for overriding the tracker URL and corresponding UI changes. Update tests as appropriate.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 2, 2026

@copilot let's only use the filter approach for now. Please remove the option mechanism for overriding the tracker URL and corresponding UI changes. Update tests as appropriate.

Done in 4084030. Removed the tracker_url option, its PHPStan type, defaults, the settings page field/validation, and the two option-related tests. Only the wp_parsely_tracker_url filter in get_tracker_url() and its integration test remain.

Copilot AI changed the title Add wp_parsely_tracker_url filter and Custom Tracker URL setting Add wp_parsely_tracker_url filter Apr 2, 2026
Copilot AI requested a review from jblz April 2, 2026 13:53
Copilot finished work on behalf of jblz April 2, 2026 13:54
@acicovic acicovic added this to the 3.23.0 milestone Apr 6, 2026
@acicovic acicovic added the Changelog: Added PR to be added under the changelog's "Added" section label Apr 6, 2026
@acicovic
Copy link
Copy Markdown
Collaborator

acicovic commented Apr 6, 2026

I've switched esc_url() to esc_url_raw(), since get_tracker_url() feeds into wp_register_script(), the REST API, and GraphQL, none of which expect HTML-encoded output. esc_url() would double-encode & to & in any filter-supplied URL with query parameters.

Although this isn't a usage we expect, I've made the switch as a failsafe for any case.

@acicovic
Copy link
Copy Markdown
Collaborator

acicovic commented Apr 6, 2026

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 6, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 6, 2026

📝 Walkthrough

Walkthrough

This PR adds a wp_parsely_tracker_url filter to Parsely::get_tracker_url() allowing publishers to override the tracker script URL dynamically. The computed default tracker URL is passed through the filter, then re-sanitized before being returned.

Changes

Cohort / File(s) Summary
Filter Implementation
src/class-parsely.php
Added wp_parsely_tracker_url filter to get_tracker_url() method, enabling publishers to override the default CDN tracker URL. Sanitization changed from esc_url() to esc_url_raw() after applying the filter.
Test Coverage
tests/Integration/OtherTest.php
Added test_get_tracker_url_filter() integration test verifying that the wp_parsely_tracker_url filter successfully overrides the default tracker URL when registered.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

Feature: PCI

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary change: adding a new WordPress filter for the tracker URL.
Description check ✅ Passed The description covers all required template sections with detailed explanation of changes, motivation, and testing approach.
Linked Issues check ✅ Passed The PR fully implements the code-based filter solution from issue #4107, including the filter registration, integration test, and proper URL escaping with esc_url_raw().
Out of Scope Changes check ✅ Passed All changes are directly scoped to the filter implementation requirements; no unrelated modifications to other functionality or systems.
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 copilot/fix-issue-4107

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 PHPStan (2.1.46)

Invalid configuration:
Unexpected item 'parameters › type_coverage'.


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.

Copy link
Copy Markdown
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.

🧹 Nitpick comments (1)
tests/Integration/OtherTest.php (1)

416-428: Strengthen the filter test by asserting input and removing the hook after assertion.

This test proves override behavior, but it will be more deterministic if it also validates the incoming default URL and cleans up the callback.

Proposed test hardening patch.
 public function test_get_tracker_url_filter(): void {
 	self::set_options( array( 'apikey' => self::VALID_SITE_ID ) );
+	$expected_default_url = 'https://cdn.parsely.com/keys/' . self::VALID_SITE_ID . '/p.js';
 	$custom_url = 'https://my-first-party-cdn.example.com/p.js';
 
+	$callback = static function ( string $tracker_url ) use ( $custom_url, $expected_default_url ): string {
+		self::assertSame( $expected_default_url, $tracker_url );
+		return $custom_url;
+	};
+
 	add_filter(
 		'wp_parsely_tracker_url',
-		function () use ( $custom_url ): string {
-			return $custom_url;
-		}
+		$callback
 	);
 
 	self::assertSame( $custom_url, self::$parsely->get_tracker_url() );
+	remove_filter( 'wp_parsely_tracker_url', $callback );
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/Integration/OtherTest.php` around lines 416 - 428, The test
test_get_tracker_url_filter should validate the default URL passed into the
wp_parsely_tracker_url filter and clean up the hook afterwards: replace the
simple add_filter callback with a closure that accepts the incoming $default (or
$url) argument, assert that $default equals the expected default tracker URL
(derived from using self::VALID_SITE_ID or by calling the code that produces the
default), then return $custom_url; after calling
self::$parsely->get_tracker_url(), remove the filter (remove_filter for
'wp_parsely_tracker_url' with the same closure or a named callback) so the
filter does not leak into other tests.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@tests/Integration/OtherTest.php`:
- Around line 416-428: The test test_get_tracker_url_filter should validate the
default URL passed into the wp_parsely_tracker_url filter and clean up the hook
afterwards: replace the simple add_filter callback with a closure that accepts
the incoming $default (or $url) argument, assert that $default equals the
expected default tracker URL (derived from using self::VALID_SITE_ID or by
calling the code that produces the default), then return $custom_url; after
calling self::$parsely->get_tracker_url(), remove the filter (remove_filter for
'wp_parsely_tracker_url' with the same closure or a named callback) so the
filter does not leak into other tests.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 372e4cc2-3a92-43f6-88e0-26979e43ac1f

📥 Commits

Reviewing files that changed from the base of the PR and between a4bc623 and 21b63ec.

📒 Files selected for processing (2)
  • src/class-parsely.php
  • tests/Integration/OtherTest.php

@acicovic acicovic merged commit 42549d1 into develop Apr 6, 2026
36 checks passed
@acicovic acicovic deleted the copilot/fix-issue-4107 branch April 6, 2026 08:14
@jblz
Copy link
Copy Markdown
Contributor

jblz commented Apr 6, 2026

Thanks, @acicovic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: Added PR to be added under the changelog's "Added" section

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Add filter to load tracker from an alternative URL

3 participants