<?php
/*
Plugin Name: If You Liked That (Multi Category)...
Plugin URI: http://www.mikeschepker.com/wordpress/if-you-liked-that-multi-category/
Description: Following the content of a post, this plugin inserts links to a user selectable number of other posts in the same category as the main post. This version of the plugin supports multiple categories being listed below the post instead of posts just from one category.
Version: 1.2
Author: shep
Author URI: http://www.mikeschepker.com
*/
/* Copyright 2007 Mike Schepker and Martin Ford (email : mike@mikeschepker.com and wordpress@bochgoch.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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
add_option('ifyoulikedthat_numberOfPosts', '5', 'The number of links to related posts generated by the If You Liked That plugin', 'yes');
add_option('ifyoulikedthat_orderBy', 'rand()', 'The selection method for the related posts generated by the If You Liked That plugin', 'yes');
add_option('ifyoulikedthat_showSentence', 'Y', 'Flag to select if a sentence of the post will be shown after the link by the If You Liked That plugin', 'yes');
add_option('ifyoulikedthat_showHeading', 'If you liked that post, then try these...', 'Heading shown above the If You Liked That plugin links', 'yes');
add_option('ifyoulikedthat_excludeCategories', '', 'A comma separated list of categories for which linked posts will not be displayed (leave blank for no exlusions)', 'yes');
add_option('ifyoulikedthat_singleOnly', 'N', 'Flag to select if links are only posted on single post pages', 'yes');
function if_you_liked_that($content) {
global $wpdb;
global $post;
$cat = get_the_category();
// originally we just used the first category - this has been superceded by super awesomeness below
//$cat = $cat[0];
// put all the category IDs into an array so we can use them in our sql query below
foreach ( (array)$cat as $cat_i ) {
$the_cats[] = $cat_i->cat_ID;
}
// now stick them together into a string for our sql query: 'cat1', 'cat2', 'cat3', ...
$cats_string = "'" . implode("', '", $the_cats) . "'";
$numPosts = get_option('ifyoulikedthat_numberOfPosts');
$orderBy = get_option('ifyoulikedthat_orderBy');
$showSentence = get_option('ifyoulikedthat_showSentence');
$Heading = get_option('ifyoulikedthat_showHeading');
$excludeCategories = get_option('ifyoulikedthat_excludeCategories');
if (strlen($excludeCategories) == 0) {$excludeCategories = '-9999';}
$singleOnly = get_option('ifyoulikedthat_singleOnly');
if ($post->post_type == 'page') {return $content;} // exclude on pages
if (!is_single() and $singleOnly == 'Y') {return $content;} // include only on single view
$ifYouLikedThats = $wpdb->get_results("SELECT t1.ID, t1.post_title, t1.post_date, t1.post_content, t3.display_name FROM $wpdb->posts t1, $wpdb->post2cat t2, $wpdb->users t3 WHERE t1.ID=t2.post_id AND t1.post_status = 'publish' AND t1.ID != ".$post->ID." AND t2.category_id IN (".$cats_string.") AND t2.category_id NOT IN (".$excludeCategories.") AND t1.post_author = t3.ID ORDER BY ".$orderBy." LIMIT ".$numPosts);
if (count($ifYouLikedThats) > 0) {
$ifYouLikedThatsHTML = '<div id="ifyoulikedthat"><h3>'.$Heading.'</h3>';
foreach ($ifYouLikedThats as $ifYouLikedThat) {
$postSentence = '';
if (substr(strtolower($showSentence),0,1) == 'y') {
$pieces = split("\. ?", $ifYouLikedThat->post_content);
if (strlen($pieces[0]) > 0) {
$postSentence = '<br />'.strip_tags($pieces[0]).'.';
}
}
$ifYouLikedThatsHTML .= '<p><a href="'.get_permalink($ifYouLikedThat->ID).'">'.$ifYouLikedThat->post_title.'</a> by '.$ifYouLikedThat->display_name.' on '.date('F jS, Y',strtotime($ifYouLikedThat->post_date)).$postSentence.'</p>';
}
$ifYouLikedThatsHTML .= '</div>';
} else {
$ifYouLikedThatsHTML = '';
}
return $content.=$ifYouLikedThatsHTML;
}
// execute main function on execution of the_content
add_action('the_content', 'if_you_liked_that');
function if_you_liked_that_css() {
echo "
<style type='text/css'>
#ifyoulikedthat {
margin: 0 0 2em 0;
padding: 0;
}
#ifyoulikedthat p
{
margin:0;
padding: 0;
font-size: 0.8em;
}
#ifyoulikedthat p a
{
margin:0;
padding: 0;
font-size: 1em;
font-weight: 900;
}
</style>
";
}
//poke the css into the <head>
add_action('wp_head', 'if_you_liked_that_css');
function plugin_menu() {
$message = null;
$message_updated = __("If You Liked That Options Updated.");
// update options
if ($_POST['action'] && $_POST['action'] == 'iylt_update') {
$message = $message_updated;
update_option('ifyoulikedthat_numberOfPosts', $_POST['ifyoulikedthat_numberOfPosts']);
update_option('ifyoulikedthat_orderBy', $_POST['ifyoulikedthat_orderBy']);
update_option('ifyoulikedthat_showSentence', $_POST['ifyoulikedthat_showSentence']);
update_option('ifyoulikedthat_showHeading', $_POST['ifyoulikedthat_showHeading']);
update_option('ifyoulikedthat_excludeCategories', $_POST['ifyoulikedthat_excludeCategories']);
update_option('ifyoulikedthat_singleOnly', $_POST['ifyoulikedthat_singleOnly']);
wp_cache_flush();
}
if ($message) : ?>
<div id="message" class="updated fade"><p><?php echo $message; ?></p></div>
<?php endif; ?>
<div id="dropmessage" class="updated" style="display:none;"></div>
<div class="wrap">
<h2><?php _e('If You Liked That Plugin Options'); ?></h2>
<p><?php _e('With queries, questions or suggestions <a title="Bochgoch Home for Wordpress Plugins" href="http://www.bochgoch.com/?p=wp">Visit Bochgoch</a>.') ?></p>
<form name="dofollow" action="" method="post">
<table>
<tr>
<th scope="row" style="text-align:right; vertical-align:top;"><?php _e('The heading to display above the linked posts:')?></td>
<td>
<input size="80" name="ifyoulikedthat_showHeading" value="<?php echo stripcslashes(get_option('ifyoulikedthat_showHeading')); ?>"/>
</td>
</tr>
<tr>
<th scope="row" style="text-align:right; vertical-align:top;"><?php _e('The number of post links to display:')?></td>
<td>
<input size="2" name="ifyoulikedthat_numberOfPosts" value="<?php echo stripcslashes(get_option('ifyoulikedthat_numberOfPosts')); ?>"/>
</td>
</tr>
<tr>
<th scope="row" style="text-align:right; vertical-align:top;"><?php _e('Ordering method for the displayed posts:')?></td>
<td>
<input type="radio" name="ifyoulikedthat_orderBy" value="rand()" <?php if (get_option('ifyoulikedthat_orderBy')=='rand()') echo "checked"; ?>> Random<br>
<input type="radio" name="ifyoulikedthat_orderBy" value="post_date asc" <?php if (get_option('ifyoulikedthat_orderBy')=='post_date asc') echo "checked"; ?>> Date of post - ascending<br>
<input type="radio" name="ifyoulikedthat_orderBy" value="post_date desc" <?php if (get_option('ifyoulikedthat_orderBy')=='post_date desc') echo "checked"; ?>> Date of post - descending<br>
<input type="radio" name="ifyoulikedthat_orderBy" value="post_title asc" <?php if (get_option('ifyoulikedthat_orderBy')=='post_title asc') echo "checked"; ?>> Title of post - ascending<br>
<input type="radio" name="ifyoulikedthat_orderBy" value="post_title desc" <?php if (get_option('ifyoulikedthat_orderBy')=='post_title desc') echo "checked"; ?>> Title of post - descending<br>
</td>
</tr>
<tr>
<th scope="row" style="text-align:right; vertical-align:top;"><?php _e('Show the first sentence of linked posts:')?></td>
<td>
<input size="1" name="ifyoulikedthat_showSentence" value="<?php echo stripcslashes(get_option('ifyoulikedthat_showSentence')); ?>"/> (Y/N)
</td>
</tr>
<tr>
<th scope="row" style="text-align:right; vertical-align:top;"><?php _e('A comma separated list of categories for which linked posts will not be displayed:')?></td>
<td>
<input size="12" name="ifyoulikedthat_excludeCategories" value="<?php echo stripcslashes(get_option('ifyoulikedthat_excludeCategories')); ?>"/>
</td>
</tr>
<tr>
<th scope="row" style="text-align:right; vertical-align:top;"><?php _e('Select if links are only posted on single post pages:')?></td>
<td>
<input size="1" name="ifyoulikedthat_singleOnly" value="<?php echo stripcslashes(get_option('ifyoulikedthat_singleOnly')); ?>"/> (Y/N)
</td>
</tr>
</table>
<p class="submit">
<input type="hidden" name="action" value="iylt_update" />
<input type="submit" name="Submit" value="<?php _e('Update Options')?> »" />
</p>
</form>
</div>
<?php
}
// mt_add_pages() is the sink function for the 'admin_menu' hook
function mt_add_pages() {
// Add a new menu under Options:
add_options_page('If you liked that options', 'If You Liked That', 8, 'if_you_liked_that.php', 'plugin_menu');
}
add_action('admin_menu', 'mt_add_pages');
?>