AdSense ads in your WordPress Posts. Requires WordPress 1.5 or higer. For complete usage and configuration click on AdsenseDeluxe under the "Options" menu.
Author: Acme Technologies
Author URI: http://www.acmetech.com/
*/
//error_reporting(E_ERROR | E_WARNING | E_PARSE);
//--
//-- Version of the plugin
//--
$__ADSENSE_DELUXE_VERSION__ = "0.8";
//--
//-- You can select in the Adsense-Deluxe options page to give something back to this
//-- plugin's author (me) by having 5% of the ads shown on your WP blog use my adsense
//-- client ID. This is DISABLED by default, and I assure you I do nothing in the code
//-- to subversively turn it on! The way it works is if you enable the option (and it's
//-- just as easily disabled...), approximately 5% of the time an adsense ad block is
//-- displayed, it will use my AdSense client-id, and if someone happens to click one of
//-- those ads, I benefit from it and you've helped encourage me to continue supporting
//-- this plugin. If you're going to enable this option, you can make me feel even happier
//-- posting a comment on the blog page for this plugin to let mee know, and I can
//-- personally thank you...
//-- http://www.acmetech.com/blog/2005/07/26/adsense-deluxe-wordpress-plugin/
//--
$__ACMETECH_CLIENT_ID__ = "pub-6179066220764588";
$__ACMETECH_AD_PARTNER__ = "1881826992";
//--
//-- CONSTANTS
//--
define('ADSDEL_OPTIONS_ID', 'acmetech_adsensedeluxe');
//--
//-- OUTPUTS debugging info in html comments on blog pages.
//--
$__AdSDelx_Debug__ = false;
//--
//-- If set to false, live adsense ads displayed in Post editing preview
//--
$__AdSDelx_USE_PREV_PLACEHOLDER = true;
/*
adsense-deluxe
This function replaces or tags with actual Google Adsense code
*/
if (function_exists('is_plugin_page') && is_plugin_page()) :
AdsenseDeluxeOptionsPanel(); // check here to see if the broken 1.5 options page feature is fixed
else :
function adsense_deluxe_insert_ads($data) {
global $__AdSDelx_USE_PREV_PLACEHOLDER,
$__ACMETECH_CLIENT_ID__,
$__ACMETECH_AD_PARTNER__,
$doing_rss, /* will be true if getting RSS feed */
$_adsdel_adcount; /* tracks number of posts we've processed on home page */
$MAX_ADS_PER_PAGE = 3; // MAX # of AdSense ads to allow on a given page
$EDITING_PAGE = false;
$PLACEHOLDER = '<!--@@-->';
$PLACEHOLDER_DISABLED = '<!--@@-->';
/*
* For format of $options, see _AdsDel_CreateDefaultOptions()
*
*/
$options = get_option(ADSDEL_OPTIONS_ID);
//-- see if global switch is off
if( ! $options['all_enabled'] ){
return "\n\n" . $data;
}
// NO ADSENSE IN FEEDS!
if($doing_rss){
//return "\n\n" . $data;
return $data;
}
if( strstr($_SERVER['PHP_SELF'], 'post.php') ){
// user is editing a page or post, show placeholders, not real ads
$EDITING_PAGE = ($__AdSDelx_USE_PREV_PLACEHOLDER ? true : false);
}
// set up some variables we need
$patts = array();
$subs = array();
$default = $options['default'];
$rewardAut = $options['reward_author'];
$qualifer = '';
$msg = "\n";
$msg .= "\n"; //DEBUGGING
$msg .= "\n"; //DEBUGGING
$msg .= "\n"; //DEBUGGING
$msg .= "\n"; //DEBUGGING
if( isset($_adsdel_adcount) )
$msg .= "\n"; //DEBUGGING
//-- fill in stuff to search for ($patts) and substition blocks ($subs)
foreach( $options['ads'] as $key => $vals ){
if( $key == $default ){
$msg .= "\n\n"; //DEBUGGING
$patts[] = "";
$subs[] = ($vals['enabled'] ? stripslashes($vals['adsense']) : "\n");
if($EDITING_PAGE) $subs[ sizeof($subs)-1] = str_replace('@@', 'adsense', ($vals['enabled'] ? $PLACEHOLDER : $PLACEHOLDER_DISABLED));
}
$msg .= "\n"; //DEBUGGING
$patts[] = "";
$subs[] = ($vals['enabled'] ? stripslashes($vals['adsense']) : "");
if($EDITING_PAGE) $subs[ sizeof($subs)-1] = str_replace('@@', 'adsense#'.$key, ($vals['enabled'] ? $PLACEHOLDER : $PLACEHOLDER_DISABLED));
}
if( rand(0, 100) >= 95 && ! $EDITING_PAGE && $rewardAut ){
if( is_single() || is_page() ){
$msg .= "\n"; //DEBUGGING
$subbed = preg_replace ( '/pub-[0-9]+/', $__ACMETECH_CLIENT_ID__, $subs );
$subs = preg_replace ( '/google_ad_channel *= *\"[^"]*\"/', 'google_ad_channel = "1478884331"', $subbed );
$subbed = preg_replace ( '/ctxt_ad_partner *= *\"[^"]*\"/', 'ctxt_ad_partner = "' . $__ACMETECH_AD_PARTNER__ . '"', $subs );
$subs = preg_replace ( '/ctxt_ad_section *= *\"[^"]*\"/', 'ctxt_ad_section = "20007"', $subbed );
}
}
// check that post contains adsense token so we can count # of times
// we've shown ads in this page load
$matchCount = 0;
$matchCount = preg_match_all ( "//", $data, $matches , PREG_PATTERN_ORDER );
$show_ads = false;
$msg .= "\n"; //DEBUGGING
if( $matchCount > 0 ){
//--
//-- Have to take into account the fact that perhaps we've already shown
//-- 2 ads for a page (not necessarily a single post page), but the current $data
//-- contains 2 or more placeholder comments.
//-- Since replacements in $data are done en_masse, we might go
//-- over our limit for this post, but but we'll prefer that over
//-- not showing at least $MAX_ADS_PER_PAGE ad blocks.
//--
$show_ads = true;
if( ! isset($_adsdel_adcount) ){
$_adsdel_adcount = $matchCount;
}else{
if( $_adsdel_adcount > $MAX_ADS_PER_PAGE )
$show_ads = false;
$_adsdel_adcount+=$matchCount;
}
}
if( $show_ads )
{
// NOTE: might have to use ksort() on patts,subs if wrong blocks are being subbed in.
if( is_single() )
{
if( $options['enabled_for']['posts'] )
return str_replace($patts, $subs, $data); //. $msg;
return $data;
}
elseif ( is_home() )
{
$msg .= "\n"; //DEBUGGING
$msg .= "\n"; //DEBUGGING
if( $options['enabled_for']['home'] )
return str_replace($patts, $subs, $data);
return $data;
}
elseif( is_page() )
{
$msg .= "\n"; //DEBUGGING
if( $options['enabled_for']['page'] )
return str_replace($patts, $subs, $data);
return $data;
}
elseif( is_archive() )
{
$msg .= "\n"; //DEBUGGING
if( $options['enabled_for']['archives'] )
return str_replace($patts, $subs, $data);// .$msg;
return $data;
}
elseif( is_search() )
{
$msg .= "\n"; //DEBUGGING
if( $options['enabled_for']['archives'] )
return str_replace($patts, $subs, $data);
return $data; // . $msg;
}
else
{
$msg .= "\n"; //DEBUGGING
return str_replace($patts, $subs, $data); // . $msg;
//return str_replace( $tag, '', $data );
}
}else{// if( $show_ads )
return $data ; //. $msg;
}
} // function adsense_deluxe_insert_ads(...)
/*
* Can be used outside the loop. Prints the adsense code for a named Ad block.
* Leave the parameter empty to output the default block.
* example: for a block named "blue_banner", call adsense_deluxe_ads("blue_banner");
* or within your templates, use
*/
function adsense_deluxe_ads($adname='') {
global $__AdSDelx_USE_PREV_PLACEHOLDER,
$_adsdel_adcount; /* tracks number of posts we've processed on home page */
$MAX_ADS_PER_PAGE = 3; // MAX # of AdSense ads to allow on a given page
$EDITING_PAGE = false;
/*
* For format of $options, see _AdsDel_CreateDefaultOptions()
*
*/
$options = get_option(ADSDEL_OPTIONS_ID);
//-- see if global switch is off
if( ! $options['all_enabled'] ){
echo "\n\n";
return;
}
// set up some variables we need
$patts = array();
$subs = array();
$default = $options['default'];
if( $adname == '' )
$adname = $default;
$show_ads = true;
$msg = "\n";
//-- locate ad block
foreach( $options['ads'] as $key => $vals ){
if( $key == $adname ){
$msg .= "\n";
if( ! isset($_adsdel_adcount) ){
$_adsdel_adcount = 0;
}else{
if( $_adsdel_adcount > $MAX_ADS_PER_PAGE )
$show_ads = false;
}
$_adsdel_adcount+=1;
$msg .= "\n"; //DEBUGGING
//echo $msg;
if( $show_ads )
echo ($vals['enabled'] ? stripslashes($vals['adsense']) : "");
return;
}
}
$msg .= "\n";
echo $msg;
} // function adsense_deluxe_ads(...)
function add_adsense_deluxe_handle_head()
{
global $__ADSENSE_DELUXE_VERSION__;
echo "\n".'' . "\n";
}
function _AdsDel_GetVersion(){
global $__ADSENSE_DELUXE_VERSION__;
return $__ADSENSE_DELUXE_VERSION__;
}
function _AdsDel_FormatVersion(){
return "v" . _AdsDel_GetVersion() . "";
}
function _AdsDel_DisplayAvailUpdate($pi_vers=0.0)
{
$pi_vers+=0.0;
$options = get_option(ADSDEL_OPTIONS_ID);
// NEXT LINE ONLY FOR TESTING CODE, just ignore...
//unset($options['next_update_check']); unset($options['latest_version']); update_option(ADSDEL_OPTIONS_ID, $options); return '';
if( isset($options) ){
$check = $options['next_update_check'];
if( time() > (integer)$check ){
$next_week = time() + (7 * 24 * 60 * 60);
$options['next_update_check'] = $next_week;
$new_vers = _AdsDel_VersionCheck();
if( $new_vers != '' ){
$options['latest_version'] = floatval($new_vers);
}else{
$options['latest_version'] = floatval($pi_vers);
}
update_option(ADSDEL_OPTIONS_ID, $options);
}
}
if( isset($options) && isset($options['latest_version']) ){
$new_vers = $options['latest_version'];
if( floatval($options['latest_version']) > $pi_vers ){
return " DOWNLOAD LATEST UPDATE (v$new_vers)";
}
}else{
return '';
}
}
function _AdsDel_VersionCheck()
{
$string = '';
$url = "http://software.acmetech.com/wordpress/plugins/adsense-deluxe-version.txt";
$url = parse_url ($url);
if ($handle = @fsockopen ($url['host'], 80,$errno, $errstr,10)) {
fwrite ($handle, "GET $url[path]?$url[query] HTTP/1.0\r\nHost: $url[host]\r\nConnection: Close\r\n\r\n");
while (!feof($handle)) {
$string .= @fread($handle, 30);
}
$string = explode ("
", $string);
$string = array_pop ($string);
$string = trim($string);
}
fclose($handle);
return 0+$string; // convert to float
}
/*
**
** Create default set of options and add to database
**/
function _AdsDel_CreateDefaultOptions()
{
$ADSDEL_OPTIONS_ID = 'acmetech_adsensedeluxe';
$options = array();
$options['version'] = (string)_AdsDel_GetVersion(); //this is a string but casting it anyway
$options['next_update_check'] = time(); // when to check for update to plugin next.
$options['all_enabled'] = true; // controls whether all ads on/off; can also disable at ad-level
//-- control whether ads are enabled for specific areas:
//-- individual posts, Pages, home page or any archive page
$options['enabled_for'] = array('home' => true,'posts' => true,'page'=>true,'archives' =>true);
$options['default'] = NULL; // always have to check against NULL for default.
$options['reward_author'] = false; // DO NOT reward author with 5% of adsense impressions
$options['ads'] = array();
add_option(ADSDEL_OPTIONS_ID, $options, 'Options for AdSense-Deluxe from www.acmetech.com');
return $options;
}
function _AdsDel_CheckOptions($o)
{
if( ! isset($o['all_enabled']) )
$o['all_enabled'] = true;
if( ! isset($o['ads']) )
$o['ads'] = array();
if( ! isset($o['default']) )
$o['default'] = NULL;
if( ! isset($o['reward_author']) )
$o['reward_author'] = false; // DEFAULT IS TO not REWARD PLUGIN AUTHOR...
foreach( $options['ads'] as $key => $vals ){
if( ! isset($vals['enabled']) )
$o['ads'][$key]['enabled'] = true;
if( ! isset($vals['desc']) )
$o['ads'][$key]['desc'] = '(No Description)';
}
}
/*
**
** Output Top of Options page.
**/
function _AdsDel_Header()
{
global $__ADSENSE_DELUXE_VERSION__;
$get_url = $_SERVER[PHP_SELF] . '?page=' . basename(__FILE__);
$def_url = $get_url . "&fn=debug";
echo "\n