Пример #1
0
 /**
  * Get locations of objects.
  *
  * <code>
  * $results = GeoMashupDB::get_object_locations( array( 
  * 	'object_name' => 'user', 
  * 	'minlat' => 30,
  * 	'maxlat' => 40, 
  * 	'minlon' => -106, 
  * 	'maxlat' => -103 ) 
  * );
  * </code>
  * 
  * @since 1.3
  *
  * @param string $query_args Override default args.
  * @return array Array of matching rows.
  */
 public static function get_object_locations($query_args = '')
 {
     global $wpdb;
     $default_args = array('minlat' => null, 'maxlat' => null, 'minlon' => null, 'maxlon' => null, 'radius_km' => null, 'radius_mi' => null, 'map_cat' => null, 'tax_query' => null, 'map_post_type' => 'any', 'object_name' => 'post', 'show_future' => 'false', 'suppress_filters' => false, 'limit' => 0, 'map_offset' => 0);
     $query_args = wp_parse_args($query_args, $default_args);
     // Construct the query
     $object_name = $query_args['object_name'];
     $object_store = self::object_storage($object_name);
     if (empty($object_store)) {
         return null;
     }
     // Giving tables an alias was a mistake, now filters depend on them
     $field_string = "gmlr.object_id, gmlr.geo_date, o.{$object_store['label_column']} as label, gml.*";
     $table_string = "{$wpdb->prefix}geo_mashup_locations gml " . "INNER JOIN {$wpdb->prefix}geo_mashup_location_relationships gmlr " . $wpdb->prepare('ON gmlr.object_name = %s AND gmlr.location_id = gml.id ', $object_name) . "INNER JOIN {$object_store['table']} o ON o.{$object_store['id_column']} = gmlr.object_id";
     $wheres = array();
     $groupby = '';
     $having = '';
     if ('post' == $object_name) {
         $field_string .= ', o.post_author';
         if ($query_args['show_future'] == 'true') {
             $wheres[] = 'post_status in ( \'publish\',\'future\' )';
         } else {
             if ($query_args['show_future'] == 'only') {
                 $wheres[] = 'post_status = \'future\'';
             } else {
                 $wheres[] = 'post_status = \'publish\'';
             }
         }
     } else {
         if ('comment' == $object_name) {
             $wheres[] = 'comment_approved = \'1\'';
         }
     }
     $location_args = wp_array_slice_assoc($query_args, array_keys(GM_Location_Query::get_defaults()));
     $location_query = new GM_Location_Query($location_args);
     // Handle inclusion and exclusion of terms
     if (!empty($query_args['tax_query']) and is_array($query_args['tax_query'])) {
         $tax_query = $query_args['tax_query'];
     } else {
         $tax_query = array();
     }
     if (!empty($query_args['map_cat'])) {
         $cats = preg_split('/[,\\s]+/', $query_args['map_cat']);
         $escaped_include_ids = array();
         $escaped_exclude_ids = array();
         foreach ($cats as $cat) {
             if (is_numeric($cat)) {
                 if ($cat < 0) {
                     $escaped_exclude_ids[] = abs($cat);
                     $escaped_exclude_ids = array_merge($escaped_exclude_ids, get_term_children($cat, 'category'));
                 } else {
                     $escaped_include_ids[] = intval($cat);
                     $escaped_include_ids = array_merge($escaped_include_ids, get_term_children($cat, 'category'));
                 }
             } else {
                 // Slugs might begin with a dash, so we only include them
                 $term = get_term_by('slug', $cat, 'category');
                 if ($term) {
                     $escaped_include_ids[] = $term->term_id;
                     $escaped_include_ids = array_merge($escaped_include_ids, get_term_children($term->term_id, 'category'));
                 }
             }
         }
         if (!empty($escaped_include_ids)) {
             $tax_query[] = array('taxonomy' => 'category', 'terms' => $escaped_include_ids, 'field' => 'term_id');
         }
         if (!empty($escaped_exclude_ids)) {
             $tax_query[] = array('taxonomy' => 'category', 'terms' => $escaped_exclude_ids, 'operator' => 'NOT IN', 'field' => 'term_id');
         }
     }
     // end if map_cat exists
     if (!empty($tax_query)) {
         $tax_clauses = get_tax_sql($tax_query, 'o', $object_store['id_column']);
         $table_string .= $tax_clauses['join'];
         $wheres[] = preg_replace('/^ AND/', '', $tax_clauses['where']);
         $groupby = 'GROUP BY gmlr.object_id';
     }
     if ('post' == $object_name) {
         // Handle inclusion and exclusion of post types
         if ('any' == $query_args['map_post_type']) {
             $include_post_types = '';
             $searchable_post_types = GeoMashup::get_searchable_post_types();
             if (!empty($searchable_post_types)) {
                 $include_post_types .= "o.post_type IN ('" . join("', '", array_map('esc_sql', $searchable_post_types)) . "')";
             }
             $wheres[] = $include_post_types;
         } else {
             if (!is_array($query_args['map_post_type'])) {
                 $query_args['map_post_type'] = preg_split('/[,\\s]+/', $query_args['map_post_type']);
             }
             $wheres[] = "o.post_type IN ('" . join("', '", $query_args['map_post_type']) . "')";
         }
     }
     if (!empty($query_args['object_id'])) {
         $wheres[] = 'gmlr.object_id = ' . esc_sql($query_args['object_id']);
     } else {
         if (!empty($query_args['object_ids'])) {
             $wheres[] = 'gmlr.object_id IN ( ' . esc_sql($query_args['object_ids']) . ' )';
         }
     }
     if (!empty($query_args['exclude_object_ids'])) {
         $wheres[] = 'gmlr.object_id NOT IN ( ' . esc_sql($query_args['exclude_object_ids']) . ' )';
     }
     list($l_cols, $l_join, $l_where, $l_groupby) = $location_query->get_sql('o', $object_store['id_column']);
     $field_string .= $l_cols;
     $table_string .= $l_join;
     if (empty($groupby) and !empty($l_groupby)) {
         $groupby = 'GROUP BY ' . $l_groupby;
     }
     $where = empty($wheres) ? '' : 'WHERE ' . implode(' AND ', $wheres) . $l_where;
     $sort = isset($query_args['sort']) ? $query_args['sort'] : $object_store['sort'];
     $sort = empty($sort) ? '' : 'ORDER BY ' . esc_sql($sort);
     $offset = absint($query_args['map_offset']);
     $limit = absint($query_args['limit']);
     if ($limit or $offset) {
         $limit = " LIMIT {$offset},{$limit}";
     } else {
         $limit = '';
     }
     if (!$query_args['suppress_filters']) {
         $field_string = apply_filters('geo_mashup_locations_fields', $field_string);
         $table_string = apply_filters('geo_mashup_locations_join', $table_string);
         $where = apply_filters('geo_mashup_locations_where', $where);
         $sort = apply_filters('geo_mashup_locations_orderby', $sort);
         $groupby = apply_filters('geo_mashup_locations_groupby', $groupby);
         $limit = apply_filters('geo_mashup_locations_limits', $limit);
         $suppress_post_filters = defined('GEO_MASHUP_SUPPRESS_POST_FILTERS') && GEO_MASHUP_SUPPRESS_POST_FILTERS;
         if ('post' === $object_name and !$suppress_post_filters and isset($GLOBALS['sitepress'])) {
             // Ok, we're catering to WPML here. If we ever integrate with a WP_Query object for posts,
             // this could be made more general
             // This filter will include all translatable post types, I hope
             add_filter('get_translatable_documents', array(__CLASS__, 'wpml_filter_get_translatable_documents'));
             // Apply post query filters, changing posts table references to our alias
             // As of WPML 2.9 these calls can trigger undefined variable notices
             $table_string = $GLOBALS['sitepress']->posts_join_filter($table_string, null);
             $table_string = str_replace($wpdb->posts . '.', 'o.', $table_string);
             $where = $GLOBALS['sitepress']->posts_where_filter($where, null);
             $where = str_replace($wpdb->posts . '.', 'o.', $where);
             remove_filter('get_translatable_documents', array(__CLASS__, 'wpml_filter_get_translatable_documents'));
         }
     }
     $query_string = "SELECT {$field_string} FROM {$table_string} {$where} {$groupby} {$having} {$sort} {$limit}";
     $wpdb->query($query_string);
     return $wpdb->last_result;
 }
Пример #2
0
 function test_gm_location_query()
 {
     global $wpdb;
     $unlocated_user_ids = $this->factory->user->create_many(2);
     $nv_user_id = $this->factory->user->create();
     $nv_location = $this->get_nv_test_location();
     GeoMashupDB::set_object_location('user', $nv_user_id, $nv_location, false);
     $location_query = new GM_Location_Query(array('minlat' => $nv_location->lat - 1, 'maxlat' => $nv_location->lat + 1, 'minlon' => $nv_location->lng - 1, 'maxlon' => $nv_location->lng + 1));
     list($cols, $join, $where, $groupby) = $location_query->get_sql($wpdb->users, 'ID');
     $this->assertNotContains('distance_km', $cols, 'Got a distance_km column for a non-radius query.');
     $this->assertEmpty($groupby, 'Got a groupby value for a non-radius query.');
     $sql = "SELECT {$wpdb->users}.ID\n\t\t\tFROM {$wpdb->users}{$join}\n\t\t\tWHERE 1=1{$where}\n\t\t";
     $results = $wpdb->get_results($sql);
     $this->assertCount(1, $results);
     $this->assertEquals($nv_user_id, $results[0]->ID);
 }