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 | * Nosara Tracks for Jetpack |
||
| 4 | * |
||
| 5 | * @package jetpack-tracking |
||
| 6 | */ |
||
| 7 | |||
| 8 | namespace Automattic\Jetpack; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * The Tracking class, used to record events in wpcom |
||
| 12 | */ |
||
| 13 | class Tracking { |
||
| 14 | private $product_name; |
||
| 15 | private $connection; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Creates the Tracking object. |
||
| 19 | * |
||
| 20 | * @param String $product_name the slug of the product that we are tracking. |
||
| 21 | * @param Automattic\Jetpack\Connection\Manager $connection the connection manager object. |
||
|
0 ignored issues
–
show
|
|||
| 22 | */ |
||
| 23 | function __construct( $product_name = 'jetpack', $connection = null ) { |
||
| 24 | $this->product_name = $product_name; |
||
| 25 | $this->connection = $connection; |
||
| 26 | if ( is_null( $this->connection ) ) { |
||
| 27 | // TODO We should always pass a Connection. |
||
| 28 | $this->connection = new Connection\Manager(); |
||
| 29 | } |
||
| 30 | } |
||
| 31 | |||
| 32 | View Code Duplication | function enqueue_tracks_scripts() { |
|
| 33 | wp_enqueue_script( 'jptracks', plugins_url( '_inc/lib/tracks/tracks-ajax.js', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION, true ); |
||
| 34 | wp_localize_script( |
||
| 35 | 'jptracks', |
||
| 36 | 'jpTracksAJAX', |
||
| 37 | array( |
||
| 38 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
||
| 39 | 'jpTracksAJAX_nonce' => wp_create_nonce( 'jp-tracks-ajax-nonce' ), |
||
| 40 | ) |
||
| 41 | ); |
||
| 42 | } |
||
| 43 | |||
| 44 | function record_user_event( $event_type, $data = array(), $user = null ) { |
||
| 45 | if ( ! $user ) { |
||
| 46 | $user = wp_get_current_user(); |
||
| 47 | } |
||
| 48 | $site_url = get_option( 'siteurl' ); |
||
| 49 | |||
| 50 | $data['_via_ua'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : ''; |
||
| 51 | $data['_via_ip'] = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : ''; |
||
| 52 | $data['_lg'] = isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : ''; |
||
| 53 | $data['blog_url'] = $site_url; |
||
| 54 | $data['blog_id'] = \Jetpack_Options::get_option( 'id' ); |
||
| 55 | |||
| 56 | // Top level events should not be namespaced |
||
| 57 | if ( '_aliasUser' != $event_type ) { |
||
| 58 | $event_type = $this->product_name . '_' . $event_type; |
||
| 59 | } |
||
| 60 | |||
| 61 | $data['jetpack_version'] = defined( 'JETPACK__VERSION' ) ? JETPACK__VERSION : '0'; |
||
| 62 | |||
| 63 | return $this->tracks_record_event( $user, $event_type, $data ); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Record an event in Tracks - this is the preferred way to record events from PHP. |
||
| 68 | * |
||
| 69 | * @param mixed $identity username, user_id, or WP_user object |
||
| 70 | * @param string $event_name The name of the event |
||
| 71 | * @param array $properties Custom properties to send with the event |
||
| 72 | * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred |
||
| 73 | * |
||
| 74 | * @return bool true for success | \WP_Error if the event pixel could not be fired |
||
| 75 | */ |
||
| 76 | function tracks_record_event( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
||
| 77 | |||
| 78 | // We don't want to track user events during unit tests/CI runs. |
||
| 79 | if ( $user instanceof \WP_User && 'wptests_capabilities' === $user->cap_key ) { |
||
| 80 | return false; |
||
| 81 | } |
||
| 82 | |||
| 83 | $event_obj = $this->tracks_build_event_obj( $user, $event_name, $properties, $event_timestamp_millis ); |
||
| 84 | |||
| 85 | if ( is_wp_error( $event_obj->error ) ) { |
||
| 86 | return $event_obj->error; |
||
| 87 | } |
||
| 88 | |||
| 89 | return $event_obj->record(); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Procedurally build a Tracks Event Object. |
||
| 94 | * NOTE: Use this only when the simpler Automattic\Jetpack\Tracking->jetpack_tracks_record_event() function won't work for you. |
||
| 95 | * |
||
| 96 | * @param $identity WP_user object |
||
| 97 | * @param string $event_name The name of the event |
||
| 98 | * @param array $properties Custom properties to send with the event |
||
| 99 | * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred |
||
| 100 | * |
||
| 101 | * @return \Jetpack_Tracks_Event|\WP_Error |
||
| 102 | */ |
||
| 103 | function tracks_build_event_obj( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
||
| 104 | $identity = $this->tracks_get_identity( $user->ID ); |
||
| 105 | |||
| 106 | $properties['user_lang'] = $user->get( 'WPLANG' ); |
||
| 107 | |||
| 108 | $blog_details = array( |
||
| 109 | 'blog_lang' => isset( $properties['blog_lang'] ) ? $properties['blog_lang'] : get_bloginfo( 'language' ), |
||
| 110 | ); |
||
| 111 | |||
| 112 | $timestamp = ( $event_timestamp_millis !== false ) ? $event_timestamp_millis : round( microtime( true ) * 1000 ); |
||
| 113 | $timestamp_string = is_string( $timestamp ) ? $timestamp : number_format( $timestamp, 0, '', '' ); |
||
| 114 | |||
| 115 | return new \Jetpack_Tracks_Event( |
||
| 116 | array_merge( |
||
| 117 | $blog_details, |
||
| 118 | (array) $properties, |
||
| 119 | $identity, |
||
| 120 | array( |
||
| 121 | '_en' => $event_name, |
||
| 122 | '_ts' => $timestamp_string, |
||
| 123 | ) |
||
| 124 | ) |
||
| 125 | ); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Get the identity to send to tracks. |
||
| 130 | * |
||
| 131 | * @param int $user_id The user id of the local user |
||
| 132 | * |
||
| 133 | * @return array $identity |
||
| 134 | */ |
||
| 135 | function tracks_get_identity( $user_id ) { |
||
| 136 | |||
| 137 | // Meta is set, and user is still connected. Use WPCOM ID |
||
| 138 | $wpcom_id = get_user_meta( $user_id, 'jetpack_tracks_wpcom_id', true ); |
||
| 139 | if ( $wpcom_id && $this->connection->is_user_connected( $user_id ) ) { |
||
| 140 | return array( |
||
| 141 | '_ut' => 'wpcom:user_id', |
||
| 142 | '_ui' => $wpcom_id, |
||
| 143 | ); |
||
| 144 | } |
||
| 145 | |||
| 146 | // User is connected, but no meta is set yet. Use WPCOM ID and set meta. |
||
| 147 | if ( $this->connection->is_user_connected( $user_id ) ) { |
||
| 148 | $wpcom_user_data = $this->connection->get_connected_user_data( $user_id ); |
||
| 149 | update_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'] ); |
||
| 150 | |||
| 151 | return array( |
||
| 152 | '_ut' => 'wpcom:user_id', |
||
| 153 | '_ui' => $wpcom_user_data['ID'], |
||
| 154 | ); |
||
| 155 | } |
||
| 156 | |||
| 157 | // User isn't linked at all. Fall back to anonymous ID. |
||
| 158 | $anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true ); |
||
| 159 | if ( ! $anon_id ) { |
||
| 160 | $anon_id = \Jetpack_Tracks_Client::get_anon_id(); |
||
| 161 | add_user_meta( $user_id, 'jetpack_tracks_anon_id', $anon_id, false ); |
||
| 162 | } |
||
| 163 | |||
| 164 | if ( ! isset( $_COOKIE['tk_ai'] ) && ! headers_sent() ) { |
||
| 165 | setcookie( 'tk_ai', $anon_id ); |
||
| 166 | } |
||
| 167 | |||
| 168 | return array( |
||
| 169 | '_ut' => 'anon', |
||
| 170 | '_ui' => $anon_id, |
||
| 171 | ); |
||
| 172 | |||
| 173 | } |
||
| 174 | } |
||
| 175 |
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.