PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 13.4.4
Jetpack – WP Security, Backup, Speed, & Growth v13.4.4
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 13.4.4, at src/class-tracking.php

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