1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
|
<?php namespace WpAssetCleanUp;
/** * Class PluginTracking * @package WpAssetCleanUp */ class PluginTracking { /** * The data to send to the Asset CleanUp site * * @access private */ public $data;
/** * Initiate Settings Class * * @var Settings */ public $settings;
/** * @var bool */ public $showTrackingNotice = false;
/** * PluginTracking constructor. */ public function __construct() { $this->settings = new Settings(); }
/** * */ public function init() { // Schedule add_action('wp', array($this, 'schedule_events')); add_action('init', array($this, 'schedule_send'));
// Triggers when Buttons from the Top Notice are clicked and page is reloaded (non-AJAX call) // This is a fallback in case there are JS errors and the AJAX call is not triggering if (isset($_GET['wpacu_is_page_reload']) && $_GET['wpacu_is_page_reload']) { add_action('admin_init', array($this, 'opt_in_out')); }
// Before "Settings" are saved in the database, right after form submit // Check "Allow Usage Tracking" value and take action if it's enabled add_action('wpacu_before_save_settings', array($this, 'check_for_settings_optin'));
// Notice on the top screen within the Dashboard to get permission from the user to allow tracking add_action('admin_notices', array($this, 'admin_notice'));
add_action('admin_head', array($this, 'notice_styles')); add_action('admin_footer', array($this, 'notice_scripts'));
// Close the notice when action is taken by AJAX call add_action('wp_ajax_' . WPACU_PLUGIN_ID . '_close_tracking_notice', array($this, 'ajaxCloseTrackingNoticeCallback')); }
/** * @param bool $isAjaxCall * @return bool|string|void */ public function opt_in_out($isAjaxCall = false) { if (! array_key_exists('wpacu_action', $_REQUEST)) { return false; }
$response = ''; $redirect = true;
if ($isAjaxCall) { $redirect = false; }
$wpacuAction = isset($_REQUEST['wpacu_action']) ? $_REQUEST['wpacu_action'] : '';
if ($wpacuAction === 'wpacu_opt_into_tracking') { $response = $this->check_for_optin(); }
if ($wpacuAction === 'wpacu_opt_out_of_tracking') { $response = $this->check_for_optout();
if ($redirect) { // Reload the same page without the Asset CleanUp query action wp_redirect(remove_query_arg(array('wpacu_action', 'wpacu_is_page_reload'))); exit(); } }
return $response; }
/** * Trigger scheduling */ public function schedule_events() { $this->weekly_events(); }
/** * Schedule weekly events * * @access private * @since 1.6 * @return void */ private function weekly_events() { if (! wp_next_scheduled('wpacu_weekly_scheduled_events')) { wp_schedule_event(current_time('timestamp', true), 'weekly', 'wpacu_weekly_scheduled_events'); } }
/** * Check if the user has opted into tracking * * @access private * @return bool */ private function tracking_allowed() { $allowUsageTracking = $this->settings->getOption('allow_usage_tracking'); return (bool) $allowUsageTracking; } /** * Setup the data that is going to be tracked * * @access private * @return void */ public function setup_data() { $data = array();
// Retrieve current theme info $theme_data = wp_get_theme(); $theme = $theme_data->Name . ' ' . $theme_data->Version;
$settingsClass = new Settings();
$data['php_version'] = phpversion(); $data['wpacu_version'] = WPACU_PLUGIN_VERSION; $data['wpacu_settings'] = $settingsClass->getAll(); $data['wpacu_first_usage'] = get_option(WPACU_PLUGIN_ID.'_first_usage'); $data['wpacu_review_info'] = get_option(WPACU_PLUGIN_ID.'_review_notice_status'); $data['wp_version'] = get_bloginfo('version'); $data['server'] = isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : ''; $data['multisite'] = is_multisite() ? 'Yes' : 'No'; $data['theme'] = $theme;
// Retrieve current plugin information $adminPluginFile = ABSPATH . '/wp-admin/includes/plugin.php'; if (! function_exists( 'get_plugins') && is_file($adminPluginFile)) { include $adminPluginFile; }
$plugins = array_keys(get_plugins()); $active_plugins = get_option('active_plugins', array());
foreach ($plugins as $key => $plugin) { if (in_array($plugin, $active_plugins)) { // Remove active plugins from list so we can show active and inactive separately unset($plugins[$key]); } }
$data['active_plugins'] = $active_plugins; $data['inactive_plugins'] = $plugins; $data['locale'] = get_locale();
$this->data = $data; }
/** * Send the data to the Asset CleanUp server * * @access private * * @param bool $override If we should override the tracking setting. * @param bool $ignore_last_checkin If we should ignore when the last check in was. * * @return bool */ public function send_checkin($override = false, $ignore_last_checkin = false) { // Allows us to stop the plugin's own site from checking in, and a filter for any related sites if (apply_filters('wpacu_disable_tracking_checkin', false)) { return false; }
if (! $override && ! $this->tracking_allowed()) { return false; }
// Send a maximum of once per week $last_send = $this->get_last_send();
if (! $ignore_last_checkin && is_numeric($last_send) && $last_send > strtotime('-1 week')) { return 'Not Sent: Only Weekly'; }
$this->setup_data();
$response = wp_remote_post('https://www.gabelivan.com/tracking/?wpacu_action=checkin', array( 'method' => 'POST', 'timeout' => 8, 'redirection' => 5, 'httpversion' => '1.1', 'blocking' => false, 'body' => $this->data, 'user-agent' => 'WPACU/' . WPACU_PLUGIN_VERSION . '; ' . get_bloginfo('url') ));
Misc::addUpdateOption(WPACU_PLUGIN_ID.'_tracking_last_send', time());
return wp_remote_retrieve_body($response); }
/** * @param $savedSettings * * @return array */ public function check_for_settings_optin($savedSettings) { // Send an initial check in when "Settings" are saved if (isset($savedSettings['allow_usage_tracking']) && $savedSettings['allow_usage_tracking'] == 1) { $this->send_checkin( true ); }
return $savedSettings; }
/** * Check for a new opt-in via the admin notice or after "Settings" is saved * * @return bool|void */ public function check_for_optin() { if (! Menu::userCanManageAssets()) { return; }
// Update the value in the "Settings" area $this->settings->updateOption('allow_usage_tracking', 1);
// Send the tracking data $response = $this->send_checkin(true);
// Mark the notice to be hidden Misc::addUpdateOption(WPACU_PLUGIN_ID . '_hide_tracking_notice', 1);
return $response; }
/** * @return string */ public function check_for_optout() { if (! Menu::userCanManageAssets()) { return 'Unauthorized'; }
// Disable tracking option from "Settings" and mark the notice as hidden (to not show again) $this->settings->deleteOption('allow_usage_tracking'); Misc::addUpdateOption(WPACU_PLUGIN_ID . '_hide_tracking_notice', 1);
return 'success'; }
/** * Get the last time a checkin was sent * * @access private * @return false|string */ private function get_last_send() { return get_option(WPACU_PLUGIN_ID . '_tracking_last_send'); }
/** * Schedule a weekly checkin * * We send once a week (while tracking is allowed) to check in, which can be * used to determine active sites. * * @return void */ public function schedule_send() { if (Misc::doingCron()) { add_action('wpacu_weekly_scheduled_events', array($this, 'send_checkin')); } }
/** * Returns true or false for showing the top tracking notice * * @return bool */ public function show_tracking_notice() { // On URL request (for debugging) if (array_key_exists('wpacu_show_tracking_notice', $_GET)) { return true; }
// If another Asset CleanUp notice (e.g. for plugin review) is already shown // don't also show this one below/above it if (defined('WPACU_ADMIN_REVIEW_NOTICE_SHOWN')) { return false; }
$hide_notice = get_option(WPACU_PLUGIN_ID . '_hide_tracking_notice');
if ($hide_notice) { return false; }
if ($this->settings->getOption('allow_usage_tracking')) { return false; }
if (! Menu::userCanManageAssets()) { return false; }
if (false !== stripos(network_site_url('/'), 'dev') || false !== stripos(network_site_url('/'), 'localhost') || false !== strpos(network_site_url('/'), ':8888') // This is common with MAMP on OS X ) { update_option(WPACU_PLUGIN_ID . '_tracking_notice', '1'); return false; }
return true; }
/** * */ public function ajaxCloseTrackingNoticeCallback() { check_ajax_referer('wpacu_plugin_tracking_nonce', 'wpacu_security');
$action = isset($_POST['action']) ? $_POST['action'] : false;
if ($action !== WPACU_PLUGIN_ID . '_close_tracking_notice' || ! $action) { exit('Invalid Action'); }
$wpacuAction = isset($_POST['wpacu_action']) ? $_POST['wpacu_action'] : false;
if (! $wpacuAction) { exit('Invalid Asset CleanUp Action'); }
// Allow to Disallow (depending on the action chosen) $response = $this->opt_in_out(true); echo $response;
exit(); }
/** * */ public function notice_styles() { ?> <style type="text/css"> .wpacu-tracking-notice { border-left-color: #008f9c; }
.wpacu-tracking-notice .wpacu-action-links { margin: 0 0 8px; }
.wpacu-tracking-notice .wpacu-action-links ul { list-style: none; margin: 0; }
.wpacu-tracking-notice .wpacu-action-links ul li.wpacu-optin { float: left; margin-right: 10px; }
.wpacu-tracking-notice .wpacu-action-links ul li.wpacu-optout { float: left; margin-right: 5px; }
.wpacu-tracking-notice .wpacu-action-links ul li.wpacu-more-info { float: left; margin-top: 5px; margin-left: 5px; }
#wpacu-tracked-data-list { margin: 14px 0; }
#wpacu-tracked-data-list .table-striped { border: none; border-spacing: 0; }
#wpacu-tracked-data-list .wpacu_table_wrap .table.table-striped th, #wpacu-tracked-data-list .wpacu_table_wrap .table.table-striped td { padding: 0.62rem; vertical-align: top; border-top: 1px solid #eceeef; }
#wpacu-tracked-data-list .table-striped tbody tr:nth-of-type(even) { background-color: rgba(0, 143, 156, 0.05); }
#wpacu-tracked-data-list .table-striped tbody tr td:first-child { font-weight: bold; } </style> <?php }
/** * Display the admin notice to users that have not opted-in or out * * @return void */ public function admin_notice() { if (! $this->show_tracking_notice()) { return; }
$this->setup_data();
$optin_url = add_query_arg(array('wpacu_action' => 'wpacu_opt_into_tracking', 'wpacu_is_page_reload' => true)); $optout_url = add_query_arg(array('wpacu_action' => 'wpacu_opt_out_of_tracking', 'wpacu_is_page_reload' => true));
?> <div class="wpacu-tracking-notice notice is-dismissible"> <p><?php echo __('Allow Asset CleanUp to anonymously track plugin usage in order to help us make the plugin better? No sensitive or personal data is collected.', 'wp-asset-clean-up'); ?></p> <div class="wpacu-action-links"> <ul> <li class="wpacu-optin"> <a href="<?php echo esc_url($optin_url); ?>" data-wpacu-close-action="wpacu_opt_into_tracking" class="wpacu-close-tracking-notice button-primary"><?php echo __('Allow, I\'m happy to help', 'easy-digital-downloads'); ?></a> </li> <li class="wpacu-optout"> <a href="<?php echo esc_url($optout_url); ?>" data-wpacu-close-action="wpacu_opt_out_of_tracking" class="wpacu-close-tracking-notice button-secondary"><?php echo __('No, do not allow', 'easy-digital-downloads'); ?></a></li> <li class="wpacu-more-info"><span style="color: #004567;" class="dashicons dashicons-info"></span> <a id="wpacu-show-tracked-data" href="#">What kind of data will be sent for the tracking?</a></li> </ul> <div style="clear: both;"></div> </div>
<div style="display: none;" id="wpacu-tracked-data-list"> <?php self::showSentInfoDataTable($this->data); ?> </div> </div> <?php if (! defined('WPACU_ADMIN_TRACKING_NOTICE_SHOWN')) { define('WPACU_ADMIN_TRACKING_NOTICE_SHOWN', true); }
// Only mark it as shown after it was printed $this->showTrackingNotice = true; }
/** * */ public function notice_scripts() { if (! $this->showTrackingNotice) { return; } ?> <script type="text/javascript"> jQuery(document).ready(function($) { var $wpacuTrackedDataList = $('#wpacu-tracked-data-list');
// Tracking Info Link Clicked $('#wpacu-show-tracked-data').on('click', function() { if ($wpacuTrackedDataList.is(':hidden')) { $wpacuTrackedDataList.slideDown('fast'); } else { $wpacuTrackedDataList.slideUp('fast'); } });
// 'x' click from the top right of the notice $(document).on('click', '.wpacu-tracking-notice .notice-dismiss', function(event) { $('[data-wpacu-close-action="wpacu_opt_out_of_tracking"]').trigger('click'); });
// button click $('.wpacu-close-tracking-notice').on('click', function(e) { e.preventDefault();
$('.wpacu-tracking-notice').fadeOut('fast');
var wpacuXhr = new XMLHttpRequest(), wpacuCloseAction = $(this).attr('data-wpacu-close-action'), wpacuSecurityNonce = '<?php echo wp_create_nonce('wpacu_plugin_tracking_nonce'); ?>';
wpacuXhr.open('POST', '<?php echo admin_url('admin-ajax.php'); ?>'); wpacuXhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); wpacuXhr.onload = function () { if (wpacuXhr.status === 200) { } else if (wpacuXhr.status !== 200) { } };
wpacuXhr.send(encodeURI('action=<?php echo WPACU_PLUGIN_ID . '_close_tracking_notice'; ?>&wpacu_action=' + wpacuCloseAction + '&wpacu_security='+ wpacuSecurityNonce)); }); }); </script> <?php }
/** * @param $data */ public static function showSentInfoDataTable($data) { ?> <div class="wpacu_table_wrap"> <table class="table table-striped"> <tr> <td style="width: 182px;">PHP Version:</td> <td><?php echo $data['php_version']; ?></td> </tr> <tr> <td>Asset CleanUp Info:</td> <td>Version: <?php echo $data['wpacu_version']; ?>, Settings & Usage Information</td> </tr> <tr> <td>WordPress Version:</td> <td><?php echo $data['wp_version']; ?></td> </tr> <tr> <td>Server:</td> <td><?php echo $data['server']; ?></td> </tr> <tr> <td>Multisite:</td> <td><?php echo $data['multisite']; ?></td> </tr> <tr> <td>Theme:</td> <td><?php echo $data['theme']; ?></td> </tr> <tr> <td>Locale:</td> <td><?php echo $data['locale']; ?></td> </tr> <tr> <td>Plugins:</td> <td>The list of active & inactive plugins</td> </tr> </table> </div> <?php } }
|