<?
/*
Plugin Name: Brian's Latest Comments
Plugin URI: http://meidell/latestcomments/
Version: 1.5.8
Description: This shows an overview of the recently active articles and the last people to comment on them. Original idea and code fixes contributed by <a href="http://binarybonsai.com">Michael Heilemann</a>.<br />If you have <a href="http://binarybonsai.com/archives/2004/08/17/time-since-plugin/">Dunstan's Time Since</a> installed, this plugin uses it for the title="" attributes on the comments and posts. (For WordPress 1.5)
Author: Brian Meidell
Author URI: http://meidell.dk/blog

Version 1.5: 	Now works without LOCK TABLE and CREATE TEMPORARY TABLE priviledges.
Version 1.5.1:  Can't remember what I did here
Version 1.5.2: 	Fixed count select statement to not include spammy comments
Version 1.5.3: 	Properly excludes track- and pingbacks
Version 1.5.4:  Excludes posts that are not published, even if they have comments
Version 1.5.5:	Fade old comments, fixed bug that wreaked havoc with Time Since
Version 1.5.6:	Bugfix from Jonas Rabbe (http://www.jonas.rabbe.com/) pertaining to timesince
Version 1.5.7:	Bugfix so old colors can be darker than new colors (stupid oversight), thanks to http://spiri.dk for spotting it.
		Bugfix where single digit hex would cause invalid colors, thanks to http://www.wereldkeuken.be/ for the fix.
Version 1.5.8:	Bugfix from Matthias Schutz regarding time_since.

*/ 

function blc_latest_comments($num_posts = 5, $num_comments = 6, $hide_pingbacks_and_trackbacks = true, $prefix = "<li class='alternate'>", $postfix = "</li>", $fade_old = true, $range_in_days = 10, $new_col = "#444444", $old_col = "#cccccc")
{
	global $wpdb, $tablecomments, $tableposts;

	function clamp($min, $max, $val) 
	{
		return max($min,min($max,$val));
	}

	function get_channel($col, $offset)
	{
		return hexdec(substr($col, $offset, 2));
	}

	function channels($col)
	{
		$r = get_channel($col, 1);
		$g = get_channel($col, 3);
		$b = get_channel($col, 5);
		return array($r, $g, $b);
	}

	$max_time = $range_in_days * 24 * 60 * 60 ; 

	function scale_channel($old, $new, $diff, $max_time)
	{
		$range = $old - $new;
		$c = round($diff/$max_time*($range))+$new; 
		$c = clamp(min($new, $old), max($new, $old), $c);
		$c_hex = str_pad(dechex($c), 2, '0', STR_PAD_LEFT);
		return $c_hex;
	}

	$usetimesince = function_exists('time_since'); // Work nicely with Dunstan's Time Since plugin (adapted by Michael Heilemann)

	// This is compensating for the lack of subqueries in mysql 3.x
	// The approach used in previous versions needed the user to
	// have database lock and create tmp table priviledges. 
	// This uses more queries and manual DISTINCT code, but it works with just select privs.
	if(!$hide_pingbacks_and_trackbacks)
		$ping = "";
	else
		$ping = "AND comment_type<>'pingback' AND comment_type<>'trackback'";
	$posts = $wpdb->get_results("SELECT 
		comment_post_ID, post_title 
		FROM ($tablecomments LEFT JOIN $tableposts ON (comment_post_ID = ID))
		WHERE comment_approved = '1' 
		AND $tableposts.post_status='publish'
		$ping
		ORDER BY comment_date DESC;");
		
	$seen = array();	
	$num = 0;

	if($fade_old)
	{
		list($r_new, $g_new, $b_new) = channels($new_col);
		list($r_old, $g_old, $b_old) = channels($old_col);
	}
	
	foreach($posts as $post)
	{
		// The following 5 lines is a manual DISTINCT and LIMIT,
		// since mysql 3.x doesn't allow you to control which way a DISTINCT
		// select merges multiple entries.
		if(array_key_exists($post->comment_post_ID, $seen))
			continue;
		$seen[$post->comment_post_ID] = true;	
		if($num++ > $num_posts)
			break;
		
		$commenters = $wpdb->get_results("SELECT *, UNIX_TIMESTAMP(comment_date) AS unixdate FROM $tablecomments
	       			WHERE comment_approved = '1'
				AND comment_post_ID = '".$post->comment_post_ID."'
				$ping
				ORDER BY comment_date DESC
				LIMIT $num_comments;");

		$count = $wpdb->get_var("SELECT COUNT(comment_ID) AS c FROM $tablecomments WHERE comment_post_ID = $post->comment_post_ID AND comment_approved = '1' ".$ping);
		$i = 0;
		$link = get_permalink($post->comment_post_ID);
		if($usetimesince)
			$title = " title=\"Last comment was ".time_since($commenters[0]->unixdate)." ago\"";
		else
			$title  = "";
		echo $prefix."<a href=\"".$link."\"$title class=\"activityentry\">".stripslashes($post->post_title). "</a>&nbsp;&nbsp;<a href=\"$link#comments\" title=\"Go to the comments of this entry\">".$count."</a><br />\n<small>";
		foreach($commenters as $commenter)
		{
			if($usetimesince)
				$title = " title=\"Posted ".time_since($commenter->unixdate)." ago\"";

			if($fade_old)
			{
				$diff = time() - $commenter->unixdate;
				$r_hex = scale_channel($r_old, $r_new, $diff, $max_time);
				$g_hex = scale_channel($g_old, $g_new, $diff, $max_time);
				$b_hex = scale_channel($b_old, $b_new, $diff, $max_time);
				$colstr = " style=\"color: #".$r_hex.$g_hex.$b_hex.";\"";
			}

			if($i++ > 0)
				echo ", ";
		
			echo "<a$colstr href=\"".$link . "#comment-" . $commenter->comment_ID."\"$title>".stripslashes($commenter->comment_author)."</a>";
		}
		if($count > $num_comments) 
			echo " <a href=\"$link#comments\" title=\"Go to the comments of this entry\">[...]</a>";
		echo "</small>".$postfix."\n";

	}
}

?>
