Completed
Push — update/jitm-users ( 671cee )
by
unknown
30:51 queued 19:55
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_activate_module',   array( __CLASS__, 'track_activate_module'), 1, 1 );
20
		add_action( 'jetpack_deactivate_module', array( __CLASS__, 'track_deactivate_module'), 1, 1 );
21
		add_action( 'jetpack_user_authorized',   array( __CLASS__, 'track_user_linked' ) );
22
		add_action( 'wp_login_failed',           array( __CLASS__, 'track_failed_login_attempts' ) );
23
	}
24
25 View Code Duplication
	static function enqueue_tracks_scripts() {
26
		wp_enqueue_script( 'jptracks', plugins_url( '_inc/lib/tracks/tracks-ajax.js', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION, true );
27
		wp_localize_script( 'jptracks', 'jpTracksAJAX', array(
28
			'ajaxurl'            => admin_url( 'admin-ajax.php' ),
29
			'jpTracksAJAX_nonce' => wp_create_nonce( 'jp-tracks-ajax-nonce' ),
30
		) );
31
	}
32
33
	/* User has linked their account */
34
	static function track_user_linked() {
35
		$user_id = get_current_user_id();
36
		$anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true );
37
38
		if ( $anon_id ) {
39
			self::record_user_event( '_aliasUser', array( 'anonId' => $anon_id ) );
40
			delete_user_meta( $user_id, 'jetpack_tracks_anon_id' );
41
			if ( ! headers_sent() ) {
42
				setcookie( 'tk_ai', 'expired', time() - 1000 );
43
			}
44
		}
45
46
		$wpcom_user_data = Jetpack::get_connected_user_data( $user_id );
47
		update_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'] );
48
49
		self::record_user_event( 'wpa_user_linked', array() );
50
	}
51
52
	/* Activated module */
53
	static function track_activate_module( $module ) {
54
		self::record_user_event( 'module_activated', array( 'module' => $module ) );
55
	}
56
57
	/* Deactivated module */
58
	static function track_deactivate_module( $module ) {
59
		self::record_user_event( 'module_deactivated', array( 'module' => $module ) );
60
	}
61
62
	/* Failed login attempts */
63
	static function track_failed_login_attempts( $login ) {
64
		require_once( JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php' );
65
		self::record_user_event( 'failed_login', array( 'origin_ip' => jetpack_protect_get_ip(), 'login' => $login ) );
66
	}
67
68
	static function record_user_event( $event_type, $data= array(), $user = null ) {
69
70
		if ( ! $user ) {
71
			$user = wp_get_current_user();
72
		}
73
		$site_url = get_option( 'siteurl' );
74
75
		$data['_via_ua']  = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
76
		$data['_via_ip']  = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : '';
77
		$data['_lg']      = isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '';
78
		$data['blog_url'] = $site_url;
79
		$data['blog_id']  = Jetpack_Options::get_option( 'id' );
80
81
		// Top level events should not be namespaced
82
		if ( '_aliasUser' != $event_type ) {
83
			$event_type = self::$product_name . '_' . $event_type;
84
		}
85
86
		$data['jetpack_version'] = defined( 'JETPACK__VERSION' ) ? JETPACK__VERSION : '0';
87
88
		jetpack_tracks_record_event( $user, $event_type, $data );
89
	}
90
}
91
92
add_action( 'init',  array( 'JetpackTracking', 'track_jetpack_usage' ) );
93