Advertisement
Guest User

Untitled

a guest
May 26th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. function pmc_track_post_vews()
  2. {
  3. if(empty($_SESSION['_post_viewed']))
  4. {
  5. $_post_viewed = array();
  6. }
  7. else
  8. {
  9. $_post_viewed = $_SESSION['_post_viewed'];
  10. }
  11. global $wp_query;
  12. wp_reset_query();
  13. if(empty($_post_viewed) || !in_array($wp_query->post->ID,$_post_viewed))
  14. {
  15. wp_print_scripts('jquery');
  16. $token = wp_create_nonce( 'pmc_token' );
  17. if ( ! is_front_page() && ( is_page() || is_single() ) ) {
  18. echo '<!-- View Track --><script type="text/javascript">/* <![CDATA[ */ jQuery.post("' . admin_url('admin-ajax.php') . '", { action: "pmc_track_view", id: ' . $wp_query->post->ID . ', token: "' . $token . '" }); /* ]]> */</script><!-- /View Track -->';
  19. }
  20. }
  21. }
  22. add_action( 'wp_head', 'pmc_track_post_vews' );
  23.  
  24. add_action( 'wp_ajax_nopriv_pmc_track_view', 'fnc_update_post_view' );
  25. add_action( 'wp_ajax_pmc_track_view', 'fnc_update_post_view' );
  26. function fnc_update_post_view()
  27. {
  28. if ( ! wp_verify_nonce( $_POST['token'], 'pmc_token' ) ) die();
  29. $_postid = esc_attr( $_POST['id'] );
  30. $_views_today = (get_post_meta($_postid, '_views_today',true))?get_post_meta($_postid, '_views_today',true):0;
  31.  
  32. if(isset($_SESSION['_post_viewed']))
  33. {
  34. $_post_viewed = $_SESSION['_post_viewed'];
  35. }
  36. else
  37. {
  38. $_post_viewed = array();
  39. }
  40. if(empty($_post_viewed) || !in_array($_postid, $_post_viewed))
  41. {
  42. $_post_viewed[] = $_postid;
  43. $_SESSION['_post_viewed'] = $_post_viewed;
  44. update_post_meta($_postid, '_views_today', ($_views_today+1));
  45. }
  46. wp_die();
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement