public function widget($args, $instance) { global $xi_widget_args, $xi_widget_instance, $upcoming_events; $xi_widget_args = $ags; $xi_widget_instance = $instance; if (!is_array($instance['terms'])) { $upcoming_events = XiQuery::get_upcoming_events($instance['num_events']); } else { $upcoming_events = XiQuery::get_upcoming_events($instance['num_events'], $instance['terms']); } XiUtilities::include_template('widget_upcoming-events.php'); }
/** * Get a list of events, optionally filterable by category */ public static function events_list($atts) { $atts = shortcode_atts(array('calendar_id' => '', 'category_id' => '', 'events_per_page' => 10, 'show_category_filter' => 'true'), $atts, 'xi_events_list'); $terms = array(); $terms[XiEvents::$category_taxonomy_name] = array(); $terms[XiEvents::$calendar_taxonomy_name] = array(); if (!empty($atts['category_id'])) { $terms[XiEvents::$category_taxonomy_name] = explode(',', $atts['category_id']); $terms[XiEvents::$category_taxonomy_name] = array_map('trim', $terms[XiEvents::$category_taxonomy_name]); } if (!empty($atts['calendar_id'])) { $terms[XiEvents::$calendar_taxonomy_name] = explode(',', $atts['calendar_id']); $terms[XiEvents::$calendar_taxonomy_name] = array_map('trim', $terms[XiEvents::$calendar_taxonomy_name]); } global $xi_shortcode_attributes, $xi_events; $xi_shortcode_attributes = $atts; $xi_events = XiQuery::get_upcoming_events($atts['events_per_page'], $terms); $template = XiUtilities::get_include_template('shortcode_event-list.php'); return $template; }
<?php global $xi_calendar_id, $xi_shortcode_attributes; $categories = XiQuery::get_categories_for_calendar($xi_calendar_id); ?> <script type="text/javascript"> jQuery(document).ready(function($) { var category_filter_id = $('#xi-categories').val(); var xi_source = { url: "<?php echo admin_url('admin-ajax.php'); ?> ", type: 'POST', data: { action: 'events_json', calendar_id: <?php echo $xi_calendar_id; ?> , category_id: category_filter_id }, error: function(jqXHR, textStatus, errorThrown) { console.log('Xi Error when fetching events.'); console.log(textStatus); console.log(errorThrown); } }; $("#xi-fullcalendar").fullCalendar({ header: {
public static function get_upcoming_events($limit = 3, $terms = false) { $args = array('post_type' => XiEvents::$post_type_name, 'meta_key' => 'xi_event_start_query_friendly', 'meta_type' => 'DATE', 'orderby' => 'meta_value title', 'order' => 'ASC', 'posts_per_page' => $limit, 'meta_query' => array('relation' => 'OR', array('key' => 'xi_event_start_query_friendly', 'value' => date('Y-m-d'), 'compare' => '>=', 'type' => 'DATE'), array('key' => 'xi_event_end_query_friendly', 'value' => date('Y-m-d'), 'compare' => '>=', 'type' => 'DATE'), array('key' => 'xi_recurrence_date', 'value' => date('Y-m-d'), 'compare' => '>=', 'type' => 'DATE'))); if ($terms !== false && is_array($terms)) { $args['tax_query'] = array(); $args['tax_query']['relation'] = "AND"; foreach ($terms as $taxonomy => $value) { if ($value['filter'] == "1") { unset($value['filtered']); $args['tax_query'][] = array('taxonomy' => $taxonomy, 'terms' => $value, 'operator' => "IN"); } } } $query = new WP_Query($args); $events = array(); $date1 = null; $date2 = null; $days_diff = null; if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); $event = new stdClass(); $event->ID = get_the_ID(); $event->permalink = get_the_permalink(); $event->title = get_the_title(); $start_timestamp = strtotime(get_post_meta($event->ID, 'xi_event_start_raw', true)); $end_timestamp = strtotime(get_post_meta($event->ID, 'xi_event_end_raw', true)); $date_format = apply_filters('xi_events_date_format', 'm/d/Y'); $event->date = date_i18n($date_format, $start_timestamp); $event->start_timestamp = $start_timestamp; $event->end_timestamp = $end_timestamp; $events[] = $event; $date1 = new DateTime(get_post_meta($event->ID, 'xi_event_start_query_friendly', true)); $date2 = new DateTime(get_post_meta($event->ID, 'xi_event_end_query_friendly', true)); $days_diff = $date2->diff($date1)->format("%a"); // List out the recurrence dates if they exist $recurrence_dates = get_post_meta($event->ID, 'xi_recurrence_date', false); foreach ($recurrence_dates as $recurrence_date) { $recurrence_event = new stdClass(); $recurrence_event->ID = $event->ID; $recurrence_event->title = $event->title; $recurrence_event->start_timestamp = strtotime($recurrence_date); if ($days_diff > 1) { // dates can get buggy with strtotime("+0 days") for some reason. $recurrence_event->end_timestamp = strtotime('+{$days_diff} days', strtotime($recurrence_date)); } else { $recurrence_event->end_timestamp = $recurrence_event->start_timestamp; } $recurrence_event->date = date_i18n($date_format, $recurrence_event->start_timestamp); $recurrence_event->permalink = $event->permalink . (parse_url($event->permalink, PHP_URL_QUERY) == NULL ? '?' : '&') . 'instance=' . $recurrence_date; $events[] = $recurrence_event; } } } // The events can be out of order and contain more events than desired now, so we need to filter down $events = XiQuery::filter_events($events, $limit); wp_reset_postdata(); return $events; }
/** * This is more or less proof of concept at this point. Needs to be significantly expanded upon. * @TODO Other filters? * @TODO Nonce input for security and stuff. */ public static function events_json() { global $xi_json_data; $calendar_id = intval($_POST['calendar_id']); $start_date = $_POST['start']; $end_date = $_POST['end']; $category_id = intval($_POST['category_id']); $results = XiQuery::get_events($calendar_id, $start_date, $end_date, $category_id); if ($results === false) { die(json_encode(array())); } $events = array(); if ($results->have_posts()) { while ($results->have_posts()) { $results->the_post(); $xi_event_id = get_the_ID(); $xi_event_meta = XiEventmeta::clean_meta(get_post_meta($xi_event_id)); // Set up the event information, conforms to requirements of fullcalendar $event = new stdClass(); $event->id = $xi_event_id; $event->title = get_the_title(); $event->allDay = boolval($xi_event_meta['xi_event_all_day']); $event->start = date_i18n('c', strtotime($xi_event_meta['xi_event_start_raw'])); $event->end = date_i18n('c', strtotime($xi_event_meta['xi_event_end_raw'])); $event->url = get_the_permalink(); // The last two fields depend on categories set up. This needs more attention for filtering. $categories = wp_get_post_terms($xi_event_id, XiEvents::$category_taxonomy_name); $event->categories = $categories; if (count($categories) == 1) { // Is there a more clever way to handle multiple categorization?? $event->className = 'category-' . $categories[0]->slug; $event->color = get_term_meta($categories[0]->term_id, 'xi_category_color', true); } elseif (count($categories) > 1) { $categories_str = ""; foreach ($categories as $category) { $categories_str .= ' category-' . $category->slug; } $event->className = trim($categories_str); } else { $event->className = 'uncategorized'; } $events[] = $event; $date1 = new DateTime($xi_event_meta['xi_event_start_query_friendly']); $date2 = new DateTime($xi_event_meta['xi_event_end_query_friendly']); $days_diff = $date2->diff($date1)->format("%a"); // List out the recurrence dates if they exist $recurrence_dates = get_post_meta($xi_event_id, 'xi_recurrence_date', false); foreach ($recurrence_dates as $recurrence_date) { $recurrence_event = new stdClass(); $recurrence_event->id = $xi_event_id; $recurrence_event->title = $event->title; $recurrence_event->allDay = $event->allDay; $recurrence_event->start = date_i18n('c', strtotime($recurrence_date)); if ($days_diff > 0) { // dates can get buggy with strtotime("+0 days") for some reason. $recurrence_event->end = date_i18n('c', strtotime('+{$days_diff} days', strtotime($recurrence_date))); } else { $recurrence_event->end = $recurrence_event->start; } $recurrence_event->categories = $event->categories; $recurrence_event->className = $event->className . " recurrence_event"; $recurrence_event->color = $event->color; $recurrence_event->url = $event->url . (parse_url($event->url, PHP_URL_QUERY) == NULL ? '?' : '&') . 'instance=' . $recurrence_date; $events[] = $recurrence_event; } } } wp_reset_postdata(); $xi_json_data = $events; include XI__PLUGIN_DIR . '/ajax/json-endpoint.php'; die; }