Passed
Push — master ( 8161cf...b8b7ed )
by Chris
04:50
created

MonsterInsights_API_Auth::delete_auth()   C

Complexity

Conditions 15
Paths 20

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 27
nc 20
nop 1
dl 0
loc 41
rs 5.9166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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, 'authenticate' ) );
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 authenticate() {
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
		// Invalid UA code
153
		$ua = monsterinsights_is_valid_ua( $_REQUEST['ua'] );
154
		if ( empty( $ua ) ) {
155
			wp_send_json_error(
156
				array(
157
					'error'   => 'authenticate_invalid_ua',
158
					'message' => 'Invalid UA code sent',
159
					'version'   => MONSTERINSIGHTS_VERSION,
160
					'pro'   	=> monsterinsights_is_pro_version(),
161
				)
162
			);
163
		}
164
165
		$profile = array( 
166
			'key'      => sanitize_text_field( $_REQUEST['key'] ),
167
			'token'    => sanitize_text_field( $_REQUEST['token'] ),
168
			'ua'       => monsterinsights_is_valid_ua( $_REQUEST['ua'] ),
169
			'viewname' => sanitize_text_field( $_REQUEST['miview'] ),
170
			'a'        => sanitize_text_field( $_REQUEST['a'] ),
171
			'w'        => sanitize_text_field( $_REQUEST['w'] ),
172
			'p'        => sanitize_text_field( $_REQUEST['p'] ),
173
			'siteurl'  => site_url(),
174
			'neturl'   => network_admin_url(),
175
		);
176
177
		$worked = $this->verify_auth( $profile );
178
		if ( ! $worked ) {
179
			wp_send_json_error(
180
				array(
181
					'error'     => 'authenticate_auth_verification_failed',
182
					'message'   => 'Authenticate verification failed',
183
					'version'   => MONSTERINSIGHTS_VERSION,
184
					'pro'   	=> monsterinsights_is_pro_version(),
185
				)
186
			);
187
		}
188
189
		// Rotate tt
190
		$this->rotate_tt();
191
192
		// Save Profile
193
		$is_network = $_REQUEST['network'] === 'network';
194
		if ( $is_network ) {
195
			MonsterInsights()->auth->set_network_analytics_profile( $profile );
0 ignored issues
show
Bug Best Practice introduced by
The property $auth is declared protected in MonsterInsights_Lite. Since you implement __get, consider adding a @property or @property-read.
Loading history...
196
		} else {
197
			MonsterInsights()->auth->set_analytics_profile( $profile );
198
		}
199
200
		// Clear cache
201
		$where = $is_network ? 'network' : 'site';
202
		MonsterInsights()->reporting->delete_aggregate_data( $where );
203
204
		wp_send_json_success();
205
	}
206
207
	public function authenticate_listener(){
208
		// Make sure it's for us
209
		if ( empty( $_REQUEST['mi-oauth-action'] ) || $_REQUEST['mi-oauth-action'] !== 'auth' ) {
210
			return;
211
		}
212
213
		// User can authenticate
214
		if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
215
			return;
216
		}
217
218
		// Invalid request
219
		if ( empty( $_REQUEST['tt'] ) || ! $this->validate_tt( $_REQUEST['tt'] ) ) {
220
			return;
221
		}
222
223
		// Make sure has required params
224
		if ( empty( $_REQUEST['key'] )      ||
225
			 empty( $_REQUEST['token'] )    ||
226
			 empty( $_REQUEST['ua'] )       ||
227
			 empty( $_REQUEST['miview'] )   ||
228
			 empty( $_REQUEST['a'] )        ||
229
			 empty( $_REQUEST['w'] )        ||
230
			 empty( $_REQUEST['p'] )
231
		) {
232
			return;
233
		}
234
235
		// Invalid UA code
236
		$ua = monsterinsights_is_valid_ua( $_REQUEST['ua'] );
237
		if ( empty( $ua ) ) {
238
			return;
239
		}
