From b1c9c670eb9893ab0f77ac0d08cd41e7585f9edc Mon Sep 17 00:00:00 2001 From: ultimatemember Date: Sat, 3 Jan 2015 23:18:27 +0200 Subject: [PATCH] Cron jobs and tracking --- admin/core/um-admin-actions.php | 35 +++++++ admin/core/um-admin-tracking.php | 151 +++++++++++++++++++++++++++++++ admin/um-admin-init.php | 2 + core/um-cron.php | 38 ++++++++ core/um-taxonomies.php | 2 +- um-config.php | 10 ++ um-init.php | 3 + 7 files changed, 240 insertions(+), 1 deletion(-) create mode 100644 admin/core/um-admin-tracking.php create mode 100644 core/um-cron.php diff --git a/admin/core/um-admin-actions.php b/admin/core/um-admin-actions.php index a8ee3386..f5e5f0c9 100644 --- a/admin/core/um-admin-actions.php +++ b/admin/core/um-admin-actions.php @@ -1,5 +1,40 @@ ReduxFramework->set('allow_tracking', 1); + + update_option('um_tracking_notice', 1 ); + + $tracking = new UM_Admin_Tracking(); + $tracking->send_checkin( true ); + + exit( wp_redirect( remove_query_arg('um_adm_action') ) ); + } + + /*** + *** @Opt-out of tracking + ***/ + add_action('um_admin_do_action__opt_out_of_tracking', 'um_admin_do_action__opt_out_of_tracking'); + function um_admin_do_action__opt_out_of_tracking( $action ){ + global $ultimatemember; + if ( !is_admin() || !current_user_can('manage_options') ) die(); + + global $reduxConfig; + $reduxConfig->ReduxFramework->set('allow_tracking', 0); + + update_option('um_tracking_notice', 1 ); + + exit( wp_redirect( remove_query_arg('um_adm_action') ) ); + } + /*** *** @Un-install UM completely ***/ diff --git a/admin/core/um-admin-tracking.php b/admin/core/um-admin-tracking.php new file mode 100644 index 00000000..94de1efe --- /dev/null +++ b/admin/core/um-admin-tracking.php @@ -0,0 +1,151 @@ +schedule_send(); + + add_action( 'admin_notices', array( $this, 'admin_notices' ) ); + + } + + /*** + *** @setup info array + ***/ + private function setup_data() { + + $data = array(); + + // Retrieve current theme info + if ( get_bloginfo( 'version' ) < '3.4' ) { + $theme_data = get_theme_data( get_stylesheet_directory() . '/style.css' ); + $theme = $theme_data['Name']; + $theme_ver = $theme_data['Version']; + } else { + $theme_data = wp_get_theme(); + $theme = $theme_data->Name; + $theme_ver = $theme_data->Version; + } + + $data['name'] = get_bloginfo('name'); + $data['url'] = home_url(); + $data['theme'] = $theme; + $data['theme_version'] = $theme_ver; + $data['wp_version'] = get_bloginfo( 'version' ); + $data['version'] = ULTIMATEMEMBER_VERSION; + $data['email'] = get_bloginfo( 'admin_email' ); + + $result = count_users(); + $data['users_count'] = $result['total_users']; + + // Retrieve current plugin information + if( ! function_exists( 'get_plugins' ) ) { + include ABSPATH . '/wp-admin/includes/plugin.php'; + } + + $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; + + $this->data = $data; + + } + + /*** + *** @check if tracking is allowed + ***/ + private function tracking_allowed() { + if ( !um_get_option('allow_tracking') ) + return 0; + + if( stristr( network_site_url( '/' ), 'dev' ) !== false || + stristr( network_site_url( '/' ), 'localhost' ) !== false || + stristr( network_site_url( '/' ), ':8888' ) !== false // This is common with MAMP on OS X + ) { + return 0; + } + + return 1; + } + + /*** + *** @get last send time + ***/ + private function get_last_send() { + return get_option( 'um_tracking_last_send' ); + } + + /*** + *** @send a report + ***/ + public function send_checkin( $override = false ) { + + 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; + + $this->setup_data(); + + $request = wp_remote_post( 'http://ultimatemember.com/?um_action=checkin', array( + 'method' => 'POST', + 'timeout' => 20, + 'redirection' => 5, + 'httpversion' => '1.0', + 'blocking' => true, + 'body' => $this->data, + 'user-agent' => 'UM/' . ULTIMATEMEMBER_VERSION . '; ' . get_bloginfo( 'url' ), + ) ); + + update_option( 'um_tracking_last_send', time() ); + + } + + /*** + *** @run a scheduled report + ***/ + private function schedule_send() { + add_action( 'um_weekly_scheduled_events', array( $this, 'send_checkin' ) ); + } + + /*** + *** @show admin notices + ***/ + public function admin_notices() { + + if( ! current_user_can( 'manage_options' ) ) + return; + + $hide_notice = get_option('um_tracking_notice'); + + if ( $hide_notice ) + return; + + $optin_url = add_query_arg( 'um_adm_action', 'opt_into_tracking' ); + $optout_url = add_query_arg( 'um_adm_action', 'opt_out_of_tracking' ); + + echo '

'; + echo __( 'Help us improve Ultimate Member by allowing us to anonymously track plugin and theme usage. This helps us to improve plugin and theme compatibility. No sensitive data will be tracked.', 'ultimatemember' ); + echo '
'; + echo '' . __( 'Allow tracking', 'ultimatemember' ) . ''; + echo ' ' . __( 'Do not allow tracking', 'ultimatemember' ) . ''; + echo '

'; + + } + +} \ No newline at end of file diff --git a/admin/um-admin-init.php b/admin/um-admin-init.php index 3e322d2a..73647d8b 100644 --- a/admin/um-admin-init.php +++ b/admin/um-admin-init.php @@ -34,6 +34,7 @@ class UM_Admin_API { require_once um_path . 'admin/core/um-admin-roles.php'; require_once um_path . 'admin/core/um-admin-builder.php'; require_once um_path . 'admin/core/um-admin-dragdrop.php'; + require_once um_path . 'admin/core/um-admin-tracking.php'; require_once um_path . 'admin/core/um-admin-actions-user.php'; require_once um_path . 'admin/core/um-admin-actions-modal.php'; @@ -54,6 +55,7 @@ class UM_Admin_API { $this->access = new UM_Admin_Access(); $this->builder = new UM_Admin_Builder(); $this->dragdrop = new UM_Admin_DragDrop(); + $this->tracking = new UM_Admin_Tracking(); if ( is_admin() && current_user_can('manage_options') && diff --git a/core/um-cron.php b/core/um-cron.php new file mode 100644 index 00000000..c5649a92 --- /dev/null +++ b/core/um-cron.php @@ -0,0 +1,38 @@ + 604800, + 'display' => __( 'Once Weekly', 'ultimatemember' ) + ); + + return $schedules; + } + + public function schedule_Events() { + $this->weekly_events(); + $this->daily_events(); + } + + private function weekly_events() { + if ( ! wp_next_scheduled( 'um_weekly_scheduled_events' ) ) { + wp_schedule_event( current_time( 'timestamp' ), 'weekly', 'um_weekly_scheduled_events' ); + } + } + + private function daily_events() { + if ( ! wp_next_scheduled( 'um_daily_scheduled_events' ) ) { + wp_schedule_event( current_time( 'timestamp' ), 'daily', 'um_daily_scheduled_events' ); + } + } + +} \ No newline at end of file diff --git a/core/um-taxonomies.php b/core/um-taxonomies.php index c51221ff..1d5342dd 100644 --- a/core/um-taxonomies.php +++ b/core/um-taxonomies.php @@ -11,7 +11,7 @@ class UM_Taxonomies { /*** *** @Create taxonomies for use for UM ***/ - function create_taxonomies( $user_id = null ) { + function create_taxonomies() { register_post_type( 'um_form', array( 'labels' => array( diff --git a/um-config.php b/um-config.php index b6a12994..491afcdf 100644 --- a/um-config.php +++ b/um-config.php @@ -1433,6 +1433,16 @@ $this->sections[] = array( 'title' => __( 'Advanced'), 'fields' => array( + array( + 'id' => 'allow_tracking', + 'type' => 'switch', + 'title' => __( 'Allow anonymous tracking' ), + 'default' => 0, + 'desc' => 'Help us improve Ultimate Member by allowing us to anonymously track plugin and theme usage. This helps us to improve plugin and theme compatibility. No sensitive data will be tracked.', + 'on' => 'Allow tracking', + 'off' => 'Do not allow', + ), + array( 'id' => 'admin_load_time', 'type' => 'switch', diff --git a/um-init.php b/um-init.php index 6e707f44..aaec8ef8 100644 --- a/um-init.php +++ b/um-init.php @@ -52,6 +52,7 @@ class UM_API { require_once um_path . 'core/um-members.php'; require_once um_path . 'core/um-logout.php'; require_once um_path . 'core/um-modal.php'; + require_once um_path . 'core/um-cron.php'; require_once um_path . 'core/lib/mobiledetect/Mobile_Detect.php'; @@ -113,6 +114,8 @@ class UM_API { $this->members = new UM_Members(); $this->logout = new UM_Logout(); $this->modal = new UM_Modal(); + $this->cron = new UM_Cron(); + $this->mobile = new Mobile_Detect; $this->options = get_option('um_options');