Automattic /
jetpack
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 = 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 tracks_record_event() function won't work for you. |
||
| 26 | * @param \Tracks_Event $event The event object. |
||
| 27 | * @return \Tracks_Event|\WP_Error |
||
| 28 | */ |
||
| 29 | function tracks_record_event_raw( $event ) { |
||
| 30 | return Tracks_Client::record_event( $event ); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Procedurally build a Tracks Event Object. |
||
| 35 | * NOTE: Use this only when the simpler 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 |
||
|
0 ignored issues
–
show
|
|||
| 40 | * @return \Tracks_Event|\WP_Error |
||
| 41 | */ |
||
| 42 | function tracks_build_event_obj( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
||
| 43 | |||
| 44 | $identity = tracks_get_identity( $user->ID ); |
||
| 45 | |||
| 46 | $properties['user_lang'] = $user->get( 'WPLANG' ); |
||
| 47 | |||
| 48 | $blog_details = array( |
||
| 49 | 'blog_lang' => isset( $properties['blog_lang'] ) ? $properties['blog_lang'] : get_bloginfo( 'language' ) |
||
| 50 | ); |
||
| 51 | |||
| 52 | $timestamp = ( $event_timestamp_millis !== false ) ? $event_timestamp_millis : round( microtime( true ) * 1000 ); |
||
| 53 | $timestamp_string = is_string( $timestamp ) ? $timestamp : number_format( $timestamp, 0, '', '' ); |
||
| 54 | |||
| 55 | return new Tracks_Event( array_merge( $blog_details, (array) $properties, $identity, array( |
||
| 56 | '_en' => $event_name, |
||
| 57 | '_ts' => $timestamp_string |
||
| 58 | ) ) ); |
||
| 59 | } |
||
| 60 | |||
| 61 | /* |
||
| 62 | * Get the identity to send to tracks. |
||
| 63 | * |
||
| 64 | * @param int $user_id The user id of the local user |
||
| 65 | * @return array $identity |
||
| 66 | */ |
||
| 67 | function tracks_get_identity( $user_id ) { |
||
| 68 | |||
| 69 | // Meta is set, and user is still connected. Use WPCOM ID |
||
| 70 | $wpcom_id = get_user_meta( $user_id, 'jetpack_tracks_wpcom_id', true ); |
||
| 71 | if ( $wpcom_id && Jetpack::is_user_connected( $user_id ) ) { |
||
|
0 ignored issues
–
show
$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...
|
|||
| 72 | return array( |
||
| 73 | '_ut' => 'wpcom:user_id', |
||
| 74 | '_ui' => $wpcom_id |
||
| 75 | ); |
||
| 76 | } |
||
| 77 | |||
| 78 | // User is connected, but no meta is set yet. Use WPCOM ID and set meta. |
||
| 79 | if ( Jetpack::is_user_connected( $user_id ) ) { |
||
|
0 ignored issues
–
show
$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...
|
|||
| 80 | $wpcom_user_data = Jetpack::get_connected_user_data( $user_id ); |
||
| 81 | add_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'], true ); |
||
| 82 | |||
| 83 | return array( |
||
| 84 | '_ut' => 'wpcom:user_id', |
||
| 85 | '_ui' => $wpcom_user_data['ID'] |
||
| 86 | ); |
||
| 87 | } |
||
| 88 | |||
| 89 | // User isn't linked at all. Fall back to anonymous ID. |
||
| 90 | $anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true ); |
||
| 91 | if ( ! $anon_id ) { |
||
| 92 | $anon_id = Tracks_Client::get_anon_id(); |
||
| 93 | add_user_meta( $user_id, 'jetpack_tracks_anon_id', $anon_id, false ); |
||
| 94 | } |
||
| 95 | |||
| 96 | if ( ! isset( $_COOKIE[ 'tk_ai' ] ) && ! headers_sent() ) { |
||
| 97 | setcookie( 'tk_ai', $anon_id ); |
||
| 98 | } |
||
| 99 | |||
| 100 | return array( |
||
| 101 | '_ut' => 'anon', |
||
| 102 | '_ui' => $anon_id |
||
| 103 | ); |
||
| 104 | |||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Record an event in Tracks - this is the preferred way to record events from PHP. |
||
| 109 | * |
||
| 110 | * @param mixed $identity username, user_id, or WP_user object |
||
|
0 ignored issues
–
show
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 /**
* @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...
|
|||
| 111 | * @param string $event_name The name of the event |
||
| 112 | * @param array $properties Custom properties to send with the event |
||
| 113 | * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred |
||
|
0 ignored issues
–
show
Should the type for parameter
$event_timestamp_millis not be false|integer?
This check looks for 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...
|
|||
| 114 | * @return bool true for success | \WP_Error if the event pixel could not be fired |
||
| 115 | */ |
||
| 116 | function tracks_record_event( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
||
| 117 | $event_obj = tracks_build_event_obj( $user, $event_name, $properties, $event_timestamp_millis ); |
||
| 118 | |||
| 119 | if ( is_wp_error( $event_obj->error ) ) { |
||
| 120 | return $event_obj->error; |
||
| 121 | } |
||
| 122 | |||
| 123 | return $event_obj->record(); |
||
| 124 | } |
||
| 125 |
This check looks for
@paramannotations 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.