public static function nearestContacts($how_many = 5, $target = NULL)
 {
     /* returns a multidimensional array of x contacts, sorted by distance from target, containing an innter array of details */
     if (isset($target)) {
         // set up variables
         $distance_array = array();
         // grab all registered users from db
         require_once __DIR__ . '/Database.php';
         $days_of_week = array(1 => 'mondays', 2 => 'tuesdays', 3 => 'wednesdays', 4 => 'thursdays', 5 => 'fridays', 6 => 'saturdays', 7 => 'sundays');
         // set variabe for earliest_time field corresponding to 'today'
         $earliest_time_today = 'earliest_time_' . $days_of_week[current_time(date('N', time()))];
         $latest_time_today = 'latest_time_' . $days_of_week[current_time(date('N', time()))];
         $result_set = \ZwsContactsDatabase\Database::getAllRecordsWhereIsNot('id', array('field' => $earliest_time_today, 'value' => 'UNAVL'));
         // loop postcodes and get distances
         if (!empty($result_set) && $result_set !== false && is_array($result_set)) {
             foreach ($result_set as $key => $row) {
                 // get distance from contact to each target. Returns false if error or target not found.
                 $distance = \ZwsContactsDatabase\DistanceCalculator::getDistance(sanitize_text_field($target), sanitize_text_field($row->postcode));
                 // if distance successfully returned add to the distance array
                 if ($distance !== false) {
                     $distance_array[sanitize_text_field($row->id)] = array('distance' => $distance, 'postcode' => sanitize_text_field($row->postcode), 'lng' => sanitize_text_field($row->lng), 'lat' => sanitize_text_field($row->lat), 'first_name' => sanitize_text_field($row->first_name), 'last_name' => sanitize_text_field($row->last_name), 'phone' => sanitize_text_field($row->phone), 'email' => sanitize_email($row->email), $earliest_time_today => sanitize_text_field($row->{$earliest_time_today}), $latest_time_today => sanitize_text_field($row->{$latest_time_today}), 'max_radius' => sanitize_text_field($row->max_radius), 'extra_info' => esc_textarea($row->extra_info));
                 }
             }
             if (!empty($distance_array)) {
                 $top_n = [];
                 $c = 0;
                 foreach ($distance_array as $key => $value) {
                     // if within max radius, and available, and within requested size of resultset
                     if (apply_filters('zws_filter_enforce_numeric', $value['distance']) <= apply_filters('zws_filter_enforce_numeric', $value['max_radius']) && $c < $how_many) {
                         // add the id to the actual dataset, at it will no longer be available as the key
                         $value['id'] = sanitize_text_field($key);
                         $top_n[$c] = $value;
                         $c++;
                     }
                 }
                 return $top_n;
             }
         }
         return false;
     }
 }
 public static function display_nearest($target_postcode)
 {
     // check params have been passed
     if (!isset($target_postcode)) {
         return false;
     }
     require_once __DIR__ . '/DistanceCalculator.php';
     require_once __DIR__ . '/Helpers.php';
     $options = get_site_option(self::OPTIONS_LABEL);
     $success = false;
     $how_many_contacts = 5;
     $contacts_array = \ZwsContactsDatabase\DistanceCalculator::nearestContacts($how_many_contacts, $target_postcode);
     if ($contacts_array !== false) {
         $contacts_array_safe = [];
         // display the  elements
         echo '<div class="contact-list"><h2>' . $how_many_contacts . ' Closest Contacts</h2>';
         echo '<small><a href="' . \ZwsContactsDatabase\Helpers::set_url_query_cleared(array('postback' => 'false')) . '">Back to administration dashboard</a></small>';
         // set variabe for earliest_time field corresponding to 'today'
         $days_of_week = array(1 => 'mondays', 2 => 'tuesdays', 3 => 'wednesdays', 4 => 'thursdays', 5 => 'fridays', 6 => 'saturdays', 7 => 'sundays');
         $earliest_time_today = 'earliest_time_' . $days_of_week[current_time(date('N', time()))];
         $latest_time_today = 'latest_time_' . $days_of_week[current_time(date('N', time()))];
         foreach ($contacts_array as $key => $value) {
             // ensure variables from database are safe to output and add them to the contacts array. Only include contacts within their specified radius from target ...
             // ... only those who are available TODAY are returned from the DistanceCalculator::nearestContacts method, above.
             // ... ALL of TODAY's results (up to max results) are displayed, with the available times also printed.
             $contacts_array_safe[$key]['id'] = sanitize_text_field($value['id']);
             $contacts_array_safe[$key]['distance'] = apply_filters('zws_filter_enforce_numeric', $value['distance']);
             $contacts_array_safe[$key]['postcode'] = apply_filters('zws_filter_basic_sanitize', $value['postcode']);
             $contacts_array_safe[$key]['lat'] = apply_filters('zws_filter_basic_sanitize', $value['lat']);
             $contacts_array_safe[$key]['lng'] = apply_filters('zws_filter_basic_sanitize', $value['lng']);
             $contacts_array_safe[$key]['first_name'] = apply_filters('zws_filter_basic_sanitize', $value['first_name']);
             $contacts_array_safe[$key]['last_name'] = apply_filters('zws_filter_basic_sanitize', $value['last_name']);
             $contacts_array_safe[$key]['phone'] = apply_filters('zws_filter_enforce_numeric', $value['phone']);
             $contacts_array_safe[$key]['email'] = sanitize_email($value['email']);
             $contacts_array_safe[$key][$earliest_time_today] = apply_filters('zws_filter_basic_sanitize', $value[$earliest_time_today]);
             $contacts_array_safe[$key][$latest_time_today] = apply_filters('zws_filter_basic_sanitize', $value[$latest_time_today]);
             $contacts_array_safe[$key]['max_radius'] = apply_filters('zws_filter_enforce_numeric', $value['max_radius']);
             $contacts_array_safe[$key]['extra_info'] = nl2br(stripslashes(apply_filters('zws_filter_text_with_linebreak', $value['extra_info'])));
         }
         // add contacts array to map config
         $map_config['contacts_array_safe'] = $contacts_array_safe;
         // set up additional map config
         $map_config['target_postcode'] = $target_postcode;
         $map_config['contact_icon_url'] = apply_filters('zws_filter_basic_sanitize', $options['zws_contacts_database_plugin_map_contact_icon_url']);
         $map_config['target_icon_url'] = apply_filters('zws_filter_basic_sanitize', $options['zws_contacts_database_plugin_map_target_icon_url']);
         $map_config['base_icon_url'] = apply_filters('zws_filter_basic_sanitize', $options['zws_contacts_database_plugin_map_base_icon_url']);
         $map_config['base_postcode'] = apply_filters('zws_filter_sanitize_postcode', $options['zws_contacts_database_plugin_base_postcode']);
         $map_config['base_name'] = apply_filters('zws_filter_basic_sanitize', $options['zws_contacts_database_plugin_base_name']);
         $map_config['base_coordinates'] = $options['zws_contacts_database_plugin_base_coordinates'];
         $map_config['users_id'] = get_current_user_id();
         $map_config['zoom'] = apply_filters('zws_filter_validate_integer', $options['zws_contacts_database_plugin_google_map_zoom']);
         // display the map
         if (self::display_map($map_config)) {
             $success = true;
         }
         echo '<ol class="contact-info-list">';
         $c = 0;
         // counter to give each entry's available times fields a unique class name for jQuery
         foreach ($contacts_array_safe as $key => $value) {
             // display textual elements
             echo '<li style="margin-bottom:1em;">';
             echo '<ul class="contact-info-list-inner">';
             echo '<li>Distance from target: ' . $value['distance'] . ' miles</li>';
             echo '<li>Name of contact: ' . stripslashes($value['first_name']) . ' ' . stripslashes($value['last_name']) . '</li>';
             echo '<li>Postcode of contact: ' . $value['postcode'] . '</li>';
             echo '<li>Phone of contact: <a href="tel:' . $value['phone'] . '">' . $value['phone'] . '</a></li>';
             echo '<li>Email of contact: <a href="mailto:' . $value['email'] . '">' . $value['email'] . '</a></li>';
             echo '<li>Extra notes: ' . $value['extra_info'] . '</li>';
             echo '<li><button class="modal_opener_' . $c . '">View time that ' . $value['first_name'] . ' is available</button><div class="zws-contacts-db-times-available">' . '<ul class="contact-info-list-inner_' . $c . '">';
             echo '<li>Earliest: ' . $value[$earliest_time_today] . '</li>';
             echo '<li style="border-bottom:1px solid silver;">Latest: ' . $value[$latest_time_today] . '</li>';
             echo '</ul></div></li>';
             echo '</ul>';
             echo '</li>';
             $c++;
         }
         echo '</ol><div>';
     }
     // return true if map has successfully displayed, otherwise false.
     return true ? $success : false;
 }