| 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 |
function init() { |
| 18 |
$this->tracking = new Tracks( 'jetpack' ); |
| 19 |
|
| 20 |
// For tracking stuff via js/ajax |
| 21 |
add_action( 'admin_enqueue_scripts', array( $this->tracking, 'enqueue_tracks_scripts' ) ); |
| 22 |
|
| 23 |
add_action( 'jetpack_activate_module', array( $this, 'jetpack_activate_module' ), 1, 1 ); |
| 24 |
add_action( 'jetpack_deactivate_module', array( $this, 'jetpack_deactivate_module' ), 1, 1 ); |
| 25 |
add_action( 'jetpack_user_authorized', array( $this, 'jetpack_user_authorized' ) ); |
| 26 |
add_action( 'wp_login_failed', array( $this, 'wp_login_failed' ) ); |
| 27 |
|
| 28 |
// Track that we've begun verifying the previously generated secret. |
| 29 |
add_action( 'jetpack_verify_secrets_begin', array( $this, 'jetpack_verify_secrets_begin' ), 10, 2 ); |
| 30 |
add_action( 'jetpack_verify_secrets_success', array( $this, 'jetpack_verify_secrets_success' ), 10, 2 ); |
| 31 |
add_action( 'jetpack_verify_secrets_fail', array( $this, 'jetpack_verify_secrets_fail' ), 10, 3 ); |
| 32 |
|
| 33 |
// Universal ajax callback for all tracking events triggered via js |
| 34 |
add_action( 'wp_ajax_jetpack_tracks', array( $this, 'wp_ajax_jetpack_tracks' ) ); |
| 35 |
|
| 36 |
add_action( 'jetpack_verify_api_authorization_request_error_double_encode', array( $this, 'jetpack_verify_api_authorization_request_error_double_encode' ) ); |
| 37 |
add_action( 'jetpack_connection_register_fail', array( $this, 'jetpack_connection_register_fail' ), 10, 2 ); |
| 38 |
add_action( 'jetpack_connection_register_success', array( $this, 'jetpack_connection_register_success' ) ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Track that a specific module has been activated. |
| 43 |
* |
| 44 |
* @access public |
| 45 |
* |
| 46 |
* @param string $module Module slug. |
| 47 |
*/ |
| 48 |
public function jetpack_activate_module( $module ) { |
| 49 |
$this->tracking->record_user_event( 'module_activated', array( 'module' => $module ) ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Track that a specific module has been deactivated. |
| 54 |
* |
| 55 |
* @access public |
| 56 |
* |
| 57 |
* @param string $module Module slug. |
| 58 |
*/ |
| 59 |
public function jetpack_deactivate_module( $module ) { |
| 60 |
$this->tracking->record_user_event( 'module_deactivated', array( 'module' => $module ) ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Track that the user has successfully received an auth token. |
| 65 |
* |
| 66 |
* @access public |
| 67 |
*/ |
| 68 |
public function jetpack_user_authorized() { |
| 69 |
$user_id = get_current_user_id(); |
| 70 |
$anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true ); |
| 71 |
|
| 72 |
if ( $anon_id ) { |
| 73 |
$this->tracking->record_user_event( '_aliasUser', array( 'anonId' => $anon_id ) ); |
| 74 |
delete_user_meta( $user_id, 'jetpack_tracks_anon_id' ); |
| 75 |
if ( ! headers_sent() ) { |
| 76 |
setcookie( 'tk_ai', 'expired', time() - 1000 ); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
$connection_manager = new Connection_Manager(); |
| 81 |
$wpcom_user_data = $connection_manager->get_connected_user_data( $user_id ); |
| 82 |
update_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'] ); |
| 83 |
|
| 84 |
$this->tracking->record_user_event( 'wpa_user_linked', array() ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Track that we've begun verifying the secrets. |
| 89 |
* |
| 90 |
* @access public |
| 91 |
* |
| 92 |
* @param string $action Type of secret (one of 'register', 'authorize', 'publicize'). |
| 93 |
* @param \WP_User $user The user object. |
| 94 |
*/ |
| 95 |
public function jetpack_verify_secrets_begin( $action, $user ) { |
| 96 |
$this->tracking->record_user_event( "jpc_verify_{$action}_begin", array(), $user ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Track that we've succeeded in 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_success( $action, $user ) { |
| 108 |
$this->tracking->record_user_event( "jpc_verify_{$action}_success", array(), $user ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Track that we've failed 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 |
* @param \WP_Error $error Error object. |
| 119 |
*/ |
| 120 |
public function jetpack_verify_secrets_fail( $action, $user, $error ) { |
| 121 |
$this->tracking->record_user_event( |
| 122 |
"jpc_verify_{$action}_fail", |
| 123 |
array( |
| 124 |
'error_code' => $error->get_error_code(), |
| 125 |
'error_message' => $error->get_error_message(), |
| 126 |
), |
| 127 |
$user |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Track a failed login attempt. |
| 133 |
* |
| 134 |
* @access public |
| 135 |
* |
| 136 |
* @param string $login Username or email address. |
| 137 |
*/ |
| 138 |
public function wp_login_failed( $login ) { |
| 139 |
require_once JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php'; |
| 140 |
$this->tracking->record_user_event( |
| 141 |
'failed_login', |
| 142 |
array( |
| 143 |
'origin_ip' => jetpack_protect_get_ip(), |
| 144 |
'login' => $login, |
| 145 |
) |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Track a connection failure at the registration step. |
| 151 |
* |
| 152 |
* @access public |
| 153 |
* |
| 154 |
* @param string|int $error The error code. |
| 155 |
* @param \WP_Error $registered The error object. |
| 156 |
*/ |
| 157 |
function jetpack_connection_register_fail( $error, $registered ) { |
| 158 |
$this->tracking->record_user_event( 'jpc_register_fail', array( |
| 159 |
'error_code' => $error, |
| 160 |
'error_message' => $registered->get_error_message() |
| 161 |
) ); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Track that the registration step of the connection has been successful. |
| 166 |
* |
| 167 |
* @access public |
| 168 |
* |
| 169 |
* @param string $from The 'from' GET parameter. |
| 170 |
*/ |
| 171 |
function jetpack_connection_register_success( $from ) { |
| 172 |
$this->tracking->record_user_event( 'jpc_register_success', array( |
| 173 |
'from' => $from |
| 174 |
) ); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Track that the site is incorrectly double-encoding redirects from http to https. |
| 179 |
* |
| 180 |
* @access public |
| 181 |
*/ |
| 182 |
function jetpack_verify_api_authorization_request_error_double_encode() { |
| 183 |
$this->tracking->record_user_event( 'error_double_encode' ); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Universal method for for all tracking events triggered via the JavaScript client. |
| 188 |
* |
| 189 |
* @access public |
| 190 |
*/ |
| 191 |
function wp_ajax_jetpack_tracks() { |
| 192 |
// Check for nonce |
| 193 |
if ( ! isset( $_REQUEST['tracksNonce'] ) || ! wp_verify_nonce( $_REQUEST['tracksNonce'], 'jp-tracks-ajax-nonce' ) ) { |
| 194 |
wp_die( 'Permissions check failed.' ); |
| 195 |
} |
| 196 |
|
| 197 |
if ( ! isset( $_REQUEST['tracksEventName'] ) || ! isset( $_REQUEST['tracksEventType'] ) ) { |
| 198 |
wp_die( 'No valid event name or type.' ); |
| 199 |
} |
| 200 |
|
| 201 |
$tracks_data = array(); |
| 202 |
if ( 'click' === $_REQUEST['tracksEventType'] && isset( $_REQUEST['tracksEventProp'] ) ) { |
| 203 |
if ( is_array( $_REQUEST['tracksEventProp'] ) ) { |
| 204 |
$tracks_data = $_REQUEST['tracksEventProp']; |
| 205 |
} else { |
| 206 |
$tracks_data = array( 'clicked' => $_REQUEST['tracksEventProp'] ); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
$this->tracking->record_user_event( $_REQUEST['tracksEventName'], $tracks_data ); |
| 211 |
wp_send_json_success(); |
| 212 |
wp_die(); |
| 213 |
} |
| 214 |
} |
| 215 |
|