/** * GMW PT function - add location to GEO my WP * * use this function if you want to add location using a custom form. * * function accepts an accociative array as below: * * $args = array ( * 'post_id' => false, //must pass post id in order to work * 'address' => false, //can be eiter single line of full address field or an array of the adress components ex ( array( 'street' => '5060 lincoln st', 'city' => 'hollywood', 'state' => 'florida' ) * 'additional_info' => array( 'phone' => false, 'fax' => false, 'email' => false, 'website' => false ), * 'map_icon' => false * ); */ function gmw_pt_update_location($args, $force_refresh = false) { $args = array_replace_recursive(array('post_id' => false, 'address' => false, 'additional_info' => array('phone' => false, 'fax' => false, 'email' => false, 'website' => false), 'map_icon' => '_default.png'), $args); if (empty($args['post_id']) || empty($args['address'])) { return; } if (empty($args['map_icon'])) { $args['map_icon'] = '_default.png'; } $address_apt = $address = $args['address']; $multiple_fields = false; if (is_array($args['address'])) { $mulitple_field = true; $address_apt = implode(' ', $address); unset($address['apt']); $address = implode(' ', $address); } //geocode the address $geocoded_address = GEO_my_WP::geocoder($address, $force_refresh); //if geocode failed delete the user's location if (isset($geocoded_address['error'])) { global $wpdb; $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}places_locator WHERE `post_id` = %d", array($args['post_id']))); do_action('gmw_pt_udpate_location_after_location_deleted', $args, $geocoded_address); return; } //if multiple address field passed through array if ($multiple_fields == true) { if (!in_array('street', $args['address']) || empty($args['address']['street'])) { $street = $geocoded_address['street']; } if (!in_array('apt', $args['address']) || empty($args['address']['apt'])) { $apt = $geocoded_address['apt']; } if (!in_array('city', $args['address']) || empty($args['address']['city'])) { $city = $geocoded_address['city']; } if (!in_array('zipcode', $args['address']) || empty($args['address']['zipcode'])) { $zipcode = $geocoded_address['zipcode']; } $state = $geocoded_address['state_short']; $country = $geocoded_address['country_short']; } else { $street = $geocoded_address['street']; $apt = $geocoded_address['apt']; $city = $geocoded_address['city']; $state = $geocoded_address['state_short']; $zipcode = $geocoded_address['zipcode']; $country = $geocoded_address['country_short']; } $locationArgs = array('args' => $args, 'address' => $address, 'geocoded' => $geocoded_address); $LocationArgs = apply_filters('gmw_pt_before_location_updated', $locationArgs); do_action('gmw_pt_before_location_updated', $locationArgs); //$featuredPost = ( isset( $_POST['_wppl_featured_post'] ) ) ? $_POST['_wppl_featured_post'] : 0; //Save information to database global $wpdb; $wpdb->replace($wpdb->prefix . 'places_locator', array('post_id' => $locationArgs['args']['post_id'], 'feature' => 0, 'post_type' => get_post_type($locationArgs['args']['post_id']), 'post_title' => get_the_title($locationArgs['args']['post_id']), 'post_status' => 'publish', 'street_number' => !empty($locationArgs['geocoded']['street_number']) ? $locationArgs['geocoded']['street_number'] : '', 'street_name' => !empty($locationArgs['geocoded']['street_name']) ? $locationArgs['geocoded']['street_name'] : '', 'street' => $street, 'apt' => $apt, 'city' => $city, 'state' => $state, 'state_long' => $locationArgs['geocoded']['state_long'], 'zipcode' => $zipcode, 'country' => $country, 'country_long' => $locationArgs['geocoded']['country_long'], 'address' => $address_apt, 'formatted_address' => $locationArgs['geocoded']['formatted_address'], 'phone' => $locationArgs['args']['additional_info']['phone'], 'fax' => $locationArgs['args']['additional_info']['fax'], 'email' => $locationArgs['args']['additional_info']['email'], 'website' => $locationArgs['args']['additional_info']['website'], 'lat' => $locationArgs['geocoded']['lat'], 'long' => $locationArgs['geocoded']['lng'], 'map_icon' => $args['map_icon'])); do_action('gmw_pt_after_location_updated', $locationArgs); }
/** * When displaying results on page load * * @since 2.5 * */ public function page_load_results() { $page_load_options = $this->form['page_load_results']; $this->form['org_address'] = ''; $this->form['get_per_page'] = !empty($_GET[$this->form['url_px'] . 'per_page']) ? $_GET[$this->form['url_px'] . 'per_page'] : current(explode(",", $page_load_options['per_page'])); $this->form['radius'] = !empty($page_load_options['radius']) ? $page_load_options['radius'] : 200; $this->form['search_results']['display_map'] = $page_load_options['display_map']; $this->form['units_array'] = gmw_get_units_array($this->form['page_load_results']['units']); //temporary solution - this part needs improvment. //need to change all kind of display_? options to the one global name such as results_list. //to easier control the outcome if (isset($page_load_options['display_posts'])) { $this->form['search_results']['display_posts'] = true; $this->form['search_results']['display_members'] = true; $this->form['search_results']['display_groups'] = true; $this->form['search_results']['display_users'] = true; } else { unset($this->form['search_results']['display_posts'], $this->form['search_results']['display_groups'], $this->form['search_results']['display_members'], $this->form['search_results']['display_users']); } //display results based on user's current location if (isset($page_load_options['user_location']) && !empty($this->form['user_position'])) { // get user's current location $this->form['org_address'] = $this->form['user_position']['address']; $this->form['your_lat'] = $this->form['user_position']['lat']; $this->form['your_lng'] = $this->form['user_position']['lng']; //if no user's position check for address filter } elseif (!empty($page_load_options['address_filter'])) { // get user's current location exsit $this->form['org_address'] = sanitize_text_field($page_load_options['address_filter']); $this->form['location'] = GEO_my_WP::geocoder($this->form['org_address']); //if geocode was unsuccessful return error message if (isset($this->form['location']['error'])) { //return $this->no_results( $this->form['location']['error'] ); return; } else { $this->form['your_lat'] = $this->form['location']['lat']; $this->form['your_lng'] = $this->form['location']['lng']; } } $this->form = apply_filters("gmw_page_load_results_before_results", $this->form); $this->form = apply_filters("gmw_page_load_results_before_results_{$this->form['ID']}", $this->form); $this->form = apply_filters("gmw_{$this->form['prefix']}_page_load_results_before_results", $this->form); $this->results(); }
/** * GMW function - add location to Users * * use this function if you want to add location to users using a custom form. * * function accepts an accociative array as below: * * $args = array ( * 'user_id' => false, //must pass user id in order for it to work * 'address' => false, //can be eiter single line of full address field or an array of the adress components ex ( array( 'street' => '5060 lincoln st', 'city' => 'hollywood', 'state' => 'florida' ) * 'map_icon' => false * ); */ function gmw_update_user_location($args) { //default args $args = array_replace_recursive(array('user_id' => false, 'address' => false, 'map_icon' => '_default.png'), $args); if (empty($args['user_id']) || empty($args['address'])) { return; } if (empty($args['map_icon'])) { $args['map_icon'] = '_default.png'; } $address_apt = $address = $args['address']; $multiple_fields = false; if (is_array($args['address'])) { $mulitple_field = true; $address_apt = implode(' ', $address); unset($address['apt']); $address = implode(' ', $address); } //geocode the address $geocoded_address = GEO_my_WP::geocoder($address); //if geocode failed delete the user's location if (isset($geocoded_address['error'])) { global $wpdb; $wpdb->query($wpdb->prepare("DELETE FROM wppl_friends_locator WHERE member_id = %d", $args['user_id'])); do_action('gmw_user_after_location_deleted', $args, $geocoded_address); return; } //if multiple address field passed through array if ($multiple_fields == true) { //if no street entered by the user try to get it from the geocoded details if (!in_array('street', $args['address'])) { $street = $geocoded_address['street']; } //if no apt entered by the user try to get it from the geocoded details if (!in_array('apt', $args['address'])) { $apt = $geocoded_address['apt']; } //if no city entered by the user try to get it from the geocoded details if (!in_array('city', $args['address'])) { $city = $geocoded_address['city']; } $state = $geocoded_address['state_short']; //if no zipcode entered by the user try to get it from the geocoded details if (!in_array('zipcode', $args['address'])) { $zipcode = $geocoded_address['zipcode']; } $country = $geocoded_address['country_short']; //if single address field entered } else { $street = $geocoded_address['street']; $apt = $geocoded_address['apt']; $city = $geocoded_address['city']; $state = $geocoded_address['state_short']; $zipcode = $geocoded_address['zipcode']; $country = $geocoded_address['country_short']; } //get the locaiton information into array $locationArgs = array('args' => $args, 'address' => $address, 'geocoded' => $geocoded_address); //allow plugins filter the information $LocationArgs = apply_filters('gmw_user_before_location_updated', $locationArgs); //actions before saving the location in database do_action('gmw_user_before_location_updated', $locationArgs); //Save information to database global $wpdb; $wpdb->replace('wppl_friends_locator', array('member_id' => $args['user_id'], 'street_number' => $locationArgs['geocoded']['street_number'], 'street_name' => $locationArgs['geocoded']['street_name'], 'street' => $street, 'apt' => $apt, 'city' => $city, 'state' => $state, 'state_long' => $locationArgs['geocoded']['state_long'], 'zipcode' => $zipcode, 'country' => $country, 'country_long' => $locationArgs['geocoded']['country_long'], 'address' => $address_apt, 'formatted_address' => $locationArgs['geocoded']['formatted_address'], 'lat' => $locationArgs['geocoded']['lat'], 'long' => $locationArgs['geocoded']['lng'], 'map_icon' => $args['map_icon'])); //actions after saving the location in database do_action('gmw_user_after_location_updated', $locationArgs); }
/** * Members Query */ function members_query() { global $wpdb, $bp; //set join type based on the query. if no address entered will join all members even if they have no location $tJoin = "RIGHT"; $tJoin2 = "LEFT"; //when doing query by address entered if ($this->formData['address']) { // $this->formData['orderby'] = 'distance'; //do INNER JOIN. we will show only members with location $tJoin = $tJoin2 = "INNER"; //geocode the address entered $this->returned_address = GEO_my_WP::geocoder($this->formData['address']); //If form submitted and address was not found stop search and display no results if (!isset($this->returned_address) || empty($this->returned_address)) { $this->formData['query'] = false; $this->gmwSD['your_lat'] = false; $this->gmwSD['your_lng'] = false; $this->gmwSD['org_address'] = 'bad'; //modify the query to no results $this->clauses['bp_user_query']['where'] = ' AND 0 = 1 '; $this->clauses['bp_user_query']['orderby'] = ' ORDER BY u.display_name'; $message = apply_filters('gmw_sd_no_addrss_found_message', __('Sorry, the address was not found. Please try a different address.', 'GMW')); ?> <script> jQuery('#message').html('<p>' + '<?php echo $message; ?> ' + '</p>'); </script> <?php } else { $this->formData['query'] = 'address'; $this->gmwSD['your_lat'] = $this->returned_address['lat']; $this->gmwSD['your_lng'] = $this->returned_address['lng']; $this->gmwSD['org_address'] = !empty($_POST) ? $_POST['search_terms'] : $this->formData['address']; } } elseif ($this->formData['orderby'] == 'distance') { $this->formData['orderby'] = 'active'; } $users_table = $bp->version < '2.0' ? $wpdb->usermeta : $bp->members->table_name_last_activity; if ($this->formData['orderby'] == 'alphabetical') { // if ( !bp_disable_profile_sync() || !bp_is_active( 'xprofile' ) ) { $this->clauses['bp_user_query']['select'] = "SELECT DISTINCT u.ID as id , gmwlocations.member_id"; $this->clauses['bp_user_query']['from'] = " FROM wppl_friends_locator gmwlocations {$tJoin} JOIN {$wpdb->users} u ON gmwlocations.member_id = u.ID"; $this->clauses['bp_user_query']['orderby'] = "ORDER BY u.display_name"; $this->clauses['bp_user_query']['order'] = "ASC"; // When profile sync is disabled, alphabetical sorts must happen against // the xprofile table /* } else { $fullname_field_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s", bp_xprofile_fullname_field_name() ) ); $this->clauses['bp_user_query']['select'] = "SELECT DISTINCT u.user_id as id , gmwlocations.member_id"; $this->clauses['bp_user_query']['from'] = " FROM wppl_friends_locator gmwlocations {$tJoin} JOIN {$bp->profile->table_name_data} u ON gmwlocations.member_id = id"; //$this->clauses['bp_user_query']['where'] = "WHERE u.field_id = {$fullname_field_id} "; $this->clauses['bp_user_query']['orderby'] = "ORDER BY u.value"; $this->clauses['bp_user_query']['order'] = "ASC"; } */ } elseif ($this->formData['orderby'] == 'newest' || $this->formData['orderby'] == 'active' || $this->formData['orderby'] == '') { $orderby = $bp->version < '2.0' ? 'u.meta_value' : 'u.date_recorded'; $this->clauses['bp_user_query']['select'] = "SELECT DISTINCT u.user_id , gmwlocations.member_id"; $this->clauses['bp_user_query']['from'] = " FROM wppl_friends_locator gmwlocations {$tJoin} JOIN {$users_table} u ON gmwlocations.member_id = u.user_id"; //$this->clauses['bp_user_query']['where'] = "u.component = 'members' AND u.type = 'last_activity''"; if ($this->formData['orderby'] == 'newest') { $this->clauses['bp_user_query']['orderby'] = "ORDER BY u.user_id"; } else { $this->clauses['bp_user_query']['orderby'] = "ORDER BY {$orderby}"; } $this->clauses['bp_user_query']['order'] = "DESC"; //when order by distance } elseif ($this->formData['orderby'] == 'distance') { $this->clauses['bp_user_query']['select'] = "SELECT gmwlocations.member_id, u.user_id as id"; $this->clauses['bp_user_query']['from'] = " FROM wppl_friends_locator gmwlocations INNER JOIN {$users_table} u ON gmwlocations.member_id = u.user_id"; //$this->clauses['bp_user_query']['where'] = "WHERE u.meta_key = 'last_activity'"; if ($this->gmwSD['your_lat'] != false) { $this->clauses['bp_user_query']['orderby'] = "ORDER BY distance"; } else { $this->formData['query'] = false; $this->gmwSD['your_lat'] = false; $this->gmwSD['your_lng'] = false; $this->clauses['bp_user_query']['where'] = 'AND 0 = 1'; $this->clauses['bp_user_query']['orderby'] = 'ORDER BY u.user_id'; ?> <script> jQuery(window).ready(function() { jQuery('#message').html("<p>We couldn't find the address you enter. Please try a different address.</p>"); }); </script> <?php } $this->clauses['bp_user_query']['order'] = "ASC"; } /* * if address entered * prepare the filter of the select clause of the SQL function. the function join the members table with * wppl_friends_locator table, will calculate the distance and will get only the members that * within the radius was chosen */ if ($this->gmwSD['your_lat'] !== false) { $this->clauses['bp_user_query']['select'] .= $wpdb->prepare(" , ROUND( %d * acos( cos( radians( %s ) ) * cos( radians( gmwlocations.lat ) ) * cos( radians( gmwlocations.long ) - radians( %s ) ) + sin( radians( %s ) ) * sin( radians( gmwlocations.lat) ) ),1 ) AS distance ", $this->gmwSD['units'], $this->gmwSD['your_lat'], $this->gmwSD['your_lng'], $this->gmwSD['your_lat']); $this->clauses['bp_user_query']['having'] = $wpdb->prepare(" HAVING distance <= %d OR distance IS NULL", $this->formData['radius']); $this->clauses['wp_user_query']['query_fields'] = $wpdb->prepare(" ,ROUND( %d * acos( cos( radians( %s ) ) * cos( radians( gmwlocations.lat ) ) * cos( radians( gmwlocations.long ) - radians( %s ) ) + sin( radians( %s ) ) * sin( radians( gmwlocations.lat) ) ),1 ) AS distance", $this->gmwSD['units'], $this->gmwSD['your_lat'], $this->gmwSD['your_lng'], $this->gmwSD['your_lat']); } //select all fields from location table $this->clauses['wp_user_query']['query_fields'] .= " , gmwlocations.lat, gmwlocations.long, gmwlocations.address, gmwlocations.formatted_address"; $this->clauses['wp_user_query']['query_from'] = " {$tJoin2} JOIN wppl_friends_locator gmwlocations ON ID = gmwlocations.member_id "; }
/** * Modify the BP query caluses * * @param unknown_type $gmwBpQuery * @return unknown */ public function bp_pre_user_query($gmwBpQuery) { global $wpdb; //break the select clause into 2: SELECT and FROM so we can modify it based on out needs $select_clause = explode('FROM', $gmwBpQuery->uid_clauses['select']); //find the user_id column based on the query type $uid_col = in_array($gmwBpQuery->query_vars['type'], array("alphabetical", 'distance')) ? 'u.ID' : 'u.user_id'; //default values $fields = ''; $from = ''; $having = ''; $where = ''; $orderby = ''; //if address entered if (!empty($this->formData['address'])) { //geocode the address entered $this->returned_address = GEO_my_WP::geocoder(sanitize_text_field($this->formData['address'])); //If form submitted and address was not found stop search and display no results if (isset($this->returned_address['error'])) { $this->formData['address_found'] = false; $this->formData['your_lat'] = false; $this->formData['your_lng'] = false; $this->formData['address'] = 'error'; //modify the query to no results $gmwBpQuery->uid_clauses['where'] = ' WHERE 0 = 1 '; $gmwBpQuery->uid_clauses['orderby'] = ' '; $gmwBpQuery->uid_clauses['limit'] = ' '; $gmwBpQuery->uid_clauses['order'] = ' '; ?> <script> //pass some values to javascript jQuery(window).ready(function($) { jQuery('#members-dir-list #message').html('<?php echo $this->labels['address_error_message']; ?> '); }); </script> <?php } else { $this->formData['address_found'] = true; $this->formData['your_lat'] = $this->returned_address['lat']; $this->formData['your_lng'] = $this->returned_address['lng']; //do radius calculation $fields = $wpdb->prepare(" , ROUND( %d * acos( cos( radians( %s ) ) * cos( radians( gmwlocations.lat ) ) * cos( radians( gmwlocations.long ) - radians( %s ) ) + sin( radians( %s ) ) * sin( radians( gmwlocations.lat) ) ),1 ) AS distance ", array($this->settings['units'], $this->formData['your_lat'], $this->formData['your_lng'], $this->formData['your_lat'])); //from clause joining locations table $from = "INNER JOIN wppl_friends_locator gmwlocations ON {$uid_col} = gmwlocations.member_id"; //HAVING clause to display members within the distance entered if (!empty($this->formData['radius'])) { $having = $wpdb->prepare('HAVING distance <= %d OR distance IS NULL ', $this->formData['radius']); } //if order by distance if ($gmwBpQuery->query_vars['type'] == 'distance') { $gmwBpQuery->uid_clauses['orderby'] = 'ORDER BY distance'; } } //if no address entered } else { //join the locations table to the query $from = "LEFT JOIN wppl_friends_locator gmwlocations ON {$uid_col} = gmwlocations.member_id"; if ($this->formData['orderby'] == 'distance') { $this->formData['orderby'] = 'active'; } } //apply our filters to BP_user_qeury clauses $gmwBpQuery->query_vars['count_total'] = 'sql_calc_found_rows'; $gmwBpQuery->uid_clauses['select'] = "{$select_clause[0]} {$fields} FROM {$select_clause[1]} {$from} "; $gmwBpQuery->uid_clauses['where'] .= $where; $gmwBpQuery->uid_clauses['where'] .= $having; //modify the clauses $gmwBpQuery = apply_filters('gmw_sd_after_bp_pre_user_query', $gmwBpQuery, $this->formData, $this->settings); return $gmwBpQuery; }