Skip to:
Content

BuddyPress.org

Changeset 6934


Ignore:
Timestamp:
04/21/2013 05:22:31 PM (12 years ago)
Author:
boonebgorges
Message:

In BP_User_Query, allow 'include' params to be combined with the 'user_id' filter

This enhancement allows plugins to filter friend lists by passing an 'include'
parameter to BP_User_Query.

Fixes #4938

Props dontdream

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-classes.php

    r6924 r6934  
    297297
    298298        // 'user_id' - When a user id is passed, limit to the friends of the user
    299         // Only parse this if no 'include' param is passed, to account for
    300         // friend request queries
    301299        // @todo remove need for bp_is_active() check
    302         if ( empty( $include ) && ! empty( $user_id ) && bp_is_active( 'friends' ) ) {
     300        if ( ! empty( $user_id ) && bp_is_active( 'friends' ) ) {
    303301            $friend_ids = friends_get_friend_user_ids( $user_id );
    304302            $friend_ids = $wpdb->escape( implode( ',', (array) $friend_ids ) );
     
    307305                $sql['where'][] = "u.{$this->uid_name} IN ({$friend_ids})";
    308306
    309             // If the user has no friends, and we're not including specific users, make sure the query returns null
    310             } elseif ( empty( $include ) ) {
     307            // If the user has no friends, the query should always
     308            // return no users
     309            } else {
    311310                $sql['where'][] = $this->no_results['where'];
    312311            }
  • trunk/tests/testcases/core/classes.php

    r6933 r6934  
    3636        $this->assertEquals( $friend_ids, array( $u1 ) );
    3737    }
     38
     39    /**
     40     * @ticket 4938
     41     */
     42    public function test_bp_user_query_friends_with_include() {
     43        $u1 = $this->create_user();
     44        $u2 = $this->create_user();
     45        $u3 = $this->create_user();
     46        $u4 = $this->create_user();
     47        friends_add_friend( $u1, $u2, true );
     48        friends_add_friend( $u1, $u3, true );
     49
     50        $q = new BP_User_Query( array(
     51            'user_id' => $u1,
     52
     53            // Represents an independent filter passed by a plugin
     54            // u4 is not a friend of u1 and should not be returned
     55            'include' => array( $u2, $u4 ),
     56        ) );
     57
     58        $friends = is_array( $q->results ) ? array_values( $q->results ) : array();
     59        $friend_ids = wp_list_pluck( $friends, 'ID' );
     60        $this->assertEquals( $friend_ids, array( $u2 ) );
     61    }
     62
     63    public function test_bp_user_query_friends_with_include_but_zero_friends() {
     64        $u1 = $this->create_user();
     65        $u2 = $this->create_user();
     66        $u3 = $this->create_user();
     67        $u4 = $this->create_user();
     68
     69        $q = new BP_User_Query( array(
     70            'user_id' => $u1,
     71
     72            // Represents an independent filter passed by a plugin
     73            // u4 is not a friend of u1 and should not be returned
     74            'include' => array( $u2, $u4 ),
     75        ) );
     76
     77        $friends = is_array( $q->results ) ? array_values( $q->results ) : array();
     78        $friend_ids = wp_list_pluck( $friends, 'ID' );
     79        $this->assertEquals( $friend_ids, array() );
     80    }
     81
    3882}
Note: See TracChangeset for help on using the changeset viewer.