Completed
Push — add/product-ratings-to-search ( 91683c...b05261 )
by
unknown
344:08 queued 334:42
created

Tracking::wp_ajax_jetpack_tracks()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 12
nop 0
dl 0
loc 23
rs 8.4444
c 0
b 0
f 0
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
		// Universal ajax callback for all tracking events triggered via js.
34
		add_action( 'wp_ajax_jetpack_tracks', array( $this->tracking, 'ajax_tracks' ) );
35
36
		add_action( 'jetpack_activate_module', array( $this, 'jetpack_activate_module' ), 1, 1 );
37
		add_action( 'jetpack_deactivate_module', array( $this, 'jetpack_deactivate_module' ), 1, 1 );
38
		add_action( 'jetpack_user_authorized', array( $this, 'jetpack_user_authorized' ) );
39
		add_action( 'wp_login_failed', array( $this, 'wp_login_failed' ) );
40
41
		// Tracking XMLRPC server events.
42
		add_action( 'jetpack_xmlrpc_server_event', array( $this, 'jetpack_xmlrpc_server_event' ), 10, 4 );
43
44
		// Track that we've begun verifying the previously generated secret.
45
		add_action( 'jetpack_verify_secrets_begin', array( $this, 'jetpack_verify_secrets_begin' ), 10, 2 );
46
		add_action( 'jetpack_verify_secrets_success', array( $this, 'jetpack_verify_secrets_success' ), 10, 2 );
47
		add_action( 'jetpack_verify_secrets_fail', array( $this, 'jetpack_verify_secrets_fail' ), 10, 3 );
48
49
		add_action( 'jetpack_verify_api_authorization_request_error_double_encode', array( $this, 'jetpack_verify_api_authorization_request_error_double_encode' ) );
50
		add_action( 'jetpack_connection_register_fail', array( $this, 'jetpack_connection_register_fail' ), 10, 2 );
51
		add_action( 'jetpack_connection_register_success', array( $this, 'jetpack_connection_register_success' ) );
52
	}
53
54
	/**
55
	 * Track that a specific module has been activated.
56
	 *
57
	 * @access public
58
	 *
59
	 * @param string $module Module slug.
60
	 */
61
	public function jetpack_activate_module( $module ) {
62
		$this->tracking->record_user_event( 'module_activated', array( 'module' => $module ) );
63
	}
64
65
	/**
66
	 * Track that a specific module has been deactivated.
67
	 *
68
	 * @access public
69
	 *
70
	 * @param string $module Module slug.
71
	 */
72
	public function jetpack_deactivate_module( $module ) {
73
		$this->tracking->record_user_event( 'module_deactivated', array( 'module' => $module ) );
74
	}
75
76
	/**
77
	 * Track that the user has successfully received an auth token.
78
	 *
79
	 * @access public
80
	 */
81
	public function jetpack_user_authorized() {
82
		$user_id = get_current_user_id();
83
		$anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true );
84
85
		if ( $anon_id ) {
86
			$this->tracking->record_user_event( '_aliasUser', array( 'anonId' => $anon_id ) );
87
			delete_user_meta( $user_id, 'jetpack_tracks_anon_id' );
88
			if ( ! headers_sent() ) {
89
				setcookie( 'tk_ai', 'expired', time() - 1000 );
90
			}
91
		}
92
93
		$connection_manager = new Connection_Manager();
94
		$wpcom_user_data    = $connection_manager->get_connected_user_data( $user_id );
95
		if ( isset( $wpcom_user_data['ID'] ) ) {
96
			update_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'] );
97
		}
98
99
		$this->tracking->record_user_event( 'wpa_user_linked', array() );
100
	}
101
102
	/**
103
	 * Track that we've begun verifying the secrets.
104
	 *
105
	 * @access public
106
	 *
107
	 * @param string $action Type of secret (one of 'register', 'authorize', 'publicize').
108
	 * @param \WP_User $user The user object.
109
	 */
110
	public function jetpack_verify_secrets_begin( $action, $user ) {
111
		$this->tracking->record_user_event( "jpc_verify_{$action}_begin", array(), $user );
112
	}
