<?php
/*
Plugin Name: BetterWPMail
Plugin URI: http://nslog.com/plugins/betterwpmail/
Description: Cleans up WordPress moderation and notification emails.
Version: 0.3
Author: Erik J. Barzeski
Author URI: http://nslog.com/
*/

/* 
ChangeLog
0.1        * Initial release
0.1.1    * Added an "edit" link to the MODERATE email
        * Removed &p= from a few of the URLs as unnecessary
0.2        * Tweaked location of article permalink to match across both formats
0.3        * Added support (only) for WordPress 2.1
0.4        * Removed a rogue "Entry  :" from pingback notification
*/



/*
function:    wp_notify_moderator
changes :    * (0.1) modified layout of email to more clearly show the comment
            * (0.1) moved IP address information to bottom
            * (0.1) added link to entry comments page (edit.php?p=####&c=1)
            * (0.11) removed &p= from one of the URLs (commented out)
            * (0.2) tweaked location of article permalink to match across both formats
            * (0.3) WordPress 2.1 (only) support added
            * (0.4) Removed a rogue "Entry  :" from pingback notification
*/

if ( ! function_exists('wp_notify_postauthor') ) :
function 
wp_notify_postauthor($comment_id$comment_type='')
{
    global 
$wpdb;
    
    
$comment get_comment($comment_id);
    
$post    get_post($comment->comment_post_ID);
    
$user    get_userdata$post->post_author );

    
// If there's no email to send the comment to
    
if ('' == $user->user_email) return false;

    
$comment_author_domain = @gethostbyaddr($comment->comment_author_IP);

    
$blogname get_settings('blogname');
    
    if (
'comment' == $comment_type || empty( $comment_type ) ) {
        
$notify_message  sprintf__('The entry "%s" was recently commented upon:'), $post->post_title ) . "\n";
        
$notify_message .= "<" get_permalink($comment->comment_post_ID) . ">\n\n";
        
        
$notify_message .= sprintf__('Author : %s'), $comment->comment_author ) . "\n";
        
$notify_message .= sprintf__('E-mail : %s'), $comment->comment_author_email ) . "\n";
        
$notify_message .= sprintf__('URI    : %s'), $comment->comment_author_url ) . "\n";

        
$notify_message .= __('Comment: ') . "\n--------\n" $comment->comment_content "\n--------\n\n";
        
        
$notify_message .= sprintf__('IP    : %1$s , %2$s'), $comment->comment_author_IP$comment_author_domain ) . "\n";
        
$notify_message .= sprintf__('Whois : <http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . ">\n\n";

        
$subject sprintf__('[%1$s] Comment: "%2$s"'), $blogname$post->post_title );
    }
    elseif(
'trackback' == $comment_type || 'pingback' == $comment_type)
    {
        
$notify_message  sprintf__('The entry "%s" was recetly pinged:'), $post->post_title ) . "\n";
        
$notify_message .= "<" get_permalink($comment->comment_post_ID) . ">\n\n";
        
        
$notify_message .= sprintf__('Website: %s'), $comment->comment_author ) . "\n";
        
$notify_message .= sprintf__('IP     : %1$s , %2$s'), $comment->comment_author_IP$comment_author_domain ) . "\n";
        
$notify_message .= sprintf__('URI    : <%s>'), $comment->comment_author_url ) . "\n";
        
$notify_message .= __('Excerpt: ') . "\n--------\n" $comment->comment_content "\n--------\n\n";
        
$subject sprintf__('[%1$s] Pingback: "%2$s"'), $blogname$post->post_title );
    }

    
$notify_message .= sprintf__('Edit  : <%s>'), get_settings('siteurl').'/wp-admin/comment.php?action=editcomment&c='.$comment->comment_ID ) . "\n";
    
$notify_message .= sprintf__('Spam  : <%s>'), get_settings('siteurl')."/wp-admin/comment.php?action=cdc&dt=spam&c=$comment_id" ) . "\n";
    
$notify_message .= sprintf__('Delete: <%s>'), get_settings('siteurl')."/wp-admin/comment.php?action=cdc&c=$comment_id" ) . "\n";
    
$notify_message .= sprintf__('All   : <%s>'), get_settings('siteurl').'/wp-admin/edit.php?p=' $post->ID '&c=1') . "\n";

    
$notify_message str_replace("\r"''$notify_message);

    
$wp_email 'wordpress@' preg_replace('#^www\.#'''strtolower($_SERVER['SERVER_NAME']));

    if ( 
'' == $comment->comment_author ) {
        
$from "From: \"$blogname\" <$wp_email>";
        if ( 
'' != $comment->comment_author_email )
            
$reply_to "Reply-To: $comment->comment_author_email";
     } else {
        
$from "From: \"$comment->comment_author\" <$wp_email>";
        if ( 
'' != $comment->comment_author_email )
            
$reply_to "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>";
     }

    
