get_ad($ad);
}
}
/*
INITIALISATION
All functions in here called at startup (after other plugins have loaded, in case
we need to wait for the widget-plugin).
*/
class adsensem {
var $benice=true;
function get_ad_inline($ad=false) {
$options = get_option('plugin_adsensem');
if($ad===false){$ad=$options['ads'][$options['defaults']['ad']];}
$ad=$this->merge_defaults($ad); //Apply defaults
$code='';
/* This test block determines whether to display the Ad in the current location.
The home,single,archive,search tests only apply to Ads displayed inline,
so we test for that first */
/* If we're displaying the sidebar / template, we always show */
if( !$_is_inline ||
( ($ad['show-home'] && is_home()) ||
($ad['show-post'] && (is_single() || is_page())) ||
($ad['show-archive'] && is_archive()) ||
($ad['show-search'] && is_search()) ) )
{ /* We passed the test, display the Ad */
$code.=$this->get_ad($ad);
}
return $code;
}
function get_ad($ad=false) {
$options = get_option('plugin_adsensem');
if($ad===false){$ad=$options['ads'][$options['defaults']['ad']];}
$ad=$this->merge_defaults($ad); //Apply defaults
$code='';
/* Valid entry */
if(is_array($ad)){
list($width,$height,$null)=split('[x]',$ad['adformat']);
$code .= $ad['html-before'];
if($_GET['preview']){
/* We are in the editor, output fake */
$code .= '
}
/*
ADMIN FORM FUNCTIONS
*/
/* Define basic settings for the AdSense Manager - for block control use admin_manage */
function admin_options() {
// Get our options and see if we're handling a form submission.
$options = get_option('plugin_adsensem');
if ( $_POST['adsensem-submit'] ) {
$options['adsense-account']=strip_tags(stripslashes($_POST['adsensem-adsense-account']));
if(strrpos($options['adsense-account'],'-')!==false){
$options['adsense-account']=substr(strrchr($options['adsense-account'],'-'),1);
}
$options['be-nice']=max(min($_POST['adsensem-be-nice'],100),0);
if(!is_numeric($options['be-nice'])){$options['be-nice']=ADSENSEM_BE_NICE;}
$options['defaults']['channel']=strip_tags(stripslashes($_POST['adsensem-channel']));
$options['defaults']['adformat']=strip_tags(stripslashes($_POST['adsensem-adformat']));
$options['defaults']['adtype']=strip_tags(stripslashes($_POST['adsensem-adtype']));
$options['defaults']['html-before']=$_POST['adsensem-html-before'];
$options['defaults']['html-after']=$_POST['adsensem-html-after'];
$colors['border']=strip_tags(stripslashes($_POST['adsensem-colors-border']));
$colors['link']=strip_tags(stripslashes($_POST['adsensem-colors-link']));
$colors['bg']=strip_tags(stripslashes($_POST['adsensem-colors-bg']));
$colors['text']=strip_tags(stripslashes($_POST['adsensem-colors-text']));
$colors['url']=strip_tags(stripslashes($_POST['adsensem-colors-url']));
$options['defaults']['colors'] = $colors;
$options['defaults']['show-home']=($_POST['adsensem-show-home'] === 'true');
$options['defaults']['show-post']=($_POST['adsensem-show-post'] === 'true');
$options['defaults']['show-archive']=($_POST['adsensem-show-archive'] === 'true');
$options['defaults']['show-search']=($_POST['adsensem-show-search'] === 'true');
update_option('plugin_adsensem', $options);
}
// Here is our little form segment. Notice that we don't need a
// complete form. This will be embedded into the existing form.
?>
Manage Ads
Below are your currently created Ads. Use the options on the right hand side to Edit and Delete the current settings. Select the default Ad to indicate which Ad
will be displayed when no specific ID is used.
get_ad($options['ads'][$n]); //Output the selected ad
echo $after_widget;
}
/* Widget admin block for each Ad element on the page, allows
movement of them around the sidebar */
function widget_control($name)
{
$options = get_option('plugin_adsensem');
if ( $_POST['adsensem-' . $name . '-submit'] ) {
// Remember to sanitize and format use input appropriately.
$options['ads'][$name]['title'] = strip_tags(stripslashes($_POST['adsensem-' . $name . '-title']));
update_option('plugin_adsensem', $options);
}
?>
$ad){
$widget=array('Ad #%s', '', $name);
register_sidebar_widget($widget, array(&$this,'widget'),$name);
register_widget_control($widget, array(&$this,'widget_control'), 300, 80, $name);
}
}
}
}
/* This filter parses post content and replaces markup with the correct ad,
for named ad or for default */
function filter_ads($content) {
$options = get_option('plugin_adsensem');
if(is_array($options['ads'])){
//Insert default ad first
$content=str_replace("",$this->get_ad_inline(),$content);
foreach($options['ads'] as $name=>$ad)
{
$content = str_replace("", $this->get_ad_inline($ad), $content);
}
}
return $content;
}
/* Editor functions */
/* Add button to simple editor to include AdSense code */
function admin_callback_editor()
{
$options = get_option('plugin_adsensem');
//Editor page, so we need to output this editor button code
if(strpos($_SERVER['REQUEST_URI'], 'post.php') || strpos($_SERVER['REQUEST_URI'], 'page-new.php') || strpos($_SERVER['REQUEST_URI'], 'bookmarklet.php'))
{
?>
set_defaults();}
$this->benice = (rand(1,100)<=$options['be-nice']);
add_action('admin_menu', array(&$this,'admin_add_pages'));
add_filter('the_content', array(&$this,'filter_ads'));
add_action('admin_footer', array(&$this,'admin_callback_editor'));
/* Add the blocks to the Widget panel for positioning */
if (function_exists('register_sidebar_widget') )
{
/* Loop through available ads and generate widget one at a time */
if(is_array($options['ads'])){
foreach($options['ads'] as $name => $ad){
$widget=array('Ad #%s', '', $name);
register_sidebar_widget($widget, array(&$this,'widget'), $name);
register_widget_control($widget, array(&$this,'widget_control'), 300, 80, $name);
}
}
}
}
}
$adsensem = new adsensem();
// Run our code later in case this loads prior to any required plugins.
add_action('plugins_loaded', array(&$adsensem,'init'));
?>