- Timestamp:
- 08/24/2021 04:16:17 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-friends/classes/class-bp-friends-friendship.php
r13086 r13092 4 4 * 5 5 * @package BuddyPress 6 * @subpackage Friends Classes6 * @subpackage FriendsFriendship 7 7 * @since 1.0.0 8 8 */ … … 132 132 * 133 133 * @since 1.0.0 134 * 135 * @global BuddyPress $bp The one true BuddyPress instance. 136 * @global wpdb $wpdb WordPress database object. 134 137 */ 135 138 public function populate() { … … 161 164 162 165 if ( ! empty( $this->populate_friend_details ) ) { 163 if ( $this->friend_user_id == bp_displayed_user_id()) {166 if ( bp_displayed_user_id() === $this->friend_user_id ) { 164 167 $this->friend = new BP_Core_User( $this->initiator_user_id ); 165 168 } else { … … 174 177 * @since 1.0.0 175 178 * 179 * @global BuddyPress $bp The one true BuddyPress instance. 180 * @global wpdb $wpdb WordPress database object. 181 * 176 182 * @return bool True on success, false on failure. 177 183 */ … … 182 188 183 189 $this->initiator_user_id = apply_filters( 'friends_friendship_initiator_user_id_before_save', $this->initiator_user_id, $this->id ); 184 $this->friend_user_id = apply_filters( 'friends_friendship_friend_user_id_before_save', $this->friend_user_id,$this->id );185 $this->is_confirmed = apply_filters( 'friends_friendship_is_confirmed_before_save', $this->is_confirmed,$this->id );186 $this->is_limited = apply_filters( 'friends_friendship_is_limited_before_save', $this->is_limited,$this->id );187 $this->date_created = apply_filters( 'friends_friendship_date_created_before_save', $this->date_created,$this->id );190 $this->friend_user_id = apply_filters( 'friends_friendship_friend_user_id_before_save', $this->friend_user_id, $this->id ); 191 $this->is_confirmed = apply_filters( 'friends_friendship_is_confirmed_before_save', $this->is_confirmed, $this->id ); 192 $this->is_limited = apply_filters( 'friends_friendship_is_limited_before_save', $this->is_limited, $this->id ); 193 $this->date_created = apply_filters( 'friends_friendship_date_created_before_save', $this->date_created, $this->id ); 188 194 189 195 /** … … 192 198 * @since 1.0.0 193 199 * 194 * @param BP_Friends_Friendship $value Current friendship request object.200 * @param BP_Friends_Friendship $value Current friendship object. Passed by reference. 195 201 */ 196 202 do_action_ref_array( 'friends_friendship_before_save', array( &$this ) ); 197 203 198 204 // Update. 199 if ( !empty( $this->id ) ) {205 if ( ! empty( $this->id ) ) { 200 206 $result = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->friends->table_name} SET initiator_user_id = %d, friend_user_id = %d, is_confirmed = %d, is_limited = %d, date_created = %s WHERE id = %d", $this->initiator_user_id, $this->friend_user_id, $this->is_confirmed, $this->is_limited, $this->date_created, $this->id ) ); 201 207 … … 211 217 * @since 1.0.0 212 218 * 213 * @param BP_Friends_Friendship $value Current friendship request object.219 * @param BP_Friends_Friendship $value Current friendship object. Passed by reference. 214 220 */ 215 221 do_action_ref_array( 'friends_friendship_after_save', array( &$this ) ); … … 223 229 * @since 1.0.0 224 230 * 231 * @global BuddyPress $bp The one true BuddyPress instance. 232 * @global wpdb $wpdb WordPress database object. 233 * 225 234 * @return bool|int 226 235 */ … … 240 249 * @since 2.6.0 241 250 * 242 * @param int $user_idID of the user whose friends are being retrieved.243 * @param array $args{251 * @param int $user_id ID of the user whose friends are being retrieved. 252 * @param array $args { 244 253 * Optional. Filter parameters. 245 254 * @type int $id ID of specific friendship to retrieve. … … 251 260 * @type string $sort_order ASC or DESC. Default DESC. 252 261 * } 253 * @param string $operator 262 * @param string $operator Optional. Operator to use in `wp_list_filter()`. 254 263 * 255 264 * @return array $friendships Array of friendship objects. … … 376 385 * @since 2.7.0 377 386 * 387 * @global BuddyPress $bp The one true BuddyPress instance. 388 * @global wpdb $wpdb WordPress database object. 389 * 378 390 * @param int $user_id ID of the user. 379 391 * @return array … … 403 415 */ 404 416 public static function get_friend_user_ids( $user_id, $friend_requests_only = false, $assoc_arr = false ) { 405 global $wpdb;406 417 407 418 if ( ! empty( $friend_requests_only ) ) { 408 419 $args = array( 409 'is_confirmed' => 0,410 'friend_user_id' => $user_id 420 'is_confirmed' => 0, 421 'friend_user_id' => $user_id, 411 422 ); 412 423 } else { … … 443 454 444 455 // Can't friend yourself. 445 if ( $user_id == $friend_id ) {456 if ( $user_id === $friend_id ) { 446 457 return $friendship_id; 447 458 } … … 453 464 $args = array( 454 465 'initiator_user_id' => $friend_id, 455 'friend_user_id' => $friend_id 466 'friend_user_id' => $friend_id, 456 467 ); 468 457 469 $result = self::get_friendships( $user_id, $args, 'OR' ); 458 470 if ( $result ) { … … 469 481 * @param int $user_id The ID of the user who has received the 470 482 * friendship requests. 471 * @return array|bool An array of user IDs ,or false if none are found.483 * @return array|bool An array of user IDs or false if none are found. 472 484 */ 473 485 public static function get_friendship_request_user_ids( $user_id ) { … … 499 511 */ 500 512 public static function total_friend_count( $user_id = 0 ) { 501 global $wpdb;502 513 503 514 if ( empty( $user_id ) ) { … … 510 521 */ 511 522 512 $args = array( 513 'is_confirmed' => 1, 514 ); 523 $args = array( 'is_confirmed' => 1 ); 515 524 $friendships = self::get_friendships( $user_id, $args ); 516 525 $count = count( $friendships ); … … 529 538 * Search the friends of a user by a search string. 530 539 * 531 * @since 1.0.0 540 * @todo Optimize this function. 541 * 542 * @since 1.0.0 543 * 544 * @global BuddyPress $bp The one true BuddyPress instance. 545 * @global wpdb $wpdb WordPress database object. 532 546 * 533 547 * @param string $filter The search string, matched against xprofile … … 546 560 global $wpdb; 547 561 548 /* 549 * TODO: Optimize this function. 550 */ 551 552 if ( empty( $user_id ) ) 562 $bp = buddypress(); 563 564 if ( empty( $user_id ) ) { 553 565 $user_id = bp_loggedin_user_id(); 566 } 554 567 555 568 // Only search for matching strings at the beginning of the … … 558 571 559 572 $pag_sql = ''; 560 if ( ! empty( $limit ) && !empty( $page ) )573 if ( ! empty( $limit ) && ! empty( $page ) ) { 561 574 $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 562 563 if ( !$friend_ids = BP_Friends_Friendship::get_friend_user_ids( $user_id ) ) 575 } 576 577 $friend_ids = self::get_friend_user_ids( $user_id ); 578 if ( ! $friend_ids ) { 564 579 return false; 580 } 565 581 566 582 // Get all the user ids for the current user's friends. 567 583 $fids = implode( ',', wp_parse_id_list( $friend_ids ) ); 568 584 569 if ( empty( $fids ) ) 585 if ( empty( $fids ) ) { 570 586 return false; 571 572 $bp = buddypress(); 587 } 573 588 574 589 // Filter the user_ids based on the search criteria. … … 584 599 $total_friend_ids = $wpdb->get_var( $total_sql ); 585 600 586 if ( empty( $filtered_friend_ids ) ) 601 if ( empty( $filtered_friend_ids ) ) { 587 602 return false; 588 589 return array( 'friends' => array_map( 'intval', $filtered_friend_ids ), 'total' => (int) $total_friend_ids ); 603 } 604 605 return array( 606 'friends' => array_map( 'intval', $filtered_friend_ids ), 607 'total' => (int) $total_friend_ids, 608 ); 590 609 } 591 610 … … 608 627 */ 609 628 public static function check_is_friend( $initiator_userid, $possible_friend_userid ) { 610 global $wpdb;611 629 612 630 if ( empty( $initiator_userid ) || empty( $possible_friend_userid ) ) { … … 615 633 616 634 // Can't friend yourself. 617 if ( $initiator_userid == $possible_friend_userid ) {635 if ( $initiator_userid === $possible_friend_userid ) { 618 636 return 'not_friends'; 619 637 } 620 638 621 BP_Friends_Friendship::update_bp_friends_cache( $initiator_userid, $possible_friend_userid );639 self::update_bp_friends_cache( $initiator_userid, $possible_friend_userid ); 622 640 623 641 return bp_core_get_incremented_cache( $initiator_userid . ':' . $possible_friend_userid, 'bp_friends' ); 624 642 } 625 643 626 627 644 /** 628 645 * Find uncached friendships between a user and one or more other users and cache them. … … 630 647 * @since 3.0.0 631 648 * 632 * @param int $user_id The ID of the primary user for whom we want 649 * @global BuddyPress $bp The one true BuddyPress instance. 650 * @global wpdb $wpdb WordPress database object. 651 * 652 * @param int $user_id The ID of the primary user for whom we want 633 653 * to check friendships statuses. 634 654 * @param int|array|string $possible_friend_ids The IDs of the one or more users 635 655 * to check friendship status with primary user. 636 * @return null637 656 */ 638 657 public static function update_bp_friends_cache( $user_id, $possible_friend_ids ) { 639 658 global $wpdb; 640 $bp = buddypress(); 659 660 $bp = buddypress(); 641 661 $possible_friend_ids = wp_parse_id_list( $possible_friend_ids ); 642 662 … … 649 669 } 650 670 } 671 651 672 if ( empty( $fetch ) ) { 652 673 return; … … 695 716 */ 696 717 public static function get_bulk_last_active( $user_ids ) { 697 global $wpdb;698 699 718 $last_activities = BP_Core_User::get_last_activity( $user_ids ); 700 719 701 720 // Sort and structure as expected in legacy function. 702 721 usort( $last_activities, function( $a, $b ) { 703 if ( $a['date_recorded'] == $b['date_recorded'] ) {722 if ( $a['date_recorded'] === $b['date_recorded'] ) { 704 723 return 0; 705 724 } … … 710 729 $retval = array(); 711 730 foreach ( $last_activities as $last_activity ) { 712 $u = new stdClass;731 $u = new stdClass(); 713 732 $u->last_activity = $last_activity['date_recorded']; 714 733 $u->user_id = $last_activity['user_id']; … … 725 744 * @since 1.0.0 726 745 * 746 * @global BuddyPress $bp The one true BuddyPress instance. 747 * @global wpdb $wpdb WordPress database object. 748 * 727 749 * @param int $friendship_id ID of the friendship to be accepted. 728 750 * @return int Number of database rows updated. 729 751 */ 730 public static function accept( $friendship_id) {752 public static function accept( $friendship_id ) { 731 753 global $wpdb; 732 754 … … 740 762 * 741 763 * @since 1.6.0 764 * 765 * @global BuddyPress $bp The one true BuddyPress instance. 766 * @global wpdb $wpdb WordPress database object. 742 767 * 743 768 * @param int $friendship_id ID of the friendship to be withdrawn. 744 769 * @return int Number of database rows deleted. 745 770 */ 746 public static function withdraw( $friendship_id) {771 public static function withdraw( $friendship_id ) { 747 772 global $wpdb; 748 773 … … 756 781 * 757 782 * @since 1.0.0 783 * 784 * @global BuddyPress $bp The one true BuddyPress instance. 785 * @global wpdb $wpdb WordPress database object. 758 786 * 759 787 * @param int $friendship_id ID of the friendship to be rejected. 760 788 * @return int Number of database rows deleted. 761 789 */ 762 public static function reject( $friendship_id) {790 public static function reject( $friendship_id ) { 763 791 global $wpdb; 764 792 … … 774 802 * 775 803 * @since 1.0.0 804 * 805 * @global BuddyPress $bp The one true BuddyPress instance. 806 * @global wpdb $wpdb WordPress database object. 776 807 * 777 808 * @param string $filter String to search by. … … 793 824 794 825 $pag_sql = ''; 795 if ( ! empty( $limit ) && !empty( $page ) )826 if ( ! empty( $limit ) && ! empty( $page ) ) { 796 827 $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * intval( $limit ) ), intval( $limit ) ); 828 } 797 829 798 830 $bp = buddypress(); … … 805 837 } 806 838 807 $filtered_fids = $wpdb->get_col( $sql);808 809 if ( empty( $filtered_fids ) ) 839 $filtered_fids = $wpdb->get_col( $sql ); 840 841 if ( empty( $filtered_fids ) ) { 810 842 return false; 843 } 811 844 812 845 return $filtered_fids; … … 819 852 * 820 853 * @since 1.0.0 854 * 855 * @global BuddyPress $bp The one true BuddyPress instance. 856 * @global wpdb $wpdb WordPress database object. 821 857 * 822 858 * @param string $filter Search term. … … 842 878 } 843 879 844 $user_count = $wpdb->get_col( $sql);845 846 if ( empty( $user_count ) ) 880 $user_count = $wpdb->get_col( $sql ); 881 882 if ( empty( $user_count ) ) { 847 883 return false; 884 } 848 885 849 886 return $user_count[0]; … … 857 894 * @since 1.0.0 858 895 * 896 * @global BuddyPress $bp The one true BuddyPress instance. 897 * @global wpdb $wpdb WordPress database object. 898 * 859 899 * @param array $user_ids Array of user IDs. 860 * @return array User IDs, sorted by the associated display names. 900 * @return array|bool User IDs, sorted by the associated display names. 901 * False if XProfile component is not active. 861 902 */ 862 903 public static function sort_by_name( $user_ids ) { 863 904 global $wpdb; 864 905 865 if ( ! bp_is_active( 'xprofile' ) )906 if ( ! bp_is_active( 'xprofile' ) ) { 866 907 return false; 908 } 867 909 868 910 $bp = buddypress(); … … 877 919 * 878 920 * @since 1.0.0 921 * 922 * @global BuddyPress $bp The one true BuddyPress instance. 923 * @global wpdb $wpdb WordPress database object. 879 924 * 880 925 * @param int $user_id ID of the user whose friends are being retrieved. … … 893 938 894 939 for ( $i = 0, $count = count( $results ); $i < $count; ++$i ) { 895 $fids[] = ( $results[ $i]->friend_user_id == $user_id ) ? $results[$i]->initiator_user_id : $results[$i]->friend_user_id;940 $fids[] = ( $results[ $i ]->friend_user_id === $user_id ) ? $results[ $i ]->initiator_user_id : $results[ $i ]->friend_user_id; 896 941 } 897 942 898 943 // Remove duplicates. 899 if ( count( $fids ) > 0 ) 944 if ( count( $fids ) > 0 ) { 900 945 return array_flip( array_flip( $fids ) ); 901 else 902 return false; 946 } 947 948 return false; 903 949 } 904 950 … … 912 958 * - users who have been banned from the group 913 959 * 914 * @since 1.0.0915 960 * @todo Need to do a group component check before using group functions. 961 * 962 * @since 1.0.0 916 963 * 917 964 * @param int $user_id ID of the user whose friends are being counted. 918 965 * @param int $group_id ID of the group friends are being invited to. 919 * @return int $invitable_count Eligiblefriend count.966 * @return bool|int False if group component is not active, and friend count. 920 967 */ 921 968 public static function get_invitable_friend_count( $user_id, $group_id ) { 969 970 if ( ! bp_is_active( 'group' ) ) { 971 return false; 972 } 922 973 923 974 // Setup some data we'll use below. 924 975 $is_group_admin = groups_is_user_admin( $user_id, $group_id ); 925 $friend_ids = BP_Friends_Friendship::get_friend_user_ids( $user_id );976 $friend_ids = self::get_friend_user_ids( $user_id ); 926 977 $invitable_count = 0; 927 978 … … 929 980 930 981 // If already a member, they cannot be invited again. 931 if ( groups_is_user_member( (int) $friend_ids[ $i], $group_id ) ) {982 if ( groups_is_user_member( (int) $friend_ids[ $i ], $group_id ) ) { 932 983 continue; 933 984 } 934 985 935 986 // If user already has invite, they cannot be added. 936 if ( groups_check_user_has_invite( (int) $friend_ids[ $i], $group_id ) ) {987 if ( groups_check_user_has_invite( (int) $friend_ids[ $i ], $group_id ) ) { 937 988 continue; 938 989 } 939 990 940 991 // If user is not group admin and friend is banned, they cannot be invited. 941 if ( ( false === $is_group_admin ) && groups_is_user_banned( (int) $friend_ids[ $i], $group_id ) ) {992 if ( ( false === $is_group_admin ) && groups_is_user_banned( (int) $friend_ids[ $i ], $group_id ) ) { 942 993 continue; 943 994 } … … 953 1004 * 954 1005 * @since 2.7.0 1006 * 1007 * @global BuddyPress $bp The one true BuddyPress instance. 1008 * @global wpdb $wpdb WordPress database object. 955 1009 * 956 1010 * @param int|string|array $friendship_ids Single friendship ID or comma-separated/array list of friendship IDs. … … 972 1026 * 973 1027 * @param int $friendship_id ID of the friendship. 974 * @return null|stdClass friend_user_id and initiator_user_id.1028 * @return null|stdClass 975 1029 */ 976 1030 public static function get_user_ids_for_friendship( $friendship_id ) { 977 978 1031 $friendship = new BP_Friends_Friendship( $friendship_id, false, false ); 979 1032 … … 982 1035 } 983 1036 984 $retval = new StdClass;985 $retval->friend_user_id = $friendship->friend_user_id;1037 $retval = new StdClass(); 1038 $retval->friend_user_id = $friendship->friend_user_id; 986 1039 $retval->initiator_user_id = $friendship->initiator_user_id; 987 1040 … … 993 1046 * 994 1047 * @since 1.0.0 1048 * 1049 * @global BuddyPress $bp The one true BuddyPress instance. 1050 * @global wpdb $wpdb WordPress database object. 995 1051 * 996 1052 * @param int $user_id ID of the user being expunged. … … 1033 1089 // Delete cached friendships. 1034 1090 wp_cache_delete( $friend_id, 'bp_friends_friendships_for_user' ); 1035 BP_Friends_Friendship::total_friend_count( $friend_id ); 1091 1092 self::total_friend_count( $friend_id ); 1036 1093 } 1037 1094
Note: See TracChangeset
for help on using the changeset viewer.