1 | <?php |
---|
2 | /** |
---|
3 | * Tracks class. |
---|
4 | * |
---|
5 | * @package automattic/jetpack |
---|
6 | */ |
---|
7 | |
---|
8 | namespace Automattic\Jetpack\Plugin; |
---|
9 | |
---|
10 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
---|
11 | use Automattic\Jetpack\IP\Utils as IP_Utils; |
---|
12 | use Automattic\Jetpack\Tracking as Tracks; |
---|
13 | |
---|
14 | /** |
---|
15 | * Tracks class. |
---|
16 | */ |
---|
17 | class Tracking { |
---|
18 | /** |
---|
19 | * Tracking object. |
---|
20 | * |
---|
21 | * @var Tracks |
---|
22 | * |
---|
23 | * @access private |
---|
24 | */ |
---|
25 | private $tracking; |
---|
26 | /** |
---|
27 | * Prevents the Tracking from being intialized more then once. |
---|
28 | * |
---|
29 | * @var bool |
---|
30 | */ |
---|
31 | private $initalized = false; |
---|
32 | |
---|
33 | /** |
---|
34 | * Initialization function. |
---|
35 | */ |
---|
36 | public function init() { |
---|
37 | if ( $this->initalized ) { |
---|
38 | return; |
---|
39 | } |
---|
40 | $this->initalized = true; |
---|
41 | $this->tracking = new Tracks( 'jetpack' ); |
---|
42 | |
---|
43 | // For tracking stuff via js/ajax. |
---|
44 | add_action( 'admin_enqueue_scripts', array( $this->tracking, 'enqueue_tracks_scripts' ) ); |
---|
45 | |
---|
46 | add_action( 'jetpack_activate_module', array( $this, 'jetpack_activate_module' ), 1, 1 ); |
---|
47 | add_action( 'jetpack_deactivate_module', array( $this, 'jetpack_deactivate_module' ), 1, 1 ); |
---|
48 | add_action( 'jetpack_user_authorized', array( $this, 'jetpack_user_authorized' ) ); |
---|
49 | add_action( 'wp_login_failed', array( $this, 'wp_login_failed' ) ); |
---|
50 | |
---|
51 | // Tracking XMLRPC server events. |
---|
52 | add_action( 'jetpack_xmlrpc_server_event', array( $this, 'jetpack_xmlrpc_server_event' ), 10, 4 ); |
---|
53 | |
---|
54 | // Track that we've begun verifying the previously generated secret. |
---|
55 | add_action( 'jetpack_verify_secrets_begin', array( $this, 'jetpack_verify_secrets_begin' ), 10, 2 ); |
---|
56 | add_action( 'jetpack_verify_secrets_success', array( $this, 'jetpack_verify_secrets_success' ), 10, 2 ); |
---|
57 | add_action( 'jetpack_verify_secrets_fail', array( $this, 'jetpack_verify_secrets_fail' ), 10, 3 ); |
---|
58 | |
---|
59 | add_action( 'jetpack_verify_api_authorization_request_error_double_encode', array( $this, 'jetpack_verify_api_authorization_request_error_double_encode' ) ); |
---|
60 | add_action( 'jetpack_connection_register_fail', array( $this, 'jetpack_connection_register_fail' ), 10, 2 ); |
---|
61 | add_action( 'jetpack_connection_register_success', array( $this, 'jetpack_connection_register_success' ) ); |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * Track that a specific module has been activated. |
---|
66 | * |
---|
67 | * @access public |
---|
68 | * |
---|
69 | * @param string $module Module slug. |
---|
70 | */ |
---|
71 | public function jetpack_activate_module( $module ) { |
---|
72 | $this->tracking->record_user_event( 'module_activated', array( 'module' => $module ) ); |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * Track that a specific module has been deactivated. |
---|
77 | * |
---|
78 | * @access public |
---|
79 | * |
---|
80 | * @param string $module Module slug. |
---|
81 | */ |
---|
82 | public function jetpack_deactivate_module( $module ) { |
---|
83 | $this->tracking->record_user_event( 'module_deactivated', array( 'module' => $module ) ); |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Track that the user has successfully received an auth token. |
---|
88 | * |
---|
89 | * @access public |
---|
90 | */ |
---|
91 | public function jetpack_user_authorized() { |
---|
92 | $user_id = get_current_user_id(); |
---|
93 | $anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true ); |
---|
94 | |
---|
95 | if ( $anon_id ) { |
---|
96 | $this->tracking->record_user_event( '_aliasUser', array( 'anonId' => $anon_id ) ); |
---|
97 | delete_user_meta( $user_id, 'jetpack_tracks_anon_id' ); |
---|
98 | if ( ! headers_sent() ) { |
---|
99 | setcookie( 'tk_ai', 'expired', time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), false ); // phpcs:ignore Jetpack.Functions.SetCookie -- Want this accessible. |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | $connection_manager = new Connection_Manager(); |
---|
104 | $wpcom_user_data = $connection_manager->get_connected_user_data( $user_id ); |
---|
105 | if ( isset( $wpcom_user_data['ID'] ) ) { |
---|
106 | update_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'] ); |
---|
107 | } |
---|
108 | |
---|
109 | $this->tracking->record_user_event( 'wpa_user_linked', array() ); |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * Track that we've begun verifying the secrets. |
---|
114 | * |
---|
115 | * @access public |
---|
116 | * |
---|
117 | * @param string $action Type of secret (one of 'register', 'authorize', 'publicize'). |
---|
118 | * @param \WP_User $user The user object. |
---|
119 | */ |
---|
120 | public function jetpack_verify_secrets_begin( $action, $user ) { |
---|
121 | $this->tracking->record_user_event( "jpc_verify_{$action}_begin", array(), $user ); |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * Track that we've succeeded in verifying the secrets. |
---|
126 | * |
---|
127 | * @access public |
---|
128 | * |
---|
129 | * @param string $action Type of secret (one of 'register', 'authorize', 'publicize'). |
---|
130 | * @param \WP_User $user The user object. |
---|
131 | */ |
---|
132 | public function jetpack_verify_secrets_success( $action, $user ) { |
---|
133 | $this->tracking->record_user_event( "jpc_verify_{$action}_success", array(), $user ); |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * Track that we've failed verifying the secrets. |
---|
138 | * |
---|
139 | * @access public |
---|
140 | * |
---|
141 | * @param string $action Type of secret (one of 'register', 'authorize', 'publicize'). |
---|
142 | * @param \WP_User $user The user object. |
---|
143 | * @param \WP_Error $error Error object. |
---|
144 | */ |
---|
145 | public function jetpack_verify_secrets_fail( $action, $user, $error ) { |
---|
146 | $this->tracking->record_user_event( |
---|
147 | "jpc_verify_{$action}_fail", |
---|
148 | array( |
---|
149 | 'error_code' => $error->get_error_code(), |
---|
150 | 'error_message' => $error->get_error_message(), |
---|
151 | ), |
---|
152 | $user |
---|
153 | ); |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * Track a failed login attempt. |
---|
158 | * |
---|
159 | * @access public |
---|
160 | * |
---|
161 | * @param string $login Username or email address. |
---|
162 | */ |
---|
163 | public function wp_login_failed( $login ) { |
---|
164 | $this->tracking->record_user_event( |
---|
165 | 'failed_login', |
---|
166 | array( |
---|
167 | 'origin_ip' => IP_Utils::get_ip(), |
---|
168 | 'login' => $login, |
---|
169 | ) |
---|
170 | ); |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | * Track a connection failure at the registration step. |
---|
175 | * |
---|
176 | * @access public |
---|
177 | * |
---|
178 | * @param string|int $error The error code. |
---|
179 | * @param \WP_Error $registered The error object. |
---|
180 | */ |
---|
181 | public function jetpack_connection_register_fail( $error, $registered ) { |
---|
182 | $this->tracking->record_user_event( |
---|
183 | 'jpc_register_fail', |
---|
184 | array( |
---|
185 | 'error_code' => $error, |
---|
186 | 'error_message' => $registered->get_error_message(), |
---|
187 | ) |
---|
188 | ); |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | * Track that the registration step of the connection has been successful. |
---|
193 | * |
---|
194 | * @access public |
---|
195 | * |
---|
196 | * @param string $from The 'from' GET parameter. |
---|
197 | */ |
---|
198 | public function jetpack_connection_register_success( $from ) { |
---|
199 | $this->tracking->record_user_event( |
---|
200 | 'jpc_register_success', |
---|
201 | array( |
---|
202 | 'from' => $from, |
---|
203 | ) |
---|
204 | ); |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * Handles the jetpack_xmlrpc_server_event action that combines several types of events that |
---|
209 | * happen during request serving. |
---|
210 | * |
---|
211 | * @param String $action the action name, i.e., 'remote_authorize'. |
---|
212 | * @param String $stage the execution stage, can be 'begin', 'success', 'error', etc. |
---|
213 | * @param array|WP_Error|IXR_Error $parameters (optional) extra parameters to be passed to the tracked action. |
---|
214 | * @param WP_User $user (optional) the acting user. |
---|
215 | */ |
---|
216 | public function jetpack_xmlrpc_server_event( $action, $stage, $parameters = array(), $user = null ) { |
---|
217 | |
---|
218 | if ( is_wp_error( $parameters ) ) { |
---|
219 | $parameters = array( |
---|
220 | 'error_code' => $parameters->get_error_code(), |
---|
221 | 'error_message' => $parameters->get_error_message(), |
---|
222 | ); |
---|
223 | } elseif ( is_a( $parameters, '\\IXR_Error' ) ) { |
---|
224 | $parameters = array( |
---|
225 | 'error_code' => $parameters->code, |
---|
226 | 'error_message' => $parameters->message, |
---|
227 | ); |
---|
228 | } |
---|
229 | |
---|
230 | $this->tracking->record_user_event( 'jpc_' . $action . '_' . $stage, $parameters, $user ); |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * Track that the site is incorrectly double-encoding redirects from http to https. |
---|
235 | * |
---|
236 | * @access public |
---|
237 | */ |
---|
238 | public function jetpack_verify_api_authorization_request_error_double_encode() { |
---|
239 | $this->tracking->record_user_event( 'error_double_encode' ); |
---|
240 | } |
---|
241 | } |
---|