Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/20/2016 02:42:15 PM (9 years ago)
Author:
dcavins
Message:

Use new function BP_Friends_Friendships::get_friendships().

Take advantage of the caching introduced
in BP_Friends_Friendships::get_friendships()
by using get_friendships() as the underlying function in
get_friend_user_ids(), get_friendship_id(), total_friend_count(),
check_is_friend(),
and delete_all_for_user().

Props dcavins, boonebgorges.

Fixes #6978.

File:
1 edited

Legend:

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

    r11123 r11124  
    390390        global $wpdb;
    391391
    392         if ( !empty( $friend_requests_only ) ) {
    393             $oc_sql = 'AND is_confirmed = 0';
    394             $friend_sql = $wpdb->prepare( " WHERE friend_user_id = %d", $user_id );
     392        if ( ! empty( $friend_requests_only ) ) {
     393            $args = array(
     394                'is_confirmed' => 0,
     395                'friend_user_id' => $user_id
     396            );
    395397        } else {
    396             $oc_sql = 'AND is_confirmed = 1';
    397             $friend_sql = $wpdb->prepare( " WHERE (initiator_user_id = %d OR friend_user_id = %d)", $user_id, $user_id );
    398         }
    399 
    400         $bp = buddypress();
    401         $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" );
     398            $args = array(
     399                'is_confirmed' => 1,
     400            );
     401        }
     402
     403        $friendships = self::get_friendships( $user_id, $args );
     404
    402405        $fids = array();
    403 
    404         for ( $i = 0, $count = count( $friends ); $i < $count; ++$i ) {
    405             if ( !empty( $assoc_arr ) ) {
    406                 $fids[] = array( 'user_id' => ( $friends[$i]->friend_user_id == $user_id ) ? $friends[$i]->initiator_user_id : $friends[$i]->friend_user_id );
     406        foreach ( $friendships as $friendship ) {
     407            if ( ! empty( $assoc_arr ) ) {
     408                $fids[] = array( 'user_id' => ( $friendship->friend_user_id == $user_id ) ? $friendship->initiator_user_id : $friendship->friend_user_id );
    407409            } else {
    408                 $fids[] = ( $friends[$i]->friend_user_id == $user_id ) ? $friends[$i]->initiator_user_id : $friends[$i]->friend_user_id;
     410                $fids[] = ( $friendship->friend_user_id == $user_id ) ? $friendship->initiator_user_id : $friendship->friend_user_id;
    409411            }
    410412        }
     
    423425     */
    424426    public static function get_friendship_id( $user_id, $friend_id ) {
    425         global $wpdb;
    426 
    427         $bp = buddypress();
    428 
    429         $query = $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 ) );
    430 
    431         return is_numeric( $query ) ? (int) $query : $query;
     427        $friendship_id = null;
     428
     429        // Can't friend yourself.
     430        if ( $user_id == $friend_id ) {
     431            return $friendship_id;
     432        }
     433
     434        /*
     435         * Find friendships where the possible_friend_userid is the
     436         * initiator or friend.
     437         */
     438        $args = array(
     439            'initiator_user_id' => $friend_id,
     440            'friend_user_id'    => $friend_id
     441        );
     442        $result = self::get_friendships( $user_id, $args, 'OR' );
     443        if ( $result ) {
     444            $friendship_id = current( $result )->id;
     445        }
     446        return $friendship_id;
    432447    }
    433448
     
    445460
    446461        if ( false === $friend_requests ) {
    447             global $wpdb;
    448 
    449             $bp = buddypress();
    450 
    451             $friend_requests = $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 ) );
     462            $friend_requests = self::get_friend_user_ids( $user_id, true );
    452463
    453464            wp_cache_set( $user_id, $friend_requests, 'bp_friends_requests' );
     
    475486        global $wpdb;
    476487
    477         if ( empty( $user_id ) )
     488        if ( empty( $user_id ) ) {
    478489            $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();
    479 
    480         $bp = buddypress();
     490        }
    481491
    482492        /*
     
    485495         */
    486496
    487         $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 ) );
     497        $args = array(
     498            'is_confirmed' => 1,
     499        );
     500        $friendships = self::get_friendships( $user_id, $args );
     501        $count       = count( $friendships );
    488502
    489503        // Do not update meta if user has never had friends.
    490         if ( empty( $count ) && !bp_get_user_meta( $user_id, 'total_friend_count', true ) )
     504        if ( ! $count && ! bp_get_user_meta( $user_id, 'total_friend_count', true ) ) {
    491505            return 0;
     506        }
    492507
    493508        bp_update_user_meta( $user_id, 'total_friend_count', (int) $count );
     
    584599        }
    585600
    586         $bp = buddypress();
    587 
    588         $result = $wpdb->get_results( $wpdb->prepare( "SELECT id, initiator_user_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)", $initiator_userid, $possible_friend_userid, $possible_friend_userid, $initiator_userid ) );
    589 
    590         if ( ! empty( $result ) ) {
    591             if ( 0 == (int) $result[0]->is_confirmed ) {
    592                 $status = $initiator_userid == $result[0]->initiator_user_id ? 'pending' : 'awaiting_response';
     601        // Can't friend yourself.
     602        if ( $initiator_userid == $possible_friend_userid ) {
     603            return 'not_friends';
     604        }
     605
     606        /*
     607         * Find friendships where the possible_friend_userid is the
     608         * initiator or friend.
     609         */
     610        $args = array(
     611            'initiator_user_id' => $possible_friend_userid,
     612            'friend_user_id'    => $possible_friend_userid
     613        );
     614        $result = self::get_friendships( $initiator_userid, $args, 'OR' );
     615
     616        if ( $result ) {
     617            $friendship = current( $result );
     618            if ( ! $friendship->is_confirmed ) {
     619                $status = $initiator_userid == $friendship->initiator_user_id ? 'pending' : 'awaiting_response';
    593620            } else {
    594621                $status = 'is_friend';
     
    919946        $bp = buddypress();
    920947
    921         // Get friends of $user_id.
    922         $friend_ids = BP_Friends_Friendship::get_friend_user_ids( $user_id );
    923 
    924         // Delete all friendships related to $user_id.
    925         $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->friends->table_name} WHERE friend_user_id = %d OR initiator_user_id = %d", $user_id, $user_id ) );
     948        // Get all friendships, of any status, for the user.
     949        $friendships    = self::get_friendships( $user_id );
     950        $friend_ids     = array();
     951        $friendship_ids = array();
     952        foreach ( $friendships as $friendship ) {
     953            $friendship_ids[] = $friendship->id;
     954            if ( $friendship->is_confirmed ) {
     955                $friend_ids[] = ( $friendship->friend_user_id == $user_id ) ? $friendship->initiator_user_id : $friendship->friend_user_id;
     956            }
     957        }
     958
     959        // Delete the friendships from the database.
     960        if ( $friendship_ids ) {
     961            $friendship_ids_sql = implode( ',', wp_parse_id_list( $friendship_ids ) );
     962            $wpdb->query( "DELETE FROM {$bp->friends->table_name} WHERE id IN ({$friendship_ids_sql})" );
     963        }
    926964
    927965        // Delete friend request notifications for members who have a
     
    931969        }
    932970
    933         // Loop through friend_ids and update their counts.
     971        // Clean up the friendships cache.
     972        foreach ( $friendship_ids as $friendship_id ) {
     973            wp_cache_delete( $friendship_id, 'bp_friends_friendships' );
     974        }
     975
     976        // Loop through friend_ids to scrub user caches and update total count metas.
    934977        foreach ( (array) $friend_ids as $friend_id ) {
     978            // Delete cached friendships.
     979            wp_cache_delete( $friend_id, 'bp_friends_friendships_for_user' );
    935980            BP_Friends_Friendship::total_friend_count( $friend_id );
    936981        }
     982
     983        // Delete cached friendships.
     984        wp_cache_delete( $user_id, 'bp_friends_friendships_for_user' );
    937985    }
    938986}
Note: See TracChangeset for help on using the changeset viewer.