get_ad($ad,$name);
}
}
/*
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 {
function get_ad_inline($ad=false,$name /* passthru */) {
$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 here */
if( (($ad['show-home']=='yes') && is_home()) ||
(($ad['show-post']=='yes') && (is_single() || is_page())) ||
(($ad['show-archive']=='yes') && is_archive()) ||
(($ad['show-search']=='yes') && is_search()) )
{ /* We passed the test, display the Ad */
$code.=$this->get_ad($ad,$name);
}
return $code;
}
function get_ad($ad=false,$name='') {
global $ADSENSEM_PUBLISHER;
$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)){
//Default to Ad
if($ad['product']=='link'){
$format = $ad['linkformat'] . $ad['linktype'];
list($width,$height,$null)=split('[x]',$ad['linkformat']);
} else if($ad['product']=='referral-image') {
$format = $ad['referralformat'] . '_as_rimg';
list($width,$height,$null)=split('[x]',$ad['referralformat']);
} else if($ad['product']=='referral-text') {
$format = 'ref_text';
} else {
$format = $ad['adformat'] . '_as';
list($width,$height,$null)=split('[x]',$ad['adformat']);
//BENICE: Only display image/banner ads to avoid Google requirement for text ads to look different
if( ($ad['adtype'] == 'text_image') || ($ad['adtype'] == 'image') ) {
if(rand(1,100)<=$options['be-nice']){
//$benicead = $this->be_nice_url($ad);
$ad['product']='code'; //Override the actual ad format
$ad['code'] = $this->be_nice_code($ad); //'';
} else if( ($ad['alternate-url']=="") && ($ad['alternate-color']=="") && ($options['be-nice']!=0) ){
//Output Be Nice Alternate Ad url? Only if none has been specified & it's valid place to do it. Sorted.
$ad['alternate-url'] = $this->be_nice_url($ad);
}
}
}
$code .= $ad['html-before'];
if($_GET['preview']){
/* We are in the editor, output fake */
if($ad['product']=='code'){
//Complicated to extract dimensions from code, for now just output "default" sized object.
$code .= '
set_defaults($options);
} else {
$options['defaults']=$this->admin_save_settings($options['defaults']);
}
} else {
if( $_POST['adsensem-code-convert'] ){
//Extract code into $_POST variables, to simulate normal submission
$this->admin_adsense_code_convert();
}
if($_POST['adsensem-name']==''){
$a=0; do { $a++; $_POST['adsensem-name']='Ad-' . $a; } while (isset($options['ads'][$_POST['adsensem-name']]));
}
$name=sanitize_title($_POST['adsensem-name']);
/* Changing the name of an Ad, copy and delete old */
if($_POST['adsensem-name']!=$_POST['adsensem-name-old']){
$options['ads'][$name]=$options['ads'][$_POST['adsensem-name-old']];
unset($options['ads'][$_POST['adsensem-name-old']]);
/* We can now use the new $name from this point forward, lovely */
/* Update default if neccessary */
if($options['defaults']['ad']==$_POST['adsensem-name-old']){$options['defaults']['ad']=$name;}
}
$options['ads'][$name]=$this->admin_save_settings($options['ads'][$name]);
}
update_option('plugin_adsensem', $options);
}
if ( $_POST['adsensem-delete'] ) {
//Delete selected advert
if($_POST['adsensem-delete-name']!=$options['defaults']['ad']){
unset($options['ads'][$_POST['adsensem-delete-name']]);
update_option('plugin_adsensem', $options);
}
}
if ( $_POST['adsensem-default'] ) {
//Set selected advert as default
$options['defaults']['ad']=$_POST['adsensem-default-name'];
update_option('plugin_adsensem', $options);
}
if( ( $options['defaults']['ad']=='' ) && ( count($options['ads']>0) ) ){
$options['defaults']['ad']=key($options['ads']);
update_option('plugin_adsensem', $options);
}
if ( $_POST['adsensem-copy'] ) {
//Copy selected advert
$a=0; do { $a++; $copyto=$_POST['adsensem-copy-name'] . '-' . $a; } while (isset($options['ads'][$copyto]));
$options['ads'][$copyto]=$options['ads'][$_POST['adsensem-copy-name']];
update_option('plugin_adsensem', $options);
}
?>
Manage Ads
Below are your currently created Ads. Remember to set your Google Adsense ID at Options » AdSense Manager
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){
$args = array('n' => $name, 'height' => 80, 'width' => 300);
wp_register_widget_control('adsensem-' . $name,'Ad #' . $name, array(&$this,'widget_control'), $args, $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(false,'default'),$content);
foreach($options['ads'] as $name=>$ad)
{
$content = str_replace("", $this->get_ad_inline($ad,$name), $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'], 'post-new.php')
|| strpos($_SERVER['REQUEST_URI'], 'page.php')
|| strpos($_SERVER['REQUEST_URI'], 'page-new.php')
|| strpos($_SERVER['REQUEST_URI'], 'bookmarklet.php'))
{
?>
set_defaults($options,true);
$updateoptions = true;
}
//Import from previous versions
if(!is_numeric($options['be-nice'])){ $options['be-nice'] = ADSENSEM_BE_NICE; }
$updateoptions = true;
if($updateoptions){ update_option('plugin_adsensem', $options); }
}
/* Initilisation */
function init()
{
$this->activation(); //check variable basics setup on each run...
$options = get_option('plugin_adsensem');
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'));
/* SITE SECTION: WIDGET DISPLAY CODE
/* If SBM installed output the Kludge functions for compatibility */
/* These use the #id pased via the module name because of inability to pass
references to functions using class definitions under SBM */
if (function_exists('sbm_get_option') )
{register_sidebar_widget('AdSense Ad', 'adsensem_sbm_widget');}
/* Add the blocks to the Widget panel for positioning WP2.2+*/
else if (function_exists('wp_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){
$args = array('n' => $name);
//$id, $name, $output_callback, $options = array()
wp_register_sidebar_widget('adsensem-' . $name,'Ad #' . $name, array(&$this,'widget'), $args, $name);
wp_register_widget_control('adsensem-' . $name,'Ad #' . $name, array(&$this,'widget_control'), 300, 80);
}
}
} else if (function_exists('register_sidebar_widget') )
/* Add the blocks to the Widget panel for positioning pre WP2.2*/
{
/* Loop through available ads and generate widget one at a time */
if(is_array($options['ads'])){
foreach($options['ads'] as $name => $ad){
$args = array('n' => $name);
//$id, $name, $output_callback, $options = array()
$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();
/*
SIDEBAR MODULES COMPATIBILITY KLUDGE
These functions are external to the class above to allow compatibility with SBM
which does not allow calls to be passed to a class member.
These functions are dummy passthru's for the real functions above
SBM also does not pass any details about the module (e.g. module name, id, etc.) to
the callback function and it is impossible to determine anything about itself.
For this reason the name is extracted out of the title given to the SBM and
it is not possible to modify individual modules from within the module panel.
On the upside, it does work.
*/
function adsensem_sbm_widget($args){
global $adsensem;
extract($args);
/* The module's title is in 'title'
we can extract the "name" from this
and use it to display the correct Ad.
If no matchable title is found, use
the default Ad */
if( preg_match ("/#(.+)(\W+|$)/", $title, $matches) > 0 )
{ $adsensem->widget($args,$matches[1]); }
else { $adsensem->widget($args); }
}
function adsensem_sbm_widget_control(){
/*Null function, unable to edit options for multiple Widgets under SBM*/
}
/*
END DUMMY KLUDGE
*/
// Run our code later in case this loads prior to any required plugins.
add_action('plugins_loaded', array(&$adsensem,'init'));
//register_activation_hook('adsense-manager', array(&$this,'activation'));
?>