PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 11.2.2
Jetpack – WP Security, Backup, Speed, & Growth v11.2.2
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 / class-tracking.php

class-tracking.php in Jetpack – WP Security, Backup, Speed, & Growth 11.2.2, at src/class-tracking.php

242 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Tracking as Tracks;
12
13 /**
14 * Tracks class.
15 */
16 class Tracking {
17 /**
18 * Tracking object.
19 *
20 * @var Tracks
21 *
22 * @access private
23 */
24 private $tracking;
25 /**
26 * Prevents the Tracking from being intialized more then once.
27 *
28 * @var bool
29 */
30 private $initalized = false;
31
32 /**
33 * Initialization function.
34 */
35 public function init() {
36 if ( $this->initalized ) {
37 return;
38 }
39 $this->initalized = true;
40 $this->tracking = new Tracks( 'jetpack' );
41
42 // For tracking stuff via js/ajax.
43 add_action( 'admin_enqueue_scripts', array( $this->tracking, 'enqueue_tracks_scripts' ) );
44
45 add_action( 'jetpack_activate_module', array( $this, 'jetpack_activate_module' ), 1, 1 );
46 add_action( 'jetpack_deactivate_module', array( $this, 'jetpack_deactivate_module' ), 1, 1 );
47 add_action( 'jetpack_user_authorized', array( $this, 'jetpack_user_authorized' ) );
48 add_action( 'wp_login_failed', array( $this, 'wp_login_failed' ) );
49
50 // Tracking XMLRPC server events.
51 add_action( 'jetpack_xmlrpc_server_event', array( $this, 'jetpack_xmlrpc_server_event' ), 10, 4 );
52
53 // Track that we've begun verifying the previously generated secret.
54 add_action( 'jetpack_verify_secrets_begin', array( $this, 'jetpack_verify_secrets_begin' ), 10, 2 );
55 add_action( 'jetpack_verify_secrets_success', array( $this, 'jetpack_verify_secrets_success' ), 10, 2 );
56 add_action( 'jetpack_verify_secrets_fail', array( $this, 'jetpack_verify_secrets_fail' ), 10, 3 );
57
58 add_action( 'jetpack_verify_api_authorization_request_error_double_encode', array( $this, 'jetpack_verify_api_authorization_request_error_double_encode' ) );
59 add_action( 'jetpack_connection_register_fail', array( $this, 'jetpack_connection_register_fail' ), 10, 2 );
60 add_action( 'jetpack_connection_register_success', array( $this, 'jetpack_connection_register_success' ) );
61 }
62
63 /**
64 * Track that a specific module has been activated.
65 *
66 * @access public
67 *
68 * @param string $module Module slug.
69 */
70 public function jetpack_activate_module( $module ) {
71 $this->tracking->record_user_event( 'module_activated', array( 'module' => $module ) );
72 }
73
74 /**
75 * Track that a specific module has been deactivated.
76 *
77 * @access public
78 *
79 * @param string $module Module slug.
80 */
81 public function jetpack_deactivate_module( $module ) {
82 $this->tracking->record_user_event( 'module_deactivated', array( 'module' => $module ) );
83 }
84
85 /**
86 * Track that the user has successfully received an auth token.
87 *
88 * @access public
89 */
90 public function jetpack_user_authorized() {
91 $user_id = get_current_user_id();
92 $anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true );
93
94 if ( $anon_id ) {
95 $this->tracking->record_user_event( '_aliasUser', array( 'anonId' => $anon_id ) );
96 delete_user_meta( $user_id, 'jetpack_tracks_anon_id' );
97 if ( ! headers_sent() ) {
98 setcookie( 'tk_ai', 'expired', time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), false ); // phpcs:ignore Jetpack.Functions.SetCookie -- Want this accessible.
99 }
100 }
101
102 $connection_manager = new Connection_Manager();
103 $wpcom_user_data = $connection_manager->get_connected_user_data( $user_id );
104 if ( isset( $wpcom_user_data['ID'] ) ) {
105 update_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'] );
106 }
107
108 $this->tracking->record_user_event( 'wpa_user_linked', array() );
109 }
110
111 /**
112 * Track that we've begun verifying the secrets.
113 *
114 * @access public
115 *
116 * @param string $action Type of secret (one of 'register', 'authorize', 'publicize').
117 * @param \WP_User $user The user object.
118 */
119 public function jetpack_verify_secrets_begin( $action, $user ) {
120 $this->tracking->record_user_event( "jpc_verify_{$action}_begin", array(), $user );
121 }
122
123 /**
124 * Track that we've succeeded in verifying the secrets.
125 *
126 * @access public
127 *
128 * @param string $action Type of secret (one of 'register', 'authorize', 'publicize').
129 * @param \WP_User $user The user object.
130 */
131 public function jetpack_verify_secrets_success( $action, $user ) {
132 $this->tracking->record_user_event( "jpc_verify_{$action}_success", array(), $user );
133 }
134
135 /**
136 * Track that we've failed verifying the secrets.
137 *
138 * @access public
139 *
140 * @param string $action Type of secret (one of 'register', 'authorize', 'publicize').
141 * @param \WP_User $user The user object.
142 * @param \WP_Error $error Error object.
143 */
144 public function jetpack_verify_secrets_fail( $action, $user, $error ) {
145 $this->tracking->record_user_event(
146 "jpc_verify_{$action}_fail",
147 array(
148 'error_code' => $error->get_error_code(),
149 'error_message' => $error->get_error_message(),
150 ),
151 $user
152 );
153 }
154
155 /**
156 * Track a failed login attempt.
157 *
158 * @access public
159 *
160 * @param string $login Username or email address.
161 */
162 public function wp_login_failed( $login ) {
163 require_once JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php';
164 $this->tracking->record_user_event(
165 'failed_login',
166 array(
167 'origin_ip' => jetpack_protect_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 }
242