__( 'Display upcoming events', 'am-events' )), array('width' => 400)); } /** * Front-end display of widget. * * @see WP_Widget::widget() * * @param array $args Widget arguments. * @param array $instance Saved values from database. */ public function widget( $args, $instance ) { extract( $args ); global $post; /* User-selected settings. */ $title = apply_filters('widget_title', $instance['title'] ); $venue = $instance['venue']; $category = $instance['category']; $postcount = $instance['postcount']; $template = $instance['template']; $before = $instance['before']; $after = $instance['after']; $emptyevents = $instance['emptyevents']; $offset = $instance['offset']; /* Before widget (defined by themes). */ echo $before_widget; $taxQuery = array( 'relation' => 'AND' ); /* Event category filter args */ if ($category !== "all") { $taxCategory = array( 'taxonomy' => 'am_event_categories', 'field' => 'slug', 'terms' => $category, ); $taxQuery[] = $taxCategory; } /* Venue filter args */ if ($venue !== "all") { $taxVenue = array( 'taxonomy' => 'am_venues', 'field' => 'slug', 'terms' => $venue, ); $taxQuery[] = $taxVenue; } /* WP_Query args */ $args = array( 'post_type' => 'am_event', // show only am_event cpt 'post_status' => 'publish', // show only published 'posts_per_page' => $postcount, // number of events to show 'tax_query' => $taxQuery, // sort by meta value 'am_startdate' ascending 'meta_key' => 'am_startdate', 'orderby' => 'meta_value', 'order' => 'ASC', 'meta_query' => array( array( 'key' => 'am_enddate', // display events with an end date greater than // the current time - 24hrs 'value' => date(am_get_default_date_format(), time() - intval($offset)), 'compare' => ">" // startdate > value ), ), ); /* Title of widget (before and after defined by themes). */ if ( ! empty( $title ) ) echo $before_title . $title . $after_title; echo $before; $loop = new WP_Query( $args ); if (!($loop->have_posts())) { echo $emptyevents; } else { while ($loop->have_posts()) { $loop->the_post(); $post_id = get_the_ID(); // Old template system (1.3.1 and older) $output = $this->parse_event_old($template); // new parsing system, 1.4.0 and newer $output = $this->parse_event($output); echo $output; } } echo $after; /* After widget (defined by themes). */ echo $after_widget; } /** * Parses the shortcodes, old method used for backward compatibility * @param type $content * @return type */ protected function parse_event_old($template) { // get post meta $meta_venues = am_get_the_venue(); $meta_event_categories = am_get_the_event_category(); $meta_startdate = am_get_the_startdate(); $meta_enddate = am_get_the_enddate(); // get timestamps of dates $timestamp_start = strtotime($meta_startdate); $timestamp_end = strtotime($meta_enddate); //get all the widget template data $template_startdate = date(_x('m/d/Y', 'upcoming events widget', 'am-events'), $timestamp_start); $template_enddate = date(_x('m/d/Y', 'upcoming events widget', 'am-events'), $timestamp_end); $template_starttime = date( _x('H:i', 'upcoming events widget', 'am-events'), $timestamp_start); $template_endtime = date( _x('H:i', 'upcoming events widget', 'am-events'), $timestamp_end); $template_startdayname = getWeekDay(date('N', $timestamp_start)); $template_enddayname = getWeekDay(date('N', $timestamp_end)); $template_venue = ''; if (count($meta_venues) > 0) $template_venue = $meta_venues[0]->name; $template_event_category = ''; if (count($meta_event_categories) > 0) $template_event_category = $meta_event_categories[0]->name; $template_title = get_the_title(); $template_content = get_the_content(); // Widget template tags $search = array( '{{start_day_name}}', '{{start_date}}', '{{start_time}}', '{{end_day_name}}', '{{end_date}}', '{{end_time}}', '{{title}}', '{{event_category}}', '{{venue}}', '{{content}}', '{{thumbnail}}', ); $replace = array( $template_startdayname, $template_startdate, $template_starttime, $template_enddayname, $template_enddate, $template_endtime, $template_title, $template_event_category, $template_venue, $template_content, ); return str_replace($search, $replace, $template); } /** * Parses the shortcodes * @param type $content * @return type * @since 1.4.0 */ protected function parse_event($content) { //Array of valid shortcodes $shortcodes = array( 'event-title', //The event title 'start-date', //The start date of the event (uses the date format from the feed options, if it is set. Otherwise uses the default WordPress date format) 'end-date', //The end date of the event (uses the date format from the feed options, if it is set. Otherwise uses the default WordPress date format) 'event-venue', //The event venue 'event-category', //The event category 'content', //The event content (number of words can be limited by the 'limit' attribute) 'permalink', //The event post permalink 'excerpt', //The event excerpt 'thumbnail', //The event thumbnail 'meta', //Custom meta field 'if', //Conditional tag ); $regex = '/\\[(\\[?)(' . implode( '|', $shortcodes ) . ')(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*+(?:\\[(?!\\/\\2\\])[^\\[]*+)*+)\\[\\/\\2\\])?)(\\]?)/s'; return preg_replace_callback( $regex, array( $this, 'process_shortcode' ), $content ); } /** * Parses a shortcode, returning the appropriate event information * Much of this code is 'borrowed' from WordPress' own shortcode handling stuff! */ protected function process_shortcode( $m ) { if ( '[' == $m[1] && ']' == $m[6] ) return substr( $m[0], 1, -1 ); //Extract any attributes contained in the shortcode extract( shortcode_atts( array( 'format' => '', 'limit' => '0', 'size' => 'post-thumbnail', 'link' => 'false', 'key' => '', 'cond' => '', ), shortcode_parse_atts( $m[3] ) ) ); //Sanitize the attributes $format = esc_attr( $format ); $cond = esc_attr( $cond ); $size = esc_attr( $size ); $key = esc_attr( $key ); $limit = absint( $limit ); $link = ( 'true' === $link ); // Do the appropriate stuff depending on which shortcode we're looking at. // See valid shortcode list (above) for explanation of each shortcode switch ( $m[2] ) { case 'event-title': $title = esc_html( trim( get_the_title())); //If a word limit has been set, trim the title to the required length if ( 0 != $limit ) { preg_match( '/([\S]+\s*){0,' . $limit . '}/', $title , $title ); $title = trim( $title[0] ); } if ($link) { return $m[1] . '' .$title. '' . $m[6]; } else { return $m[1] . $title . $m[6]; } case 'thumbnail': $thumbnail = get_the_post_thumbnail(get_the_ID(), $size ); return $m[1] . $thumbnail. $m[6]; case 'content': $content = get_the_content(); //If a word limit has been set, trim the title to the required length if ( 0 != $limit ) { preg_match( '/([\S]+\s*){0,' . $limit . '}/', $content, $content ); $content = trim( $content[0] ); } return $m[1] . $content . $m[6]; case 'permalink': return $m[1] . get_permalink() . $m[6]; case 'excerpt': $excerpt = get_the_excerpt(); if ( 0 != $limit ) { preg_match( '/([\S]+\s*){0,' . $limit . '}/', $excerpt, $excerpt ); $excerpt = trim( $excerpt[0] ); } return $m[1] . get_the_excerpt() . $m[6]; case 'event-category': $categoryArray = am_get_the_event_category(); if (count($categoryArray) > 0) { if ($link) return $m[1] . '' . $categoryArray[0]->name . '' . $m[6]; else return $m[1] . $categoryArray[0]->name . $m[6]; } else { return '-'; } case 'event-venue': $venueArray = am_get_the_venue(); if (count($venueArray) > 0) { if ($link) return $m[1] . '' . $venueArray[0]->name . '' . $m[6]; else return $m[1] . $venueArray[0]->name . $m[6]; } else { return '-'; } case 'start-date': $startdate = am_get_the_startdate(); $format = $format === '' ? "m/d/Y H:i" : $format; return $m[1] . date_i18n( $format, strtotime($startdate) ) . $m[6]; case 'end-date': $enddate = am_get_the_enddate(); $format = $format === '' ? "m/d/Y H:i" : $format; return $m[1] . date_i18n( $format, strtotime($enddate) ) . $m[6]; case 'meta': $meta = get_post_meta ( get_the_ID(), $key, true ); return $m[1] . $meta . $m[6]; case 'if': switch ($cond) { case 'startdate-not-enddate': $result = am_get_the_startdate() !== am_get_the_enddate() ? $m[1] . $m[5] . $m[6] : ''; return $this->parse_event($result); case 'startday-not-endday': $start_day = date('mdY', strtotime(am_get_the_startdate())); $end_day = date('mdY', strtotime(am_get_the_enddate())); $result = $start_day !== $end_day ? $m[1] . $m[5] . $m[6] : ''; return $this->parse_event($result); case 'startdate-is-enddate': $result = am_get_the_startdate() === am_get_the_enddate() ? $m[1] . $m[5] . $m[6] : ''; return $this->parse_event($result); case 'has-venue': $result = am_get_the_venue() == true ? $m[1] . $m[5] . $m[6] : ''; return $this->parse_event($result); case 'has-category': $result = am_get_the_event_category() == true ? $m[1] . $m[5] . $m[6] : ''; return $this->parse_event($result); case 'startday-is-endday': $start_day = date('mdY', strtotime(am_get_the_startdate())); $end_day = date('mdY', strtotime(am_get_the_enddate())); $result = $start_day === $end_day ? $m[1] . $m[5] . $m[6] : ''; return $this->parse_event($result); default: return $this->parse_event($m[1] . $m[5] . $m[6]); } } } /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. */ public function form( $instance ) { $default_template = "
[start-date format='D d.m.Y H:s'] - [end-date format='D d.m.Y H:s']
[event-category], [event-venue]
[content limit=25]... read more...
"; $defaults = array( 'title' => __('Upcoming Events', 'am-events'), 'category' => 'all', 'venue' => 'all', 'postcount' => '3', 'offset' => 86400, // 24 hours 'emptyevents' => 'No upcoming events
', 'template' => $default_template, 'after' => '' . __('See More Events ->', 'am-events') . '
', 'before' => '', ); $instance = wp_parse_args( (array) $instance, $defaults ); $title = $instance[ 'title' ]; $category = $instance[ 'category' ]; $venue = $instance[ 'venue' ]; $emptyevents= $instance[ 'emptyevents' ]; $template = $instance[ 'template' ]; $before = $instance[ 'before' ]; $after = $instance[ 'after' ]; $offset = $instance[ 'offset' ]; $args = array( 'hide_empty' => false ); $categories = get_terms('am_event_categories', $args); $venues = get_terms('am_venues', $args); ?>