/** * Outputs the content of the widget * * @param array $args * @param array $instance */ public function widget($args, $instance) { $events = array(); $count = $instance['count'] ? $instance['count'] : 3; if (class_exists('WPE_WordCampEvents')) { $event_class = WPE_WordCampEvents::get_instance(); $event_args = array('count' => $count); $events = $event_class->get_events($event_args); } // ****** // Output all the widget content echo $args['before_widget']; // Output the widget title if (!empty($instance['title'])) { echo $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title']; } // Output the city name if it is available if (class_exists('WPEngine\\GeoIp')) { $geoip = WPEngine\GeoIp::instance(); if ($geoip->city()) { echo '<p class="event-field home-location">' . ucwords($geoip->city()) . '</p>'; } } echo '<ul class="events">'; foreach ($events as $event) { if ($event['website']) { $a_open = '<a href="' . $event['website'] . '">'; $a_close = '</a>'; } else { $a_open = $a_close = ''; } echo <<<HTML \t<li> \t\t<p class="event-field event-date">{$event['date']}</p> \t\t<p class="event-field event-location">{$event['location']}</p> \t\t<p class="event-field event-name">{$a_open}{$event['title']}{$a_close}</p> \t</li> HTML; } echo '</ul>'; echo $args['after_widget']; }
/** * Sorts events by distance from user * * Uses GeoIP to determine user's location * * @link http://andrew.hedges.name/experiments/haversine/ * @param array $events The events before sorting * @return array $events The events after sorting */ private function sort_events_by_distance($events) { // If we can't get the user's location, abort if (!class_exists('WPEngine\\GeoIp')) { return $events; } $geo = WPEngine\GeoIp::instance(); $lat1 = deg2rad($geo->latitude()); $lng1 = deg2rad($geo->longitude()); $radius = 3961; // Radius of the Earth in miles $dist = array(); foreach ($events as $key => $event) { // If there's no latlng, let's set it to a ridiculously huge distance if (!$event['latlng']) { $events[$key]['distance'] = 1000000; $dist[$key] = 1000000; continue; } $lng2 = deg2rad($event['latlng']['lng']); $lat2 = deg2rad($event['latlng']['lat']); $dlng = $lng2 - $lng1; $dlat = $lat2 - $lat1; $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2); $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); $d = $radius * $c; $events[$key]['distance'] = $d; $dist[$key] = $d; } // Sort the events by their timestamp array_multisort($dist, SORT_ASC, $events); return $events; }