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

monsterinsights_get_v4_id()   B

Complexity

Conditions 9
Paths 15

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 12
c 1
b 0
f 0
nc 15
nop 0
dl 0
loc 28
rs 8.0555
1
<?php
2
/**
3
 * Option functions.
4
 *
5
 * @since 6.0.0
6
 *
7
 * @package MonsterInsights
8
 * @subpackage Options
9
 * @author  Chris Christoff
10
 */
11
12
// Exit if accessed directly
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
function monsterinsights_get_options() {
18
	$settings = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $settings is dead and can be removed.
Loading history...
19
	$option_name = monsterinsights_get_option_name();
20
	//$settings             = get_site_option( $option_name );
21
	//$use_network_settings = ! empty( $use_network_settings['use_network_settings'] ) ? true : false;
22
	//$is_network           = is_multisite();
23
24
	//if ( $is_network && $use_network_settings ) {
25
	//    return $settings;
26
	//} else if ( $is_network ) {
27
		$settings = get_option( $option_name );
28
	//} else {
29
	//    return $settings;
30
	//}
31
	if ( empty( $settings ) || ! is_array( $settings ) ) {
32
		$settings = array();
33
	}
34
	return $settings;
35
}
36
37
/**
38
 * Helper method for getting a setting's value. Falls back to the default
39
 * setting value if none exists in the options table.
40
 *
41
 * @since 6.0.0
42
 * @access public
43
 *
44
 * @param string $key   The setting key to retrieve.
45
 * @param mixed $default   The default value of the setting key to retrieve.
46
 * @return string       The value of the setting.
47
 */
48
function monsterinsights_get_option( $key = '', $default = false ) {
49
	global $monsterinsights_settings;
50
	$value = ! empty( $monsterinsights_settings[ $key ] ) ? $monsterinsights_settings[ $key ] : $default;
51
	$value = apply_filters( 'monsterinsights_get_option', $value, $key, $default );
52
	return apply_filters( 'monsterinsights_get_option_' . $key, $value, $key, $default );
53
}
54
55
/**
56
 * Helper method for getting the UA string.
57
 *
58
 * @since 6.0.0
59
 * @access public
60
 *
61
 * @return string The UA to use.
62
 */
63
function monsterinsights_get_ua() {
64
	// Allow short circuiting (for staging sites)
65
	if ( defined( 'MONSTERINSIGHTS_DISABLE_TRACKING' ) && MONSTERINSIGHTS_DISABLE_TRACKING ) {
0 ignored issues
show
Bug introduced by
The constant MONSTERINSIGHTS_DISABLE_TRACKING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
66
		return '';
67
	}
68
69
	// Try getting it from the auth UA
70
	$ua = MonsterInsights()->auth->get_ua();
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...
71
72
	// If that didn't work, try the manual UA at the site level
73
	if ( empty( $ua ) ) {
74
		$ua = MonsterInsights()->auth->get_manual_ua();
75
		// If that didn't work try getting it from the network
76
		if ( empty( $ua ) ) {
77
			$ua = monsterinsights_get_network_ua();
78
			// If that didn't work, try getting it from the overall constant. If it's not there, leave it blank
79
			if ( empty( $ua ) ) {
80
				$ua = defined( 'MONSTERINSIGHTS_GA_UA' ) && MONSTERINSIGHTS_GA_UA ? monsterinsights_is_valid_ua( MONSTERINSIGHTS_GA_UA ) : '';
0 ignored issues
show
Bug introduced by
The constant MONSTERINSIGHTS_GA_UA was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
81
			}
82
		}
83
	}
84
85
	// Feed through the filter
86
	$pre_filter = $ua;
87
	$ua = apply_filters( 'monsterinsights_get_ua', $ua );
88
89
	// Only run through monsterinsights_is_valid_ua if it's different than pre-filter
90
	return $pre_filter === $ua ? $ua : monsterinsights_is_valid_ua( $ua );
91
}
92
93
function monsterinsights_get_tracking_ids() {
94
	$ids = array();
95
96
	$ua = monsterinsights_get_ua_to_output();
97
	if ( $ua ) {
98
		$ids[] = $ua;
99
	}
100
101
	$v4 = monsterinsights_get_v4_id_to_output();
102
	if ( $v4 ) {
103
		$ids[] = $v4;
104
	}
105
106
	return $ids;
107
}
108
109
/**
110
 * Helper method for getting the network UA string.
111
 *
112
 * @since 6.0.0
113
 * @access public
114
 *
115
 * @return string The UA to use.
116
 */
