<?php
function serpentsoft_set_post_views($postID) {
    $count_key = 'serpentsoft_post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if ($count == '') {
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    } else {
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

//To keep the count accurate, lets get rid of prefetching
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

function serpentsoft_track_post_views($post_id) {
    if (!is_single())
        return;
    if (empty($post_id)) {
        global $post;
        $post_id = $post->ID;
    }
    
    if( function_exists('w3tc_pgcache_flush') ){ ?>
        <!-- W3TC_DYNAMIC_SECURITY mfunc --><?php serpentsoft_set_post_views($post_id); ?><!-- /mfunc W3TC_DYNAMIC_SECURITY -->
        <?php return;
    }
    
    serpentsoft_set_post_views($post_id);
}

add_action('wp_head', 'serpentsoft_track_post_views');

// Get Posts Views
function serpentsoft_get_post_views($postID) {
    $count_key = 'serpentsoft_post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if ($count == '') {
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0";
    }
    return $count;
} ?>