/**
  * Run a search query.
  * 
  * @since 1.5
  * @uses apply_filters() geo_mashup_search_query_args Filter the geo query arguments.
  *
  * @param string|array $args Search parameters.
  * @return array Search results.
  **/
 public function query($args)
 {
     $default_args = array('object_name' => 'post', 'object_ids' => null, 'exclude_object_ids' => null, 'units' => 'km', 'location_text' => '', 'radius' => null, 'sort' => 'distance_km ASC');
     $this->query_vars = wp_parse_args($args, $default_args);
     /** @var $units */
     extract($this->query_vars);
     $this->results = array();
     $this->result_count = 0;
     $this->result = null;
     $this->current_result = -1;
     $this->units = $units;
     $this->max_km = 20000;
     $this->distance_factor = 'km' == $units ? 1 : self::MILES_PER_KILOMETER;
     $this->near_location = GeoMashupDB::blank_location(ARRAY_A);
     $geo_query_args = wp_array_slice_assoc($this->query_vars, array('object_name', 'sort', 'exclude_object_ids', 'limit'));
     if (!empty($near_lat) and !empty($near_lng)) {
         $this->near_location['lat'] = $near_lat;
         $this->near_location['lng'] = $near_lng;
     } else {
         if (!empty($location_text)) {
             $geocode_text = empty($geolocation) ? $location_text : $geolocation;
             if (!GeoMashupDB::geocode($geocode_text, $this->near_location)) {
                 // No search center was found, we can't continue
                 return $this->results;
             }
         } else {
             // No coordinates to search near
             return $this->results;
         }
     }
     $radius_km = $this->max_km;
     if (!empty($radius)) {
         $radius_km = abs($radius) / $this->distance_factor;
     }
     $geo_query_args['radius_km'] = $radius_km;
     $geo_query_args['near_lat'] = $this->near_location['lat'];
     $geo_query_args['near_lng'] = $this->near_location['lng'];
     if (isset($map_cat)) {
         $geo_query_args['map_cat'] = $map_cat;
     }
     $geo_query_args = apply_filters('geo_mashup_search_query_args', $geo_query_args);
     $this->results = GeoMashupDB::get_object_locations($geo_query_args);
     $this->result_count = count($this->results);
     if ($this->result_count > 0) {
         $this->max_km = $this->results[$this->result_count - 1]->distance_km;
     } else {
         $this->max_km = $radius_km;
     }
     return $this->results;
 }
 /**
  * Store the inline location from a save location shortcode before it is removed.
  * 
  * @since 1.3
  *
  * @param array $shortcode_match 
  * @return The matched content, or an empty string if it was a save location shortcode.
  */
 public function replace_save_pre_shortcode($shortcode_match)
 {
     $content = $shortcode_match[0];
     $tag_index = array_search('geo_mashup_save_location', $shortcode_match);
     if ($tag_index !== false) {
         // There is an inline location - save the attributes
         $this->inline_location = shortcode_parse_atts(stripslashes($shortcode_match[$tag_index + 1]));
         // If lat and lng are missing, try to geocode based on address
         $success = false;
         if ((empty($this->inline_location['lat']) or empty($this->inline_location['lng'])) and !empty($this->inline_location['address'])) {
             $query = $this->inline_location['address'];
             $this->inline_location = GeoMashupDB::blank_object_location(ARRAY_A);
             $success = GeoMashupDB::geocode($query, $this->inline_location);
             if (!$success) {
                 // Delay and try again
                 sleep(1);
                 $success = GeoMashupDB::geocode($query, $this->inline_location);
             }
         } else {
             if (is_numeric($this->inline_location['lat']) and is_numeric($this->inline_location['lng'])) {
                 // lat and lng were supplied
                 $success = true;
             }
         }
         if ($success) {
             // Remove the tag
             $content = '';
         } else {
             $message = is_wp_error(GeoMashupDB::$geocode_error) ? GeoMashupDB::$geocode_error->get_error_message() : __('Address not found - try making it less detailed', 'GeoMashup');
             $content = str_replace(']', ' geocoding_error="' . $message . '"]', $content);
             $this->inline_location = null;
         }
     }
     return $content;
 }
 /**
  * Save an object location from data posted by the location editor.
  * 
  * @since 1.3
  * @access public
  * @uses GeoMashupDB::set_object_location()
  * @uses GeoMashupDB::delete_location()
  *
  * @param string $object_name The name of the object being edited.
  * @param string $object_id The ID of the object being edited.
  * @return bool|WP_Error True or a WordPress error.
  */
 function save_posted_object_location($object_name, $object_id)
 {
     // Check the nonce
     if (empty($_POST['geo_mashup_nonce']) || !wp_verify_nonce($_POST['geo_mashup_nonce'], 'geo-mashup-edit')) {
         return new WP_Error('invalid_request', __('Object location not saved - invalid request.', 'GeoMashup'));
     }
     $action = $this->get_submit_action();
     if ('save' == $action or 'geocode' == $action) {
         $date_string = $_POST['geo_mashup_date'] . ' ' . $_POST['geo_mashup_hour'] . ':' . $_POST['geo_mashup_minute'] . ':00';
         $geo_date = date('Y-m-d H:i:s', strtotime($date_string));
         $post_location = array();
         // If PHP has added slashes, WP will do it again before saving
         $post_location['saved_name'] = stripslashes($_POST['geo_mashup_location_name']);
         if ('geocode' == $action) {
             $status = GeoMashupDB::geocode($_POST['geo_mashup_search'], $post_location);
             if ($status != 200) {
                 $post_location = array();
             }
         } else {
             if (!empty($_POST['geo_mashup_select'])) {
                 $selected_items = explode('|', $_POST['geo_mashup_select']);
                 $post_location = intval($selected_items[0]);
             } else {
                 $post_location['id'] = $_POST['geo_mashup_location_id'];
                 list($lat, $lng) = split(',', $_POST['geo_mashup_location']);
                 $post_location['lat'] = trim($lat);
                 $post_location['lng'] = trim($lng);
                 $post_location['geoname'] = $_POST['geo_mashup_geoname'];
                 $post_location['address'] = stripslashes($_POST['geo_mashup_address']);
                 $post_location['postal_code'] = $_POST['geo_mashup_postal_code'];
                 $post_location['country_code'] = $_POST['geo_mashup_country_code'];
                 $post_location['admin_code'] = $_POST['geo_mashup_admin_code'];
                 $post_location['sub_admin_code'] = $_POST['geo_mashup_sub_admin_code'];
                 $post_location['locality_name'] = $_POST['geo_mashup_locality_name'];
             }
         }
         if (!empty($post_location)) {
             $error = GeoMashupDB::set_object_location($object_name, $object_id, $post_location, true, $geo_date);
             if (is_wp_error($error)) {
                 return $error;
             }
         }
     } else {
         if ('delete' == $action) {
             $error = GeoMashupDB::delete_object_location($object_name, $object_id);
             if (is_wp_error($error)) {
                 return $error;
             }
         }
     }
     return true;
 }