These are my changes to the plugin...
First I've replaced the PHP content of the plugin.postviews.php file with this (update the comments at the top of the file as you wish)...
plugin.postviews.php
add_action('entry_block', 'plugin_postviews_do');
add_filter('the_content', 'plugin_postviews_track', -1);
function plugin_postviews_calc($id, $calc) {
$dir = entry_dir($id);
if (!$dir) return;
$f = $dir . '/view_counter' .EXT;
$v = io_load_file($f);
if ($v===false){
$v = 0;
}
return $v;
}
function plugin_postviews_do($id) {
global $fpdb, $smarty;
$q = $fpdb->getQuery();
$calc = $q->single;
$v = plugin_postviews_calc($id, $calc);
$smarty->assign('views', $v);
}
function plugin_postviews_track($content) {
global $smarty;
$id = $smarty->get_template_vars('id');
return $content .
'<img src="/'.PLUGINS_DIR.'postviews/v.php?v='.$id.'" width="1" height="1"/>';
}
The main changes are to add a content filter to the plugin. This filter, implemented in the plugin_postviews_track method, simply gets the ID of the current post and returns the content with the tracking pixel code attached.
The actual tracking work happens in the v.php file. This file should be in the same directory as the plugin.postviews.php file. It looks like this...
v.php
<?php
chdir(__DIR__ . '/../..'); // simulate working from blog root
require_once('defaults.php');
require_once(INCLUDES_DIR.'includes.php');
if (function_exists('system_init')) {
system_init();
}
else {
plugin_loadall();
}
if (isset($_GET['v'])) {
$id = $_GET['v'];
$calc = true;
// make sure we have a valid ID
if (strlen($id) == 18) {
$dir = entry_dir($id);
if (!$dir) return;
$f = $dir . '/view_counter' .EXT;
$v = io_load_file($f);
if ($v===false){
$v = 0;
}
elseif ($v < 0) {
$v = 0;
$calc = false;
}
if ($calc && !user_loggedin()) {
$v++;
io_write_file($f, $v);
}
// output 1x1 transparent tracking pixel
header("Content-type: image/gif");
header("Content-Length: 42");
header("Cache-Control: private, no-cache, no-cache=Set-Cookie, proxy-revalidate");
header("Expires: Wed, 11 Jan 2000 12:59:00 GMT");
header("Pragma: no-cache");
echo base64_decode('R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEA');
}
}
?>
What that does is load/increment the number of views for the blog post identified by its ID. The output of the script is a 1x1 transparent pixel, also knows as a tracking pixel, which is how we are able to use this script as the source to the img tag in the previous source file.
What you'll find with this change is it tracks the real 'user' views better. Since bots usually don't load things like images the view counter will not include bot requests.
-i