Passed
Push — master ( 8403c0...3ede7f )
by Chris
08:42
created

MonsterInsights_Auth::set_manual_v4_id()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 21
rs 9.8333
1
<?php
2
/**
3
 * Auth class.
4
 *
5
 * Helper for auth.
6
 *
7
 * @since 7.0.0
8
 *
9
 * @package MonsterInsights
10
 * @subpackage Auth
11
 * @author  Chris Christoff
12
 */
13
14
// Exit if accessed directly
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
final class MonsterInsights_Auth {
20
21
	private $profile  = array();
22
	private $network = array();
23
24
	/**
25
	 * Primary class constructor.
26
	 *
27
	 * @access public
28
	 * @since 7.0.0
29
	 */
30
	public function __construct() {
31
		$this->profile       = $this->get_analytics_profile();
32
		$this->network       = $this->get_network_analytics_profile();
33
	}
34
35
	public function is_manual( $type = false ) {
36
		$result = ! empty( $this->profile['manual'] );
37
38
		if ( ! $result || empty( $type ) ) {
39
			return $result;
40
		}
41
42
		return $type === 'ua'
43
			? monsterinsights_is_valid_ua( $this->profile['manual'] )
44
			: monsterinsights_is_valid_v4_id( $this->profile['manual'] );
45
	}
46
47
	public function is_network_manual( $type = false ) {
48
		$result = ! empty( $this->network['manual'] );
49
50
		if ( ! $result || empty( $type ) ) {
51
			return $result;
52
		}
53
54
		return $type === 'ua'
55
			? monsterinsights_is_valid_ua( $this->network['manual'] )
56
			: monsterinsights_is_valid_v4_id( $this->network['manual'] );
57
	}
58
59
	public function is_authed( $type = false ) {
60
		$result = ! empty( $this->profile['key'] );
61
62
		if ( ! $result || empty( $type ) ) {
63
			return $result;
64
		}
65
66
		return $this->get_connected_type() === $type && ! empty( $this->profile[ $type ] );
67
	}
68
69
	public function is_network_authed( $type = false ) {
70
		$result = ! empty( $this->network['key'] );
71
72
		if ( ! $result || empty( $type ) ) {
73
			return $result;
74
		}
75
76
		return $this->get_connected_type() === $type && ! empty( $this->network[ $type ] );
77
	}
78
79
	public function get_analytics_profile( $force = false ) {
80
		if ( ! empty( $this->profile ) && ! $force ) {
81
			return $this->profile;
82
		} else {
83
			$profile = get_option( 'monsterinsights_site_profile', array() );
84
			$this->profile = $profile;
85
			return $profile;
86
		}
87
	}
88
89
	public function get_network_analytics_profile( $force = false ) {
90
		if ( ! empty( $this->network ) && ! $force ) {
91
			return $this->network;
92
		} else {
93
			$profile = get_site_option( 'monsterinsights_network_profile', array() );
94
			$this->network = $profile;
95
			return $profile;
96
		}
97
	}
98
99
	public function set_analytics_profile( $data = array() ){
100
		update_option( 'monsterinsights_site_profile', $data );
101
		$this->profile      = $data;
102
103
		// If this is the first time, save the date when they connected.
104
		$over_time    = get_option( 'monsterinsights_over_time', array() );
105
		$needs_update = false;
106
		if ( monsterinsights_is_pro_version() && empty( $over_time['connected_date_pro'] ) ) {
107
			$over_time['connected_date_pro'] = time();
108
			$needs_update                    = true;
109
		}
110
		if ( ! monsterinsights_is_pro_version() && empty( $over_time['connected_date_lite'] ) ) {
111
			$over_time['connected_date_lite'] = time();
112
			$needs_update                     = true;
113
		}
114
		if ( $needs_update ) {
115
			update_option( 'monsterinsights_over_time', $over_time, false );
116
		}
117
	}
118
119
	public function set_network_analytics_profile( $data = array() ){
120
		update_site_option( 'monsterinsights_network_profile', $data );
121
		$this->network      = $data;
122
	}
123
124
	public function delete_analytics_profile( $migrate = true ){
125
		if ( $migrate ) {
126
			$newdata = array();
127
			if ( isset( $this->profile['ua'] ) ) {
128
				$newdata['manual'] = $this->profile['ua'];
129
			}
130
			if ( isset( $this->profile['v4'] ) ) {
131
				$newdata['manual_v4'] = $this->profile['v4'];
132
			}
133
			$this->profile      = $newdata;
134
			$this->set_analytics_profile( $newdata );
135
		} else {
136
			$this->profile      = array();
137
			delete_option( 'monsterinsights_site_profile' );
138
		}
139
	}
140
141
	public function delete_network_analytics_profile( $migrate = true ){
142
		if ( $migrate ) {
143
			$newdata = array();
144
			if ( isset( $this->network['ua'] ) ) {
145
				$newdata['manual'] = $this->network['ua'];
146
			}
147
			if ( isset( $this->network['v4'] ) ) {
148
				$newdata['manual_v4'] = $this->network['v4'];
149
			}
150
			$this->network      = $newdata;
151
			$this->set_network_analytics_profile( $newdata );
152
		} else {
153
			$this->network      = array();
154
			delete_site_option( 'monsterinsights_network_profile' );
155
		}
156
	}
157
158
	public function set_manual_ua( $ua = '' ) {
159
		if ( empty( $ua ) ) {
160
			return;
161
		}
162
163
		if ( $this->is_authed() ) {
164
			MonsterInsights()->api_auth->delete_auth();
0 ignored issues
show
Bug introduced by
The method delete_auth() does not exist on MonsterInsights_Auth. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

164
			MonsterInsights()->api_auth->/** @scrutinizer ignore-call */ 
165
                                delete_auth();

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...
165
		}
166
167
		$data = array();
168
		if ( empty( $this->profile ) ) {
169
			$data['manual'] = $ua;
170
		} else {
171
			$data           = $this->profile;
172
			$data['manual'] = $ua;
173
		}
174
175
		do_action( 'monsterinsights_reports_delete_aggregate_data' );
176
177
		$this->profile      = $data;
178
		$this->set_analytics_profile( $data );
179
	}
180
181
	/**
182
	 * @param string $id
183
	 * @param array  $data
184
	 * @param bool   $is_manual_ua
185
	 * @param bool   $is_manual_v4
186
	 * @param bool   $is_authed_ua
187
	 * @param bool   $is_authed_v4
188
	 *
189
	 * @return false|array
190
	 */
191
	private function prepare_dual_tracking_data( $id, $data, $is_manual_ua, $is_manual_v4, $is_authed_ua, $is_authed_v4 ) {
192
		if ( empty( $id ) ) {
193
			$key = false;
194
195
			if ( $is_manual_ua || $is_manual_v4 ) {
196
				$key = $is_manual_ua ? 'manual_v4' : 'manual';
197
			} elseif ( $is_authed_ua || $is_authed_v4 ) {
198
				$key = $is_authed_ua ? 'v4' : 'ua';
199
			}
200
201
			if ( $key && ! empty( $data[ $key ] ) ) {
202
				unset( $data[ $key ] );
203
			}
204
		} else {
205
			$is_dual_tracking_id_v4 = monsterinsights_is_valid_v4_id( $id );
206
			$is_dual_tracking_id_ua = monsterinsights_is_valid_ua( $id );
207
208
			$is_valid_dual_tracking_id = ( $is_dual_tracking_id_ua && ( $is_manual_v4 || $is_authed_v4 ) ) ||
209
			                             ( $is_dual_tracking_id_v4 && ( $is_manual_ua || $is_authed_ua ) );
210
211
			if ( ! $is_valid_dual_tracking_id ) {
212
				return false;
213
			}
214
215
			if ( $is_manual_ua || $is_manual_v4 ) {
216
				$key = $is_dual_tracking_id_v4 ? 'manual_v4' : 'manual';
217
			} else {
218
				$key = $is_dual_tracking_id_v4 ? 'v4' : 'ua';
219
			}
220
221
			$data[ $key ] = $id;
222
		}
223
224
		return $data;
225
	}
226
227
	public function set_dual_tracking_id ( $id = '' ) {
228
		$data = empty( $this->profile ) ? array() : $this->profile;
229
230
		$is_manual_ua = $this->is_manual( 'ua' );
231
		$is_manual_v4 = $this->is_manual( 'v4' );
232
		$is_authed_ua = $this->is_authed( 'ua' );
233
		$is_authed_v4 = $this->is_authed( 'v4' );
234
235
		$prepared_data = $this->prepare_dual_tracking_data( $id, $data, $is_manual_ua, $is_manual_v4, $is_authed_ua, $is_authed_v4 );
0 ignored issues
show
Bug introduced by
It seems like $is_manual_ua can also be of type string; however, parameter $is_manual_ua of MonsterInsights_Auth::prepare_dual_tracking_data() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

235
		$prepared_data = $this->prepare_dual_tracking_data( $id, $data, /** @scrutinizer ignore-type */ $is_manual_ua, $is_manual_v4, $is_authed_ua, $is_authed_v4 );
Loading history...
Bug introduced by
It seems like $is_manual_v4 can also be of type string; however, parameter $is_manual_v4 of MonsterInsights_Auth::prepare_dual_tracking_data() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

235
		$prepared_data = $this->prepare_dual_tracking_data( $id, $data, $is_manual_ua, /** @scrutinizer ignore-type */ $is_manual_v4, $is_authed_ua, $is_authed_v4 );
Loading history...
Bug introduced by
It seems like $data can also be of type boolean; however, parameter $data of MonsterInsights_Auth::prepare_dual_tracking_data() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

235
		$prepared_data = $this->prepare_dual_tracking_data( $id, /** @scrutinizer ignore-type */ $data, $is_manual_ua, $is_manual_v4, $is_authed_ua, $is_authed_v4 );
Loading history...
236
		if ( $prepared_data === false ) {
237
			return;
238
		}
239
240
		$this->profile = $prepared_data;
241
		$this->set_analytics_profile( $prepared_data );
242
	}
243
244
	public function set_network_dual_tracking_id ( $id = '' ) {
245
		$data = empty( $this->network ) ? array() : $this->network;
246
247
		$is_manual_ua = $this->is_network_manual( 'ua' );
248
		$is_manual_v4 = $this->is_network_manual( 'v4' );
249
		$is_authed_ua = $this->is_network_authed( 'ua' );
250
		$is_authed_v4 = $this->is_network_authed( 'v4' );
251
252
		$prepared_data = $this->prepare_dual_tracking_data( $id, $data, $is_manual_ua, $is_manual_v4, $is_authed_ua, $is_authed_v4 );
0 ignored issues
show
Bug introduced by
It seems like $is_manual_ua can also be of type string; however, parameter $is_manual_ua of MonsterInsights_Auth::prepare_dual_tracking_data() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

252
		$prepared_data = $this->prepare_dual_tracking_data( $id, $data, /** @scrutinizer ignore-type */ $is_manual_ua, $is_manual_v4, $is_authed_ua, $is_authed_v4 );
Loading history...
Bug introduced by
It seems like $is_manual_v4 can also be of type string; however, parameter $is_manual_v4 of MonsterInsights_Auth::prepare_dual_tracking_data() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

252
		$prepared_data = $this->prepare_dual_tracking_data( $id, $data, $is_manual_ua, /** @scrutinizer ignore-type */ $is_manual_v4, $is_authed_ua, $is_authed_v4 );
Loading history...
253
		if ( $prepared_data === false ) {
254
			return;
255
		}
256
257
		$this->network = $prepared_data;
258
		$this->set_network_analytics_profile( $prepared_data );
259
	}
260
261
	public function set_manual_v4_id( $v4 = '' ) {
262
		if ( empty( $v4 ) ) {
263
			return;
264
		}
265
266
		if ( $this->is_authed() ) {
267
			MonsterInsights()->api_auth->delete_auth();
268
		}
269
270
		$data = array();
271
		if ( empty( $this->profile ) ) {
272
			$data['manual_v4'] = $v4;
273
		} else {
274
			$data           = $this->profile;
275
			$data['manual_v4'] = $v4;
276
		}
277
278
		do_action( 'monsterinsights_reports_delete_aggregate_data' );
279
280
		$this->profile      = $data;
281
		$this->set_analytics_profile( $data );
282
	}
283
284
	public function set_network_manual_ua( $ua = '' ) {
285
		if ( empty( $ua ) ) {
286
			return;
287
		}
288
289
		if ( $this->is_network_authed() ) {
290
			MonsterInsights()->api_auth->delete_auth();
291
		}
292
293
		$data = array();
294
		if ( empty( $this->network ) ) {
295
			$data['manual'] = $ua;
296
		} else {
297
			$data           = $this->network;
298
			$data['manual'] = $ua;
299
		}
300
301
		do_action( 'monsterinsights_reports_delete_network_aggregate_data' );
302
303
		$this->network = $data;
304
		$this->set_network_analytics_profile( $data );
305
	}
306
307
	public function set_network_manual_v4_id( $v4 = '' ) {
308
		if ( empty( $v4 ) ) {
309
			return;
310
		}
311
312
		if ( $this->is_network_authed() ) {
313
			MonsterInsights()->api_auth->delete_auth();
314
		}
315
316
		$data = array();
317
		if ( empty( $this->network ) ) {
318
			$data['manual_v4'] = $v4;
319
		} else {
320
			$data           = $this->network;
321
			$data['manual_v4'] = $v4;
322
		}
323
324
		do_action( 'monsterinsights_reports_delete_network_aggregate_data' );
325
326
		$this->network = $data;
327
		$this->set_network_analytics_profile( $data );
328
	}
329
330
	public function get_measurement_protocol_secret() {
331
		return ! empty( $this->profile['measurement_protocol_secret'] ) ? $this->profile['measurement_protocol_secret'] : '';
332
	}
333
334
	public function get_network_measurement_protocol_secret() {
335
		return ! empty( $this->network['measurement_protocol_secret'] ) ? $this->network['measurement_protocol_secret'] : '';
336
	}
337
338
	public function set_measurement_protocol_secret( $value ) {
339
		$data = array();
340
		if ( empty( $this->profile ) ) {
341
			$data['measurement_protocol_secret'] = $value;
342
		} else {
343
			$data                                = $this->profile;
344
			$data['measurement_protocol_secret'] = $value;
345
		}
346
347
		$this->profile = $data;
348
		$this->set_analytics_profile( $data );
349
	}
350
351
	public function set_network_measurement_protocol_secret( $value ) {
352
		$data = array();
353
		if ( empty( $this->network ) ) {
354
			$data['measurement_protocol_secret'] = $value;
355
		} else {
356
			$data                                = $this->network;
357
			$data['measurement_protocol_secret'] = $value;
358
		}
359
360
		$this->network = $data;
361
		$this->set_network_analytics_profile( $data );
362
	}
363
364
	public function delete_manual_ua() {
365
		if ( ! empty( $this->profile ) && ! empty( $this->profile['manual'] ) ) {
366
			unset( $this->profile['manual'] );
367
			$this->set_analytics_profile( $this->profile );
368
		}
369
	}
370
371
	public function delete_network_manual_ua() {
372
		if ( ! empty( $this->network ) && ! empty( $this->network['manual'] ) ) {
373
			unset( $this->network['manual'] );
374
			$this->set_network_analytics_profile( $this->network );
375
		}
376
	}
377
378
	public function delete_manual_v4_id() {
379
		if ( ! empty( $this->profile ) && ! empty( $this->profile['manual_v4'] ) ) {
380
			unset( $this->profile['manual_v4'] );
381
			$this->set_analytics_profile( $this->profile );
382
		}
383
	}
384
385
	public function delete_network_manual_v4_id() {
386
		if ( ! empty( $this->network ) && ! empty( $this->network['manual_v4'] ) ) {
387
			unset( $this->network['manual_v4'] );
388
			$this->set_network_analytics_profile( $this->network );
389
		}
390
	}
391
392
	public function get_connected_type() {
393
		return empty( $this->profile['connectedtype'] ) ? 'ua' : $this->profile['connectedtype'];
394
	}
395
396
	public function get_manual_ua() {
397
		return ! empty( $this->profile['manual'] ) ? monsterinsights_is_valid_ua( $this->profile['manual'] ) : '';
398
	}
399
400
	public function get_manual_v4_id() {
401
		return ! empty( $this->profile['manual_v4'] ) ? monsterinsights_is_valid_v4_id( $this->profile['manual_v4'] ) : '';
402
	}
403
404
	public function get_network_manual_ua() {
405
		return ! empty( $this->network['manual'] ) ? monsterinsights_is_valid_ua( $this->network['manual'] ) : '';
406
	}
407
408
	public function get_network_manual_v4_id() {
409
		return ! empty( $this->network['manual_v4'] ) ? monsterinsights_is_valid_v4_id( $this->network['manual_v4'] ) : '';
410
	}
411
412
	public function get_ua() {
413
		return ! empty( $this->profile['ua'] ) ? monsterinsights_is_valid_ua( $this->profile['ua'] ) : '';
414
	}
415
416
	public function get_v4_id() {
417
		return ! empty( $this->profile['v4'] ) ? monsterinsights_is_valid_v4_id( $this->profile['v4'] ) : '';
418
	}
419
420
	public function get_network_ua() {
421
		return ! empty( $this->network['ua'] ) ? monsterinsights_is_valid_ua( $this->network['ua'] ) : '';
422
	}
423
424
	public function get_network_v4_id() {
425
		return ! empty( $this->network['v4'] ) ? monsterinsights_is_valid_v4_id( $this->network['v4'] ) : '';
426
	}
427
428
	public function get_viewname(){
429
		return ! empty( $this->profile['viewname'] ) ? $this->profile['viewname'] : '';
430
	}
431
432
	public function get_network_viewname(){
433
		return ! empty( $this->network['viewname'] ) ? $this->network['viewname'] : '';
434
	}
435
436
	public function get_accountid(){
437
		return ! empty( $this->profile['a'] ) ? $this->profile['a'] : '';
438
	}
439
440
	public function get_network_accountid(){
441
		return ! empty( $this->network['a'] ) ? $this->network['a'] : '';
442
	}
443
444
	public function get_propertyid(){
445
		return ! empty( $this->profile['w'] ) ? $this->profile['w'] : '';
446
	}
447
448
	public function get_network_propertyid(){
449
		return ! empty( $this->network['w'] ) ? $this->network['w'] : '';
450
	}
451
452
	public function get_viewid(){ // also known as profileID
453
		return ! empty( $this->profile['p'] ) ? $this->profile['p'] : '';
454
	}
455
456
	public function get_network_viewid(){ // also known as profileID
457
		return ! empty( $this->network['p'] ) ? $this->network['p'] : '';
458
	}
459
460
	public function get_key(){
461
		return ! empty( $this->profile['key'] ) ? $this->profile['key'] : '';
462
	}
463
464
	public function get_network_key(){
465
		return ! empty( $this->network['key'] ) ? $this->network['key'] : '';
466
	}
467
468
	public function get_token(){
469
		return ! empty( $this->profile['token'] ) ? $this->profile['token'] : '';
470
	}
471
472
	public function get_network_token(){
473
		return ! empty( $this->network['token'] ) ? $this->network['token'] : '';
474
	}
475
476
	public function get_referral_url() {
477
		$auth = MonsterInsights()->auth;
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...
478
479
		if ( $this->is_authed() ) {
480
			$acc_id      = $auth->get_accountid();
481
			$view_id     = $auth->get_viewid();
482
			$property_id = $auth->get_propertyid();
483
		} else if ( $this->is_network_authed() ) {
484
			$acc_id      = $auth->get_network_accountid();
485
			$view_id     = $auth->get_network_viewid();
486
			$property_id = $auth->get_network_propertyid();
487
		}
488
489
		if ( ! empty( $acc_id ) && ! empty( $view_id ) && ! empty( $property_id ) ) {
490
			$format = $auth->get_connected_type() === 'ua'
491
				? 'a%sw%sp%s/'
492
				: 'p%2$s';
493
			return sprintf( $format, $acc_id, $property_id, $view_id );
494
		}
495
496
		return '';
497
	}
498
}
499