<?php
/*
Plugin Name: Link Rel
Plugin URI: http://boxofchocolates.ca/projects/wp-linkrel
Description: Displays link relationships for the previous and next posts in the head of the document. Modified by <a href="http://bd.dotted.jp/" title="BirDesign">Kaira</a>.
Version: 0.2a
Author: Derek Featherstone
Author URI: http://boxofchocolates.ca
*/


function linkrel($post_ID)
{
    if (
is_single()) {
        
rel_next_post();
        
rel_previous_post();
    }

    if (
is_home() || is_search()) {
        
rel_next_posts();
        
rel_previous_posts();
    }

    if (!
is_home()) {
        echo 
'<link rel="home" href="' get_settings('siteurl') .'" title="' get_settings('blogname') . '" />';
    }

    return 
$post_ID;
}

function 
rel_next_posts($label='Next Page &raquo;'$max_page=0) {
    global 
$paged$result$request$posts_per_page$wpdb$max_num_pages;
    if ( !
$max_page ) {
            if ( isset(
$max_num_pages) ) {
                
$max_page $max_num_pages;
            } else {
                
preg_match('#FROM\s(.*)\sGROUP BY#siU'$request$matches);
                
$fromwhere $matches[1];
                
$numposts $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere");
                
$max_page $max_num_pages ceil($numposts $posts_per_page);
            }
    }
    if ( !
$paged )
        
$paged 1;
    
$nextpage intval($paged) + 1;
    if ( (! 
is_single()) && (empty($paged) || $nextpage <= $max_page) ) {
        echo 
"\t".'<link rel="next" href="';
        
next_posts($max_page);
        echo 
'" title="'.$label .'" />'."\n";
    }
}

function 
rel_previous_posts($label='&laquo; Previous Page') {
    global 
$paged;
    if ( (!
is_single())    && ($paged 1) ) {
        echo 
"\t".'<link rel="prev" href="';
        
previous_posts();
        echo 
'" title="'$label .'" />'."\n";
    }
}

function 
rel_next_post($format='%'$next=''$title='yes'$in_same_cat=
'no'$limitnext=1$excluded_categories='') {
    global 
$tableposts$posts_per_page$post$wpdb$single;
    if(
== $posts_per_page || == $single) {

        
$current_post_date $post->post_date;
        
$current_category $post->post_category;

        
$sqlcat '';
        if (
$in_same_cat != 'no') {
            
$sqlcat " AND post_category='$current_category' ";
        }

        
$sql_exclude_cats '';
        if (!empty(
$excluded_categories)) {
            
$blah explode('and'$excluded_categories);
            foreach(
$blah as $category) {
                
$category intval($category);
                
$sql_exclude_cats .= " AND post_category != $category";
            }
        }

        
$now current_time('mysql');

        
$limitnext--;

        
$relnextpost = @$wpdb->get_row("SELECT ID,post_title FROM $tableposts WHERE post_date > '$current_post_date' AND post_date < '$now' AND post_status = 'publish' $sqlcat $sql_exclude_cats AND ID != $post->ID ORDER BY post_date ASC LIMIT $limitnext,1");
        if (
$relnextpost) {
            
$string "\t".'<link rel="next" href="'.get_permalink($relnextpost->ID).'" title="'.$next;
            if (
$title=='yes') {
                
$string .= wptexturize(strip_tags(stripslashes($relnextpost->post_title)));
            }
            
$string .= '" />'."\n";
            
$format str_replace('%'$string$format);
            echo 
$format;
        }
    }
}


function 
rel_previous_post($format='%'$previous=''$title='yes'$in_same_cat='no'$limitprev=1$excluded_categories='') {
    global 
$tableposts$id$post$wpdb;
    global 
$p$posts$posts_per_page$s$single;
    global 
$querystring_start$querystring_equal$querystring_separator;

    if((
$p) || ($posts_per_page == 1) || == $single) {

        
$current_post_date $post->post_date;
        
$current_category $post->post_category;

        
$sqlcat '';
        if (
$in_same_cat != 'no') {
            
$sqlcat " AND post_category = '$current_category' ";
        }

        
$sql_exclude_cats '';
        if (!empty(
$excluded_categories)) {
            
$blah explode('and'$excluded_categories);
            foreach(
$blah as $category) {
                
$category intval($category);
                
$sql_exclude_cats .= " AND post_category != $category";
            }
        }

        
$limitprev--;
        
$rellastpost = @$wpdb->get_row("SELECT ID, post_title FROM $tableposts WHERE post_date < '$current_post_date' AND post_status = 'publish' $sqlcat $sql_exclude_cats ORDER BY post_date DESC LIMIT $limitprev, 1");
        if (
$rellastpost) {
            
$string "\t".'<link rel="prev" href="'.get_permalink($rellastpost->ID).'" title="'.$previous;
            if (
$title == 'yes') {
                
$string .= wptexturize(strip_tags(stripslashes($rellastpost->post_title)));
            }
            
$string .= '" />'."\n";
            
$format str_replace('%'$string$format);
            echo 
$format;
        }
    }
}

?>