| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Nosara Tracks for Jetpack | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | require_once( dirname( __FILE__ ) . '/_inc/lib/tracks/client.php' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class JetpackTracking { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 	static $product_name = 'jetpack'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 	static function track_jetpack_usage() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 		if ( ! Jetpack::is_active() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 		// For tracking stuff via js/ajax | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 		add_action( 'admin_enqueue_scripts',         array( __CLASS__, 'enqueue_tracks_scripts' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 		add_action( 'jetpack_activate_module',   array( __CLASS__, 'track_activate_module'), 1, 1 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		add_action( 'jetpack_deactivate_module', array( __CLASS__, 'track_deactivate_module'), 1, 1 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		add_action( 'jetpack_user_authorized',       array( __CLASS__, 'track_user_linked' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 24 |  | View Code Duplication | 	static function enqueue_tracks_scripts() { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | 		wp_enqueue_script( 'jptracks', plugins_url( '_inc/lib/tracks/tracks-ajax.js', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION, true ); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | 		wp_localize_script( 'jptracks', 'jpTracksAJAX', array( | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | 			'ajaxurl'            => admin_url( 'admin-ajax.php' ), | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | 			'jpTracksAJAX_nonce' => wp_create_nonce( 'jp-tracks-ajax-nonce' ), | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | 		) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	/* User has linked their account */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	static function track_user_linked() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		$user_id = get_current_user_id(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		$anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		if ( $anon_id ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 			self::record_user_event( '_aliasUser', array( 'anonId' => $anon_id ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 			delete_user_meta( $user_id, 'jetpack_tracks_anon_id' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 			if ( ! headers_sent() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 				setcookie( 'tk_ai', 'expired', time() - 1000 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		$wpcom_user_data = Jetpack::get_connected_user_data( $user_id ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		update_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		self::record_user_event( 'wpa_user_linked', array() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	/* Activated module */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	static function track_activate_module( $module ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		self::record_user_event( 'module_activated', array( 'module' => $module ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	/* Deactivated module */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	static function track_deactivate_module( $module ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		self::record_user_event( 'module_deactivated', array( 'module' => $module ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	static function record_user_event( $event_type, $data= array(), $user = null ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		if ( ! $user ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			$user = wp_get_current_user(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		$site_url = get_option( 'siteurl' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$data['_via_ua']  = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		$data['_via_ip']  = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		$data['_lg']      = isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		$data['blog_url'] = $site_url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		$data['blog_id']  = Jetpack_Options::get_option( 'id' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		// Top level events should not be namespaced | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		if ( '_aliasUser' != $event_type ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			$event_type = self::$product_name . '_' . $event_type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		$data['jetpack_version'] = defined( 'JETPACK__VERSION' ) ? JETPACK__VERSION : '0'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		jetpack_tracks_record_event( $user, $event_type, $data ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  | add_action( 'init',  array( 'JetpackTracking', 'track_jetpack_usage' ) ); | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |  | 
            
                        
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.