117
function monsterinsights_get_network_ua() {
118
	if ( ! is_multisite() ) {
119
		return '';
120
	}
121
122
	// First try network auth UA
123
	$ua = MonsterInsights()->auth->get_network_ua();
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...
124
	if ( ! empty( $ua ) ) {
125
		return $ua;
126
	}
127
128
	// Then try manual network UA
129
	$ua = MonsterInsights()->auth->get_network_manual_ua();
130
	if ( ! empty( $ua ) ) {
131
		return $ua;
132
	}
133
134
	// See if the constant is defined
135
	if ( defined( 'MONSTERINSIGHTS_MS_GA_UA' ) && monsterinsights_is_valid_ua( MONSTERINSIGHTS_MS_GA_UA ) ) {
0 ignored issues
show
Bug introduced by
The constant MONSTERINSIGHTS_MS_GA_UA was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
136
		return MONSTERINSIGHTS_MS_GA_UA;
137
	}
138
139
	return '';
140
}
141
142
/**
143
 * Helper method for getting the UA string that's output on the frontend.
144
 *
145
 * @since 6.0.0
146
 * @access public
147
 *
148
 * @param array $args Allow calling functions to give args to use in future applications.
149
 * @return string The UA to use on frontend.
150
 */
151
function monsterinsights_get_ua_to_output( $args = array() ) {
152
	$ua = monsterinsights_get_ua();
153
	$ua = apply_filters( 'monsterinsights_get_ua_to_output', $ua, $args );
154
	return monsterinsights_is_valid_ua( $ua );
155
}
156
157
/**
158
 * Helper method for getting the V4 string.
159
 *
160
 * @since 6.0.0
161
 * @access public
162
 *
163
 * @return string The V4 ID to use.
164
 */
165
function monsterinsights_get_v4_id() {
166
	// Allow short circuiting (for staging sites)
167
	if ( defined( 'MONSTERINSIGHTS_DISABLE_TRACKING' ) && MONSTERINSIGHTS_DISABLE_TRACKING ) {
0 ignored issues
show
Bug introduced by
The constant MONSTERINSIGHTS_DISABLE_TRACKING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
168
		return '';
169
	}
170
171
	// Try getting it from the auth V4
172
	$v4_id = MonsterInsights()->auth->get_v4_id();
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...
173
174
	// If that didn't work, try the manual V4 at the site level
175
	if ( empty( $v4_id ) ) {
176
		$v4_id = MonsterInsights()->auth->get_manual_v4_id();
177
		// If that didn't work try getting it from the network
178
		if ( empty( $v4_id ) ) {
179
			$v4_id = monsterinsights_get_network_v4_id();
180
			// If that didn't work, try getting it from the overall constant. If it's not there, leave it blank
181
			if ( empty( $v4_id ) ) {
182
				$v4_id = defined( 'MONSTERINSIGHTS_GA_V4_ID' ) && MONSTERINSIGHTS_GA_V4_ID ? monsterinsights_is_valid_v4_id( MONSTERINSIGHTS_GA_V4_ID ) : '';
0 ignored issues
show
Bug introduced by
The constant MONSTERINSIGHTS_GA_V4_ID was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
183
			}
184
		}
185
	}
186
187
	// Feed through the filter
188
	$pre_filter = $v4_id;
189
	$v4_id = apply_filters( 'monsterinsights_get_v4_id', $v4_id );
190
191
	// Only run through monsterinsights_is_valid_v4 if it's different than pre-filter
192
	return $pre_filter === $v4_id ? $v4_id : monsterinsights_is_valid_v4_id( $v4_id );
193
}
194
195
/**
196
 * Helper method for getting the network V4 string.
197
 *
198
 * @since 6.0.0
199
 * @access public
200
 *
201
 * @return string The V4 ID to use.
202
 */
