Skip to:
Content

BuddyPress.org

Changeset 8514


Ignore:
Timestamp:
06/14/2014 06:49:55 PM (12 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.

Location:
trunk
Files:
3 edited

Legend:

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

    r7999 r8514  
    3939
    4040                if ( isset( $user_query->results[ $friend_id ] ) ) {
    41                         $user_query->results[ $friend_id ]->is_friend = $fs->is_confirmed;
     41                        if ( 0 == $fs->is_confirmed ) {
     42                                $status = $fs->initiator_user_id == bp_loggedin_user_id() ? 'pending' : 'awaiting_response';
     43                        } else {
     44                                $status = 'is_friend';
     45                        }
     46
     47                        $user_query->results[ $friend_id ]->is_friend         = $fs->is_confirmed;
     48                        $user_query->results[ $friend_id ]->friendship_status = $status;
    4249                }
    4350        }
  • 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}
  • trunk/tests/phpunit/testcases/friends/functions.php

    r8173 r8514  
    136136                $this->assertTrue( friends_add_friend( $u1, $u2 ) );
    137137        }
     138
     139        /**
     140         * @group friends_check_friendship_status
     141         */
     142        public function test_friends_check_friendship_status_in_members_loop() {
     143                $now = time();
     144                $u1 = $this->create_user( array(
     145                        'last_activity' => date( 'Y-m-d H:i:s', $now ),
     146                ) );
     147                $u2 = $this->create_user( array(
     148                        'last_activity' => date( 'Y-m-d H:i:s', $now - 100 ),
     149                ) );
     150                $u3 = $this->create_user( array(
     151                        'last_activity' => date( 'Y-m-d H:i:s', $now - 200 ),
     152                ) );
     153                $u4 = $this->create_user( array(
     154                        'last_activity' => date( 'Y-m-d H:i:s', $now - 300 ),
     155                ) );
     156                $u5 = $this->create_user( array(
     157                        'last_activity' => date( 'Y-m-d H:i:s', $now - 400 ),
     158                ) );
     159
     160                friends_add_friend( $u1, $u2, true );
     161                friends_add_friend( $u1, $u3, false );
     162                friends_add_friend( $u4, $u1, false );
     163
     164                $old_user = get_current_user_id();
     165                $this->set_current_user( $u1 );
     166
     167                $found = array();
     168                if ( bp_has_members() ) : while ( bp_members() ) : bp_the_member();
     169                        $found[ bp_get_member_user_id() ] = friends_check_friendship_status( $u1, bp_get_member_user_id() );
     170                endwhile; endif;
     171
     172                $expected = array(
     173                        $u1 => 'not_friends',
     174                        $u2 => 'is_friend',
     175                        $u3 => 'pending',
     176                        $u4 => 'awaiting_response',
     177                        $u5 => 'not_friends',
     178                );
     179
     180                $this->assertSame( $expected, $found );
     181
     182                // clean up
     183                $GLOBALS['members_template'] = null;
     184                $this->set_current_user( $old_user );
     185        }
     186
     187        /**
     188         * @group friends_check_friendship_status
     189         */
     190        public function test_friends_check_friendship_status_not_in_members_loop() {
     191                $now = time();
     192                $u1 = $this->create_user( array(
     193                        'last_activity' => date( 'Y-m-d H:i:s', $now ),
     194                ) );
     195                $u2 = $this->create_user( array(
     196                        'last_activity' => date( 'Y-m-d H:i:s', $now - 100 ),
     197                ) );
     198                $u3 = $this->create_user( array(
     199                        'last_activity' => date( 'Y-m-d H:i:s', $now - 200 ),
     200                ) );
     201                $u4 = $this->create_user( array(
     202                        'last_activity' => date( 'Y-m-d H:i:s', $now - 300 ),
     203                ) );
     204                $u5 = $this->create_user( array(
     205                        'last_activity' => date( 'Y-m-d H:i:s', $now - 400 ),
     206                ) );
     207
     208                friends_add_friend( $u1, $u2, true );
     209                friends_add_friend( $u1, $u3, false );
     210                friends_add_friend( $u4, $u1, false );
     211
     212                $found = array(
     213                        $u1 => friends_check_friendship_status( $u1, $u1 ),
     214                        $u2 => friends_check_friendship_status( $u1, $u2 ),
     215                        $u3 => friends_check_friendship_status( $u1, $u3 ),
     216                        $u4 => friends_check_friendship_status( $u1, $u4 ),
     217                        $u5 => friends_check_friendship_status( $u1, $u5 ),
     218                );
     219
     220                $expected = array(
     221                        $u1 => 'not_friends',
     222                        $u2 => 'is_friend',
     223                        $u3 => 'pending',
     224                        $u4 => 'awaiting_response',
     225                        $u5 => 'not_friends',
     226                );
     227
     228                $this->assertSame( $expected, $found );
     229        }
    138230}
Note: See TracChangeset for help on using the changeset viewer.