| 1 |
( function( $ ) { |
| 2 |
|
| 3 |
$( document ).ready(function() { |
| 4 |
// Setup vars |
| 5 |
var user_id = parseInt( gamipress_events.user_id ); |
| 6 |
var post_id = parseInt( gamipress_events.post_id ); |
| 7 |
|
| 8 |
// Trigger track visit function at init |
| 9 |
gamipress_track_visit( user_id, post_id ); |
| 10 |
}); |
| 11 |
|
| 12 |
/** |
| 13 |
* Visit tracking |
| 14 |
* |
| 15 |
* @since 1.5.1 |
| 16 |
* @updated 1.7.9 Added nonce usage |
| 17 |
* |
| 18 |
* @var int user_id |
| 19 |
* @var int post_id |
| 20 |
*/ |
| 21 |
function gamipress_track_visit( user_id, post_id ) { |
| 22 |
// Bail if user is not logged in |
| 23 |
if( user_id === 0 ) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
// Bail if user already visited this post |
| 28 |
if( gamipress_is_post_visited( post_id ) ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
// Add the post ID to the list of posts visited |
| 33 |
gamipress_add_post_visited( post_id ); |
| 34 |
|
| 35 |
$.ajax({ |
| 36 |
url: gamipress_events.ajaxurl, |
| 37 |
type: 'POST', |
| 38 |
data: { |
| 39 |
action: 'gamipress_track_visit', |
| 40 |
nonce: gamipress_events.nonce, |
| 41 |
user_id: user_id, |
| 42 |
post_id: post_id |
| 43 |
}, |
| 44 |
success: function(response) { |
| 45 |
// Debug success response |
| 46 |
if( gamipress_events.debug_mode ) { |
| 47 |
console.log( response ); |
| 48 |
} |
| 49 |
} |
| 50 |
}).fail( function (response) { |
| 51 |
// Debug any server error |
| 52 |
if( gamipress_events.debug_mode ) { |
| 53 |
console.log( response ); |
| 54 |
} |
| 55 |
}); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Check if post has been visited yet |
| 60 |
* |
| 61 |
* @since 1.9.2 |
| 62 |
* |
| 63 |
* @var int post_id |
| 64 |
*/ |
| 65 |
function gamipress_is_post_visited( post_id ) { |
| 66 |
|
| 67 |
var posts_visited = gamipress_get_posts_visited(); |
| 68 |
|
| 69 |
return posts_visited.includes( post_id ); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get the user posts visited (only if browser supports local storage) |
| 75 |
* |
| 76 |
* @since 1.9.2 |
| 77 |
*/ |
| 78 |
function gamipress_get_posts_visited() { |
| 79 |
|
| 80 |
var posts_visited = []; |
| 81 |
|
| 82 |
if( window.localStorage ) { |
| 83 |
var posts_cache_date = window.localStorage.getItem('gamipress_posts_visited_date'); |
| 84 |
|
| 85 |
// Check if cache date and server date matches |
| 86 |
if( posts_cache_date !== gamipress_events.server_date ) { |
| 87 |
// Reset posts visited cache for today |
| 88 |
window.localStorage.removeItem('gamipress_posts_visited'); |
| 89 |
|
| 90 |
// Update cache date |
| 91 |
window.localStorage.setItem( 'gamipress_posts_visited_date', gamipress_events.server_date ); |
| 92 |
} else { |
| 93 |
|
| 94 |
var posts_cached = window.localStorage.getItem('gamipress_posts_visited'); |
| 95 |
|
| 96 |
if( posts_cached ) { |
| 97 |
var posts_array = posts_cached.split(','); |
| 98 |
|
| 99 |
// Turn array items to int |
| 100 |
for( var i = 0; i < posts_array.length; i++ ) { |
| 101 |
|
| 102 |
var post_id = parseInt( posts_array[i] ); |
| 103 |
|
| 104 |
// Prevent to insert duplicated entries |
| 105 |
if( ! isNaN( post_id ) && ! posts_visited.includes( post_id ) ) { |
| 106 |
posts_visited[posts_visited.length] = post_id; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return posts_visited; |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Add the post id given to the visited posts list |
| 120 |
* |
| 121 |
* @since 1.9.2 |
| 122 |
* |
| 123 |
* @var int post_id |
| 124 |
*/ |
| 125 |
function gamipress_add_post_visited( post_id ) { |
| 126 |
|
| 127 |
if( window.localStorage ) { |
| 128 |
var posts_cached = window.localStorage.getItem('gamipress_posts_visited'); |
| 129 |
|
| 130 |
if( ! posts_cached ) { |
| 131 |
posts_cached = ''; |
| 132 |
} |
| 133 |
|
| 134 |
var posts_visited = posts_cached.split(','); |
| 135 |
|
| 136 |
posts_visited.push( post_id ); |
| 137 |
|
| 138 |
window.localStorage.setItem( 'gamipress_posts_visited', posts_visited.join(',') ); |
| 139 |
} |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
} )( jQuery ); |