|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Routes for VUE are registered here. |
|
4
|
|
|
* |
|
5
|
|
|
* @package monsterinsights |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class MonsterInsights_Rest_Routes |
|
10
|
|
|
*/ |
|
11
|
|
|
class MonsterInsights_Rest_Routes { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* MonsterInsights_Rest_Routes constructor. |
|
15
|
|
|
*/ |
|
16
|
|
|
public function __construct() { |
|
17
|
|
|
|
|
18
|
|
|
add_action( 'wp_ajax_monsterinsights_vue_get_license', array( $this, 'get_license' ) ); |
|
19
|
|
|
add_action( 'wp_ajax_monsterinsights_vue_get_profile', array( $this, 'get_profile' ) ); |
|
20
|
|
|
add_action( 'wp_ajax_monsterinsights_vue_get_settings', array( $this, 'get_settings' ) ); |
|
21
|
|
|
add_action( 'wp_ajax_monsterinsights_vue_update_settings', array( $this, 'update_settings' ) ); |
|
22
|
|
|
add_action( 'wp_ajax_monsterinsights_vue_get_addons', array( $this, 'get_addons' ) ); |
|
23
|
|
|
add_action( 'wp_ajax_monsterinsights_update_manual_ua', array( $this, 'update_manual_ua' ) ); |
|
24
|
|
|
|
|
25
|
|
|
add_action( 'admin_notices', array( $this, 'hide_old_notices' ), 0 ); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Ajax handler for grabbing the license |
|
30
|
|
|
*/ |
|
31
|
|
|
public function get_license() { |
|
32
|
|
|
|
|
33
|
|
|
check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
|
34
|
|
|
|
|
35
|
|
|
if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
|
36
|
|
|
return; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$site_license = array( |
|
40
|
|
|
'key' => MonsterInsights()->license->get_site_license_key(), |
|
41
|
|
|
'type' => MonsterInsights()->license->get_site_license_type(), |
|
42
|
|
|
'is_disabled' => MonsterInsights()->license->site_license_disabled(), |
|
43
|
|
|
'is_expired' => MonsterInsights()->license->site_license_expired(), |
|
44
|
|
|
'is_invalid' => MonsterInsights()->license->site_license_invalid(), |
|
45
|
|
|
); |
|
46
|
|
|
$network_license = array( |
|
47
|
|
|
'key' => MonsterInsights()->license->get_network_license_key(), |
|
48
|
|
|
'type' => MonsterInsights()->license->get_network_license_type(), |
|
49
|
|
|
'is_disabled' => MonsterInsights()->license->network_license_disabled(), |
|
50
|
|
|
'is_expired' => MonsterInsights()->license->network_license_expired(), |
|
51
|
|
|
'is_invalid' => MonsterInsights()->license->network_license_disabled(), |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
wp_send_json( array( |
|
55
|
|
|
'site' => $site_license, |
|
56
|
|
|
'network' => $network_license, |
|
57
|
|
|
) ); |
|
58
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Ajax handler for grabbing the license |
|
63
|
|
|
*/ |
|
64
|
|
|
public function get_profile() { |
|
65
|
|
|
|
|
66
|
|
|
check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
|
67
|
|
|
|
|
68
|
|
|
if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
wp_send_json( array( |
|
73
|
|
|
'ua' => MonsterInsights()->auth->get_ua(), |
|
74
|
|
|
'viewname' => MonsterInsights()->auth->get_viewname(), |
|
75
|
|
|
'manual_ua' => MonsterInsights()->auth->get_manual_ua(), |
|
76
|
|
|
'network_ua' => MonsterInsights()->auth->get_network_ua(), |
|
77
|
|
|
'network_viewname' => MonsterInsights()->auth->get_network_viewname(), |
|
78
|
|
|
'network_manual_ua' => MonsterInsights()->auth->get_network_manual_ua(), |
|
79
|
|
|
) ); |
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Ajax handler for grabbing the license |
|
85
|
|
|
*/ |
|
86
|
|
|
public function get_settings() { |
|
87
|
|
|
|
|
88
|
|
|
check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
|
89
|
|
|
|
|
90
|
|
|
if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
|
91
|
|
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$options = monsterinsights_get_options(); |
|
95
|
|
|
|
|
96
|
|
|
// Array fields are needed even if empty. |
|
97
|
|
|
$array_fields = array( 'view_reports', 'save_settings', 'ignore_users' ); |
|
98
|
|
|
foreach ( $array_fields as $array_field ) { |
|
99
|
|
|
if ( ! isset( $options[ $array_field ] ) ) { |
|
100
|
|
|
$options[ $array_field ] = array(); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
if ( isset( $options['custom_code'] ) ) { |
|
104
|
|
|
$options['custom_code'] = stripslashes( $options['custom_code'] ); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
wp_send_json( $options ); |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Ajax handler for grabbing the license |
|
113
|
|
|
*/ |
|
114
|
|
|
public function update_settings() { |
|
115
|
|
|
|
|
116
|
|
|
check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
|
117
|
|
|
|
|
118
|
|
|
if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
|
119
|
|
|
return; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if ( isset( $_POST['setting'] ) ) { |
|
123
|
|
|
$setting = sanitize_text_field( wp_unslash( $_POST['setting'] ) ); |
|
124
|
|
|
if ( isset( $_POST['value'] ) ) { |
|
125
|
|
|
$value = $this->handle_sanitization( $setting, $_POST['value'] ); |
|
126
|
|
|
monsterinsights_update_option( $setting, $value ); |
|
127
|
|
|
} else { |
|
128
|
|
|
monsterinsights_update_option( $setting, false ); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
wp_send_json_success(); |
|
133
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Sanitization specific to each field. |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $field The key of the field to sanitize. |
|
140
|
|
|
* @param string $value The value of the field to sanitize. |
|
141
|
|
|
* |
|
142
|
|
|
* @return mixed The sanitized input. |
|
143
|
|
|
*/ |
|
144
|
|
|
private function handle_sanitization( $field, $value ) { |
|
145
|
|
|
|
|
146
|
|
|
$value = wp_unslash( $value ); |
|
147
|
|
|
|
|
148
|
|
|
// Textarea fields. |
|
149
|
|
|
$textarea_fields = array( |
|
150
|
|
|
'custom_code', |
|
151
|
|
|
); |
|
152
|
|
|
|
|
153
|
|
|
if ( in_array( $field, $textarea_fields, true ) ) { |
|
154
|
|
|
if ( function_exists( 'sanitize_textarea_field' ) ) { |
|
155
|
|
|
return sanitize_textarea_field( $value ); |
|
|
|
|
|
|
156
|
|
|
} else { |
|
157
|
|
|
return wp_kses( $value, array() ); |
|
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$array_value = json_decode( $value, true ); |
|
|
|
|
|
|
162
|
|
|
if ( is_array( $array_value ) ) { |
|
163
|
|
|
$value = $array_value; |
|
164
|
|
|
// Don't save empty values. |
|
165
|
|
|
foreach ( $value as $key => $item ) { |
|
166
|
|
|
if ( is_array( $item ) ) { |
|
167
|
|
|
$empty = true; |
|
168
|
|
|
foreach ( $item as $item_value ) { |
|
169
|
|
|
if ( ! empty( $item_value ) ) { |
|
170
|
|
|
$empty = false; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
if ( $empty ) { |
|
174
|
|
|
unset( $value[ $key ] ); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
// Reset array keys because JavaScript can't handle arrays with non-sequential keys. |
|
180
|
|
|
$value = array_values( $value ); |
|
181
|
|
|
|
|
182
|
|
|
return $value; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return sanitize_text_field( $value ); |
|
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Return the state of the addons ( installed, activated ) |
|
191
|
|
|
*/ |
|
192
|
|
|
public function get_addons() { |
|
193
|
|
|
|
|
194
|
|
|
check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
|
195
|
|
|
|
|
196
|
|
|
if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
|
197
|
|
|
return; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$addons_data = monsterinsights_get_addons(); |
|
201
|
|
|
$parsed_addons = array(); |
|
202
|
|
|
$installed_plugins = get_plugins(); |
|
203
|
|
|
|
|
204
|
|
|
foreach ( $addons_data as $addons_type => $addons ) { |
|
205
|
|
|
foreach ( $addons as $addon ) { |
|
206
|
|
|
$slug = 'monsterinsights-' . $addon->slug; |
|
207
|
|
|
if ( 'monsterinsights-ecommerce' === $slug ) { |
|
208
|
|
|
$addon = $this->get_addon( $installed_plugins, $addons_type, $addon, $slug ); |
|
209
|
|
|
if ( empty( $addon->installed ) ) { |
|
210
|
|
|
$slug = 'ga-ecommerce'; |
|
211
|
|
|
$addon = $this->get_addon( $installed_plugins, $addons_type, $addon, $slug ); |
|
212
|
|
|
} |
|
213
|
|
|
} else { |
|
214
|
|
|
$addon = $this->get_addon( $installed_plugins, $addons_type, $addon, $slug ); |
|
215
|
|
|
} |
|
216
|
|
|
$parsed_addons[ $addon->slug ] = $addon; |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
// Include data about the plugins needed by some addons ( WooCommerce, EDD, Google AMP, CookieBot, etc ). |
|
221
|
|
|
// WooCommerce. |
|
222
|
|
|
$parsed_addons['woocommerce'] = array( |
|
223
|
|
|
'active' => class_exists( 'WooCommerce' ), |
|
224
|
|
|
); |
|
225
|
|
|
// Edd. |
|
226
|
|
|
$parsed_addons['easy_digital_downloads'] = array( |
|
227
|
|
|
'active' => class_exists( 'Easy_Digital_Downloads' ), |
|
228
|
|
|
); |
|
229
|
|
|
// MemberPress. |
|
230
|
|
|
$parsed_addons['memberpress'] = array( |
|
231
|
|
|
'active' => defined( 'MEPR_VERSION' ) && version_compare( MEPR_VERSION, '1.3.43', '>' ), |
|
|
|
|
|
|
232
|
|
|
); |
|
233
|
|
|
// Cookiebot. |
|
234
|
|
|
$parsed_addons['cookiebot'] = array( |
|
235
|
|
|
'active' => function_exists( 'cookiebot_active' ) && cookiebot_active(), |
|
236
|
|
|
); |
|
237
|
|
|
// Cookie Notice. |
|
238
|
|
|
$parsed_addons['cookie_notice'] = array( |
|
239
|
|
|
'active' => class_exists( 'Cookie_Notice' ), |
|
240
|
|
|
); |
|
241
|
|
|
// Fb Instant Articles. |
|
242
|
|
|
$parsed_addons['instant_articles'] = array( |
|
243
|
|
|
'active' => defined( 'IA_PLUGIN_VERSION' ) && version_compare( IA_PLUGIN_VERSION, '3.3.4', '>' ), |
|
|
|
|
|
|
244
|
|
|
); |
|
245
|
|
|
// Google AMP. |
|
246
|
|
|
$parsed_addons['google_amp'] = array( |
|
247
|
|
|
'active' => defined( 'AMP__FILE__' ), |
|
248
|
|
|
); |
|
249
|
|
|
// WPForms. |
|
250
|
|
|
$parsed_addons['wpforms'] = array( |
|
251
|
|
|
'active' => function_exists( 'wpforms' ), |
|
252
|
|
|
); |
|
253
|
|
|
// Gravity Forms. |
|
254
|
|
|
$parsed_addons['gravity_forms'] = array( |
|
255
|
|
|
'active' => class_exists( 'GFCommon' ), |
|
256
|
|
|
); |
|
257
|
|
|
// Formidable Forms. |
|
258
|
|
|
$parsed_addons['formidable_forms'] = array( |
|
259
|
|
|
'active' => class_exists( 'FrmHooksController' ), |
|
260
|
|
|
); |
|
261
|
|
|
|
|
262
|
|
|
wp_send_json( $parsed_addons ); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
public function get_addon( $installed_plugins, $addons_type, $addon, $slug ) { |
|
266
|
|
|
$active = false; |
|
267
|
|
|
$installed = false; |
|
268
|
|
|
$plugin_basename = monsterinsights_get_plugin_basename_from_slug( $slug ); |
|
269
|
|
|
|
|
270
|
|
|
if ( isset( $installed_plugins[ $plugin_basename ] ) ) { |
|
271
|
|
|
$installed = true; |
|
272
|
|
|
$ms_active = is_plugin_active_for_network( $plugin_basename ); |
|
|
|
|
|
|
273
|
|
|
$ss_active = is_plugin_active( $plugin_basename ); |
|
|
|
|
|
|
274
|
|
|
|
|
275
|
|
|
if ( is_multisite() && is_network_admin() ) { |
|
276
|
|
|
$active = is_plugin_active_for_network( $plugin_basename ); |
|
277
|
|
|
} else { |
|
278
|
|
|
$active = is_plugin_active( $plugin_basename ); |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
if ( empty( $addon->url ) ) { |
|
282
|
|
|
$addon->url = ''; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
$addon->type = $addons_type; |
|
286
|
|
|
$addon->installed = $installed; |
|
287
|
|
|
$addon->active = $active; |
|
288
|
|
|
$addon->basename = $plugin_basename; |
|
289
|
|
|
return $addon; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Use custom notices in the Vue app on the Settings screen. |
|
294
|
|
|
*/ |
|
295
|
|
|
public function hide_old_notices() { |
|
296
|
|
|
|
|
297
|
|
|
$screen = get_current_screen(); |
|
|
|
|
|
|
298
|
|
|
// Bail if we're not on a MonsterInsights screen. |
|
299
|
|
|
if ( empty( $screen->id ) || strpos( $screen->id, 'monsterinsights' ) === false ) { |
|
300
|
|
|
return; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
// Hide admin notices on the settings screen. |
|
304
|
|
|
if ( in_array( $screen->id, array( |
|
305
|
|
|
'insights_page_monsterinsights_settings', |
|
306
|
|
|
'toplevel_page_monsterinsights_settings', |
|
307
|
|
|
'toplevel_page_monsterinsights_network-network', |
|
308
|
|
|
'insights_page_monsterinsights_network', |
|
309
|
|
|
), true ) ) { |
|
310
|
|
|
remove_all_actions( 'admin_notices' ); |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* Update manual ua. |
|
317
|
|
|
*/ |
|
318
|
|
|
public function update_manual_ua() { |
|
319
|
|
|
|
|
320
|
|
|
check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
|
321
|
|
|
|
|
322
|
|
|
if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
|
323
|
|
|
return; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
$manual_ua_code = isset( $_POST['manual_ua_code'] ) ? sanitize_text_field( wp_unslash( $_POST['manual_ua_code'] ) ) : ''; |
|
327
|
|
|
$manual_ua_code = monsterinsights_is_valid_ua( $manual_ua_code ); // Also sanitizes the string. |
|
328
|
|
|
$manual_ua_code_old = MonsterInsights()->auth->get_manual_ua(); |
|
329
|
|
|
if ( ! empty( $_REQUEST['isnetwork'] ) && sanitize_text_field( wp_unslash( $_REQUEST['isnetwork'] ) ) ) { |
|
330
|
|
|
define( 'WP_NETWORK_ADMIN', true ); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
if ( $manual_ua_code && $manual_ua_code_old && $manual_ua_code_old === $manual_ua_code ) { |
|
334
|
|
|
// Same code we had before |
|
335
|
|
|
// Do nothing. |
|
336
|
|
|
wp_send_json_success(); |
|
337
|
|
|
} else if ( $manual_ua_code && $manual_ua_code_old && $manual_ua_code_old !== $manual_ua_code ) { |
|
338
|
|
|
// Different UA code. |
|
339
|
|
|
if ( is_network_admin() ) { |
|
340
|
|
|
MonsterInsights()->auth->set_network_manual_ua( $manual_ua_code ); |
|
341
|
|
|
} else { |
|
342
|
|
|
MonsterInsights()->auth->set_manual_ua( $manual_ua_code ); |
|
343
|
|
|
} |
|
344
|
|
|
} else if ( $manual_ua_code && empty( $manual_ua_code_old ) ) { |
|
345
|
|
|
// Move to manual. |
|
346
|
|
|
if ( is_network_admin() ) { |
|
347
|
|
|
MonsterInsights()->auth->set_network_manual_ua( $manual_ua_code ); |
|
348
|
|
|
} else { |
|
349
|
|
|
MonsterInsights()->auth->set_manual_ua( $manual_ua_code ); |
|
350
|
|
|
} |
|
351
|
|
|
} else if ( empty( $manual_ua_code ) && $manual_ua_code_old ) { |
|
352
|
|
|
// Deleted manual. |
|
353
|
|
|
if ( is_network_admin() ) { |
|
354
|
|
|
MonsterInsights()->auth->delete_network_manual_ua(); |
|
355
|
|
|
} else { |
|
356
|
|
|
MonsterInsights()->auth->delete_manual_ua(); |
|
357
|
|
|
} |
|
358
|
|
|
} else if ( isset( $_POST['manual_ua_code'] ) && empty( $manual_ua_code ) ) { |
|
359
|
|
|
wp_send_json_error(); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
wp_send_json_success(); |
|
363
|
|
|
} |
|
364
|
|
|
} |
|
365
|
|
|
|