<?

/*
Plugin Name: MethylBlue Simple Captcha
Plugin URI: http://www.methylblue.com/blog/simple-wordpress-captcha-plugin/
Description: For comments, the plugin asks a really simple question that humans can answer but bots can't
Author: Max Howell
Version: 1.2
Author URI: http://www.methylblue.com/
*/


function mxcl_comment_form()
{
    if (isset( 
$GLOBALS['user_ID'] ))
        
// user is logged in
        
return;

    echo <<<END

                <p id="mxcl_comment_form">
                    <input type="text" name="answer" id="answer" size="22" tabindex="4"/>
                    <label for="answer"><small>Are you a spam-bot? <span style='color:#999'>(required)</span></small></label>
                </p>

                 <script type="text/javascript">
                    var urlField = document.getElementById( "url" );
                    var submitp = urlField.parentNode;
                    var answerDiv = document.getElementById( "mxcl_comment_form" );
                    submitp.appendChild( answerDiv, urlField );
                </script>

END;
}


function 
mxcl_comment_post$post_ID )
{
    global 
$wpdb$user_ID$comment_type;

    if (isset( 
$user_ID ) || $comment_type !== '')
        return 
$post_ID;

    
$answer $_POST['answer'];
    
$entry_id $_POST['comment_post_ID'];

    if (
strToLower$answer ) != 'no') {
        
//roll back
        
$wpdb->query"DELETE FROM $wpdb->comments WHERE comment_ID = $post_ID" );

        
//Recount the comments
        
$count $wpdb->get_var"select count(*) from $wpdb->comments where comment_post_id = $entry_id and comment_approved = '1'" );
        
$wpdb->query"update $wpdb->posts set comment_count = $count where ID = $entry_id" );
        
$comment_count_cache[$entry_id]--;

        
$text  '<p>';
        
$text .= ($answer == 'yes')
               ? 
'Sorry, spam-bots can\'t post comments ;)'
                 
'Sorry, I can\'t be sure if you\'re human!';
        
$text .= '<p><big><big>Please press Back, and type "<b>no</b>" where it asks "<i>Are you a spam-bot?</i>"</big></big>';
        
$text .= '<p>Sorry for this hassle, but I get a lot of automated-comment-spam :(';

        die( 
$text );
    }

    return 
$post_ID;
}


add_action'comment_form''mxcl_comment_form' );
add_action'comment_post''mxcl_comment_post' );

?>