daily pastebin goal
59%
SHARE
TWEET

Location Filtering BP

a guest Aug 27th, 2014 262 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. add_action( 'bp_members_directory_order_options', 'mycred_pro_add_sorting_options' );
  3. function mycred_pro_add_sorting_options() {
  4.         if(is_user_logged_in()):
  5.         ?>
  6.                 <option value="points-asc">Distance: Closest to me</option>
  7.                 <option value="points-desc">Distance: Farthest from me</option>
  8.         <?php
  9.         endif;
  10. }
  11.  
  12. add_action( 'bp_pre_user_query', 'mycred_pro_pre_user_query' );
  13. function mycred_pro_pre_user_query( $BP_User_Query ) {
  14.         // Only run this if one of our custom options is selected
  15.         if ( in_array( $BP_User_Query->query_vars['type'], array( 'points-asc', 'points-desc' ) ) ) {
  16.                 global $wpdb, $scope;
  17.  
  18.                 // Adjust SELECT
  19.                 $BP_User_Query->uid_clauses['select'] = "
  20.                         select distinct um.user_id, um.meta_value as latitude, um2.meta_value as longitude
  21.                         from wp_usermeta as um
  22.                         left join wp_usermeta as um2 on um.user_id=um2.user_id and um2.meta_key = 'geo_long'
  23.                         ";
  24.  
  25.                 // Adjust WHERE
  26.                 $BP_User_Query->uid_clauses['where'] = "WHERE um.meta_key='geo_lat'";
  27.                
  28.                 //Additional Custom WHERE
  29.                 if(bp_current_action() == 'my-friends'){
  30.                         $friends_list = friends_get_friend_user_ids( bp_loggedin_user_id(), false, true );
  31.                        
  32.                         foreach($friends_list as $friend):
  33.                                 $ids_to_include[] = $friend['user_id'];
  34.                         endforeach;
  35.                        
  36.                 }else{
  37.                         $ids_to_include = explode("=", $scope);
  38.                         $ids_to_include = explode(",",$ids_to_include[1]);
  39.                 }
  40.                
  41.                 $ids_string = "";
  42.  
  43.                 $ids_to_include = array_diff($ids_to_include, array(bp_loggedin_user_id()));
  44.                 $id_count = 0;
  45.                
  46.                 foreach($ids_to_include as $id){
  47.                         if($id == ""){
  48.                                 continue;
  49.                         }else{
  50.                                 if($id_count == 0)
  51.                                         $ids_string .= " AND (um.user_id = ".$id;
  52.                                 else
  53.                                         $ids_string .= " OR um.user_id = ".$id;        
  54.                                                                
  55.                                 $id_count++;
  56.                         }
  57.                 }
  58.                 if($ids_string != "")
  59.                         $ids_string .= ")";    
  60.                
  61.                 $BP_User_Query->uid_clauses['where'] .= $ids_string;
  62.                
  63.                 // Adjust ORDER BY
  64.                 $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)";
  65.  
  66.                 // Adjust ORDER
  67.                 $BP_User_Query->uid_clauses['order'] = ( $BP_User_Query->query_vars['type'] == 'points-asc' ) ? 'ASC' : 'DESC';
  68.        
  69.         }
  70. }
  71.  
  72.  
  73. function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2, $unit = 'Mi') {
  74.      $theta = $longitude1 - $longitude2;
  75.      $distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
  76.      $distance = acos($distance);
  77.      $distance = rad2deg($distance);
  78.      $distance = $distance * 60 * 1.1515; switch($unit) {
  79.           case 'Mi': break; case 'Km' : $distance = $distance * 1.609344;
  80.      }
  81.      return (round($distance,2));
  82. }
  83.  
  84. function set_lat_long( $field_id, $value ){
  85.         global $bp;
  86.        
  87.         if($field_id == 36){
  88.         $url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$value."&sensor=false";
  89.                
  90.         $response = wp_remote_get($url);
  91.         $geodata = json_decode($response['body'], true);
  92.        
  93.         $lat = $geodata["results"][0]["geometry"]["location"]["lat"];
  94.         $long = $geodata["results"][0]["geometry"]["location"]["lng"];
  95.         update_user_meta($bp->displayed_user->id, "geo_lat", $lat);
  96.         update_user_meta($bp->displayed_user->id, "geo_long", $long);
  97.         }
  98. }
  99.  
  100. add_action( 'xprofile_profile_field_data_updated', 'set_lat_long', 1, 2 );
  101.  
  102. //Credit: Godavid33 :)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top