BP_Friends_Friendship::get_friendships( int $user_id, array $args = array(), string $operator = 'AND' )

Get the friendships for a given user.

Description

Parameters

$user_id

(Required) ID of the user whose friends are being retrieved.

$args

(Optional) Filter parameters.

  • 'id'
    (int) ID of specific friendship to retrieve.
  • 'initiator_user_id'
    (int) ID of friendship initiator.
  • 'friend_user_id'
    (int) ID of specific friendship to retrieve.
  • 'is_confirmed'
    (int) Whether the friendship has been accepted.
  • 'is_limited'
    (int) Whether the friendship is limited.
  • 'order_by'
    (string) Column name to order by.
  • 'sort_order'
    (string) ASC or DESC. Default DESC.

Default value: array()

$operator

(Optional) Operator to use in wp_list_filter().

Default value: 'AND'

Return

(array) $friendships Array of friendship objects.

Source

File: bp-friends/classes/class-bp-friends-friendship.php

241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
public static function get_friendships( $user_id, $args = array(), $operator = 'AND' ) {
 
    if ( empty( $user_id ) ) {
        $user_id = bp_loggedin_user_id();
    }
 
    $r = bp_parse_args( $args, array(
        'id'                => null,
        'initiator_user_id' => null,
        'friend_user_id'    => null,
        'is_confirmed'      => null,
        'is_limited'        => null,
        'order_by'          => 'date_created',
        'sort_order'        => 'DESC',
        'page'              => null,
        'per_page'          => null
    ), 'bp_get_user_friendships' );
 
    // First, we get all friendships that involve the user.
    $friendship_ids = wp_cache_get( $user_id, 'bp_friends_friendships_for_user' );
    if ( false === $friendship_ids ) {
        $friendship_ids = self::get_friendship_ids_for_user( $user_id );
        wp_cache_set( $user_id, $friendship_ids, 'bp_friends_friendships_for_user' );
    }
 
    // Prime the membership cache.
    $uncached_friendship_ids = bp_get_non_cached_ids( $friendship_ids, 'bp_friends_friendships' );
    if ( ! empty( $uncached_friendship_ids ) ) {
        $uncached_friendships = self::get_friendships_by_id( $uncached_friendship_ids );
 
        foreach ( $uncached_friendships as $uncached_friendship ) {
            wp_cache_set( $uncached_friendship->id, $uncached_friendship, 'bp_friends_friendships' );
        }
    }
 
    // Assemble filter array.
    $filters = wp_array_slice_assoc( $r, array( 'id', 'initiator_user_id', 'friend_user_id', 'is_confirmed', 'is_limited' ) );
    foreach ( $filters as $filter_name => $filter_value ) {
        if ( is_null( $filter_value ) ) {
            unset( $filters[ $filter_name ] );
        }
    }
 
    // Populate friendship array from cache, and normalize.
    $friendships = array();
    $int_keys    = array( 'id', 'initiator_user_id', 'friend_user_id' );
    $bool_keys   = array( 'is_confirmed', 'is_limited' );
    foreach ( $friendship_ids as $friendship_id ) {
        // Create a limited BP_Friends_Friendship object (don't fetch the user details).
        $friendship = new BP_Friends_Friendship( $friendship_id, false, false );
 
        // Sanity check.
        if ( ! isset( $friendship->id ) ) {
            continue;
        }
 
        // Integer values.
        foreach ( $int_keys as $index ) {
            $friendship->{$index} = intval( $friendship->{$index} );
        }
 
        // Boolean values.
        foreach ( $bool_keys as $index ) {
            $friendship->{$index} = (bool) $friendship->{$index};
        }
 
        // We need to support the same operators as wp_list_filter().
        if ( 'OR' == $operator || 'NOT' == $operator ) {
            $matched = 0;
 
            foreach ( $filters as $filter_name => $filter_value ) {
                if ( isset( $friendship->{$filter_name} ) && $filter_value == $friendship->{$filter_name} ) {
                    $matched++;
                }
            }
 
            if ( ( 'OR' == $operator && $matched > 0 )
              || ( 'NOT' == $operator && 0 == $matched ) ) {
                $friendships[ $friendship->id ] = $friendship;
            }
 
        } else {
            /*
             * This is the more typical 'AND' style of filter.
             * If any of the filters miss, we move on.
             */
            foreach ( $filters as $filter_name => $filter_value ) {
                if ( ! isset( $friendship->{$filter_name} ) || $filter_value != $friendship->{$filter_name} ) {
                    continue 2;
                }
            }
            $friendships[ $friendship->id ] = $friendship;
        }
 
    }
 
    // Sort the results on a column name.
    if ( in_array( $r['order_by'], array( 'id', 'initiator_user_id', 'friend_user_id' ) ) ) {
        $friendships = bp_sort_by_key( $friendships, $r['order_by'], 'num', true );
    }
 
    // Adjust the sort direction of the results.
    if ( 'ASC' === strtoupper( $r['sort_order'] ) ) {
        // `true` to preserve keys.
        $friendships = array_reverse( $friendships, true );
    }
 
    // Paginate the results.
    if ( $r['per_page'] && $r['page'] ) {
        $start       = ( $r['page'] - 1 ) * ( $r['per_page'] );
        $friendships = array_slice( $friendships, $start, $r['per_page'] );
    }
 
    return $friendships;
}

Changelog

Changelog
Version Description
BuddyPress 2.6.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.