BP_Friends_Friendship::update_bp_friends_cache( int $user_id, int|array|string $possible_friend_ids )

Find uncached friendships between a user and one or more other users and cache them.

Description

Parameters

$user_id

(Required) The ID of the primary user for whom we want to check friendships statuses.

$possible_friend_ids

(Required) The IDs of the one or more users to check friendship status with primary user.

Return

(null)

Source

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

632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
public static function update_bp_friends_cache( $user_id, $possible_friend_ids ) {
    global $wpdb;
    $bp = buddypress();
    $possible_friend_ids = wp_parse_id_list( $possible_friend_ids );
 
    $fetch = array();
    foreach ( $possible_friend_ids as $friend_id ) {
        // Check for cached items in both friendship directions.
        if ( false === bp_core_get_incremented_cache( $user_id . ':' . $friend_id, 'bp_friends' )
            || false === bp_core_get_incremented_cache( $friend_id . ':' . $user_id, 'bp_friends' ) ) {
            $fetch[] = $friend_id;
        }
    }
    if ( empty( $fetch ) ) {
        return;
    }
 
    $friend_ids_sql = implode( ',', array_unique( $fetch ) );
    $sql = $wpdb->prepare( "SELECT initiator_user_id, friend_user_id, is_confirmed FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d AND friend_user_id IN ({$friend_ids_sql}) ) OR (initiator_user_id IN ({$friend_ids_sql}) AND friend_user_id = %d )", $user_id, $user_id );
    $friendships = $wpdb->get_results( $sql );
 
    // Use $handled to keep track of all of the $possible_friend_ids we've matched.
    $handled = array();
    foreach ( $friendships as $friendship ) {
        $initiator_user_id = (int) $friendship->initiator_user_id;
        $friend_user_id    = (int) $friendship->friend_user_id;
        if ( 1 === (int) $friendship->is_confirmed ) {
            $status_initiator = $status_friend = 'is_friend';
        } else {
            $status_initiator = 'pending';
            $status_friend    = 'awaiting_response';
        }
        bp_core_set_incremented_cache( $initiator_user_id . ':' . $friend_user_id, 'bp_friends', $status_initiator );
        bp_core_set_incremented_cache( $friend_user_id . ':' . $initiator_user_id, 'bp_friends', $status_friend );
 
        $handled[] = ( $initiator_user_id === $user_id ) ? $friend_user_id : $initiator_user_id;
    }
 
    // Set all those with no matching entry to "not friends" status.
    $not_friends = array_diff( $fetch, $handled );
 
    foreach ( $not_friends as $not_friend_id ) {
        bp_core_set_incremented_cache( $user_id . ':' . $not_friend_id, 'bp_friends', 'not_friends' );
        bp_core_set_incremented_cache( $not_friend_id . ':' . $user_id, 'bp_friends', 'not_friends' );
    }
}

Changelog

Changelog
Version Description
BuddyPress 3.0.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.