<?php
/*
Plugin Name: Slashdigglicious
Plugin URI: http://www.tsaiberspace.net/projects/wordpress/slashdigglicious/
Description: Add user-submission icons and links at the bottom of each post: Digg, del.icio.us, etc.
Author: Robert Tsai
Author URI: http://www.tsaiberspace.net/
Version: 1.0.5
*/

function slashdiggliciousEnabled($unused)
{
	/*
	 * You can suppress links by including <!--noslashdigglicious--> in the
	 * text of your post.
	 *
	 * (See get_the_content implementation.)
	 */
	global $post, $page, $pages, $single;
	if ($page > count($pages))
		$page = count($pages);
        $post_content = $pages[$page-1];

	if (strpos($post_content, '<!--slashdigglicious-->') !== false)
		return true;
	if (!$single && strpos($post_content, '<!--more-->') !== false)
		return false;
	if (strpos($post_content, '<!--noslashdigglicious-->') !== false)
		return false;
	return true;
}

function slashdiggliciousLinks($link, $title)
{
	$link = urlencode($link);
	$title = urlencode($title);
        
        
	$slashdot = '<a href="http://slashdot.org/bookmark.pl?url='
			. "$link&amp;title=$title\""
                        . ' rel="nofollow"'
			. ' title="Slashdot It!">'
		. '<img src="http://slashdot.org/favicon.ico"'
			. ' height="16" width="16" alt="[Slashdot]" />'
		. '</a>';

	$digg = '<a href="http://digg.com/submit?phase=2&amp;url='
			. "$link&amp;title=$title"
                        . '" rel="nofollow"'
			. ' title="Digg This Story">'
		. '<img src="http://digg.com/favicon.ico"'
			. ' width="16" height="16" alt="[Digg]" />'
		. '</a>';

	$propeller = '<a href="http://www.propeller.com/submit/?U='
			. "$link&amp;T=$title"
                        . '" rel="nofollow"'
			. ' title="Propeller">'
		. '<img src="http://www.navycs.com/images/prop.gif"'
			. ' width="16" height="16" alt="[Propeller]" />'
		. '</a>';

	$reddit = '<a href="http://reddit.com/submit?url='
			. "$link&amp;title=$title"
                        . '" rel="nofollow"'
			. ' title="Reddit">'
		. '<img src="http://reddit.com/favicon.ico"'
			. ' width="16" height="16" alt="[Reddit]" />'
		. '</a>';

	$delicious = '<a href="http://del.icio.us/post?url='
			. "$link&amp;title=$title"
			. '"'
                        . ' rel="nofollow"'
		. ' title="Save to del.icio.us"'
		. ' onclick="window.open(\'http://del.icio.us/post'
			. '?v=4&amp;noui&amp;jump=close&amp;url='
			. "$link&amp;title=$title', 'delicious',"
			. " 'toolbar=no,width=700,height=400');"
			. ' return false;">'
		. '<img src="http://del.icio.us/favicon.ico"'
			. ' width="16" height="16" alt="[del.icio.us]" />'
		. '</a>';

	$facebook = '<a href="http://www.facebook.com/share.php?'
			. "u=$link"
                        . '" rel="nofollow"'
			. ' title="Share on Facebook">'
		. '<img src="http://www.facebook.com/favicon.ico"'
			. ' width="16" height="16" alt="[Facebook]" />'
		. '</a>';

	$technorati = '<a href="http://technorati.com/faves?add='
		. $link . '" title="Add to my Technorati Favorites" rel="nofollow">'
		. '<img src="http://technorati.com/favicon.ico"'
			. ' width="16" height="16" alt="[Technorati]" />'
		. '</a>';

	$google = '<a href="http://www.google.com/bookmarks/mark?op=edit&amp;'
			. 'output=popup&amp;bkmk='
			. "$link&amp;title=$title"
                        . '" rel="nofollow"'
			. ' title="Save to Google Bookmarks">'
		. '<img src="http://www.google.com/favicon.ico"'
			. ' width="16" height="16" alt="[Google]" />'
		. '</a>';

	$stumbleupon = '<a href="http://www.stumbleupon.com/submit?url='
			. "$link&amp;title=$title"
                        . '" rel="nofollow"'
			. ' title="Stumble it!">'
		. '<img src="http://www.navycs.com/images/stumbleupon.png"'
			. ' width="16" height="16" alt="[StumbleUpon]" />'
		. '</a>';

        $sphinn = '<a href="http://www.sphinn.com/submit?url='
			. urldecode($link)
                        . '&title='
                        . urldecode($title)
                        . '" rel="nofollow"'
                        . ' title="Sphinn Me!">'
		. '<img src="http://www.navycs.com/images/sphinn.gif"'
			. ' width="16" height="16" alt="[Sphinn]" />'
		. '</a>';

        $bloglines = '<a href="http://www.bloglines.com/sub/'
			. urldecode($link)
                        . '" rel="nofollow"'
                        . ' title="Add to Bloglines">'
		. '<img src="http://www.navycs.com/images/bloglines.png"'
			. ' width="16" height="16" alt="[Bloglines]" />'
		. '</a>';

        $yahoo = '<a href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u='
			. urldecode($link)
                        . '&='
                        . "$title"
                        . '" rel="nofollow"'
                        . ' title="My Yahoo!">'
		. '<img src="http://www.navycs.com/images/yahoomyweb.png"'
			. ' width="16" height="16" alt="[Yahoo]" />'
		. '</a>';

	return "<div class=\"slashdigglicious\">\n"
		. implode("\n", array(
			$slashdot,
			$digg,
	                $propeller,
			$reddit,
			$delicious,
			$facebook,
			$technorati,
			$google,
			$stumbleupon,
                        $sphinn,
                        $bloglines,
                        $yahoo,
			))
		. "\n</div>";
}

function slashdiggliciousify($content)
{
	if (!slashdiggliciousEnabled($content))
		return $content;

	return "$content\n"
		. slashdiggliciousLinks(get_permalink(),
			the_title('', '', false));
}

if (function_exists('add_action')) {
	add_action('the_content', 'slashdiggliciousify');
}

?>
