Completed
Push — renovate/gridicons-3.x ( c004c1...f8ccd4 )
by
unknown
284:06 queued 275:32
created

_inc/lib/tracks/client.php (5 issues)

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
 * 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
// Now, let's export a sprinkling of syntactic sugar!
18
19
/**
20
 * Procedurally (vs. Object-oriented), track an event object (or flat array)
21
 * NOTE: Use this only when the simpler jetpack_tracks_record_event() function won't work for you.
22
 * @param \Jetpack_Tracks_Event $event The event object.
23
 * @return \Jetpack_Tracks_Event|\WP_Error
24
 *
25
 * @deprecated 7.5.0 use Automattic\Jetpack\Tracking->tracks_record_event_raw instead
26
 */
27
function jetpack_tracks_record_event_raw( $event ) {
28
	// @TODO: Enable these once we're sure that we won't be flooding sites with notices.
29
	//_doing_it_wrong( 'jetpack_tracks_record_event_raw', 'Use Automattic\Jetpack\Tracking::tracks_record_event instead.', '7.5.0' );
30
	return Jetpack_Tracks_Client::record_event( $event );
31
}
32
33
/**
34
 * Procedurally build a Tracks Event Object.
35
 * NOTE: Use this only when the simpler jetpack_tracks_record_event() function won't work for you.
36
 * @param $identity WP_user object
37
 * @param string $event_name The name of the event
38
 * @param array $properties Custom properties to send with the event
39
 * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred
40
 * @return \Jetpack_Tracks_Event|\WP_Error
41
 *
42
 * @deprecated 7.5.0 use Automattic\Jetpack\Tracking->tracks_build_event_obj instead
43
 */
44 View Code Duplication
function jetpack_tracks_build_event_obj( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) {
0 ignored issues
show
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...
45
	// @TODO: Enable these once we're sure that we won't be flooding sites with notices.
46
	//_doing_it_wrong( 'jetpack_tracks_build_event_obj', 'Use Automattic\Jetpack\Tracking::tracks_build_event_obj instead.', '7.5.0' );
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.0 use Automattic\Jetpack\Tracking->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.0 use Automattic\Jetpack\Tracking->tracks_get_identity instead
72
 */
73
function jetpack_tracks_get_identity( $user_id ) {
74
	// @TODO: Enable these once we're sure that we won't be flooding sites with notices.
75
	//_doing_it_wrong( 'jetpack_tracks_get_identity', 'Use Automattic\Jetpack\Tracking::tracks_get_identity instead.', '7.5.0' );
76
77
	// Meta is set, and user is still connected.  Use WPCOM ID
78
	$wpcom_id = get_user_meta( $user_id, 'jetpack_tracks_wpcom_id', true );
79
	if ( $wpcom_id && Jetpack::is_user_connected( $user_id ) ) {
80
		return array(
81
			'_ut' => 'wpcom:user_id',
82
			'_ui' => $wpcom_id
83
		);
84
	}
85
86
	// User is connected, but no meta is set yet.  Use WPCOM ID and set meta.
87
	if ( Jetpack::is_user_connected( $user_id ) ) {
88
		$wpcom_user_data = Jetpack::get_connected_user_data( $user_id );
89
		update_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'] );
90
91
		return array(
92
			'_ut' => 'wpcom:user_id',
93
			'_ui' => $wpcom_user_data['ID']
94
		);
95
	}
96
97
	// User isn't linked at all.  Fall back to anonymous ID.
98
	$anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true );
99
	if ( ! $anon_id ) {
100
		$anon_id = Jetpack_Tracks_Client::get_anon_id();
101
		add_user_meta( $user_id, 'jetpack_tracks_anon_id', $anon_id, false );
102
	}
103
104
	if ( ! isset( $_COOKIE[ 'tk_ai' ] ) && ! headers_sent() ) {
105
		setcookie( 'tk_ai', $anon_id );
106
	}
107
108
	return array(
109
		'_ut' => 'anon',
110
		'_ui' => $anon_id
111
	);
112
113
}
114
115
/**
116
 * Record an event in Tracks - this is the preferred way to record events from PHP.
117
 *
118
 * @param mixed $identity username, user_id, or WP_user object
119
 * @param string $event_name The name of the event
120
 * @param array $properties Custom properties to send with the event
121
 * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred
122
 * @return bool true for success | \WP_Error if the event pixel could not be fired
123
 *
124
 * @deprecated 7.5.0 use Automattic\Jetpack\Tracking->tracks_record_event instead
125
 */
126 View Code Duplication
function jetpack_tracks_record_event( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) {
0 ignored issues
show
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...
127
	// @TODO: Enable these once we're sure that we won't be flooding sites with notices.
128
	//_doing_it_wrong( 'jetpack_tracks_record_event', 'Use Automattic\Jetpack\Tracking::tracks_record_event instead.', '7.5.0' );
129
130
	// We don't want to track user events during unit tests/CI runs.
131
	if ( $user instanceof WP_User && 'wptests_capabilities' === $user->cap_key ) {
132
		return false;
133
	}
134
135
	$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.0 use Automattic\Jetpack\Tracking->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...
136
137
	if ( is_wp_error( $event_obj->error ) ) {
138
		return $event_obj->error;
139
	}
140
141
	return $event_obj->record();
0 ignored issues
show
The method record does only exist in Jetpack_Tracks_Event, but not in WP_Error.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
142
}
143