Skip to:
Content

BuddyPress.org

Ticket #5457: 5457.01.patch

File 5457.01.patch, 7.3 KB (added by r-a-y, 13 years ago)
  • bp-friends/bp-friends-classes.php

    class BP_Friends_Friendship {  
    172172        /**
    173173         * Get the IDs of a given user's friends.
    174174         *
    175          * @param int $user_id ID of the user whose friends are being retreived.
    176          * @param bool $friend_requests_only Optional. Whether to fetch
    177          *        unaccepted requests only. Default: false.
    178          * @param bool $assoc_arr Optional. True to receive an array of arrays
    179          *        keyed as 'user_id' => $user_id; false to get a one-dimensional
    180          *        array of user IDs. Default: false.
     175         * @param int $user_id The user ID whose friends are being retrieved.
     176         * @param bool $friend_requests_only Optional. Whether to only fetch people
     177         *        who initiated a friend request with the user in question. Otherwise,
     178         *        grabs user IDs regardless of who initiated the friendship. Defaults
     179         *        to false.
     180         * @param bool $get_all Optional. Whether to fetch friendships regardless of
     181         *        requested or confirmed status.  Defaults to false.
     182         * @return array
    181183         */
    182         public static function get_friend_user_ids( $user_id, $friend_requests_only = false, $assoc_arr = false ) {
     184        public static function get_friend_user_ids( $user_id, $friend_requests_only = false, $get_all = false ) {
    183185                global $wpdb, $bp;
    184186
    185                 if ( !empty( $friend_requests_only ) ) {
    186                         $oc_sql = 'AND is_confirmed = 0';
    187                         $friend_sql = $wpdb->prepare( " WHERE friend_user_id = %d", $user_id );
     187                $oc_sql = '';
     188
     189                // get the user IDs of those who initiated a friend request with this user
     190                if ( ! empty( $friend_requests_only ) ) {
     191                        // get only pending requests
     192                        if ( ! $get_all ) {
     193                                $oc_sql = 'AND is_confirmed = 0';
     194                        }
     195
     196                        $fids = $wpdb->get_col( $wpdb->prepare( "SELECT initiator_user_id FROM {$bp->friends->table_name} WHERE friend_user_id = %d {$oc_sql} ORDER BY date_created DESC", $user_id ) );
     197
     198                // get the user IDs where either this user initiated the friendship or where
     199                // other users requested this user
    188200                } else {
    189                         $oc_sql = 'AND is_confirmed = 1';
    190                         $friend_sql = $wpdb->prepare( " WHERE (initiator_user_id = %d OR friend_user_id = %d)", $user_id, $user_id );
    191                 }
     201                        // get only confirmed requests
     202                        if ( ! $get_all ) {
     203                                $oc_sql = 'AND is_confirmed = 1';
     204                        }
    192205
    193                 $friends = $wpdb->get_results( "SELECT friend_user_id, initiator_user_id FROM {$bp->friends->table_name} {$friend_sql} {$oc_sql} ORDER BY date_created DESC" );
    194                 $fids = array();
     206                        $friends = $wpdb->get_results( $wpdb->prepare( "SELECT friend_user_id, initiator_user_id FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d OR friend_user_id = %d) {$oc_sql} ORDER BY date_created DESC", $user_id, $user_id ) );
    195207
    196                 for ( $i = 0, $count = count( $friends ); $i < $count; ++$i ) {
    197                         if ( !empty( $assoc_arr ) ) {
    198                                 $fids[] = array( 'user_id' => ( $friends[$i]->friend_user_id == $user_id ) ? $friends[$i]->initiator_user_id : $friends[$i]->friend_user_id );
    199                         } else {
     208                        $fids = array();
     209       
     210                        // loop through each result and add to friend ids array
     211                        for ( $i = 0, $count = count( $friends ); $i < $count; ++$i ) {
    200212                                $fids[] = ( $friends[$i]->friend_user_id == $user_id ) ? $friends[$i]->initiator_user_id : $friends[$i]->friend_user_id;
    201213                        }
     214
    202215                }
    203216
    204217                return $fids;
    … … class BP_Friends_Friendship {  
    219232        }
    220233
    221234        /**
    222          * Get a list of IDs of users who have requested friendship of a given user.
     235         * Get a list of user IDs that have requested friendship for a given user.
     236         *
     237         * Friendships with these user IDs have not been confirmed by the user yet.
    223238         *
    224239         * @param int $user_id The ID of the user who has received the
    225240         *        friendship requests.
  • bp-friends/bp-friends-functions.php

    function friends_get_friendship_id( $initiator_user_id, $friend_user_id ) {  
    249249/**
    250250 * Get the IDs of a given user's friends.
    251251 *
    252  * @param int $user_id ID of the user whose friends are being retreived.
    253  * @param bool $friend_requests_only Optional. Whether to fetch unaccepted
    254  *        requests only. Default: false.
    255  * @param bool $assoc_arr Optional. True to receive an array of arrays keyed as
    256  *        'user_id' => $user_id; false to get a one-dimensional array of user
    257  *        IDs. Default: false.
     252 * @param int $user_id The user ID whose friends are being retrieved.
     253 * @param bool $friend_requests_only Optional. Whether to only fetch people
     254 *        who initiated a friend request with the user in question. Otherwise,
     255 *        grabs user IDs regardless of who initiated the friendship. Defaults
     256 *        to false.
     257 * @param bool $get_all Optional. Whether to fetch friendships regardless of
     258 *        requested or confirmed status.  Defaults to false.
     259 * @return array
    258260 */
    259 function friends_get_friend_user_ids( $user_id, $friend_requests_only = false, $assoc_arr = false ) {
    260         return BP_Friends_Friendship::get_friend_user_ids( $user_id, $friend_requests_only, $assoc_arr );
     261function friends_get_friend_user_ids( $user_id, $friend_requests_only = false, $get_all = false ) {
     262        return BP_Friends_Friendship::get_friend_user_ids( $user_id, $friend_requests_only, $get_all );
    261263}
    262264
    263265/**
  • tests/testcases/friends/class-bp-friends-friendship.php

    class BP_Tests_BP_Friends_Friendship_TestCases extends BP_UnitTestCase {  
    115115                friends_add_friend( $u1, $u2, true );
    116116                $this->assertEquals( 'is_friend', BP_Friends_Friendship::check_is_friend( $u1, $u2 ) );
    117117        }
     118
     119        /**
     120         * @group get_friend_user_ids
     121         */
     122        public function test_get_friend_user_ids_request_only() {
     123                $u1 = $this->create_user();
     124                $u2 = $this->create_user();
     125                $u3 = $this->create_user();
     126
     127                // force add friendship
     128                friends_add_friend( $u2, $u1, true );
     129
     130                // user 3 initiates friendship with user 1
     131                friends_add_friend( $u3, $u1 );
     132
     133                // get unconfirmed user IDs that have requested friendship with user 1
     134                $this->assertEquals( array( $u3 ), BP_Friends_Friendship::get_friend_user_ids( $u1, true ) );
     135        }
     136
     137        /**
     138         * @group get_friend_user_ids
     139         */
     140        public function test_get_friend_user_ids_confirmed_only() {
     141                $u1 = $this->create_user();
     142                $u2 = $this->create_user();
     143                $u3 = $this->create_user();
     144
     145                // force add friendship
     146                friends_add_friend( $u2, $u1, true );
     147
     148                // add a friendship request
     149                friends_add_friend( $u3, $u1 );
     150
     151                // get only confirmed friend user IDs for user 1
     152                $this->assertEquals( array( $u2 ), BP_Friends_Friendship::get_friend_user_ids( $u1 ) );
     153        }
     154
     155        /**
     156         * @group get_friend_user_ids
     157         */
     158        public function test_get_friend_user_ids_all() {
     159                $u1 = $this->create_user();
     160                $u2 = $this->create_user();
     161                $u3 = $this->create_user();
     162
     163                // user 1 requests user 3; force friendship
     164                friends_add_friend( $u1, $u3, true );
     165
     166                // user 2 requests user 1
     167                friends_add_friend( $u2, $u1 );
     168
     169                // get all user IDs for friendships with user 1 regardless of confirmed status
     170                $this->assertEquals( array( $u3, $u2 ), BP_Friends_Friendship::get_friend_user_ids( $u1, false, true ) );
     171        }
    118172}