Completed
Push — add/nosara-tracks ( 481cb6...757671 )
by
unknown
17:43 queued 08:58
created

class.jetpack-tracks.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Nosara Tracks for Jetpack
4
 */
5
6
require_once( dirname( __FILE__ ) . '/_inc/lib/tracks/client.php' );
7
8
class JetpackTracking {
9
	static $product_name = 'jetpack';
10
11
	static function track_jetpack_usage() {
12
		if ( ! Jetpack::is_active() ) {
13
			return;
14
		}
15
16
		add_action( 'jetpack_pre_activate_module',   array( __CLASS__, 'track_activate_module'), 1, 1 );
17
		add_action( 'jetpack_pre_deactivate_module', array( __CLASS__, 'track_deactivate_module'), 1, 1 );
18
		add_action( 'jetpack_user_authorized',       array( __CLASS__, 'track_user_linked' ) );
19
	}
20
21
	/* User has linked their account */
22
	static function track_user_linked() {
23
		$user_id = get_current_user_id();
24
		$anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true );
25
26
		if ( $anon_id ) {
27
			self::record_user_event( '_aliasUser', array( 'anonId' => $anon_id ) );
28
			delete_user_meta( $user_id, 'jetpack_tracks_anon_id' );
29
			if ( ! headers_sent() ) {
30
				setcookie( 'tk_ai', 'expired', time() - 1000 );
31
			}
32
		}
33
34
		$wpcom_user_data = Jetpack::get_connected_user_data( $user_id );
35
		update_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'] );
36
37
		self::record_user_event( 'user_linked', array() );
38
	}
39
40
	/* Activated module */
41
	static function track_activate_module( $module ) {
42
		self::record_user_event( 'module_activated', array( 'module' => $module ) );
43
	}
44
45
	/* Deactivated module */
46
	static function track_deactivate_module( $module ) {
47
		self::record_user_event( 'module_deactivated', array( 'module' => $module ) );
48
	}
49
50
	static function record_user_event( $event_type, $data ) {
51
52
		$user = wp_get_current_user();
53
		$site_url = get_option( 'siteurl' );
54
55
		$data['_via_ua']  = $_SERVER['HTTP_USER_AGENT'];
56
		$data['_via_ip']  = $_SERVER['REMOTE_ADDR'];
57
		$data['_lg']      = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
58
		$data['blog_url'] = $site_url;
59
		$data['blod_id']  = Jetpack_Options::get_option( 'id' );
60
61
		$top_level_events = array( '_aliasUser' );
62
63
		$event_name = self::$product_name . '_' . $event_type;
64
65
		if ( in_array( $event_type, $top_level_events ) ) {
66
			$event_name = $event_type;
67
		}
68
69
		$data['jetpack_version'] = defined( 'JETPACK__VERSION' ) ? JETPACK__VERSION : '0';
70
71
		$response = tracks_record_event( $user, $event_name, $data );
72
73
		if ( is_wp_error( $response ) ) {
74
			error_log( "There was an error: ".$response->get_error_message() );
0 ignored issues
show
The method get_error_message cannot be called on $response (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
75
		}
76
	}
77
}
78
79
add_action( 'init',  array( 'JetpackTracking', 'track_jetpack_usage' ) );
80