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
|
|
|
|
35
|
|
|
return $settings; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Helper method for getting a setting's value. Falls back to the default |
40
|
|
|
* setting value if none exists in the options table. |
41
|
|
|
* |
42
|
|
|
* @param string $key The setting key to retrieve. |
43
|
|
|
* @param mixed $default The default value of the setting key to retrieve. |
44
|
|
|
* |
45
|
|
|
* @return string The value of the setting. |
46
|
|
|
* @since 6.0.0 |
47
|
|
|
* @access public |
48
|
|
|
* |
49
|
|
|
*/ |
50
|
|
|
function monsterinsights_get_option( $key = '', $default = false ) { |
51
|
|
|
global $monsterinsights_settings; |
52
|
|
|
$value = ! empty( $monsterinsights_settings[ $key ] ) ? $monsterinsights_settings[ $key ] : $default; |
53
|
|
|
$value = apply_filters( 'monsterinsights_get_option', $value, $key, $default ); |
54
|
|
|
|
55
|
|
|
return apply_filters( 'monsterinsights_get_option_' . $key, $value, $key, $default ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Helper method for getting the V4 string. |
60
|
|
|
* |
61
|
|
|
* @return string The V4 ID to use. |
62
|
|
|
* @since 6.0.0 |
63
|
|
|
* @access public |
64
|
|
|
* |
65
|
|
|
*/ |
66
|
|
|
function monsterinsights_get_v4_id() { |
67
|
|
|
// Allow short circuiting (for staging sites) |
68
|
|
|
if ( defined( 'MONSTERINSIGHTS_DISABLE_TRACKING' ) && MONSTERINSIGHTS_DISABLE_TRACKING ) { |
|
|
|
|
69
|
|
|
return ''; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Try getting it from the auth V4 |
73
|
|
|
$v4_id = MonsterInsights()->auth->get_v4_id(); |
|
|
|
|
74
|
|
|
|
75
|
|
|
// If that didn't work, try the manual V4 at the site level |
76
|
|
|
if ( empty( $v4_id ) ) { |
77
|
|
|
$v4_id = MonsterInsights()->auth->get_manual_v4_id(); |
78
|
|
|
// If that didn't work try getting it from the network |
79
|
|
|
if ( empty( $v4_id ) ) { |
80
|
|
|
$v4_id = monsterinsights_get_network_v4_id(); |
81
|
|
|
// If that didn't work, try getting it from the overall constant. If it's not there, leave it blank |
82
|
|
|
if ( empty( $v4_id ) ) { |
83
|
|
|
$v4_id = defined( 'MONSTERINSIGHTS_GA_V4_ID' ) && MONSTERINSIGHTS_GA_V4_ID ? monsterinsights_is_valid_v4_id( MONSTERINSIGHTS_GA_V4_ID ) : ''; |
|
|
|
|
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Feed through the filter |
89
|
|
|
$pre_filter = $v4_id; |
90
|
|
|
$v4_id = apply_filters( 'monsterinsights_get_v4_id', $v4_id ); |
91
|
|
|
|
92
|
|
|
// Only run through monsterinsights_is_valid_v4 if it's different than pre-filter |
93
|
|
|
return $pre_filter === $v4_id ? $v4_id : monsterinsights_is_valid_v4_id( $v4_id ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Helper method for getting the network V4 string. |
98
|
|
|
* |
99
|
|
|
* @return string The V4 ID to use. |
100
|
|
|
* @since 6.0.0 |
101
|
|
|
* @access public |
102
|
|
|
* |
103
|
|
|
*/ |
104
|
|
|
function monsterinsights_get_network_v4_id() { |
105
|
|
|
if ( ! is_multisite() ) { |
106
|
|
|
return ''; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// First try network auth V4 |
110
|
|
|
$v4_id = MonsterInsights()->auth->get_network_v4_id(); |
|
|
|
|
111
|
|
|
if ( ! empty( $v4_id ) ) { |
112
|
|
|
return $v4_id; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// Then try manual network V4 |
116
|
|
|
$v4_id = MonsterInsights()->auth->get_network_manual_v4_id(); |
117
|
|
|
if ( ! empty( $v4_id ) ) { |
118
|
|
|
return $v4_id; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// See if the constant is defined |
122
|
|
|
if ( defined( 'MONSTERINSIGHTS_MS_GA_V4_ID' ) && monsterinsights_is_valid_v4_id( MONSTERINSIGHTS_MS_GA_V4_ID ) ) { |
|
|
|
|
123
|
|
|
return MONSTERINSIGHTS_MS_GA_V4_ID; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return ''; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Helper method for getting the UA string that's output on the frontend. |
131
|
|
|
* |
132
|
|
|
* @param array $args Allow calling functions to give args to use in future applications. |
133
|
|
|
* |
134
|
|
|
* @return string The UA to use on frontend. |
135
|
|
|
* @since 6.0.0 |
136
|
|
|
* @access public |
137
|
|
|
* |
138
|
|
|
*/ |
139
|
|
|
function monsterinsights_get_v4_id_to_output( $args = array() ) { |
140
|
|
|
$v4_id = monsterinsights_get_v4_id(); |
141
|
|
|
$v4_id = apply_filters( 'monsterinsights_get_v4_id_to_output', $v4_id, $args ); |
142
|
|
|
|
143
|
|
|
return monsterinsights_is_valid_v4_id( $v4_id ); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Helper method for updating a setting's value. |
148
|
|
|
* |
149
|
|
|
* @param string $key The setting key. |
150
|
|
|
* @param string $value The value to set for the key. |
151
|
|
|
* |
152
|
|
|
* @return boolean True if updated, false if not. |
153
|
|
|
* @since 6.0.0 |
154
|
|
|
* @access public |
155
|
|
|
* |
156
|
|
|
*/ |
157
|
|
|
function monsterinsights_update_option( $key = '', $value = false ) { |
158
|
|
|
|
159
|
|
|
// If no key, exit |
160
|
|
|
if ( empty( $key ) ) { |
161
|
|
|
return false; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if ( empty( $value ) ) { |
165
|
|
|
$remove_option = monsterinsights_delete_option( $key ); |
166
|
|
|
|
167
|
|
|
return $remove_option; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$option_name = monsterinsights_get_option_name(); |
171
|
|
|
|
172
|
|
|
// First let's grab the current settings |
173
|
|
|
|
174
|
|
|
// if on network panel or if on single site using network settings |
175
|
|
|
//$settings = get_site_option( $option_name ); |
176
|
|
|
//$use_network_settings = ! empty( $use_network_settings['use_network_settings'] ) ? true : false; |
177
|
|
|
//$is_network = is_multisite(); |
178
|
|
|
//$update_network_option = true; |
179
|
|
|
//if ( ! is_network_admin() && ! ( $is_network && $use_network_settings ) ) { |
180
|
|
|
$settings = get_option( $option_name ); |
181
|
|
|
// $update_network_option = false; |
182
|
|
|
//} |
183
|
|
|
|
184
|
|
|
if ( ! is_array( $settings ) ) { |
185
|
|
|
$settings = array(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
// Let's let devs alter that value coming in |
189
|
|
|
$value = apply_filters( 'monsterinsights_update_option', $value, $key ); |
190
|
|
|
|
191
|
|
|
// Next let's try to update the value |
192
|
|
|
$settings[ $key ] = $value; |
193
|
|
|
$did_update = false; |
|
|
|
|
194
|
|
|
//if ( $update_network_option ) { |
195
|
|
|
// $did_update = update_site_option( $option_name, $settings ); |
196
|
|
|
//} else { |
197
|
|
|
$did_update = update_option( $option_name, $settings ); |
198
|
|
|
//} |
199
|
|
|
|
200
|
|
|
// If it updated, let's update the global variable |
201
|
|
|
if ( $did_update ) { |
202
|
|
|
global $monsterinsights_settings; |
203
|
|
|
$monsterinsights_settings[ $key ] = $value; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $did_update; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Helper method for deleting a setting's value. |
211
|
|
|
* |
212
|
|
|
* @param string $key The setting key. |
213
|
|
|
* |
214
|
|
|
* @return boolean True if removed, false if not. |
215
|
|
|
* @since 6.0.0 |
216
|
|
|
* @access public |
217
|
|
|
* |
218
|
|
|
*/ |
219
|
|
|
function monsterinsights_delete_option( $key = '' ) { |
220
|
|
|
// If no key, exit |
221
|
|
|
if ( empty( $key ) ) { |
222
|
|
|
return false; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$option_name = monsterinsights_get_option_name(); |
226
|
|
|
|
227
|
|
|
// First let's grab the current settings |
228
|
|
|
|
229
|
|
|
// if on network panel or if on single site using network settings |
230
|
|
|
//$settings = get_site_option( $option_name ); |
231
|
|
|
//$use_network_settings = ! empty( $use_network_settings['use_network_settings'] ) ? true : false; |
232
|
|
|
//$is_network = is_multisite(); |
233
|
|
|
//$update_network_option = true; |
234
|
|
|
//if ( ! is_network_admin() && ! ( $is_network && $use_network_settings ) ) { |
235
|
|
|
$settings = get_option( $option_name ); |
236
|
|
|
// $update_network_option = false; |
237
|
|
|
//} |
238
|
|
|
|
239
|
|
|
// Next let's try to remove the key |
240
|
|
|
if ( isset( $settings[ $key ] ) ) { |
241
|
|
|
unset( $settings[ $key ] ); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$did_update = false; |
|
|
|
|
245
|
|
|
//if ( $update_network_option ) { |
246
|
|
|
// $did_update = update_site_option( 'monsterinsights_settings', $settings ); |
247
|
|
|
//} else { |
248
|
|
|
$did_update = update_option( $option_name, $settings ); |
249
|
|
|
//} |
250
|
|
|
|
251
|
|
|
// If it updated, let's update the global variable |
252
|
|
|
if ( $did_update ) { |
253
|
|
|
global $monsterinsights_settings; |
254
|
|
|
$monsterinsights_settings = $settings; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $did_update; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Helper method for deleting multiple settings value. |
262
|
|
|
* |
263
|
|
|
* @param string $key The setting key. |
264
|
|
|
* |
265
|
|
|
* @return boolean True if removed, false if not. |
266
|
|
|
* @since 6.0.0 |
267
|
|
|
* @access public |
268
|
|
|
* |
269
|
|
|
*/ |
270
|
|
|
function monsterinsights_delete_options( $keys = array() ) { |
271
|
|
|
// If no keys, exit |
272
|
|
|
if ( empty( $keys ) || ! is_array( $keys ) ) { |
273
|
|
|
return false; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
$option_name = monsterinsights_get_option_name(); |
277
|
|
|
|
278
|
|
|
// First let's grab the current settings |
279
|
|
|
|
280
|
|
|
// if on network panel or if on single site using network settings |
281
|
|
|
//$settings = get_site_option( $option_name ); |
282
|
|
|
//$use_network_settings = ! empty( $use_network_settings['use_network_settings'] ) ? true : false; |
283
|
|
|
//$is_network = is_multisite(); |
284
|
|
|
//$update_network_option = true; |
285
|
|
|
//if ( ! is_network_admin() && ! ( $is_network && $use_network_settings ) ) { |
286
|
|
|
$settings = get_option( $option_name ); |
287
|
|
|
// $update_network_option = false; |
288
|
|
|
//} |
289
|
|
|
|
290
|
|
|
// Next let's try to remove the keys |
291
|
|
|
foreach ( $keys as $key ) { |
292
|
|
|
if ( isset( $settings[ $key ] ) ) { |
293
|
|
|
unset( $settings[ $key ] ); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
$did_update = false; |
|
|
|
|
298
|
|
|
//if ( $update_network_option ) { |
299
|
|
|
// $did_update = update_site_option( 'monsterinsights_settings', $settings ); |
300
|
|
|
//} else { |
301
|
|
|
$did_update = update_option( $option_name, $settings ); |
302
|
|
|
//} |
303
|
|
|
|
304
|
|
|
// If it updated, let's update the global variable |
305
|
|
|
if ( $did_update ) { |
306
|
|
|
global $monsterinsights_settings; |
307
|
|
|
$monsterinsights_settings = $settings; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
return $did_update; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
function monsterinsights_sanitize_tracking_id( $id ) { |
314
|
|
|
$id = (string) $id; // Rare case, but let's make sure it never happens. |
315
|
|
|
$id = trim( $id ); |
316
|
|
|
|
317
|
|
|
if ( empty( $id ) ) { |
318
|
|
|
return ''; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
// Replace all type of dashes (n-dash, m-dash, minus) with normal dashes. |
322
|
|
|
$id = str_replace( array( '–', '—', '−' ), '-', $id ); |
323
|
|
|
|
324
|
|
|
return $id; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Is this a valid GT code |
329
|
|
|
* |
330
|
|
|
* @param string $gt_code |
331
|
|
|
* |
332
|
|
|
* @return bool |
333
|
|
|
*/ |
334
|
|
|
function monsterinsights_is_valid_gt( $gt_code = '' ) { |
335
|
|
|
return (bool) preg_match( '/^GT-[a-zA-Z0-9]{5,}$/', $gt_code ); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
function monsterinsights_is_valid_v4_id( $v4_code = '' ) { |
339
|
|
|
$v4_code = monsterinsights_sanitize_tracking_id( $v4_code ); |
340
|
|
|
|
341
|
|
|
if ( |
342
|
|
|
preg_match( '/G-[A-Za-z\d]+/', $v4_code ) || |
343
|
|
|
monsterinsights_is_valid_gt( $v4_code ) |
344
|
|
|
) { |
345
|
|
|
return strtoupper( $v4_code ); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
return ''; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Helper method for getting the license information. |
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
|
|
|
* |
357
|
|
|
* @return string The value of the setting. |
358
|
|
|
* @since 6.0.0 |
359
|
|
|
* @access public |
360
|
|
|
* |
361
|
|
|
*/ |
362
|
|
|
function monsterinsights_get_license() { |
363
|
|
|
$license = MonsterInsights()->license->get_site_license(); |
|
|
|
|
364
|
|
|
$license = $license ? $license : MonsterInsights()->license->get_network_license(); |
365
|
|
|
$default = MonsterInsights()->license->get_default_license_key(); |
366
|
|
|
if ( empty( $license ) && ! empty( $default ) ) { |
367
|
|
|
$license = array(); |
368
|
|
|
$license['key'] = MonsterInsights()->license->get_default_license_key(); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
return $license; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Helper method for getting the license key. |
376
|
|
|
* |
377
|
|
|
* @param string $key The setting key to retrieve. |
378
|
|
|
* @param mixed $default_value The default value of the setting key to retrieve. |
379
|
|
|
* |
380
|
|
|
* @return string The value of the setting. |
381
|
|
|
* @since 6.0.0 |
382
|
|
|
* @access public |
383
|
|
|
* |
384
|
|
|
*/ |
385
|
|
|
function monsterinsights_get_license_key() { |
386
|
|
|
if ( monsterinsights_is_pro_version() ) { |
387
|
|
|
return MonsterInsights()->license->get_license_key(); |
|
|
|
|
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
return ''; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
function monsterinsights_get_option_name() { |
394
|
|
|
//if ( monsterinsights_is_pro_version() ) { |
395
|
|
|
return 'monsterinsights_settings'; |
396
|
|
|
//} else { |
397
|
|
|
// return 'monsterinsights_settings'; |
398
|
|
|
//} |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Export necessary settings to export as JSON. |
403
|
|
|
* |
404
|
|
|
* @return string |
405
|
|
|
*/ |
406
|
|
|
function monsterinsights_export_settings() { |
407
|
|
|
$settings = monsterinsights_get_options(); |
408
|
|
|
$exclude = array( |
409
|
|
|
'analytics_profile', |
410
|
|
|
'analytics_profile_code', |
411
|
|
|
'analytics_profile_name', |
412
|
|
|
'oauth_version', |
413
|
|
|
'cron_last_run', |
414
|
|
|
'monsterinsights_oauth_status', |
415
|
|
|
); |
416
|
|
|
|
417
|
|
|
foreach ( $exclude as $e ) { |
418
|
|
|
if ( ! empty( $settings[ $e ] ) ) { |
419
|
|
|
unset( $settings[ $e ] ); |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
// Get site notes. |
424
|
|
|
$settings['site_notes'] = monsterinsights_get_site_notes_to_export(); |
425
|
|
|
|
426
|
|
|
return wp_json_encode( $settings ); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Always return 'gtag' when grabbing the tracking mode. |
431
|
|
|
* |
432
|
|
|
* @param string $value The value to override. |
433
|
|
|
* |
434
|
|
|
* @return string |
435
|
|
|
*/ |
436
|
|
|
function monsterinsights_force_tracking_mode( $value ) { |
|
|
|
|
437
|
|
|
return 'gtag'; |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
add_filter( 'monsterinsights_get_option_tracking_mode', 'monsterinsights_force_tracking_mode' ); |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Always return 'js' when grabbing the events mode. |
444
|
|
|
* |
445
|
|
|
* @param string $value The value to override. |
446
|
|
|
* |
447
|
|
|
* @return string |
448
|
|
|
*/ |
449
|
|
|
function monsterinsights_force_events_mode( $value ) { |
|
|
|
|
450
|
|
|
return 'js'; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
add_filter( 'monsterinsights_get_option_events_mode', 'monsterinsights_force_events_mode' ); |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Prepare site notes to export. |
457
|
|
|
*/ |
458
|
|
|
function monsterinsights_get_site_notes_to_export() { |
459
|
|
|
$notes_db = new MonsterInsights_Site_Notes_DB_Base(); |
460
|
|
|
|
461
|
|
|
$note_items = $notes_db->get_items( array( |
462
|
|
|
'per_page' => -1, |
463
|
|
|
'orderby' => 'id', |
464
|
|
|
'order' => 'asc', |
465
|
|
|
'page' => 1, |
466
|
|
|
) ); |
467
|
|
|
|
468
|
|
|
$notes = array(); |
469
|
|
|
|
470
|
|
|
foreach ( $note_items['items'] as $note_item ) { |
471
|
|
|
$notes[] = array( |
472
|
|
|
'note_title' => $note_item['note_title'], |
473
|
|
|
'note_date' => $note_item['note_date_ymd'], |
474
|
|
|
'important' => $note_item['important'], |
475
|
|
|
'category_name' => empty( $note_item['category']['name'] ) ? '' : html_entity_decode( $note_item['category']['name'] ), |
476
|
|
|
); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
$categories = $notes_db->get_categories( array( |
480
|
|
|
'per_page' => -1, |
481
|
|
|
'page' => 1, |
482
|
|
|
'orderby' => 'term_id', |
483
|
|
|
'order' => 'asc', |
484
|
|
|
) ); |
485
|
|
|
|
486
|
|
|
$note_categories = array(); |
487
|
|
|
|
488
|
|
|
if ( is_array( $categories ) && ! empty( $categories ) ) { |
489
|
|
|
foreach ( $categories as $category ) { |
490
|
|
|
$note_categories[] = array( |
491
|
|
|
'name' => html_entity_decode( $category['name'] ), |
492
|
|
|
'color' => $category['background_color'], |
493
|
|
|
); |
494
|
|
|
} |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
return array( |
498
|
|
|
'notes' => $notes, |
499
|
|
|
'categories' => $note_categories, |
500
|
|
|
); |
501
|
|
|
} |
502
|
|
|
|