Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function pmc_track_post_vews()
- {
- if(empty($_SESSION['_post_viewed']))
- {
- $_post_viewed = array();
- }
- else
- {
- $_post_viewed = $_SESSION['_post_viewed'];
- }
- global $wp_query;
- wp_reset_query();
- if(empty($_post_viewed) || !in_array($wp_query->post->ID,$_post_viewed))
- {
- wp_print_scripts('jquery');
- $token = wp_create_nonce( 'pmc_token' );
- if ( ! is_front_page() && ( is_page() || is_single() ) ) {
- 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 -->';
- }
- }
- }
- add_action( 'wp_head', 'pmc_track_post_vews' );
- add_action( 'wp_ajax_nopriv_pmc_track_view', 'fnc_update_post_view' );
- add_action( 'wp_ajax_pmc_track_view', 'fnc_update_post_view' );
- function fnc_update_post_view()
- {
- if ( ! wp_verify_nonce( $_POST['token'], 'pmc_token' ) ) die();
- $_postid = esc_attr( $_POST['id'] );
- $_views_today = (get_post_meta($_postid, '_views_today',true))?get_post_meta($_postid, '_views_today',true):0;
- if(isset($_SESSION['_post_viewed']))
- {
- $_post_viewed = $_SESSION['_post_viewed'];
- }
- else
- {
- $_post_viewed = array();
- }
- if(empty($_post_viewed) || !in_array($_postid, $_post_viewed))
- {
- $_post_viewed[] = $_postid;
- $_SESSION['_post_viewed'] = $_post_viewed;
- update_post_meta($_postid, '_views_today', ($_views_today+1));
- }
- wp_die();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement