Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
06/14/2014 06:49:55 PM (9 years ago)
Author:
r-a-y
Message:

Check BP_User_Query when using friends_check_friendship_status().

Previously, friends_check_friendship_status() did not reference the data
already queried in the members loop via BP_User_Query. As a result, if
a user was logged in and viewing the member directory, this added twenty
additional DB queries to the page.

This commit:

  • Adds an additional property - friendship_status - to bp_friends_filter_user_query_populate_extras(). This property references the various friendship statuses from BP_Friends_Friendship::check_is_friend()
  • Checks this new property in friends_check_friendship_status() before querying the DB
  • Adds unit tests

Props r-a-y, boonebgorges.

Fixes #5703.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-friends/bp-friends-functions.php

    r8173 r8514  
    196196 * Get the friendship status of two friends.
    197197 *
    198  * Will return 'is_friends', 'not_friends', or 'pending'.
     198 * Will return 'is_friends', 'not_friends', 'pending' or 'awaiting_response'.
    199199 *
    200200 * @param int $user_id ID of the first user.
     
    203203 */
    204204function friends_check_friendship_status( $user_id, $possible_friend_id ) {
     205    global $members_template;
     206
     207    // check the BP_User_Query first
     208    // @see bp_friends_filter_user_query_populate_extras()
     209    if ( ! empty( $members_template->in_the_loop ) ) {
     210        if ( isset( $members_template->member->friendship_status ) ) {
     211            return $members_template->member->friendship_status;
     212
     213        // make sure that the friends BP_User_Query was registered before assuming
     214        // status as 'not_friends'
     215        } elseif ( has_filter( 'bp_user_query_populate_extras', 'bp_friends_filter_user_query_populate_extras' ) ) {
     216            return 'not_friends';
     217        }
     218    }
     219
    205220    return BP_Friends_Friendship::check_is_friend( $user_id, $possible_friend_id );
    206221}
Note: See TracChangeset for help on using the changeset viewer.