public static function action_wp_ajax_betareno_get_ideas() { // default is within 5 miles of downtown Reno $default_args = array('lat' => '39.524435', 'lng' => '-119.811745', 'r' => '5'); $args = wp_parse_args($_GET, $default_args); extract($args); $response = array('code' => 200, 'message' => 'ok'); $geo_query = array('object_name' => 'post', 'map_post_type' => 'idea', 'near_lat' => $lat, 'near_lng' => $lng, 'radius_mi' => $r); $idea_locations = GeoMashupDB::get_object_locations($geo_query); $response['ideas'] = array(); foreach ($idea_locations as $idea_location) { $idea = array('ID' => $idea_location->object_id, 'what' => $idea_location->label, 'latitude' => $idea_location->lat, 'longitude' => $idea_location->lng); // Who $actor_terms = wp_get_object_terms($idea_location->object_id, 'actor'); if (is_wp_error($actor_terms)) { $idea['who'] = ''; } else { $idea['who'] = $actor_terms[0]->name; } $idea['when'] = get_post_meta($idea_location->object_id, 'when', true); $idea['votes'] = 1; // Photos $idea['before_photo_url'] = ''; $before_photo_attachments = get_posts(array('post_type' => 'attachment', 'post_mime_type' => 'image', 'post_parent' => $idea_location->object_id, 'meta_key' => 'photo_type', 'meta_value' => 'before')); if (!empty($before_photo_attachments)) { list($idea['before_photo_url'], $width, $height) = wp_get_attachment_image_src($before_photo_attachments[0]->ID); } $idea['after_photo_url'] = ''; $after_photo_attachments = get_posts(array('post_type' => 'attachment', 'post_mime_type' => 'image', 'post_parent' => $idea_location->object_id, 'meta_key' => 'photo_type', 'meta_value' => 'after')); if (!empty($after_photo_attachments)) { list($idea['after_photo_url'], $width, $height) = wp_get_attachment_image_src($after_photo_attachments[0]->ID); } $response['ideas'][] = $idea; } echo json_encode($response); exit; }
/** * List located posts by area template tag. * * Returns an HTML list of all located posts by country and state. May try to look up * this information when absent. * * @since 1.2 * @link http://github.com/cyberhobo/wordpress-geo-mashup/wiki/Tag-Reference#list-located-posts-by-area * * @param string|array $args Template tag arguments. * @return string List HTML. */ public static function list_located_posts_by_area($args) { static $instance_count = 1; $args = wp_parse_args($args); if ($instance_count > 1) { $id_suffix = '-' . $instance_count; } else { $id_suffix = ''; } $list_html = '<div id="gm-area-list' . $id_suffix . '" class="gm-area-list">'; $countries = GeoMashupDB::get_distinct_located_values('country_code', array('object_name' => 'post')); $country_count = count($countries); $country_heading = ''; foreach ($countries as $country) { if ($country_count > 1) { $country_name = GeoMashupDB::get_administrative_name($country->country_code); $country_name = $country_name ? $country_name : $country->country_code; $country_heading = '<h3 id="' . $country->country_code . $id_suffix . '">' . $country_name . '</h3>'; } $states = GeoMashupDB::get_distinct_located_values('admin_code', array('country_code' => $country->country_code, 'object_name' => 'post')); if (empty($states)) { $states = array((object) array('admin_code' => null)); } foreach ($states as $state) { $location_query = array('object_name' => 'post', 'country_code' => $country->country_code, 'admin_code' => $state->admin_code, 'sort' => 'post_title'); $post_locations = GeoMashupDB::get_object_locations($location_query); if (count($post_locations) > 0) { if (!empty($country_heading)) { $list_html .= $country_heading; $country_heading = ''; } if (null != $states[0]->admin_code) { $state_name = GeoMashupDB::get_administrative_name($country->country_code, $state->admin_code); $state_name = $state_name ? $state_name : $state->admin_code; $list_html .= '<h4 id="' . $country->country_code . '-' . $state->admin_code . $id_suffix . '">' . $state_name . '</h4>'; } $list_html .= '<ul class="gm-index-posts">'; foreach ($post_locations as $post_location) { $list_html .= '<li><a href="' . get_permalink($post_location->object_id) . '">' . $post_location->label . '</a>'; if (isset($args['include_address']) && $args['include_address'] == 'true') { $list_html .= '<p>' . $post_location->address . '</p>'; } $list_html .= '</li>'; } $list_html .= '</ul>'; } } } $list_html .= '</div>'; return $list_html; }
/** * 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; }
function test_tax_cat_query() { $tag_taxonomy = WP_UnitTest_Factory_For_Term::DEFAULT_TAXONOMY; $cat_taxonomy = 'category'; // Hierarchical terms must be inserted by ID $cat1_ids = wp_insert_term('category1', $cat_taxonomy); $cat1_term_id = $cat1_ids['term_id']; $bare_post_ids = $this->factory->post->create_many(2); GeoMashupDB::set_object_location('post', $bare_post_ids[0], $this->rand_location(), false); wp_set_post_terms($bare_post_ids[1], $cat1_term_id, $cat_taxonomy); GeoMashupDB::set_object_location('post', $bare_post_ids[1], $this->rand_location(), false); $tag1_post_ids = $this->factory->post->create_many(2); wp_set_post_terms($tag1_post_ids[0], 'tag1', $tag_taxonomy); GeoMashupDB::set_object_location('post', $tag1_post_ids[0], $this->rand_location(), false); wp_set_post_terms($tag1_post_ids[1], 'tag1', $tag_taxonomy); wp_set_post_terms($tag1_post_ids[1], $cat1_term_id, $cat_taxonomy); GeoMashupDB::set_object_location('post', $tag1_post_ids[1], $this->rand_location(), false); $tag2_post_ids = $this->factory->post->create_many(2); wp_set_post_terms($tag2_post_ids[0], 'tag2', $tag_taxonomy); GeoMashupDB::set_object_location('post', $tag2_post_ids[0], $this->rand_location(), false); wp_set_post_terms($tag2_post_ids[1], 'tag2', $tag_taxonomy); wp_set_post_terms($tag2_post_ids[1], $cat1_term_id, $cat_taxonomy); GeoMashupDB::set_object_location('post', $tag2_post_ids[1], $this->rand_location(), false); $tag1_locs = GeoMashupDB::get_object_locations(array('map_cat' => 'category1', 'tax_query' => array(array('taxonomy' => $tag_taxonomy, 'terms' => 'tag1', 'field' => 'slug')))); $this->assertEquals(1, count($tag1_locs)); $this->assertContains($tag1_post_ids[1], wp_list_pluck($tag1_locs, 'object_id')); $tag_locs = GeoMashupDB::get_object_locations(array('map_cat' => 'category1', 'tax_query' => array(array('taxonomy' => $tag_taxonomy, 'terms' => array('tag1', 'tag2'), 'field' => 'slug')))); $this->assertEquals(2, count($tag_locs)); $this->assertContains($tag1_post_ids[1], wp_list_pluck($tag_locs, 'object_id')); $this->assertContains($tag2_post_ids[1], wp_list_pluck($tag_locs, 'object_id')); }