Ticket #5911: 5911.04.patch
File 5911.04.patch, 17.9 KB (added by , 10 years ago) |
---|
-
src/bp-groups/bp-groups-classes.php
diff --git src/bp-groups/bp-groups-classes.php src/bp-groups/bp-groups-classes.php index b9017b9..baa7931 100644
class BP_Groups_Member { 2440 2440 * @param string $type If 'sent', results are limited to those 2441 2441 * invitations that have actually been sent (non-draft). 2442 2442 * Default: 'sent'. 2443 * @param int $inviter_id ID of the user who invited 2443 2444 * @return int|null The ID of the invitation if found, otherwise null. 2444 2445 */ 2445 public static function check_has_invite( $user_id, $group_id, $type = 'sent' ) { 2446 global $wpdb, $bp; 2446 public static function check_has_invite( $user_id, $group_id, $type = 'sent', $inviter_id = 0 ) { 2447 global $wpdb; 2448 $bp = buddypress(); 2447 2449 2448 if ( empty( $user_id ) ) 2450 if ( empty( $user_id ) ) { 2449 2451 return false; 2452 } 2450 2453 2451 $sql = "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 0 AND inviter_id != 0"; 2454 $sql = array( 2455 'select' => "SELECT id FROM {$bp->groups->table_name_members}", 2456 'where' => array( 2457 'user' => $wpdb->prepare( 'user_id = %d', $user_id ), 2458 'group' => $wpdb->prepare( 'group_id = %d', $group_id ), 2459 'confirmed' => 'is_confirmed = 0', 2460 'inviter' => 'inviter_id != 0', 2461 ), 2462 ); 2452 2463 2453 if ( 'sent' == $type ) 2454 $sql .= " AND invite_sent = 1"; 2464 if ( ! empty( $inviter_id ) ) { 2465 $sql['where']['inviter'] = $wpdb->prepare( 'inviter_id = %d', $inviter_id ); 2466 } 2467 2468 if ( 'sent' == $type ) { 2469 $sql['where']['sent'] = 'invite_sent = 1'; 2470 } 2471 2472 $where = ' WHERE ' . join( ' AND ', $sql['where'] ); 2473 $query = $sql['select'] . $where; 2455 2474 2456 return $wpdb->get_var( $ wpdb->prepare( $sql, $user_id, $group_id ));2475 return $wpdb->get_var( $query ); 2457 2476 } 2458 2477 2459 2478 /** … … class BP_Groups_Member { 2461 2480 * 2462 2481 * @param int $user_id ID of the user. 2463 2482 * @param int $group_id ID of the group. 2483 * @param string $type If 'sent', results are limited to those 2484 * invitations that have actually been sent (non-draft). 2485 * Default: 'sent'. 2486 * @param int $inviter_id ID of the user who invited 2464 2487 * @return int Number of records deleted. 2465 2488 */ 2466 public static function delete_invite( $user_id, $group_id ) { 2467 global $wpdb, $bp; 2489 public static function delete_invite( $user_id, $group_id, $type = 'sent', $inviter_id = 0 ) { 2490 global $wpdb; 2491 $bp = buddypress(); 2468 2492 2469 if ( empty( $user_id ) ) 2493 if ( empty( $user_id ) ) { 2470 2494 return false; 2495 } 2496 2497 $sql = array( 2498 'delete' => "DELETE FROM {$bp->groups->table_name_members}", 2499 'where' => array( 2500 'user' => $wpdb->prepare( 'user_id = %d', $user_id ), 2501 'group' => $wpdb->prepare( 'group_id = %d', $group_id ), 2502 'confirmed' => 'is_confirmed = 0', 2503 'inviter' => 'inviter_id != 0', 2504 ), 2505 ); 2506 2507 if ( ! empty( $inviter_id ) ) { 2508 $sql['where']['inviter'] = $wpdb->prepare( 'inviter_id = %d', $inviter_id ); 2509 } 2510 2511 if ( 'sent' == $type ) { 2512 $sql['where']['sent'] = 'invite_sent = 1'; 2513 } 2514 2515 $where = ' WHERE ' . join( ' AND ', $sql['where'] ); 2516 $query = $sql['delete'] . $where; 2471 2517 2472 return $wpdb->query( $ wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 0 AND inviter_id != 0 AND invite_sent = 1", $user_id, $group_id ));2518 return $wpdb->query( $query ); 2473 2519 } 2474 2520 2475 2521 /** -
src/bp-groups/bp-groups-functions.php
diff --git src/bp-groups/bp-groups-functions.php src/bp-groups/bp-groups-functions.php index ec3ec01..d374e84 100644
function groups_leave_group( $group_id, $user_id = 0 ) { 377 377 } 378 378 } 379 379 380 // This is exactly the same as deleting an invite, just is_confirmed = 1 NOT 0.381 if ( ! groups_uninvite_user( $user_id, $group_id ) ) {380 // This is exactly the same as rejecting an invite, just is_confirmed = 1 NOT 0. 381 if ( ! groups_reject_invite( $user_id, $group_id ) ) { 382 382 return false; 383 383 } 384 384 … … function groups_invite_user( $args = '' ) { 910 910 'is_confirmed' => 0 911 911 ); 912 912 913 $args = wp_parse_args( $args, $defaults ); 914 extract( $args, EXTR_SKIP ); 913 $r = wp_parse_args( $args, $defaults ); 915 914 916 if ( empty( $ user_id ) || empty( $group_id ) )915 if ( empty( $r['user_id'] ) || empty( $r['group_id'] ) ) { 917 916 return false; 917 } 918 918 919 // if the user has already requested membership, accept the request920 if ( $membership_id = groups_check_for_membership_request( $user_id, $group_id) ) {921 groups_accept_membership_request( $membership_id, $user_id, $group_id );919 // bail if the user is already a member 920 if ( groups_is_user_member( $r['user_id'], $r['group_id'] ) ) { 921 return false; 922 922 923 923 // Otherwise, create a new invitation 924 } else if ( ! groups_ is_user_member( $user_id, $group_id ) && ! groups_check_user_has_invite( $user_id, $group_id, 'all') ) {924 } else if ( ! groups_check_user_has_invite( $r['user_id'], $r['group_id'], 'all', $r['inviter_id'] ) ) { 925 925 $invite = new BP_Groups_Member; 926 $invite->group_id = $ group_id;927 $invite->user_id = $ user_id;928 $invite->date_modified = $ date_modified;929 $invite->inviter_id = $ inviter_id;930 $invite->is_confirmed = $ is_confirmed;926 $invite->group_id = $r['group_id']; 927 $invite->user_id = $r['user_id']; 928 $invite->date_modified = $r['date_modified']; 929 $invite->inviter_id = $r['inviter_id']; 930 $invite->is_confirmed = $r['is_confirmed']; 931 931 932 if ( ! $invite->save() )932 if ( ! $invite->save() ) { 933 933 return false; 934 } 934 935 935 do_action( 'groups_invite_user', $ args);936 do_action( 'groups_invite_user', $r ); 936 937 } 937 938 938 939 return true; … … function groups_invite_user( $args = '' ) { 945 946 * 946 947 * @param int $user_id ID of the user. 947 948 * @param int $group_id ID of the group. 949 * @param int $inviter_id ID of the user who invited. 948 950 * @return bool True on success, false on failure. 949 951 */ 950 function groups_uninvite_user( $user_id, $group_id ) {952 function groups_uninvite_user( $user_id, $group_id, $type = 'sent', $inviter_id = 0 ) { 951 953 952 if ( ! BP_Groups_Member::delete( $user_id, $group_id ) )954 if ( ! BP_Groups_Member::delete_invite( $user_id, $group_id, $type, $inviter_id ) ) { 953 955 return false; 956 } 954 957 955 do_action( 'groups_uninvite_user', $group_id, $user_id );958 do_action( 'groups_uninvite_user', $group_id, $user_id, $inviter_id ); 956 959 957 960 return true; 958 961 } … … function groups_accept_invite( $user_id, $group_id ) { 994 997 $member->delete_request( $user_id, $group_id ); 995 998 } 996 999 1000 // Remove all invites 1001 groups_uninvite_user( $user_id, $group_id, 'all' ); 1002 997 1003 // Modify group meta 998 1004 groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() ); 999 1005 … … function groups_send_invites( $user_id, $group_id ) { 1050 1056 $group = groups_get_group( array( 'group_id' => $group_id ) ); 1051 1057 1052 1058 for ( $i = 0, $count = count( $invited_users ); $i < $count; ++$i ) { 1059 $membership_id = groups_check_for_membership_request( $invited_users[$i], $group_id ); 1060 1061 if ( ! empty( $membership_id ) ) { 1062 groups_accept_membership_request( $membership_id, $invited_users[$i], $group_id ); 1063 groups_uninvite_user( $invited_users[$i], $group_id, 'all', $user_id ); 1064 continue; 1065 } 1066 1053 1067 $member = new BP_Groups_Member( $invited_users[$i], $group_id ); 1054 1068 1055 1069 // Send the actual invite … … function groups_get_invites_for_group( $user_id, $group_id ) { 1085 1099 * @param int $group_id ID of potential group. 1086 1100 * @param string $type Optional. Use 'sent' to check for sent invites, 'all' to 1087 1101 * check for all. Default: 'sent'. 1102 * @param int $inviter_id ID of the user who invited 1088 1103 * @return bool True if an invitation is found, otherwise false. 1089 1104 */ 1090 function groups_check_user_has_invite( $user_id, $group_id, $type = 'sent' ) {1091 return BP_Groups_Member::check_has_invite( $user_id, $group_id, $type );1105 function groups_check_user_has_invite( $user_id, $group_id, $type = 'sent', $inviter_id = 0 ) { 1106 return BP_Groups_Member::check_has_invite( $user_id, $group_id, $type, $inviter_id ); 1092 1107 } 1093 1108 1094 1109 /** -
src/bp-groups/bp-groups-template.php
diff --git src/bp-groups/bp-groups-template.php src/bp-groups/bp-groups-template.php index 37a4904..9137b9f 100644
function bp_group_invite_user_remove_invite_url() { 4502 4502 } 4503 4503 4504 4504 /** 4505 * Output an information if the invited user has previously requested a membership 4506 * 4507 * @since BuddyPress (2.2.0) 4508 * 4509 * @uses bp_get_group_membership_requested() 4510 */ 4511 function bp_group_membership_requested() { 4512 echo bp_get_group_membership_requested(); 4513 } 4514 add_action( 'bp_group_send_invites_item', 'bp_group_membership_requested' ); 4515 4516 /** 4517 * Returns an information if the invited user has previously requested a membership 4518 * 4519 * @since BuddyPress (2.2.0) 4520 * 4521 * @uses groups_check_for_membership_request() to check if the member has requested a membership 4522 * @uses apply_filters() calls 'bp_get_group_membership_requested' to override output 4523 * @return string HTML Output 4524 */ 4525 function bp_get_group_membership_requested() { 4526 global $invites_template; 4527 4528 $user_id = intval( $invites_template->invite->user->id ); 4529 $group_id = intval( $invites_template->invite->group_id ); 4530 $output = ''; 4531 4532 if ( ! groups_check_for_membership_request( $user_id, $group_id ) ) { 4533 return $output; 4534 } 4535 4536 $output = '<p class="description">' . sprintf( __( '%s has previously requested to join this group. Sending an invitation will automatically add the member to the group.', 'buddypress' ), $invites_template->invite->user->user_link ) . '</p>'; 4537 4538 return apply_filters( 'bp_get_group_membership_requested', $output, $user_id, $group_id ); 4539 } 4540 4541 /** 4505 4542 * Output pagination links for group invitations. 4506 4543 * 4507 4544 * @since BuddyPress (2.0.0) -
src/bp-templates/bp-legacy/buddypress-functions.php
diff --git src/bp-templates/bp-legacy/buddypress-functions.php src/bp-templates/bp-legacy/buddypress-functions.php index a5230c7..9a97411 100644
function bp_legacy_theme_ajax_invite_user() { 1037 1037 1038 1038 if ( 'invite' == $_POST['friend_action'] ) { 1039 1039 $group = groups_get_group( $group_id ); 1040 $user_status = 'is_invited'; 1040 1041 1041 1042 // Users who have previously requested membership do not need 1042 1043 // another invitation created for them 1043 1044 if ( BP_Groups_Member::check_for_membership_request( $friend_id, $group_id ) ) { 1044 1045 $user_status = 'is_pending'; 1046 } 1045 1047 1046 1048 // Create the user invitation 1047 } else if ( groups_invite_user( array( 'user_id' => $friend_id, 'group_id' => $group_id ) ) ) { 1048 $user_status = 'is_invited'; 1049 if ( ! groups_invite_user( array( 'user_id' => $friend_id, 'group_id' => $group_id ) ) ) { 1050 return; 1051 } 1049 1052 1050 // Miscellaneous failure 1051 } else { 1053 if ( ! empty( $_POST['is_new_template'] ) ) { 1052 1054 return; 1053 1055 } 1054 1056 … … function bp_legacy_theme_ajax_invite_user() { 1072 1074 exit; 1073 1075 1074 1076 } elseif ( 'uninvite' == $_POST['friend_action'] ) { 1075 // Users who have previously requested membership should not1076 // have their requests deleted on the "uninvite" action1077 if ( BP_Groups_Member::check_for_membership_request( $friend_id, $group_id ) ) {1078 return;1079 }1080 1077 1081 1078 // Remove the unsent invitation 1082 if ( ! groups_uninvite_user( $friend_id, $group_id ) ) {1079 if ( ! groups_uninvite_user( $friend_id, $group_id, 'all', bp_loggedin_user_id() ) ) { 1083 1080 return; 1084 1081 } 1085 1082 -
src/bp-templates/bp-legacy/buddypress/groups/single/send-invites.php
diff --git src/bp-templates/bp-legacy/buddypress/groups/single/send-invites.php src/bp-templates/bp-legacy/buddypress/groups/single/send-invites.php index 6f3d9ef..6fe2a58 100644
16 16 17 17 </form><!-- #send-invite-form --> 18 18 19 <?php else : ?> 20 21 <div id="message" class="info"> 22 <p><?php _e( 'Once you have built up friend connections you will be able to invite others to your group.', 'buddypress' ); ?></p> 23 </div> 24 19 25 <?php endif; ?> 20 26 21 27 <?php do_action( 'bp_after_group_send_invites_content' ); ?> -
src/bp-templates/bp-legacy/js/buddypress.js
diff --git src/bp-templates/bp-legacy/js/buddypress.js src/bp-templates/bp-legacy/js/buddypress.js index e2eec0b..8fa3ae1 100644
jq(document).ready( function() { 1039 1039 friend_action = 'uninvite'; 1040 1040 } 1041 1041 1042 ajaxdata = { 1043 action: 'groups_invite_user', 1044 'friend_action' : friend_action, 1045 'cookie' : bp_get_cookies(), 1046 '_wpnonce' : jq('#_wpnonce_invite_uninvite_user').val(), 1047 'friend_id' : friend_id, 1048 'group_id' : jq('#group_id').val(), 1049 'is_new_template' : 1, 1050 }; 1051 1042 1052 if ( ! invites_new_template ) { 1043 1053 jq( '.item-list-tabs li.selected' ).addClass( 'loading' ); 1054 ajaxdata.is_new_template = 0; 1044 1055 } 1045 1056 1046 jq.post( ajaxurl, { 1047 action: 'groups_invite_user', 1048 'friend_action': friend_action, 1049 'cookie': bp_get_cookies(), 1050 '_wpnonce': jq('#_wpnonce_invite_uninvite_user').val(), 1051 'friend_id': friend_id, 1052 'group_id': jq('#group_id').val() 1053 }, 1057 jq.post( ajaxurl, ajaxdata, 1054 1058 function(response) 1055 1059 { 1056 1060 if ( jq('#message') ) { -
tests/phpunit/testcases/groups/functions.php
diff --git tests/phpunit/testcases/groups/functions.php tests/phpunit/testcases/groups/functions.php index 6433349..eaa77b6 100644
Bar!'; 647 647 groups_accept_invite( $u2, $g ); 648 648 $this->assertEquals( 0, groups_get_invite_count_for_user( $u2 ) ); 649 649 } 650 651 /** 652 * @group invites 653 * @group BP5911 654 */ 655 public function test_groups_invite_user_private_group() { 656 $u1 = $this->factory->user->create(); 657 $u2 = $this->factory->user->create(); 658 $u3 = $this->factory->user->create(); 659 660 $g = $this->factory->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 661 662 self::add_user_to_group( $u3, $g ); 663 664 // create invitation 665 $invite_not_member = groups_invite_user( array( 666 'user_id' => $u2, 667 'group_id' => $g, 668 'inviter_id' => $u1, 669 ) ); 670 671 // create invitation 672 $invite_is_member = groups_invite_user( array( 673 'user_id' => $u3, 674 'group_id' => $g, 675 'inviter_id' => $u1, 676 ) ); 677 678 $this->assertTrue( $invite_not_member ); 679 $this->assertFalse( $invite_is_member ); 680 } 681 682 /** 683 * @group invites 684 * @group BP5911 685 */ 686 public function test_groups_invite_user_multiple_invites() { 687 $u1 = $this->factory->user->create(); 688 $u2 = $this->factory->user->create(); 689 $u3 = $this->factory->user->create(); 690 $u4 = $this->factory->user->create(); 691 692 $g = $this->factory->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 693 694 // Add u3 so that he can invite 695 self::add_user_to_group( $u3, $g ); 696 697 // u2 is requesting membership 698 groups_send_membership_request( $u2, $g ); 699 700 // u1 invites u2 701 $invite_not_member = groups_invite_user( array( 702 'user_id' => $u2, 703 'group_id' => $g, 704 'inviter_id' => $u1, 705 ) ); 706 707 // u1 invites u4 708 $invite_not_member = groups_invite_user( array( 709 'user_id' => $u4, 710 'group_id' => $g, 711 'inviter_id' => $u1, 712 ) ); 713 714 // u3 is also inviting u4 715 $invite_is_member = groups_invite_user( array( 716 'user_id' => $u4, 717 'group_id' => $g, 718 'inviter_id' => $u3, 719 ) ); 720 721 // Send invites 722 groups_send_invites( $u1, $g ); 723 groups_send_invites( $u3, $g ); 724 725 // Check u2 is now a member 726 $this->assertEquals( 1, groups_is_user_member( $u2, $g ) ); 727 728 // Check u4 has invites 729 $this->assertNotEmpty( groups_check_user_has_invite( $u4, $g ) ); 730 731 // u4 is accepting the invite 732 groups_accept_invite( $u4, $g ); 733 734 // Check u2 is now a member 735 $this->assertEquals( 1, groups_is_user_member( $u2, $g ) ); 736 737 // Check u1 & u3 has no more invites 738 $this->assertEmpty( groups_get_invites_for_group( $u1, $g ) ); 739 $this->assertEmpty( groups_get_invites_for_group( $u3, $g ) ); 740 } 741 742 /** 743 * @group invites 744 * @group BP5911 745 */ 746 public function test_groups_send_membership_request_has_multiple_invites() { 747 $u1 = $this->factory->user->create(); 748 $u2 = $this->factory->user->create(); 749 $u3 = $this->factory->user->create(); 750 751 $g = $this->factory->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 752 753 // Add u3 so that he can invite 754 self::add_user_to_group( $u3, $g ); 755 756 // u1 invites u2 757 $invite_not_member = groups_invite_user( array( 758 'user_id' => $u2, 759 'group_id' => $g, 760 'inviter_id' => $u1, 761 ) ); 762 763 // u3 invites u2 764 $invite_not_member = groups_invite_user( array( 765 'user_id' => $u2, 766 'group_id' => $g, 767 'inviter_id' => $u3, 768 ) ); 769 770 // Send invites 771 groups_send_invites( $u1, $g ); 772 groups_send_invites( $u3, $g ); 773 774 // u2 is requesting membership 775 groups_send_membership_request( $u2, $g ); 776 777 // Check u2 is now a member 778 $this->assertEquals( 1, groups_is_user_member( $u2, $g ) ); 779 780 // Check u1 & u3 has no more invites 781 $this->assertEmpty( groups_get_invites_for_group( $u1, $g ) ); 782 $this->assertEmpty( groups_get_invites_for_group( $u3, $g ) ); 783 } 784 785 /** 786 * @group invites 787 * @group BP5911 788 */ 789 public function test_groups_reject_invite_multiple_invites() { 790 $u1 = $this->factory->user->create(); 791 $u2 = $this->factory->user->create(); 792 $u3 = $this->factory->user->create(); 793 794 $g = $this->factory->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 795 796 // Add u3 so that he can invite 797 self::add_user_to_group( $u3, $g ); 798 799 // u1 invites u2 800 $invite_not_member = groups_invite_user( array( 801 'user_id' => $u2, 802 'group_id' => $g, 803 'inviter_id' => $u1, 804 ) ); 805 806 // u3 invites u2 807 $invite_not_member = groups_invite_user( array( 808 'user_id' => $u2, 809 'group_id' => $g, 810 'inviter_id' => $u3, 811 ) ); 812 813 // Send invites 814 groups_send_invites( $u1, $g ); 815 groups_send_invites( $u3, $g ); 816 817 groups_reject_invite( $u2, $g ); 818 819 // Check u2 is not a member 820 $this->assertEmpty( groups_is_user_member( $u2, $g ) ); 821 822 // Check u1 & u3 has no more invites 823 $this->assertEmpty( groups_get_invites_for_group( $u1, $g ) ); 824 $this->assertEmpty( groups_get_invites_for_group( $u3, $g ) ); 825 } 650 826 }