Skip to:
Content

BuddyPress.org

Changeset 12429


Ignore:
Timestamp:
08/12/2019 08:42:49 PM (12 months ago)
Author:
dcavins
Message:

Introduce BP_Groups_Invitation_Manager.

  • Add BP_Groups_Invitation_Manager, a new class that extends BP_Invitation_Manager for the purpose of using the new API to manage group invitations and requests.
  • Migrate group invitations to the new table on update, via bp_groups_migrate_invitations().
  • Update group invitation function logic to make use of BP_Groups_Invitation_Manager.

See #6210.

Location:
trunk
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-update.php

    r12428 r12429  
    554554 * - Make sure the custom visibility is disabled for the default profile field.
    555555 * - Create the invitations table.
     556 * - Migrate requests and invitations to the new table.
    556557 *
    557558 * @since 5.0.0
     
    584585
    585586    bp_core_install_invitations();
     587
     588    if ( bp_is_active( 'groups' ) ) {
     589        bp_groups_migrate_invitations();
     590    }
    586591}
    587592
  • trunk/src/bp-groups/bp-groups-cache.php

    r12273 r12429  
    256256
    257257/**
     258 * Clear caches on saving a group invitation or request.
     259 * The save action is called when inserting a new record or using the save() method
     260 * to update an existing record.
     261 *
     262 * @since 5.0.0
     263 *
     264 * @param BP_Invitation object $invitation Characteristics of the invitation just saved.
     265 */
     266function bp_groups_clear_user_group_cache_on_invitation_save( BP_Invitation $invitation ) {
     267    if ( sanitize_key( 'BP_Groups_Invitation_Manager' ) !== $invitation->class ) {
     268        return;
     269    }
     270
     271    wp_cache_delete( $invitation->id, 'bp_groups_invitations_as_memberships' );
     272}
     273add_action( 'bp_invitation_after_save', 'bp_groups_clear_user_group_cache_on_invitation_save', 10, 2 );
     274
     275/**
     276 * Clear caches on invitation deletion or update.
     277 * This also catches changes like sending an invite or marking one as accepted.
     278 *
     279 * @since 5.0.0
     280 *
     281 * @param array $args Associative array of columns/values describing invitations about to be deleted.
     282 */
     283function bp_groups_clear_user_group_cache_on_invitation_change( $args ) {
     284    $args['fields' ] = 'ids';
     285    $affected_invitation_ids = groups_get_invites( $args );
     286    foreach ( $affected_invitation_ids as $invitation_id ) {
     287        wp_cache_delete( $invitation_id, 'bp_groups_invitations_as_memberships' );
     288    }
     289}
     290add_action( 'bp_invitation_before_delete', 'bp_groups_clear_user_group_cache_on_invitation_change' );
     291add_action( 'bp_invitation_before_update', 'bp_groups_clear_user_group_cache_on_invitation_change' );
     292
     293/**
    258294 * Clear group memberships cache on miscellaneous actions not covered by the 'after_save' hook.
    259295 *
     
    270306}
    271307add_action( 'bp_groups_member_before_delete', 'bp_groups_clear_user_group_cache_on_other_events', 10, 2 );
    272 add_action( 'bp_groups_member_before_delete_invite', 'bp_groups_clear_user_group_cache_on_other_events', 10, 2 );
    273 add_action( 'groups_accept_invite', 'bp_groups_clear_user_group_cache_on_other_events', 10, 2 );
    274308
    275309/**
  • trunk/src/bp-groups/bp-groups-functions.php

    r12426 r12429  
    952952    $user_id = intval( $user_id );
    953953
     954    // Standard memberships
    954955    $membership_ids = wp_cache_get( $user_id, 'bp_groups_memberships_for_user' );
    955956    if ( false === $membership_ids ) {
     
    967968        }
    968969    }
     970
     971    // Prime the invitations- and requests-as-memberships cache
     972    $invitation_ids = array();
     973    if ( true !== $r['is_confirmed'] || false !== $r['invite_sent'] ) {
     974        $invitation_ids = groups_get_invites( array(
     975            'user_id'     => $user_id,
     976            'invite_sent' => 'all',
     977            'type'        => 'all',
     978            'fields'      => 'ids'
     979        ) );
     980
     981        // Prime the invitations cache.
     982        $uncached_invitation_ids = bp_get_non_cached_ids( $invitation_ids, 'bp_groups_invitations_as_memberships' );
     983
     984        $uncached_invitations = groups_get_invites( array(
     985            'ids'         => $uncached_invitation_ids,
     986            'invite_sent' => 'all',
     987            'type'        => 'all'
     988        ) );
     989        foreach ( $uncached_invitations as $uncached_invitation ) {
     990            // Reshape the result as a membership db entry.
     991            $invitation = new StdClass;
     992            $invitation->id            = $uncached_invitation->id;
     993            $invitation->group_id      = $uncached_invitation->item_id;
     994            $invitation->user_id       = $uncached_invitation->user_id;
     995            $invitation->inviter_id    = $uncached_invitation->inviter_id;
     996            $invitation->is_admin      = false;
     997            $invitation->is_mod        = false;
     998            $invitation->user_title    = '';
     999            $invitation->date_modified = $uncached_invitation->date_modified;
     1000            $invitation->comments      = $uncached_invitation->content;
     1001            $invitation->is_confirmed  = false;
     1002            $invitation->is_banned     = false;
     1003            $invitation->invite_sent   = $uncached_invitation->invite_sent;
     1004            wp_cache_set( $uncached_invitation->id, $invitation, 'bp_groups_invitations_as_memberships' );
     1005        }
     1006
     1007    }
     1008
    9691009
    9701010    // Assemble filter array for use in `wp_list_filter()`.
     
    10071047
    10081048        $groups[ $group_id ] = $membership;
     1049    }
     1050
     1051    // Populate group invitations array from cache, and normalize.
     1052    foreach ( $invitation_ids as $invitation_id ) {
     1053        $invitation = wp_cache_get( $invitation_id, 'bp_groups_invitations_as_memberships' );
     1054
     1055        // Sanity check.
     1056        if ( ! isset( $invitation->group_id ) ) {
     1057            continue;
     1058        }
     1059
     1060        // Integer values.
     1061        foreach ( $int_keys as $index ) {
     1062            $invitation->{$index} = intval( $invitation->{$index} );
     1063        }
     1064
     1065        // Boolean values.
     1066        foreach ( $bool_keys as $index ) {
     1067            $invitation->{$index} = (bool) $invitation->{$index};
     1068        }
     1069
     1070        foreach ( $filters as $filter_name => $filter_value ) {
     1071            if ( ! isset( $invitation->{$filter_name} ) || $filter_value != $invitation->{$filter_name} ) {
     1072                continue 2;
     1073            }
     1074        }
     1075
     1076        $group_id = (int) $invitation->group_id;
     1077
     1078        $groups[ $group_id ] = $invitation;
    10091079    }
    10101080
     
    12561326 *
    12571327 * @since 2.6.0
    1258  *
    1259  * @param int $user_id ID of the user.
    1260  * @param int $group_id ID of the group.
     1328 * @since 5.0.0 Added $type parameter.
     1329 *
     1330 * @param int    $user_id  ID of the user.
     1331 * @param int    $group_id ID of the group.
     1332 * @param string $type     If 'sent', results are limited to those invitations
     1333 *                         that have actually been sent (non-draft).
     1334 *                         Possible values: 'sent', 'draft', or 'all' Default: 'sent'.
    12611335 * @return int|bool ID of the membership if the user is invited, otherwise false.
    12621336 */
    1263 function groups_is_user_invited( $user_id, $group_id ) {
    1264     $is_invited = false;
    1265 
    1266     $user_groups = bp_get_user_groups( $user_id, array(
    1267         'invite_sent' => true,
    1268         'is_confirmed' => false,
    1269     ) );
    1270 
    1271     if ( isset( $user_groups[ $group_id ] ) ) {
    1272         $is_invited = $user_groups[ $group_id ]->id;
    1273     }
    1274 
    1275     return $is_invited;
     1337function groups_is_user_invited( $user_id, $group_id, $type = 'sent' ) {
     1338    return groups_check_has_invite_from_user( $user_id, $group_id, false, $type );
    12761339}
    12771340
     
    12861349 */
    12871350function groups_is_user_pending( $user_id, $group_id ) {
    1288     $is_pending = false;
    1289 
    1290     $user_groups = bp_get_user_groups( $user_id, array(
    1291         'invite_sent' => false,
    1292         'is_confirmed' => false,
    1293     ) );
    1294 
    1295     if ( isset( $user_groups[ $group_id ] ) ) {
    1296         $is_pending = $user_groups[ $group_id ]->id;
    1297     }
    1298 
    1299     return $is_pending;
     1351    if ( empty( $user_id ) || empty( $group_id ) ) {
     1352        return false;
     1353    }
     1354
     1355    $args = array(
     1356        'user_id'     => $user_id,
     1357        'item_id'     => $group_id,
     1358    );
     1359    $invites_class = new BP_Groups_Invitation_Manager();
     1360
     1361    return $invites_class->request_exists( $args );
    13001362}
    13011363
     
    13161378
    13171379/**
    1318  * Get IDs of users with outstanding invites to a given group from a specified user.
    1319  *
    1320  * @since 1.0.0
    1321  *
    1322  * @param int               $user_id ID of the inviting user.
     1380 * Get group objects for groups that a user is currently invited to.
     1381 *
     1382 * @since 1.0.0
     1383 *
     1384 * @param int               $user_id ID of the invited user.
    13231385 * @param int|bool          $limit   Limit to restrict to.
    13241386 * @param int|bool          $page    Optional. Page offset of results to return.
    13251387 * @param string|array|bool $exclude Array of comma-separated list of group IDs
    13261388 *                                   to exclude from results.
    1327  * @return array $value IDs of users who have been invited to the group by the
    1328  *                      user but have not yet accepted.
     1389 * @return array {
     1390 *     @type array $groups Array of groups returned by paginated query.
     1391 *     @type int   $total  Count of groups matching query.
     1392 * }
    13291393 */
    13301394function groups_get_invites_for_user( $user_id = 0, $limit = false, $page = false, $exclude = false ) {
    1331 
    1332     if ( empty( $user_id ) )
     1395    if ( empty( $user_id ) ) {
    13331396        $user_id = bp_loggedin_user_id();
    1334 
    1335     return BP_Groups_Member::get_invites( $user_id, $limit, $page, $exclude );
     1397    }
     1398
     1399    $group_ids = groups_get_invited_to_group_ids( $user_id );
     1400
     1401    // Remove excluded groups.
     1402    if ( $exclude ) {
     1403        $group_ids = array_diff( $group_ids, wp_parse_id_list( $exclude ) );
     1404    }
     1405
     1406    // Avoid passing an empty array.
     1407    if ( ! $group_ids ) {
     1408        $group_ids = array( 0 );
     1409    }
     1410
     1411    // Get a filtered list of groups.
     1412    $args = array(
     1413        'include'     => $group_ids,
     1414        'show_hidden' => true,
     1415        'per_page'    => $limit,
     1416        'page'        => $page,
     1417    );
     1418    $groups = groups_get_groups( $args );
     1419
     1420    return array( 'groups' => $groups['groups'], 'total' => groups_get_invite_count_for_user( $user_id ) );
    13361421}
    13371422
     
    13491434    }
    13501435
    1351     return BP_Groups_Member::get_invite_count_for_user( $user_id );
     1436    return count( groups_get_invited_to_group_ids( $user_id ) );
     1437}
     1438
     1439/**
     1440 * Get an array of group IDs to which a user is invited.
     1441 *
     1442 * @since 5.0.0
     1443 *
     1444 * @param int $user_id The user ID.
     1445 *
     1446 * @return array Array of group IDs.
     1447 */
     1448 function groups_get_invited_to_group_ids( $user_id = 0 ) {
     1449    if ( empty( $user_id ) ) {
     1450        $user_id = bp_loggedin_user_id();
     1451    }
     1452
     1453    $group_ids = groups_get_invites( array(
     1454        'user_id'     => $user_id,
     1455        'invite_sent' => 'sent',
     1456        'fields'      => 'item_ids'
     1457    ) );
     1458
     1459    return array_unique( $group_ids );
    13521460}
    13531461
     
    13651473 *     @type string $date_modified Optional. Modified date for the invitation.
    13661474 *                                 Default: current date/time.
    1367  *     @type bool   $is_confirmed  Optional. Whether the invitation should be
    1368  *                                 marked confirmed. Default: false.
     1475 *     @type string $content       Optional. Message to invitee.
     1476 *     @type bool   $send_invite   Optional. Whether the invitation should be
     1477 *                                 sent now. Default: false.
    13691478 * }
    13701479 * @return bool True on success, false on failure.
     
    13721481function groups_invite_user( $args = '' ) {
    13731482
    1374     $args = bp_parse_args( $args, array(
     1483    $r = bp_parse_args( $args, array(
    13751484        'user_id'       => false,
    13761485        'group_id'      => false,
    13771486        'inviter_id'    => bp_loggedin_user_id(),
    13781487        'date_modified' => bp_core_current_time(),
    1379         'is_confirmed'  => 0
     1488        'content'       => '',
     1489        'send_invite'   => 0
    13801490    ), 'groups_invite_user' );
    1381     extract( $args, EXTR_SKIP );
    1382 
    1383     if ( ! $user_id || ! $group_id || ! $inviter_id ) {
     1491
     1492    $inv_args = array(
     1493        'user_id'       => $r['user_id'],
     1494        'item_id'       => $r['group_id'],
     1495        'inviter_id'    => $r['inviter_id'],
     1496        'date_modified' => $r['date_modified'],
     1497        'content'       => $r['content'],
     1498        'send_invite'   => $r['send_invite']
     1499    );
     1500
     1501    // Create the unsent invitataion.
     1502    $invites_class = new BP_Groups_Invitation_Manager();
     1503    $created       = $invites_class->add_invitation( $inv_args );
     1504
     1505    /**
     1506     * Fires after the creation of a new group invite.
     1507     *
     1508     * @since 1.0.0
     1509     *
     1510     * @param array    $r       Array of parsed arguments for the group invite.
     1511     * @param int|bool $created The ID of the invitation or false if it couldn't be created.
     1512     */
     1513    do_action( 'groups_invite_user', $r, $created );
     1514
     1515    return $created;
     1516}
     1517
     1518/**
     1519 * Uninvite a user from a group.
     1520 *
     1521 * @since 1.0.0
     1522 *
     1523 * @param int $user_id  ID of the user.
     1524 * @param int $group_id ID of the group.
     1525 * @param int $inviter_id ID of the inviter.
     1526 * @return bool True on success, false on failure.
     1527 */
     1528function groups_uninvite_user( $user_id, $group_id, $inviter_id = false ) {
     1529    if ( empty( $user_id ) || empty( $group_id ) ) {
    13841530        return false;
    13851531    }
    13861532
    1387     // If the user has already requested membership, accept the request.
    1388     if ( $membership_id = groups_check_for_membership_request( $user_id, $group_id ) ) {
    1389         groups_accept_membership_request( $membership_id, $user_id, $group_id );
    1390 
    1391     // Otherwise, create a new invitation.
    1392     } elseif ( ! groups_is_user_member( $user_id, $group_id ) && ! groups_check_user_has_invite( $user_id, $group_id, 'all' ) ) {
    1393         $invite                = new BP_Groups_Member;
    1394         $invite->group_id      = $group_id;
    1395         $invite->user_id       = $user_id;
    1396         $invite->date_modified = $date_modified;
    1397         $invite->inviter_id    = $inviter_id;
    1398         $invite->is_confirmed  = $is_confirmed;
    1399 
    1400         if ( !$invite->save() )
    1401             return false;
    1402 
     1533    $invites_class = new BP_Groups_Invitation_Manager();
     1534    $success       = $invites_class->delete( array(
     1535        'user_id'    => $user_id,
     1536        'item_id'    => $group_id,
     1537        'inviter_id' => $inviter_id,
     1538    ) );
     1539
     1540    if ( $success ) {
    14031541        /**
    1404          * Fires after the creation of a new group invite.
     1542         * Fires after uninviting a user from a group.
    14051543         *
    14061544         * @since 1.0.0
     1545         * @since 2.7.0 Added $inviter_id parameter
    14071546         *
    1408          * @param array $args Array of parsed arguments for the group invite.
     1547         * @param int $group_id    ID of the group being uninvited from.
     1548         * @param int $user_id     ID of the user being uninvited.
     1549         * @param int $inviter_id  ID of the inviter.
    14091550         */
    1410         do_action( 'groups_invite_user', $args );
    1411     }
    1412 
    1413     return true;
    1414 }
    1415 
    1416 /**
    1417  * Uninvite a user from a group.
    1418  *
    1419  * Functionally, this is equivalent to removing a user from a group.
    1420  *
    1421  * @since 1.0.0
    1422  *
    1423  * @param int $user_id  ID of the user.
    1424  * @param int $group_id ID of the group.
    1425  * @return bool True on success, false on failure.
    1426  */
    1427 function groups_uninvite_user( $user_id, $group_id ) {
    1428 
    1429     if ( ! BP_Groups_Member::delete_invite( $user_id, $group_id ) )
    1430         return false;
    1431 
    1432     /**
    1433      * Fires after uninviting a user from a group.
    1434      *
    1435      * @since 1.0.0
    1436      *
    1437      * @param int $group_id ID of the group being uninvited from.
    1438      * @param int $user_id  ID of the user being uninvited.
    1439      */
    1440     do_action( 'groups_uninvite_user', $group_id, $user_id );
    1441 
    1442     return true;
     1551        do_action( 'groups_uninvite_user', $group_id, $user_id, $inviter_id );
     1552    }
     1553
     1554    return $success;
    14431555}
    14441556
     
    14551567 */
    14561568function groups_accept_invite( $user_id, $group_id ) {
    1457 
    1458     // If the user is already a member (because BP at one point allowed two invitations to
    1459     // slip through), delete all existing invitations/requests and return true.
    1460     if ( groups_is_user_member( $user_id, $group_id ) ) {
    1461         if ( groups_check_user_has_invite( $user_id, $group_id ) ) {
    1462             groups_delete_invite( $user_id, $group_id );
    1463         }
    1464 
    1465         if ( groups_check_for_membership_request( $user_id, $group_id ) ) {
    1466             groups_delete_membership_request( null, $user_id, $group_id );
    1467         }
    1468 
    1469         return true;
    1470     }
    1471 
    1472     $member = new BP_Groups_Member( $user_id, $group_id );
    1473 
    1474     // Save the inviter ID so that we can pass it to the action below.
    1475     $inviter_id = $member->inviter_id;
    1476 
    1477     $member->accept_invite();
    1478 
    1479     if ( !$member->save() ) {
     1569    $invites_class = new BP_Groups_Invitation_Manager();
     1570    $args = array(
     1571        'user_id'     => $user_id,
     1572        'item_id'     => $group_id,
     1573        'invite_sent' => 'sent',
     1574    );
     1575
     1576    return $invites_class->accept_invitation( $args );
     1577}
     1578
     1579/**
     1580 * Reject a group invitation.
     1581 *
     1582 * @since 1.0.0
     1583 * @since 5.0.0 The $inviter_id arg was added.
     1584 *
     1585 * @param int $user_id    ID of the user.
     1586 * @param int $group_id   ID of the group.
     1587 * @param int $inviter_id ID of the inviter.
     1588 *
     1589 * @return bool True on success, false on failure.
     1590 */
     1591function groups_reject_invite( $user_id, $group_id, $inviter_id = false ) {
     1592    if ( empty( $user_id ) || empty( $group_id ) ) {
    14801593        return false;
    14811594    }
    14821595
    1483     // Remove request to join.
    1484     if ( $member->check_for_membership_request( $user_id, $group_id ) ) {
    1485         $member->delete_request( $user_id, $group_id );
    1486     }
    1487 
    1488     // Modify group meta.
    1489     groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
    1490 
    1491     /**
    1492      * Fires after a user has accepted a group invite.
    1493      *
    1494      * @since 1.0.0
    1495      * @since 2.8.0 The $inviter_id arg was added.
    1496      *
    1497      * @param int $user_id    ID of the user who accepted the group invite.
    1498      * @param int $group_id   ID of the group being accepted to.
    1499      * @param int $inviter_id ID of the user who invited this user to the group.
    1500      */
    1501     do_action( 'groups_accept_invite', $user_id, $group_id, $inviter_id );
    1502 
    1503     return true;
    1504 }
    1505 
    1506 /**
    1507  * Reject a group invitation.
    1508  *
    1509  * @since 1.0.0
    1510  *
    1511  * @param int $user_id  ID of the user.
    1512  * @param int $group_id ID of the group.
    1513  * @return bool True on success, false on failure.
    1514  */
    1515 function groups_reject_invite( $user_id, $group_id ) {
    1516     if ( ! BP_Groups_Member::delete_invite( $user_id, $group_id ) )
    1517         return false;
     1596    $invites_class = new BP_Groups_Invitation_Manager();
     1597    $success       = $invites_class->delete( array(
     1598        'user_id'    => $user_id,
     1599        'item_id'    => $group_id,
     1600        'inviter_id' => $inviter_id,
     1601    ) );
    15181602
    15191603    /**
     
    15211605     *
    15221606     * @since 1.0.0
    1523      *
    1524      * @param int $user_id  ID of the user rejecting the invite.
    1525      * @param int $group_id ID of the group being rejected.
     1607     * @since 5.0.0 The $inviter_id arg was added.
     1608     *
     1609     * @param int $user_id    ID of the user rejecting the invite.
     1610     * @param int $group_id   ID of the group being rejected.
     1611     * @param int $inviter_id ID of the inviter.
    15261612     */
    1527     do_action( 'groups_reject_invite', $user_id, $group_id );
    1528 
    1529     return true;
     1613    do_action( 'groups_reject_invite', $user_id, $group_id, $inviter_id );
     1614
     1615    return $success;
    15301616}
    15311617
     
    15341620 *
    15351621 * @since 1.0.0
     1622 * @since 5.0.0 The $inviter_id arg was added.
    15361623 *
    15371624 * @param int $user_id  ID of the invited user.
    15381625 * @param int $group_id ID of the group.
     1626 * @param int $inviter_id ID of the inviter.
     1627 *
    15391628 * @return bool True on success, false on failure.
    15401629 */
    1541 function groups_delete_invite( $user_id, $group_id ) {
    1542     if ( ! BP_Groups_Member::delete_invite( $user_id, $group_id ) )
     1630function groups_delete_invite( $user_id, $group_id, $inviter_id = false ) {
     1631    if ( empty( $user_id ) || empty( $group_id ) ) {
    15431632        return false;
     1633    }
     1634
     1635    $invites_class = new BP_Groups_Invitation_Manager();
     1636    $success       = $invites_class->delete( array(
     1637        'user_id'    => $user_id,
     1638        'item_id'    => $group_id,
     1639        'inviter_id' => $inviter_id,
     1640    ) );
    15441641
    15451642    /**
     
    15471644     *
    15481645     * @since 1.9.0
     1646     * @since 5.0.0 The $inviter_id arg was added.
    15491647     *
    15501648     * @param int $user_id  ID of the user whose invitation is being deleted.
    15511649     * @param int $group_id ID of the group whose invitation is being deleted.
     1650     * @param int $inviter_id ID of the inviter.
    15521651     */
    1553     do_action( 'groups_delete_invite', $user_id, $group_id );
     1652    do_action( 'groups_delete_invite', $user_id, $group_id, $inviter_id );
    15541653
    15551654    return true;
     
    15571656
    15581657/**
    1559  * Send all pending invites by a single user to a specific group.
    1560  *
    1561  * @since 1.0.0
    1562  *
    1563  * @param int $user_id  ID of the inviting user.
    1564  * @param int $group_id ID of the group.
    1565  */
    1566 function groups_send_invites( $user_id, $group_id ) {
    1567 
    1568     if ( empty( $user_id ) )
    1569         $user_id = bp_loggedin_user_id();
    1570 
    1571     // Send friend invites.
    1572     $invited_users = groups_get_invites_for_group( $user_id, $group_id );
    1573     $group = groups_get_group( $group_id );
    1574 
    1575     for ( $i = 0, $count = count( $invited_users ); $i < $count; ++$i ) {
    1576         $member = new BP_Groups_Member( $invited_users[$i], $group_id );
    1577 
    1578         // Skip if we've already sent an invite to this user.
    1579         if ( $member->invite_sent ) {
    1580             continue;
    1581         }
    1582 
    1583         // Send the actual invite.
    1584         groups_notification_group_invites( $group, $member, $user_id );
    1585 
    1586         $member->invite_sent = 1;
    1587         $member->save();
     1658 * Send some or all pending invites by a single user to a specific group.
     1659 *
     1660 * @since 1.0.0
     1661 * @since 5.0.0 Parameters changed to associative array.
     1662 *
     1663 * @param array $args {
     1664 *     An array of optional arguments.
     1665 *     @type int    $user_id       ID of the invited user.
     1666 *     @type string $invitee_email Email address of the invited user, if not a member of the site.
     1667 *     @type string $group_id      ID of the group or an array of group IDs.
     1668 *     @type string $inviter_id    ID of the user extending the invitation.
     1669 *     @type bool   $force_resend  Whether to resend the email & notification if one has already been sent.
     1670 * }
     1671 */
     1672function groups_send_invites( $args = array() ) {
     1673    // Backward compatibility with old method of passing arguments.
     1674    if ( ! is_array( $args ) || func_num_args() > 1 ) {
     1675        _deprecated_argument( __METHOD__, '5.0.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
     1676
     1677        $old_args_keys = array(
     1678            0 => 'inviter_id',
     1679            1 => 'group_id',
     1680        );
     1681
     1682        $args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
     1683    }
     1684
     1685    $r = bp_parse_args( $args, array(
     1686        'user_id'       => false,
     1687        'invitee_email' => '',
     1688        'group_id'      => 0,
     1689        'inviter_id'    => bp_loggedin_user_id(),
     1690        'force_resend'  => false,
     1691    ), 'groups_send_invitation' );
     1692
     1693    /*
     1694     * We will generally only want to fetch unsent invitations.
     1695     * If force_resend is true, then we need to fetch both sent and draft invites.
     1696     */
     1697    if ( $r['force_resend'] ) {
     1698        $args['invite_sent'] = 'all';
     1699    } else {
     1700        $args['invite_sent'] = 'draft';
     1701    }
     1702
     1703    $args = array(
     1704        'user_id'       => $r['user_id'],
     1705        'invitee_email' => $r['invitee_email'],
     1706        'item_id'       => $r['group_id'],
     1707        'inviter_id'    => $r['inviter_id'],
     1708    );
     1709    $invites = groups_get_invites( $args );
     1710
     1711    $invited_users = array();
     1712
     1713    $invites_class = new BP_Groups_Invitation_Manager();
     1714    foreach ( $invites as $invite ) {
     1715        $invited_users[] = $invite->user_id;
     1716        $invites_class->send_invitation_by_id( $invite->id );
    15881717    }
    15891718
     
    15981727     * @param int   $user_id       ID of the inviting user.
    15991728     */
    1600     do_action( 'groups_send_invites', $group_id, $invited_users, $user_id );
    1601 }
    1602 
    1603 /**
    1604  * Get IDs of users with outstanding invites to a given group from a specified user.
     1729    do_action( 'groups_send_invites', $r['group_id'], $invited_users, $r['inviter_id'] );
     1730}
     1731
     1732/**
     1733 * Get IDs of users with outstanding invites to a given group.
    16051734 *
    16061735 * @since 1.0.0
     
    16211750
    16221751/**
     1752 * Get invitations to a given group filtered by arguments.
     1753 *
     1754 * @since 5.0.0
     1755 *
     1756 * @param int   $group_id ID of the group.
     1757 * @param array $args     Invitation arguments.
     1758 *                        See BP_Invitation::get() for list.
     1759 *
     1760 * @return array $invites     Matching BP_Invitation objects.
     1761 */
     1762function groups_get_invites( $args = array() ) {
     1763    $invites_class = new BP_Groups_Invitation_Manager();
     1764    return $invites_class->get_invitations( $args );
     1765}
     1766
     1767/**
    16231768 * Check to see whether a user has already been invited to a group.
    16241769 *
     
    16331778 * @param string $type     Optional. Use 'sent' to check for sent invites,
    16341779 *                         'all' to check for all. Default: 'sent'.
    1635  * @return int|bool ID of the membership if found, otherwise false.
     1780 * @return int|bool ID of the first found membership if found, otherwise false.
    16361781 */
    16371782function groups_check_user_has_invite( $user_id, $group_id, $type = 'sent' ) {
    1638     $invite = false;
     1783    return groups_check_has_invite_from_user( $user_id, $group_id, false, $type );
     1784}
     1785
     1786/**
     1787 * Check to see whether a user has already been invited to a group by a particular user.
     1788 *
     1789 * By default, the function checks for invitations that have been sent.
     1790 * Entering 'all' as the $type parameter will return unsent invitations as
     1791 * well (useful to make sure AJAX requests are not duplicated).
     1792 *
     1793 * @since 5.0.0
     1794 *
     1795 * @param int    $user_id    ID of potential group member.
     1796 * @param int    $group_id   ID of potential group.
     1797 * @param string $inviter_id Optional. Use 'sent' to check for sent invites,
     1798 *                           'all' to check for all. Default: 'sent'.
     1799 * @param string $type       Optional. Specify a user ID to limit to only invited from that user.
     1800 *                           Default: 'false'.
     1801 * @return int|bool ID of the first found membership if found, otherwise false.
     1802 */
     1803 function groups_check_has_invite_from_user( $user_id, $group_id, $inviter_id = false, $type = 'sent' ) {
     1804    if ( empty( $user_id ) || empty( $group_id ) ) {
     1805        return false;
     1806    }
    16391807
    16401808    $args = array(
    1641         'is_confirmed' => false,
    1642         'is_banned'    => null,
    1643         'is_admin'     => null,
    1644         'is_mod'       => null,
     1809        'user_id'     => $user_id,
     1810        'item_id'     => $group_id,
     1811        'invite_sent' => 'sent',
    16451812    );
    1646 
    1647     if ( 'sent' === $type ) {
    1648         $args['invite_sent'] = true;
    1649     }
    1650 
    1651     $user_groups = bp_get_user_groups( $user_id, $args );
    1652 
    1653     if ( isset( $user_groups[ $group_id ] ) && 0 !== $user_groups[ $group_id ]->inviter_id ) {
    1654         $invite = $user_groups[ $group_id ]->id;
    1655     }
    1656 
    1657     return $invite;
     1813    if ( $inviter_id ) {
     1814        $args['inviter_id'] = $inviter_id;
     1815    }
     1816    if ( $type === 'draft' || $type === 'all' ) {
     1817        $args['invite_sent'] = $type;
     1818    }
     1819
     1820    $invites_class = new BP_Groups_Invitation_Manager();
     1821
     1822    return $invites_class->invitation_exists( $args );
    16581823}
    16591824
     
    18321997 * @since 1.0.0
    18331998 *
    1834  * @param int $requesting_user_id ID of the user requesting membership.
    1835  * @param int $group_id           ID of the group.
     1999 * @param array|string $args {
     2000 *     Array of arguments.
     2001 *     @type int    $user_id       ID of the user being invited.
     2002 *     @type int    $group_id      ID of the group to which the user is being invited.
     2003 *     @type string $content       Optional. Message to invitee.
     2004 *     @type string $date_modified Optional. Modified date for the invitation.
     2005 *                                 Default: current date/time.
     2006 * }
    18362007 * @return bool True on success, false on failure.
    18372008 */
    1838 function groups_send_membership_request( $requesting_user_id, $group_id ) {
    1839 
    1840     // Prevent duplicate requests.
    1841     if ( groups_check_for_membership_request( $requesting_user_id, $group_id ) )
    1842         return false;
    1843 
    1844     // Check if the user is already a member or is banned.
    1845     if ( groups_is_user_member( $requesting_user_id, $group_id ) || groups_is_user_banned( $requesting_user_id, $group_id ) )
    1846         return false;
    1847 
    1848     // Check if the user is already invited - if so, simply accept invite.
    1849     if ( groups_check_user_has_invite( $requesting_user_id, $group_id ) ) {
    1850         groups_accept_invite( $requesting_user_id, $group_id );
    1851         return true;
    1852     }
    1853 
    1854     $requesting_user                = new BP_Groups_Member;
    1855     $requesting_user->group_id      = $group_id;
    1856     $requesting_user->user_id       = $requesting_user_id;
    1857     $requesting_user->inviter_id    = 0;
    1858     $requesting_user->is_admin      = 0;
    1859     $requesting_user->user_title    = '';
    1860     $requesting_user->date_modified = bp_core_current_time();
    1861     $requesting_user->is_confirmed  = 0;
    1862     $requesting_user->comments      = isset( $_POST['group-request-membership-comments'] ) ? $_POST['group-request-membership-comments'] : '';
    1863 
    1864     if ( $requesting_user->save() ) {
    1865         $admins = groups_get_group_admins( $group_id );
    1866 
    1867         // Saved okay, now send the email notification.
    1868         for ( $i = 0, $count = count( $admins ); $i < $count; ++$i )
    1869             groups_notification_new_membership_request( $requesting_user_id, $admins[$i]->user_id, $group_id, $requesting_user->id );
     2009function groups_send_membership_request( $args = array() ) {
     2010    // Backward compatibility with old method of passing arguments.
     2011    if ( ! is_array( $args ) || func_num_args() > 1 ) {
     2012        _deprecated_argument( __METHOD__, '5.0.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
     2013
     2014        $old_args_keys = array(
     2015            0 => 'user_id',
     2016            1 => 'group_id',
     2017        );
     2018
     2019        $args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
     2020    }
     2021
     2022    $r = bp_parse_args( $args, array(
     2023        'user_id'       => false,
     2024        'group_id'      => false,
     2025        'content'       => '',
     2026        'date_modified' => bp_core_current_time(),
     2027    ), 'groups_send_membership_request' );
     2028
     2029    $inv_args = array(
     2030        'user_id'       => $r['user_id'],
     2031        'item_id'       => $r['group_id'],
     2032        'content'       => $r['content'],
     2033        'date_modified' => $r['date_modified'],
     2034    );
     2035
     2036    $invites_class = new BP_Groups_Invitation_Manager();
     2037    $request_id = $invites_class->add_request( $inv_args );
     2038
     2039    // If a new request was created, send the emails.
     2040    if ( $request_id && is_int( $request_id ) ) {
     2041        $invites_class->send_request_notification_by_id( $request_id );
     2042        $admins = groups_get_group_admins( $r['group_id'] );
    18702043
    18712044        /**
     
    18772050         * @param array $admins              Array of group admins.
    18782051         * @param int   $group_id            ID of the group being requested to.
    1879          * @param int   $requesting_user->id ID of the membership.
     2052         * @param int   $request_id          ID of the request.
    18802053         */
    1881         do_action( 'groups_membership_requested', $requesting_user_id, $admins, $group_id, $requesting_user->id );
    1882 
    1883         return true;
     2054        do_action( 'groups_membership_requested', $r['user_id'], $admins, $r['group_id'], $request_id );
     2055
     2056        return $request_id;
    18842057    }
    18852058
     
    18912064 *
    18922065 * @since 1.0.0
    1893  *
    1894  * @param int $membership_id ID of the membership object.
     2066 * @since 5.0.0 Deprecated $membership_id argument.
     2067 *
     2068 * @param int $membership_id Deprecated 5.0.0.
     2069 * @param int $user_id       Required. ID of the user who requested membership.
     2070 *                           Provide this value along with $group_id to override
     2071 *                           $membership_id.
     2072 * @param int $group_id      Required. ID of the group to which membership is being
     2073 *                           requested. Provide this value along with $user_id to
     2074 *                           override $membership_id.
     2075 * @return bool True on success, false on failure.
     2076 */
     2077function groups_accept_membership_request( $membership_id, $user_id = 0, $group_id = 0 ) {
     2078
     2079    if ( ! empty( $membership_id ) ) {
     2080        _deprecated_argument( __METHOD__, '5.0.0', sprintf( __( 'Argument `membership_id` passed to %1$s  is deprecated. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
     2081    }
     2082
     2083    if ( ! $user_id || ! $group_id ) {
     2084        return false;
     2085    }
     2086
     2087    $invites_class = new BP_Groups_Invitation_Manager();
     2088    $args = array(
     2089        'user_id' => $user_id,
     2090        'item_id' => $group_id,
     2091    );
     2092
     2093    return $invites_class->accept_request( $args );
     2094}
     2095
     2096/**
     2097 * Reject a pending group membership request.
     2098 *
     2099 * @since 1.0.0
     2100 *
     2101 * @param int $membership_id Deprecated 5.0.0.
    18952102 * @param int $user_id       Optional. ID of the user who requested membership.
    18962103 *                           Provide this value along with $group_id to override
     
    19012108 * @return bool True on success, false on failure.
    19022109 */
    1903 function groups_accept_membership_request( $membership_id, $user_id = 0, $group_id = 0 ) {
    1904 
    1905     if ( !empty( $user_id ) && !empty( $group_id ) ) {
    1906         $membership = new BP_Groups_Member( $user_id, $group_id );
    1907     } else {
    1908         $membership = new BP_Groups_Member( false, false, $membership_id );
    1909     }
    1910 
    1911     $membership->accept_request();
    1912 
    1913     if ( !$membership->save() ) {
    1914         return false;
    1915     }
    1916 
    1917     // Check if the user has an outstanding invite, if so delete it.
    1918     if ( groups_check_user_has_invite( $membership->user_id, $membership->group_id ) ) {
    1919         groups_delete_invite( $membership->user_id, $membership->group_id );
    1920     }
    1921 
    1922     /**
    1923      * Fires after a group membership request has been accepted.
    1924      *
    1925      * @since 1.0.0
    1926      *
    1927      * @param int  $user_id  ID of the user who accepted membership.
    1928      * @param int  $group_id ID of the group that was accepted membership to.
    1929      * @param bool $value    If membership was accepted.
    1930      */
    1931     do_action( 'groups_membership_accepted', $membership->user_id, $membership->group_id, true );
    1932 
    1933     return true;
    1934 }
    1935 
    1936 /**
    1937  * Reject a pending group membership request.
    1938  *
    1939  * @since 1.0.0
    1940  *
    1941  * @param int $membership_id ID of the membership object.
    1942  * @param int $user_id       Optional. ID of the user who requested membership.
    1943  *                           Provide this value along with $group_id to override
    1944  *                           $membership_id.
    1945  * @param int $group_id      Optional. ID of the group to which membership is being
    1946  *                           requested. Provide this value along with $user_id to
    1947  *                           override $membership_id.
    1948  * @return bool True on success, false on failure.
    1949  */
    19502110function groups_reject_membership_request( $membership_id, $user_id = 0, $group_id = 0 ) {
    1951     if ( !$membership = groups_delete_membership_request( $membership_id, $user_id, $group_id ) ) {
     2111
     2112    if ( ! empty( $membership_id ) ){
     2113        _deprecated_argument( __METHOD__, '5.0.0', sprintf( __( 'Argument `membership_id` passed to %1$s  is deprecated. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
     2114    }
     2115
     2116    if ( ! groups_delete_membership_request( false, $user_id, $group_id ) ) {
    19522117        return false;
    19532118    }
     
    19622127     * @param bool $value    If membership was accepted.
    19632128     */
    1964     do_action( 'groups_membership_rejected', $membership->user_id, $membership->group_id, false );
     2129    do_action( 'groups_membership_rejected', $user_id, $group_id, false );
    19652130
    19662131    return true;
     
    19722137 * @since 1.2.0
    19732138 *
    1974  * @param int $membership_id ID of the membership object.
     2139 * @param int $membership_id Deprecated 5.0.0.
    19752140 * @param int $user_id       Optional. ID of the user who requested membership.
    19762141 *                           Provide this value along with $group_id to override
     
    19822147 */
    19832148function groups_delete_membership_request( $membership_id, $user_id = 0, $group_id = 0 ) {
    1984     if ( !empty( $user_id ) && !empty( $group_id ) )
    1985         $membership = new BP_Groups_Member( $user_id, $group_id );
    1986     else
    1987         $membership = new BP_Groups_Member( false, false, $membership_id );
    1988 
    1989     if ( ! BP_Groups_Member::delete_request( $membership->user_id, $membership->group_id ) )
     2149    if ( ! empty( $membership_id ) ){
     2150        _deprecated_argument( __METHOD__, '5.0.0', sprintf( __( 'Argument `membership_id` passed to %1$s  is deprecated. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
     2151    }
     2152
     2153    if ( empty( $user_id ) || empty( $group_id ) ) {
    19902154        return false;
    1991 
    1992     return $membership;
     2155    }
     2156
     2157    $invites_class = new BP_Groups_Invitation_Manager();
     2158    $success       = $invites_class->delete_requests( array(
     2159        'user_id' => $user_id,
     2160        'item_id' => $group_id
     2161    ) );
     2162
     2163    return $success;
     2164}
     2165
     2166/**
     2167 * Get group membership requests filtered by arguments.
     2168 *
     2169 * @since 5.0.0
     2170 *
     2171 * @param int   $group_id ID of the group.
     2172 * @param array $args     Invitation arguments.
     2173 *                        See BP_Invitation::get() for list.
     2174 *
     2175 * @return array $requests Matching BP_Invitation objects.
     2176 */
     2177function groups_get_requests( $args = array() ) {
     2178    $invites_class = new BP_Groups_Invitation_Manager();
     2179    return $invites_class->get_requests( $args );
    19932180}
    19942181
     
    20002187 * @param int $user_id  ID of the user.
    20012188 * @param int $group_id ID of the group.
    2002  * @return int|bool ID of the membership if found, otherwise false.
     2189 * @return int|bool ID of the request if found, otherwise false.
    20032190 */
    20042191function groups_check_for_membership_request( $user_id, $group_id ) {
    2005     $request = false;
    2006 
    2007     $user_groups = bp_get_user_groups( $user_id, array(
    2008         'is_confirmed' => false,
    2009         'is_banned'    => false,
    2010         'is_admin'     => null,
    2011         'is_mod'       => null
     2192    if ( empty( $user_id ) || empty( $group_id ) ) {
     2193        return false;
     2194    }
     2195
     2196    $args = array(
     2197        'user_id' => $user_id,
     2198        'item_id' => $group_id,
     2199    );
     2200    $invites_class = new BP_Groups_Invitation_Manager();
     2201
     2202    return $invites_class->request_exists( $args );
     2203}
     2204
     2205 /**
     2206  * Get an array of group IDs to which a user has requested membership.
     2207  *
     2208  * @since 5.0.0
     2209  *
     2210  * @param int $user_id The user ID.
     2211  *
     2212  * @return array Array of group IDs.
     2213  */
     2214 function groups_get_membership_requested_group_ids( $user_id = 0 ) {
     2215    if ( ! $user_id ) {
     2216        $user_id = bp_loggedin_user_id();
     2217    }
     2218
     2219    $group_ids     = groups_get_requests( array(
     2220        'user_id' => $user_id,
     2221        'fields'  => 'item_ids'
    20122222    ) );
    20132223
    2014     if ( isset( $user_groups[ $group_id ] ) && 0 === $user_groups[ $group_id ]->inviter_id ) {
    2015         $request = $user_groups[ $group_id ]->id;
    2016     }
    2017 
    2018     return $request;
     2224    return $group_ids;
     2225}
     2226
     2227 /**
     2228  * Get an array of group IDs to which a user has requested membership.
     2229  *
     2230  * @since 5.0.0
     2231  *
     2232  * @param int $user_id The user ID.
     2233  *
     2234  * @return array Array of group IDs.
     2235  */
     2236 function groups_get_membership_requested_user_ids( $group_id = 0 ) {
     2237    if ( ! $group_id ) {
     2238        $group_id = bp_get_current_group_id();
     2239    }
     2240
     2241    $requests = groups_get_requests( array(
     2242        'item_id' => $group_id,
     2243        'fields'  => 'user_ids'
     2244    ) );
     2245
     2246    return $requests;
    20192247}
    20202248
     
    20272255 * @return bool True on success, false on failure.
    20282256 */
    2029 function groups_accept_all_pending_membership_requests( $group_id ) {
    2030     $user_ids = BP_Groups_Member::get_all_membership_request_user_ids( $group_id );
    2031 
    2032     if ( !$user_ids )
     2257function groups_accept_all_pending_membership_requests( $group_id = 0 ) {
     2258    if ( ! $group_id ) {
     2259        $group_id = bp_get_current_group_id();
     2260    }
     2261
     2262    $user_ids = groups_get_membership_requested_user_ids( $group_id );
     2263
     2264    if ( ! $user_ids ) {
    20332265        return false;
    2034 
    2035     foreach ( (array) $user_ids as $user_id )
     2266    }
     2267
     2268    foreach ( (array) $user_ids as $user_id ) {
    20362269        groups_accept_membership_request( false, $user_id, $group_id );
     2270    }
    20372271
    20382272    /**
     
    27102944    }
    27112945
    2712     $requests = BP_Groups_Member::get_user_memberships( $user->ID, array(
    2713         'type'     => 'pending_request',
     2946    $requests = groups_get_requests( array(
     2947        'user_id'  => $user->ID,
    27142948        'page'     => $page,
    27152949        'per_page' => $number,
     
    27172951
    27182952    foreach ( $requests as $request ) {
    2719         $group = groups_get_group( $request->group_id );
     2953        $group = groups_get_group( $request->item_id );
    27202954
    27212955        $item_data = array(
     
    27763010    }
    27773011
    2778     $invitations = BP_Groups_Member::get_user_memberships( $user->ID, array(
    2779         'type'     => 'pending_sent_invitation',
    2780         'page'     => $page,
    2781         'per_page' => $number,
     3012    $invitations = groups_get_invites( array(
     3013        'inviter_id'  => $user->ID,
     3014        'page'        => $page,
     3015        'per_page'    => $number,
    27823016    ) );
    27833017
    27843018    foreach ( $invitations as $invitation ) {
    2785         $group = groups_get_group( $invitation->group_id );
     3019        $group = groups_get_group( $invitation->item_id );
    27863020
    27873021        $item_data = array(
     
    28463080    }
    28473081
    2848     $invitations = BP_Groups_Member::get_user_memberships( $user->ID, array(
    2849         'type'     => 'pending_received_invitation',
     3082    $invitations = groups_get_invites( array(
     3083        'user_id'  => $user->ID,
    28503084        'page'     => $page,
    28513085        'per_page' => $number,
     
    28533087
    28543088    foreach ( $invitations as $invitation ) {
    2855         $group = groups_get_group( $invitation->group_id );
     3089        $group = groups_get_group( $invitation->item_id );
    28563090
    28573091        $item_data = array(
     
    28903124    );
    28913125}
     3126
     3127/**
     3128 * Migrate invitations and requests from pre-5.0 group_members table to invitations table.
     3129 *
     3130 * @since 5.0.0
     3131 */
     3132function bp_groups_migrate_invitations() {
     3133    global $wpdb;
     3134    $bp = buddypress();
     3135
     3136    $records = $wpdb->get_results( "SELECT id, group_id, user_id, inviter_id, date_modified, comments, invite_sent FROM {$bp->groups->table_name_members} WHERE is_confirmed = 0 AND is_banned = 0" );
     3137
     3138    $processed = array();
     3139    $values = array();
     3140    foreach ( $records as $record ) {
     3141        $values[] = $wpdb->prepare(
     3142            "(%d, %d, %s, %s, %d, %d, %s, %s, %s, %d, %d)",
     3143            (int) $record->user_id,
     3144            (int) $record->inviter_id,
     3145            '',
     3146            'bp_groups_invitation_manager',
     3147            (int) $record->group_id,
     3148            0,
     3149            ( 0 === (int) $record->inviter_id ) ? 'request' : 'invite',
     3150            $record->comments,
     3151            $record->date_modified,
     3152            (int) $record->invite_sent,
     3153            0
     3154        );
     3155        $processed[] = (int) $record->id;
     3156    }
     3157
     3158    $table_name = BP_Invitation_Manager::get_table_name();
     3159    $query = "INSERT INTO {$table_name} (user_id, inviter_id, invitee_email, class, item_id, secondary_item_id, type, content, date_modified, invite_sent, accepted) VALUES ";
     3160    $query .= implode(', ', $values );
     3161    $query .= ';';
     3162    $wpdb->query( $query );
     3163
     3164    $ids_to_delete = implode( ',', $processed );
     3165    if ( $ids_to_delete ) {
     3166        $wpdb->query( "DELETE FROM {$bp->groups->table_name_members} WHERE ID IN ($ids_to_delete)" );
     3167    }
     3168}
  • trunk/src/bp-groups/bp-groups-notifications.php

    r12087 r12429  
    144144    );
    145145
     146    $request_message = '';
     147    $requests = groups_get_requests( $args = array(
     148        'user_id'    => $requesting_user_id,
     149        'item_id'    => $group_id,
     150    ) );
     151    if ( $requests ) {
     152        $request_message = current( $requests )->content;
     153    }
     154
    146155    $group = groups_get_group( $group_id );
    147156    $args  = array(
     
    152161            'group.id'             => $group_id,
    153162            'group-requests.url'   => esc_url( bp_get_group_permalink( $group ) . 'admin/membership-requests' ),
    154             'membership.id'        => $membership_id,
    155163            'profile.url'          => esc_url( bp_core_get_user_domain( $requesting_user_id ) ),
    156164            'requesting-user.id'   => $requesting_user_id,
    157165            'requesting-user.name' => bp_core_get_user_displayname( $requesting_user_id ),
     166            'request.message'      => $request_message,
    158167            'unsubscribe'          => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
    159168        ),
     
    290299 * @since 1.0.0
    291300 *
    292  * @param BP_Groups_Group  $group           Group object.
    293  * @param BP_Groups_Member $member          Member object.
    294  * @param int              $inviter_user_id ID of the user who sent the invite.
     301 * @param BP_Groups_Group      $group           Group object.
     302 * @param BP_Groups_Member|int $member          Member object or invited_user_id.
     303 * @param int                  $inviter_user_id ID of the user who sent the invite.
    295304 */
    296305function groups_notification_group_invites( &$group, &$member, $inviter_user_id ) {
    297 
    298     // Bail if member has already been invited.
    299     if ( ! empty( $member->invite_sent ) ) {
    300         return;
    301     }
    302306
    303307    // @todo $inviter_ud may be used for caching, test without it
    304308    $inviter_ud      = bp_core_get_core_userdata( $inviter_user_id );
    305     $invited_user_id = $member->user_id;
     309
     310    if ( $member instanceof BP_Groups_Member ) {
     311        $invited_user_id = $member->user_id;
     312    } else if ( is_int( $member ) ) {
     313        $invited_user_id = $member;
     314    }
    306315
    307316    // Trigger a BuddyPress Notification.
     
    327336    );
    328337
     338    $invite_message = '';
     339    $invitations = groups_get_invites( $args = array(
     340        'user_id'    => $invited_user_id,
     341        'item_id'    => $group->id,
     342        'inviter_id' => $inviter_user_id,
     343    ) );
     344    if ( $invitations ) {
     345        $invite_message = current( $invitations )->content;
     346    }
     347
    329348    $args         = array(
    330349        'tokens' => array(
    331             'group'        => $group,
    332             'group.url'    => bp_get_group_permalink( $group ),
    333             'group.name'   => $group->name,
    334             'inviter.name' => bp_core_get_userlink( $inviter_user_id, true, false, true ),
    335             'inviter.url'  => bp_core_get_user_domain( $inviter_user_id ),
    336             'inviter.id'   => $inviter_user_id,
    337             'invites.url'  => esc_url( $invited_link . '/invites/' ),
    338             'unsubscribe'  => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
     350            'group'          => $group,
     351            'group.url'      => bp_get_group_permalink( $group ),
     352            'group.name'     => $group->name,
     353            'inviter.name'   => bp_core_get_userlink( $inviter_user_id, true, false, true ),
     354            'inviter.url'    => bp_core_get_user_domain( $inviter_user_id ),
     355            'inviter.id'     => $inviter_user_id,
     356            'invites.url'    => esc_url( $invited_link . '/invites/' ),
     357            'invite.message' => $invite_message,
     358            'unsubscribe'    => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
    339359        ),
    340360    );
     361
    341362    bp_send_email( 'groups-invitation', (int) $invited_user_id, $args );
    342363}
  • trunk/src/bp-groups/bp-groups-template.php

    r12409 r12429  
    54875487        global $requests_template;
    54885488
     5489        $link = add_query_arg( array(
     5490            '_wpnonce' => wp_create_nonce( 'groups_reject_membership_request' ),
     5491            'user_id'  => $requests_template->request->user_id,
     5492            'action'   => 'reject'
     5493        ), trailingslashit( bp_get_group_permalink( groups_get_current_group() ) ) . 'admin/membership-requests/' );
     5494
    54895495        /**
    54905496         * Filters the URL to use to reject a membership request.
     
    54945500         * @param string $value URL to use to reject a membership request.
    54955501         */
    5496         return apply_filters( 'bp_get_group_request_reject_link', wp_nonce_url( trailingslashit( bp_get_group_permalink( groups_get_current_group() ) . 'admin/membership-requests/reject/' . $requests_template->request->membership_id ), 'groups_reject_membership_request' ) );
     5502        return apply_filters( 'bp_get_group_request_reject_link', $link );
    54975503    }
    54985504
     
    55115517        global $requests_template;
    55125518
     5519        $link = add_query_arg( array(
     5520            '_wpnonce' => wp_create_nonce( 'groups_accept_membership_request' ),
     5521            'user_id'  => $requests_template->request->user_id,
     5522            'action'   => 'accept'
     5523        ), trailingslashit( bp_get_group_permalink( groups_get_current_group() ) ) . 'admin/membership-requests/' );
     5524
    55135525        /**
    55145526         * Filters the URL to use to accept a membership request.
     
    55185530         * @param string $value URL to use to accept a membership request.
    55195531         */
    5520         return apply_filters( 'bp_get_group_request_accept_link', wp_nonce_url( trailingslashit( bp_get_group_permalink( groups_get_current_group() ) . 'admin/membership-requests/accept/' . $requests_template->request->membership_id ), 'groups_accept_membership_request' ) );
     5532        return apply_filters( 'bp_get_group_request_accept_link', $link );
    55215533    }
    55225534
  • trunk/tests/phpunit/testcases/groups/functions.php

    r12328 r12429  
    1313        self::$group_ids = $factory->group->create_many( 2, array(
    1414            'creator_id' => self::$user_ids[2],
     15            'status'     => 'private'
    1516        ) );
    1617    }
     
    115116        $u1 = self::factory()->user->create();
    116117        $u2 = self::factory()->user->create();
    117         $g = self::factory()->group->create();
     118        $g = self::factory()->group->create( array( 'status' => 'private', 'creator_id' => $u2 ) );
     119
    118120        groups_invite_user( array(
    119121            'user_id' => $u1,
    120122            'group_id' => $g,
    121123            'inviter_id' => $u2,
    122         ) );
    123 
    124         groups_accept_invite( $u2, $g );
    125 
    126         $this->assertEquals( 1, bp_get_user_meta( $u2, 'total_group_count', true ) );
     124            'send_invite' => 1,
     125        ) );
     126
     127        groups_accept_invite( $u1, $g );
     128
     129        $this->assertEquals( 1, bp_get_user_meta( $u1, 'total_group_count', true ) );
    127130    }
    128131
     
    138141        $this->set_current_user( $u2 );
    139142
    140         $g = self::factory()->group->create();
    141         groups_send_membership_request( $u1, $g );
     143        $g = self::factory()->group->create( array( 'status' => 'private' ) );
     144        groups_send_membership_request( array(
     145            'user_id'       => $u1,
     146            'group_id'      => $g,
     147        ) );
    142148
    143149        groups_accept_membership_request( 0, $u1, $g );
     
    242248        $u1 = self::factory()->user->create();
    243249        $u2 = self::factory()->user->create();
    244         $g = self::factory()->group->create( array( 'creator_id' => $u1 ) );
     250        $g = self::factory()->group->create( array( 'status' => 'private', 'creator_id' => $u1 ) );
    245251        groups_invite_user( array(
    246             'user_id' => $u1,
    247             'group_id' => $g,
    248             'inviter_id' => $u2,
     252            'user_id'     => $u2,
     253            'group_id'    => $g,
     254            'inviter_id'  => $u1,
     255            'send_invite' => 1,
    249256        ) );
    250257
     
    261268        $u1 = self::factory()->user->create();
    262269        $u2 = self::factory()->user->create();
    263         $g = self::factory()->group->create( array( 'creator_id' => $u1 ) );
    264 
    265         groups_send_membership_request( $u2, $g );
     270        $g = self::factory()->group->create( array( 'status' => 'private', 'creator_id' => $u1 ) );
     271
     272        groups_send_membership_request( array(
     273            'user_id'       => $u2,
     274            'group_id'      => $g,
     275        ) );
    266276        groups_accept_membership_request( 0, $u2, $g );
    267277
     
    633643        $u1 = self::factory()->user->create();
    634644        $u2 = self::factory()->user->create();
    635         $g = self::factory()->group->create( array( 'creator_id' => $u1 ) );
     645        $g = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
    636646
    637647        // create invitation
     
    640650            'group_id'   => $g,
    641651            'inviter_id' => $u1,
    642         ) );
    643 
    644         // send the invite
    645         // this function is imperative to set the 'invite_sent' flag in the DB
    646         // why is this separated from groups_invite_user()?
    647         // @see groups_screen_group_invite()
    648         groups_send_invites( $u1, $g );
     652            'send_invite' => 1
     653        ) );
    649654
    650655        // assert invite count
     
    891896     */
    892897    public function test_bp_groups_pending_requests_personal_data_exporter() {
    893         groups_send_membership_request( self::$user_ids[0], self::$group_ids[0] );
     898        groups_send_membership_request( array(
     899            'user_id'       => self::$user_ids[0],
     900            'group_id'      => self::$group_ids[0],
     901        ) );
    894902
    895903        $test_user = new WP_User( self::$user_ids[0] );
     
    908916    public function test_bp_groups_pending_sent_invitations_personal_data_exporter() {
    909917        groups_invite_user( array(
    910             'user_id'    => self::$user_ids[1],
    911             'group_id'   => self::$group_ids[0],
    912             'inviter_id' => self::$user_ids[0],
    913         ) );
    914 
    915         $test_user = new WP_User( self::$user_ids[0] );
     918            'user_id'     => self::$user_ids[0],
     919            'group_id'    => self::$group_ids[0],
     920            'inviter_id'  => self::$user_ids[2],
     921            'send_invite' => 1,
     922        ) );
     923
     924        $test_user = new WP_User( self::$user_ids[2] );
    916925
    917926        $actual = bp_groups_pending_sent_invitations_personal_data_exporter( $test_user->user_email, 1 );
     
    930939            'user_id'    => self::$user_ids[0],
    931940            'group_id'   => self::$group_ids[0],
    932             'inviter_id' => self::$user_ids[1],
     941            'inviter_id' => self::$user_ids[2],
    933942        ) );
    934943
Note: See TracChangeset for help on using the changeset viewer.