settings = new Settings(); } /** * */ public function init() { // Schedule add_action('wp', array($this, 'scheduleEvents' )); add_action('init', array($this, 'scheduleSend' )); // 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, 'optInOut' )); } // 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, 'checkForSettingsOptIn' )); // Notice on the top screen within the Dashboard to get permission from the user to allow tracking add_action('admin_notices', array($this, 'adminNotice')); add_action('admin_head', array($this, 'noticeStyles' )); add_action('admin_footer', array($this, 'noticeScripts' )); // 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 optInOut($isAjaxCall = false) { if ( ! isset($_REQUEST['wpacu_action']) ) { 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->checkForOptIn(); } if ($wpacuAction === 'wpacu_opt_out_of_tracking') { $response = $this->checkForOptOut(); 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 scheduleEvents() { $this->weeklyEvents(); } /** * Schedule weekly events * * @access private * @since 1.6 * @return void */ private function weeklyEvents() { 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 trackingAllowed() { $allowUsageTracking = $this->settings->getOption('allow_usage_tracking'); return (bool) $allowUsageTracking; } /** * Set up the data that is going to be tracked * * @access private * @return void */ public function setupData() { $data = array(); // Retrieve current theme info $themeData = wp_get_theme(); $theme = $themeData->get('Name') . ' ' . $themeData->get('Version'); $settingsClass = new Settings(); $data['php_version'] = PHP_VERSION; $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 = Misc::getActivePlugins(); 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 $ignoreLastCheckIn If we should ignore when the last check in was. * * @return bool */ public function sendCheckIn($override = false, $ignoreLastCheckIn = 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->trackingAllowed()) { return false; } // Send a maximum of once per week $lastSend = $this->getLastSend(); if ( ! $ignoreLastCheckIn && is_numeric($lastSend) && $lastSend > strtotime('-1 week')) { return 'Not Sent: Only Weekly'; } $this->setupData(); $response = wp_remote_post('https://www.assetcleanup.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 checkForSettingsOptIn($savedSettings) { // Send an initial check in when "Settings" are saved if (isset($savedSettings['allow_usage_tracking']) && $savedSettings['allow_usage_tracking'] == 1) { $this->sendCheckIn( true ); } return $savedSettings; } /** * Check for a new opt-in via the admin notice or after "Settings" is saved * * @return bool|void */ public function checkForOptIn() { if (! Menu::userCanManageAssets()) { return; } // Update the value in the "Settings" area $this->settings->updateOption('allow_usage_tracking', 1); // Send the tracking data $response = $this->sendCheckIn(true); // Mark the notice to be hidden Misc::addUpdateOption(WPACU_PLUGIN_ID . '_hide_tracking_notice', 1); return $response; } /** * @return string */ public function checkForOptOut() { 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 getLastSend() { 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 scheduleSend() { if (Misc::doingCron()) { add_action('wpacu_weekly_scheduled_events', array($this, 'sendCheckIn' )); } } /** * Returns true or false for showing the top tracking notice * * @return bool */ public function showTrackingNotice() { // On URL request (for debugging) if ( isset($_GET['wpacu_show_tracking_notice']) ) { 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; } if ($this->settings->getOption('allow_usage_tracking')) { return false; } if (get_option(WPACU_PLUGIN_ID . '_hide_tracking_notice')) { 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->optInOut(true); echo esc_html($response); exit(); } /** * */ public function noticeStyles() { ?> showTrackingNotice) { return; } ?> showTrackingNotice()) { return; } $this->setupData(); $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)); ?>

showTrackingNotice = true; } /** * @param $data */ public static function showSentInfoDataTable($data) { ?>
PHP Version:
Asset CleanUp Info: Version: , Settings & Usage Information
WordPress Version:
Server:
Multisite:
Theme:
Locale:
Plugins: The list of active & inactive plugins