client = $client;
}
}
/**
* Don't show the notice
*
* @return \self
*/
public function hide_notice()
{
$this->show_notice = false;
return $this;
}
/**
* Add extra data if needed
*
* @param array $data
*
* @return \self
*/
public function add_extra($data = array())
{
$this->extra_data = $data;
return $this;
}
/**
* Set custom notice text
*
* @param string $text
*
* @return \self
*/
public function notice($text)
{
$this->notice = $text;
return $this;
}
/**
* Initialize insights
*
* @return void
*/
public function init()
{
if ($this->client->type == 'plugin') {
$this->init_plugin();
}
}
/**
* Initialize plugin hooks
*
* @return void
*/
public function init_plugin()
{
// plugin deactivate popup
if (! $this->is_local_server()) {
add_filter('plugin_action_links_' . $this->client->basename, array($this, 'plugin_action_links'));
add_action('admin_footer', array($this, 'deactivate_scripts'));
}
$this->init_common();
register_activation_hook($this->client->file, array($this, 'activate_plugin'));
register_deactivation_hook($this->client->file, array($this, 'deactivation_cleanup'));
}
/**
* Initialize common hooks
*
* @return void
*/
protected function init_common()
{
if ($this->show_notice) {
// tracking notice
add_action('admin_notices', array($this, 'admin_notice'));
}
add_action('admin_init', array($this, 'handle_optin_optout'));
// uninstall reason
add_action('wp_ajax_' . wp_kses_post($this->client->slug) . '_submit-uninstall-reason', array($this, 'uninstall_reason_submission'));
// cron events
add_filter('cron_schedules', array($this, 'add_weekly_schedule'));
add_action($this->client->slug . '_tracker_send_event', array($this, 'send_tracking_data'));
// add_action( 'admin_init', array( $this, 'send_tracking_data' ) ); // test
}
/**
* Send tracking data to AppSero server
*
* @param boolean $override
*
* @return void
*/
public function send_tracking_data($override = false)
{
// skip on AJAX Requests
if (defined('DOING_AJAX') && DOING_AJAX) {
return;
}
if (! $this->tracking_allowed() && ! $override) {
return;
}
// Send a maximum of once per week
$last_send = $this->get_last_send();
if ($last_send && $last_send > strtotime('-1 week')) {
return;
}
$tracking_data = $this->get_tracking_data();
$response = $this->client->send_request($tracking_data, 'track');
update_option($this->client->slug . '_tracking_last_send', time());
}
/**
* Get the tracking data points
*
* @return array
*/
protected function get_tracking_data()
{
$all_plugins = $this->get_all_plugins();
$users = get_users(array(
'role' => 'administrator',
'orderby' => 'ID',
'order' => 'ASC',
'number' => 1,
'paged' => 1,
));
$admin_user = (is_array($users) && ! empty($users)) ? $users[0] : false;
$first_name = $last_name = '';
if ($admin_user) {
$first_name = $admin_user->first_name ? $admin_user->first_name : $admin_user->display_name;
$last_name = $admin_user->last_name;
}
$data = array(
'url' => esc_url(home_url()),
'site' => $this->get_site_name(),
'admin_email' => get_option('admin_email'),
'first_name' => $first_name,
'last_name' => $last_name,
'hash' => $this->client->hash,
'server' => $this->get_server_info(),
'wp' => $this->get_wp_info(),
'users' => $this->get_user_counts(),
'active_plugins' => count($all_plugins['active_plugins']),
'inactive_plugins' => count($all_plugins['inactive_plugins']),
'ip_address' => $this->get_user_ip_address(),
'project_version' => $this->client->project_version,
'tracking_skipped' => false,
);
// Add metadata
if ($extra = $this->get_extra_data()) {
$data['extra'] = $extra;
}
// Check this has previously skipped tracking
$skipped = get_option($this->client->slug . '_tracking_skipped');
if ($skipped === 'yes') {
delete_option($this->client->slug . '_tracking_skipped');
$data['tracking_skipped'] = true;
}
return apply_filters($this->client->slug . '_tracker_data', $data);
}
/**
* If a child class wants to send extra data
*
* @return mixed
*/
protected function get_extra_data()
{
if (is_callable($this->extra_data)) {
return call_user_func($this->extra_data);
}
if (is_array($this->extra_data)) {
return $this->extra_data;
}
return array();
}
/**
* Explain the user which data we collect
*
* @return array
*/
protected function data_we_collect()
{
$data = array(
'Server environment details (php, mysql, server, WordPress versions)',
'Number of users in your site',
'Site language',
'Number of active and inactive plugins',
'Site name and url',
'Your name and email address',
);
return $data;
}
/**
* Check if the user has opted into tracking
*
* @return bool
*/
public function tracking_allowed()
{
$allow_tracking = get_option($this->client->slug . '_allow_tracking', 'no');
return $allow_tracking == 'yes';
}
/**
* Get the last time a tracking was sent
*
* @return false|string
*/
private function get_last_send()
{
return get_option($this->client->slug . '_tracking_last_send', false);
}
/**
* Check if the notice has been dismissed or enabled
*
* @return boolean
*/
public function notice_dismissed()
{
$hide_notice = get_option($this->client->slug . '_tracking_notice', null);
if ('hide' == $hide_notice) {
return true;
}
return false;
}
/**
* Check if the current server is localhost
*
* @return boolean
*/
private function is_local_server()
{
return false;
$is_local = in_array($_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1'));
return apply_filters('appsero_is_local', $is_local);
}
/**
* Schedule the event weekly
*
* @return void
*/
private function schedule_event()
{
$hook_name = $this->client->slug . '_tracker_send_event';
if (! wp_next_scheduled($hook_name)) {
wp_schedule_event(time(), 'weekly', $hook_name);
}
}
/**
* Clear any scheduled hook
*
* @return void
*/
private function clear_schedule_event()
{
wp_clear_scheduled_hook($this->client->slug . '_tracker_send_event');
}
/**
* Display the admin notice to users that have not opted-in or out
*
* @return void
*/
public function admin_notice()
{
if ($this->notice_dismissed()) {
return;
}
if ($this->tracking_allowed()) {
return;
}
if (! current_user_can('manage_options')) {
return;
}
// don't show tracking if a local server
if ($this->is_local_server()) {
return;
}
$optin_url = wp_nonce_url( add_query_arg( $this->client->slug . '_tracker_optin', 'true' ), '_wpnonce' );
$optout_url = wp_nonce_url( add_query_arg( $this->client->slug . '_tracker_optout', 'true' ), '_wpnonce' );
if (empty($this->notice)) {
$notice = sprintf($this->client->darkify_trans('Want to help make %1$s even more awesome? Allow %1$s to collect non-sensitive diagnostic data and usage information.'), $this->client->name);
} else {
$notice = $this->notice;
}
$policy_url = 'https://' . 'appsero.com/privacy-policy/';
$notice .= ' (' . $this->client->darkify_trans('what we collect') . ')';
$notice .= '
' . implode(', ', $this->data_we_collect()) . '. No sensitive data is tracked. ';
$notice .= 'We are using Appsero to collect your data. Learn more about how Appsero collects and handle your data.