<?php
/*
Plugin Name: SeeSpam
Plugin URI: http://mikeage.net/content/software/see-spam-wordpress-plugin/
Description: This plugin lets you clearly see items marked as spam, with separate tabs for trackbacks, pingbacks, and comments. Items can be unspammed, or batch deleted. Once activated, go to the <a href="http://mikeage.net/content/wordpress/wp-admin/edit-comments.php">Comments Tab</a>, and look at the new submenus.
Version: 0.1
Author: Mike "Mikeage" Miller
Author URI: http://mikeage.net
*/


function seespam_get_spam_count($type="")
{
    global 
$wpdb;
    
$filter ="";
    if (
$type=="comments") {
        
$filter " AND comment_type =''";
    }
    if (
$type=="trackbacks") {
        
$filter " AND comment_type ='trackback'";
    }
    if (
$type=="pingbacks") {
        
$filter " AND comment_type ='pingback'";
    }
    
$comments $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = 'spam' " $filter)
    ;
    return 
$comments;
}


function 
seespam_manage_page()
{
    global 
$wpdb$submenu;
    
add_submenu_page('edit-comments.php'"SeeSpam (All)""All Spam (" seespam_get_spam_count() . ")"1,'seespam_all',"seespam_all");
    
add_submenu_page('edit-comments.php'"SeeSpam (Comments)""Comment Spam (" seespam_get_spam_count("comments") . ")"1,'seespam_comments','seespam_comments');
    
add_submenu_page('edit-comments.php'"SeeSpam (Trackbacks)""Trackback Spam (" seespam_get_spam_count("trackbacks") . ")"1,'seespam_trackbacks','seespam_trackbacks');
    
add_submenu_page('edit-comments.php'"SeeSpam (Pingbacks)""Pingback Spam (" seespam_get_spam_count("pingbacks") . ")"1,'seespam_pingbacks','seespam_pingbacks');
}

function 
seespam_all()
{
    
seespam_show();
}
function 
seespam_comments()
{
    
seespam_show('comments');
}
function 
seespam_trackbacks()
{
    
seespam_show('trackbacks');
}
function 
seespam_pingbacks()
{
    
seespam_show('pingbacks');
}


