| 1 |
<?php |
| 2 |
/* |
| 3 |
* Google Maps Widget |
| 4 |
* Plugin usage tracking |
| 5 |
* (c) Web factory Ltd, 2012 - 2015 |
| 6 |
*/ |
| 7 |
|
| 8 |
|
| 9 |
// include only file |
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
die(); |
| 12 |
} |
| 13 |
|
| 14 |
|
| 15 |
class GMW_tracking { |
| 16 |
// set things up |
| 17 |
static function init() { |
| 18 |
$options = get_option(GMW_OPTIONS); |
| 19 |
|
| 20 |
self::check_opt_in_out(); |
| 21 |
|
| 22 |
// ask user if he wants to allow tracking |
| 23 |
if (is_admin() && !isset($options['allow_tracking'])) { |
| 24 |
add_action('admin_notices', array(__CLASS__, 'tracking_notice')); |
| 25 |
} |
| 26 |
|
| 27 |
add_action(GMW_CRON, array(__CLASS__, 'send_data')); |
| 28 |
// todo - write this properly, so it doesn't run each time, $force ... |
| 29 |
GMW_tracking::setup_cron(); |
| 30 |
} // init |
| 31 |
|
| 32 |
|
| 33 |
// register additional cron interval |
| 34 |
static function register_cron_intervals($schedules) { |
| 35 |
$schedules['gmw_weekly'] = array( |
| 36 |
'interval' => DAY_IN_SECONDS * 7, |
| 37 |
'display' => 'Once a Week'); |
| 38 |
$schedules['gmw_biweekly'] = array( |
| 39 |
'interval' => DAY_IN_SECONDS * 14, |
| 40 |
'display' => 'Once every two Weeks'); |
| 41 |
|
| 42 |
return $schedules; |
| 43 |
} // cron_intervals |
| 44 |
|
| 45 |
|
| 46 |
// clear cron scheadule |
| 47 |
static function clear_cron() { |
| 48 |
wp_clear_scheduled_hook(GMW_CRON); |
| 49 |
} // clear_cron |
| 50 |
|
| 51 |
|
| 52 |
// setup cron job when user allows tracking |
| 53 |
static function setup_cron() { |
| 54 |
$options = get_option(GMW_OPTIONS); |
| 55 |
|
| 56 |
if (isset($options['allow_tracking']) && $options['allow_tracking'] === true) { |
| 57 |
if (!wp_next_scheduled(GMW_CRON)) { |
| 58 |
wp_schedule_event(time() + 300, 'gmw_biweekly', GMW_CRON); |
| 59 |
} |
| 60 |
} else { |
| 61 |
self::clear_cron(); |
| 62 |
} |
| 63 |
} // setup_cron |
| 64 |
|
| 65 |
|
| 66 |
// save user's choice for (not) allowing tracking |
| 67 |
static function check_opt_in_out() { |
| 68 |
$options = get_option(GMW_OPTIONS); |
| 69 |
|
| 70 |
if (isset($_GET['gmw_tracking']) && $_GET['gmw_tracking'] == 'opt_in') { |
| 71 |
$options['allow_tracking'] = true; |
| 72 |
update_option(GMW_OPTIONS, $options); |
| 73 |
self::send_data(true); |
| 74 |
wp_redirect(esc_url_raw(remove_query_arg('gmw_tracking'))); |
| 75 |
die(); |
| 76 |
} else if (isset($_GET['gmw_tracking']) && $_GET['gmw_tracking'] == 'opt_out') { |
| 77 |
$options['allow_tracking'] = false; |
| 78 |
update_option(GMW_OPTIONS, $options); |
| 79 |
wp_redirect(esc_url_raw(remove_query_arg('gmw_tracking'))); |
| 80 |
die(); |
| 81 |
} |
| 82 |
} // check_opt_in_out |
| 83 |
|
| 84 |
|
| 85 |
// display tracking notice |
| 86 |
static function tracking_notice() { |
| 87 |
$optin_url = add_query_arg('gmw_tracking', 'opt_in'); |
| 88 |
$optout_url = add_query_arg('gmw_tracking', 'opt_out'); |
| 89 |
|
| 90 |
echo '<div class="updated"><p>'; |
| 91 |
echo __('Please help us improve <strong>Google Maps Widget</strong> by allowing us to track anonymous usage data. Absolutely <strong>no sensitive data is tracked</strong> (<a href="http://www.googlemapswidget.com/plugin-tracking-info/" target="_blank">complete disclosure & details of our tracking policy</a>).', 'google-maps-widget'); |
| 92 |
echo '<br /><a href="' . esc_url($optin_url) . '" style="vertical-align: baseline; margin-top: 15px;" class="button-primary">' . __('Allow', 'google-maps-widget') . '</a>'; |
| 93 |
echo ' <a href="' . esc_url($optout_url) . '" class="">' . __('Do not allow tracking', 'google-maps-widget') . '</a>'; |
| 94 |
echo '</p></div>'; |
| 95 |
} // tracking_notice |
| 96 |
|
| 97 |
|
| 98 |
// send usage data once a week to our server |
| 99 |
static function send_data($force = false) { |
| 100 |
$options = get_option(GMW_OPTIONS); |
| 101 |
|
| 102 |
if ($force == false && (!isset($options['allow_tracking']) || $options['allow_tracking'] !== true)) { |
| 103 |
return; |
| 104 |
} |
| 105 |
if ($force == false && ($options['last_tracking'] && $options['last_tracking'] > strtotime( '-6 days'))) { |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
$data = self::prepare_data(); |
| 110 |
$request = wp_remote_post('http://www.googlemapswidget.com/tracking.php', array( |
| 111 |
'method' => 'POST', |
| 112 |
'timeout' => 10, |
| 113 |
'redirection' => 3, |
| 114 |
'httpversion' => '1.0', |
| 115 |
'body' => $data, |
| 116 |
'user-agent' => 'GMW/' . GMW::$version)); |
| 117 |
|
| 118 |
$options['last_tracking'] = current_time('timestamp'); |
| 119 |
update_option(GMW_OPTIONS, $options); |
| 120 |
} // send_data |
| 121 |
|
| 122 |
|
| 123 |
// get and prepare data that will be sent out |
| 124 |
static function prepare_data() { |
| 125 |
$options = get_option(GMW_OPTIONS); |
| 126 |
$data = array(); |
| 127 |
$current_user = wp_get_current_user(); |
| 128 |
|
| 129 |
$data['url'] = home_url(); |
| 130 |
if ($current_user && isset($current_user->user_email) && !empty($current_user->user_email)) { |
| 131 |
$data['admin_email'] = $current_user->user_email; |
| 132 |
} else { |
| 133 |
$data['admin_email'] = get_bloginfo('admin_email'); |
| 134 |
} |
| 135 |
$data['wp_version'] = get_bloginfo('version'); |
| 136 |
$data['gmw_version'] = GMW::$version; |
| 137 |
$data['gmw_first_version'] = $options['first_version']; |
| 138 |
$data['gmw_first_install'] = $options['first_install']; |
| 139 |
$data['gmw_activated'] = GMW::is_activated(); |
| 140 |
$data['ioncube'] = extension_loaded('IonCube Loader'); |
| 141 |
$data['gmw_count'] = self::count_active_widgets(); |
| 142 |
|
| 143 |
if (get_bloginfo('version') < '3.4') { |
| 144 |
$theme = get_theme_data(get_stylesheet_directory() . '/style.css'); |
| 145 |
$data['theme_name'] = $theme['Name']; |
| 146 |
$data['theme_version'] = $theme['Version']; |
| 147 |
} else { |
| 148 |
$theme = wp_get_theme(); |
| 149 |
$data['theme_name'] = $theme->Name; |
| 150 |
$data['theme_version'] = $theme->Version; |
| 151 |
} |
| 152 |
|
| 153 |
// get current plugin information |
| 154 |
if (!function_exists('get_plugins')) { |
| 155 |
include ABSPATH . '/wp-admin/includes/plugin.php'; |
| 156 |
} |
| 157 |
|
| 158 |
$plugins = get_plugins(); |
| 159 |
$active_plugins = get_option('active_plugins', array()); |
| 160 |
|
| 161 |
foreach ($active_plugins as $plugin) { |
| 162 |
$data['plugins'][$plugin] = @$plugins[$plugin]; |
| 163 |
} |
| 164 |
|
| 165 |
return $data; |
| 166 |
} // prepare_data |
| 167 |
|
| 168 |
|
| 169 |
// counts the number of active GMW widgets in sidebars |
| 170 |
static function count_active_widgets() { |
| 171 |
$count = 0; |
| 172 |
|
| 173 |
$sidebars = get_option('sidebars_widgets', array()); |
| 174 |
foreach ($sidebars as $sidebar_name => $widgets) { |
| 175 |
if (strpos($sidebar_name, 'inactive') !== false || strpos($sidebar_name, 'orphaned') !== false) { |
| 176 |
continue; |
| 177 |
} |
| 178 |
if (is_array($widgets)) { |
| 179 |
foreach ($widgets as $widget_name) { |
| 180 |
if (strpos($widget_name, 'googlemapswidget') !== false) { |
| 181 |
$count++; |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
} // foreach sidebar |
| 186 |
|
| 187 |
return $count; |
| 188 |
} // count_active_widgets |
| 189 |
} // class GMW_tracking |