Changeset 2077 for trunk/bp-friends/bp-friends-classes.php
- Timestamp:
- 11/02/2009 07:54:21 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-friends/bp-friends-classes.php
r2055 r2077 8 8 var $is_limited; 9 9 var $date_created; 10 10 11 11 var $is_request; 12 12 var $populate_friend_details; 13 13 14 14 var $friend; 15 15 16 16 function bp_friends_friendship( $id = null, $is_request = false, $populate_friend_details = true ) { 17 17 $this->is_request = $is_request; 18 18 19 19 if ( $id ) { 20 20 $this->id = $id; … … 26 26 function populate() { 27 27 global $wpdb, $bp, $creds; 28 28 29 29 if ( $friendship = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->friends->table_name} WHERE id = %d", $this->id ) ) ) { 30 30 $this->initiator_user_id = $friendship->initiator_user_id; … … 34 34 $this->date_created = $friendship->date_created; 35 35 } 36 36 37 37 // if running from ajax. 38 38 if ( !$bp->displayed_user->id ) 39 39 $bp->displayed_user->id = $creds['current_userid']; 40 40 41 41 if ( $this->populate_friend_details ) { 42 42 if ( $this->friend_user_id == $bp->displayed_user->id ) { … … 47 47 } 48 48 } 49 49 50 50 function save() { 51 51 global $wpdb, $bp; … … 57 57 $this->date_created = apply_filters( 'friends_friendship_date_created_before_save', $this->date_created, $this->id ); 58 58 59 do_action( 'friends_friendship_before_save', $this ); 60 59 do_action( 'friends_friendship_before_save', $this ); 60 61 61 if ( $this->id ) { 62 62 // Update … … 68 68 } 69 69 70 do_action( 'friends_friendship_after_save', $this ); 71 70 do_action( 'friends_friendship_after_save', $this ); 71 72 72 return $result; 73 73 } 74 74 75 75 function delete() { 76 76 global $wpdb, $bp; 77 77 78 78 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->friends->table_name} WHERE id = %d", $this->id ) ); 79 79 } 80 80 81 81 /* Static Functions */ 82 82 83 83 function get_friend_user_ids( $user_id, $friend_requests_only = false, $assoc_arr = false ) { 84 84 global $wpdb, $bp; 85 85 86 86 if ( $friend_requests_only ) { 87 87 $oc_sql = $wpdb->prepare( "AND is_confirmed = 0" ); … … 93 93 94 94 $friends = $wpdb->get_results( $wpdb->prepare( "SELECT friend_user_id, initiator_user_id FROM {$bp->friends->table_name} $friend_sql $oc_sql ORDER BY date_created DESC" ) ); 95 95 96 96 for ( $i = 0; $i < count($friends); $i++ ) { 97 97 if ( $assoc_arr ) … … 100 100 $fids[] = ( $friends[$i]->friend_user_id == $user_id ) ? $friends[$i]->initiator_user_id : $friends[$i]->friend_user_id; 101 101 } 102 102 103 103 return $fids; 104 104 } 105 105 106 106 function get_friendship_ids( $user_id, $friend_requests_only = false ) { 107 107 global $wpdb, $bp; … … 114 114 $friend_sql = $wpdb->prepare ( " WHERE (initiator_user_id = %d OR friend_user_id = %d)", $user_id, $user_id ); 115 115 } 116 116 117 117 return $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$bp->friends->table_name} $friend_sql $oc_sql" ) ); 118 118 } 119 119 120 120 function get_friendship_id( $user_id, $friend_id ) { 121 121 global $wpdb, $bp; 122 122 123 123 return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->friends->table_name} WHERE ( initiator_user_id = %d AND friend_user_id = %d ) OR ( initiator_user_id = %d AND friend_user_id = %d ) AND is_confirmed = 1", $user_id, $friend_id, $friend_id, $user_id ) ); 124 124 } 125 125 126 126 function total_friend_count( $user_id = false) { 127 127 global $wpdb, $bp; 128 128 129 129 if ( !$user_id ) 130 130 $user_id = $bp->displayed_user->id; 131 132 /* This is stored in 'total_friend_count' usermeta. 131 132 /* This is stored in 'total_friend_count' usermeta. 133 133 This function will recalculate, update and return. */ 134 134 135 135 $count = $wpdb->get_var( $wpdb->prepare( "SELECT count(id) FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d OR friend_user_id = %d) AND is_confirmed = 1", $user_id, $user_id ) ); 136 136 137 137 if ( !$count ) 138 138 return 0; 139 139 140 140 update_usermeta( $user_id, 'total_friend_count', $count ); 141 141 return $count; 142 142 } 143 143 144 144 function search_friends( $filter, $user_id, $limit = null, $page = null ) { 145 145 global $wpdb, $bp; 146 146 147 147 // TODO: Optimize this function. 148 148 149 149 if ( !$user_id ) 150 150 $user_id = $bp->loggedin_user->id; 151 151 152 152 $filter = like_escape( $wpdb->escape( $filter ) ); 153 153 154 154 if ( $limit && $page ) 155 155 $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 156 156 157 157 if ( !$friend_ids = BP_Friends_Friendship::get_friend_user_ids( $user_id ) ) 158 158 return false; … … 160 160 // Get all the user ids for the current user's friends. 161 161 $fids = implode( ',', $friend_ids ); 162 162 163 163 if ( empty($fids) ) 164 164 return false; … … 173 173 } 174 174 175 $filtered_friend_ids = $wpdb->get_col($sql); 176 $total_friend_ids = $wpdb->get_var($total_sql); 177 175 $filtered_friend_ids = $wpdb->get_col($sql); 176 $total_friend_ids = $wpdb->get_var($total_sql); 177 178 178 if ( !$filtered_friend_ids ) 179 179 return false; … … 181 181 return array( 'friends' => $filtered_friend_ids, 'total' => (int)$total_friend_ids ); 182 182 } 183 183 184 184 function check_is_friend( $loggedin_userid, $possible_friend_userid ) { 185 185 global $wpdb, $bp; 186 186 187 187 if ( !$loggedin_userid || !$possible_friend_userid ) 188 188 return false; 189 189 190 190 $result = $wpdb->get_results( $wpdb->prepare( "SELECT id, is_confirmed FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d AND friend_user_id = %d) OR (initiator_user_id = %d AND friend_user_id = %d)", $loggedin_userid, $possible_friend_userid, $possible_friend_userid, $loggedin_userid ) ); 191 191 192 192 if ( $result ) { 193 193 if ( 0 == (int)$result[0]->is_confirmed ) { … … 200 200 } 201 201 } 202 202 203 203 function get_bulk_last_active( $user_ids ) { 204 204 global $wpdb, $bp; … … 206 206 return $wpdb->get_results( $wpdb->prepare( "SELECT meta_value as last_activity, user_id FROM " . CUSTOM_USER_META_TABLE . " WHERE meta_key = 'last_activity' AND user_id IN ( {$user_ids} ) ORDER BY meta_value DESC" ) ); 207 207 } 208 208 209 209 function accept($friendship_id) { 210 210 global $wpdb, $bp; … … 212 212 return $wpdb->query( $wpdb->prepare( "UPDATE {$bp->friends->table_name} SET is_confirmed = 1, date_created = FROM_UNIXTIME(%d) WHERE id = %d AND friend_user_id = %d", time(), $friendship_id, $bp->loggedin_user->id ) ); 213 213 } 214 214 215 215 function reject($friendship_id) { 216 216 global $wpdb, $bp; 217 217 218 218 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->friends->table_name} WHERE id = %d AND friend_user_id = %d", $friendship_id, $bp->loggedin_user->id ) ); 219 219 } 220 220 221 221 function search_users( $filter, $user_id, $limit = null, $page = null ) { 222 222 global $wpdb, $bp; 223 223 224 224 $filter = like_escape( $wpdb->escape( $filter ) ); 225 225 226 $usermeta_table = $wpdb->base_prefix . 'usermeta'; 226 227 $users_table = $wpdb->base_prefix . 'users'; … … 228 229 if ( $limit && $page ) 229 230 $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 230 231 231 232 // filter the user_ids based on the search criteria. 232 233 if ( function_exists('xprofile_install') ) { … … 235 236 $sql = $wpdb->prepare( "SELECT DISTINCT user_id as id FROM $usermeta_table WHERE meta_value LIKE '$filter%%' ORDER BY d.value DESC $pag_sql" ); 236 237 } 237 238 $filtered_fids = $wpdb->get_col($sql); 239 238 239 $filtered_fids = $wpdb->get_col($sql); 240 240 241 if ( !$filtered_fids ) 241 242 return false; … … 243 244 return $filtered_fids; 244 245 } 245 246 246 247 function search_users_count( $filter ) { 247 248 global $wpdb, $bp; 248 249 249 250 $filter = like_escape( $wpdb->escape( $filter ) ); 251 250 252 $usermeta_table = $wpdb->prefix . 'usermeta'; 251 253 $users_table = $wpdb->base_prefix . 'users'; 252 254 253 255 // filter the user_ids based on the search criteria. 254 256 if ( function_exists('xprofile_install') ) { … … 258 260 } 259 261 260 $user_count = $wpdb->get_col($sql); 261 262 $user_count = $wpdb->get_col($sql); 263 262 264 if ( !$user_count ) 263 265 return false; … … 265 267 return $user_count[0]; 266 268 } 267 269 268 270 function sort_by_name( $user_ids ) { 269 271 global $wpdb, $bp; 270 272 271 273 if ( !function_exists( 'xprofile_install') ) 272 274 return false; 273 275 274 276 return $wpdb->get_results( $wpdb->prepare( "SELECT user_id FROM {$bp->profile->table_name_data} pd, {$bp->profile->table_name_fields} pf WHERE pf.id = pd.field_id AND pf.name = %s AND pd.user_id IN ( {$user_ids} ) ORDER BY pd.value ASC", BP_XPROFILE_FULLNAME_FIELD_NAME ) ); 275 277 } 276 278 277 279 function get_random_friends( $user_id, $total_friends = 5 ) { 278 280 global $wpdb, $bp; … … 284 286 $fids[] = ( $results[$i]->friend_user_id == $user_id ) ? $results[$i]->initiator_user_id : $results[$i]->friend_user_id; 285 287 } 286 288 287 289 // remove duplicates 288 290 if ( count($fids) > 0 ) … … 291 293 return false; 292 294 } 293 295 294 296 function get_invitable_friend_count( $user_id, $group_id ) { 295 297 global $wpdb, $bp; 296 298 297 299 $friend_ids = BP_Friends_Friendship::get_friend_user_ids( $user_id ); 298 300 299 301 $invitable_count = 0; 300 302 for ( $i = 0; $i < count($friend_ids); $i++ ) { 301 303 302 304 if ( BP_Groups_Member::check_is_member( (int)$friend_ids[$i], $group_id ) ) 303 305 continue; 304 306 305 307 if ( BP_Groups_Member::check_has_invite( (int)$friend_ids[$i], $group_id ) ) 306 308 continue; 307 309 308 310 $invitable_count++; 309 311 } … … 311 313 return $invitable_count; 312 314 } 313 315 314 316 function get_user_ids_for_friendship( $friendship_id ) { 315 317 global $wpdb, $bp; … … 317 319 return $wpdb->get_row( $wpdb->prepare( "SELECT friend_user_id, initiator_user_id FROM {$bp->friends->table_name} WHERE id = %d", $friendship_id ) ); 318 320 } 319 321 320 322 function delete_all_for_user( $user_id ) { 321 323 global $wpdb, $bp; 322 324 323 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->friends->table_name} WHERE friend_user_id = %d OR initiator_user_id = %d", $user_id, $user_id ) ); 324 325 // Delete friend request notifications for members who have a notification from this user. 325 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->friends->table_name} WHERE friend_user_id = %d OR initiator_user_id = %d", $user_id, $user_id ) ); 326 327 // Delete friend request notifications for members who have a notification from this user. 326 328 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->core->table_name_notifications} WHERE component_name = 'friends' AND ( component_action = 'friendship_request' OR component_action = 'friendship_accepted' ) AND item_id = %d", $user_id ) ); 327 329 } 328 330 } 329 331 330 332 331 333
Note: See TracChangeset
for help on using the changeset viewer.