- Timestamp:
- 09/20/2016 02:42:15 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-friends/classes/class-bp-friends-friendship.php
r11123 r11124 390 390 global $wpdb; 391 391 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 ); 395 397 } 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 402 405 $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 ); 407 409 } 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; 409 411 } 410 412 } … … 423 425 */ 424 426 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; 432 447 } 433 448 … … 445 460 446 461 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 ); 452 463 453 464 wp_cache_set( $user_id, $friend_requests, 'bp_friends_requests' ); … … 475 486 global $wpdb; 476 487 477 if ( empty( $user_id ) ) 488 if ( empty( $user_id ) ) { 478 489 $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id(); 479 480 $bp = buddypress(); 490 } 481 491 482 492 /* … … 485 495 */ 486 496 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 ); 488 502 489 503 // 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 ) ) { 491 505 return 0; 506 } 492 507 493 508 bp_update_user_meta( $user_id, 'total_friend_count', (int) $count ); … … 584 599 } 585 600 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'; 593 620 } else { 594 621 $status = 'is_friend'; … … 919 946 $bp = buddypress(); 920 947 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 } 926 964 927 965 // Delete friend request notifications for members who have a … … 931 969 } 932 970 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. 934 977 foreach ( (array) $friend_ids as $friend_id ) { 978 // Delete cached friendships. 979 wp_cache_delete( $friend_id, 'bp_friends_friendships_for_user' ); 935 980 BP_Friends_Friendship::total_friend_count( $friend_id ); 936 981 } 982 983 // Delete cached friendships. 984 wp_cache_delete( $user_id, 'bp_friends_friendships_for_user' ); 937 985 } 938 986 }
Note: See TracChangeset
for help on using the changeset viewer.