function mc_instance_list($id, $occur = false, $template = '<h3>{title}</h3>{description}', $list = '<li>{date}, {time}</li>', $before = "<ul>", $after = "</ul>", $instance = false)
{
    global $wpdb;
    $id = (int) $id;
    $output = '';
    if ($instance == true) {
        $sql = "SELECT * FROM " . my_calendar_event_table() . " WHERE occur_id={$id}";
    } else {
        $sql = "SELECT * FROM " . my_calendar_event_table() . " WHERE occur_event_id={$id}";
    }
    $results = $wpdb->get_results($sql);
    if (is_array($results) && is_admin()) {
        foreach ($results as $result) {
            $begin = "<span id='occur_date_{$result->occur_id}'>" . date_i18n(get_option('mc_date_format'), strtotime($result->occur_begin)) . ', ' . date(get_option('mc_time_format'), strtotime($result->occur_begin)) . "</span>";
            if ($result->occur_id == $occur) {
                $form_control = '';
                $edit = "<em>" . __('Editing Now', 'my-calendar') . "</em>";
            } else {
                $form_control = "{$begin}: <button class='delete_occurrence' type='button' data-value='{$result->occur_id}' aria-describedby='occur_date_{$result->occur_id}' />" . __('Delete', 'my-calendar') . "</button> ";
                $edit = "<a href='" . admin_url('admin.php?page=my-calendar') . "&amp;mode=edit&amp;event_id={$id}&amp;date={$result->occur_id}' aria-describedby='occur_date_{$result->occur_id}'>" . __('Edit', 'my-calendar') . "</a>";
            }
            $output .= "<li>{$form_control}{$edit}</li>";
        }
    } else {
        $details = '';
        foreach ($results as $result) {
            $event_id = $result->occur_id;
            $event = mc_get_event($event_id);
            $array = mc_create_tags($event);
            if (in_array($template, array('details', 'grid', 'list', 'mini')) || mc_key_exists($template)) {
                if (get_option('mc_use_' . $template . '_template') == 1) {
                    $template = mc_get_template($template);
                } else {
                    if (mc_key_exists($template)) {
                        $template = mc_get_custom_template($template);
                    } else {
                        $details = my_calendar_draw_event($event, $type = "single", $event->event_begin, $event->event_time, '');
                    }
                }
            }
            $item = $list != '' ? jd_draw_template($array, $list) : '';
            if ($details == '') {
                $details = $template != '' ? jd_draw_template($array, $template) : '';
            }
            $output .= $item;
            if ($list == '') {
                break;
            }
        }
        $output = $details . $before . $output . $after;
    }
    return get_option('mc_process_shortcodes') == 'true' ? do_shortcode($output) : $output;
}
function mc_format_rss($events)
{
    if (is_array($events) && !empty($events)) {
        $template = "\n<item>\r\n\t\t\t<title>{rss_title}</title>\r\n\t\t\t<link>{details_link}</link>\r\n\t\t\t<pubDate>{rssdate}</pubDate>\r\n\t\t\t<dc:creator>{author}</dc:creator>  \t\r\n\t\t\t<description><![CDATA[{rss_description}]]></description>\r\n\t\t\t<ev:startdate>{dtstart}</ev:startdate>\r\n\t\t\t<ev:enddate>{dtend}</ev:enddate>\r\n\t\t\t<content:encoded><![CDATA[<div class='vevent'>\r\n\t\t\t<h1 class='summary'>{rss_title}</h1>\r\n\t\t\t<div class='description'>{rss_description}</div>\r\n\t\t\t<p class='dtstart' title='{ical_start}'>Begins: {time} on {date}</p>\r\n\t\t\t<p class='dtend' title='{ical_end}'>Ends: {endtime} on {enddate}</p>\t\r\n\t\t\t<p>Recurrance: {recurs}</p>\r\n\t\t\t<p>Repetition: {repeats} times</p>\r\n\t\t\t<div class='location'>{rss_hcard}</div>\r\n\t\t\t{rss_link_title}\r\n\t\t\t</div>]]></content:encoded>\r\n\t\t\t<dc:format xmlns:dc='http://purl.org/dc/elements/1.1/'>text/html</dc:format>\r\n\t\t\t<dc:source xmlns:dc='http://purl.org/dc/elements/1.1/'>" . home_url() . "</dc:source>\r\n\t\t\t{guid}\r\n\t\t  </item>\n";
        if (get_option('mc_use_rss_template') == 1) {
            $template = mc_get_template('rss');
        }
        $charset = get_bloginfo('charset');
        $output = '<?xml version="1.0" encoding="' . $charset . '"?>
		<rss version="2.0"
			xmlns:content="http://purl.org/rss/1.0/modules/content/"
			xmlns:dc="http://purl.org/dc/elements/1.1/"
			xmlns:ev="http://purl.org/rss/1.0/modules/event/"
			xmlns:atom="http://www.w3.org/2005/Atom"
			xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
			xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
			>
		<channel>
		  <title>' . get_bloginfo('name') . ' Calendar</title>
		  <link>' . home_url() . '</link>
		  <description>' . get_bloginfo('description') . ': My Calendar Events</description>
		  <language>' . get_bloginfo('language') . '</language>
		  <managingEditor>' . get_bloginfo('admin_email') . ' (' . get_bloginfo('name') . ' Admin)</managingEditor>
		  <generator>My Calendar WordPress Plugin http://www.joedolson.com/my-calendar/</generator>
		  <lastBuildDate>' . mysql2date('D, d M Y H:i:s +0000', current_time('timestamp')) . '</lastBuildDate>
		  <atom:link href="' . htmlentities(esc_url(add_query_arg($_GET, mc_get_current_url()))) . '" rel="self" type="application/rss+xml" />';
        foreach ($events as $date) {
            foreach (array_keys($date) as $key) {
                $event =& $date[$key];
                $array = mc_create_tags($event);
                $output .= jd_draw_template($array, $template, 'rss');
            }
        }
        $output .= '</channel>
		</rss>';
        return mc_strip_to_xml($output);
    } else {
        return false;
    }
}
function my_calendar_draw_event($event, $type = "calendar", $process_date, $time, $template = '')
{
    // if event is not approved, return without processing
    if (get_option('mc_event_approve') == 'true' && (int) $event->event_approved !== 1) {
        return '';
    }
    // if event ends at midnight today (e.g., very first thing of the day), exit without re-drawing
    // or if event started yesterday & has event_hide_end checked.
    $ends_at_midnight = $event->event_endtime == '00:00:00' && date('Y-m-d', strtotime($event->occur_end)) == $process_date && date('Y-m-d', strtotime($event->occur_begin)) != $process_date ? true : false;
    // hides events if hiding end time & not first day.
    $hide_day_two = apply_filters('mc_hide_additional_days', false, $event) && date('Y-m-d', strtotime($event->occur_begin)) != date('Y-m-d', strtotime($process_date)) ? true : false;
    if ($ends_at_midnight || $hide_day_two) {
        return '';
    }
    if ($event->category_private == 1 && !is_user_logged_in()) {
        return '';
    }
    // assign empty values to template sections
    $header = $address = $more = $author = $list_title = $title = $output = $container = $short = $description = $link = $vcal = $gcal = '';
    $date_format = get_option('mc_date_format') != '' ? get_option('mc_date_format') : get_option('date_format');
    $data = mc_create_tags($event);
    $details = '';
    if (mc_show_details($time, $type)) {
        $details = apply_filters('mc_custom_template', false, $data, $event, $type, $process_date, $time, $template);
        $template = apply_filters('mc_use_custom_template', $template, $data, $event, $type, $process_date, $time);
        if ($details === false) {
            if ($template != '' && mc_file_exists(sanitize_file_name($template))) {
                $template = @file_get_contents(mc_get_file(sanitize_file_name($template)));
                $details = jd_draw_template($data, $template);
            } else {
                if ($template != '' && mc_key_exists($template)) {
                    $template = mc_get_custom_template($template);
                    $details = jd_draw_template($data, $template);
                } else {
                    switch ($type) {
                        case 'mini':
                            $template = mc_get_template('mini');
                            if (get_option('mc_use_mini_template') == 1) {
                                $details = jd_draw_template($data, $template);
                            }
                            break;
                        case 'list':
                            $template = mc_get_template('list');
                            if (get_option('mc_use_list_template') == 1) {
                                $details = jd_draw_template($data, $template);
                            }
                            break;
                        case 'single':
                            $template = mc_get_template('details');
                            if (get_option('mc_use_details_template') == 1) {
                                $details = jd_draw_template($data, $template);
                            }
                            break;
                        case 'calendar':
                        default:
                            $template = mc_get_template('grid');
                            if (get_option('mc_use_grid_template') == 1) {
                                $details = jd_draw_template($data, $template);
                            }
                    }
                }
            }
        }
    }
    $mc_display_author = get_option('mc_display_author');
    $display_map = get_option('mc_show_map');
    $display_address = get_option('mc_show_address');
    $mc_display_more = get_option('mc_display_more');
    $uid = 'mc_' . $event->occur_id;
    $day_id = date('d', strtotime($process_date));
    $image = mc_category_icon($event);
    $has_image = $image != '' ? ' has-image' : '';
    $event_classes = mc_event_classes($event, $day_id, $type);
    $header .= "<div id='{$uid}-{$day_id}-{$type}' class='{$event_classes}'>\n";
    switch ($type) {
        case 'calendar':
            $title_template = mc_get_template('title') == '' ? '{title}' : mc_get_template('title');
            break;
        case 'list':
            $title_template = mc_get_template('title_list') == '' ? '{title}' : mc_get_template('title_list');
            break;
        case 'single':
            $title_template = mc_get_template('title_solo') == '' ? '{title}' : mc_get_template('title_solo');
            break;
        default:
            $title_template = mc_get_template('title') == '' ? '{title}' : mc_get_template('title');
    }
    $event_title = jd_draw_template($data, $title_template);
    $event_title = $event_title == '' ? jd_draw_template($data, '{title}') : mc_kses_post($event_title);
    //prevent empty titles
    if (strpos($event_title, 'href') === false && $type != 'mini' && $type != 'list') {
        if (get_option('mc_open_uri') == 'true') {
            $details_link = esc_url(mc_get_details_link($event));
            $wrap = "<a href='{$details_link}' class='url summary{$has_image}'>";
            $balance = "</a>";
        } else {
            $wrap = "<a href='#{$uid}-{$day_id}-{$type}-details' class='url summary{$has_image}'>";
            $balance = "</a>";
        }
    } else {
        $wrap = $balance = '';
    }
    $current_date = date_i18n(apply_filters('mc_date_format', $date_format, 'details'), strtotime($process_date));
    $group_class = $event->event_span == 1 ? ' multidate group' . $event->event_group_id : '';
    $heading_level = apply_filters('mc_heading_level_table', 'h3', $type, $time, $template);
    $inner_heading = apply_filters('mc_heading_inner_title', $wrap . $image . trim($event_title) . $balance, $event_title, $event);
    $header .= $type != 'single' && $type != 'list' ? "<{$heading_level} class='event-title summary{$group_class}' id='{$uid}-{$day_id}-{$type}-title'>{$inner_heading}</{$heading_level}>\n" : '';
    $event_title = $type == 'single' ? apply_filters('mc_single_event_title', $event_title, $event) : $event_title;
    $title = $type == 'single' && !is_singular('mc-events') ? "<h2 class='event-title summary'>{$image} {$event_title}</h2>\n" : '<span class="summary screen-reader-text">' . $event_title . '</span>';
    $title = apply_filters('mc_event_title', $title, $event, $event_title, $image);
    $header .= '<span class="summary">' . $title . '</span>';
    $close_image = apply_filters('mc_close_button', "<img src=\"" . plugin_dir_url(__FILE__) . "images/event-close.png\" alt='" . __('Close', 'my-calendar') . "' />");
    $close_button = "<button type='button' aria-controls='{$uid}-{$day_id}-{$type}-details' class='mc-toggle close'>{$close_image}</button>";
    if (mc_show_details($time, $type)) {
        $close = $type == 'calendar' || $type == 'mini' ? $close_button : '';
        if ($details === false) {
            // put together address information as vcard
            if ($display_address == 'true' || $display_map == 'true') {
                $address = mc_hcard($event, $display_address, $display_map);
            }
            // end vcard
            $time_html = mc_time_html($event, $type, $current_date);
            if ($type == "list") {
                $heading_level = apply_filters('mc_heading_level_list', 'h3', $type, $time, $template);
                $list_title = "<{$heading_level} class='event-title summary' id='{$uid}-{$day_id}-{$type}-title'>{$image}" . $event_title . "</{$heading_level}>\n";
            }
            if ($mc_display_author == 'true') {
                if ($event->event_author != 0) {
                    $e = get_userdata($event->event_author);
                    $author = '<p class="event-author">' . __('Posted by', 'my-calendar') . ' <span class="author-name">' . $e->display_name . "</span></p>\n";
                }
            }
            if ($mc_display_more != 'false' && !isset($_GET['mc_id'])) {
                $details_label = mc_get_details_label($event, $data);
                $details_link = mc_get_details_link($event);
                if (_mc_is_url($details_link)) {
                    $more = "<p class='mc_details'><a href='" . esc_url($details_link) . "'>{$details_label}</a></p>\n";
                } else {
                    $more = '';
                }
            }
            $more = apply_filters('mc_details_grid_link', $more, $event);
            // handle link expiration
            $event_link = mc_event_link($event);
            if (function_exists('mc_google_cal') && get_option('mc_show_gcal') == 'true') {
                $gcal_link = "<p class='gcal'>" . jd_draw_template($data, '{gcal_link}') . "</p>";
                $gcal = $gcal_link;
            }
            if (function_exists('my_calendar_generate_vcal') && get_option('mc_show_event_vcal') == 'true') {
                $url = add_query_arg('vcal', $uid, home_url());
                $vcal_link = "<p class='ical'><a rel='nofollow' href='" . esc_url($url) . "'>" . __('iCal', 'my-calendar') . "</a></p>\n";
                $vcal = $vcal_link;
            }
            $sizes = get_intermediate_image_sizes();
            if (in_array('medium', $sizes)) {
                $default_size = 'medium';
            } else {
                $default_size = 'thumbnail';
            }
            $default_size = apply_filters('mc_default_image_size', $default_size);
            if (is_numeric($event->event_post) && $event->event_post != 0 && (isset($data[$default_size]) && $data[$default_size] != '')) {
                $atts = apply_filters('mc_post_thumbnail_atts', array('class' => 'mc-image photo'));
                $image = get_the_post_thumbnail($event->event_post, $default_size, $atts);
            } else {
                $image = $event->event_image != '' ? "<img src='{$event->event_image}' alt='' class='mc-image photo' />" : '';
            }
            if (get_option('mc_desc') == 'true' || $type == 'single') {
                $description = wpautop(stripcslashes(mc_kses_post($event->event_desc)), 1);
                $description = "<div class='longdesc description'>{$description}</div>";
            }
            if (get_option('mc_short') == 'true' && $type != 'single') {
                $short = wpautop(stripcslashes(mc_kses_post($event->event_short)), 1);
                $short = "<div class='shortdesc'>{$short}</div>";
            }
            if (get_option('mc_event_registration') == 'true') {
                switch ($event->event_open) {
                    case '0':
                        $status = mc_kses_post(get_option('mc_event_closed'));
                        break;
                    case '1':
                        $status = mc_kses_post(get_option('mc_event_open'));
                        break;
                    case '2':
                        $status = '';
                        break;
                    default:
                        $status = '';
                }
            } else {
                $status = '';
            }
            $status = $status != '' ? "<p>{$status}</p>" : '';
            $status = apply_filters('mc_registration_state', $status, $event);
            $return_url = apply_filters('mc_return_uri', get_option('mc_uri'));
            $return = $type == 'single' ? "<p class='view-full'><a href='{$return_url}'>" . __('View full calendar', 'my-calendar') . "</a></p>" : '';
            if (!mc_show_details($time, $type)) {
                $description = $short = $status = '';
            }
            if (get_option('mc_gmap') == 'true') {
                $map = is_singular('mc-events') || $type == 'single' ? mc_generate_map($event) : '';
            } else {
                $map = '';
            }
            if ($event_link != '' && get_option('mc_event_link') != 'false') {
                $is_external = mc_external_link($event_link);
                $external_class = $is_external ? "class='{$type}-link external url'" : "class='{$type}-link url'";
                $link_template = mc_get_template('link') != '' ? mc_get_template('link') : '{title}';
                $link_text = jd_draw_template($data, $link_template);
                $link = "<p><a href='" . esc_url($event_link) . "' {$external_class}>" . $link_text . "</a></p>";
            }
            $details = "\n" . $close . $time_html . $list_title . $image . "<div class='location'>" . $map . $address . "</div>" . $description . $short . $link . $status . $author . "<div class='sharing'>" . $vcal . $gcal . $more . "</div>" . $return;
        } else {
            // if a custom template is in use
            $toggle = $type == 'calendar' || $type == 'mini' ? $close_button : '';
            $details = $toggle . $details . "\n";
        }
        $img_class = $image != '' ? ' has-image' : ' no-image';
        $container = "<div id='{$uid}-{$day_id}-{$type}-details' class='details{$img_class}' role='alert' aria-labelledby='{$uid}-{$day_id}-{$type}-title'>\n";
        $container = apply_filters('mc_before_event', $container, $event, $type, $time);
        $details = $header . $container . apply_filters('mc_inner_content', $details, $event, $type, $time);
        $details .= apply_filters('mc_after_event', '', $event, $type, $time);
        $details .= $close;
        // second close button
        $details .= "</div><!--ends .details--></div>";
        $details = apply_filters('mc_event_content', $details, $event, $type, $time);
    } else {
        $details = apply_filters('mc_before_event_no_details', $container, $event, $type, $time) . $header . apply_filters('mc_after_event_no_details', '', $event, $type, $time) . "</div>";
    }
    return $details;
}
function mc_instance_list($id, $occur = false, $template = '<h3>{title}</h3>{description}', $list = '<li>{date}, {time}</li>', $before = "<ul>", $after = "</ul>")
{
    global $wpdb;
    $id = (int) $id;
    $output = '';
    $sql = "SELECT * FROM " . my_calendar_event_table() . " WHERE occur_event_id={$id}";
    $results = $wpdb->get_results($sql);
    if (is_array($results) && is_admin()) {
        foreach ($results as $result) {
            if ($result->occur_id == $occur) {
                $form_control = '';
                $current = "<em>" . __('Editing: ', 'my-calendar') . "</em>";
                $end = '';
            } else {
                $form_control = "<input type='checkbox' name='delete_occurrences[]' id='delete_{$result->occur_id}' value='{$result->occur_id}' aria-labelledby='occur_label occur_date' /> <label id='occur_label' for='delete_{$result->occur_id}'>Delete</label> ";
                $current = "<a href='" . admin_url('admin.php?page=my-calendar') . "&amp;mode=edit&amp;event_id={$id}&amp;date={$result->occur_id}'>";
                $end = "</a>";
            }
            $begin = "<span id='occur_date'>" . date_i18n(get_option('mc_date_format'), strtotime($result->occur_begin)) . ', ' . date(get_option('mc_time_format'), strtotime($result->occur_begin)) . "</span>";
            $output .= "<li>{$form_control}{$current}{$begin}{$end}</li>";
        }
    } else {
        $details = '';
        foreach ($results as $result) {
            $event_id = $result->occur_id;
            $event = mc_get_event($event_id);
            $array = mc_create_tags($event);
            if (in_array($template, array('details', 'grid', 'list', 'mini'))) {
                if (get_option('mc_use_' . $template . '_template') == 1) {
                    $template = mc_get_template($template);
                } else {
                    $template = false;
                    $details = my_calendar_draw_event($event, $type = "single", $event->event_begin, $event->event_time, '');
                }
            }
            $item = $list != '' ? jd_draw_template($array, $list) : '';
            if ($details == '') {
                $details = $template != '' ? jd_draw_template($array, $template) : '';
            }
            $output .= $item;
        }
        $output = $details . $before . $output . $after;
    }
    return $output;
}