Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/02/2009 07:54:21 PM (15 years ago)
Author:
apeatling
Message:

Merging 1.1 branch changes and syncing.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-friends/bp-friends-classes.php

    r2055 r2077  
    88    var $is_limited;
    99    var $date_created;
    10    
     10
    1111    var $is_request;
    1212    var $populate_friend_details;
    13    
     13
    1414    var $friend;
    15    
     15
    1616    function bp_friends_friendship( $id = null, $is_request = false, $populate_friend_details = true ) {
    1717        $this->is_request = $is_request;
    18        
     18
    1919        if ( $id ) {
    2020            $this->id = $id;
     
    2626    function populate() {
    2727        global $wpdb, $bp, $creds;
    28        
     28
    2929        if ( $friendship = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->friends->table_name} WHERE id = %d", $this->id ) ) ) {
    3030            $this->initiator_user_id = $friendship->initiator_user_id;
     
    3434            $this->date_created = $friendship->date_created;
    3535        }
    36        
     36
    3737        // if running from ajax.
    3838        if ( !$bp->displayed_user->id )
    3939            $bp->displayed_user->id = $creds['current_userid'];
    40        
     40
    4141        if ( $this->populate_friend_details ) {
    4242            if ( $this->friend_user_id == $bp->displayed_user->id ) {
     
    4747        }
    4848    }
    49    
     49
    5050    function save() {
    5151        global $wpdb, $bp;
     
    5757        $this->date_created = apply_filters( 'friends_friendship_date_created_before_save', $this->date_created, $this->id );
    5858
    59         do_action( 'friends_friendship_before_save', $this );       
    60        
     59        do_action( 'friends_friendship_before_save', $this );
     60
    6161        if ( $this->id ) {
    6262            // Update
     
    6868        }
    6969
    70         do_action( 'friends_friendship_after_save', $this );       
    71        
     70        do_action( 'friends_friendship_after_save', $this );
     71
    7272        return $result;
    7373    }
    74    
     74
    7575    function delete() {
    7676        global $wpdb, $bp;
    77        
     77
    7878        return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->friends->table_name} WHERE id = %d", $this->id ) );
    7979    }
    80    
     80
    8181    /* Static Functions */
    82    
     82
    8383    function get_friend_user_ids( $user_id, $friend_requests_only = false, $assoc_arr = false ) {
    8484        global $wpdb, $bp;
    85        
     85
    8686        if ( $friend_requests_only ) {
    8787            $oc_sql = $wpdb->prepare( "AND is_confirmed = 0" );
     
    9393
    9494        $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
    9696        for ( $i = 0; $i < count($friends); $i++ ) {
    9797            if ( $assoc_arr )
     
    100100                $fids[] = ( $friends[$i]->friend_user_id == $user_id ) ? $friends[$i]->initiator_user_id : $friends[$i]->friend_user_id;
    101101        }
    102        
     102
    103103        return $fids;
    104104    }
    105    
     105
    106106    function get_friendship_ids( $user_id, $friend_requests_only = false ) {
    107107        global $wpdb, $bp;
     
    114114            $friend_sql = $wpdb->prepare ( " WHERE (initiator_user_id = %d OR friend_user_id = %d)", $user_id, $user_id );
    115115        }
    116        
     116
    117117        return $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$bp->friends->table_name} $friend_sql $oc_sql" ) );
    118118    }
    119    
     119
    120120    function get_friendship_id( $user_id, $friend_id ) {
    121121        global $wpdb, $bp;
    122        
     122
    123123        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 ) );
    124124    }
    125    
     125
    126126    function total_friend_count( $user_id = false) {
    127127        global $wpdb, $bp;
    128        
     128
    129129        if ( !$user_id )
    130130            $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.
    133133           This function will recalculate, update and return. */
    134        
     134
    135135        $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 ) );
    136136
    137137        if ( !$count )
    138138            return 0;
    139        
     139
    140140        update_usermeta( $user_id, 'total_friend_count', $count );
    141141        return $count;
    142142    }
    143    
     143
    144144    function search_friends( $filter, $user_id, $limit = null, $page = null ) {
    145145        global $wpdb, $bp;
    146        
     146
    147147        // TODO: Optimize this function.
    148        
     148
    149149        if ( !$user_id )
    150150            $user_id = $bp->loggedin_user->id;
    151        
     151
    152152        $filter = like_escape( $wpdb->escape( $filter ) );
    153        
     153
    154154        if ( $limit && $page )
    155155            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
    156        
     156
    157157        if ( !$friend_ids = BP_Friends_Friendship::get_friend_user_ids( $user_id ) )
    158158            return false;
     
    160160        // Get all the user ids for the current user's friends.
    161161        $fids = implode( ',', $friend_ids );
    162        
     162
    163163        if ( empty($fids) )
    164164            return false;
     
    173173        }
    174174
    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
    178178        if ( !$filtered_friend_ids )
    179179            return false;
     
    181181        return array( 'friends' => $filtered_friend_ids, 'total' => (int)$total_friend_ids );
    182182    }
    183            
     183
    184184    function check_is_friend( $loggedin_userid, $possible_friend_userid ) {
    185185        global $wpdb, $bp;
    186        
     186
    187187        if ( !$loggedin_userid || !$possible_friend_userid )
    188188            return false;
    189            
     189
    190190        $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
    192192        if ( $result ) {
    193193            if ( 0 == (int)$result[0]->is_confirmed ) {
     
    200200        }
    201201    }
    202    
     202
    203203    function get_bulk_last_active( $user_ids ) {
    204204        global $wpdb, $bp;
     
    206206        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" ) );
    207207    }
    208    
     208
    209209    function accept($friendship_id) {
    210210        global $wpdb, $bp;
     
    212212        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 ) );
    213213    }
    214    
     214
    215215    function reject($friendship_id) {
    216216        global $wpdb, $bp;
    217        
     217
    218218        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 ) );
    219219    }
    220    
     220
    221221    function search_users( $filter, $user_id, $limit = null, $page = null ) {
    222222        global $wpdb, $bp;
    223        
     223
    224224        $filter = like_escape( $wpdb->escape( $filter ) );
     225
    225226        $usermeta_table = $wpdb->base_prefix . 'usermeta';
    226227        $users_table = $wpdb->base_prefix . 'users';
     
    228229        if ( $limit && $page )
    229230            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
    230        
     231
    231232        // filter the user_ids based on the search criteria.
    232233        if ( function_exists('xprofile_install') ) {
     
    235236            $sql = $wpdb->prepare( "SELECT DISTINCT user_id as id FROM $usermeta_table WHERE meta_value LIKE '$filter%%' ORDER BY d.value DESC $pag_sql" );
    236237        }
    237        
    238         $filtered_fids = $wpdb->get_col($sql); 
    239        
     238
     239        $filtered_fids = $wpdb->get_col($sql);
     240
    240241        if ( !$filtered_fids )
    241242            return false;
     
    243244        return $filtered_fids;
    244245    }
    245    
     246
    246247    function search_users_count( $filter ) {
    247248        global $wpdb, $bp;
    248        
     249
    249250        $filter = like_escape( $wpdb->escape( $filter ) );
     251
    250252        $usermeta_table = $wpdb->prefix . 'usermeta';
    251253        $users_table = $wpdb->base_prefix . 'users';
    252        
     254
    253255        // filter the user_ids based on the search criteria.
    254256        if ( function_exists('xprofile_install') ) {
     
    258260        }
    259261
    260         $user_count = $wpdb->get_col($sql); 
    261        
     262        $user_count = $wpdb->get_col($sql);
     263
    262264        if ( !$user_count )
    263265            return false;
     
    265267        return $user_count[0];
    266268    }
    267    
     269
    268270    function sort_by_name( $user_ids ) {
    269271        global $wpdb, $bp;
    270        
     272
    271273        if ( !function_exists( 'xprofile_install') )
    272274            return false;
    273    
     275
    274276        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 ) );
    275277    }
    276    
     278
    277279    function get_random_friends( $user_id, $total_friends = 5 ) {
    278280        global $wpdb, $bp;
     
    284286            $fids[] = ( $results[$i]->friend_user_id == $user_id ) ? $results[$i]->initiator_user_id : $results[$i]->friend_user_id;
    285287        }
    286        
     288
    287289        // remove duplicates
    288290        if ( count($fids) > 0 )
     
    291293            return false;
    292294    }
    293    
     295
    294296    function get_invitable_friend_count( $user_id, $group_id ) {
    295297        global $wpdb, $bp;
    296298
    297299        $friend_ids = BP_Friends_Friendship::get_friend_user_ids( $user_id );
    298        
     300
    299301        $invitable_count = 0;
    300302        for ( $i = 0; $i < count($friend_ids); $i++ ) {
    301            
     303
    302304            if ( BP_Groups_Member::check_is_member( (int)$friend_ids[$i], $group_id ) )
    303305                continue;
    304            
     306
    305307            if ( BP_Groups_Member::check_has_invite( (int)$friend_ids[$i], $group_id )  )
    306308                continue;
    307                
     309
    308310            $invitable_count++;
    309311        }
     
    311313        return $invitable_count;
    312314    }
    313    
     315
    314316    function get_user_ids_for_friendship( $friendship_id ) {
    315317        global $wpdb, $bp;
     
    317319        return $wpdb->get_row( $wpdb->prepare( "SELECT friend_user_id, initiator_user_id FROM {$bp->friends->table_name} WHERE id = %d", $friendship_id ) );
    318320    }
    319    
     321
    320322    function delete_all_for_user( $user_id ) {
    321323        global $wpdb, $bp;
    322324
    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.
    326328        $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 ) );
    327329    }
    328330}
    329    
     331
    330332
    331333
Note: See TracChangeset for help on using the changeset viewer.