function 
seespam_show($toShow="")
{
    global 
$wpdb;

    if (
'unspam' == $_POST['action'])  {
        
$i 0;
        foreach(
$_POST['not_spam'] as $comment) {
        
$comment = (int) $comment;
        
$wpdb->query("UPDATE $wpdb->comments SET comment_approved = '0' WHERE comment_ID = '$comment'");
        ++
$i;
    }
        echo 
"<div class=\"updated fade\"><p>$i comment" . ($i != "s" "") . " unspammed</p></div>";
    }
    if (
'delete_all' == $_POST['action']) {
        
$deleted $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_approved = 'spam'");
        if (
$deleted) {
            echo 
"<div class=\"updated fade\"><p>All Spam Deleted</p></div>";
        }
    }
    if (
'delete_comments' == $_POST['action']) {
        
$deleted $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_approved = 'spam' AND comment_type =''");
        if (
$deleted) {
            echo 
"<div class=\"updated fade\"><p>All Spam Comments Deleted</p></div>";
        }
    }
    if (
'delete_trackbacks' == $_POST['action']) {
        
$deleted $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_approved = 'spam' AND comment_type='trackback'");
        if (
$deleted) {
            echo 
"<div class=\"updated fade\"><p>All Spam Trackbacks Deleted</p></div>";
        }
    }
    if (
'delete_pingbacks' == $_POST['action']) {
        
$deleted $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_approved = 'spam' AND comment_type='pingback'");
        if (
$deleted) {
            echo 
"<div class=\"updated fade\"><p>All Spam Pingbacks Deleted</p></div>";
        }
    }
    
?>

    <div class="wrap">
    <?php

    
if ($toShow == "") {
        echo 
"<h2>All Spam</h2>";
    } else if (
$toShow =="comments") {
        echo 
"<h2>All Comment Spam</h2>";
    } else if (
$toShow =="trackbacks") {
        echo 
"<h2>All Trackback Spam</h2>";
    } else if (
$toShow =="pingbacks") {
        echo 
"<h2>All Pingback Spam</h2>";
    } else {
        echo 
"<h2>Huh?</h2>";
    }

    
$spam_count seespam_get_spam_count();
    
    echo 
"<p>SeeSpam allows you to delete everything listed below ($spam_count items) with just one click. Note that there's 
no undo for this, so be careful!</p>"
;

    if (
$toShow =="") {
    echo 
"<form method=\"post\" action=\"edit-comments.php?page=seespam_all\" name=\"form1\">";
        echo 
"<input type=\"hidden\" name=\"action\" value=\"delete_all\" />";
        echo 
"<input type=\"submit\" name=\"Submit\" value=\"Delete All Spam\" />";
    echo 
"</form>";
    } else if (
$toShow =="comments") {
    echo 
"<form method=\"post\" action=\"edit-comments.php?page=seespam_comments\" name=\"form1\">";
        echo 
"<input type=\"hidden\" name=\"action\" value=\"delete_comments\" />";
        echo 
"<input type=\"submit\" name=\"Submit\" value=\"Delete All Spam Comments\" />";
    echo 
"</form>";
    } else if (
$toShow =="trackbacks") {
    echo 
"<form method=\"post\" action=\"edit-comments.php?page=seespam_trackbacks\" name=\"form1\">";
        echo 
"<input type=\"hidden\" name=\"action\" value=\"delete_trackbacks\" />";
        echo 
"<input type=\"submit\" name=\"Submit\" value=\"Delete All Spam Trackbacks\" />";
    echo 
"</form>";
    } else if (
$toShow =="pingbacks") {
    echo 
"<form method=\"post\" action=\"edit-comments.php?page=seespam_pingbacks\" name=\"form1\">";
        echo 
"<input type=\"hidden\" name=\"action\" value=\"delete_pingbacks\" />";
        echo 
"<input type=\"submit\" name=\"Submit\" value=\"Delete All Spam Pingbacks\" />";
    echo 
"</form>";
    }
    
?>
    </div>

   <div class="wrap">
    <h2>Spam Found</h2>
    <?php
    $filter 
="";
    if (
$toShow=="comments") {
        
$filter " AND comment_type =''";
    }
    if (
$toShow=="trackbacks") {
        
$filter " AND comment_type ='trackback'";
    }
    if (
$toShow=="pingbacks") {
        
$filter " AND comment_type ='pingback'";
    }
    
    
$comments $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = 'spam' " $filter);
    if (
$comments) {        
    if (
$toShow =="") {
    echo 
"<form method=\"post\" action=\"edit-comments.php?page=seespam_all\" name=\"form2\">";
    } else if (
$toShow =="comments") {
    echo 
"<form method=\"post\" action=\"edit-comments.php?page=seespam_comments\" name=\"form2\">";
    } else if (
$toShow =="trackbacks") {
    echo 
"<form method=\"post\" action=\"edit-comments.php?page=seespam_trackbacks\" name=\"form2\">";
    } else if (
$toShow =="pingbacks") {
    echo 
"<form method=\"post\" action=\"edit-comments.php?page=seespam_pingbacks\" name=\"form2\">";
    }
?>
        <input type="hidden" name="action" value="unspam" />
        <input type="submit" name="submit" value="Unspam Selected Comments" />

        <table width="100%" cellpadding="3" cellspacing="3">
<!--
        <th scope="col">Unspam?</th>
        <th scope="col">Name</th>
        <th scope="col">Email</th>
        <th scope="col">URI</th>
        <th scope="col">IP</th>
        <th scope="col">Comments</th>
-->
<tr>
<th scope="col" rowspan="2">UnSpam</th>
        <th scope="col">Name</th>
        <th scope="col">URI</th>
        <th scope="col">IP</th>
</tr>
<tr>
<th scope="col" colspan="3">Comment</th>
</tr>


        <?php
        
foreach($comments as $comment) {
            
$comment_date mysql2date(get_settings("date_format") . " @ " get_settings("time_format"), 
        
$comment->comment_date);

            
$post_title $wpdb->get_var("SELECT post_title FROM $wpdb->posts WHERE ID='$comment->comment_post_ID'");
            
$bgcolor '';
            
$class = ('alternate' == $class) ? '' 'alternate';
            
?>
<tr class='<?php echo $class?>'>
            <td align="center" rowspan="2"><input type="checkbox" name="not_spam[]" value="<?php echo $comment->comment_ID?>" /></td>
            <td><?php echo $comment->comment_author ?></td>
            <td><?php echo $comment->comment_author_url ?></td>
            <td align="center"><a href="http://ws.arin.net/cgi-bin/whois.pl?queryinput=<?php echo $comment->comment_author_IP ?>"><?php echo $comment->comment_author_IP ?></a></td>
</tr>
<tr class='<?php echo $class?>'>
        <td colspan="3"><?php echo $comment->comment_content ?></td>
</tr>
            <?php
        
}
    
?>
    </table>
    <input type="submit" name="submit" value="Unspam Selected Comments" />
    </form>
    <?php
    
}
    
?>
    </div>
    
    <?php
}
add_action('admin_menu''seespam_manage_page');

?>