PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.9
Jetpack – WP Security, Backup, Speed, & Growth v7.9
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / src / Tracking.php

Tracking.php in Jetpack – WP Security, Backup, Speed, & Growth 7.9, at src/Tracking.php

224 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Automattic\Jetpack\Plugin;
3
4 use Automattic\Jetpack\Tracking as Tracks;
5 use Automattic\Jetpack\Connection\Manager as Connection_Manager;
6
7 class Tracking {
8 /**
9 * Tracking object.
10 *
11 * @var Tracks
12 *
13 * @access private
14 */
15 private $tracking;
16 /**
17 * Prevents the Tracking from being intialized more then once.
18 * @var bool
19 */
20 private $initalized = false;
21
22 function init() {
23 if ( $this->initalized ) {
24 return;
25 }
26 $this->initalized = true;
27 $this->tracking = new Tracks( 'jetpack' );
28
29 // For tracking stuff via js/ajax
30 add_action( 'admin_enqueue_scripts', array( $this->tracking, 'enqueue_tracks_scripts' ) );
31
32 add_action( 'jetpack_activate_module', array( $this, 'jetpack_activate_module' ), 1, 1 );
33 add_action( 'jetpack_deactivate_module', array( $this, 'jetpack_deactivate_module' ), 1, 1 );
34 add_action( 'jetpack_user_authorized', array( $this, 'jetpack_user_authorized' ) );
35 add_action( 'wp_login_failed', array( $this, 'wp_login_failed' ) );
36
37 // Track that we've begun verifying the previously generated secret.
38 add_action( 'jetpack_verify_secrets_begin', array( $this, 'jetpack_verify_secrets_begin' ), 10, 2 );
39 add_action( 'jetpack_verify_secrets_success', array( $this, 'jetpack_verify_secrets_success' ), 10, 2 );
40 add_action( 'jetpack_verify_secrets_fail', array( $this, 'jetpack_verify_secrets_fail' ), 10, 3 );
41
42 // Universal ajax callback for all tracking events triggered via js
43 add_action( 'wp_ajax_jetpack_tracks', array( $this, 'wp_ajax_jetpack_tracks' ) );
44
45 add_action( 'jetpack_verify_api_authorization_request_error_double_encode', array( $this, 'jetpack_verify_api_authorization_request_error_double_encode' ) );
46 add_action( 'jetpack_connection_register_fail', array( $this, 'jetpack_connection_register_fail' ), 10, 2 );
47 add_action( 'jetpack_connection_register_success', array( $this, 'jetpack_connection_register_success' ) );
48 }
49
50 /**
51 * Track that a specific module has been activated.
52 *
53 * @access public
54 *
55 * @param string $module Module slug.
56 */
57 public function jetpack_activate_module( $module ) {
58 $this->tracking->record_user_event( 'module_activated', array( 'module' => $module ) );
59 }
60
61 /**
62 * Track that a specific module has been deactivated.
63 *
64 * @access public
65 *
66 * @param string $module Module slug.
67 */
68 public function jetpack_deactivate_module( $module ) {
69 $this->tracking->record_user_event( 'module_deactivated', array( 'module' => $module ) );
70 }
71
72 /**
73 * Track that the user has successfully received an auth token.
74 *
75 * @access public
76 */
77 public function jetpack_user_authorized() {
78 $user_id = get_current_user_id();
79 $anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true );
80
81 if ( $anon_id ) {
82 $this->tracking->record_user_event( '_aliasUser', array( 'anonId' => $anon_id ) );
83 delete_user_meta( $user_id, 'jetpack_tracks_anon_id' );
84 if ( ! headers_sent() ) {
85 setcookie( 'tk_ai', 'expired', time() - 1000 );
86 }
87 }
88
89 $connection_manager = new Connection_Manager();
90 $wpcom_user_data = $connection_manager->get_connected_user_data( $user_id );
91 update_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'] );
92
93 $this->tracking->record_user_event( 'wpa_user_linked', array() );
94 }
95
96 /**
97 * Track that we've begun verifying the secrets.
98 *
99 * @access public
100 *
101 * @param string $action Type of secret (one of 'register', 'authorize', 'publicize').
102 * @param \WP_User $user The user object.
103 */
104 public function jetpack_verify_secrets_begin( $action, $user ) {
105 $this->tracking->record_user_event( "jpc_verify_{$action}_begin", array(), $user );
106 }
107
108 /**
109 * Track that we've succeeded in verifying the secrets.
110 *
111 * @access public
112 *
113 * @param string $action Type of secret (one of 'register', 'authorize', 'publicize').
114 * @param \WP_User $user The user object.
115 */
116 public function jetpack_verify_secrets_success( $action, $user ) {
117 $this->tracking->record_user_event( "jpc_verify_{$action}_success", array(), $user );
118 }
119
120 /**
121 * Track that we've failed verifying the secrets.
122 *
123 * @access public
124 *
125 * @param string $action Type of secret (one of 'register', 'authorize', 'publicize').
126 * @param \WP_User $user The user object.
127 * @param \WP_Error $error Error object.
128 */
129 public function jetpack_verify_secrets_fail( $action, $user, $error ) {
130 $this->tracking->record_user_event(
131 "jpc_verify_{$action}_fail",
132 array(
133 'error_code' => $error->get_error_code(),
134 'error_message' => $error->get_error_message(),
135 ),
136 $user
137 );
138 }
139
140 /**
141 * Track a failed login attempt.
142 *
143 * @access public
144 *
145 * @param string $login Username or email address.
146 */
147 public function wp_login_failed( $login ) {
148 require_once JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php';
149 $this->tracking->record_user_event(
150 'failed_login',
151 array(
152 'origin_ip' => jetpack_protect_get_ip(),
153 'login' => $login,
154 )
155 );
156 }
157
158 /**
159 * Track a connection failure at the registration step.
160 *
161 * @access public
162 *
163 * @param string|int $error The error code.
164 * @param \WP_Error $registered The error object.
165 */
166 function jetpack_connection_register_fail( $error, $registered ) {
167 $this->tracking->record_user_event( 'jpc_register_fail', array(
168 'error_code' => $error,
169 'error_message' => $registered->get_error_message()
170 ) );
171 }
172
173 /**
174 * Track that the registration step of the connection has been successful.
175 *
176 * @access public
177 *
178 * @param string $from The 'from' GET parameter.
179 */
180 function jetpack_connection_register_success( $from ) {
181 $this->tracking->record_user_event( 'jpc_register_success', array(
182 'from' => $from
183 ) );
184 }
185
186 /**
187 * Track that the site is incorrectly double-encoding redirects from http to https.
188 *
189 * @access public
190 */
191 function jetpack_verify_api_authorization_request_error_double_encode() {
192 $this->tracking->record_user_event( 'error_double_encode' );
193 }
194
195 /**
196 * Universal method for for all tracking events triggered via the JavaScript client.
197 *
198 * @access public
199 */
200 function wp_ajax_jetpack_tracks() {
201 // Check for nonce
202 if ( ! isset( $_REQUEST['tracksNonce'] ) || ! wp_verify_nonce( $_REQUEST['tracksNonce'], 'jp-tracks-ajax-nonce' ) ) {
203 wp_die( 'Permissions check failed.' );
204 }
205
206 if ( ! isset( $_REQUEST['tracksEventName'] ) || ! isset( $_REQUEST['tracksEventType'] ) ) {
207 wp_die( 'No valid event name or type.' );
208 }
209
210 $tracks_data = array();
211 if ( 'click' === $_REQUEST['tracksEventType'] && isset( $_REQUEST['tracksEventProp'] ) ) {
212 if ( is_array( $_REQUEST['tracksEventProp'] ) ) {
213 $tracks_data = $_REQUEST['tracksEventProp'];
214 } else {
215 $tracks_data = array( 'clicked' => $_REQUEST['tracksEventProp'] );
216 }
217 }
218
219 $this->tracking->record_user_event( $_REQUEST['tracksEventName'], $tracks_data );
220 wp_send_json_success();
221 wp_die();
222 }
223 }
224