diff --git a/includes/admin/class-wzfp-settings.php b/includes/admin/class-wzfp-settings.php index 53197ce..40318af 100644 --- a/includes/admin/class-wzfp-settings.php +++ b/includes/admin/class-wzfp-settings.php @@ -248,6 +248,28 @@ public static function get_settings_general() { 'other_archives' => esc_html__( 'Other archives', 'where-did-they-go-from-here' ), ), ), + 'track_users' => array( + 'id' => 'track_users', + 'name' => esc_html__( 'Track user groups', 'where-did-they-go-from-here' ) . ':', + 'desc' => esc_html__( 'Uncheck above to disable tracking if the current user falls into any one of these groups.', 'where-did-they-go-from-here' ), + 'type' => 'multicheck', + 'default' => array( + 'editors' => 'editors', + 'admins' => 'admins', + ), + 'options' => array( + 'authors' => esc_html__( 'Authors', 'where-did-they-go-from-here' ), + 'editors' => esc_html__( 'Editors', 'where-did-they-go-from-here' ), + 'admins' => esc_html__( 'Admins', 'where-did-they-go-from-here' ), + ), + ), + 'logged_in' => array( + 'id' => 'logged_in', + 'name' => esc_html__( 'Track logged-in users', 'where-did-they-go-from-here' ), + 'desc' => esc_html__( 'Uncheck to stop tracking logged in users. Only logged out visitors will be tracked if this is disabled. Unchecking this will override the above setting.', 'where-did-they-go-from-here' ), + 'type' => 'checkbox', + 'options' => true, + ), 'wg_in_admin' => array( 'id' => 'wg_in_admin', 'name' => esc_html__( 'Add admin column', 'where-did-they-go-from-here' ), diff --git a/includes/tracker.php b/includes/tracker.php index f7d8c08..aa41732 100644 --- a/includes/tracker.php +++ b/includes/tracker.php @@ -96,23 +96,52 @@ function wherego_tracker_parser() { function wherego_enqueue_scripts() { global $post; - if ( is_singular() ) { - - wp_enqueue_script( 'wherego_tracker', plugins_url( 'includes/js/wherego_tracker.min.js', WHEREGO_PLUGIN_FILE ), array( 'jquery' ), '1.0', true ); - - wp_localize_script( - 'wherego_tracker', - 'ajax_wherego_tracker', - array( - 'ajax_url' => admin_url( 'admin-ajax.php' ), - 'wherego_nonce' => wp_create_nonce( 'wherego-tracker-nonce' ), - 'wherego_id' => $post->ID, - 'wherego_sitevar' => wherego_get_referer(), - 'wherego_rnd' => wp_rand( 1, time() ), - ) - ); - } + $track_users = wherego_get_option( 'track_users' ); + + if ( is_singular() && ( 'draft' !== $post->post_status ) && ! is_customize_preview() ) { + + $current_user = wp_get_current_user(); // Let's get the current user. + $post_author = ( (int) $current_user->ID === (int) $post->post_author ) ? true : false; // Is the current user the post author? + $current_user_admin = ( current_user_can( 'manage_options' ) ) ? true : false; // Is the current user an admin? + $current_user_editor = ( ( current_user_can( 'edit_others_posts' ) ) && ( ! current_user_can( 'manage_options' ) ) ) ? true : false; // Is the current user an editor? + + $include_code = true; + if ( ( $post_author ) && ( empty( $track_users['authors'] ) ) ) { + $include_code = false; + } + if ( ( $current_user_admin ) && ( empty( $track_users['admins'] ) ) ) { + $include_code = false; + } + if ( ( $current_user_editor ) && ( empty( $track_users['editors'] ) ) ) { + $include_code = false; + } + if ( ( $current_user->exists() ) && ( ! wherego_get_option( 'logged_in' ) ) ) { + $include_code = false; + } + if ( $include_code ) { + + wp_enqueue_script( + 'wherego_tracker', + plugins_url( 'includes/js/wherego_tracker.min.js', WHEREGO_PLUGIN_FILE ), + array( 'jquery' ), + '1.0', + true + ); + + wp_localize_script( + 'wherego_tracker', + 'ajax_wherego_tracker', + array( + 'ajax_url' => admin_url( 'admin-ajax.php' ), + 'wherego_nonce' => wp_create_nonce( 'wherego-tracker-nonce' ), + 'wherego_id' => $post->ID, + 'wherego_sitevar' => wherego_get_referer(), + 'wherego_rnd' => wp_rand( 1, time() ), + ) + ); + } + } } add_action( 'wp_enqueue_scripts', 'wherego_enqueue_scripts' ); diff --git a/readme.txt b/readme.txt index 04dabeb..7575795 100644 --- a/readme.txt +++ b/readme.txt @@ -104,6 +104,7 @@ You can also use this function to display posts on any type of page generated by * Features: * Support for PolyLang and WPML + * New options to stop tracking logged in users, authors, editors or admins * Enhancements: * Improved caching with inbuilt expiry. Use WZP_CACHE_TIME in your wp-config.php to set how long the cache should be set for. Default is one week. Setting it to `false` will disable expiry