240
241
		$profile = array( 
242
			'key'      => sanitize_text_field( $_REQUEST['key'] ),
243
			'token'    => sanitize_text_field( $_REQUEST['token'] ),
244
			'ua'       => monsterinsights_is_valid_ua( $_REQUEST['ua'] ),
245
			'viewname' => sanitize_text_field( $_REQUEST['miview'] ),
246
			'a'        => sanitize_text_field( $_REQUEST['a'] ), // AccountID
247
			'w'        => sanitize_text_field( $_REQUEST['w'] ), // PropertyID
248
			'p'        => sanitize_text_field( $_REQUEST['p'] ), // View ID
249
			'siteurl'  => site_url(),
250
			'neturl'   => network_admin_url(),
251
		);
252
253
		$worked = $this->verify_auth( $profile );
254
		if ( ! $worked ) {
255
			return;
256
		}
257
258
		// Rotate tt
259
		$this->rotate_tt();
260
261
		// Save Profile
262
		$this->is_network_admin() ? MonsterInsights()->auth->set_network_analytics_profile( $profile ) : MonsterInsights()->auth->set_analytics_profile( $profile );
0 ignored issues
show
Bug Best Practice introduced by
The property $auth is declared protected in MonsterInsights_Lite. Since you implement __get, consider adding a @property or @property-read.
Loading history...
263
264
		// Clear cache
265
		$where = $this->is_network_admin() ? 'network' : 'site';
266
		MonsterInsights()->reporting->delete_aggregate_data( $where );
267
268
		$url = $this->is_network_admin() ? network_admin_url( 'admin.php?page=monsterinsights_network' )  : admin_url( 'admin.php?page=monsterinsights_settings' ) ;
269
		$url = add_query_arg( array(
270
			 'mi_action' => 'auth',
271
			 'success'   => 'true',
272
			), $url );
273
		wp_safe_redirect( $url );
274
		exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
275
	}
276
277
	public function maybe_reauthenticate(){
278
279
		// Check nonce
280
		check_ajax_referer( 'mi-admin-nonce', 'nonce' );
281
282
		// current user can authenticate
283
		if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
284
			wp_send_json_error( array(	'message' => __( "You don't have permission to re-authenticate MonsterInsights.", 'google-analytics-for-wordpress' ) ) );
285
		}
286
287
		if ( ! empty( $_REQUEST['isnetwork'] ) && $_REQUEST['isnetwork'] ) {
288
			define( 'WP_NETWORK_ADMIN', true );
289
		}
290
291
		// Only for Pro users, require a license key to be entered first so we can link to things.
292
		$valid = is_network_admin() ? MonsterInsights()->license->is_network_licensed() : MonsterInsights()->license->is_site_licensed();
293
		if ( monsterinsights_is_pro_version() && ! $valid ) {
294
			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' ) ) );
295
		}
296
297
		// we do have a current auth
298
		if ( ! $this->is_network_admin() && ! MonsterInsights()->auth->is_authed() ) {
299
			wp_send_json_error( array(	'message' => __( "Cannot re-authenticate. Please authenticate.", 'google-analytics-for-wordpress' ) ) );
300
		} else if ( $this->is_network_admin() && ! MonsterInsights()->auth->is_network_authed() ) {
301
			wp_send_json_error( array(	'message' => __( "Cannot re-authenticate the network. Please authenticate on the network settings panel.", 'google-analytics-for-wordpress' ) ) );
302
		}
303
304
		$siteurl = add_query_arg( array(
305
			'tt'        => $this->get_tt(),
306
			'sitei'     => $this->get_sitei(),
307
			'miversion' => MONSTERINSIGHTS_VERSION,
308
			'ajaxurl'   => admin_url( 'admin-ajax.php' ),
309
			'network'   => is_network_admin() ? 'network' : 'site',
310
			'siteurl'   => is_network_admin() ? network_admin_url() : site_url(),
311
			'key'       => MonsterInsights()->auth->get_key(),
312
			'token'     => MonsterInsights()->auth->get_token(),
313
			'return'    => is_network_admin() ? network_admin_url( 'admin.php?page=monsterinsights_network' ) : admin_url( 'admin.php?page=monsterinsights_settings' ),
314
		 ), $this->get_route( 'https://' . monsterinsights_get_api_url() . 'auth/reauth/{type}' ) );
