<?php
/*
Plugin Name: XFish Meta
Plugin URI: http://www.uberdose.com/wordpress/xfish.html
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.8
Author: Dirk Zimmermann
Author URI: http://www.uberdose.com/
*/

/**
* Outputs meta tags "keywords" and "description" in the header
* of a page.
*/
function xfish_meta_head_start()
{
    global 
$single$posts$post_meta_cache$xfish_ob_level;
    
//print_r($posts);
    
$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);
    }
    if (
$meta_string != null) {
        echo 
$meta_string;
    }
}

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

function 
add_keywords_textinput() {
    global 
$post;
    
    
$keywords get_post_meta($post->ID'keywords'true);
    
    echo 
'<fieldset id="postkeywords"><legend><a href="http://www.uberdose.com/wordpress/xfish.html" title="Insert your meta keywords for this post here.">' __('Meta Keywords''xfish_meta') . '</a></legend>';
    echo 
'<div><input type="text" name="keywords" id="keywords" size="80" value="' $keywords '" /><br />' __('Separate keywords with commas''xfish_meta') . '</div></fieldset>';
}

function 
update_post_keywords($id) {
    
delete_post_meta($id'keywords');
    
$setting $_POST["keywords"];
    
add_post_meta($id'keywords'$setting);
}

function 
add_description_textinput() {
    global 
$post;
    
    
$description get_post_meta($post->ID'description'true);
    
    echo 
'<fieldset id="postdescription"><legend><a href="http://www.uberdose.com/wordpress/xfish.html" title="Insert a short sentence or so what this post is about.">' __('Meta description''xfish_meta') . '</a></legend>';
    echo 
'<div><input type="text" name="description" id="description" size="80" value="' $description '" /><br />' __('Should end with a \'.\'''xfish_meta') . '</div></fieldset>';
}

function 
update_post_description($id) {
    
delete_post_meta($id'description');
    
$setting $_POST["description"];
    
add_post_meta($id'description'$setting);
}

function 
edit_page_form_keywords () {
    global 
$post;
    
    
$keywords get_post_meta($post->ID'keywords'true);
    
    echo 
'<fieldset id="postkeywords"><legend><a href="http://www.uberdose.com/wordpress/xfish.html" title="Insert your meta keywords for this post here.">' __('Meta Keywords''xfish_meta') . '</a></legend>';
    echo 
'<div><input type="text" name="keywords" id="keywords" size="80" value="' $keywords '" /><br />' __('Separate keywords with commas''xfish_meta') . '</div></fieldset>';
}

function 
edit_page_form_description () {
    global 
$post;
    
    
$description get_post_meta($post->ID'description'true);
    
    echo 
'<fieldset id="postdescription"><legend><a href="http://www.uberdose.com/wordpress/xfish.html" title="Insert a short sentence or so what this post is about.">' __('Meta description''xfish_meta') . '</a></legend>';
    echo 
'<div><input type="text" name="description" id="description" size="80" value="' $description '" /><br />' __('Should end with a \'.\'''xfish_meta') . '</div></fieldset>';
}

// hook up the keywords
add_action('simple_edit_form''add_keywords_textinput');
add_action('edit_form_advanced''add_keywords_textinput');
add_action('edit_post''update_post_keywords');
add_action('publish_post''update_post_keywords');
add_action('edit_page_form''edit_page_form_keywords');

// hook up the description
add_action('simple_edit_form''add_description_textinput');
add_action('edit_form_advanced''add_description_textinput');
add_action('edit_post''update_post_description');
add_action('publish_post''update_post_description');
add_action('edit_page_form''edit_page_form_description');

add_action('wp_head''xfish_meta_head_start');

?>