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