Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'bp_members_directory_order_options', 'mycred_pro_add_sorting_options' );
- function mycred_pro_add_sorting_options() {
- if(is_user_logged_in()):
- ?>
- <option value="points-asc">Distance: Closest to me</option>
- <option value="points-desc">Distance: Farthest from me</option>
- <?php
- endif;
- }
- add_action( 'bp_pre_user_query', 'mycred_pro_pre_user_query' );
- function mycred_pro_pre_user_query( $BP_User_Query ) {
- // Only run this if one of our custom options is selected
- if ( in_array( $BP_User_Query->query_vars['type'], array( 'points-asc', 'points-desc' ) ) ) {
- global $wpdb, $scope;
- // Adjust SELECT
- $BP_User_Query->uid_clauses['select'] = "
- select distinct um.user_id, um.meta_value as latitude, um2.meta_value as longitude
- from wp_usermeta as um
- left join wp_usermeta as um2 on um.user_id=um2.user_id and um2.meta_key = 'geo_long'
- ";
- // Adjust WHERE
- $BP_User_Query->uid_clauses['where'] = "WHERE um.meta_key='geo_lat'";
- //Additional Custom WHERE
- if(bp_current_action() == 'my-friends'){
- $friends_list = friends_get_friend_user_ids( bp_loggedin_user_id(), false, true );
- foreach($friends_list as $friend):
- $ids_to_include[] = $friend['user_id'];
- endforeach;
- }else{
- $ids_to_include = explode("=", $scope);
- $ids_to_include = explode(",",$ids_to_include[1]);
- }
- $ids_string = "";
- $ids_to_include = array_diff($ids_to_include, array(bp_loggedin_user_id()));
- $id_count = 0;
- foreach($ids_to_include as $id){
- if($id == ""){
- continue;
- }else{
- if($id_count == 0)
- $ids_string .= " AND (um.user_id = ".$id;
- else
- $ids_string .= " OR um.user_id = ".$id;
- $id_count++;
- }
- }
- if($ids_string != "")
- $ids_string .= ")";
- $BP_User_Query->uid_clauses['where'] .= $ids_string;
- // Adjust ORDER BY
- $BP_User_Query->uid_clauses['orderby'] = "ORDER BY (((acos(sin((".get_user_meta(get_current_user_id(), "geo_lat",true)."*pi()/180)) * sin((latitude*pi()/180))+cos((".get_user_meta(get_current_user_id(), "geo_lat",true)."*pi()/180)) * cos((latitude*pi()/180)) * cos(((".get_user_meta(get_current_user_id(), "geo_long",true)."- longitude)*pi()/180))))*180/pi())*60*1.1515)";
- // Adjust ORDER
- $BP_User_Query->uid_clauses['order'] = ( $BP_User_Query->query_vars['type'] == 'points-asc' ) ? 'ASC' : 'DESC';
- }
- }
- function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2, $unit = 'Mi') {
- $theta = $longitude1 - $longitude2;
- $distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
- $distance = acos($distance);
- $distance = rad2deg($distance);
- $distance = $distance * 60 * 1.1515; switch($unit) {
- case 'Mi': break; case 'Km' : $distance = $distance * 1.609344;
- }
- return (round($distance,2));
- }
- function set_lat_long( $field_id, $value ){
- global $bp;
- if($field_id == 36){
- $url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$value."&sensor=false";
- $response = wp_remote_get($url);
- $geodata = json_decode($response['body'], true);
- $lat = $geodata["results"][0]["geometry"]["location"]["lat"];
- $long = $geodata["results"][0]["geometry"]["location"]["lng"];
- update_user_meta($bp->displayed_user->id, "geo_lat", $lat);
- update_user_meta($bp->displayed_user->id, "geo_long", $long);
- }
- }
- add_action( 'xprofile_profile_field_data_updated', 'set_lat_long', 1, 2 );
- //Credit: Godavid33 :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement