Completed
Push — update/package-tracks-move-mor... ( d28e64...dfcceb )
by
unknown
127:50 queued 121:01
created

Tracking::track_deactivate_module()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Nosara Tracks for Jetpack
4
 */
5
6
namespace Automattic\Jetpack;
7
8
class Tracking {
9
	public $product_name;
10
11
	function __construct( $product_name = 'jetpack' ) {
12
		$this->product_name = $product_name;
13
	}
14
15 View Code Duplication
	static function enqueue_tracks_scripts() {
16
		wp_enqueue_script( 'jptracks', plugins_url( '_inc/lib/tracks/tracks-ajax.js', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION, true );
17
		wp_localize_script(
18
			'jptracks',
19
			'jpTracksAJAX',
20
			array(
21
				'ajaxurl'            => admin_url( 'admin-ajax.php' ),
22
				'jpTracksAJAX_nonce' => wp_create_nonce( 'jp-tracks-ajax-nonce' ),
23
			)
24
		);
25
	}
26
27
	static function record_user_event( $event_type, $data = array(), $user = null ) {
28
		if ( ! $user ) {
29
			$user = wp_get_current_user();
30
		}
31
		$site_url = get_option( 'siteurl' );
32
33
		$data['_via_ua']  = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
34
		$data['_via_ip']  = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : '';
35
		$data['_lg']      = isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '';
36
		$data['blog_url'] = $site_url;
37
		$data['blog_id']  = \Jetpack_Options::get_option( 'id' );
38
39
		// Top level events should not be namespaced
40
		if ( '_aliasUser' != $event_type ) {
41
			$event_type = self::$product_name . '_' . $event_type;
42
		}
43
44
		$data['jetpack_version'] = defined( 'JETPACK__VERSION' ) ? JETPACK__VERSION : '0';
45
46
		return self::jetpack_tracks_record_event( $user, $event_type, $data );
0 ignored issues
show
Bug introduced by
The method jetpack_tracks_record_event() does not exist on Automattic\Jetpack\Tracking. Did you maybe mean tracks_record_event()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
47
	}
48
49
	/**
50
	 * Procedurally build a Tracks Event Object.
51
	 * NOTE: Use this only when the simpler \Automattic\Jetpack\Tracking::jetpack_tracks_record_event() function won't work for you.
52
	 *
53
	 * @param $identity WP_user object
54
	 * @param string                  $event_name The name of the event
55
	 * @param array                   $properties Custom properties to send with the event
56
	 * @param int                     $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred
0 ignored issues
show
Documentation introduced by
Should the type for parameter $event_timestamp_millis not be false|integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
57
	 * @return \Jetpack_Tracks_Event|\WP_Error
58
	 */
59
	function tracks_build_event_obj( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) {
60
61
		$tracks   = new \Automattic\Jetpack\Tracking();
62
		$identity = $tracks->tracks_get_identity( $user->ID );
63
64
		$properties['user_lang'] = $user->get( 'WPLANG' );
65
66
		$blog_details = array(
67
			'blog_lang' => isset( $properties['blog_lang'] ) ? $properties['blog_lang'] : get_bloginfo( 'language' ),
68
		);
69
70
		$timestamp        = ( $event_timestamp_millis !== false ) ? $event_timestamp_millis : round( microtime( true ) * 1000 );
71
		$timestamp_string = is_string( $timestamp ) ? $timestamp : number_format( $timestamp, 0, '', '' );
72
73
		return new \Jetpack_Tracks_Event(
74
			array_merge(
75
				$blog_details,
76
				(array) $properties,
77
				$identity,
78
				array(
79
					'_en' => $event_name,
80
					'_ts' => $timestamp_string,
81
				)
82
			)
83
		);
84
	}
85
86
	/*
87
	 * Get the identity to send to tracks.
88
	 *
89
	 * @param int $user_id The user id of the local user
90
	 * @return array $identity
91
	 */
92
	function tracks_get_identity( $user_id ) {
93
94
		// Meta is set, and user is still connected.  Use WPCOM ID
95
		$wpcom_id = get_user_meta( $user_id, 'jetpack_tracks_wpcom_id', true );
96
		if ( $wpcom_id && \Jetpack::is_user_connected( $user_id ) ) {
0 ignored issues
show
Documentation introduced by
$user_id is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
97
			return array(
98
				'_ut' => 'wpcom:user_id',
99
				'_ui' => $wpcom_id,
100
			);
101
		}
102
103
		// User is connected, but no meta is set yet.  Use WPCOM ID and set meta.
104
		if ( \Jetpack::is_user_connected( $user_id ) ) {
0 ignored issues
show
Documentation introduced by
$user_id is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
			$wpcom_user_data = Jetpack::get_connected_user_data( $user_id );
106
			update_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'] );
107
108
			return array(
109
				'_ut' => 'wpcom:user_id',
110
				'_ui' => $wpcom_user_data['ID'],
111
			);
112
		}
113
114
		// User isn't linked at all.  Fall back to anonymous ID.
115
		$anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true );
116
		if ( ! $anon_id ) {
117
			$anon_id = \Jetpack_Tracks_Client::get_anon_id();
118
			add_user_meta( $user_id, 'jetpack_tracks_anon_id', $anon_id, false );
119
		}
120
121
		if ( ! isset( $_COOKIE['tk_ai'] ) && ! headers_sent() ) {
122
			setcookie( 'tk_ai', $anon_id );
123
		}
124
125
		return array(
126
			'_ut' => 'anon',
127
			'_ui' => $anon_id,
128
		);
129
130
	}
131
132
	/**
133
	 * Record an event in Tracks - this is the preferred way to record events from PHP.
134
	 *
135
	 * @param mixed  $identity username, user_id, or WP_user object
0 ignored issues
show
Bug introduced by
There is no parameter named $identity. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
136
	 * @param string $event_name The name of the event
137
	 * @param array  $properties Custom properties to send with the event
138
	 * @param int    $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred
0 ignored issues
show
Documentation introduced by
Should the type for parameter $event_timestamp_millis not be false|integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
139
	 * @return bool true for success | \WP_Error if the event pixel could not be fired
140
	 */
141
	function tracks_record_event( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) {
142
143
		// We don't want to track user events during unit tests/CI runs.
144
		if ( $user instanceof WP_User && 'wptests_capabilities' === $user->cap_key ) {
0 ignored issues
show
Bug introduced by
The class Automattic\Jetpack\WP_User does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
145
			return false;
146
		}
147
148
		$event_obj = self::tracks_build_event_obj( $user, $event_name, $properties, $event_timestamp_millis );
149
150
		if ( is_wp_error( $event_obj->error ) ) {
151
			return $event_obj->error;
152
		}
153
154
		return $event_obj->record();
155
	}
156
}
157