<?php
/*
Plugin Name: Ribbon Maker
Plugin URI: http://wordpress.org/extend/plugins/ribbon-maker
Description: When activated, this plugin will put a ribbon of your design, message and custom link on the top right corner of your website.
Author: Al Lamb
Version: 0.1
License: GPLv2
Author URI: http://bowierocks.com
*/

function ribbon_maker_admin() {
	add_submenu_page( 'tools.php', 'Ribbon Maker Settings', 'Ribbon Maker', 10, __FILE__, 'ribbon_maker_admin_menu' );
}

function ribbon_maker_admin_menu() {
	echo('
		<div class="wrap">
		<h2>Ribbon Maker Options</h2><address style="font-size: 8pt; font-weight: 700;">Version 1.1 (<a href="http://www.bowierocks.com/ribbon-maker" target="_blank">Ribbon Maker</a>)</address>
	');
	if ( isset( $_POST['submit'] ) ) {
		$str = strtr( $_POST['ribbon_maker_url'], array( '"' => '&#34;', '\\' => '', '\'' => '&#39;' ) );
		$ribbon_maker = update_option( 'ribbon_maker_url', $str );
		$str = strtr( $_POST['ribbon_maker_title'], array( '"' => '&#34;', '\\' => '', '\'' => '&#39;' ) );
		$ribbon_maker .= update_option( 'ribbon_maker_title', $str);
		$str = strtr( $_POST['ribbon_maker_admin_offset'], array( '"' => '&#34;', '\\' => '', '\'' => '&#39;' ) );
		$ribbon_maker .= update_option( 'ribbon_maker_admin_offset', $str );
		$str = strtr( $_POST['ribbon_maker_user_offset'], array( '"' => '&#34;', '\\' => '', '\'' => '&#39;' ) );
		$ribbon_maker .= update_option( 'ribbon_maker_user_offset', $str );
		$str = strtr( $_POST['ribbon_maker_message'], array( '"' => '&#34;', '\\' => '', '\'' => '&#39;' ) );
		$ribbon_maker .= update_option( 'ribbon_maker_message', $str );
		$str = strtr( $_POST['ribbon_maker_bgcolor'], array( '"' => '&#34;', '\\' => '', '\'' => '&#39;' ) );
		$ribbon_maker .= update_option( 'ribbon_maker_bgcolor', $str );
		$str = strtr( $_POST['ribbon_maker_fgcolor'], array( '"' => '&#34;', '\\' => '', '\'' => '&#39;' ) );
		$ribbon_maker .= update_option( 'ribbon_maker_fgcolor', $str );
		if ( $ribbon_maker )
			echo('<div class="updated"><p><strong>Your setting have been saved.</strong></p></div>');
		else
			echo('<div class="error"><p><strong>Your setting have not been saved.</strong></p></div>');
	}

	echo('
		<form action="" method="post">
		<table class="form-table">
		<tr><td>URL to Link to<br/><span class="ribbon_maker_hint">Insert full HTTP quallifed link. for example: http://bowierocks.com/donate</span></td>
<td><input type="text" name="ribbon_maker_url" size="80" value="'.get_option( 'ribbon_maker_url','http://bowierocks.com/ribbon-maker' ).'"></td>
</tr>
		<tr><td>TITLE for Link<br/><span class="ribbon_maker_hint">This is the message that pops up when you hove over links.</span></td>
<td><input type="text" name="ribbon_maker_title" size="80" value="'.get_option( 'ribbon_maker_title','Get Support at BowieRocks.com for Ribbon Maker' ).'"></td>
</tr>
		<tr><td>MESSAGE for Ribbon<br/><span class="ribbon_maker_hint">This is the message that pops up when you hove over links.</span></td>
<td><input type="text" name="ribbon_maker_message" size="80" value="'.get_option( 'ribbon_maker_message', 'Ribbon Maker by Al Lamb' ).'"></td>
</tr>
		<tr><td>BGCOLOR of Ribbon<br/><span class="ribbon_maker_hint">This is the message that pops up when you hove over links.</span></td>
<td><input type="text" name="ribbon_maker_bgcolor" size="10" maxlength=7 value="'.get_option( 'ribbon_maker_bgcolor','000000' ).'"></td>
</tr>
		<tr><td>FGCOLOR of Ribbon<br/><span class="ribbon_maker_hint">This is the color of the text comprising the message.</span></td>
<td><input type="text" name="ribbon_maker_fgcolor" size="10" maxlength=7 value="'.get_option( 'ribbon_maker_fgcolor','ffffff' ).'"></td>
</tr>
		<tr><td>Admin Vertical Offset<br/><span class="ribbon_maker_hint">Enter a positive number to push the ribbon down, negative for up. Suggest 28 to offset the ADMIN bar.</span></td>
<td><input type="text" name="ribbon_maker_admin_offset" size="5" maxlength=5 value="'.get_option( 'ribbon_maker_admin_offset','0' ).'"></td>
</tr>
		<tr><td>User Vertical Offset<br/><span class="ribbon_maker_hint">Enter a positive number to push the ribbon down, negative for up. Suggest 28 to offset the USER bar.</span></td>
<td><input type="text" name="ribbon_maker_user_offset" size="5" maxlength=5 value="'.get_option( 'ribbon_maker_user_offset','0' ).'"></td>
</tr>
		<hr />
		<tr><td colspan="2"><hr /></td></tr>
		<tr><td><input class="button-primary" type="submit" name="submit" value="Save Changes" /></td><td>&nbsp;</td></tr>
		</table>
		</form>
		</div>
	' );
}

function ribbon_maker_admin_css() {
	echo( '
		<style type="text/css">
		.ribbon_maker_admin_hint {
		font-size: 7pt;
		font-style: italic;
		color: #080;
		}
		</style>
	' );
}
add_action( 'admin_menu', 'ribbon_maker_admin' );
add_action( 'admin_head', 'ribbon_maker_admin_css' );

function ribbon_maker_get_color($sample,$color) {
  switch($color) {
    case "red":
      $sample = substr($sample,0,2);
      break;
    case "green":
      $sample = substr($sample,2,2);
      break;
    case "blue":
      $sample = substr($sample,4,2);
      break;
    default:
      $sample = "00"; //should never get this! but just in case
  }
  return hexdec($sample);
}

function ribbon_maker_create_ribbon($string,$bgcolor,$fgcolor) {
  $font   = 5;
  $width  = ImageFontWidth($font) * strlen($string)+ImageFontWidth($font)*4;
  $height = ImageFontHeight($font)+7;
  $im = imagecreatetruecolor ($width,$height);
  
  $back = imagecolorallocate($im, (int)18, (int)52, (int)86);
  $draw_color = imagecolorallocate ($im, (int)ribbon_maker_get_color($bgcolor,"red"), (int)ribbon_maker_get_color($bgcolor,"green"), (int)ribbon_maker_get_color($bgcolor,"blue"));
  $text_color = imagecolorallocate ($im, (int)ribbon_maker_get_color($fgcolor,"red"), (int)ribbon_maker_get_color($fgcolor,"green"), (int)ribbon_maker_get_color($fgcolor,"blue"));

  imageline($im, 0,2,$width,2,$draw_color);

  imagefilledrectangle($im,0,4,$width,$height-5,$draw_color);

  imageline($im, 0,$height-3,$width,$height-3,$draw_color);

  imagestring ($im, $font, ImageFontWidth($font)*2, 3,  $string, $text_color);

  $new_img = imagerotate ($im, 360-45, 0);

  imagecolortransparent($new_img,$back);


  imagepng ($new_img,plugin_dir_path(__FILE__)."ribbon_maker_ribbon.png");//this actually writes out the image!
  imagedestroy ($im);
  imagedestroy ($new_img);
}

function render_ribbon_maker() {
	if(is_admin_bar_showing()) 
	  $offset = get_option("ribbon_maker_admin_offset");
	else
	  $offset = get_option("ribbon_maker_user_offset");
	
	ribbon_maker_create_ribbon(get_option('ribbon_maker_message'),get_option('ribbon_maker_bgcolor'),get_option('ribbon_maker_fgcolor'));

	$img = plugins_url('ribbon_maker_ribbon.png',__FILE__);

	echo "<a target='_blank' class='ribbon-maker' href='". get_option("ribbon_maker_url")."' title='".get_option("ribbon_maker_title")."'><img src='{$img}' alt='Ribbon Maker' style='position: fixed; top: {$offset}px; right: 0; z-index: 100000; cursor: pointer;' /></a>";
}

add_action( 'wp_footer', 'render_ribbon_maker' );
?>