$ad) { if (!empty($ad->name)) { $widgets[$ad->name] = $ad; } } foreach ($widgets as $name => $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-$name", "Ad#$name", array('advman','widget'), $args, $name); // wp_register_widget_control("advman-$name", "Ad#$name", array('advman','widget_control'), $args, $name); } elseif (function_exists('register_sidebar_module') ) { register_sidebar_module("Ad #$name", 'advman_sbm_widget', "advman-$name", $args ); // register_sidebar_module_control("Ad #$name", array('advman','widget_control'), "advman-$name"); } } } } } 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; } } // 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=='') { $n = substr($args['widget_id'],9); //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; if($ad->title != '') { echo $before_title . $ad->title . $after_title; } echo $ad->get_ad(); //Output the selected ad echo $after_widget; } } /** * Called when the Wordpress footer displays, and adds a comment in the HTML for debugging purposes */ function footer() { ?> 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()) ) { $ads[] = $ad; $totalWeight += $ad->get('weight', true); } } // 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 PREVIOUS BACKUP OF AD DATABASE */ if (!empty($_REQUEST['advman-revert-db'])) { $advman_version = OX_Tools::sanitize_number($_REQUEST['advman-revert-db']); $advman_backup = get_option('plugin_adsensem_backup'); if (!empty($advman_backup[$advman_version])) { $_advman = $advman_backup[$advman_version]; update_option('plugin_adsensem',$_advman); if (!empty($_REQUEST['advman-block-upgrade'])) { die(); } } } /* END REVERT TO PREVIOUS BACKUP OF AD DATABASE */ /* 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 */ add_action('plugins_loaded', array('advman','init'), 1); ?>