Skip to:
Content

BuddyPress.org

Ticket #6025: 6025.05.patch

File 6025.05.patch, 17.4 KB (added by dcavins, 11 years ago)

Refreshed patch

  • src/bp-groups/bp-groups-functions.php

    diff --git src/bp-groups/bp-groups-functions.php src/bp-groups/bp-groups-functions.php
    index 9cadd37..7534010 100644
    function groups_delete_invite( $user_id, $group_id ) {  
    12321232 */
    12331233function groups_send_invites( $user_id, $group_id ) {
    12341234
    1235         if ( empty( $user_id ) )
     1235        if ( empty( $user_id ) ) {
    12361236                $user_id = bp_loggedin_user_id();
     1237        }
     1238        if ( empty( $group_id ) ) {
     1239                $group_id = bp_get_current_group_id();
     1240        }
    12371241
    1238         // Send friend invites.
    1239         $invited_users = groups_get_invites_for_group( $user_id, $group_id );
    1240         $group = groups_get_group( array( 'group_id' => $group_id ) );
     1242        // If the inviter can't extend invitations to this group, bail.
     1243        if ( ! bp_groups_user_can_send_invites( $group_id, $user_id ) ) {
     1244                return false;
     1245        }
    12411246
    1242         for ( $i = 0, $count = count( $invited_users ); $i < $count; ++$i ) {
    1243                 $member = new BP_Groups_Member( $invited_users[$i], $group_id );
     1247        // Get outstanding invites to the group from this user.
     1248        $invited_users = groups_get_invites_for_group( $user_id, $group_id );
     1249        $success = 0;
    12441250
    1245                 // Send the actual invite
    1246                 groups_notification_group_invites( $group, $member, $user_id );
     1251        foreach ( $invited_users as $invitee_id ) {
     1252                if ( groups_send_single_invite( $group_id, $invitee_id, $user_id, $resend = false ) ) {
     1253                        $success++;
     1254                }
     1255        }
    12471256
    1248                 $member->invite_sent = 1;
    1249                 $member->save();
     1257    // If all invitations were successfully sent, return success.
     1258        if ( count( $invited_users ) == $success ) {
     1259                return true;
     1260        } else {
     1261                return false;
    12501262        }
    12511263
    12521264        /**
    … … function groups_send_invites( $user_id, $group_id ) {  
    12611273}
    12621274
    12631275/**
     1276 * Send specific pending group invitations keyed by group and invitee id
     1277 *
     1278 * @param int  $group_id   ID of the group.
     1279 * @param int  $invitee_id ID of the invited user.
     1280 * @param int  $inviter_id ID of the user who created the invitation.
     1281 * @param bool $resend     Whether to resend notification email if already sent.
     1282 *
     1283 * @since 2.4.0
     1284 */
     1285function groups_send_single_invite( $group_id, $invitee_id, $inviter_id, $resend = false ) {
     1286
     1287        if ( empty( $group_id ) || empty( $invitee_id ) || empty( $inviter_id ) ) {
     1288                return false;
     1289        }
     1290
     1291        // Check that an invitation exists for this user/group combination.
     1292        // $type = 'all' checks for sent or unsent invitations.
     1293        // @TODO: If #5911 is adopted, add $inviter_id argument.
     1294        if ( ! groups_check_user_has_invite( $invitee_id, $group_id, $type = 'all' ) ) {
     1295                return false;
     1296        }
     1297
     1298        $group = groups_get_group( array( 'group_id' => $group_id ) );
     1299        $member = new BP_Groups_Member( $invitee_id, $group_id );
     1300
     1301        // Send the invitation email.
     1302        $sent = groups_notification_group_invites( $group, $member, $inviter_id, $resend );
     1303        // Save the invite_sent property if applicable.
     1304        $saved = false;
     1305
     1306        if ( $sent ) {
     1307                if ( ! empty( $member->invite_sent ) ) {
     1308                        // Nothing to update
     1309                        $saved = true;
     1310                } else {
     1311                        $member->invite_sent = 1;
     1312                        $saved = $member->save();
     1313                }
     1314        }
     1315
     1316        /**
     1317         * Fires after the sending of a single group invite.
     1318         *
     1319         * @since 2.4.0
     1320         *
     1321         * @param int $group_id   ID of the group to which the invitee has been invited.
     1322         * @param int $invitee_id ID of the user being invited to the group.
     1323         * @param int $inviter_id ID of the user extending the invitation.
     1324         */
     1325        do_action( 'groups_send_single_invite', $group_id, $invitee_id, $inviter_id );
     1326
     1327        $success = ( $sent && $saved ) ? true : false;
     1328        return $success;
     1329 }
     1330
     1331/**
    12641332 * Get IDs of users with outstanding invites to a given group from a specified user.
    12651333 *
    12661334 * @param int $user_id  ID of the inviting user.
  • src/bp-groups/bp-groups-notifications.php

    diff --git src/bp-groups/bp-groups-notifications.php src/bp-groups/bp-groups-notifications.php
    index 09244b4..69c293f 100644
    add_action( 'groups_promoted_member', 'groups_notification_promoted_member', 10,  
    510510 * @param BP_Groups_Group  $group           Group object.
    511511 * @param BP_Groups_Member $member          Member object.
    512512 * @param int              $inviter_user_id ID of the user who sent the invite.
     513 * @param bool             $resend          Whether to resend notification email if already sent.
    513514 *
    514  * @return null|false False on failure.
     515 * @return bool False on failure, true if success or nothing needed to be done.
    515516 */
    516 function groups_notification_group_invites( &$group, &$member, $inviter_user_id ) {
     517function groups_notification_group_invites( &$group, &$member, $inviter_user_id, $resend = false ) {
    517518
    518         // Bail if member has already been invited
    519         if ( ! empty( $member->invite_sent ) ) {
    520                 return;
     519        // Bail if the group and member objects are not populated.
     520        if ( empty( $group->id ) || empty( $member->id ) ) {
     521                return false;
     522        }
     523
     524        // Bail if the passed $inviter_user_id isn't the inviter as recorded in the BP_Groups_Member object.
     525        // @TODO: This will be resolved by #5911's change to groups_check_user_has_invite()
     526        if ( $inviter_user_id != $member->inviter_id ) {
     527                return false;
    521528        }
    522529
     530        // Bail if member has already been invited and $resend isn't specified.
     531        if ( ! $resend && ! empty( $member->invite_sent ) ) {
     532                return true;
     533        }
     534
    523535        // @todo $inviter_ud may be used for caching, test without it
    524536        $inviter_ud   = bp_core_get_core_userdata( $inviter_user_id );
    525537        $inviter_name = bp_core_get_userlink( $inviter_user_id, true, false, true );
    … … To view %5$s\'s profile visit: %6$s  
    604616         */
    605617        $message = apply_filters_ref_array( 'groups_notification_group_invites_message', array( $message, &$group, $inviter_name, $inviter_link, $invites_link, $group_link, $settings_link ) );
    606618
    607         wp_mail( $to, $subject, $message );
     619        $success = wp_mail( $to, $subject, $message );
    608620
    609621        /**
    610622         * Fires after the notification is sent that a member has been invited to a group.
    … … To view %5$s\'s profile visit: %6$s  
    617629         * @param BP_Groups_Group $group            Group object.
    618630         */
    619631        do_action( 'bp_groups_sent_invited_email', $invited_user_id, $subject, $message, $group );
     632
     633        return $success;
    620634}
    621635
    622636/** Notifications *************************************************************/
  • tests/phpunit/testcases/groups/class-bp-groups-member.php

    diff --git tests/phpunit/testcases/groups/class-bp-groups-member.php tests/phpunit/testcases/groups/class-bp-groups-member.php
    index 062694d..0ed30c7 100644
     
    44 * @group BP_Groups_Member
    55 */
    66class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase {
    7         public static function invite_user_to_group( $user_id, $group_id, $inviter_id ) {
     7        public static function invite_user_to_group( $user_id, $group_id, $inviter_id, $draft = false ) {
    88                $invite                = new BP_Groups_Member;
    99                $invite->group_id      = $group_id;
    1010                $invite->user_id       = $user_id;
    1111                $invite->date_modified = bp_core_current_time();
    1212                $invite->inviter_id    = $inviter_id;
    1313                $invite->is_confirmed  = 0;
    14                 $invite->invite_sent   = 1;
     14                $invite->invite_sent   = $draft ? 0 : 1;
    1515
    1616                $invite->save();
    1717                return $invite->id;
    … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase {  
    11961196                $members = new BP_Group_Member_Query( array( 'group_id' => $g1 ) );
    11971197                $this->assertEqualSets( array( $u1, $u2, $u3 ), $members->user_ids );
    11981198        }
     1199
     1200        /**
     1201         * @group groups_send_single_invite
     1202         * @group group_invitations
     1203         * @group group_membership
     1204         */
     1205        public function test_groups_send_single_invite() {
     1206                $g1 = $this->factory->group->create();
     1207                $u1 = $this->factory->user->create();
     1208                $u2 = $this->factory->user->create();
     1209                self::add_user_to_group( $u1, $g1 );
     1210
     1211                // Test expected functionality
     1212                self::invite_user_to_group( $u2, $g1, $u1, $draft = true );
     1213                $this->assertTrue( is_null( groups_check_user_has_invite( $u2, $g1, $type = 'sent' ) ) );
     1214
     1215                $success = groups_send_single_invite( $g1, $u2, $u1 );
     1216
     1217                $this->assertTrue( $success );
     1218                $this->assertTrue( is_numeric( groups_check_user_has_invite( $u2, $g1, $type = 'sent' ) ) );
     1219                unset( $success );
     1220
     1221        }
     1222
     1223        /**
     1224         * @group groups_send_single_invite
     1225         * @group group_invitations
     1226         * @group group_membership
     1227         */
     1228        public function test_groups_send_single_invite_no_invite() {
     1229                $g1 = $this->factory->group->create();
     1230                $u1 = $this->factory->user->create();
     1231                $u2 = $this->factory->user->create();
     1232                self::add_user_to_group( $u1, $g1 );
     1233
     1234                // Test when user doesn't have invitation.
     1235                $this->assertTrue( is_null( groups_check_user_has_invite( $u2, $g1, $type = 'sent' ) ) );
     1236
     1237                $success = groups_send_single_invite( $g1, $u2, $u1 );
     1238
     1239                $this->assertFalse( $success );
     1240                unset( $success );
     1241        }
     1242
     1243        /**
     1244         * @group groups_send_single_invite
     1245         * @group group_invitations
     1246         * @group group_membership
     1247         */
     1248        public function test_groups_send_single_invite_has_request() {
     1249                $g1 = $this->factory->group->create();
     1250                $u1 = $this->factory->user->create();
     1251                $u2 = $this->factory->user->create();
     1252                self::add_user_to_group( $u1, $g1 );
     1253
     1254                // Test when user has submitted a request for membership, but has not been invited
     1255                self::create_group_membership_request( $u2, $g1 );
     1256                $this->assertTrue( is_numeric( groups_check_for_membership_request( $u2, $g1 ) ) );
     1257                $this->assertTrue( is_null( groups_check_user_has_invite( $u2, $g1, $type = 'all' ) ) );
     1258
     1259                $success = groups_send_single_invite( $g1, $u2, $u1 );
     1260
     1261                $this->assertFalse( $success );
     1262                unset( $success );
     1263        }
     1264
     1265        /**
     1266         * @group groups_send_single_invite
     1267         * @group group_invitations
     1268         * @group group_membership
     1269         */
     1270        public function test_groups_send_single_invite_already_member() {
     1271                $g1 = $this->factory->group->create();
     1272                $u1 = $this->factory->user->create();
     1273                $u2 = $this->factory->user->create();
     1274                self::add_user_to_group( $u1, $g1 );
     1275
     1276                // Test when user already belongs to the group
     1277                self::add_user_to_group( $u2, $g1 );
     1278                $success = groups_send_single_invite( $g1, $u2, $u1 );
     1279
     1280                $this->assertFalse( $success );
     1281                unset( $success );
     1282        }
     1283
     1284        /**
     1285         * @group groups_send_single_invite
     1286         * @group group_invitations
     1287         * @group group_membership
     1288         */
     1289        public function test_groups_send_single_invite_no_group() {
     1290                $g1 = $this->factory->group->create();
     1291                $u1 = $this->factory->user->create();
     1292                $u2 = $this->factory->user->create();
     1293                self::add_user_to_group( $u1, $g1 );
     1294
     1295                // Test when group isn't specified
     1296                $success = groups_send_single_invite( null, $u2, $u1 );
     1297
     1298                $this->assertFalse( $success );
     1299                unset( $success );
     1300        }
     1301
     1302        /**
     1303         * @group groups_send_single_invite
     1304         * @group group_invitations
     1305         * @group group_membership
     1306         */
     1307        public function test_groups_send_single_invite_user_empty() {
     1308                $g1 = $this->factory->group->create();
     1309                $u1 = $this->factory->user->create();
     1310                self::add_user_to_group( $u1, $g1 );
     1311
     1312                // Test when user isn't specified
     1313                $success = groups_send_single_invite( $g1, null, $u1 );
     1314
     1315                $this->assertFalse( $success );
     1316                unset( $success );
     1317        }
     1318
     1319        /**
     1320         * @group groups_send_single_invite
     1321         * @group group_invitations
     1322         * @group group_membership
     1323         */
     1324        public function test_groups_send_single_invite_bad_group_param() {
     1325                $g1 = $this->factory->group->create();
     1326                $u1 = $this->factory->user->create();
     1327                $u2 = $this->factory->user->create();
     1328                self::add_user_to_group( $u1, $g1 );
     1329
     1330                // Test when group doesn't exist: Bad group ID
     1331                $success = groups_send_single_invite( 457819810, $u2, $u1 );
     1332
     1333                $this->assertFalse( $success );
     1334                unset( $success );
     1335        }
     1336
     1337        /**
     1338         * @group groups_send_single_invite
     1339         * @group group_invitations
     1340         * @group group_membership
     1341         */
     1342        public function test_groups_send_single_invite_bad_invitee_id() {
     1343                $g1 = $this->factory->group->create();
     1344                $u1 = $this->factory->user->create();
     1345                self::add_user_to_group( $u1, $g1 );
     1346
     1347                // Test when group doesn't exist: Bad invitee ID
     1348                $success = groups_send_single_invite( $g1, 47161302, $u1 );
     1349
     1350                $this->assertFalse( $success );
     1351                unset( $success );
     1352        }
     1353
     1354        /**
     1355         * @group groups_send_single_invite
     1356         * @group group_invitations
     1357         * @group group_membership
     1358         */
     1359        public function test_groups_send_single_invite_bad_inviter_id() {
     1360                $g1 = $this->factory->group->create();
     1361                $u1 = $this->factory->user->create();
     1362                $u2 = $this->factory->user->create();
     1363                self::add_user_to_group( $u1, $g1 );
     1364
     1365                // Bad inviter ID
     1366                $success = groups_send_single_invite( $g1, $u2, 472816989 );
     1367
     1368                $this->assertFalse( $success );
     1369                unset( $success );
     1370        }
     1371
     1372        /**
     1373         * @group groups_send_invites
     1374         * @group group_invitations
     1375         * @group group_membership
     1376         */
     1377        public function test_groups_send_invites_expected_behavior() {
     1378                $g1 = $this->factory->group->create();
     1379                $u1 = $this->factory->user->create();
     1380                $u2 = $this->factory->user->create();
     1381                $u3 = $this->factory->user->create();
     1382                self::add_user_to_group( $u1, $g1 );
     1383
     1384                // Test expected functionality
     1385                // Create some draft invitations that we can send
     1386                self::invite_user_to_group( $u2, $g1, $u1, $draft = true );
     1387                self::invite_user_to_group( $u3, $g1, $u1, $draft = true );
     1388
     1389                $success = groups_send_invites( $u1, $g1 );
     1390
     1391                $this->assertTrue( $success );
     1392        }
     1393
     1394        /**
     1395         * @group groups_send_invites
     1396         * @group group_invitations
     1397         * @group group_membership
     1398         */
     1399        public function test_groups_send_invites_none_to_send() {
     1400                $g1 = $this->factory->group->create();
     1401                $u1 = $this->factory->user->create();
     1402                self::add_user_to_group( $u1, $g1 );
     1403
     1404                // User has no outstanding invites to send.
     1405                $success = groups_send_invites( $u1, $g1 );
     1406
     1407                $this->assertTrue( $success );
     1408        }
     1409
     1410        /**
     1411         * @group groups_send_invites
     1412         * @group group_invitations
     1413         * @group group_membership
     1414         */
     1415        public function test_groups_send_invites_not_a_group_member() {
     1416                $g1 = $this->factory->group->create();
     1417                $u1 = $this->factory->user->create();
     1418
     1419                // User doesn't belong to group.
     1420                $success = groups_send_invites( $u1, $g1 );
     1421
     1422                $this->assertFalse( $success );
     1423        }
     1424
     1425        /**
     1426         * @group groups_send_invites
     1427         * @group group_invitations
     1428         * @group group_membership
     1429         */
     1430        public function test_groups_send_invites_use_current_user() {
     1431                $g1 = $this->factory->group->create();
     1432                $u1 = $this->factory->user->create();
     1433                $u2 = $this->factory->user->create();
     1434                $u3 = $this->factory->user->create();
     1435                $old_current_user = get_current_user_id();
     1436                self::add_user_to_group( $u1, $g1 );
     1437
     1438                // Create some draft invitations that we can send
     1439                self::invite_user_to_group( $u2, $g1, $u1, $draft = true );
     1440                self::invite_user_to_group( $u3, $g1, $u1, $draft = true );
     1441
     1442                // Test when user/group isn't specified
     1443                // User isn't specified, but current user does have outstanding invites.
     1444                $this->set_current_user( $u1 );
     1445
     1446                $success = groups_send_invites( null, $g1 );
     1447
     1448                $this->assertTrue( $success );
     1449                $this->set_current_user( $old_current_user );
     1450        }
     1451
     1452        /**
     1453         * @group groups_send_invites
     1454         * @group group_invitations
     1455         * @group group_membership
     1456         */
     1457        public function test_groups_send_invites_empty_group() {
     1458                $g1 = $this->factory->group->create();
     1459                $u1 = $this->factory->user->create();
     1460                $u2 = $this->factory->user->create();
     1461                $u3 = $this->factory->user->create();
     1462                self::add_user_to_group( $u1, $g1 );
     1463
     1464                // Create some draft invitations that we can send
     1465                self::invite_user_to_group( $u2, $g1, $u1, $draft = true );
     1466                self::invite_user_to_group( $u3, $g1, $u1, $draft = true );
     1467
     1468                // Group isn't specified, we're not in a group context
     1469                $success = groups_send_invites( $u1, null );
     1470
     1471                $this->assertFalse( $success );
     1472        }
     1473
     1474        /**
     1475         * @group groups_send_invites
     1476         * @group group_invitations
     1477         * @group group_membership
     1478         */
     1479        public function test_groups_send_invites_empty_group_in_group() {
     1480                $g1 = $this->factory->group->create();
     1481                $g1_obj = groups_get_group( array( 'group_id' => $g1 ) );
     1482                $u1 = $this->factory->user->create();
     1483                $u2 = $this->factory->user->create();
     1484                $u3 = $this->factory->user->create();
     1485                self::add_user_to_group( $u1, $g1 );
     1486
     1487                // Create some draft invitations that we can send
     1488                self::invite_user_to_group( $u2, $g1, $u1, $draft = true );
     1489                self::invite_user_to_group( $u3, $g1, $u1, $draft = true );
     1490
     1491                // Group isn't specified, we are in a group context
     1492                $this->go_to( bp_get_group_permalink( $g1_obj ) );
     1493
     1494                $success = groups_send_invites( $u1, null );
     1495
     1496                $this->assertTrue( $success );
     1497        }
     1498
     1499        /**
     1500         * @group groups_send_invites
     1501         * @group group_invitations
     1502         * @group group_membership
     1503         */
     1504        public function test_groups_send_invites_bad_user_param() {
     1505                $g1 = $this->factory->group->create();
     1506                $u1 = $this->factory->user->create();
     1507                $u2 = $this->factory->user->create();
     1508                $u3 = $this->factory->user->create();
     1509                self::add_user_to_group( $u1, $g1 );
     1510
     1511                // Create some draft invitations that we can send
     1512                self::invite_user_to_group( $u2, $g1, $u1, $draft = true );
     1513                self::invite_user_to_group( $u3, $g1, $u1, $draft = true );
     1514
     1515                // Test when user/group doesn't exist: Bad user ID
     1516                $success = groups_send_invites( 569845135, $g1 );
     1517
     1518                $this->assertFalse( $success );
     1519        }
     1520
     1521        /**
     1522         * @group groups_send_invites
     1523         * @group group_invitations
     1524         * @group group_membership
     1525         */
     1526        public function test_groups_send_invites_bad_group_param() {
     1527                $g1 = $this->factory->group->create();
     1528                $u1 = $this->factory->user->create();
     1529                $u2 = $this->factory->user->create();
     1530                $u3 = $this->factory->user->create();
     1531                self::add_user_to_group( $u1, $g1 );
     1532
     1533                // Create some draft invitations that we can send
     1534                self::invite_user_to_group( $u2, $g1, $u1, $draft = true );
     1535                self::invite_user_to_group( $u3, $g1, $u1, $draft = true );
     1536
     1537                // Bad group ID
     1538                $success = groups_send_invites( $u1, 586234125 );
     1539
     1540                $this->assertFalse( $success );
     1541        }
    11991542}