2012-07-11 16:28:51 +00:00
< ? php
// Exit if accessed directly
if ( ! defined ( 'ABSPATH' ) ) exit ;
class BP_Friends_Friendship {
var $id ;
var $initiator_user_id ;
var $friend_user_id ;
var $is_confirmed ;
var $is_limited ;
var $date_created ;
var $is_request ;
var $populate_friend_details ;
var $friend ;
2012-08-08 16:49:22 +00:00
function bp_friends_friendship ( $id = null , $is_request = false , $populate_friend_details = true ) {
$this -> __construct ( $id , $is_request , $populate_friend_details );
}
2012-07-11 16:28:51 +00:00
function __construct ( $id = null , $is_request = false , $populate_friend_details = true ) {
$this -> is_request = $is_request ;
2012-08-08 16:49:22 +00:00
if ( $id ) {
$this -> id = $id ;
2012-07-11 16:28:51 +00:00
$this -> populate_friend_details = $populate_friend_details ;
$this -> populate ( $this -> id );
}
}
function populate () {
2012-08-08 16:49:22 +00:00
global $wpdb , $bp , $creds ;
2012-07-11 16:28:51 +00:00
if ( $friendship = $wpdb -> get_row ( $wpdb -> prepare ( " SELECT * FROM { $bp -> friends -> table_name } WHERE id = %d " , $this -> id ) ) ) {
$this -> initiator_user_id = $friendship -> initiator_user_id ;
$this -> friend_user_id = $friendship -> friend_user_id ;
$this -> is_confirmed = $friendship -> is_confirmed ;
$this -> is_limited = $friendship -> is_limited ;
$this -> date_created = $friendship -> date_created ;
}
2012-08-08 16:49:22 +00:00
// if running from ajax.
if ( ! $bp -> displayed_user -> id )
$bp -> displayed_user -> id = $creds [ 'current_userid' ];
if ( $this -> populate_friend_details ) {
if ( $this -> friend_user_id == $bp -> displayed_user -> id ) {
2012-07-11 16:28:51 +00:00
$this -> friend = new BP_Core_User ( $this -> initiator_user_id );
} else {
$this -> friend = new BP_Core_User ( $this -> friend_user_id );
}
}
}
function save () {
global $wpdb , $bp ;
$this -> initiator_user_id = apply_filters ( 'friends_friendship_initiator_user_id_before_save' , $this -> initiator_user_id , $this -> id );
$this -> friend_user_id = apply_filters ( 'friends_friendship_friend_user_id_before_save' , $this -> friend_user_id , $this -> id );
$this -> is_confirmed = apply_filters ( 'friends_friendship_is_confirmed_before_save' , $this -> is_confirmed , $this -> id );
$this -> is_limited = apply_filters ( 'friends_friendship_is_limited_before_save' , $this -> is_limited , $this -> id );
$this -> date_created = apply_filters ( 'friends_friendship_date_created_before_save' , $this -> date_created , $this -> id );
do_action_ref_array ( 'friends_friendship_before_save' , array ( & $this ) );
2012-08-08 16:49:22 +00:00
if ( $this -> id ) {
// Update
2012-07-11 16:28:51 +00:00
$result = $wpdb -> query ( $wpdb -> prepare ( " UPDATE { $bp -> friends -> table_name } SET initiator_user_id = %d, friend_user_id = %d, is_confirmed = %d, is_limited = %d, date_created = %s ) WHERE id = %d " , $this -> initiator_user_id , $this -> friend_user_id , $this -> is_confirmed , $this -> is_limited , $this -> date_created , $this -> id ) );
} else {
2012-08-08 16:49:22 +00:00
// Save
2012-07-11 16:28:51 +00:00
$result = $wpdb -> query ( $wpdb -> prepare ( " INSERT INTO { $bp -> friends -> table_name } ( initiator_user_id, friend_user_id, is_confirmed, is_limited, date_created ) VALUES ( %d, %d, %d, %d, %s ) " , $this -> initiator_user_id , $this -> friend_user_id , $this -> is_confirmed , $this -> is_limited , $this -> date_created ) );
$this -> id = $wpdb -> insert_id ;
}
do_action ( 'friends_friendship_after_save' , array ( & $this ) );
return $result ;
}
function delete () {
global $wpdb , $bp ;
return $wpdb -> query ( $wpdb -> prepare ( " DELETE FROM { $bp -> friends -> table_name } WHERE id = %d " , $this -> id ) );
}
2012-08-08 16:49:22 +00:00
/* Static Functions */
2012-07-11 16:28:51 +00:00
function get_friend_user_ids ( $user_id , $friend_requests_only = false , $assoc_arr = false ) {
global $wpdb , $bp ;
2012-08-08 16:49:22 +00:00
if ( $friend_requests_only ) {
2012-07-11 16:28:51 +00:00
$oc_sql = $wpdb -> prepare ( " AND is_confirmed = 0 " );
$friend_sql = $wpdb -> prepare ( " WHERE friend_user_id = %d " , $user_id );
} else {
$oc_sql = $wpdb -> prepare ( " AND is_confirmed = 1 " );
$friend_sql = $wpdb -> prepare ( " WHERE (initiator_user_id = %d OR friend_user_id = %d) " , $user_id , $user_id );
}
$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 " ) );
$fids = array ();
for ( $i = 0 , $count = count ( $friends ); $i < $count ; ++ $i ) {
2012-08-08 16:49:22 +00:00
if ( $assoc_arr )
2012-07-11 16:28:51 +00:00
$fids [] = array ( 'user_id' => ( $friends [ $i ] -> friend_user_id == $user_id ) ? $friends [ $i ] -> initiator_user_id : $friends [ $i ] -> friend_user_id );
2012-08-08 16:49:22 +00:00
else
2012-07-11 16:28:51 +00:00
$fids [] = ( $friends [ $i ] -> friend_user_id == $user_id ) ? $friends [ $i ] -> initiator_user_id : $friends [ $i ] -> friend_user_id ;
}
return $fids ;
}
function get_friendship_id ( $user_id , $friend_id ) {
global $wpdb , $bp ;
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 ) );
}
function get_friendship_request_user_ids ( $user_id ) {
global $wpdb , $bp ;
return $wpdb -> get_col ( $wpdb -> prepare ( " SELECT initiator_user_id FROM { $bp -> friends -> table_name } WHERE friend_user_id = %d AND is_confirmed = 0 " , $user_id ) );
}
function total_friend_count ( $user_id = 0 ) {
global $wpdb , $bp ;
2012-08-08 16:49:22 +00:00
if ( ! $user_id )
$user_id = ( $bp -> displayed_user -> id ) ? $bp -> displayed_user -> id : $bp -> loggedin_user -> id ;
2012-07-11 16:28:51 +00:00
/* This is stored in 'total_friend_count' usermeta .
This function will recalculate , update and return . */
$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 ) );
// Do not update meta if user has never had friends
2012-08-08 16:49:22 +00:00
if ( ! $count && ! bp_get_user_meta ( $user_id , 'total_friend_count' , true ) )
2012-07-11 16:28:51 +00:00
return 0 ;
2012-08-08 16:49:22 +00:00
bp_update_user_meta ( $user_id , 'total_friend_count' , ( int ) $count );
return ( int ) $count ;
2012-07-11 16:28:51 +00:00
}
function search_friends ( $filter , $user_id , $limit = null , $page = null ) {
global $wpdb , $bp ;
// TODO: Optimize this function.
2012-08-08 16:49:22 +00:00
if ( ! $user_id )
$user_id = $bp -> loggedin_user -> id ;
2012-07-11 16:28:51 +00:00
$filter = like_escape ( $wpdb -> escape ( $filter ) );
2012-08-08 16:49:22 +00:00
if ( $limit && $page )
2012-07-11 16:28:51 +00:00
$pag_sql = $wpdb -> prepare ( " LIMIT %d, %d " , intval ( ( $page - 1 ) * $limit ), intval ( $limit ) );
if ( ! $friend_ids = BP_Friends_Friendship :: get_friend_user_ids ( $user_id ) )
return false ;
// Get all the user ids for the current user's friends.
$fids = implode ( ',' , $friend_ids );
2012-08-08 16:49:22 +00:00
if ( empty ( $fids ) )
2012-07-11 16:28:51 +00:00
return false ;
// filter the user_ids based on the search criteria.
if ( bp_is_active ( 'xprofile' ) ) {
$sql = " SELECT DISTINCT user_id FROM { $bp -> profile -> table_name_data } WHERE user_id IN ( $fids ) AND value LIKE ' $filter %%' { $pag_sql } " ;
$total_sql = " SELECT COUNT(DISTINCT user_id) FROM { $bp -> profile -> table_name_data } WHERE user_id IN ( $fids ) AND value LIKE ' $filter %%' " ;
} else {
$sql = " SELECT DISTINCT user_id FROM { $wpdb -> usermeta } WHERE user_id IN ( $fids ) AND meta_key = 'nickname' AND meta_value LIKE ' $filter %%' { $pag_sql } " ;
$total_sql = " SELECT COUNT(DISTINCT user_id) FROM { $wpdb -> usermeta } WHERE user_id IN ( $fids ) AND meta_key = 'nickname' AND meta_value LIKE ' $filter %%' " ;
}
2012-08-08 16:49:22 +00:00
$filtered_friend_ids = $wpdb -> get_col ( $sql );
$total_friend_ids = $wpdb -> get_var ( $total_sql );
2012-07-11 16:28:51 +00:00
2012-08-08 16:49:22 +00:00
if ( ! $filtered_friend_ids )
2012-07-11 16:28:51 +00:00
return false ;
2012-08-08 16:49:22 +00:00
return array ( 'friends' => $filtered_friend_ids , 'total' => ( int ) $total_friend_ids );
2012-07-11 16:28:51 +00:00
}
function check_is_friend ( $loggedin_userid , $possible_friend_userid ) {
global $wpdb , $bp ;
2012-08-08 16:49:22 +00:00
if ( ! $loggedin_userid || ! $possible_friend_userid )
2012-07-11 16:28:51 +00:00
return false ;
$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 ) );
2012-08-08 16:49:22 +00:00
if ( $result ) {
if ( 0 == ( int ) $result [ 0 ] -> is_confirmed ) {
2012-07-11 16:28:51 +00:00
return 'pending' ;
} else {
return 'is_friend' ;
}
} else {
return 'not_friends' ;
}
}
function get_bulk_last_active ( $user_ids ) {
2012-08-08 16:49:22 +00:00
global $wpdb , $bp ;
2012-07-11 16:28:51 +00:00
return $wpdb -> get_results ( $wpdb -> prepare ( " SELECT meta_value as last_activity, user_id FROM { $wpdb -> usermeta } WHERE meta_key = %s AND user_id IN ( { $user_ids } ) ORDER BY meta_value DESC " , bp_get_user_meta_key ( 'last_activity' ) ) );
}
function accept ( $friendship_id ) {
global $wpdb , $bp ;
2012-08-08 16:49:22 +00:00
return $wpdb -> query ( $wpdb -> prepare ( " UPDATE { $bp -> friends -> table_name } SET is_confirmed = 1, date_created = %s WHERE id = %d AND friend_user_id = %d " , bp_core_current_time (), $friendship_id , $bp -> loggedin_user -> id ) );
2012-07-11 16:28:51 +00:00
}
function reject ( $friendship_id ) {
global $wpdb , $bp ;
2012-08-08 16:49:22 +00:00
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 ) );
2012-07-11 16:28:51 +00:00
}
function search_users ( $filter , $user_id , $limit = null , $page = null ) {
2012-08-08 16:49:22 +00:00
global $wpdb , $bp ;
2012-07-11 16:28:51 +00:00
$filter = like_escape ( $wpdb -> escape ( $filter ) );
$usermeta_table = $wpdb -> base_prefix . 'usermeta' ;
2012-08-08 16:49:22 +00:00
$users_table = $wpdb -> base_prefix . 'users' ;
2012-07-11 16:28:51 +00:00
2012-08-08 16:49:22 +00:00
if ( $limit && $page )
2012-07-11 16:28:51 +00:00
$pag_sql = $wpdb -> prepare ( " LIMIT %d, %d " , intval ( ( $page - 1 ) * intval ( $limit ) ), intval ( $limit ) );
// filter the user_ids based on the search criteria.
if ( bp_is_active ( 'xprofile' ) ) {
$sql = $wpdb -> prepare ( " SELECT DISTINCT d.user_id as id FROM { $bp -> profile -> table_name_data } d, $users_table u WHERE d.user_id = u.id AND d.value LIKE ' $filter %%' ORDER BY d.value DESC $pag_sql " );
} else {
$sql = $wpdb -> prepare ( " SELECT DISTINCT user_id as id FROM $usermeta_table WHERE meta_value LIKE ' $filter %%' ORDER BY d.value DESC $pag_sql " );
}
$filtered_fids = $wpdb -> get_col ( $sql );
2012-08-08 16:49:22 +00:00
if ( ! $filtered_fids )
2012-07-11 16:28:51 +00:00
return false ;
return $filtered_fids ;
}
function search_users_count ( $filter ) {
global $wpdb , $bp ;
$filter = like_escape ( $wpdb -> escape ( $filter ) );
$usermeta_table = $wpdb -> prefix . 'usermeta' ;
2012-08-08 16:49:22 +00:00
$users_table = $wpdb -> base_prefix . 'users' ;
2012-07-11 16:28:51 +00:00
// filter the user_ids based on the search criteria.
if ( bp_is_active ( 'xprofile' ) ) {
$sql = $wpdb -> prepare ( " SELECT COUNT(DISTINCT d.user_id) FROM { $bp -> profile -> table_name_data } d, $users_table u WHERE d.user_id = u.id AND d.value LIKE ' $filter %%' " );
} else {
$sql = $wpdb -> prepare ( " SELECT COUNT(DISTINCT user_id) FROM $usermeta_table WHERE meta_value LIKE ' $filter %%' " );
}
$user_count = $wpdb -> get_col ( $sql );
2012-08-08 16:49:22 +00:00
if ( ! $user_count )
2012-07-11 16:28:51 +00:00
return false ;
return $user_count [ 0 ];
}
function sort_by_name ( $user_ids ) {
global $wpdb , $bp ;
if ( ! bp_is_active ( 'xprofile' ) )
return false ;
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 () ) );
}
function get_random_friends ( $user_id , $total_friends = 5 ) {
global $wpdb , $bp ;
2012-08-08 16:49:22 +00:00
$sql = $wpdb -> prepare ( " SELECT friend_user_id, initiator_user_id FROM { $bp -> friends -> table_name } WHERE (friend_user_id = %d || initiator_user_id = %d) && is_confirmed = 1 ORDER BY rand() LIMIT %d " , $user_id , $user_id , $total_friends );
$results = $wpdb -> get_results ( $sql );
2012-07-11 16:28:51 +00:00
for ( $i = 0 , $count = count ( $results ); $i < $count ; ++ $i ) {
$fids [] = ( $results [ $i ] -> friend_user_id == $user_id ) ? $results [ $i ] -> initiator_user_id : $results [ $i ] -> friend_user_id ;
}
// remove duplicates
if ( count ( $fids ) > 0 )
2012-08-08 16:49:22 +00:00
return array_flip ( array_flip ( $fids ));
2012-07-11 16:28:51 +00:00
else
return false ;
}
function get_invitable_friend_count ( $user_id , $group_id ) {
// Setup some data we'll use below
$is_group_admin = BP_Groups_Member :: check_is_admin ( $user_id , $group_id );
$friend_ids = BP_Friends_Friendship :: get_friend_user_ids ( $user_id );
$invitable_count = 0 ;
for ( $i = 0 , $count = count ( $friend_ids ); $i < $count ; ++ $i ) {
// If already a member, they cannot be invited again
if ( BP_Groups_Member :: check_is_member ( ( int ) $friend_ids [ $i ], $group_id ) )
continue ;
// If user already has invite, they cannot be added
if ( BP_Groups_Member :: check_has_invite ( ( int ) $friend_ids [ $i ], $group_id ) )
continue ;
// If user is not group admin and friend is banned, they cannot be invited
if ( ( false === $is_group_admin ) && BP_Groups_Member :: check_is_banned ( ( int ) $friend_ids [ $i ], $group_id ) )
continue ;
$invitable_count ++ ;
}
return $invitable_count ;
}
function get_user_ids_for_friendship ( $friendship_id ) {
global $wpdb , $bp ;
return $wpdb -> get_row ( $wpdb -> prepare ( " SELECT friend_user_id, initiator_user_id FROM { $bp -> friends -> table_name } WHERE id = %d " , $friendship_id ) );
}
function delete_all_for_user ( $user_id ) {
global $wpdb , $bp ;
// Get friends of $user_id
$friend_ids = BP_Friends_Friendship :: get_friend_user_ids ( $user_id );
// Delete all friendships related to $user_id
$wpdb -> query ( $wpdb -> prepare ( " DELETE FROM { $bp -> friends -> table_name } WHERE friend_user_id = %d OR initiator_user_id = %d " , $user_id , $user_id ) );
// Delete friend request notifications for members who have a notification from this user.
$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 ) );
// Loop through friend_ids and update their counts
2012-08-08 16:49:22 +00:00
foreach ( ( array ) $friend_ids as $friend_id ) {
2012-07-11 16:28:51 +00:00
BP_Friends_Friendship :: total_friend_count ( $friend_id );
}
}
}
2012-08-08 16:49:22 +00:00
?>