Ad-minister

'; } function administer_content_template ($content, $index = 0) { echo administer_get_content_template($content, $index); } function administer_get_content_template ($content, $index = 0) { $url = get_option('siteurl') . '/' . PLUGINDIR . '/' . dirname(plugin_basename (__FILE__)); $nbr = $content['id']; // Get the position name $position = stripslashes($content['position']); //p2m_meta('ad_position_' . $nbr); if (!$position) $position = 'key_not_set'; $show_checked = ($content['show'] == 'true') ? ' checked="checked"' : ''; $title_edit = stripslashes($content['title']); $title_show = ($title_edit) ? $title_edit : 'Title not set'; // Handle Weight, handled internally as quota. $weight = ($content['weight']) ? $content['weight'] : 'Automatic'; $scheduele = $content['scheduele']; //p2m_meta('ad_scheduele_' . $nbr); $scheduele_div = ($scheduele = str_replace(':', ' to ', $content['scheduele'])) ? '| Scheduele: ' . $scheduele . '': ''; $wrap_checked = ($content['wrap'] == 'true') ? ' checked="checked"' : ''; $code = stripslashes($content['code']); $class = ($index % 2) ? 'odd' : 'even'; $html = '
Edit | Delete
' . $title_show . '
| Weight: ' . $weight . ' ' . $scheduele_div . '
' . $code .'
'; $html = str_replace('%nbr%', $nbr, $html); $html = str_replace('%class%', $class, $html); return $html; } // Position key: | Save function administer_position_template ($position = array(), $nbr = 0) { echo administer_get_position_template($position, $nbr); } function administer_get_position_template ($position = array(), $nbr = 0) { $url = get_option('siteurl') . '/' . PLUGINDIR . '/' . dirname(plugin_basename (__FILE__)); $key = $position['position']; // p2m_meta('position_key_' . $nbr); $desc = $position['description']; //p2m_meta('position_desc_' . $nbr); // $schecked = (p2m_meta('position_widget_' . $nbr) == 'true') ? ' checked="checked"' : ''; // Set up css formatting $class = ($nbr % 2) ? 'odd' : 'even'; if ($nbr == 9999) $class = 'new'; $style_view = (!$key) ? ' style="display: none;"': ''; $style_edit = (!$key) ? '' : ' style="display: none;"'; $html = ' %key% %desc% ' . htmlentities($position['before']) . ' ' . htmlentities($position['after']) . ' %edit_link% Remove Name: Description: Code before: Code after: ' . $type . ' Save '; // Inject template values $edit_link = ($position['type'] != 'template') ? 'Edit |' : ''; $html = str_replace('%edit_link%', $edit_link, $html); $html = str_replace('%nbr%', $nbr, $html); $html = str_replace('%key%', $key, $html); $html = str_replace('%desc%', $desc, $html); $html = str_replace('%url%', $url, $html); $html = str_replace('%class%', $class, $html); return $html; } /* ** administer_position_select () ** ** Generate the select list for the defined positions */ function administer_position_select ($nbr = '', $value = '') { // $saved = administer_get_saved_numbers('position_key_'); $html = ''; return $html; } /* ** p2m_nbr_to_save() ** ** Finds the highest number from zero that is not currently ** some content. */ function administer_nbr_to_save($prefix = 'ad_code_') { $content = get_post_meta(get_option('administer_post_id'), 'administer_content', true); if (!is_array($content)) return '0'; if (empty($content)) return '0'; // Store the ids in a separate array $ids = array_keys($content); sort($ids); // Get the smallest unpopulated id for ($i = 0; $i < $ids[count($ids) - 1] + 2; $i++) if ($i != $ids[$i]) return strval($i); } /* ** ok_to_go() ** ** Checks if the supplied post/page ID exists. */ function administer_ok_to_go() { $the_page = get_page(get_option('administer_post_id')); $ok_to_go = ($the_page->post_title) ? true : false; return $ok_to_go; } /** *** administer_delete_position ( ) *** *** Ajax function **/ function administer_delete_position () { // Parse and mould the ajax data. $args = administer_parse_ajax_data(); $position = $args['position']; $positions = get_post_meta(get_option('administer_post_id'), 'administer_positions', true); unset($positions[$position]); update_post_meta(get_option('administer_post_id'), 'administer_positions', $positions); if ($notification_domid = $_REQUEST['notification_domid']) die("p2m_ready('${notification_domid}');"); } /** *** administer_delete_content ( ) *** *** Ajax function. **/ function administer_delete_content () { // Parse and mould the ajax data. $args = administer_parse_ajax_data(); $id = $args['id']; $content = get_post_meta(get_option('administer_post_id'), 'administer_content', true); unset($content[$id]); update_post_meta(get_option('administer_post_id'), 'administer_content', $content); if ($notification_domid = $_REQUEST['notification_domid']) die("p2m_ready('${notification_domid}');"); } /** *** administer_save_content ( ) *** *** Ajax function. **/ function administer_save_content () { // Parse and mould the ajax data. $content_new = administer_parse_ajax_data(); $default = array('title' => '', 'code' => '', 'scheduele' => '', 'weight' => '', 'id' => '', 'show' => '', 'wrap' => '', 'position' => ''); $content_new = array_intersect_key($content_new, $default); // Replace the data $content = get_post_meta(get_option('administer_post_id'), 'administer_content', true); $content[$content_new['id']] = $content_new; // Save data to DB update_post_meta(get_option('administer_post_id'), 'administer_content', $content); if ($notification_domid = $_REQUEST['notification_domid']) die("p2m_ready('${notification_domid}');"); } /** *** administer_parse_ajax_data ( ) *** *** Re-structure the variables passed through xmlhttprequest into a associative array. **/ function administer_parse_ajax_data () { // Refactor the data we want to save $content_new = array(); for ($i = 0; $i < $_REQUEST['nbr_keys']; $i++) { $key = $_REQUEST['key_' . $i]; $value = stripslashes($_REQUEST['value_' . $i]); $content_new[$key] = $value; } return $content_new; } /** *** administer_save_position ( ) *** *** Ajax function. **/ function administer_save_position () { // Get and filter out variables we don't want $position = administer_parse_ajax_data(); $position_old = $position['position_old']; $default = array('position' => '', 'description' => '', 'before' => '', 'after' => '', 'type' => ''); $position = array_intersect_key($position, $default); // Get the stored positions $positions = get_post_meta(get_option('administer_post_id'), 'administer_positions', true); if (!is_array($positions)) $positions = array(); // Remove old and add new data unset($positions[$position_old]); $positions[$position['position']] = $position; // Update our Custom Fields if (!add_post_meta(get_option('administer_post_id'), 'administer_positions', $positions, true)) update_post_meta(get_option('administer_post_id'), 'administer_positions', $positions); // Stop the spinning if ($notification_domid = $_REQUEST['notification_domid']) die("p2m_ready('${notification_domid}');"); } function adminiseter_content_age($scheduele) { if (!$scheduele) return array(array('start' => '0', 'end' => '0')); $age = array(); $valid == false; $times = explode(',', $scheduele); // Content may have multiple schedueles foreach ($times as $time) { $parts = explode(':', ltrim(rtrim($time))); if (count($parts) == 2) { // Make the dates inclusive $start = strtotime($parts[0] . ' 00:00:00'); $end = strtotime($parts[1] . ' 23:59:59'); } $now = time(); $age[count($age)] = array('start' => ($start - time())/86400, 'end' => ($end - time())/86400); if ($start <= $now && $end >= $now) $valid = true; } return $age; } /** *** administer_activity_box_alerts () *** *** Stick something on the dashboard if something is due to expire or becom active. **/ function administer_activity_box_alerts () { $url = get_option('siteurl') . '/wp-admin/edit.php?page=' . dirname(plugin_basename(__FILE__)) . '/ad-minister.php'; $content = get_post_meta(get_option('administer_post_id'), 'administer_content', true); $period = get_option('administer_dashboard_period'); $li_ends = ''; $li_starts = ''; foreach($content as $con) { $ages = adminiseter_content_age($con['scheduele']); foreach ($ages as $age) { if ($age['end'] < $period && $age['end'] >= 0 && $age['end']) { $li_ends .= '
  • '; $li_ends .= '' . $con['title'] . ''; $li_ends .= ' ' . __('expires in', 'ad-minister') . ' ' . round($age['end'], 1) . ' days.'; $li_ends .= '
  • '; } if ($age['start'] < $period && $age['start'] >= 0 && $age['start']) { $li_starts .= '
  • '; $li_starts .= '' . $con['title'] . ''; $li_starts .= ' ' . __('starts in', 'ad-minister') . ' ' . round($age['start'], 1) . ' days.'; $li_starts .= '
  • '; } } } // if ($li_starts || $li_ends) printf('

    Ad-minister »

    ', $url); if ($li_starts) printf('

    ' . __('The following content is becomng active in the next %s days', 'ad-minister') . '

    ', $period, $li_starts); if ($li_ends) printf('

    ' . __('The following content expires in the next %s days', 'ad-minister') . '

    ', $period, $li_ends); if ($li_starts && !li_ends) _e('No upcoming activity.', 'ad-minister'); } /** *** 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; } } } ?>