203
function monsterinsights_get_network_v4_id() {
204
	if ( ! is_multisite() ) {
205
		return '';
206
	}
207
208
	// First try network auth UA
209
	$v4_id = MonsterInsights()->auth->get_network_v4_id();
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...
210
	if ( ! empty( $v4_id ) ) {
211
		return $v4_id;
212
	}
213
214
	// Then try manual network UA
215
	$v4_id = MonsterInsights()->auth->get_network_manual_v4_id();
216
	if ( ! empty( $v4_id ) ) {
217
		return $v4_id;
218
	}
219
220
	// See if the constant is defined
221
	if ( defined( 'MONSTERINSIGHTS_MS_GA_V4_ID' ) && monsterinsights_is_valid_v4_id( MONSTERINSIGHTS_MS_GA_V4_ID ) ) {
0 ignored issues
show
Bug introduced by
The constant MONSTERINSIGHTS_MS_GA_V4_ID was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
222
		return MONSTERINSIGHTS_MS_GA_V4_ID;
223
	}
224
225
	return '';
226
}
227
228
/**
229
 * Helper method for getting the UA string that's output on the frontend.
230
 *
231
 * @since 6.0.0
232
 * @access public
233
 *
234
 * @param array $args Allow calling functions to give args to use in future applications.
235
 * @return string The UA to use on frontend.
236
 */
237
function monsterinsights_get_v4_id_to_output( $args = array() ) {
238
	$v4_id = monsterinsights_get_v4_id();
239
	$v4_id = apply_filters( 'monsterinsights_get_v4_id_to_output', $v4_id, $args );
240
	return monsterinsights_is_valid_v4_id( $v4_id );
241
}
242
243
/**
244
 * Helper method for updating a setting's value.
245
 *
246
 * @since 6.0.0
247
 * @access public
248
 *
249
 * @param string $key   The setting key.
250
 * @param string $value The value to set for the key.
251
 * @return boolean True if updated, false if not.
252
 */
253
function monsterinsights_update_option( $key = '', $value = false ) {
254
255
	// If no key, exit
256
	if ( empty( $key ) ){
257
		return false;
258
	}
259
260
	if ( empty( $value ) ) {
261
		$remove_option = monsterinsights_delete_option( $key );
262
		return $remove_option;
263
	}
264
265
	$option_name = monsterinsights_get_option_name();
266
267
	// First let's grab the current settings
268
269
	// if on network panel or if on single site using network settings
270
	//$settings              = get_site_option( $option_name );
271
	//$use_network_settings  = ! empty( $use_network_settings['use_network_settings'] ) ? true : false;
272
	//$is_network            = is_multisite();
273
	//$update_network_option = true;
274
	//if ( ! is_network_admin() && ! ( $is_network && $use_network_settings ) ) {
275
	   $settings = get_option( $option_name );
276
	//   $update_network_option = false;
277
	//}
278
279
	if ( ! is_array( $settings ) ) {
280
		$settings = array();
281
	}
282
283
	// Let's let devs alter that value coming in
284
	$value = apply_filters( 'monsterinsights_update_option', $value, $key );
285
286
	// Next let's try to update the value
287
	$settings[ $key ] = $value;
288
	$did_update = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $did_update is dead and can be removed.
Loading history...
289
	//if ( $update_network_option ) {
290
	//    $did_update = update_site_option( $option_name, $settings );
291
	//} else {
292
		$did_update = update_option( $option_name, $settings );
293
	//}
294
295
	// If it updated, let's update the global variable
296
	if ( $did_update ){
297
		global $monsterinsights_settings;
298
		$monsterinsights_settings[ $key ] = $value;
299
	}
300
301
	return $did_update;
302
}
303
304
 /**
305
 * Helper method for deleting a setting's value.
306
 *
307
 * @since 6.0.0
308
 * @access public
309
 *
310
 * @param string $key   The setting key.
311
 * @return boolean True if removed, false if not.
312
 */
