Skip to content

Commit

Permalink
New: Track users setting
Browse files Browse the repository at this point in the history
Fixes #4
  • Loading branch information
ajaydsouza committed Mar 4, 2022
1 parent f8ec1c9 commit e248575
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 16 deletions.
22 changes: 22 additions & 0 deletions includes/admin/class-wzfp-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
Expand Down
61 changes: 45 additions & 16 deletions includes/tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e248575

Please sign in to comment.