$message_headers "MIME-Version: 1.0\n"
        
"$from\n"
        
"Content-Type: text/plain; charset=\"" get_settings('blog_charset') . "\"\n";

    if ( isset(
$reply_to) )
        
$message_headers .= $reply_to "\n";

    
$notify_message apply_filters('comment_notification_text'$notify_message$comment_id);
    
$subject apply_filters('comment_notification_subject'$subject$comment_id);
    
$message_headers apply_filters('comment_notification_headers'$message_headers$comment_id);

    @
wp_mail($user->user_email$subject$notify_message$message_headers);
   
    return 
true;
}
endif;




/*
function:    wp_notify_moderator
changes :    * (0.1) sped up $post selection from database
            * (0.1) modified layout of email to more clearly show the comment and approve/delete links
            * (0.1) moved IP address information to bottom
            * (0.1) added link to entry comments page (edit.php?p=####&c=1)
            * (0.11) removed &p= from three of the URLs (commented out)
            * (0.3)    WordPress 2.1 support with new URLs added
*/

if ( !function_exists('wp_notify_moderator') ) :
function 
wp_notify_moderator($comment_id)
{
    global 
$wpdb;

    if( 
get_option"moderation_notify" ) == )
        return 
true
    
    
$comment $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
    
$post $wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts WHERE ID='$comment->comment_post_ID' LIMIT 1");
//    $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
    
$comment_author_domain = @gethostbyaddr($comment->comment_author_IP);

    
$notify_message  sprintf__('A new comment on "%s" is waiting for your approval:'), $post->post_title ) . "\n";
    
$notify_message .= "<" get_permalink($comment->comment_post_ID) . ">\n\n";
    
    
$notify_message .= sprintf__('Author : %s'), $comment->comment_author ) . "\n";
    
$notify_message .= sprintf__('E-mail : %s'), $comment->comment_author_email ) . "\n";
    
$notify_message .= sprintf__('URI    : <%s>'), $comment->comment_author_url ) . "\n";

    
$notify_message .= __('Comment: ') . "\n--------\n" $comment->comment_content "\n--------\n\n";

    
$notify_message .= sprintf__('IP     : %1$s , %2$s'), $comment->comment_author_IP$comment_author_domain ) . "\n";
    
$notify_message .= sprintf__('Whois  : <http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . ">\n\n";

    
$notify_message .= sprintf__('Approve: <%s>'), get_settings('siteurl')."/wp-admin/comment.php?action=mac&c=$comment_id" ) . "\n";
    
$notify_message .= sprintf__('Delete : <%s>'), get_settings('siteurl')."/wp-admin/comment.php?action=cdc&c=$comment_id" ) . "\n";
    
$notify_message .= sprintf__('Spam   : <%s>'), get_settings('siteurl')."/wp-admin/comment.php?action=cdc&dt=spam&c=$comment_id" ) . "\n";
    
$notify_message .= sprintf__('Edit   : <%s>'), get_settings('siteurl')."/wp-admin/comment.php?action=editcomment&c=$comment_id" ) . "\n\n";
    
    
$notify_message .= sprintf__('All Comments on this entry: <%s>'), get_settings('siteurl').'/wp-admin/edit.php?p='.$post->ID.'&c=1') . "\n";


    
$subject sprintf__('[%1$s] MODERATE: "%2$s"'), get_settings('blogname'), $post->post_title );
    
$admin_email get_settings('admin_email');

    
$notify_message apply_filters('comment_moderation_text'$notify_message$comment_id);
    
$subject apply_filters('comment_moderation_subject'$subject$comment_id);

    
$notify_message str_replace("\r"''$notify_message);
    @
wp_mail($admin_email$subject$notify_message);
    
    return 
true;
}
endif;



/*
function:    wp_mail
changes :    none
*/
if ( !function_exists('wp_mail') ) :
function 
wp_mail($to$subject$message$headers '')
{
    if( 
$headers == '' )
    {
        
$headers "MIME-Version: 1.0\n" .
            
"From: \"".get_settings('blogname')."\" <wordpress@" preg_replace('#^www\.#'''strtolower($_SERVER['SERVER_NAME'])) . ">\n" 
            
"Content-Type: text/plain; charset=\"" get_settings('blog_charset') . "\"\n";
    }

    return @
mail($to$subject$message$headers);
}
endif;

?>