1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Google Client admin class. |
4
|
|
|
* |
5
|
|
|
* Handles retrieving whether a particular notice has been dismissed or not, |
6
|
|
|
* as well as marking a notice as dismissed. |
7
|
|
|
* |
8
|
|
|
* @since 7.0.0 |
9
|
|
|
* |
10
|
|
|
* @package MonsterInsights |
11
|
|
|
* @subpackage GA Client |
12
|
|
|
* @author Chris Christoff |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
// Exit if accessed directly |
16
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
17
|
|
|
exit; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
final class MonsterInsights_API_Auth { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Primary class constructor. |
24
|
|
|
* |
25
|
|
|
* @access public |
26
|
|
|
* @since 7.0.0 |
27
|
|
|
*/ |
28
|
|
|
public function __construct() { |
29
|
|
|
|
30
|
|
|
// Authentication Actions |
31
|
|
|
add_action( 'wp_ajax_monsterinsights_maybe_authenticate', array( $this, 'maybe_authenticate' ) ); |
32
|
|
|
add_action( 'wp_ajax_monsterinsights_maybe_reauthenticate', array( $this, 'maybe_reauthenticate' ) ); |
33
|
|
|
add_action( 'wp_ajax_monsterinsights_maybe_verify', array( $this, 'maybe_verify' ) ); |
34
|
|
|
add_action( 'wp_ajax_monsterinsights_maybe_delete', array( $this, 'maybe_delete' ) ); |
35
|
|
|
|
36
|
|
|
add_action( 'admin_init', array( $this, 'authenticate_listener' ) ); |
37
|
|
|
add_action( 'admin_init', array( $this, 'reauthenticate_listener' ) ); |
38
|
|
|
|
39
|
|
|
add_action( 'wp_ajax_nopriv_monsterinsights_is_installed', array( $this, 'is_installed' ) ); |
40
|
|
|
add_action( 'wp_ajax_nopriv_monsterinsights_rauthenticate', array( $this, 'rauthenticate' ) ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function get_tt(){ |
44
|
|
|
$tt = is_network_admin() ? get_site_option( 'monsterinsights_network_tt', '' ) : get_option( 'monsterinsights_site_tt', '' ); |
45
|
|
|
if ( empty( $tt ) ) { |
46
|
|
|
// if TT is empty, generate a new one, save it and then return it |
47
|
|
|
$tt = $this->generate_tt(); |
48
|
|
|
$this->is_network_admin() ? update_site_option( 'monsterinsights_network_tt', $tt ) : update_option( 'monsterinsights_site_tt', $tt ); |
49
|
|
|
} |
50
|
|
|
return $tt; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function rotate_tt(){ |
54
|
|
|
$tt = $this->generate_tt(); |
55
|
|
|
is_network_admin() ? update_site_option( 'monsterinsights_network_tt', $tt ) : update_option( 'monsterinsights_site_tt', $tt ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function generate_tt(){ |
59
|
|
|
return hash( 'sha512', wp_generate_password( 128, true, true ) . AUTH_SALT . uniqid( "", true ) ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function validate_tt( $passed_tt = '' ) { |
63
|
|
|
$tt = $this->get_tt(); |
64
|
|
|
return hash_equals( $tt, $passed_tt ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function is_installed() { |
68
|
|
|
wp_send_json_success( |
69
|
|
|
array( |
70
|
|
|
'version' => MONSTERINSIGHTS_VERSION, |
71
|
|
|
'pro' => monsterinsights_is_pro_version(), |
72
|
|
|
) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function maybe_authenticate(){ |
77
|
|
|
|
78
|
|
|
// Check nonce |
79
|
|
|
check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
80
|
|
|
|
81
|
|
|
// current user can authenticate |
82
|
|
|
if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
83
|
|
|
wp_send_json_error( array( 'message' => __( "You don't have permission to authenticate MonsterInsights.", 'google-analytics-for-wordpress' ) ) ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ( ! empty( $_REQUEST['isnetwork'] ) && $_REQUEST['isnetwork'] ) { |
87
|
|
|
define( 'WP_NETWORK_ADMIN', true ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Only for Pro users, require a license key to be entered first so we can link to things. |
91
|
|
|
$valid = is_network_admin() ? MonsterInsights()->license->is_network_licensed() : MonsterInsights()->license->is_site_licensed(); |
92
|
|
|
if ( monsterinsights_is_pro_version() && ! $valid ) { |
93
|
|
|
wp_send_json_error( array( 'message' => __( "Cannot authenticate. Please enter a valid, active license key for MonsterInsights Pro into the settings.", 'google-analytics-for-wordpress' ) ) ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// we do not have a current auth |
97
|
|
|
if ( ! $this->is_network_admin() && MonsterInsights()->auth->is_authed() ) { |
98
|
|
|
wp_send_json_error( array( 'message' => __( "Cannot authenticate. Please re-authenticate.", 'google-analytics-for-wordpress' ) ) ); |
99
|
|
|
} else if ( $this->is_network_admin() && MonsterInsights()->auth->is_network_authed() ) { |
100
|
|
|
wp_send_json_error( array( 'message' => __( "Cannot network authenticate. Please re-authenticate on the network settings panel.", 'google-analytics-for-wordpress' ) ) ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$sitei = $this->get_sitei(); |
104
|
|
|
//update_network_option( get_current_network_id(), 'monsterinsights_network_sitei', $sitei ); |
105
|
|
|
|
106
|
|
|
$siteurl = add_query_arg( array( |
107
|
|
|
'tt' => $this->get_tt(), |
108
|
|
|
'sitei' => $sitei, |
109
|
|
|
'miversion' => MONSTERINSIGHTS_VERSION, |
110
|
|
|
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
111
|
|
|
'network' => is_network_admin() ? 'network' : 'site', |
112
|
|
|
'siteurl' => is_network_admin() ? network_admin_url() : site_url(), |
113
|
|
|
'return' => is_network_admin() ? network_admin_url( 'admin.php?page=monsterinsights_network' ) : admin_url( 'admin.php?page=monsterinsights_settings' ), |
114
|
|
|
), $this->get_route( 'https://' . monsterinsights_get_api_url() . 'auth/new/{type}' ) ); |
115
|
|
|
|
116
|
|
|
if ( monsterinsights_is_pro_version() ) { |
117
|
|
|
$key = is_network_admin() ? MonsterInsights()->license->get_network_license_key() : MonsterInsights()->license->get_site_license_key(); |
118
|
|
|
$siteurl = add_query_arg( 'license', $key, $siteurl ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$siteurl = apply_filters( 'monsterinsights_maybe_authenticate_siteurl', $siteurl ); |
122
|
|
|
wp_send_json_success( array( 'redirect' => $siteurl ) ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function rauthenticate() { |
126
|
|
|
// Check for missing params |
127
|
|
|
$reqd_args = array( 'key', 'token', 'ua', 'miview', 'a', 'w', 'p', 'tt', 'network' ); |
128
|
|
|
foreach ( $reqd_args as $arg ) { |
129
|
|
|
if ( empty( $_REQUEST[$arg] ) ) { |
130
|
|
|
wp_send_json_error( |
131
|
|
|
array( |
132
|
|
|
'error' => 'authenticate_missing_arg', |
133
|
|
|
'message' => 'Authenticate missing parameter: ' . $arg, |
134
|
|
|
'version' => MONSTERINSIGHTS_VERSION, |
135
|
|
|
'pro' => monsterinsights_is_pro_version(), |
136
|
|
|
) |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ( ! $this->validate_tt( $_REQUEST['tt'] ) ) { |
142
|
|
|
wp_send_json_error( |
143
|
|
|
array( |
144
|
|
|
'error' => 'authenticate_invalid_tt', |
145
|
|
|
'message' => 'Invalid TT sent', |
146
|
|
|
'version' => MONSTERINSIGHTS_VERSION, |
147
|
|
|
'pro' => monsterinsights_is_pro_version(), |
148
|
|
|
) |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// If the tt is validated, send a success response to trigger the regular auth process. |
153
|
|
|
wp_send_json_success(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function authenticate_listener(){ |
157
|
|
|
// Make sure it's for us |
158
|
|
|
if ( empty( $_REQUEST['mi-oauth-action'] ) || $_REQUEST['mi-oauth-action'] !== 'auth' ) { |
159
|
|
|
return; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// User can authenticate |
163
|
|
|
if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
164
|
|
|
return; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// Invalid request |
168
|
|
|
if ( empty( $_REQUEST['tt'] ) || ! $this->validate_tt( $_REQUEST['tt'] ) ) { |
169
|
|
|
return; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// Make sure has required params |
173
|
|
|
if ( empty( $_REQUEST['key'] ) || |
174
|
|
|
empty( $_REQUEST['token'] ) || |
175
|
|
|
empty( $_REQUEST['ua'] ) || |
176
|
|
|
empty( $_REQUEST['miview'] ) || |
177
|
|
|
empty( $_REQUEST['a'] ) || |
178
|
|
|
empty( $_REQUEST['w'] ) || |
179
|
|
|
empty( $_REQUEST['p'] ) |
180
|
|
|
) { |
181
|
|
|
return; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// Invalid UA code |
185
|
|
|
$ua = monsterinsights_is_valid_ua( $_REQUEST['ua'] ); |
186
|
|
|
if ( empty( $ua ) ) { |
187
|
|
|
return; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$profile = array( |
191
|
|
|
'key' => sanitize_text_field( $_REQUEST['key'] ), |
192
|
|
|
'token' => sanitize_text_field( $_REQUEST['token'] ), |
193
|
|
|
'ua' => monsterinsights_is_valid_ua( $_REQUEST['ua'] ), |
194
|
|
|
'viewname' => sanitize_text_field( $_REQUEST['miview'] ), |
195
|
|
|
'a' => sanitize_text_field( $_REQUEST['a'] ), // AccountID |
196
|
|
|
'w' => sanitize_text_field( $_REQUEST['w'] ), // PropertyID |
197
|
|
|
'p' => sanitize_text_field( $_REQUEST['p'] ), // View ID |
198
|
|
|
'siteurl' => site_url(), |
199
|
|
|
'neturl' => network_admin_url(), |
200
|
|
|
); |
201
|
|
|
|
202
|
|
|
$worked = $this->verify_auth( $profile ); |
203
|
|
|
if ( ! $worked || is_wp_error( $worked ) ) { |
204
|
|
|
return; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
// Rotate tt |
208
|
|
|
$this->rotate_tt(); |
209
|
|
|
|
210
|
|
|
// Save Profile |
211
|
|
|
$this->is_network_admin() ? MonsterInsights()->auth->set_network_analytics_profile( $profile ) : MonsterInsights()->auth->set_analytics_profile( $profile ); |
|
|
|
|
212
|
|
|
|
213
|
|
|
// Clear cache |
214
|
|
|
$where = $this->is_network_admin() ? 'network' : 'site'; |
215
|
|
|
MonsterInsights()->reporting->delete_aggregate_data( $where ); |
216
|
|
|
|
217
|
|
|
$url = $this->is_network_admin() ? network_admin_url( 'admin.php?page=monsterinsights_network' ) : admin_url( 'admin.php?page=monsterinsights_settings' ) ; |
218
|
|
|
$url = add_query_arg( array( |
219
|
|
|
'mi_action' => 'auth', |
220
|
|
|
'success' => 'true', |
221
|
|
|
), $url ); |
222
|
|
|
wp_safe_redirect( $url ); |
223
|
|
|
exit; |
|
|
|
|
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function maybe_reauthenticate(){ |
227
|
|
|
|
228
|
|
|
// Check nonce |
229
|
|
|
check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
230
|
|
|
|
231
|
|
|
// current user can authenticate |
232
|
|
|
if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
233
|
|
|
wp_send_json_error( array( 'message' => __( "You don't have permission to re-authenticate MonsterInsights.", 'google-analytics-for-wordpress' ) ) ); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if ( ! empty( $_REQUEST['isnetwork'] ) && $_REQUEST['isnetwork'] ) { |
237
|
|
|
define( 'WP_NETWORK_ADMIN', true ); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
// Only for Pro users, require a license key to be entered first so we can link to things. |
241
|
|
|
$valid = is_network_admin() ? MonsterInsights()->license->is_network_licensed() : MonsterInsights()->license->is_site_licensed(); |
242
|
|
|
if ( monsterinsights_is_pro_version() && ! $valid ) { |
243
|
|
|
wp_send_json_error( array( 'message' => __( "Cannot re-authenticate. Please enter a valid, active license key for MonsterInsights Pro into the settings.", 'google-analytics-for-wordpress' ) ) ); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
// we do have a current auth |
247
|
|
|
if ( ! $this->is_network_admin() && ! MonsterInsights()->auth->is_authed() ) { |
248
|
|
|
wp_send_json_error( array( 'message' => __( "Cannot re-authenticate. Please authenticate.", 'google-analytics-for-wordpress' ) ) ); |
249
|
|
|
} else if ( $this->is_network_admin() && ! MonsterInsights()->auth->is_network_authed() ) { |
250
|
|
|
wp_send_json_error( array( 'message' => __( "Cannot re-authenticate the network. Please authenticate on the network settings panel.", 'google-analytics-for-wordpress' ) ) ); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
$siteurl = add_query_arg( array( |
254
|
|
|
'tt' => $this->get_tt(), |
255
|
|
|
'sitei' => $this->get_sitei(), |
256
|
|
|
'miversion' => MONSTERINSIGHTS_VERSION, |
257
|
|
|
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
258
|
|
|
'network' => is_network_admin() ? 'network' : 'site', |
259
|
|
|
'siteurl' => is_network_admin() ? network_admin_url() : site_url(), |
260
|
|
|
'key' => MonsterInsights()->auth->get_key(), |
261
|
|
|
'token' => MonsterInsights()->auth->get_token(), |
262
|
|
|
'return' => is_network_admin() ? network_admin_url( 'admin.php?page=monsterinsights_network' ) : admin_url( 'admin.php?page=monsterinsights_settings' ), |
263
|
|
|
), $this->get_route( 'https://' . monsterinsights_get_api_url() . 'auth/reauth/{type}' ) ); |
264
|
|
|
|
265
|
|
|
if ( monsterinsights_is_pro_version() ) { |
266
|
|
|
$key = is_network_admin() ? MonsterInsights()->license->get_network_license_key() : MonsterInsights()->license->get_site_license_key(); |
267
|
|
|
$siteurl = add_query_arg( 'license', $key, $siteurl ); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
$siteurl = apply_filters( 'monsterinsights_maybe_authenticate_siteurl', $siteurl ); |
271
|
|
|
|
272
|
|
|
wp_send_json_success( array( 'redirect' => $siteurl ) ); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function reauthenticate_listener(){ |
276
|
|
|
// Make sure it's for us |
277
|
|
|
if ( empty( $_REQUEST['mi-oauth-action'] ) || $_REQUEST['mi-oauth-action'] !== 'reauth' ) { |
278
|
|
|
return; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
// User can authenticate |
282
|
|
|
if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
283
|
|
|
return; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
// Invalid request |
287
|
|
|
if ( empty( $_REQUEST['tt'] ) || ! $this->validate_tt( $_REQUEST['tt'] ) ) { |
288
|
|
|
return; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
// Make sure has required params |
292
|
|
|
if ( |
293
|
|
|
empty( $_REQUEST['ua'] ) || |
294
|
|
|
empty( $_REQUEST['miview'] ) || |
295
|
|
|
empty( $_REQUEST['a'] ) || |
296
|
|
|
empty( $_REQUEST['w'] ) || |
297
|
|
|
empty( $_REQUEST['p'] ) |
298
|
|
|
) { |
299
|
|
|
return; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
// Invalid UA code |
303
|
|
|
$ua = monsterinsights_is_valid_ua( $_REQUEST['ua'] ); |
304
|
|
|
if ( empty( $ua ) ) { |
305
|
|
|
return; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
// we do have a current auth |
309
|
|
|
$existing = $this->is_network_admin() ? MonsterInsights()->auth->get_network_analytics_profile() : MonsterInsights()->auth->get_analytics_profile(); |
|
|
|
|
310
|
|
|
if ( empty( $existing['key'] ) || empty( $existing['token'] ) ) { |
311
|
|
|
return; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
$profile = array( |
315
|
|
|
'key' => $existing['key'], |
316
|
|
|
'token' => $existing['token'], |
317
|
|
|
'ua' => monsterinsights_is_valid_ua( $_REQUEST['ua'] ), |
318
|
|
|
'viewname' => sanitize_text_field( $_REQUEST['miview'] ), |
319
|
|
|
'a' => sanitize_text_field( $_REQUEST['a'] ), |
320
|
|
|
'w' => sanitize_text_field( $_REQUEST['w'] ), |
321
|
|
|
'p' => sanitize_text_field( $_REQUEST['p'] ), |
322
|
|
|
'siteurl' => site_url(), |
323
|
|
|
'neturl' => network_admin_url(), |
324
|
|
|
); |
325
|
|
|
|
326
|
|
|
// Rotate tt |
327
|
|
|
$this->rotate_tt(); |
328
|
|
|
|
329
|
|
|
// Save Profile |
330
|
|
|
$this->is_network_admin() ? MonsterInsights()->auth->set_network_analytics_profile( $profile ) : MonsterInsights()->auth->set_analytics_profile( $profile ); |
331
|
|
|
|
332
|
|
|
// Clear cache |
333
|
|
|
$where = $this->is_network_admin() ? 'network' : 'site'; |
334
|
|
|
MonsterInsights()->reporting->delete_aggregate_data( $where ); |
335
|
|
|
|
336
|
|
|
$url = $this->is_network_admin() ? network_admin_url( 'admin.php?page=monsterinsights_network' ) : admin_url( 'admin.php?page=monsterinsights_settings' ) ; |
337
|
|
|
$url = add_query_arg( array( |
338
|
|
|
'mi_action' => 'reauth', |
339
|
|
|
'success' => 'true', |
340
|
|
|
), $url ); |
341
|
|
|
wp_safe_redirect( $url ); |
342
|
|
|
exit; |
|
|
|
|
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
public function maybe_verify(){ |
346
|
|
|
|
347
|
|
|
// Check nonce |
348
|
|
|
check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
349
|
|
|
|
350
|
|
|
// current user can verify |
351
|
|
|
if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
352
|
|
|
wp_send_json_error( array( 'message' => __( "You don't have permission to verify MonsterInsights.", 'google-analytics-for-wordpress' ) ) ); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
if ( ! empty( $_REQUEST['isnetwork'] ) && $_REQUEST['isnetwork'] ) { |
356
|
|
|
define( 'WP_NETWORK_ADMIN', true ); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
// we have an auth to verify |
360
|
|
|
if ( $this->is_network_admin() && ! MonsterInsights()->auth->is_network_authed() ) { |
361
|
|
|
wp_send_json_error( array( 'message' => __( "Cannot verify. Please authenticate.", 'google-analytics-for-wordpress' ) ) ); |
362
|
|
|
} else if ( ! $this->is_network_admin() && ! MonsterInsights()->auth->is_authed() ) { |
363
|
|
|
wp_send_json_error( array( 'message' => __( "Cannot verify. Please authenticate.", 'google-analytics-for-wordpress' ) ) ); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
$valid = is_network_admin() ? MonsterInsights()->license->is_network_licensed() : MonsterInsights()->license->is_site_licensed(); |
367
|
|
|
if ( monsterinsights_is_pro_version() && ! $valid ) { |
368
|
|
|
wp_send_json_error( array( 'message' => __( "Cannot verify. Please enter a valid, active license key for MonsterInsights Pro into the settings.", 'google-analytics-for-wordpress' ) ) ); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
$worked = $this->verify_auth(); |
372
|
|
|
if ( $worked && ! is_wp_error( $worked ) ) { |
373
|
|
|
wp_send_json_success( array( 'message' => __( "Successfully verified.", 'google-analytics-for-wordpress' ) ) ); |
374
|
|
|
} else { |
375
|
|
|
wp_send_json_error( array( 'message' => __( "Could not verify.", 'google-analytics-for-wordpress' ) ) ); |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
public function verify_auth( $credentials = array() ){ |
380
|
|
|
$creds = ! empty( $credentials ) ? $credentials : ( $this->is_network_admin() ? MonsterInsights()->auth->get_network_analytics_profile( true ) : MonsterInsights()->auth->get_analytics_profile( true ) ); |
|
|
|
|
381
|
|
|
|
382
|
|
|
if ( empty( $creds['key'] ) ) { |
383
|
|
|
return new WP_Error( 'validation-error', sprintf( __( 'Verify auth key not passed', 'google-analytics-for-wordpress' ) ) ); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
$network = ! empty( $_REQUEST['network'] ) ? $_REQUEST['network'] === 'network' : $this->is_network_admin(); |
387
|
|
|
$api = new MonsterInsights_API_Request( $this->get_route( 'auth/verify/{type}/' ), array( 'network' => $network, 'tt' => $this->get_tt(), 'key' => $creds['key'], 'token' => $creds['token'] ) ); |
388
|
|
|
$ret = $api->request(); |
389
|
|
|
|
390
|
|
|
if ( is_wp_error( $ret ) ) { |
391
|
|
|
return $ret; |
392
|
|
|
} else { |
393
|
|
|
return true; |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
public function maybe_delete(){ |
398
|
|
|
|
399
|
|
|
// Check nonce |
400
|
|
|
check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
401
|
|
|
|
402
|
|
|
// current user can delete |
403
|
|
|
if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
404
|
|
|
wp_send_json_error( array( 'message' => __( "You don't have permission to deauthenticate MonsterInsights.", 'google-analytics-for-wordpress' ) ) ); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
if ( ! empty( $_REQUEST['isnetwork'] ) && $_REQUEST['isnetwork'] ) { |
408
|
|
|
define( 'WP_NETWORK_ADMIN', true ); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
// we have an auth to delete |
412
|
|
|
if ( $this->is_network_admin() && ! MonsterInsights()->auth->is_network_authed() ) { |
413
|
|
|
wp_send_json_error( array( 'message' => __( "Cannot deauthenticate. You are not currently authed.", 'google-analytics-for-wordpress' ) ) ); |
414
|
|
|
} else if ( ! $this->is_network_admin() && ! MonsterInsights()->auth->is_authed() ) { |
415
|
|
|
wp_send_json_error( array( 'message' => __( "Cannot deauthenticate. You are not currently authed.", 'google-analytics-for-wordpress' ) ) ); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
$valid = is_network_admin() ? MonsterInsights()->license->is_network_licensed() : MonsterInsights()->license->is_site_licensed(); |
419
|
|
|
if ( monsterinsights_is_pro_version() && ! $valid ) { |
420
|
|
|
wp_send_json_error( array( 'message' => __( "Cannot deauthenticate. Please enter a valid, active license key for MonsterInsights Pro into the settings.", 'google-analytics-for-wordpress' ) ) ); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
$force = ! empty( $_REQUEST['forcedelete'] ) && $_REQUEST['forcedelete'] === 'true'; |
424
|
|
|
|
425
|
|
|
$worked = $this->delete_auth( $force ); |
426
|
|
|
if ( $worked && ! is_wp_error( $worked ) ) { |
427
|
|
|
wp_send_json_success( array( 'message' => __( "Successfully deauthenticated.", 'google-analytics-for-wordpress' ) ) ); |
428
|
|
|
} else { |
429
|
|
|
if ( $force ) { |
430
|
|
|
wp_send_json_success( array( 'message' => __( "Successfully force deauthenticated.", 'google-analytics-for-wordpress' ) ) ); |
431
|
|
|
} else { |
432
|
|
|
wp_send_json_error( array( 'message' => __( "Could not deauthenticate.", 'google-analytics-for-wordpress' ) ) ); |
433
|
|
|
} |
434
|
|
|
} |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
public function delete_auth( $force = false ){ |
438
|
|
|
if ( $this->is_network_admin() && ! MonsterInsights()->auth->is_network_authed() ) { |
|
|
|
|
439
|
|
|
return false; |
440
|
|
|
} else if ( ! $this->is_network_admin() && ! MonsterInsights()->auth->is_authed() ) { |
441
|
|
|
return false; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
$creds = $this->is_network_admin() ? MonsterInsights()->auth->get_network_analytics_profile( true ) : MonsterInsights()->auth->get_analytics_profile( true ); |
445
|
|
|
|
446
|
|
|
if ( empty( $creds['key'] ) ) { |
447
|
|
|
return false; |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
// If we have a new siteurl enabled option and the profile site doesn't match the current site, deactivate anyways |
451
|
|
|
if ( is_network_admin() ) { |
452
|
|
|
$siteurl = network_admin_url(); |
453
|
|
|
if ( ! empty( $creds['neturl' ] ) && $creds['neturl'] !== $siteurl ) { |
454
|
|
|
MonsterInsights()->auth->delete_network_analytics_profile( true ); |
455
|
|
|
return true; |
456
|
|
|
} |
457
|
|
|
} else { |
458
|
|
|
$siteurl = site_url(); |
459
|
|
|
if ( ! empty( $creds['siteurl' ] ) && $creds['siteurl'] !== $siteurl ) { |
460
|
|
|
MonsterInsights()->auth->delete_analytics_profile( true ); |
461
|
|
|
return true; |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
$api = new MonsterInsights_API_Request( $this->get_route( 'auth/delete/{type}/' ), array( 'network' => $this->is_network_admin(), 'tt' => $this->get_tt(), 'key' => $creds['key'], 'token' => $creds['token'] ) ); |
466
|
|
|
$ret = $api->request(); |
467
|
|
|
|
468
|
|
|
if ( is_wp_error( $ret ) && ! $force ) { |
469
|
|
|
return false; |
470
|
|
|
} else { |
471
|
|
|
if ( $this->is_network_admin() ) { |
472
|
|
|
MonsterInsights()->auth->delete_network_analytics_profile( true ); |
473
|
|
|
} else { |
474
|
|
|
MonsterInsights()->auth->delete_analytics_profile( true ); |
475
|
|
|
|
476
|
|
|
} |
477
|
|
|
return true; |
478
|
|
|
} |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* Function to delete network auth in the uninstall process where we can't check if is network admin. |
483
|
|
|
* |
484
|
|
|
* @return bool |
485
|
|
|
*/ |
486
|
|
|
public function uninstall_network_auth() { |
487
|
|
|
|
488
|
|
|
if ( ! MonsterInsights()->auth->is_network_authed() ) { |
|
|
|
|
489
|
|
|
return false; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
$creds = MonsterInsights()->auth->get_network_analytics_profile( true ); |
493
|
|
|
|
494
|
|
|
$api = new MonsterInsights_API_Request( $this->get_route( 'auth/delete/{type}/' ), array( |
495
|
|
|
'network' => true, |
496
|
|
|
'tt' => $this->get_tt(), |
497
|
|
|
'key' => $creds['key'], |
498
|
|
|
'token' => $creds['token'] |
499
|
|
|
) ); |
500
|
|
|
// Force the network admin url otherwise this will fail not finding the url in relay. |
501
|
|
|
$api->site_url = network_admin_url(); |
|
|
|
|
502
|
|
|
$ret = $api->request(); |
503
|
|
|
if ( is_wp_error( $ret ) ) { |
504
|
|
|
return false; |
505
|
|
|
} else { |
506
|
|
|
MonsterInsights()->auth->delete_network_analytics_profile( true ); |
507
|
|
|
return true; |
508
|
|
|
} |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
public function get_type() { |
512
|
|
|
return monsterinsights_is_pro_version() ? 'pro' : 'lite'; |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
public function get_route( $route = '' ) { |
516
|
|
|
$route = str_replace( '{type}', $this->get_type(), $route ); |
517
|
|
|
$route = trailingslashit( $route ); |
518
|
|
|
return $route; |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
public function is_network_admin() { |
522
|
|
|
return is_multisite() && is_network_admin(); |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
public function get_sitei() { |
526
|
|
|
// $sitei = get_network_option( get_current_network_id(), 'monsterinsights_network_sitei', false ); |
527
|
|
|
// if ( ! empty( $sitei ) && strlen( $sitei ) >= 1 ) { |
528
|
|
|
// return $sitei; |
529
|
|
|
// } |
530
|
|
|
|
531
|
|
|
$auth_key = defined( 'AUTH_KEY' ) ? AUTH_KEY : ''; |
532
|
|
|
$secure_auth_key = defined( 'SECURE_AUTH_KEY' ) ? SECURE_AUTH_KEY : ''; |
533
|
|
|
$logged_in_key = defined( 'LOGGED_IN_KEY' ) ? LOGGED_IN_KEY : ''; |
534
|
|
|
|
535
|
|
|
$sitei = $auth_key . $secure_auth_key . $logged_in_key; |
536
|
|
|
$sitei = preg_replace('/[^a-zA-Z0-9]/', '', $sitei ); |
537
|
|
|
$sitei = sanitize_text_field( $sitei ); |
538
|
|
|
$sitei = trim( $sitei ); |
539
|
|
|
$sitei = ( strlen($sitei) > 30 ) ? substr($sitei, 0, 30 ) : $sitei; |
540
|
|
|
return $sitei; |
541
|
|
|
} |
542
|
|
|
} |
543
|
|
|
|