313
function monsterinsights_delete_option( $key = '' ) {
314
	// If no key, exit
315
	if ( empty( $key ) ){
316
		return false;
317
	}
318
319
	$option_name = monsterinsights_get_option_name();
320
321
	// First let's grab the current settings
322
323
	// if on network panel or if on single site using network settings
324
	//$settings              = get_site_option( $option_name );
325
	//$use_network_settings  = ! empty( $use_network_settings['use_network_settings'] ) ? true : false;
326
	//$is_network            = is_multisite();
327
	//$update_network_option = true;
328
	//if ( ! is_network_admin() && ! ( $is_network && $use_network_settings ) ) {
329
	   $settings = get_option( $option_name );
330
	//   $update_network_option = false;
331
	//}
332
333
	// Next let's try to remove the key
334
	if( isset( $settings[ $key ] ) ) {
335
		unset( $settings[ $key ] );
336
	}
337
338
	$did_update = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $did_update is dead and can be removed.
Loading history...
339
	//if ( $update_network_option ) {
340
	//    $did_update = update_site_option( 'monsterinsights_settings', $settings );
341
	//} else {
342
		$did_update = update_option( $option_name, $settings );
343
	//}
344
345
	// If it updated, let's update the global variable
346
	if ( $did_update ){
347
		global $monsterinsights_settings;
348
		$monsterinsights_settings = $settings;
349
	}
350
351
	return $did_update;
352
}
353
354
 /**
355
 * Helper method for deleting multiple settings value.
356
 *
357
 * @since 6.0.0
358
 * @access public
359
 *
360
 * @param string $key   The setting key.
361
 * @return boolean True if removed, false if not.
362
 */
363
function monsterinsights_delete_options( $keys = array() ) {
364
	// If no keys, exit
365
	if ( empty( $keys ) || ! is_array( $keys ) ){
366
		return false;
367
	}
368
369
	$option_name = monsterinsights_get_option_name();
370
371
	// First let's grab the current settings
372
373
	// if on network panel or if on single site using network settings
374
	//$settings              = get_site_option( $option_name );
375
	//$use_network_settings  = ! empty( $use_network_settings['use_network_settings'] ) ? true : false;
376
	//$is_network            = is_multisite();
377
	//$update_network_option = true;
378
	//if ( ! is_network_admin() && ! ( $is_network && $use_network_settings ) ) {
379
	   $settings = get_option( $option_name );
380
	//   $update_network_option = false;
381
	//}
382
383
	// Next let's try to remove the keys
384
	foreach ( $keys as $key ) {
385
		if( isset( $settings[ $key ] ) ) {
386
			unset( $settings[ $key ] );
387
		}
388
	}
389
390
	$did_update = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $did_update is dead and can be removed.
Loading history...
391
	//if ( $update_network_option ) {
392
	//    $did_update = update_site_option( 'monsterinsights_settings', $settings );
393
	//} else {
394
		$did_update = update_option( $option_name, $settings );
395
	//}
396
397
	// If it updated, let's update the global variable
398
	if ( $did_update ){
399
		global $monsterinsights_settings;
400
		$monsterinsights_settings = $settings;
401
	}
402
403
	return $did_update;
404
}
405
406
function monsterinsights_sanitize_tracking_id( $id ) {
407
	$id = (string) $id; // Rare case, but let's make sure it never happens.
408
	$id = trim( $id );
409
410
	if ( empty( $id ) ) {
411
		return '';
412
	}
413
414
	// Replace all type of dashes (n-dash, m-dash, minus) with normal dashes.
415
	$id = str_replace( array( '–', '—', '−' ), '-', $id );
416
417
	return $id;
418
}
419
420
/**
421
 * Is valid ua code.
422
 *
423
 * @access public
424
 * @since 6.0.0
425
 *
426
 * @param string $ua_code UA code to check validity for.
427
 *
428
 * @return string|false Return cleaned ua string if valid, else returns false.
429
 */
