From 9564a78b10cfeccff49a2b26223ef61df3776bdb Mon Sep 17 00:00:00 2001 From: Daan van den Bergh <18595395+Dan0sz@users.noreply.github.com> Date: Tue, 5 May 2026 14:15:03 +0200 Subject: [PATCH 1/4] Fixed: only load author/category pageview props on posts. --- src/InitOptions.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/InitOptions.php b/src/InitOptions.php index a72c2239..9f78ecbd 100644 --- a/src/InitOptions.php +++ b/src/InitOptions.php @@ -16,9 +16,9 @@ class InitOptions { /** * Constructor. * - * @return void * @since 1.0.0 * @access public + * @return void */ public function __construct() { add_filter( 'plausible_analytics_init_options', [ $this, 'maybe_add_pageview_props' ] ); @@ -43,7 +43,7 @@ public function maybe_add_pageview_props( $options = [] ) { global $post; - if ( ! $post instanceof WP_Post ) { + if ( ! $post instanceof WP_Post || ! is_single() ) { return $options; // @codeCoverageIgnore } @@ -164,11 +164,11 @@ private function url_matches_patterns( $url, $patterns ) { /** * Adds a custom parameter User Logged In if Custom Properties is enabled. * + * @since v2.4.0 + * * @param $options * * @return array - * @since v2.4.0 - * */ public function maybe_track_logged_in_users( $options = [] ) { $settings = Helpers::get_settings(); From 5ae017319f8ea0d54b29cf6ec3f5f005f7f5b429 Mon Sep 17 00:00:00 2001 From: Daan van den Bergh <18595395+Dan0sz@users.noreply.github.com> Date: Tue, 5 May 2026 14:45:18 +0200 Subject: [PATCH 2/4] Fixed test. --- tests/integration/InitOptionsTest.php | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/tests/integration/InitOptionsTest.php b/tests/integration/InitOptionsTest.php index ad09d871..c738ee6f 100644 --- a/tests/integration/InitOptionsTest.php +++ b/tests/integration/InitOptionsTest.php @@ -10,14 +10,14 @@ class InitOptionsTest extends TestCase { /** - * @return void * @see InitOptions::maybe_add_pageview_props() + * @return void */ public function testAddPageviewProps() { try { - global $post; + global $post, $wp_query; - $post_id = wp_insert_post( + $post_id = wp_insert_post( [ 'id' => 1, 'post_author' => 1, @@ -25,8 +25,10 @@ public function testAddPageviewProps() { 'post_content' => 'Test', ] ); - $test_post = get_post( $post_id ); - $post = $test_post; + $test_post = get_post( $post_id ); + $post = $test_post; + $wp_query = new \WP_Query(); + $wp_query->is_single = true; add_filter( 'plausible_analytics_settings', [ $this, 'enablePageviewProps' ] ); @@ -46,9 +48,9 @@ public function testAddPageviewProps() { } /** + * @see InitOptions::maybe_add_proxy_options() * @return void * @throws \Exception - * @see InitOptions::maybe_add_proxy_options() */ public function testAddProxyOptions() { try { @@ -65,8 +67,8 @@ public function testAddProxyOptions() { } /** - * @return void * @see InitOptions::maybe_exclude_pageview() + * @return void */ public function testExcludePageview() { /** @@ -76,7 +78,7 @@ public function testExcludePageview() { add_filter( 'plausible_analytics_settings', [ $this, 'setExcludePageview' ] ); $class = $this->getMockBuilder( InitOptions::class )->disableOriginalConstructor()->onlyMethods( [ - 'get_current_request' + 'get_current_request', ] )->getMock(); $class->method( 'get_current_request' )->willReturn( 'http://example.com/category/test' ); @@ -86,7 +88,7 @@ public function testExcludePageview() { $this->assertArrayNotHasKey( 'transformRequest', $options ); $class = $this->getMockBuilder( InitOptions::class )->disableOriginalConstructor()->onlyMethods( [ - 'get_current_request' + 'get_current_request', ] )->getMock(); $class->method( 'get_current_request' )->willReturn( 'http://test.example.com/test' ); @@ -105,7 +107,7 @@ public function testExcludePageview() { add_filter( 'plausible_analytics_settings', [ $this, 'setExcludePageviewEdgeCaseAsterisk' ] ); $class = $this->getMockBuilder( InitOptions::class )->disableOriginalConstructor()->onlyMethods( [ - 'get_current_request' + 'get_current_request', ] )->getMock(); $class->method( 'get_current_request' )->willReturn( 'http://example.com/test' ); @@ -124,7 +126,7 @@ public function testExcludePageview() { add_filter( 'plausible_analytics_settings', [ $this, 'setExcludePageviewEdgeCaseSpace' ] ); $class = $this->getMockBuilder( InitOptions::class )->disableOriginalConstructor()->onlyMethods( [ - 'get_current_request' + 'get_current_request', ] )->getMock(); $class->method( 'get_current_request' )->willReturn( 'http://example.com/test' ); @@ -138,8 +140,8 @@ public function testExcludePageview() { } /** - * @return void * @see InitOptions::maybe_track_logged_in_users() + * @return void */ public function testTrackLoggedInUsers() { try { From ad80a8e518e1e5eb4f0dc6c560376b19981cfc0f Mon Sep 17 00:00:00 2001 From: Daan van den Bergh <18595395+Dan0sz@users.noreply.github.com> Date: Tue, 5 May 2026 14:47:12 +0200 Subject: [PATCH 3/4] Fixed test. --- tests/integration/InitOptionsTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/integration/InitOptionsTest.php b/tests/integration/InitOptionsTest.php index c738ee6f..d69dc429 100644 --- a/tests/integration/InitOptionsTest.php +++ b/tests/integration/InitOptionsTest.php @@ -42,7 +42,8 @@ public function testAddPageviewProps() { $this->assertArrayHasKey( 'category', $options['customProperties'] ); $this->assertEquals( 'Uncategorized', $options['customProperties']['category'] ); } finally { - $post = null; + $post = null; + $wp_query = null; remove_filter( 'plausible_analytics_settings', [ $this, 'enablePageviewProps' ] ); } } From 09ac7f344657e5be513a1bf78f2dc2b396d448a4 Mon Sep 17 00:00:00 2001 From: Daan van den Bergh <18595395+Dan0sz@users.noreply.github.com> Date: Tue, 5 May 2026 14:53:50 +0200 Subject: [PATCH 4/4] Restore query and post after test. --- tests/integration/InitOptionsTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/integration/InitOptionsTest.php b/tests/integration/InitOptionsTest.php index d69dc429..e477bd3d 100644 --- a/tests/integration/InitOptionsTest.php +++ b/tests/integration/InitOptionsTest.php @@ -16,6 +16,8 @@ class InitOptionsTest extends TestCase { public function testAddPageviewProps() { try { global $post, $wp_query; + $old_post = $post; + $old_query = $wp_query; $post_id = wp_insert_post( [ @@ -42,8 +44,8 @@ public function testAddPageviewProps() { $this->assertArrayHasKey( 'category', $options['customProperties'] ); $this->assertEquals( 'Uncategorized', $options['customProperties']['category'] ); } finally { - $post = null; - $wp_query = null; + $post = $old_post; + $wp_query = $old_query; remove_filter( 'plausible_analytics_settings', [ $this, 'enablePageviewProps' ] ); } }