Skip to:
Content

BuddyPress.org

Ticket #6025: 6025.04.patch

File 6025.04.patch, 11.8 KB (added by r-a-y, 12 years ago)
  • src/bp-groups/bp-groups-functions.php

     
    10371037/**
    10381038 * Send all pending invites by a single user to a specific group.
    10391039 *
    1040  * @param int $user_id ID of the inviting user.
     1040 * @param int $inviter_id ID of the inviting user.
    10411041 * @param int $group_id ID of the group.
     1042 * @return bool False if there's actually a problem, true if the invitations that need to be sent are sent successfully (or if there's nothing to do).
    10421043 */
    1043 function groups_send_invites( $user_id, $group_id ) {
     1044function groups_send_invites( $inviter_id, $group_id ) {
    10441045
    1045         if ( empty( $user_id ) )
    1046                 $user_id = bp_loggedin_user_id();
     1046        if ( empty( $inviter_id ) ) {
     1047                $inviter_id = bp_loggedin_user_id();
     1048        }
     1049        if ( empty( $group_id ) ) {
     1050                $group_id = bp_get_current_group_id();
     1051        }
    10471052
    1048         // Send friend invites.
    1049         $invited_users = groups_get_invites_for_group( $user_id, $group_id );
    1050         $group = groups_get_group( array( 'group_id' => $group_id ) );
     1053        // If the inviter can't extend invitations to this group, bail.
     1054        if ( ! bp_groups_user_can_send_invites( $group_id, $inviter_id ) ) {
     1055                return false;
     1056        }
    10511057
    1052         for ( $i = 0, $count = count( $invited_users ); $i < $count; ++$i ) {
    1053                 $member = new BP_Groups_Member( $invited_users[$i], $group_id );
     1058        // Get outstanding invites to the group from this user.
     1059        $invited_users = groups_get_invites_for_group( $inviter_id, $group_id );
     1060        $success = 0;
    10541061
    1055                 // Send the actual invite
    1056                 groups_notification_group_invites( $group, $member, $user_id );
     1062        foreach ( $invited_users as $invitee_id ) {
     1063                if ( groups_send_invite_by_invitee( $group_id, $invitee_id, $inviter_id, $resend = false ) ) {
     1064                        $success++;
     1065                }
     1066        }
    10571067
    1058                 $member->invite_sent = 1;
    1059                 $member->save();
     1068        // If all invitations were successfully sent, return success.
     1069        if ( count( $invited_users ) == $success ) {
     1070                return true;
     1071        } else {
     1072                return false;
    10601073        }
    10611074
    1062         do_action( 'groups_send_invites', $group_id, $invited_users );
     1075}
     1076
     1077/**
     1078 * Send specific pending group invitations keyed by group and invitee id
     1079 *
     1080 * @param int   $group_id       ID of the group.
     1081 * @param int   $invitee_id ID of the invited user.
     1082 * @param int   $inviter_id ID of the user who created the invitation.
     1083 * @param bool  $resend         Whether to resend notification email if already sent.
     1084 *
     1085 * @since BuddyPress (2.2.0)
     1086 */
     1087function groups_send_invite_by_invitee( $group_id, $invitee_id, $inviter_id, $resend = false ) {
     1088
     1089        if ( empty( $group_id ) || empty( $invitee_id ) || empty( $inviter_id ) ) {
     1090                return false;
     1091        }
     1092
     1093        // Check that an invitation exists for this user/group combination.
     1094        // $type = 'all' checks for sent or unsent invitations.
     1095        // @TODO: If #5911 is adopted, add $inviter_id argument.
     1096        if ( ! groups_check_user_has_invite( $invitee_id, $group_id, $type = 'all' ) ) {
     1097                return false;
     1098        }
     1099
     1100        $group = groups_get_group( array( 'group_id' => $group_id ) );
     1101        $member = new BP_Groups_Member( $invitee_id, $group_id );
     1102
     1103        // Send the invitation email.
     1104        $sent = groups_notification_group_invites( $group, $member, $inviter_id, $resend );
     1105        // Save the invite_sent property if applicable.
     1106        $saved = false;
     1107
     1108        if ( $sent ) {
     1109                if ( ! empty( $member->invite_sent ) ) {
     1110                        // Nothing to update
     1111                        $saved = true;
     1112                } else {
     1113                        $member->invite_sent = 1;
     1114                        $saved = $member->save();
     1115                }
     1116        }
     1117
     1118        do_action( 'groups_send_invites', $group_id, array( $invitee_id ) );
     1119
     1120        $success = ( $sent && $saved ) ? true : false;
     1121        return $success;
    10631122}
    10641123
    10651124/**
  • src/bp-groups/bp-groups-notifications.php

     
    354354 *
    355355 * @since BuddyPress (1.0.0)
    356356 *
    357  * @param BP_Groups_Group $group Group object.
    358  * @param BP_Groups_Member $member Member object.
    359  * @param int $inviter_user_id ID of the user who sent the invite.
    360  * @return bool|null False on failure.
     357 * @param       BP_Groups_Group  $group  Group object.
     358 * @param       BP_Groups_Member  $member  Member object.
     359 * @param       int  $inviter_user_id  ID of the user who sent the invite.
     360 * @param       bool  $resend  Whether to resend notification email if already sent.
     361 * @return      bool False on failure, true if success or nothing needed to be done.
    361362 */
    362 function groups_notification_group_invites( &$group, &$member, $inviter_user_id ) {
     363function groups_notification_group_invites( &$group, &$member, $inviter_user_id, $resend = false ) {
    363364
    364         // Bail if member has already been invited
    365         if ( ! empty( $member->invite_sent ) ) {
    366                 return;
     365        // Bail if the group and member objects are not populated.
     366        if ( empty( $group->id ) || empty( $member->id ) ) {
     367                return false;
     368        }
     369
     370        // Bail if the passed $inviter_user_id isn't the inviter as recorded in the BP_Groups_Member object.
     371        // @TODO: This will be resolved by #5911's change to groups_check_user_has_invite()
     372        if ( $inviter_user_id != $member->inviter_id ) {
     373                return false;
     374        }
     375
     376        // Bail if member has already been invited and $resend isn't specified.
     377        if ( ! $resend && ! empty( $member->invite_sent ) ) {
     378                return true;
    367379        }
    368380
    369381        // @todo $inviter_ud may be used for caching, test without it
    … …  
    421433        $subject = apply_filters_ref_array( 'groups_notification_group_invites_subject', array( $subject, &$group ) );
    422434        $message = apply_filters_ref_array( 'groups_notification_group_invites_message', array( $message, &$group, $inviter_name, $inviter_link, $invites_link, $group_link, $settings_link ) );
    423435
    424         wp_mail( $to, $subject, $message );
     436        $success = wp_mail( $to, $subject, $message );
    425437
    426438        do_action( 'bp_groups_sent_invited_email', $invited_user_id, $subject, $message, $group );
     439
     440        return $success;
    427441}
    428442
    429443/** Notifications *************************************************************/
  • tests/phpunit/testcases/groups/class-bp-groups-member.php

     
    1212                parent::tearDown();
    1313        }
    1414
    15         public static function invite_user_to_group( $user_id, $group_id, $inviter_id ) {
     15        public static function invite_user_to_group( $user_id, $group_id, $inviter_id, $draft = false ) {
    1616                $invite                = new BP_Groups_Member;
    1717                $invite->group_id      = $group_id;
    1818                $invite->user_id       = $user_id;
    1919                $invite->date_modified = bp_core_current_time();
    2020                $invite->inviter_id    = $inviter_id;
    2121                $invite->is_confirmed  = 0;
    22                 $invite->invite_sent   = 1;
     22                $invite->invite_sent   = $draft ? 0 : 1;
    2323
    2424                $invite->save();
    2525                return $invite->id;
    2626        }
    2727
     28        public static function create_group_membership_request( $user_id, $group_id ) {
     29                // Pending membership requests have inviter_id = 0, is_confirmed = 0
     30                $request                = new BP_Groups_Member;
     31                $request->group_id      = $group_id;
     32                $request->user_id       = $user_id;
     33                $request->date_modified = bp_core_current_time();
     34                $request->inviter_id    = 0;
     35                $request->is_confirmed  = 0;
     36                $request->is_admin  = 0;
     37
     38                $request->save();
     39                return $request->id;
     40        }
     41
    2842        public function test_get_recently_joined_with_filter() {
    2943                $g1 = $this->factory->group->create( array(
    3044                        'name' => 'Tab',
    … …  
    217231
    218232                $this->set_current_user( $old_current_user );
    219233        }
     234
     235        public function test_groups_send_invite_by_invitee() {
     236                $g1 = $this->factory->group->create();
     237
     238                $u1 = $this->factory->user->create();
     239                $u2 = $this->factory->user->create();
     240                $u3 = $this->factory->user->create();
     241                $u4 = $this->factory->user->create();
     242                self::add_user_to_group( $u1, $g1 );
     243
     244                // Test expected functionality
     245                self::invite_user_to_group( $u2, $g1, $u1, $draft = true );
     246                $this->assertTrue( is_null( groups_check_user_has_invite( $u2, $g1, $type = 'sent' ) ) );
     247
     248                $success = groups_send_invite_by_invitee( $g1, $u2, $u1 );
     249
     250                $this->assertTrue( $success );
     251                $this->assertTrue( is_numeric( groups_check_user_has_invite( $u2, $g1, $type = 'sent' ) ) );
     252                unset( $success );
     253
     254                // Test when user doesn't have invitation.
     255                $this->assertTrue( is_null( groups_check_user_has_invite( $u3, $g1, $type = 'sent' ) ) );
     256
     257                $success = groups_send_invite_by_invitee( $g1, $u3, $u1 );
     258
     259                $this->assertFalse( $success );
     260                unset( $success );
     261
     262                // Test when user has submitted a request for membership, but has not been invited
     263                self::create_group_membership_request( $u3, $g1 );
     264                $this->assertTrue( is_numeric( groups_check_for_membership_request( $u3, $g1 ) ) );
     265                $this->assertTrue( is_null( groups_check_user_has_invite( $u3, $g1, $type = 'all' ) ) );
     266
     267                $success = groups_send_invite_by_invitee( $g1, $u3, $u1 );
     268
     269                $this->assertFalse( $success );
     270                unset( $success );
     271
     272                // Test when user already belongs to the group
     273                self::add_user_to_group( $u4, $g1 );
     274
     275                $success = groups_send_invite_by_invitee( $g1, $u4, $u1 );
     276
     277                $this->assertFalse( $success );
     278                unset( $success );
     279
     280                // Test when user/group isn't specified
     281                $success = groups_send_invite_by_invitee( null, $u2, $u1 );
     282
     283                $this->assertFalse( $success );
     284                unset( $success );
     285
     286                $success = groups_send_invite_by_invitee( $g1, null, $u1 );
     287
     288                $this->assertFalse( $success );
     289                unset( $success );
     290
     291                // Test when user/group doesn't exist
     292                // Bad group ID
     293                $success = groups_send_invite_by_invitee( 457819810, $u2, $u1 );
     294
     295                $this->assertFalse( $success );
     296                unset( $success );
     297
     298                // Bad invitee ID
     299                $success = groups_send_invite_by_invitee( $g1, 47161302, $u1 );
     300
     301                $this->assertFalse( $success );
     302                unset( $success );
     303
     304                // Bad inviter ID
     305                $success = groups_send_invite_by_invitee( $g1, $u2, 472816989 );
     306
     307                $this->assertFalse( $success );
     308                unset( $success );
     309        }
     310
     311        public function test_groups_send_invites() {
     312                $g1 = $this->factory->group->create();
     313                $g1_obj = groups_get_group( array( 'group_id' => $g1 ) );
     314
     315                $u1 = $this->factory->user->create();
     316                $u2 = $this->factory->user->create();
     317                $u3 = $this->factory->user->create();
     318                $u4 = $this->factory->user->create();
     319                $u5 = $this->factory->user->create();
     320
     321                $old_current_user = get_current_user_id();
     322
     323                self::add_user_to_group( $u1, $g1 );
     324
     325                // Test expected functionality
     326                // Create some draft invitations that we can send
     327                self::invite_user_to_group( $u2, $g1, $u1, $draft = true );
     328                self::invite_user_to_group( $u3, $g1, $u1, $draft = true );
     329
     330                $success = groups_send_invites( $u1, $g1 );
     331
     332                $this->assertTrue( $success );
     333                unset( $success );
     334
     335                // User has no outstanding invites to send.
     336                self::add_user_to_group( $u4, $g1 );
     337                $success = groups_send_invites( $u4, $g1 );
     338
     339                $this->assertTrue( $success );
     340                unset( $success );
     341
     342                // User doesn't belong to group.
     343                $success = groups_send_invites( $u5, $g1 );
     344
     345                $this->assertFalse( $success );
     346                unset( $success );
     347
     348                // Test when user/group isn't specified
     349                // User isn't specified, but current user does have outstanding invites.
     350                $this->set_current_user( $u1 );
     351
     352                $success = groups_send_invites( null, $g1 );
     353
     354                $this->assertTrue( $success );
     355                unset( $success );
     356                $this->set_current_user( $old_current_user );
     357
     358                // Group isn't specified, we're not in a group context
     359                $success = groups_send_invites( $u1, null );
     360
     361                $this->assertFalse( $success );
     362                unset( $success );
     363
     364                // Group isn't specified, we are in a group context
     365                $this->go_to( bp_get_group_permalink( $g1_obj ) );
     366
     367                $success = groups_send_invites( $u1, null );
     368
     369                $this->assertTrue( $success );
     370                unset( $success );
     371
     372                // Test when user/group doesn't exist
     373                // Bad user ID
     374                $success = groups_send_invites( 569845135, $g1 );
     375
     376                $this->assertFalse( $success );
     377                unset( $success );
     378
     379                // Bad group ID
     380                $success = groups_send_invites( $u1, 586234125 );
     381
     382                $this->assertFalse( $success );
     383                unset( $success );
     384        }
    220385}
    221386