-
Notifications
You must be signed in to change notification settings - Fork 799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add/user tracking option api #9003
Changes from 2 commits
ca84ad0
7cf4d35
d8cbde6
5083991
8632994
445566c
54c100d
751568c
8a86e95
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
class Jetpack_User_Event_Tracking { | ||
|
||
private static $_cache = array(); | ||
const KEY = 'jetpack_event_tracking'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not PHP 5.2 safe.
|
||
|
||
static function is_enabled( $user_id ) { | ||
if ( isset( self::$_cache[ $user_id ] ) ) { | ||
return self::$_cache[ $user_id ]; | ||
} | ||
$user_tracking = get_user_meta( $user_id, self::KEY, true ); | ||
if ( ! is_numeric( $user_tracking ) ) { | ||
$user_tracking = self::default_value(); | ||
} | ||
self::$_cache[ $user_id ] = (bool) $user_tracking; | ||
return (bool) $user_tracking(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
} | ||
|
||
static function is_disabled( $user_id ) { | ||
return ! self::is_enabled( $user_id ); | ||
} | ||
|
||
static function disable( $user_id ) { | ||
// user opted out | ||
self::set( $user_id, 0 ); | ||
} | ||
|
||
static function enable( $user_id ) { | ||
// user opted in | ||
self::set( $user_id, 1 ); | ||
} | ||
|
||
static private function set( $user_id, $value ) { | ||
self::$_cache[ $user_id ] = (bool) $value; | ||
update_user_meta( $user_id, self::KEY, $value ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please update this to return the |
||
} | ||
|
||
static function default_value() { | ||
/** | ||
* Return the default jetpack user event tracking opt out value. | ||
* | ||
* @since 6.0.0 | ||
* | ||
* @param bool Default to false. (user tracking enabled) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
*/ | ||
return apply_filters( 'jetpack_event_tracking', true ); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
<file>php/test_class.jetpack-client-server.php</file> | ||
<file>php/test_class.jetpack-xmlrpc-server.php</file> | ||
<file>php/test_class.jetpack-heartbeat.php</file> | ||
<file>tests/php/test_class.jetpack-user-event-tracking.php</file> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check indentation. |
||
</testsuite> | ||
<testsuite name="core-api"> | ||
<directory phpVersion="5.6.0" phpVersionOperator=">=" prefix="test_" suffix=".php">tests/php/core-api</directory> | ||
|
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_opt_out', '__return_true' ); | ||
$user_id = $this->factory->user->create(); | ||
$this->assertFalse( Jetpack_User_Event_Tracking::is_enabled( $user_id ) ); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drive-by comment since I'm looking at this in concert with tracking on the Calypso side: what's the rationale behind having a local cache for this user meta? The user meta functions already leverage WP's built-in
wp_cache_*
functions, which also persist in memory for a session like this is doing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rational was that is would be faster. But I see your point and would be happy to remove it.