Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/InitOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] );
Expand All @@ -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
}

Expand Down Expand Up @@ -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();
Expand Down
31 changes: 18 additions & 13 deletions tests/integration/InitOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@

class InitOptionsTest extends TestCase {
/**
* @return void
* @see InitOptions::maybe_add_pageview_props()
* @return void
*/
public function testAddPageviewProps() {
try {
global $post;
global $post, $wp_query;
$old_post = $post;
$old_query = $wp_query;

$post_id = wp_insert_post(
$post_id = wp_insert_post(
[
'id' => 1,
'post_author' => 1,
'post_title' => 'Test',
'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' ] );

Expand All @@ -40,15 +44,16 @@ public function testAddPageviewProps() {
$this->assertArrayHasKey( 'category', $options['customProperties'] );
$this->assertEquals( 'Uncategorized', $options['customProperties']['category'] );
} finally {
$post = null;
$post = $old_post;
$wp_query = $old_query;
remove_filter( 'plausible_analytics_settings', [ $this, 'enablePageviewProps' ] );
}
}

/**
* @see InitOptions::maybe_add_proxy_options()
* @return void
* @throws \Exception
* @see InitOptions::maybe_add_proxy_options()
*/
public function testAddProxyOptions() {
try {
Expand All @@ -65,8 +70,8 @@ public function testAddProxyOptions() {
}

/**
* @return void
* @see InitOptions::maybe_exclude_pageview()
* @return void
*/
public function testExcludePageview() {
/**
Expand All @@ -76,7 +81,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' );
Expand All @@ -86,7 +91,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' );
Expand All @@ -105,7 +110,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' );
Expand All @@ -124,7 +129,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' );
Expand All @@ -138,8 +143,8 @@ public function testExcludePageview() {
}

/**
* @return void
* @see InitOptions::maybe_track_logged_in_users()
* @return void
*/
public function testTrackLoggedInUsers() {
try {
Expand Down
Loading