Completed
Push — add/nosara-tracks ( 45fcfa...aa617c )
by
unknown
35:39 queued 18:35
created

JetpackTracking::track_user_linked()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
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
Coding Style introduced by
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
		add_action( 'jetpack_activate_module',       array( __CLASS__, 'track_activate_module'), 1, 1 );
13
		add_action( 'jetpack_pre_deactivate_module', array( __CLASS__, 'track_deactivate_module'), 1, 1 );
14
		add_action( 'jetpack_user_authorized',       array( __CLASS__, 'track_user_linked' ) );
15
	}
16
17
	/* User has linked their account */
18
	static function track_user_linked() {
19
		self::record_user_event( 'user_linked', array() );
20
	}
21
22
	/* Activated module */
23
	static function track_activate_module( $module ) {
24
		self::record_user_event( 'module_activated', array( 'module' => $module ) );
25
	}
26
27
	/* Deactivated module */
28
	static function track_deactivate_module( $module ) {
29
		self::record_user_event( 'module_deactivated', array( 'module' => $module ) );
30
	}
31
32
	static function record_user_event( $event_type, $data ) {
33
34
		$user = wp_get_current_user();
35
		$site_url = get_option( 'siteurl' );
36
37
		$data['_via_ua'] = $_SERVER['HTTP_USER_AGENT'];
38
		$data['_via_ip'] = $_SERVER['REMOTE_ADDR'];
39
		$data['_lg']     = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
40
		$data['blog_url'] = $site_url;
41
42
		$data['jetpack_version'] = defined( 'JETPACK__VERSION' ) ? JETPACK__VERSION : "0";
43
44
		$response = tracks_record_event( $user, self::$product_name.'_'.$event_type, $data );
45
46
		if ( is_wp_error( $response ) ) {
47
			error_log( "There was an error: ".$response->get_error_message() );
0 ignored issues
show
Bug introduced by
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...
48
		}
49
	}
50
}
51
52
add_action( 'init',  array( 'JetpackTracking', 'track_jetpack_usage' ) );
53