<?php 
/* 
Plugin Name: roysense
Version: 0.1
Plugin URI: http://philhord.com/wp-hacks/adsense
Description: Inserts Google Adsense inside your posts where you see fit.  Simple to use: Edit the adsense.php file to use your Adsense code instead of mine; Activate the plug-in;  Click the "Adsense" button on the quicktags bar to insert the Adsense marker in your posts.  Please make sure you read <a href="https://www.google.com/adsense/policies">Google's TOS</a> before using this plugin!
Author: Phil Hord
Author URI: http://philhord.com
*/ 

/* 
adsense
This function replaces <!--adsense--> tags with actual Google Adsense code
*/ 

function phord_insert_adsense($data) { 
    $tag = "<!--adsense-->"; 



    
/**************************************************************************
** Replace this HTML code with your own customized Google Adsense code.  **
**************************************************************************/

    $adsense_code = '
<!-- Begin Google Adsense code -->
<!-- Adjusted for Adsense Beautifier -->
<div class="randomhalfbanner">
<p>
<script type="text/javascript"><!--
google_ad_client = "pub-8787585795028617";
google_ad_width = 234;
google_ad_height = 60;
google_ad_format = "234x60_as";
google_ad_type = "text_image";
//2006-11-16: inline_banner
google_ad_channel = "7078773253";
google_color_link = "59708C";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</p>
</div>
<!-- End Google Adsense code -->
'; 

/**************************************************************************
** All done!  No more changes need to be made below this line!  Enjoy!   **
**************************************************************************/

 if( is_single() )
{
return str_replace( $tag, $adsense_code, $data );
}
elseif ( is_home() )
{
if ( get_option(”firstPost”) )
{
update_option(”firstPost”, FALSE);
return str_replace( $tag, $adsense_code, $data );
}
else
{
return str_replace( $tag, '', $data );
}
}
else
{
return str_replace( $tag, $adsense_code, $data );
}

//return str_replace( $tag, $adsense_code, $data );
} 

add_filter('the_content', 'phord_insert_adsense'); 


//---- The following AddAButton code comes from the Edit Button Template
//---- found here: http://codex.wordpress.org/Plugins
//---- and originally written by Owen Winkler
//---- It was originally here: http://www.asymptomatic.net/wp-hacks
//---- But I had trouble downloading it from there.

add_filter('admin_footer', 'phord_InsertAdsenseButton');

function phord_InsertAdsenseButton()
{
	if(strpos($_SERVER['REQUEST_URI'], 'post.php'))
	{
?>
<script language="JavaScript" type="text/javascript"><!--
var toolbar = document.getElementById("ed_toolbar");
<?php

		edit_insert_button("Adsense", "adsense_button", "Adsense");
	
?>

function adsense_button()
{
    edInsertContent(edCanvas, '<!-'+'-adsense-'+'->');
}

//--></script>
<?php
	}
}

if(!function_exists('edit_insert_button'))
{
	//edit_insert_button: Inserts a button into the editor
	function edit_insert_button($caption, $js_onclick, $title = '')
	{
	?>
	if(toolbar)
	{
		var theButton = document.createElement('input');
		theButton.type = 'button';
		theButton.value = '<?php echo $caption; ?>';
		theButton.onclick = <?php echo $js_onclick; ?>;
		theButton.className = 'ed_button';
		theButton.title = "<?php echo $title; ?>";
		theButton.id = "<?php echo "ed_{$caption}"; ?>";
		toolbar.appendChild(theButton);
	}
	<?php

	}
}
?>