315
316
		if ( monsterinsights_is_pro_version() ) {
317
			$key     = is_network_admin() ? MonsterInsights()->license->get_network_license_key() : MonsterInsights()->license->get_site_license_key();
318
			$siteurl = add_query_arg( 'license', $key, $siteurl );
319
		}
320
321
		$siteurl = apply_filters( 'monsterinsights_maybe_authenticate_siteurl', $siteurl );
322
323
		wp_send_json_success( array( 'redirect' => $siteurl ) );
324
	}
325
326
	public function reauthenticate_listener(){
327
		// Make sure it's for us
328
		if ( empty( $_REQUEST['mi-oauth-action'] ) || $_REQUEST['mi-oauth-action'] !== 'reauth' ) {
329
			return;
330
		}
331
332
		// User can authenticate
333
		if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
334
			return;
335
		}
336
337
		// Invalid request
338
		if ( empty( $_REQUEST['tt'] ) || ! $this->validate_tt( $_REQUEST['tt'] ) ) {
339
			return;
340
		}
341
342
		// Make sure has required params
343
		if (
344
			 empty( $_REQUEST['ua'] )       ||
345
			 empty( $_REQUEST['miview'] )   ||
346
			 empty( $_REQUEST['a'] )        ||
347
			 empty( $_REQUEST['w'] )        ||
348
			 empty( $_REQUEST['p'] )       
349
		) {
350
			return;
351
		}
352
353
		// Invalid UA code
354
		$ua = monsterinsights_is_valid_ua( $_REQUEST['ua'] );
355
		if ( empty( $ua ) ) {
356
			return;
357
		}
358
359
		// we do have a current auth
360
		$existing = $this->is_network_admin() ? MonsterInsights()->auth->get_network_analytics_profile() : MonsterInsights()->auth->get_analytics_profile();
0 ignored issues
show
Bug Best Practice introduced by
The property $auth is declared protected in MonsterInsights_Lite. Since you implement __get, consider adding a @property or @property-read.
Loading history...
361
		if ( empty( $existing['key'] ) || empty( $existing['token'] ) ) {
362
			return;
363
		}
364
365
		$profile = array(
366
			'key'      => $existing['key'],
367
			'token'    => $existing['token'],
368
			'ua'       => monsterinsights_is_valid_ua( $_REQUEST['ua'] ),
369
			'viewname' => sanitize_text_field( $_REQUEST['miview'] ),
370
			'a'        => sanitize_text_field( $_REQUEST['a'] ),
371
			'w'        => sanitize_text_field( $_REQUEST['w'] ),
372
			'p'        => sanitize_text_field( $_REQUEST['p'] ),
373
			'siteurl'  => site_url(),
374
			'neturl'   => network_admin_url(),
375
		);
376
377
		// Rotate tt
378
		$this->rotate_tt();
379
380
		// Save Profile
381
		$this->is_network_admin() ? MonsterInsights()->auth->set_network_analytics_profile( $profile ) : MonsterInsights()->auth->set_analytics_profile( $profile );
382
383
		// Clear cache
384
		$where = $this->is_network_admin() ? 'network' : 'site';
385
		MonsterInsights()->reporting->delete_aggregate_data( $where );
386
387
		$url = $this->is_network_admin() ? network_admin_url( 'admin.php?page=monsterinsights_network' )  : admin_url( 'admin.php?page=monsterinsights_settings' ) ;
388
		$url = add_query_arg( array(
389
			 'mi_action' => 'reauth',
390
			 'success'   => 'true',
391
			), $url );
392
		wp_safe_redirect( $url );
393
		exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
394
	}
395
396
	public function maybe_verify(){
397
398
		// Check nonce
399
		check_ajax_referer( 'mi-admin-nonce', 'nonce' );
400
401
		// current user can verify
402
		if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
403
			wp_send_json_error( array(	'message' => __( "You don't have permission to verify MonsterInsights.", 'google-analytics-for-wordpress' ) ) );
404
		}
