<?php

/*
Plugin Name: Jerome's Keywords: Related Posts 
Plugin URI: http://sw-guide.de/wordpress/plugins/jeromes-keywords-related-posts/
Description: Plugin for <a href="http://vapourtrails.ca/wp-keywords">Jerome's Keywords Plugin</a> that presents related posts according to the tags (keywords).  
Version: 2.1
Author: Michael Woehrer
Author URI: http://sw-guide.de
*/ 

/*	----------------------------------------------------------------------------
 	    ____________________________________________________
       |                                                    |
       |     Related Posts for Jerome's Keywords Plugin     |
       |                © Michael Woehrer                   |
       |____________________________________________________|

	© Copyright 2006  Michael Woehrer (michael dot woehrer at gmail dot com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

	----------------------------------------------------------------------------
	ACKNOWLEDGEMENTS:
	 - Thanks to Jerome for his "Jerome's Keywords" plugin:
	   http://vapourtrails.ca/wp-keywords
	----------------------------------------------------------------------------
*/

function jkeywords_related_posts(
	$order = 'DATE_DESC', 
	$limit_quantity = 5, 
	$limit_days = 365,
	$format = '<li>%date%: <a href="%permalink%" title="%title%">%title%</a> (%commentcount%)</li>',
	$dateformat = 'd.m.y'
	) {

	/////////////////////////////////////////
	// Exit if Jerome's Keywords plugin is not activated or does not exist
	/////////////////////////////////////////
	if( ! class_exists('JeromesKeywords')) {
		return false;
	}

	/////////////////////////////////////////
	// WordPress globals
	/////////////////////////////////////////
	global $wpdb, $id;

	/////////////////////////////////////////
	// Jerome's Keywords object
	/////////////////////////////////////////
	global $JKeywords;
	
	/////////////////////////////////////////
	// Get tags of current post
	/////////////////////////////////////////
	$tags_arr = $JKeywords->getPostTags($id);
	if (empty ($tags_arr)) {
		return false;
	}
	$tags_comma_separated = '';
	foreach($tags_arr as $loopval) { 
		$tags_comma_separated .= mysql_real_escape_string($loopval) . "','";
	}
	$tags_comma_separated = substr($tags_comma_separated, 0, -3);	// remove trailing ','


	/////////////////////////////////////////
	// PREPARE ORDER
	/////////////////////////////////////////
	switch (strtoupper($order)) {
		case 'RANDOM':
			$order_by = 'RAND()';
			break;
		case 'DATE_ASC':
			$order_by = 'posts.post_date ASC';
			break;
		default:	// 'DATE_DESC'
			$order_by = 'posts.post_date DESC';
	}

	/////////////////////////////////////////
	// Set limit of posting date. 86400 seconds = 1 day
	/////////////////////////////////////////
	$timelimit = '';
	if ($daylimit != 0) $timelimit = 'AND posts.post_date > ' . date('YmdHis', time() - $limit_days*86400);

	/////////////////////////////////////////
	// SQL query
	/////////////////////////////////////////
	$queryresult = $wpdb->get_results("SELECT DISTINCT posts.ID, posts.post_title, posts.post_date, posts.comment_count
							FROM $wpdb->posts posts, $JKeywords->_table jkeywords
							WHERE posts.ID <> $id
							AND posts.post_status = 'publish'
							AND posts.post_date < '" . current_time('mysql') . "'
							AND jkeywords.tag_name IN ('$tags_comma_separated')
							AND jkeywords.post_id = posts.ID
							$timelimit
							ORDER BY " . $order_by . "
							LIMIT $limit_quantity
							");

	/////////////////////////////////////////
	// RETURN LIST
	/////////////////////////////////////////
	$permalist = '';
	if (count($queryresult) > 0) {
		foreach($queryresult as $tag_loop) {
			// Date of post
			$loop_postdate = mysql2date($dateformat, $tag_loop->post_date);
			// Replace placeholders
			$element_loop = $format;
			$element_loop = str_replace('%date%', $loop_postdate, $element_loop);
			$element_loop = str_replace('%permalink%', get_permalink($tag_loop->ID), $element_loop);
			$element_loop = str_replace('%title%', $tag_loop->post_title, $element_loop);
			$element_loop = str_replace('%commentcount%', $tag_loop->comment_count, $element_loop);
			// Add to list
			$permalist .= $element_loop . "\n";
		}
		$permalist = "\n" . '<!-- Related Posts, generated by \'Related Posts for Jerome\'s Keywords Plugin\' - http://sw-guide.de/ -->' . "\n" . $permalist; // Please do not remove this line.
		return $permalist;
	} else {
		return false;
	}
	
}

?>