Skip to:
Content

BuddyPress.org

Changeset 13429


Ignore:
Timestamp:
02/23/2023 12:39:47 AM (4 years ago)
Author:
imath
Message:

Revert [13428] as it was made on the wrong branch (10.0)

See #8844

Location:
branches/10.0
Files:
2 edited

Legend:

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

    r13428 r13429  
    268268                if ( empty( $user_id ) ) {
    269269                        $user_id = bp_loggedin_user_id();
    270                 }
    271 
    272                 $friendships = array();
    273                 $operator    = strtoupper( $operator );
    274 
    275                 if ( ! in_array( $operator, array( 'AND', 'OR', 'NOT' ), true ) ) {
    276                         return $friendships;
    277270                }
    278271
     
    310303                }
    311304
    312                 $int_keys  = array( 'id', 'initiator_user_id', 'friend_user_id' );
    313                 $bool_keys = array( 'is_confirmed', 'is_limited' );
    314 
    315305                // Assemble filter array.
    316306                $filters = wp_array_slice_assoc( $r, array( 'id', 'initiator_user_id', 'friend_user_id', 'is_confirmed', 'is_limited' ) );
     
    318308                        if ( is_null( $filter_value ) ) {
    319309                                unset( $filters[ $filter_name ] );
    320                         } elseif ( in_array( $filter_name, $int_keys, true ) ) {
    321                                 $filters[ $filter_name ] = (int) $filter_value;
    322                         } else {
    323                                 $filters[ $filter_name ] = (bool) $filter_value;
    324310                        }
    325311                }
    326312
    327313                // Populate friendship array from cache, and normalize.
     314                $friendships = array();
     315                $int_keys    = array( 'id', 'initiator_user_id', 'friend_user_id' );
     316                $bool_keys   = array( 'is_confirmed', 'is_limited' );
    328317                foreach ( $friendship_ids as $friendship_id ) {
    329318                        // Create a limited BP_Friends_Friendship object (don't fetch the user details).
     
    346335
    347336                        // We need to support the same operators as wp_list_filter().
    348                         if ( 'OR' === $operator || 'NOT' === $operator ) {
     337                        if ( 'OR' == $operator || 'NOT' == $operator ) {
    349338                                $matched = 0;
    350339
    351340                                foreach ( $filters as $filter_name => $filter_value ) {
    352                                         if ( isset( $friendship->{$filter_name} ) && $filter_value === $friendship->{$filter_name} ) {
     341                                        if ( isset( $friendship->{$filter_name} ) && $filter_value == $friendship->{$filter_name} ) {
    353342                                                $matched++;
    354343                                        }
    355344                                }
    356345
    357                                 if ( ( 'OR' === $operator && $matched > 0 )
    358                                   || ( 'NOT' === $operator && 0 === $matched ) ) {
     346                                if ( ( 'OR' == $operator && $matched > 0 )
     347                                  || ( 'NOT' == $operator && 0 == $matched ) ) {
    359348                                        $friendships[ $friendship->id ] = $friendship;
    360349                                }
     
    366355                                 */
    367356                                foreach ( $filters as $filter_name => $filter_value ) {
    368                                         if ( ! isset( $friendship->{$filter_name} ) || $filter_value !== $friendship->{$filter_name} ) {
     357                                        if ( ! isset( $friendship->{$filter_name} ) || $filter_value != $friendship->{$filter_name} ) {
    369358                                                continue 2;
    370359                                        }
     
    443432
    444433                $friendships = self::get_friendships( $user_id, $args );
    445                 $user_id     = (int) $user_id;
    446434
    447435                $fids = array();
    448436                foreach ( $friendships as $friendship ) {
    449                         $friend_id = $friendship->friend_user_id;
    450                         if ( $friendship->friend_user_id === $user_id ) {
    451                                 $friend_id = $friendship->initiator_user_id;
    452                         }
    453 
    454437                        if ( ! empty( $assoc_arr ) ) {
    455                                 $fids[] = array( 'user_id' => $friend_id );
     438                                $fids[] = array( 'user_id' => ( $friendship->friend_user_id == $user_id ) ? $friendship->initiator_user_id : $friendship->friend_user_id );
    456439                        } else {
    457                                 $fids[] = $friend_id;
     440                                $fids[] = ( $friendship->friend_user_id == $user_id ) ? $friendship->initiator_user_id : $friendship->friend_user_id;
    458441                        }
    459442                }
     
    654637
    655638                // Can't friend yourself.
    656                 if ( (int) $initiator_userid === (int) $possible_friend_userid ) {
     639                if ( $initiator_userid === $possible_friend_userid ) {
    657640                        return 'not_friends';
    658641                }
     
    680663
    681664                $bp                  = buddypress();
    682                 $user_id             = (int) $user_id;
    683665                $possible_friend_ids = wp_parse_id_list( $possible_friend_ids );
    684666
     
    958940                $sql     = $wpdb->prepare( "SELECT friend_user_id, initiator_user_id FROM {$bp->friends->table_name} WHERE (friend_user_id = %d || initiator_user_id = %d) && is_confirmed = 1 ORDER BY rand() LIMIT %d", $user_id, $user_id, $total_friends );
    959941                $results = $wpdb->get_results( $sql );
    960                 $user_id = (int) $user_id;
    961942
    962943                for ( $i = 0, $count = count( $results ); $i < $count; ++$i ) {
    963                         $friend_user_id    = (int) $results[ $i ]->friend_user_id;
    964                         $initiator_user_id = (int) $results[ $i ]->initiator_user_id;
    965 
    966                         if ( $friend_user_id === $user_id ) {
    967                                 $fids[] = $initiator_user_id;
    968                         } else {
    969                                 $fids[] = $friend_user_id;
    970                         }
     944                        $fids[] = ( $results[ $i ]->friend_user_id === $user_id ) ? $results[ $i ]->initiator_user_id : $results[ $i ]->friend_user_id;
    971945                }
    972946
     
    10851059                global $wpdb;
    10861060
    1087                 $bp      = buddypress();
    1088                 $user_id = (int) $user_id;
     1061                $bp = buddypress();
    10891062
    10901063                // Get all friendships, of any status, for the user.
     
    10951068                        $friendship_ids[] = $friendship->id;
    10961069                        if ( $friendship->is_confirmed ) {
    1097                                 if ( $friendship->friend_user_id === $user_id ) {
    1098                                         $friend_ids[] = $friendship->initiator_user_id;
    1099                                 } else {
    1100                                         $friend_ids[] = $friendship->friend_user_id;
    1101                                 }
     1070                                $friend_ids[] = ( $friendship->friend_user_id == $user_id ) ? $friendship->initiator_user_id : $friendship->friend_user_id;
    11021071                        }
    11031072                }
  • branches/10.0/tests/phpunit/testcases/friends/class-bp-friends-friendship.php

    r13428 r13429  
    192192                $this->assertEquals( $first_query_count, $wpdb->num_queries );
    193193        }
    194 
    195         /**
    196          * @ticket BP8844
    197          */
    198         public function test_get_random_friends() {
    199                 $user_id = self::factory()->user->create();
    200                 $friends = self::factory()->user->create_many( 4 );
    201 
    202                 foreach ( $friends as $initiator_id ) {
    203                         friends_add_friend( $initiator_id, $user_id, true );
    204                 }
    205 
    206                 $random = BP_Friends_Friendship::get_random_friends( $user_id, 4 );
    207                 $this->assertFalse( in_array( $user_id, $random, true ), 'The requested user id should not be listed into random friends' );
    208                 $this->assertTrue( 4 === count( $random ) );
    209         }
    210 
    211         /**
    212          * @ticket BP8844
    213          */
    214         public function test_get_friend_user_ids() {
    215                 $user_id = self::factory()->user->create();
    216                 $friends = self::factory()->user->create_many( 4 );
    217 
    218                 foreach ( $friends as $initiator_id ) {
    219                         friends_add_friend( $initiator_id, $user_id, true );
    220                 }
    221 
    222                 $friend_user_ids = BP_Friends_Friendship::get_friend_user_ids( $user_id );
    223                 $this->assertFalse( in_array( $user_id, $friend_user_ids, true ), 'The requested user id should not be listed into random friends' );
    224                 $this->assertTrue( 4 === count( $friend_user_ids ) );
    225         }
    226 
    227         /**
    228          * @ticket BP8844
    229          */
    230         public function test_delete_all_for_user() {
    231                 $user_id = self::factory()->user->create();
    232                 $friends = self::factory()->user->create_many( 4 );
    233 
    234                 foreach ( $friends as $initiator_id ) {
    235                         friends_add_friend( $initiator_id, $user_id, true );
    236                 }
    237 
    238                 BP_Friends_Friendship::delete_all_for_user( $user_id );
    239                 $friend_user_ids = BP_Friends_Friendship::get_friendship_ids_for_user( $user_id );
    240                 $this->assertEmpty( $friend_user_ids );
    241         }
    242 
    243         /**
    244          * @ticket BP8844
    245          */
    246         public function test_get_friendships() {
    247                 $u1 = self::factory()->user->create();
    248                 $u2 = self::factory()->user->create();
    249                 $u3 = self::factory()->user->create();
    250 
    251                 friends_add_friend( $u2, $u1, false );
    252                 friends_add_friend( $u3, $u1, true );
    253 
    254                 $friendships = BP_Friends_Friendship::get_friendships( $u1, array( 'initiator_user_id' => $u2 ), 'not' );
    255                 $friendship = reset( $friendships );
    256                 $this->assertTrue( $u3 === $friendship->initiator_user_id && 1 === count( $friendships ) );
    257 
    258                 $friendships = BP_Friends_Friendship::get_friendships( $u1, array( 'initiator_user_id' => $u3, 'is_confirmed' => 0 ), 'or' );
    259                 $this->assertTrue( 2 === count( $friendships ) );
    260         }
    261194}
Note: See TracChangeset for help on using the changeset viewer.