<?php
/*
Plugin Name: XFish Meta
Plugin URI: http://www.uberdose.com/journal/archives/2004/10/02/keywords-and-description-meta-tags-for-wordpress-xfish_meta/
Description: Tries to deal correctly with the meta tags of posts like "keywords" and "description". Just insert the custom fields 'keywords' and 'description' in a post, 'keywords' being a comma separated list of keywords for that post, 'description' a short description about that post.
Version: 0.1
Author: Dirk Zimmermann
Author URI: http://www.uberdose.com/journal/
*/

/**
* Store the output buffer level so we can issue a warning if
* another plugin makes use of it too.
*/
$xfish_ob_level 0;

define('__XFISH_META_TAGS__''__XFISH_META_TAGS__');

/**
* Starts output buffering and inserts special code, so that
* xfish_meta_head_finish can finish the work when the posts
* have been retrieved from the DB.
*/
function xfish_meta_head_start()
{
    global 
$xfish_ob_level;
    
ob_start();
    
$xfish_ob_level ob_get_level();
    echo(
__XFISH_META_TAGS__);
}

/**
* Outputs meta tags "keywords" and "description" in the header
* of a page.
*/
function xfish_meta_head_finish($content)
{
    global 
$single$posts$post_meta_cache$xfish_ob_level;
    
$current_level ob_get_level();
    if (
$current_level != $xfish_ob_level) {
        
// issue a big warning here
        
echo("<b>xfish_meta: ".
        
"Something else is playing with the output buffer. " .
        
"Try to disable xfish_meta or another plugin.</b>");
    }
    
$meta_string null;
    if (
is_array($posts)) {
        foreach (
$posts as $post) {
            if (
$post) {
                
$keywords_a $keywords_i null;
                
$description_a $description_i null;
                
$id $post->ID;
                
$meta = &$post_meta_cache[$id];
                if (
is_array($meta)) {
                    
$keywords_a $meta['keywords'];
                    
$description_a $meta['description'];
                    if (
is_array($keywords_a)) {
                        
$keywords_i $keywords_a[0];
                    }
                    if (
is_array($description_a)) {
                        
$description_i $description_a[0];
                    }
                }
                if (!isset(
$description_i)) {
                    
// look for an excerpt?
                
}
                if (isset(
$keywords_i)) {
                    
//$meta_string .= sprintf("", $keywords);
                    
if (isset($keywords)) {
                        
$keywords .= ',';
                    }
                    
$keywords .= $keywords_i;
                }
                if (isset(
$description_i)) {
                    
//$meta_string .= sprintf("", $description);
                    
if (isset($description)) {
                        
$description .= ' ';
                    }
                    
$description .= $description_i;
                }
            }
        }
    }
    
    if (isset(
$keywords)) {
        
$keywords getUniqueKeywords($keywords);
        
$meta_string sprintf("<META NAME=\"keywords\" Content=\"%s\">",
        
$keywords);
    }
    if (isset(
$description)) {
        
$meta_string .= sprintf("\n<META NAME=\"description\" Content=\"%s\">",
        
$description);
    }
    
$buffered_content ob_get_contents();
    
ob_clean();
    
$buffered_content =
    
str_replace(__XFISH_META_TAGS__$meta_string$buffered_content);
    echo(
$buffered_content);
    echo(
$content);
}

function 
getUniqueKeywords($keywords)
{
    
$keywords_ar array_unique(explode(','$keywords));
    return 
implode(','$keywords_ar);
}

//add_filter('comment_text', 'hilite');
add_action('wp_head''xfish_meta_head_start');
add_filter('the_content''xfish_meta_head_finish');

?>