405
406
		if ( ! empty( $_REQUEST['isnetwork'] ) && $_REQUEST['isnetwork'] ) {
407
			define( 'WP_NETWORK_ADMIN', true );
408
		}
409
410
		// we have an auth to verify
411
		if ( $this->is_network_admin() && ! MonsterInsights()->auth->is_network_authed() ) {
412
			wp_send_json_error( array(	'message' => __( "Cannot verify. Please authenticate.", 'google-analytics-for-wordpress' ) ) );
413
		} else if ( ! $this->is_network_admin() && ! MonsterInsights()->auth->is_authed() ) {
414
			wp_send_json_error( array(	'message' => __( "Cannot verify. Please authenticate.", 'google-analytics-for-wordpress' ) ) );
415
		}
416
417
		$valid = is_network_admin() ? MonsterInsights()->license->is_network_licensed() : MonsterInsights()->license->is_site_licensed();
418
		if ( monsterinsights_is_pro_version() && ! $valid ) {
419
			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' ) ) );
420
		}
421
422
		$worked = $this->verify_auth();
423
		if ( $worked && ! is_wp_error(  $worked ) ) {
424
			wp_send_json_success( array( 'message' => __( "Successfully verified.", 'google-analytics-for-wordpress' ) ) );
425
		} else {
426
			wp_send_json_error( array( 'message' => __( "Could not verify.", 'google-analytics-for-wordpress' ) ) );
427
		}
428
	}
429
430
	public function verify_auth( $credentials = array() ){
431
		$creds = ! empty( $credentials ) ? $credentials : ( $this->is_network_admin() ? MonsterInsights()->auth->get_network_analytics_profile( true ) : MonsterInsights()->auth->get_analytics_profile( true ) );
0 ignored issues
show
Bug Best Practice introduced by
The property $auth is declared protected in MonsterInsights_Lite. Since you implement __get, consider adding a @property or @property-read.
Loading history...
432
433
		if ( empty( $creds['key'] ) ) {
434
			return false;
435
		}
436
		
437
		$api   = new MonsterInsights_API_Request( $this->get_route( 'auth/verify/{type}/' ), array( 'network' => $this->is_network_admin(), 'tt' => $this->get_tt(), 'key' => $creds['key'], 'token' => $creds['token'] ) );
438
		$ret   = $api->request();
439
		
440
		if ( is_wp_error( $ret ) ) {
441
			return false;
442
		} else {
443
			return true;
444
		}
445
	}
446
447
	public function maybe_delete(){
448
449
		// Check nonce
450
		check_ajax_referer( 'mi-admin-nonce', 'nonce' );
451
452
		// current user can delete
453
		if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
454
			wp_send_json_error( array(	'message' => __( "You don't have permission to deauthenticate MonsterInsights.", 'google-analytics-for-wordpress' ) ) );
455
		}
456
457
		if ( ! empty( $_REQUEST['isnetwork'] ) && $_REQUEST['isnetwork'] ) {
458
			define( 'WP_NETWORK_ADMIN', true );
459
		}
460
461
		// we have an auth to delete
462
		if ( $this->is_network_admin() && ! MonsterInsights()->auth->is_network_authed() ) {
463
			wp_send_json_error( array(	'message' => __( "Cannot deauthenticate. You are not currently authed.", 'google-analytics-for-wordpress' ) ) );
464
		} else if ( ! $this->is_network_admin() && ! MonsterInsights()->auth->is_authed() ) {
465
			wp_send_json_error( array(	'message' => __( "Cannot deauthenticate. You are not currently authed.", 'google-analytics-for-wordpress' ) ) );
466
		}
467
468
		$valid = is_network_admin() ? MonsterInsights()->license->is_network_licensed() : MonsterInsights()->license->is_site_licensed();
469
		if ( monsterinsights_is_pro_version() && ! $valid ) {
470
			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' ) ) );
471
		}
472
473
		$force = ! empty( $_REQUEST['forcedelete'] ) && $_REQUEST['forcedelete'] === 'true';
