Completed
Push — update/centralise-tracking ( 6855e5 )
by
unknown
167:14 queued 160:12
created

jetpack_admin_ajax_tracks_callback()   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\Tracking;
3
4
use Automattic\Jetpack\Tracking;
5
6
class Jetpack_Usage {
7
8
	private $tracking;
9
10
	function init() {
11
		$this->tracking = new Tracking( 'jetpack' );
12
13
		// For tracking stuff via js/ajax
14
		add_action( 'admin_enqueue_scripts', array( $this->tracking, 'enqueue_tracks_scripts' ) );
15
16
		add_action( 'jetpack_activate_module', array( $this, 'track_activate_module' ), 1, 1 );
17
		add_action( 'jetpack_deactivate_module', array( $this, 'track_deactivate_module' ), 1, 1 );
18
		add_action( 'jetpack_user_authorized', array( $this, 'track_user_linked' ) );
19
		add_action( 'wp_login_failed', array( $this, 'track_failed_login_attempts' ) );
20
21
		// Universal ajax callback for all tracking events triggered via js
22
		add_action( 'wp_ajax_jetpack_tracks', array( $this, 'jetpack_admin_ajax_tracks_callback' ) );
23
24
		add_action( 'jetpack_verify_api_authorization_request_error_double_encode', array( $this, 'jetpack_verify_api_authorization_request_error_double_encode' ) );
25
		add_action( 'jpc_register_fail', array( $this, 'jpc_register_fail' ) );
26
		add_action( 'jpc_register_success', array( $this, 'jpc_register_success' ) );
27
	}
28
29
	/* Activated module */
30
	public function track_activate_module( $module ) {
31
		$this->tracking->record_user_event( 'module_activated', array( 'module' => $module ) );
32
	}
33
34
	/* Deactivated module */
35
	public function track_deactivate_module( $module ) {
36
		$this->tracking->record_user_event( 'module_deactivated', array( 'module' => $module ) );
37
	}
38
39
	/* User has linked their account */
40
	public function track_user_linked() {
41
		$user_id = get_current_user_id();
42
		$anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true );
43
44
		if ( $anon_id ) {
45
			$this->tracking->record_user_event( '_aliasUser', array( 'anonId' => $anon_id ) );
46
			delete_user_meta( $user_id, 'jetpack_tracks_anon_id' );
47
			if ( ! headers_sent() ) {
48
				setcookie( 'tk_ai', 'expired', time() - 1000 );
49
			}
50
		}
51
52
		$wpcom_user_data = self::get_connected_user_data( $user_id );
0 ignored issues
show
Bug introduced by
The method get_connected_user_data() does not seem to exist on object<Automattic\Jetpack\Tracking\Jetpack_Usage>.

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...
53
		update_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'] );
54
55
		$this->tracking->record_user_event( 'wpa_user_linked', array() );
56
	}
57
58
	/**
59
	 * Track that we've begun verifying the secrets.
60
	 *
61
	 * @access public
62
	 *
63
	 * @param string $action Type of secret (one of 'register', 'authorize', 'publicize').
64
	 * @param \WP_User $user The user object.
65
	 */
66
	public function track_jetpack_verify_secrets_begin( $action, $user ) {
67
		$this->tracking->record_user_event( "jpc_verify_{$action}_begin", array(), $user );
68
	}
69
70
	/**
71
	 * Track that we've succeeded in verifying the secrets.
72
	 *
73
	 * @access public
74
	 *
75
	 * @param string $action Type of secret (one of 'register', 'authorize', 'publicize').
76
	 * @param \WP_User $user The user object.
77
	 */
78
	public function track_jetpack_verify_secrets_success( $action, $user ) {
79
		$this->tracking->record_user_event( "jpc_verify_{$action}_success", array(), $user );
80
	}
81
82
	/**
83
	 * Track that we've failed verifying the secrets.
84
	 *
85
	 * @access public
86
	 *
87
	 * @param string $action Type of secret (one of 'register', 'authorize', 'publicize').
88
	 * @param \WP_User $user The user object.
89
	 * @param \WP_Error $error Error object.
90
	 */
91
	public function track_jetpack_verify_secrets_fail( $action, $user, $error ) {
92
		$this->tracking->record_user_event(
93
			"jpc_verify_{$action}_fail",
94
			array(
95
				'error_code'    => $error->get_error_code(),
96
				'error_message' => $error->get_error_message(),
97
			),
98
			$user
99
		);
100
	}
101
102
	/* Failed login attempts */
103
	public function track_failed_login_attempts( $login ) {
104
		require_once JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php';
105
		$this->tracking->record_user_event(
106
			'failed_login',
107
			array(
108
				'origin_ip' => jetpack_protect_get_ip(),
109
				'login'     => $login,
110
			)
111
		);
112
	}
113
114
	function jpc_register_fail( $error, $registered ) {
115
		do_action( 'jpc_register_fail', $error );
116
		$this->tracking->record_user_event( 'jpc_register_fail', array(
117
			'error_code'    => $error,
118
			'error_message' => $registered->get_error_message()
119
		) );
120
	}
121
122
	function jpc_register_success( $from ) {
123
		$this->tracking->record_user_event( 'jpc_register_success', array(
124
			'from' => $from
125
		) );
126
	}
127
128
	function jetpack_verify_api_authorization_request_error_double_encode() {
129
		$this->tracking->record_user_event( 'error_double_encode' );
130
	}
131
132
	function jetpack_admin_ajax_tracks_callback() {
133
		// Check for nonce
134
		if ( ! isset( $_REQUEST['tracksNonce'] ) || ! wp_verify_nonce( $_REQUEST['tracksNonce'], 'jp-tracks-ajax-nonce' ) ) {
135
			wp_die( 'Permissions check failed.' );
136
		}
137
138
		if ( ! isset( $_REQUEST['tracksEventName'] ) || ! isset( $_REQUEST['tracksEventType'] ) ) {
139
			wp_die( 'No valid event name or type.' );
140
		}
141
142
		$tracks_data = array();
143
		if ( 'click' === $_REQUEST['tracksEventType'] && isset( $_REQUEST['tracksEventProp'] ) ) {
144
			if ( is_array( $_REQUEST['tracksEventProp'] ) ) {
145
				$tracks_data = $_REQUEST['tracksEventProp'];
146
			} else {
147
				$tracks_data = array( 'clicked' => $_REQUEST['tracksEventProp'] );
148
			}
149
		}
150
151
		$this->tracking->record_user_event( $_REQUEST['tracksEventName'], $tracks_data );
152
		wp_send_json_success();
153
		wp_die();
154
	}
155
}
156