Brett Taylor's plugin, which was based on code by Chris Beaven. Author: The Barry Bittwister Cabal Author URI: http://singlenesia.com */ // execute cookies stuff before output! drt_smart_comments::set_recent_posts_timeout(); drt_smart_comments::set_recent_posts_cookie(); class drt_smart_comments { function set_recent_posts_timeout() { if (isset($_GET['posts_timeout'])) { $time = strtotime(gmdate('Y-m-d H:i:s')) + get_settings('gmt_offset') * 3600; $timeout = 3600*24*14; setcookie("posts_timeout", $time, $time+$timeout); if($_SERVER['HTTP_REFERER']) { header("Location: ".$_SERVER['HTTP_REFERER']); exit(); } } } // returns an array function get_recent_posts() { $db =& $GLOBALS['wpdb']; $cookie = $_COOKIE['recentposts']; // if it's empty, return an empty array if (!$cookie) return array(); // make it back into an array $recent_posts = unserialize($cookie); $recent_posts_new = array(); $timeout = 3600*24*14; $time = strtotime(gmdate('Y-m-d H:i:s')) + get_settings('gmt_offset') * 3600; $current_timeout = $time - $timeout; // go through and strip out any posts without comments in the last 14 days so we don't get a giant cookie foreach ($recent_posts as $postid => $posttime) { $sql = "SELECT comment_date FROM ".$GLOBALS['tablecomments']." WHERE comment_date > FROM_UNIXTIME(".$current_timeout.") AND comment_post_ID = ".$postid." AND comment_approved = '1' ORDER BY comment_date DESC LIMIT 1"; $rs = $db->get_results($sql); if ($rs === false) die( $wpdb->ErrorMsg()); if ($rs[0]->comment_date) $recent_posts_new[$postid] = $posttime; } return $recent_posts_new; } function set_recent_posts_cookie() { $db =& $GLOBALS['wpdb']; if (isset($_GET['p'])) { $postid = $_GET['p']; $timeout = 3600*24*14; $time = strtotime(gmdate('Y-m-d H:i:s')) + get_settings('gmt_offset') * 3600; $current_timeout = $_COOKIE['posts_timeout']; if (!$current_timeout) $current_timeout = $time - $timeout; $sql = "SELECT comment_date FROM ".$GLOBALS['tablecomments']." WHERE comment_date > FROM_UNIXTIME(".$current_timeout.") AND comment_post_ID = ".$postid." AND comment_approved = '1' ORDER BY comment_date DESC LIMIT 1"; $rs = $db->get_results($sql); if ($rs === false) die( $db->ErrorMsg()); if ($rs[0]->comment_date) { // there was a recent comment, so let's set the cookie $recent_posts = drt_smart_comments::get_recent_posts(); $recent_posts[$postid] = $time; setcookie("recentposts", serialize($recent_posts), $time+$timeout); } } } function get($limit = 10) { $db =&$GLOBALS['wpdb']; $timeout = 3600*24*14; $time = strtotime(gmdate('Y-m-d H:i:s')) + get_settings('gmt_offset') * 3600; $current_timeout = $_COOKIE['posts_timeout']; if (!$current_timeout) $current_timeout = $time - $timeout; $sql = "SELECT ID, post_title, comment_author, comment_date, comment_id"; $sql .= " FROM ".$GLOBALS['tableposts']." INNER JOIN ".$GLOBALS['tablecomments']." ON ".$GLOBALS['tableposts'].".ID = ".$GLOBALS['tablecomments'].".comment_post_ID"; $sql .= " WHERE comment_date > FROM_UNIXTIME(".$current_timeout.") AND comment_approved = '1' AND post_status = 'publish'"; $sql .= " ORDER BY comment_date DESC"; $resultset = $db->get_results($sql); $recent_posts = drt_smart_comments::get_recent_posts(); if ($resultset === false) die( "Error"); $output = ""; $posts = array(); $counter = 0; while ($data = $resultset[$counter]) { $counter++; $post_id = $data->ID; $post_title = $data->post_title; $comment_author = $data->comment_author; $comment_date = $data->comment_date; $comment_id = $data->comment_id; $post = $posts[$post_id]; $cookie_time = $recent_posts[$post_id]; $comment_timestamp = strtotime($comment_date); if (!$cookie_time) $cookie_time = $current_timeout; // if it's a recent comment and it's not the post we could be looking at... if ($comment_timestamp > $cookie_time && (!isset($_GET['p']) || ($_GET['p'] != $post_id))) { if (!$post) { $posts[$post_id] = array('comment_date' => $comment_date, 'comment_author' => $comment_author, 'comment_count' => 1, 'post_title' => $post_title, 'post_id' => $post_id, 'comment_id' => $comment_id); } else { $posts[$post_id]['comment_count']++; if ( $comment_timestamp > strtotime( $post['comment_date'] ) ) { $posts[$post_id] = array('comment_date' => $comment_date, 'comment_author' => $comment_author, 'comment_count' => $posts[$post_id]['comment_count'], 'post_title' => $post_title, 'post_id' => $post_id, 'comment_id' => $comment_id); } } } } $posts = array_values($posts); sort($posts); $posts = array_reverse($posts); while (count($posts) > $limit) array_pop($posts); return( $posts ); } function show($limit = 10, $show_count = true, $show_last = true, $output_when_no_unread_comments = true, $output_when_more_unread_comments_than_limit = false, $mark_comments_as_read_text = "Mark all comments as read", $no_unread_comments_text = "There are no unread comments", $more_unread_comments_text = "There are more unread comments...") { $posts = drt_smart_comments::get($limit) ; $output = ""; foreach ($posts as $post) { if( class_exists( "drt_disco" ) ) { $d = drt_disco::mysql2disco( $post['comment_date'] ); $strs = $d->Disco(); $date = $strs['season']."/".$strs['day']."/".$strs['year']; } else { $d = $comment['comment_date']; $date = date( "m/d/Y", mktime( substr( $d,11,2 ), substr( $d,14,2 ), substr( $d,17,2 ), substr( $d,5,2 ), substr( $d,8,2 ), substr( $d,0,4 ) ) ); } $output .= "
  • ".$post['post_title'].""; if( $show_count || $show_last ) { $output .= ""; } $output .= "
  • "; } if ($posts) { $output .= "\n\t
  • ".$mark_comments_as_read_text."
  • "; if ($output_when_more_unread_comments_than_limit && count($posts) == $limit) $output .= sprintf("\n
  • ".$more_unread_comments_text."
  • "); } elseif ($output_when_no_unread_comments) $output = "
  • ".$no_unread_comments_text."
  • "; echo stripslashes($output); } } ?>