Completed
Push — enhance/failed-auth-survey ( 62d2b0 )
by
unknown
09:57
created

class.jetpack-tracks.php (1 issue)

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';
0 ignored issues
show
The visibility should be declared for property $product_name.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
10
11
	static function track_jetpack_usage() {
12
		if ( ! Jetpack::is_active() ) {
13
			return;
14
		}
15
16
		// For tracking stuff via js/ajax
17
		add_action( 'admin_enqueue_scripts',         array( __CLASS__, 'enqueue_tracks_scripts' ) );
18
19
		add_action( 'jetpack_pre_activate_module',   array( __CLASS__, 'track_activate_module'), 1, 1 );
20
		add_action( 'jetpack_pre_deactivate_module', array( __CLASS__, 'track_deactivate_module'), 1, 1 );
21
		add_action( 'jetpack_user_authorized',       array( __CLASS__, 'track_user_linked' ) );
22
	}
23
24
	static function enqueue_tracks_scripts() {
25
		wp_enqueue_script( 'jptracks', plugins_url( '_inc/lib/tracks/tracks-ajax.js', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION, true );
26
		wp_localize_script( 'jptracks', 'jpTracksAJAX', array(
27
			'ajaxurl'            => admin_url( 'admin-ajax.php' ),
28
			'jpTracksAJAX_nonce' => wp_create_nonce( 'jp-tracks-ajax-nonce' ),
29
		) );
30
	}
31
32
	/* User has linked their account */
33
	static function track_user_linked() {
34
		$user_id = get_current_user_id();
35
		$anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true );
36
37
		if ( $anon_id ) {
38
			self::record_user_event( '_aliasUser', array( 'anonId' => $anon_id ) );
39
			delete_user_meta( $user_id, 'jetpack_tracks_anon_id' );
40
			if ( ! headers_sent() ) {
41
				setcookie( 'tk_ai', 'expired', time() - 1000 );
42
			}
43
		}
44
45
		$wpcom_user_data = Jetpack::get_connected_user_data( $user_id );
46
		update_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'] );
47
48
		self::record_user_event( 'user_linked', array() );
49
	}
50
51
	/* Activated module */
52
	static function track_activate_module( $module ) {
53
		self::record_user_event( 'module_activated', array( 'module' => $module ) );
54
	}
55
56
	/* Deactivated module */
57
	static function track_deactivate_module( $module ) {
58
		self::record_user_event( 'module_deactivated', array( 'module' => $module ) );
59
	}
60
61
	static function record_user_event( $event_type, $data ) {
62
63
		$user = wp_get_current_user();
64
		$site_url = get_option( 'siteurl' );
65
66
		$data['_via_ua']  = $_SERVER['HTTP_USER_AGENT'];
67
		$data['_via_ip']  = $_SERVER['REMOTE_ADDR'];
68
		$data['_lg']      = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
69
		$data['blog_url'] = $site_url;
70
		$data['blog_id']  = Jetpack_Options::get_option( 'id' );
71
72
		// Top level events should not be namespaced
73
		if ( '_aliasUser' != $event_type ) {
74
			$event_type = self::$product_name . '_' . $event_type;
75
		}
76
77
		$data['jetpack_version'] = defined( 'JETPACK__VERSION' ) ? JETPACK__VERSION : '0';
78
79
		jetpack_tracks_record_event( $user, $event_type, $data );
80
	}
81
}
82
83
add_action( 'init',  array( 'JetpackTracking', 'track_jetpack_usage' ) );
84