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(); |
|
|
|
|
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 ) { |
|
|
|
|
66
|
|
|
return ''; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Try getting it from the auth UA |
70
|
|
|
$ua = MonsterInsights()->auth->get_ua(); |
|
|
|
|
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 ) : ''; |
|
|
|
|
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
|
|
|
/** |
94
|
|
|
* Helper method for getting the network UA string. |
95
|
|
|
* |
96
|
|
|
* @since 6.0.0 |
97
|
|
|
* @access public |
98
|
|
|
* |
99
|
|
|
* @return string The UA to use. |
100
|
|
|
*/ |
101
|
|
|
function monsterinsights_get_network_ua() { |
102
|
|
|
if ( ! is_multisite() ) { |
103
|
|
|
return ''; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// First try network auth UA |
107
|
|
|
$ua = MonsterInsights()->auth->get_network_ua(); |
|
|
|
|
108
|
|
|
if ( ! empty( $ua ) ) { |
109
|
|
|
return $ua; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Then try manual network UA |
113
|
|
|
$ua = MonsterInsights()->auth->get_network_manual_ua(); |
114
|
|
|
if ( ! empty( $ua ) ) { |
115
|
|
|
return $ua; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// See if the constant is defined |
119
|
|
|
if ( defined( 'MONSTERINSIGHTS_MS_GA_UA' ) && monsterinsights_is_valid_ua( MONSTERINSIGHTS_MS_GA_UA ) ) { |
|
|
|
|
120
|
|
|
return MONSTERINSIGHTS_MS_GA_UA; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return ''; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Helper method for getting the UA string that's output on the frontend. |
128
|
|
|
* |
129
|
|
|
* @since 6.0.0 |
130
|
|
|
* @access public |
131
|
|
|
* |
132
|
|
|
* @param array $args Allow calling functions to give args to use in future applications. |
133
|
|
|
* @return string The UA to use on frontend. |
134
|
|
|
*/ |
135
|
|
|
function monsterinsights_get_ua_to_output( $args = array() ) { |
136
|
|
|
$ua = monsterinsights_get_ua(); |
137
|
|
|
$ua = apply_filters( 'monsterinsights_get_ua_to_output', $ua, $args ); |
138
|
|
|
return monsterinsights_is_valid_ua( $ua ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Helper method for updating a setting's value. |
143
|
|
|
* |
144
|
|
|
* @since 6.0.0 |
145
|
|
|
* @access public |
146
|
|
|
* |
147
|
|
|
* @param string $key The setting key. |
148
|
|
|
* @param string $value The value to set for the key. |
149
|
|
|
* @return boolean True if updated, false if not. |
150
|
|
|
*/ |
151
|
|
|
function monsterinsights_update_option( $key = '', $value = false ) { |
152
|
|
|
|
153
|
|
|
// If no key, exit |
154
|
|
|
if ( empty( $key ) ){ |
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if ( empty( $value ) ) { |
159
|
|
|
$remove_option = monsterinsights_delete_option( $key ); |
160
|
|
|
return $remove_option; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$option_name = monsterinsights_get_option_name(); |
164
|
|
|
|
165
|
|
|
// First let's grab the current settings |
166
|
|
|
|
167
|
|
|
// if on network panel or if on single site using network settings |
168
|
|
|
//$settings = get_site_option( $option_name ); |
169
|
|
|
//$use_network_settings = ! empty( $use_network_settings['use_network_settings'] ) ? true : false; |
170
|
|
|
//$is_network = is_multisite(); |
171
|
|
|
//$update_network_option = true; |
172
|
|
|
//if ( ! is_network_admin() && ! ( $is_network && $use_network_settings ) ) { |
173
|
|
|
$settings = get_option( $option_name ); |
174
|
|
|
// $update_network_option = false; |
175
|
|
|
//} |
176
|
|
|
|
177
|
|
|
if ( ! is_array( $settings ) ) { |
178
|
|
|
$settings = array(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// Let's let devs alter that value coming in |
182
|
|
|
$value = apply_filters( 'monsterinsights_update_option', $value, $key ); |
183
|
|
|
|
184
|
|
|
// Next let's try to update the value |
185
|
|
|
$settings[ $key ] = $value; |
186
|
|
|
$did_update = false; |
|
|
|
|
187
|
|
|
//if ( $update_network_option ) { |
188
|
|
|
// $did_update = update_site_option( $option_name, $settings ); |
189
|
|
|
//} else { |
190
|
|
|
$did_update = update_option( $option_name, $settings ); |
191
|
|
|
//} |
192
|
|
|
|
193
|
|
|
// If it updated, let's update the global variable |
194
|
|
|
if ( $did_update ){ |
195
|
|
|
global $monsterinsights_settings; |
196
|
|
|
$monsterinsights_settings[ $key ] = $value; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $did_update; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Helper method for deleting a setting's value. |
204
|
|
|
* |
205
|
|
|
* @since 6.0.0 |
206
|
|
|
* @access public |
207
|
|
|
* |
208
|
|
|
* @param string $key The setting key. |
209
|
|
|
* @return boolean True if removed, false if not. |
210
|
|
|
*/ |
211
|
|
|
function monsterinsights_delete_option( $key = '' ) { |
212
|
|
|
// If no key, exit |
213
|
|
|
if ( empty( $key ) ){ |
214
|
|
|
return false; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$option_name = monsterinsights_get_option_name(); |
218
|
|
|
|
219
|
|
|
// First let's grab the current settings |
220
|
|
|
|
221
|
|
|
// if on network panel or if on single site using network settings |
222
|
|
|
//$settings = get_site_option( $option_name ); |
223
|
|
|
//$use_network_settings = ! empty( $use_network_settings['use_network_settings'] ) ? true : false; |
224
|
|
|
//$is_network = is_multisite(); |
225
|
|
|
//$update_network_option = true; |
226
|
|
|
//if ( ! is_network_admin() && ! ( $is_network && $use_network_settings ) ) { |
227
|
|
|
$settings = get_option( $option_name ); |
228
|
|
|
// $update_network_option = false; |
229
|
|
|
//} |
230
|
|
|
|
231
|
|
|
// Next let's try to remove the key |
232
|
|
|
if( isset( $settings[ $key ] ) ) { |
233
|
|
|
unset( $settings[ $key ] ); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
$did_update = false; |
|
|
|
|
237
|
|
|
//if ( $update_network_option ) { |
238
|
|
|
// $did_update = update_site_option( 'monsterinsights_settings', $settings ); |
239
|
|
|
//} else { |
240
|
|
|
$did_update = update_option( $option_name, $settings ); |
241
|
|
|
//} |
242
|
|
|
|
243
|
|
|
// If it updated, let's update the global variable |
244
|
|
|
if ( $did_update ){ |
245
|
|
|
global $monsterinsights_settings; |
246
|
|
|
$monsterinsights_settings = $settings; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return $did_update; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Helper method for deleting multiple settings value. |
254
|
|
|
* |
255
|
|
|
* @since 6.0.0 |
256
|
|
|
* @access public |
257
|
|
|
* |
258
|
|
|
* @param string $key The setting key. |
259
|
|
|
* @return boolean True if removed, false if not. |
260
|
|
|
*/ |
261
|
|
|
function monsterinsights_delete_options( $keys = array() ) { |
262
|
|
|
// If no keys, exit |
263
|
|
|
if ( empty( $keys ) || ! is_array( $keys ) ){ |
264
|
|
|
return false; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
$option_name = monsterinsights_get_option_name(); |
268
|
|
|
|
269
|
|
|
// First let's grab the current settings |
270
|
|
|
|
271
|
|
|
// if on network panel or if on single site using network settings |
272
|
|
|
//$settings = get_site_option( $option_name ); |
273
|
|
|
//$use_network_settings = ! empty( $use_network_settings['use_network_settings'] ) ? true : false; |
274
|
|
|
//$is_network = is_multisite(); |
275
|
|
|
//$update_network_option = true; |
276
|
|
|
//if ( ! is_network_admin() && ! ( $is_network && $use_network_settings ) ) { |
277
|
|
|
$settings = get_option( $option_name ); |
278
|
|
|
// $update_network_option = false; |
279
|
|
|
//} |
280
|
|
|
|
281
|
|
|
// Next let's try to remove the keys |
282
|
|
|
foreach ( $keys as $key ) { |
283
|
|
|
if( isset( $settings[ $key ] ) ) { |
284
|
|
|
unset( $settings[ $key ] ); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
$did_update = false; |
|
|
|
|
289
|
|
|
//if ( $update_network_option ) { |
290
|
|
|
// $did_update = update_site_option( 'monsterinsights_settings', $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 = $settings; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return $did_update; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Is valid ua code. |
306
|
|
|
* |
307
|
|
|
* @access public |
308
|
|
|
* @since 6.0.0 |
309
|
|
|
* |
310
|
|
|
* @param string $ua_code UA code to check validity for. |
311
|
|
|
* |
312
|
|
|
* @return string|false Return cleaned ua string if valid, else returns false. |
313
|
|
|
*/ |
314
|
|
|
function monsterinsights_is_valid_ua( $ua_code = '' ) { |
315
|
|
|
$ua_code = (string) $ua_code; // Rare case, but let's make sure it never happens. |
316
|
|
|
$ua_code = trim( $ua_code ); |
317
|
|
|
|
318
|
|
|
if ( empty( $ua_code ) ) { |
319
|
|
|
return ''; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
// Replace all type of dashes (n-dash, m-dash, minus) with normal dashes. |
323
|
|
|
$ua_code = str_replace( array( '–', '—', '−' ), '-', $ua_code ); |
324
|
|
|
|
325
|
|
|
if ( preg_match( "/^(UA|YT|MO)-\d{4,}-\d+$/", strval( $ua_code ) ) ) { |
326
|
|
|
return $ua_code; |
327
|
|
|
} else { |
328
|
|
|
return ''; |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Helper method for getting the license information. |
334
|
|
|
* |
335
|
|
|
* @since 6.0.0 |
336
|
|
|
* @access public |
337
|
|
|
* |
338
|
|
|
* @param string $key The setting key to retrieve. |
339
|
|
|
* @param mixed $default_value The default value of the setting key to retrieve. |
340
|
|
|
* @return string The value of the setting. |
341
|
|
|
*/ |
342
|
|
|
function monsterinsights_get_license() { |
343
|
|
|
$license = MonsterInsights()->license->get_site_license(); |
|
|
|
|
344
|
|
|
$license = $license ? $license : MonsterInsights()->license->get_network_license(); |
345
|
|
|
$default = MonsterInsights()->license->get_default_license_key(); |
346
|
|
|
if ( empty( $license ) && ! empty( $default ) ) { |
347
|
|
|
$license = array(); |
348
|
|
|
$license['key'] = MonsterInsights()->license->get_default_license_key(); |
349
|
|
|
} |
350
|
|
|
return $license; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Helper method for getting the license key. |
355
|
|
|
* |
356
|
|
|
* @since 6.0.0 |
357
|
|
|
* @access public |
358
|
|
|
* |
359
|
|
|
* @param string $key The setting key to retrieve. |
360
|
|
|
* @param mixed $default_value The default value of the setting key to retrieve. |
361
|
|
|
* @return string The value of the setting. |
362
|
|
|
*/ |
363
|
|
|
function monsterinsights_get_license_key() { |
364
|
|
|
if ( monsterinsights_is_pro_version() ) { |
365
|
|
|
return MonsterInsights()->license->get_license_key(); |
|
|
|
|
366
|
|
|
} |
367
|
|
|
return ''; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
function monsterinsights_get_option_name() { |
371
|
|
|
//if ( monsterinsights_is_pro_version() ) { |
372
|
|
|
return 'monsterinsights_settings'; |
373
|
|
|
//} else { |
374
|
|
|
// return 'monsterinsights_settings'; |
375
|
|
|
//} |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
function monsterinsights_export_settings() { |
379
|
|
|
$settings = monsterinsights_get_options(); |
380
|
|
|
$exclude = array( |
381
|
|
|
'analytics_profile', |
382
|
|
|
'analytics_profile_code', |
383
|
|
|
'analytics_profile_name', |
384
|
|
|
'oauth_version', |
385
|
|
|
'cron_last_run', |
386
|
|
|
'monsterinsights_oauth_status', |
387
|
|
|
); |
388
|
|
|
|
389
|
|
|
foreach ( $exclude as $e ) { |
390
|
|
|
if ( ! empty( $settings[ $e ] ) ) { |
391
|
|
|
unset( $settings[ $e ] ); |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
return wp_json_encode( $settings ); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Always return 'gtag' when grabbing the tracking mode. |
399
|
|
|
* |
400
|
|
|
* @param string $value The value to override. |
401
|
|
|
* |
402
|
|
|
* @return string |
403
|
|
|
*/ |
404
|
|
|
function monsterinsights_force_tracking_mode( $value ) { |
|
|
|
|
405
|
|
|
return 'gtag'; |
406
|
|
|
} |
407
|
|
|
add_filter( 'monsterinsights_get_option_tracking_mode', 'monsterinsights_force_tracking_mode' ); |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* Always return 'js' when grabbing the events mode. |
411
|
|
|
* |
412
|
|
|
* @param string $value The value to override. |
413
|
|
|
* |
414
|
|
|
* @return string |
415
|
|
|
*/ |
416
|
|
|
function monsterinsights_force_events_mode( $value ) { |
|
|
|
|
417
|
|
|
return 'js'; |
418
|
|
|
} |
419
|
|
|
add_filter( 'monsterinsights_get_option_events_mode', 'monsterinsights_force_events_mode' ); |
420
|
|
|
|