$ad) {
if (!empty($ad->name)) {
$id = substr(md5($ad->name), 0, 10);
$widgets[$id] = $ad;
}
}
foreach ($widgets as $id => $ad)
{
$n = __('Ad: ', 'advman') . $ad->name;
$description = __('An ad from the Advertising Manager plugin');
$args = array(
'name' => $n,
'description' => $description,
//'width' => $ad->get('width', true),
//'height' => $ad->get('height', true),
);
if (function_exists('wp_register_sidebar_widget')) {
//$id, $name, $output_callback, $options = array()
wp_register_sidebar_widget("advman-$id", $n, array('advman','widget'), $args, $ad->name);
wp_register_widget_control("advman-$id", $n, array('advman','widget_control'), null, null, $ad->name);
} elseif (function_exists('register_sidebar_module') ) {
register_sidebar_module($n, 'advman_sbm_widget', "advman-$id", $args );
register_sidebar_module_control($n, array('advman','widget_control'), "advman-$id");
}
}
}
}
}
function add_notice($action,$text,$confirm=false)
{
global $_advman;
$_advman['notices'][$action]['text'] = $text;
$_advman['notices'][$action]['confirm'] = $confirm;
}
function remove_notice($action)
{
global $_advman;
if (!empty($_advman['notices'][$action])) {
unset($_advman['notices'][$action]); //=false;
}
}
function revert_db()
{
global $_advman;
$version = OX_Tools::sanitize_number($_REQUEST['advman-revert-db']);
$backup = get_option('plugin_adsensem_backup');
if (!empty($backup[$version])) {
$_advman = $backup[$version];
update_option('plugin_adsensem',$_advman);
if (!empty($_REQUEST['advman-block-upgrade'])) {
die();
}
} else {
echo __('It looks like you are trying to load a backup of Advertising Manager. The available versions are:') . ' ';
foreach (array_keys($backup) as $key) {
echo $key . ' ';
}
echo '
';
echo __('Here is a printout of each version:') . ' ';
foreach ($backup as $key => $data) {
echo "ADVMAN VERSION $key:
";
print_r($data);
echo '
';
}
die();
}
}
// This is the function that outputs advman widget.
function widget($args,$n='')
{
// $args is an array of strings that help widgets to conform to
// the active theme: before_widget, before_title, after_widget,
// and after_title are the array keys. Default tags: li and h2.
extract($args); //nb. $name comes out of this, hence the use of $n
global $_advman;
//If name not passed in (Sidebar Modules), extract from the widget-id (WordPress Widgets)
if ($n=='') {
$l = length(__('Ad: '));
$n = substr($args['widget_name'],$l); //Chop off beginning advman- bit
}
if ($n == 'default-ad') {
$n = $_advman['default-ad'];
}
$ad = advman::select_ad($n);
if (!empty($ad) && $ad->is_available()) {
echo $before_widget;
$id = substr(md5($ad->name), 0, 10);
if(!empty($_advman['settings']['widgets'][$id]['title'])) {
echo $before_title . $_advman['settings']['widgets'][$id]['title'] . $after_title;
}
advman::update_counters($ad);
echo $ad->get_ad(); //Output the selected ad
echo $after_widget;
}
}
function widget_control($args, $name)
{
global $_advman;
$widgets = $_advman['settings']['widgets'];
$id = substr(md5($name),0,10);
// Save data if it is posted from the widget control
if ( $_POST["advman-$id-submit"] ) {
$title = strip_tags(stripslashes($_POST["advman-$id-title"]));
$widgets[$id]['title'] = $title;
$_advman['settings']['widgets'] = $widgets;
update_option('plugin_adsensem', $_advman);
}
// Clean up any data from the widgets (e.g. if ads have been renamed)
if (!empty($widgets)) {
foreach ($widgets as $i => $w) {
$found = false;
foreach ($_advman['ads'] as $ad) {
$ai = substr(md5($ad->name), 0, 10);
if ($ai == $i) {
$found = true;
break;
}
}
if (!$found) {
unset($_advman['settings']['widgets'][$i]);
update_option('plugin_adsensem', $_advman);
}
}
}
// Display the widget options
$title = isset($widgets[$id]['title']) ? $widgets[$id]['title'] : '';
?>
get_ad();
}
return '';
}
function select_ad($name)
{
global $_advman;
// Find the ads which match the name
$ads = array();
$totalWeight = 0;
foreach ($_advman['ads'] as $id => $ad) {
if ( ($ad->name == $name) && ($ad->is_available()) ) {
$weight = $ad->get('weight', true);
if ($weight > 0) {
$ads[] = $ad;
$totalWeight += $weight;
}
}
}
// Pick the ad
// Generate a number between 0 and 1
$rnd = (mt_rand(0, PHP_INT_MAX) / PHP_INT_MAX);
// Loop through ads until the selected one is chosen
$wt = 0;
foreach ($ads as $ad) {
$wt += $ad->get('weight', true);
if ( ($wt / $totalWeight) > $rnd) {
// Display the ad
return $ad;
}
}
}
/* This filter parses post content and replaces markup with the correct ad,
for named ad or for default */
function filter_ads($content)
{
global $_advman;
if (!empty($_advman['default-ad'])) {
$content = preg_replace_callback(array("//","//","/\[ad\]/"),array('advman','filter_ad_callback'),$content);
}
$content = preg_replace_callback(array("//","//","/\[ad#(.*?)\]/"),array('advman','filter_ad_callback'),$content);
return $content;
}
function update_counters($ad)
{
global $_advman_counter;
if (!empty($ad)) {
if (empty($_advman_counter['id'][$ad->id])) {
$_advman_counter['id'][$ad->id] = 1;
} else {
$_advman_counter['id'][$ad->id]++;
}
if (empty($_advman_counter['network'][$ad->network])) {
$_advman_counter['network'][$ad->network] = 1;
} else {
$_advman_counter['network'][$ad->network]++;
}
}
}
}
// SHOW ADS
if (!function_exists('adsensem_ad')) {
function adsensem_ad($name = false)
{
return advman_ad($name);
}
}
function advman_ad($name = false)
{
global $_advman;
if (empty($name)) { /* default ad */
$name = $_advman['default-ad'];
}
$ad = advman::select_ad($name);
if (!empty($ad)) {
advman::update_counters($ad);
echo $ad->get_ad();
}
}
// SHOW AN AD BY ITS NAME
if (!empty($_REQUEST['advman-ad-name'])) {
$advman_name = OX_Tools::sanitize_key($_REQUEST['advman-ad-name']);
advman_ad($advman_name);
die(0);
}
// SHOW AN AD BY ID
if (!empty($_REQUEST['advman-ad-id'])) {
$advman_id = OX_Tools::sanitize_number($_REQUEST['advman-ad-id']);
if (!empty($_advman['ads'][$advman_id])) {
$advman_ad = $_advman['ads'][$advman_id];
advman::update_counters($advman_ad);
echo $advman_ad->get_ad();
}
die(0);
}
// END
if (is_admin()) {
require_once(ADVMAN_PATH . '/class-admin.php');
// Revert to a previous version of database
if (isset($_REQUEST['advman-revert-db'])) {
advman::revert_db();
}
/* PRE-OUTPUT PROCESSING - e.g. NOTICEs (upgrade-adsense-deluxe) */
if (!empty($_POST['advman-mode'])) {
$advman_mode = OX_Tools::sanitize_key($_POST['advman-mode']);
if ($advman_mode == 'notice') {
$advman_action = OX_Tools::sanitize_key($_POST['advman-action']);
switch ($advman_action) {
case 'upgrade adsense-deluxe':
if ($_POST['advman-notice-confirm-yes']) {
require_once(ADVMAN_PATH . '/class-upgrade.php');
advman_upgrade::adsense_deluxe_to_3_0();
advman::remove_notice('upgrade adsense-deluxe');
} else {
advman::remove_notice('upgrade adsense-deluxe');
}
update_option('plugin_adsensem', $_advman);
break;
case 'optimise':
$advman_yes = isset($_POST['advman-notice-confirm-yes']);
if ($advman_yes) {
advman_admin::_set_auto_optimise(true);
} else {
advman_admin::_set_auto_optimise(false);
}
advman::remove_notice('optimise');
update_option('plugin_adsensem', $_advman);
break;
case 'activate advertising-manager':
advman::remove_notice('activate advertising-manager');
update_option('plugin_adsensem', $_advman);
break;
}
}
}
/* END PRE-OUTPUT PROCESSING */
}
/* SIDEBAR MODULES COMPATIBILITY FUNCTION */
function advman_sbm_widget($args)
{
global $k2sbm_current_module;
advman::widget($args,$k2sbm_current_module->options['name']);
}
/* SIDEBAR MODULES COMPATIBILITY FUNCTION */
if (class_exists('adsensem')) {
advman::add_notice('disable adsensem', __('Please disable Adsense Manager before using Advertising Manager'), 'ok');
update_option('plugin_adsensem', $_advman);
} else {
add_action('plugins_loaded', array('advman','init'), 1);
}
?>