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

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