113
114
	/**
115
	 * Track that we've succeeded in verifying the secrets.
116
	 *
117
	 * @access public
118
	 *
119
	 * @param string $action Type of secret (one of 'register', 'authorize', 'publicize').
120
	 * @param \WP_User $user The user object.
121
	 */
122
	public function jetpack_verify_secrets_success( $action, $user ) {
123
		$this->tracking->record_user_event( "jpc_verify_{$action}_success", array(), $user );
124
	}
125
126
	/**
127
	 * Track that we've failed verifying the secrets.
128
	 *
129
	 * @access public
130
	 *
131
	 * @param string $action Type of secret (one of 'register', 'authorize', 'publicize').
132
	 * @param \WP_User $user The user object.
133
	 * @param \WP_Error $error Error object.
134
	 */
135
	public function jetpack_verify_secrets_fail( $action, $user, $error ) {
136
		$this->tracking->record_user_event(
137
			"jpc_verify_{$action}_fail",
138
			array(
139
				'error_code'    => $error->get_error_code(),
0 ignored issues
show
Bug introduced by
The method get_error_code() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
140
				'error_message' => $error->get_error_message(),
0 ignored issues
show
Bug introduced by
The method get_error_message() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
141
			),
142
			$user
143
		);
144
	}
145
146
	/**
147
	 * Track a failed login attempt.
148
	 *
149
	 * @access public
150
	 *
151
	 * @param string $login Username or email address.
152
	 */
153
	public function wp_login_failed( $login ) {
154
		require_once JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php';
155
		$this->tracking->record_user_event(
156
			'failed_login',
157
			array(
158
				'origin_ip' => jetpack_protect_get_ip(),
159
				'login'     => $login,
160
			)
161
		);
162
	}
163
164
	/**
165
	 * Track a connection failure at the registration step.
166
	 *
167
	 * @access public
168
	 *
169
	 * @param string|int $error      The error code.
170
	 * @param \WP_Error  $registered The error object.
171
	 */
172
	function jetpack_connection_register_fail( $error, $registered ) {
173
		$this->tracking->record_user_event( 'jpc_register_fail', array(
174
			'error_code'    => $error,
175
			'error_message' => $registered->get_error_message()
0 ignored issues
show
Bug introduced by
The method get_error_message() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
176
		) );
177
	}
178
179
	/**
180
	 * Track that the registration step of the connection has been successful.
181
	 *
182
	 * @access public
183
	 *
184
	 * @param string $from The 'from' GET parameter.
185
	 */
186
	function jetpack_connection_register_success( $from ) {
187
		$this->tracking->record_user_event( 'jpc_register_success', array(
188
			'from' => $from
189
		) );
190
	}
191
192
	/**
193
	 * Handles the jetpack_xmlrpc_server_event action that combines several types of events that
194
	 * happen during request serving.
195
	 *
196
	 * @param String                   $action the action name, i.e., 'remote_authorize'.
197
	 * @param String                   $stage  the execution stage, can be 'begin', 'success', 'error', etc.
198
	 * @param array|WP_Error|IXR_Error $parameters (optional) extra parameters to be passed to the tracked action.
199
	 * @param WP_User                  $user (optional) the acting user.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $user not be WP_User|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
200
	 */
201
	public function jetpack_xmlrpc_server_event( $action, $stage, $parameters = array(), $user = null ) {
202
203
		if ( is_wp_error( $parameters ) ) {
204
			$parameters = array(
205
				'error_code'    => $parameters->get_error_code(),
206
				'error_message' => $parameters->get_error_message(),
207
			);
208
		} elseif ( is_a( $parameters, '\\IXR_Error' ) ) {
209
			$parameters = array(
210
				'error_code'    => $parameters->code,
211
				'error_message' => $parameters->message,
212
			);
213
		}
214
215
		$this->tracking->record_user_event( 'jpc_' . $action . '_' . $stage, $parameters, $user );
216
	}
217
218
	/**
219
	 * Track that the site is incorrectly double-encoding redirects from http to https.
220
	 *
221
	 * @access public
222
	 */
223
	function jetpack_verify_api_authorization_request_error_double_encode() {
224
		$this->tracking->record_user_event( 'error_double_encode' );
225
	}
226
}
227