430
function monsterinsights_is_valid_ua( $ua_code = '' ) {
431
	$ua_code = monsterinsights_sanitize_tracking_id( $ua_code );
432
433
	if ( preg_match( "/^(UA|YT|MO)-\d{4,}-\d+$/", $ua_code ) ) {
434
		return $ua_code;
435
	}
436
437
	return '';
438
}
439
440
function monsterinsights_is_valid_v4_id( $v4_code = '' ) {
441
	$v4_code = monsterinsights_sanitize_tracking_id( $v4_code );
442
443
	if ( preg_match( '/G-[A-Za-z\d]+/', $v4_code ) ) {
444
		return strtoupper( $v4_code );
445
	}
446
447
	return '';
448
}
449
450
/**
451
 * Helper method for getting the license information.
452
 *
453
 * @since 6.0.0
454
 * @access public
455
 *
456
 * @param string $key   The setting key to retrieve.
457
 * @param mixed $default_value   The default value of the setting key to retrieve.
458
 * @return string       The value of the setting.
459
 */
460
function monsterinsights_get_license() {
461
	$license  = MonsterInsights()->license->get_site_license();
0 ignored issues
show
Bug Best Practice introduced by
The property $license is declared protected in MonsterInsights_Lite. Since you implement __get, consider adding a @property or @property-read.
Loading history...
462
	$license  = $license ? $license : MonsterInsights()->license->get_network_license();
463
	$default  = MonsterInsights()->license->get_default_license_key();
464
	if ( empty( $license ) && ! empty( $default ) ) {
465
		$license        = array();
466
		$license['key'] = MonsterInsights()->license->get_default_license_key();
467
	}
468
	return $license;
469
}
470
471
/**
472
 * Helper method for getting the license key.
473
 *
474
 * @since 6.0.0
475
 * @access public
476
 *
477
 * @param string $key   The setting key to retrieve.
478
 * @param mixed $default_value   The default value of the setting key to retrieve.
479
 * @return string       The value of the setting.
480
 */
481
function monsterinsights_get_license_key() {
482
	if ( monsterinsights_is_pro_version() ) {
483
		return MonsterInsights()->license->get_license_key();
0 ignored issues
show
Bug Best Practice introduced by
The property $license is declared protected in MonsterInsights_Lite. Since you implement __get, consider adding a @property or @property-read.
Loading history...
484
	}
485
	return '';
486
}
487
488
function monsterinsights_get_option_name() {
489
	//if ( monsterinsights_is_pro_version() ) {
490
		return 'monsterinsights_settings';
491
	//} else {
492
	//	return 'monsterinsights_settings';
493
	//}
494
}
495
496
function monsterinsights_export_settings() {
497
	$settings = monsterinsights_get_options();
498
	$exclude  = array(
499
				'analytics_profile',
500
				'analytics_profile_code',
501
				'analytics_profile_name',
502
				'oauth_version',
503
				'cron_last_run',
504
				'monsterinsights_oauth_status',
505
	);
506
507
	foreach ( $exclude as $e ) {
508
		if ( ! empty( $settings[ $e ] ) ) {
509
			unset( $settings[ $e ] );
510
		}
511
	}
512
	return wp_json_encode( $settings );
513
}
514
515
/**
516
 * Always return 'gtag' when grabbing the tracking mode.
517
 *
518
 * @param string $value The value to override.
519
 *
520
 * @return string
521
 */
522
function monsterinsights_force_tracking_mode( $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

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

522
function monsterinsights_force_tracking_mode( /** @scrutinizer ignore-unused */ $value ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
523
	return 'gtag';
524
}
525
add_filter( 'monsterinsights_get_option_tracking_mode', 'monsterinsights_force_tracking_mode' );
526
527
/**
528
 * Always return 'js' when grabbing the events mode.
529
 *
530
 * @param string $value The value to override.
531
 *
532
 * @return string
533
 */
534
function monsterinsights_force_events_mode( $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

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

534
function monsterinsights_force_events_mode( /** @scrutinizer ignore-unused */ $value ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
535
	return 'js';
536
}
537
add_filter( 'monsterinsights_get_option_events_mode', 'monsterinsights_force_events_mode' );
538