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