Completed
Push — update/package-tracks-move-mor... ( 4fd80c...03eae3 )
by
unknown
100:13 queued 88:23
created

client.php ➔ jetpack_tracks_build_event_obj()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
nc 8
nop 4
dl 18
loc 18
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP Tracks Client
4
 * @autounit nosara tracks-client
5
 * Example Usage:
6
 *
7
```php
8
	include( plugin_dir_path( __FILE__ ) . 'lib/tracks/client.php');
9
	$result = jetpack_tracks_record_event( $user, $event_name, $properties );
10
11
	if ( is_wp_error( $result ) ) {
12
		// Handle the error in your app
13
	}
14
```
15
 */
16
17
// Load the client classes
18
require_once( dirname(__FILE__) . '/class.tracks-event.php' );
19
require_once( dirname(__FILE__) . '/class.tracks-client.php' );
20
21
// Now, let's export a sprinkling of syntactic sugar!
22
23
/**
24
 * Procedurally (vs. Object-oriented), track an event object (or flat array)
25
 * NOTE: Use this only when the simpler jetpack_tracks_record_event() function won't work for you.
26
 * @param \Jetpack_Tracks_Event $event The event object.
27
 * @return \Jetpack_Tracks_Event|\WP_Error
28
 *
29
 * @deprecated 7.5 use Tracking->tracks_record_event_raw instead
30
 */
31
function jetpack_tracks_record_event_raw( $event ) {
32
	return Jetpack_Tracks_Client::record_event( $event );
33
}
34
35
/**
36
 * Procedurally build a Tracks Event Object.
37
 * NOTE: Use this only when the simpler jetpack_tracks_record_event() function won't work for you.
38
 * @param $identity WP_user object
39
 * @param string $event_name The name of the event
40
 * @param array $properties Custom properties to send with the event
41
 * @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...
42
 * @return \Jetpack_Tracks_Event|\WP_Error
43
 *
44
 * @deprecated 7.5 use Tracking->jetpack_tracks_build_event_obj instead
45
 */
46 View Code Duplication
function jetpack_tracks_build_event_obj( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
48
	$identity = jetpack_tracks_get_identity( $user->ID );
0 ignored issues
show
Deprecated Code introduced by
The function jetpack_tracks_get_identity() has been deprecated with message: 7.5 use Tracking->jetpack_tracks_get_identity instead

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
49
50
	$properties['user_lang'] = $user->get( 'WPLANG' );
51
52
	$blog_details = array(
53
		'blog_lang' => isset( $properties['blog_lang'] ) ? $properties['blog_lang'] : get_bloginfo( 'language' )
54
	);
55
56
	$timestamp = ( $event_timestamp_millis !== false ) ? $event_timestamp_millis : round( microtime( true ) * 1000 );
57
	$timestamp_string = is_string( $timestamp ) ? $timestamp : number_format( $timestamp, 0, '', '' );
58
59
	return new Jetpack_Tracks_Event( array_merge( $blog_details, (array) $properties, $identity, array(
60
		'_en' => $event_name,
61
		'_ts' => $timestamp_string
62
	) ) );
63
}
64
65
/**
66
 * Get the identity to send to tracks.
67
 *
68
 * @param int $user_id The user id of the local user
69
 * @return array $identity
70
 *
71
 * @deprecated 7.5 use Tracking->jetpack_tracks_get_identity instead
72
 */
73
function jetpack_tracks_get_identity( $user_id ) {
74
75
	// Meta is set, and user is still connected.  Use WPCOM ID
76
	$wpcom_id = get_user_meta( $user_id, 'jetpack_tracks_wpcom_id', true );
77
	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...
78
		return array(
79
			'_ut' => 'wpcom:user_id',
80
			'_ui' => $wpcom_id
81
		);
82
	}
83
84
	// User is connected, but no meta is set yet.  Use WPCOM ID and set meta.
85
	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...
86
		$wpcom_user_data = Jetpack::get_connected_user_data( $user_id );
87
		update_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'] );
88
89
		return array(
90
			'_ut' => 'wpcom:user_id',
91
			'_ui' => $wpcom_user_data['ID']
92
		);
93
	}
94
95
	// User isn't linked at all.  Fall back to anonymous ID.
96
	$anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true );
97
	if ( ! $anon_id ) {
98
		$anon_id = Jetpack_Tracks_Client::get_anon_id();
99
		add_user_meta( $user_id, 'jetpack_tracks_anon_id', $anon_id, false );
100
	}
101
102
	if ( ! isset( $_COOKIE[ 'tk_ai' ] ) && ! headers_sent() ) {
103
		setcookie( 'tk_ai', $anon_id );
104
	}
105
106
	return array(
107
		'_ut' => 'anon',
108
		'_ui' => $anon_id
109
	);
110
111
}
112
113
/**
114
 * Record an event in Tracks - this is the preferred way to record events from PHP.
115
 *
116
 * @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...
117
 * @param string $event_name The name of the event
118
 * @param array $properties Custom properties to send with the event
119
 * @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...
120
 * @return bool true for success | \WP_Error if the event pixel could not be fired
121
 *
122
 * @deprecated 7.5 use Tracking->jetpack_tracks_record_event instead
123
 */
124 View Code Duplication
function jetpack_tracks_record_event( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
126
	// We don't want to track user events during unit tests/CI runs.
127
	if ( $user instanceof WP_User && 'wptests_capabilities' === $user->cap_key ) {
0 ignored issues
show
Bug introduced by
The class 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...
128
		return false;
129
	}
130
131
	$event_obj = jetpack_tracks_build_event_obj( $user, $event_name, $properties, $event_timestamp_millis );
0 ignored issues
show
Deprecated Code introduced by
The function jetpack_tracks_build_event_obj() has been deprecated with message: 7.5 use Tracking->jetpack_tracks_build_event_obj instead

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
132
133
	if ( is_wp_error( $event_obj->error ) ) {
134
		return $event_obj->error;
135
	}
136
137
	return $event_obj->record();
138
}
139