';
}
/**
*** administer_translate ( )
**/
function administer_translate(){
// Load a language
load_plugin_textdomain('p2m-ad-manager', PLUGINDIR . '/' . dirname(plugin_basename (__FILE__)) );
}
/**
*** administer_translate ( )
***
*** Enable one-click xml export.
**/
function administer_export () {
global $post_ids;
if ($_GET['administer'])
$post_ids = array(get_option('administer_post_id'));
}
/**
*** p2m_load_widgets ( )
***
*** Create the widgets...
**/
function administer_load_widgets () {
if (!($post_id = get_option('administer_post_id'))) return 0;
$positions = get_post_meta($post_id, 'administer_positions', true);
if (!is_array($positions) || empty($positions)) return 0;
foreach ($positions as $position) {
if ($position['type'] == 'widget') {
// Create a whiddgett!
$options = array('title' => $position['position']);
register_sidebar_widget('Ad: ' . $position['position'], 'administer_widget', $options);
$dims = array('width' => 400, 'height' => 200, 'params' => array('nbr' => $nbr) );
wp_register_widget_control('Ad: ' . $position['position'], $position['position'], 'administer_widget_control', $dims);
}
}
}
/**
*** p2m_widget_control ( )
***
*** I'm gonna inject the controls using javascript instead, since the widgets
*** are auto-generated. Might want to add something though.
**/
function administer_widget_control() { }
/**
*** administer_popuplate_widget_controls ( )
***
*** Inject the content of a position into a widget control, using generated JS.
**/
function administer_popuplate_widget_controls () {
global $wp_registered_widgets;
// print_r($wp_registered_widgets);
$widgets = array_keys($wp_registered_widgets);
$incpath = get_option('siteurl') . '/' . WPINC;
foreach ($wp_registered_widgets as $widget) {
if ($widget['callback'] == 'administer_widget') {
$name = str_replace('Ad: ', '', $widget['name']);
?>
$position, 'description' => '', 'before' => '', 'after' => '', 'type' => 'template');
$args = wp_parse_args($args, $defaults);
// Ignore empty calls
if (!$args['position']) return '';
$positions = get_post_meta($post_id, 'administer_positions', true);
if (!is_array($positions)) {
$positions = array();
$edit_position = true;
} else {
if (array_key_exists($args['position'], $positions)) {
$diff = array_diff($positions[$args['position']], $args);
if (!empty($diff)) $edit_position = true;
else $edit_position = false;
}
else $edit_position = true;
}
// If anything has changed, then update our database
if ($edit_position) {
$positions[$args['position']] = $args;
// Save to a Custom Field
if (!add_post_meta($post_id, 'administer_positions', $positions, true))
update_post_meta($post_id, 'administer_positions', $positions);
}
administer_display_position($args['position']);
}
function administer_display_position ($pos) {
// Display the content
if (!($post_id = get_option('administer_post_id'))) return 0;
$content = get_post_meta($post_id, 'administer_content', true);
$positions = get_post_meta($post_id, 'administer_positions', true);
if (!is_array($content) || empty($content)) return 0;
$valids = array();
$total_weight = 0;
foreach($content as $ad) {
// Skip if this content if it does not belong here.
if ($ad['position'] != $pos) continue;
// Is the option to show the content ticked.
if ($ad['show'] == 'false') continue;
// Is the content schedueled to show?
$valid = false;
$ages = adminiseter_content_age($ad['scheduele']);
foreach ($ages as $age) {
// No scheduele, so content always valid
if (!$age['start'] && !$age['end']) $valid = true;
// Check that we're in the validity period
if ($age['start'] < 0 && $age['end'] > 0) $valid = true;
}
// Sum the weights if the content is valid
if ($valid) {
array_push($valids, $ad);
// The default weight is one.
if (!$weight = $ad['weight']) $total_weight += 1;
$total_weight += $ad['weight'];
}
}
// Make sure we have some valid content
if (count($valids) == 0) return 0;
// Get a pseudo random number
$nbr = rand(1, $total_weight);
// Get the randomized bit of content from our stack of valid content
$total_weight = 0;
foreach($valids as $ad) {
if (!$weight = $ad['weight']) $total_weight += 1;
$total_weight += $weight;
if ($nbr <= $total_weight) {
// Make amersands validate
$code = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&$1', $ad['code']);
$code = stripslashes($code);
// Display the content code with optional wrapping.
if ($ad['wrap'] != 'false') echo $positions[$pos]['before'];
echo $code;
if ($ad['wrap'] != 'false') echo $positions[$pos]['after'];
break;
}
}
}
?>