474
475
		$worked = $this->delete_auth( $force );
476
		if ( $worked && ! is_wp_error(  $worked ) ) {
477
			wp_send_json_success( array( 'message' => __( "Successfully deauthenticated.", 'google-analytics-for-wordpress' ) ) );
478
		} else {
479
			if ( $force ) {
480
				wp_send_json_success( array( 'message' => __( "Successfully force deauthenticated.", 'google-analytics-for-wordpress' ) ) );
481
			} else {
482
				wp_send_json_error( array( 'message' => __( "Could not deauthenticate.", 'google-analytics-for-wordpress' ) ) );
483
			}
484
		}
485
	}
486
487
	public function delete_auth( $force = false ){
488
		if ( $this->is_network_admin() && ! MonsterInsights()->auth->is_network_authed() ) {
0 ignored issues
show
Bug Best Practice introduced by
The property $auth is declared protected in MonsterInsights_Lite. Since you implement __get, consider adding a @property or @property-read.
Loading history...
489
			return false;
490
		} else if ( ! $this->is_network_admin() && ! MonsterInsights()->auth->is_authed() ) {
491
			return false;
492
		}
493
494
		$creds = $this->is_network_admin() ? MonsterInsights()->auth->get_network_analytics_profile( true ) : MonsterInsights()->auth->get_analytics_profile( true );
495
496
		if ( empty( $creds['key'] ) ) {
497
			return false;
498
		}
499
500
		// If we have a new siteurl enabled option and the profile site doesn't match the current site, deactivate anyways
501
		if ( is_network_admin() ) {
502
			$siteurl = network_admin_url();
503
			if ( ! empty( $creds['neturl' ] ) && $creds['neturl'] !== $siteurl ) {
504
				MonsterInsights()->auth->delete_network_analytics_profile( true );
505
				return true;
506
			}
507
		} else {
508
			$siteurl = site_url();
509
			if ( ! empty( $creds['siteurl' ] ) && $creds['siteurl'] !== $siteurl ) {
510
				MonsterInsights()->auth->delete_analytics_profile( true );
511
				return true;
512
			}
513
		}
514
515
		$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'] ) );
516
		$ret   = $api->request();
517
518
		if ( is_wp_error( $ret ) && ! $force ) {
519
			return false;
520
		} else {
521
			if ( $this->is_network_admin() ) {
522
				MonsterInsights()->auth->delete_network_analytics_profile( true );
523
			} else {
524
				MonsterInsights()->auth->delete_analytics_profile( true );
525
526
			}
527
			return true;
528
		}
529
	}
530
531
	public function get_type() {
532
		return monsterinsights_is_pro_version() ? 'pro' : 'lite';
533
	}
534
535
	public function get_route( $route = '' ) {
536
		$route = str_replace( '{type}', $this->get_type(), $route );
537
		$route = trailingslashit( $route );
538
		return $route;
539
	}
540
541
	public function is_network_admin() {
542
		return is_multisite() && is_network_admin();
543
	}
544
545
	public function get_sitei() {
546
		// $sitei = get_network_option(  get_current_network_id(), 'monsterinsights_network_sitei', false );
547
		// if ( ! empty( $sitei ) && strlen( $sitei ) >= 1 ) {
548
		// 	return $sitei;
549
		// }
550
551
		$auth_key        = defined( 'AUTH_KEY' )        ? AUTH_KEY 		  : '';
552
		$secure_auth_key = defined( 'SECURE_AUTH_KEY' ) ? SECURE_AUTH_KEY : '';
553
		$logged_in_key   = defined( 'LOGGED_IN_KEY' )   ? LOGGED_IN_KEY   : '';
554
555
		$sitei = $auth_key . $secure_auth_key . $logged_in_key;
556
		$sitei = preg_replace('/[^a-zA-Z0-9]/', '', $sitei );
557
		$sitei = sanitize_text_field( $sitei );
558
		$sitei = trim( $sitei );
559
		$sitei = ( strlen($sitei) > 30 ) ? substr($sitei, 0, 30 ) : $sitei;
560
		return $sitei;
561
	}
562
}