function my_calendar_upcoming_events($before = 'default', $after = 'default', $type = 'default', $category = 'default', $template = 'default', $substitute = '', $order = 'asc', $skip = 0, $show_today = 'yes', $author = 'default', $host = 'default', $ltype = '', $lvalue = '') { global $default_template; $output = ''; $widget_defaults = (array) get_option('mc_widget_defaults'); $display_upcoming_type = $type == 'default' ? $widget_defaults['upcoming']['type'] : $type; $display_upcoming_type = $display_upcoming_type == '' ? 'event' : $display_upcoming_type; // Get number of units we should go into the future $after = $after == 'default' ? $widget_defaults['upcoming']['after'] : $after; $after = $after == '' ? 10 : $after; // Get number of units we should go into the past $before = $before == 'default' ? $widget_defaults['upcoming']['before'] : $before; $before = $before == '' ? 0 : $before; $category = $category == 'default' ? '' : $category; // allow reference by file to external template. if ($template != '' && mc_file_exists(sanitize_file_name($template))) { $template = @file_get_contents(mc_get_file(sanitize_file_name($template))); } $template = $template == 'default' ? $widget_defaults['upcoming']['template'] : $template; $template = $template == '' ? $default_template : $template; $no_event_text = $substitute == '' ? $widget_defaults['upcoming']['text'] : $substitute; $header = "<ul id='upcoming-events'>"; $footer = "</ul>"; if ($display_upcoming_type == "days" || $display_upcoming_type == "month" || $display_upcoming_type == 'month+1' || $display_upcoming_type == "year") { $temp_array = array(); if ($display_upcoming_type == "days") { $from = date('Y-m-d', strtotime("-{$before} days")); $to = date('Y-m-d', strtotime("+{$after} days")); } if ($display_upcoming_type == "month") { $from = date('Y-m-1'); $to = date('Y-m-t'); } /* Yes, this is crude. But sometimes simplicity works best. There are only 12 possibilities, after all. */ if ($display_upcoming_type == 'month+1') { $from = date('Y-m-1', strtotime('+1 month')); $to = date('Y-m-t', strtotime('+1 month')); } if ($display_upcoming_type == 'month+2') { $from = date('Y-m-1', strtotime('+2 month')); $to = date('Y-m-t', strtotime('+2 month')); } if ($display_upcoming_type == 'month+3') { $from = date('Y-m-1', strtotime('+3 month')); $to = date('Y-m-t', strtotime('+3 month')); } if ($display_upcoming_type == 'month+4') { $from = date('Y-m-1', strtotime('+4 month')); $to = date('Y-m-t', strtotime('+4 month')); } if ($display_upcoming_type == 'month+5') { $from = date('Y-m-1', strtotime('+5 month')); $to = date('Y-m-t', strtotime('+5 month')); } if ($display_upcoming_type == 'month+6') { $from = date('Y-m-1', strtotime('+6 month')); $to = date('Y-m-t', strtotime('+6 month')); } if ($display_upcoming_type == 'month+7') { $from = date('Y-m-1', strtotime('+7 month')); $to = date('Y-m-t', strtotime('+7 month')); } if ($display_upcoming_type == 'month+8') { $from = date('Y-m-1', strtotime('+8 month')); $to = date('Y-m-t', strtotime('+8 month')); } if ($display_upcoming_type == 'month+9') { $from = date('Y-m-1', strtotime('+9 month')); $to = date('Y-m-t', strtotime('+9 month')); } if ($display_upcoming_type == 'month+10') { $from = date('Y-m-1', strtotime('+10 month')); $to = date('Y-m-t', strtotime('+10 month')); } if ($display_upcoming_type == 'month+11') { $from = date('Y-m-1', strtotime('+11 month')); $to = date('Y-m-t', strtotime('+11 month')); } if ($display_upcoming_type == 'month+12') { $from = date('Y-m-1', strtotime('+12 month')); $to = date('Y-m-t', strtotime('+12 month')); } if ($display_upcoming_type == "year") { $from = date('Y-1-1'); $to = date('Y-12-31'); } $from = apply_filters('mc_upcoming_date_from', $from); $to = apply_filters('mc_upcoming_date_to', $to); $event_array = my_calendar_events($from, $to, $category, $ltype, $lvalue, 'upcoming', $author, $host); if (count($event_array) != 0) { foreach ($event_array as $key => $value) { if (is_array($value)) { foreach ($value as $k => $v) { $event = mc_create_tags($v); if ($v->category_private == 1 && !is_user_logged_in()) { // this event is private. } else { $temp_array[] = $event; } } } } } $i = 0; $last_item = $last_id = $last_date = ''; $skips = array(); foreach (reverse_array($temp_array, true, $order) as $details) { $item = jd_draw_template($details, $template); if ($i < $skip && $skip != 0) { $i++; } else { // if same group, and same date, use it. if ($details['group'] !== $last_id || $details['date'] == $last_date || $details['group'] == '0') { if (!in_array($details['dateid'], $skips)) { $output .= $item == $last_item ? '' : "<li>{$item}</li>"; } } } $skips[] = $details['dateid']; // prevent the same event from showing more than once. $last_id = $details['group']; // prevent group events from displaying in a row. Not if there are intervening events. $last_item = $item; $last_date = $details['date']; } } else { $caching = apply_filters('mc_cache_enabled', false); if ($caching) { $cache = get_transient('mc_cache_upcoming'); $output .= "<!-- Cached -->"; if ($cache) { if (isset($cache[$category])) { $events = $cache[$category]; } else { $events = mc_get_all_events($category, $before, $after, $show_today, $author, $host, $ltype, $lvalue); $cache[$category] = $events; set_transient('mc_cache_upcoming', $cache, 60 * 30); } } else { $events = mc_get_all_events($category, $before, $after, $show_today, $author, $host, $ltype, $lvalue); $cache[$category] = $events; set_transient('mc_cache_upcoming', $cache, 60 * 30); } } else { $events = mc_get_all_events($category, $before, $after, $show_today, $author, $host, $ltype, $lvalue); // grab all events within reasonable proximity } $holiday_array = array(); if (!get_option('mc_skip_holidays_category') || get_option('mc_skip_holidays_category') == '') { $holidays = array(); } else { $holidays = mc_get_all_holidays($before, $after, $show_today); $holiday_array = mc_set_date_array($holidays); } if (is_array($events) && !empty($events)) { $event_array = mc_set_date_array($events); if (is_array($holidays) && count($holidays) > 0) { $event_array = mc_holiday_limit($event_array, $holiday_array); // if there are holidays, rejigger. } } if (!empty($event_array)) { $output .= mc_produce_upcoming_events($event_array, $template, 'list', $order, $skip, $before, $after, $show_today); } else { $output = ''; } } if ($output != '') { $output = $header . $output . $footer; return $output; } else { return stripcslashes($no_event_text); } }
function my_calendar_upcoming_events($before = 'default', $after = 'default', $type = 'default', $category = 'default', $template = 'default', $substitute = '', $order = 'asc', $skip = 0, $show_today = 'yes', $author = 'default', $host = 'default', $ltype = '', $lvalue = '', $from = '', $to = '', $site = false) { global $default_template; if ($site) { $site = $site == 'global' ? BLOG_ID_CURRENT_SITE : $site; switch_to_blog($site); } $args = array('before' => $before, 'after' => $after, 'type' => $type, 'category' => $category, 'template' => $template, 'fallback' => $substitute, 'order' => $order, 'skip' => $skip, 'show_today' => $show_today, 'author' => $author, 'host' => $host, 'ltype' => $ltype, 'lvalue' => $lvalue, 'from' => $from, 'to' => $to, 'site' => $site); $hash = md5(implode(',', $args)); $output = ''; $widget_defaults = (array) get_option('mc_widget_defaults'); $display_upcoming_type = $type == 'default' ? $widget_defaults['upcoming']['type'] : $type; $display_upcoming_type = $display_upcoming_type == '' ? 'events' : $display_upcoming_type; // Get number of units we should go into the future $after = $after == 'default' ? $widget_defaults['upcoming']['after'] : $after; $after = $after == '' ? 10 : $after; // Get number of units we should go into the past $before = $before == 'default' ? $widget_defaults['upcoming']['before'] : $before; $before = $before == '' ? 0 : $before; $category = $category == 'default' ? '' : $category; // allow reference by file to external template. if ($template != '' && mc_file_exists(sanitize_file_name($template))) { $template = @file_get_contents(mc_get_file(sanitize_file_name($template))); } $template = $template == 'default' ? $widget_defaults['upcoming']['template'] : $template; $template = $template == '' ? $default_template : $template; if (mc_key_exists($template)) { $template = mc_get_custom_template($template); } $template = apply_filters('mc_upcoming_events_template', $template); $no_event_text = $substitute == '' ? $widget_defaults['upcoming']['text'] : $substitute; $header = "<ul id='upcoming-events-{$hash}' class='upcoming-events'>"; $footer = "</ul>"; $display_events = $display_upcoming_type == 'events' || $display_upcoming_type == 'event' ? true : false; if (!$display_events) { $temp_array = array(); if ($display_upcoming_type == "days") { $from = date('Y-m-d', strtotime("-{$before} days")); $to = date('Y-m-d', strtotime("+{$after} days")); } if ($display_upcoming_type == "month") { $from = date('Y-m-1'); $to = date('Y-m-t'); } if ($display_upcoming_type == 'custom' && $from != '' && $to != '') { $from = date('Y-m-d', strtotime($from)); $to = $to == 'today' ? date('Y-m-d', current_time('timestamp')) : date('Y-m-d', strtotime($to)); } /* Yes, this is crude. But sometimes simplicity works best. There are only 12 possibilities, after all. */ if ($display_upcoming_type == 'month+1') { $from = date('Y-m-1', strtotime('+1 month')); $to = date('Y-m-t', strtotime('+1 month')); } if ($display_upcoming_type == 'month+2') { $from = date('Y-m-1', strtotime('+2 month')); $to = date('Y-m-t', strtotime('+2 month')); } if ($display_upcoming_type == 'month+3') { $from = date('Y-m-1', strtotime('+3 month')); $to = date('Y-m-t', strtotime('+3 month')); } if ($display_upcoming_type == 'month+4') { $from = date('Y-m-1', strtotime('+4 month')); $to = date('Y-m-t', strtotime('+4 month')); } if ($display_upcoming_type == 'month+5') { $from = date('Y-m-1', strtotime('+5 month')); $to = date('Y-m-t', strtotime('+5 month')); } if ($display_upcoming_type == 'month+6') { $from = date('Y-m-1', strtotime('+6 month')); $to = date('Y-m-t', strtotime('+6 month')); } if ($display_upcoming_type == 'month+7') { $from = date('Y-m-1', strtotime('+7 month')); $to = date('Y-m-t', strtotime('+7 month')); } if ($display_upcoming_type == 'month+8') { $from = date('Y-m-1', strtotime('+8 month')); $to = date('Y-m-t', strtotime('+8 month')); } if ($display_upcoming_type == 'month+9') { $from = date('Y-m-1', strtotime('+9 month')); $to = date('Y-m-t', strtotime('+9 month')); } if ($display_upcoming_type == 'month+10') { $from = date('Y-m-1', strtotime('+10 month')); $to = date('Y-m-t', strtotime('+10 month')); } if ($display_upcoming_type == 'month+11') { $from = date('Y-m-1', strtotime('+11 month')); $to = date('Y-m-t', strtotime('+11 month')); } if ($display_upcoming_type == 'month+12') { $from = date('Y-m-1', strtotime('+12 month')); $to = date('Y-m-t', strtotime('+12 month')); } if ($display_upcoming_type == "year") { $from = date('Y-1-1'); $to = date('Y-12-31'); } $from = apply_filters('mc_upcoming_date_from', $from, $args); $to = apply_filters('mc_upcoming_date_to', $to, $args); $event_array = my_calendar_events($from, $to, $category, $ltype, $lvalue, 'upcoming', $author, $host, '', $site); if (count($event_array) != 0) { foreach ($event_array as $key => $value) { if (is_array($value)) { foreach ($value as $k => $v) { $event = mc_create_tags($v); if ($v->category_private == 1 && !is_user_logged_in()) { // this event is private. } else { $temp_array[] = $event; } } } } } $i = 0; $last_item = $last_id = $last_date = ''; $skips = array(); foreach (reverse_array($temp_array, true, $order) as $details) { $item = jd_draw_template($details, $template); if ($i < $skip && $skip != 0) { $i++; } else { $today = date('Y-m-d H:i', current_time('timestamp')); $date = date('Y-m-d H:i', strtotime($details['ts_occur_begin'])); $class = my_calendar_date_comp($date, $today) === true ? "past-event" : "future-event"; $category = 'mc_' . sanitize_title($details['category_name']); $prepend = apply_filters('mc_event_upcoming_before', "<li class='{$class} {$category}'>", $class, $category); $append = apply_filters('mc_event_upcoming_after', '</li>', $class, $category); // if same group, and same date, use it. if ($details['group'] !== $last_id || $details['date'] == $last_date || $details['group'] == '0') { if (!in_array($details['dateid'], $skips)) { $output .= $item == $last_item ? '' : $prepend . $item . $append; } } } $skips[] = $details['dateid']; // prevent the same event from showing more than once. $last_id = $details['group']; // prevent group events from displaying in a row. Not if there are intervening events. $last_item = $item; $last_date = $details['date']; } } else { $caching = apply_filters('mc_cache_enabled', false); if ($caching) { $cache = get_transient('mc_cache_upcoming'); $output .= "<!-- Cached -->"; if ($cache) { if (isset($cache[$category])) { $events = $cache[$category]; } else { $events = mc_get_all_events($category, $before, $after, $show_today, $author, $host, $ltype, $lvalue, $site); $cache[$category] = $events; set_transient('mc_cache_upcoming', $cache, 60 * 30); } } else { $events = mc_get_all_events($category, $before, $after, $show_today, $author, $host, $ltype, $lvalue, $site); $cache[$category] = $events; set_transient('mc_cache_upcoming', $cache, 60 * 30); } } else { $events = mc_get_all_events($category, $before, $after, $show_today, $author, $host, $ltype, $lvalue, $site); // grab all events within reasonable proximity } $holiday_array = array(); if (!get_option('mc_skip_holidays_category') || get_option('mc_skip_holidays_category') == '') { $holidays = array(); } else { $holidays = mc_get_all_holidays($before, $after, $show_today); $holiday_array = mc_set_date_array($holidays); } if (is_array($events) && !empty($events)) { $event_array = mc_set_date_array($events); if (is_array($holidays) && count($holidays) > 0) { $event_array = mc_holiday_limit($event_array, $holiday_array); // if there are holidays, rejigger. } } if (!empty($event_array)) { $output .= mc_produce_upcoming_events($event_array, $template, 'list', $order, $skip, $before, $after, $show_today); } else { $output = ''; } } if ($output != '') { $output = apply_filters('mc_upcoming_events_header', $header) . $output . apply_filters('mc_upcoming_events_footer', $footer); $return = get_option('mc_process_shortcodes') == 'true' ? do_shortcode($output) : $output; } else { $return = stripcslashes($no_event_text); } if ($site) { restore_current_blog(); } return $return; }