<?php
/*
Plugin Name: Digg This
Version: 1.0.1
Plugin URI: http://www.aviransplace.com/index.php/digg-this-wordpress-plugin/
Author: Aviran Mordo
Author URI: http://www.aviransplace.com
Description: Adds Digg story link on detection on digg referer
*/ 

function digg_this_button()
{
   digg_this("","","","",false,true);
}

function digg_this($before="", $link_text="Digg this story", $after="", $ping_str="", $show_what_is_this=true, $use_digg_button=false)
{
   global $id;
   $WHAT_IS_THIS_TEXT = 'This link was created automatically by Wordpress Digg This plugin (Download the plugin)';
   $orig_ref = $_SERVER['HTTP_REFERER'];
   $ref = strtolower($orig_ref);
   $digg_link = get_post_meta($id, 'DiggUrl', true);
   
   if ( $digg_link == '' && substr_count($ref, "_") > 0
   	&& strpos($ref, "digg.com") !== false
        && strpos($ref, "?") == false
		&& strpos($ref, "upcoming") == false
        &&
        (strpos($ref, "digg.com")==0 || 
    	strpos($ref, "http://digg.com")==0 ||
    	strpos($ref, "www.digg.com")==0 ||
    	strpos($ref, "http://www.digg.com")==0)
      )
      { 
        $digg_link=$orig_ref; 
        add_post_meta($id, 'DiggUrl', $orig_ref);
        notify_moderator_on_digg($id, $orig_ref);
      }

   if (substr_count(strtolower($digg_link),"digg.com"))
  	{
                echo "".$before;
                if ($use_digg_button)
                {
		  echo "<link rel='plugin' title='Digg This - Wordpress Plugin' href='http://www.aviransplace.com/digg-this-wordpress-plugin/' />";
                  echo "<iframe src='http://digg.com/api/diggthis.php?u=".htmlspecialchars($digg_link)."' height='82' width='55' frameborder='0' scrolling='no'></iframe>";
                }
                else
                {
  		     echo "<a href='".$digg_link."' ping='".get_permalink($post->ID)."$ping_str' target='_blank' title='Digg this story'>".$link_text."</a>";
		     echo '&nbsp;<Font Size="-2"><a href="http://www.aviransplace.com/index.php/digg-this-wordpress-plugin/" Title="'.$WHAT_IS_THIS_TEXT.'">';
		if ($show_what_is_this) echo '?';
 		     echo '</a></Font>';
                }
                echo "".$after;
  	}
}  

function notify_moderator_on_digg($post_id, $digg_ref ) {
	global $wpdb;
        
	//if( get_settings( "moderation_notify" ) == 0 )
	//	return true; 
    
	
	$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID='$post_id' LIMIT 1");

	$notify_message  = sprintf( __('A new post was dugg by digg.com users. A link back to digg.com ( %1$s ) was automaticaly added to the post #%2$s "%3$s" 

'), $digg_ref, $post_id, $post->post_title ) . "\r\n";
	$notify_message .= get_permalink($post->ID) . "\r\n\r\n";
	$subject = sprintf( __('[%1$s] A post has been dugg: "%2$s"'), get_settings('blogname'), $post->post_title );
	$admin_email = get_settings('admin_email');

	$notify_message = apply_filters('post_dugg_text', $notify_message);
	$subject = apply_filters('post_dugg_subject', $subject);

	@wp_mail($admin_email, $subject, $notify_message);
    
	return true;
}

?>
