-
Notifications
You must be signed in to change notification settings - Fork 799
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add/user tracking option api (#9003)
* Add User Event tracking Class interface * Added syncing of user tracking value as it changes as well as syncing the default value. Still need sync tests for this. * Add return to enable and disable methods. This was done to be able to return errors in case it doesn't work as expected. * Fixes typo * Update to use private statis instead of const since it is not PHP 5.2 safe * Fix static key and only send the new user data if it is set on the user level * Remove private cache since the values are already stored in build in cache * Update the multisite tests file * Fix the tests
- Loading branch information
1 parent
5080d7e
commit c9b754c
Showing
8 changed files
with
112 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
class Jetpack_User_Event_Tracking { | ||
public static $KEY = 'jetpack_event_tracking'; | ||
|
||
static function is_enabled( $user_id ) { | ||
$user_tracking = get_user_meta( $user_id, self::$KEY, true ); | ||
if ( ! is_numeric( $user_tracking ) ) { | ||
$user_tracking = self::default_value(); | ||
} | ||
return (bool) $user_tracking; | ||
} | ||
|
||
static function has_value( $user_id ) { | ||
$user_tracking = get_user_meta( $user_id, self::$KEY, true ); | ||
if ( is_numeric( $user_tracking ) ) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
static function is_disabled( $user_id ) { | ||
return ! self::is_enabled( $user_id ); | ||
} | ||
|
||
static function disable( $user_id ) { | ||
// user opted out | ||
return self::set( $user_id, 0 ); | ||
} | ||
|
||
static function enable( $user_id ) { | ||
// user opted in | ||
return self::set( $user_id, 1 ); | ||
} | ||
|
||
static private function set( $user_id, $value ) { | ||
return update_user_meta( $user_id, self::$KEY, $value ); | ||
} | ||
|
||
static function default_value() { | ||
/** | ||
* Return the default jetpack user event tracking opt out value. | ||
* | ||
* @since 6.0.0 | ||
* | ||
* @param bool Default to true. (user tracking enabled) | ||
*/ | ||
return apply_filters( 'jetpack_user_event_tracking', true ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
require dirname( __FILE__ ) . '/../../_inc/lib/class.jetpack-user-event-tracking.php'; | ||
|
||
class WP_Test_Jetpack_User_Event_Tracking extends WP_UnitTestCase { | ||
protected $user_id; | ||
|
||
public function setUp() { | ||
parent::setUp(); | ||
|
||
// create a user | ||
$this->user_id = $this->factory->user->create(); | ||
} | ||
|
||
public function test_default_value_is_disabled() { | ||
$this->assertFalse( Jetpack_User_Event_Tracking::is_disabled( $this->user_id ) ); | ||
$this->assertTrue( Jetpack_User_Event_Tracking::is_enabled( $this->user_id ) ); | ||
} | ||
|
||
public function test_enabeling_tracking() { | ||
Jetpack_User_Event_Tracking::enable( $this->user_id ); | ||
$this->assertTrue( Jetpack_User_Event_Tracking::is_enabled( $this->user_id ) ); | ||
} | ||
|
||
public function test_disabeling_tracking() { | ||
Jetpack_User_Event_Tracking::disable( $this->user_id ); | ||
$this->assertFalse( Jetpack_User_Event_Tracking::is_enabled( $this->user_id ) ); | ||
} | ||
|
||
public function test_filter_works_as_expected() { | ||
// by default opt out every user | ||
add_filter( 'jetpack_user_event_tracking', '__return_false' ); | ||
$user_id = $this->factory->user->create(); | ||
$this->assertFalse( Jetpack_User_Event_Tracking::is_enabled( $user_id ) ); | ||
} | ||
} |