Skip to content
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

Merged
merged 9 commits into from
Mar 23, 2018
49 changes: 49 additions & 0 deletions _inc/lib/class.jetpack-user-event-tracking.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

class Jetpack_User_Event_Tracking {

private static $_cache = array();
Copy link
Contributor

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.

Copy link
Member Author

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.

private static $KEY = 'jetpack_event_tracking';

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;
}

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 ) {
self::$_cache[ $user_id ] = (bool) $value;
return update_user_meta( $user_id, self::$KEY, $value );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you casting this as boolean when you save it to the cache, but not when you update the user meta data?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I want to be able to distinguish between a set value (0,1) vs the default value (false)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks!

}

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)
Copy link
Contributor

@eliorivero eliorivero Mar 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Isn't the default true?

  2. Update this line to

* @param bool $enable_tracking Whether to enable user tracking. Defaults to true, which allows user tracking.

*/
return apply_filters( 'jetpack_event_tracking', true );
}
}
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<file>tests/php/test_class.jetpack-constants.php</file>
<file>tests/php/test_class.jetpack-connection-banner.php</file>
<file>tests/php/test_class.jetpack-options.php</file>
<file>tests/php/test_class.jetpack-user-event-tracking.php</file>
</testsuite>
<testsuite name="php-lint">
<file phpVersion="5.3.0" phpVersionOperator="lte">tests/php/test_php-lint.php</file>
Expand Down
3 changes: 2 additions & 1 deletion sync/class.jetpack-sync-defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ public static function get_constants_whitelist() {
'hosting_provider' => array( 'Jetpack_Sync_Functions', 'get_hosting_provider' ),
'locale' => 'get_locale',
'site_icon_url' => array( 'Jetpack_Sync_Functions', 'site_icon_url' ),
'roles' => array( 'Jetpack_Sync_Functions', 'roles' ),
'roles' => array( 'Jetpack_Sync_Functions', 'roles' ),
'default_user_event_tracking' => array( 'Jetpack_Sync_functions', 'get_default_user_tracking_value' )
);


Expand Down
5 changes: 5 additions & 0 deletions sync/class.jetpack-sync-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public static function get_shortcodes() {
return array_keys( $shortcode_tags );
}

public static function get_default_user_tracking_value() {
jetpack_require_lib( 'class.jetpack-user-event-tracking' );
return Jetpack_User_Event_Tracking::default_value();
}

/**
* Removes any callback data since we will not be able to process it on our side anyways.
*/
Expand Down
7 changes: 7 additions & 0 deletions sync/class.jetpack-sync-module-users.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ public function expand_user( $user ) {
$user->locale = get_user_locale( $user->ID );
}
}
jetpack_require_lib( 'class.jetpack-user-event-tracking' );
$user->jetpack_tracking_enabled = Jetpack_User_Event_Tracking::is_enabled( $user->ID );

return $user;
}
Expand Down Expand Up @@ -286,6 +288,11 @@ function maybe_save_user_meta( $meta_id, $user_id, $meta_key, $value ) {
$this->add_flags( $user_id, array( 'locale_changed' => true ) );
}

jetpack_require_lib( 'class.jetpack-user-event-tracking' );
if ( $meta_key === Jetpack_User_Event_Tracking::KEY ) {
$this->add_flags( $user_id, array( 'tracking_opt_out_changed' => true ) );
}

$user = get_user_by( 'id', $user_id );
if ( $meta_key === $user->cap_key ) {
$this->add_flags( $user_id, array( 'capabilities_changed' => true ) );
Expand Down
1 change: 1 addition & 0 deletions tests/php.multisite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Copy link
Contributor

Choose a reason for hiding this comment

The 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>
Expand Down
36 changes: 36 additions & 0 deletions tests/php/test_class.jetpack-user-event-tracking.php
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 ) );
}
}