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
50 changes: 50 additions & 0 deletions _inc/lib/class.jetpack-user-event-tracking.php
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 );
}
}
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
10 changes: 9 additions & 1 deletion sync/class.jetpack-sync-module-users.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ public function expand_user( $user ) {
$user->locale = get_user_locale( $user->ID );
}
}

jetpack_require_lib( 'class.jetpack-user-event-tracking' );
if ( Jetpack_User_Event_Tracking::has_value( $user->ID ) ) {
$user->jetpack_tracking_enabled = Jetpack_User_Event_Tracking::is_enabled( $user->ID );
}
return $user;
}

Expand Down Expand Up @@ -286,6 +289,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
15 changes: 8 additions & 7 deletions tests/php.multisite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
</php>
<testsuites>
<testsuite name="general">
<file>php/test_class.jetpack.php</file>
<file>php/test_class.functions.compat.php</file>
<file>php/test_class.jetpack-network.php</file>
<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>
</testsuite>
<file>php/test_class.jetpack.php</file>
<file>php/test_class.functions.compat.php</file>
<file>php/test_class.jetpack-network.php</file>
<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>php/test_class.jetpack-user-event-tracking.php</file>
</testsuite>
<testsuite name="core-api">
<directory phpVersion="5.6.0" phpVersionOperator=">=" prefix="test_" suffix=".php">tests/php/core-api</directory>
</testsuite>
Expand Down
1 change: 1 addition & 0 deletions tests/php/sync/test_class.jetpack-sync-callables.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function test_sync_callable_whitelist() {
'site_icon_url' => Jetpack_Sync_Functions::site_icon_url(),
'shortcodes' => Jetpack_Sync_Functions::get_shortcodes(),
'roles' => Jetpack_Sync_Functions::roles(),
'default_user_event_tracking' => Jetpack_Sync_Functions::get_default_user_tracking_value()
);

if ( function_exists( 'wp_cache_is_enabled' ) ) {
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', '__return_false' );
$user_id = $this->factory->user->create();
$this->assertFalse( Jetpack_User_Event_Tracking::is_enabled( $user_id ) );
}
}