Skip to:
Content

BuddyPress.org

Changeset 12918


Ignore:
Timestamp:
04/28/2021 11:50:21 PM (3 years ago)
Author:
dcavins
Message:

Member Invites: Add creation, deletion functions.

Introduce functions that apply the logic in
BP_Members_Invitation_Manager, including:

  • bp_members_invitations_get_invites
  • bp_members_invitations_user_has_sent_invites
  • bp_members_invitations_invite_user
  • bp_members_invitation_resend_by_id
  • bp_members_invitations_delete_by_id
  • bp_members_invitations_delete_invites
  • bp_members_invitations_get_hash
  • bp_get_members_invitation_from_request

See #8139.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-members/bp-members-functions.php

    r12905 r12918  
    33433343}
    33443344add_action( 'bp_core_activated_user', 'bp_send_welcome_email', 10, 1 );
     3345
     3346/**
     3347 * Get invitations to the BP community filtered by arguments.
     3348 *
     3349 * @since 8.0.0
     3350 *
     3351 * @param array $args     Invitation arguments.
     3352 *                        See BP_Invitation::get() for list.
     3353 *
     3354 * @return array $invites     Matching BP_Invitation objects.
     3355 */
     3356function bp_members_invitations_get_invites( $args = array() ) {
     3357    $invites_class = new BP_Members_Invitation_Manager();
     3358    return $invites_class->get_invitations( $args );
     3359}
     3360
     3361/**
     3362 * Check whether a user has sent any community invitations.
     3363 *
     3364 * @since 8.0.0
     3365 *
     3366 * @param int $user_id ID of user to check for invitations sent by.
     3367 *                     Defaults to the current user's ID.
     3368 *
     3369 * @return bool $invites True if user has sent invites.
     3370 */
     3371function bp_members_invitations_user_has_sent_invites( $user_id = 0 ) {
     3372    if ( 0 === $user_id ) {
     3373        $user_id = bp_loggedin_user_id();
     3374        if ( ! $user_id ) {
     3375            return false;
     3376        }
     3377    }
     3378    $invites_class = new BP_Members_Invitation_Manager();
     3379    $args = array(
     3380        'inviter_id' => $user_id,
     3381    );
     3382    return (bool) $invites_class->invitation_exists( $args );
     3383}
     3384
     3385/**
     3386 * Invite a user to a BP community.
     3387 *
     3388 * @since 8.0.0
     3389 *
     3390 * @param array|string $args {
     3391 *     Array of arguments.
     3392 *     @type int    $invitee_email Email address of the user being invited.
     3393 *     @type int    $network_id    ID of the network to which the user is being invited.
     3394 *     @type int    $inviter_id    Optional. ID of the inviting user. Default:
     3395 *                                 ID of the logged-in user.
     3396 *     @type string $date_modified Optional. Modified date for the invitation.
     3397 *                                 Default: current date/time.
     3398 *     @type string $content       Optional. Message to invitee.
     3399 *     @type bool   $send_invite   Optional. Whether the invitation should be
     3400 *                                 sent now. Default: false.
     3401 * }
     3402 * @return bool True on success, false on failure.
     3403 */
     3404function bp_members_invitations_invite_user( $args ) {
     3405    $r = bp_parse_args( $args, array(
     3406        'invitee_email' => '',
     3407        'network_id'    => get_current_network_id(),
     3408        'inviter_id'    => bp_loggedin_user_id(),
     3409        'date_modified' => bp_core_current_time(),
     3410        'content'       => '',
     3411        'send_invite'   => 0
     3412    ), 'community_invite_user' );
     3413
     3414    $inv_args = array(
     3415        'invitee_email' => $r['invitee_email'],
     3416        'item_id'       => $r['network_id'],
     3417        'inviter_id'    => $r['inviter_id'],
     3418        'date_modified' => $r['date_modified'],
     3419        'content'       => $r['content'],
     3420        'send_invite'   => $r['send_invite']
     3421    );
     3422
     3423    // Create the invitataion.
     3424    $invites_class = new BP_Members_Invitation_Manager();
     3425    $created       = $invites_class->add_invitation( $inv_args );
     3426
     3427    /**
     3428     * Fires after the creation of a new network invite.
     3429     *
     3430     * @since 8.0.0
     3431     *
     3432     * @param array    $r       Array of parsed arguments for the network invite.
     3433     * @param int|bool $created The ID of the invitation or false if it couldn't be created.
     3434     */
     3435    do_action( 'bp_members_invitations_invite_user', $r, $created );
     3436
     3437    return $created;
     3438}
     3439
     3440/**
     3441 * Resend a membership invitation email by id.
     3442 *
     3443 * @since 8.0.0
     3444 *
     3445 * @param int $id ID of the invitation to resend.
     3446 * @return bool True on success, false on failure.
     3447 */
     3448function bp_members_invitation_resend_by_id( $id = 0 ) {
     3449
     3450    // Find the invitation before deleting it.
     3451    $existing_invite = new BP_Invitation( $id );
     3452    $invites_class   = new BP_Members_Invitation_Manager();
     3453    $success         = $invites_class->send_invitation_by_id( $id );
     3454
     3455    if ( ! $success ) {
     3456        return $success;
     3457    }
     3458
     3459    /**
     3460     * Fires after the re-sending of a network invite.
     3461     *
     3462     * @since 8.0.0
     3463     *
     3464     * @param BP_Invitation $existing_invite The invitation that was resent.
     3465     */
     3466    do_action( 'bp_members_invitations_resend_invitation', $existing_invite );
     3467
     3468    return $success;
     3469}
     3470
     3471/**
     3472 * Delete a membership invitation by id.
     3473 *
     3474 * @since 8.0.0
     3475 *
     3476 * @param int $id ID of the invitation to delete.
     3477 * @return int|bool Number of rows deleted on success, false on failure.
     3478 */
     3479function bp_members_invitations_delete_by_id( $id = 0 ) {
     3480
     3481    // Find the invitation before deleting it.
     3482    $existing_invite = new BP_Invitation( $id );
     3483    $invites_class   = new BP_Members_Invitation_Manager();
     3484    $success         = $invites_class->delete_by_id( $id );
     3485
     3486    if ( ! $success ) {
     3487        return $success;
     3488    }
     3489
     3490    // Run a different action depending on the status of the invite.
     3491    if ( ! $existing_invite->invite_sent ) {
     3492        /**
     3493         * Fires after the deletion of an unsent community invite.
     3494         *
     3495         * @since 8.0.0
     3496         *
     3497         * @param BP_Invitation $existing_invite The invitation to be deleted.
     3498         */
     3499        do_action( 'bp_members_invitations_canceled_invitation', $existing_invite );
     3500    } else if ( ! $existing_invite->accepted ) {
     3501        /**
     3502         * Fires after the deletion of a sent, but not yet accepted, community invite.
     3503         *
     3504         * @since 8.0.0
     3505         *
     3506         * @param BP_Invitation $existing_invite The invitation to be deleted.
     3507         */
     3508        do_action( 'bp_members_invitations_revoked_invitation', $existing_invite );
     3509    } else {
     3510        /**
     3511         * Fires after the deletion of a sent and accepted community invite.
     3512         *
     3513         * @since 8.0.0
     3514         *
     3515         * @param BP_Invitation $existing_invite The invitation to be deleted.
     3516         */
     3517        do_action( 'bp_members_invitations_deleted_invitation', $existing_invite );
     3518    }
     3519
     3520    return $success;
     3521}
     3522
     3523/**
     3524 * Delete a membership invitation.
     3525 *
     3526 * @since 8.0.0
     3527 *
     3528 * @param intring $args {
     3529 *     Array of arguments.
     3530 *     @type int|array $id            Id(s) of the invitation(s) to remove.
     3531 *     @type int       $invitee_email Email address of the user being invited.
     3532 *     @type int       $network_id    ID of the network to which the user is being invited.
     3533 *     @type int       $inviter_id    ID of the inviting user.
     3534 *     @type int       $accepted      Whether the invitation has been accepted yet.
     3535 *     @type int       $invite_sent   Whether the invitation has been sent yet.
     3536 * }
     3537 * @return bool True if all were deleted.
     3538 */
     3539function bp_members_invitations_delete_invites( $args ) {
     3540    $r = bp_parse_args( $args, array(
     3541        'id'            => false,
     3542        'invitee_email' => '',
     3543        'network_id'    => get_current_network_id(),
     3544        'inviter_id'    => null,
     3545        'accepted'      => null,
     3546        'invite_sent'   => null
     3547    ), 'members_invitations_delete_invites' );
     3548
     3549    $inv_args = array(
     3550        'id'            => $r['id'],
     3551        'invitee_email' => $r['invitee_email'],
     3552        'item_id'       => $r['network_id'],
     3553        'inviter_id'    => $r['inviter_id'],
     3554        'accepted'      => $r['accepted'],
     3555        'invite_sent'   => $r['invite_sent']
     3556    );
     3557
     3558    // Find the invitation(s).
     3559    $invites       = bp_members_invitations_get_invites( $inv_args );
     3560    $total_count   = count( $invites );
     3561
     3562    // Loop through, deleting each invitation.
     3563    $deleted = 0;
     3564    foreach ( $invites as $invite ) {
     3565        $success = bp_members_invitations_delete_by_id( $invite->id );
     3566        if ( $success ) {
     3567            $deleted++;
     3568        }
     3569    }
     3570
     3571    return $deleted === $total_count;
     3572}
     3573
     3574/**
     3575 * Get hash based on details of a membership invitation and the inviter.
     3576 *
     3577 * @since 8.0.0
     3578 *
     3579 * @param BP_Invitation object $invitation Invitation to create hash from.
     3580 *
     3581 * @return string $hash Calculated sha1 hash.
     3582 */
     3583function bp_members_invitations_get_hash( BP_Invitation $invitation ) {
     3584    $hash = false;
     3585
     3586    if ( ! empty( $invitation->id ) ) {
     3587        $inviter_ud = get_userdata( $invitation->inviter_id );
     3588        if ( $inviter_ud ) {
     3589            /*
     3590             * Use some inviter details as part of the hash so that invitations from
     3591             * users who are subsequently marked as spam will be invalidated.
     3592             */
     3593            $hash = wp_hash( "{$invitation->inviter_id}:{$invitation->invitee_email}:{$inviter_ud->user_status}:{$inviter_ud->user_registered}" );
     3594        }
     3595    }
     3596
     3597    // If there's a problem, return a string that will change and thus fail.
     3598    if ( ! $hash ) {
     3599        $hash = wp_generate_password( 32, false );
     3600    }
     3601
     3602    /**
     3603     * Filters the hash calculated by the invitation details.
     3604     *
     3605     * @since 8.0.0
     3606     *
     3607     * @param string $hash Calculated sha1 hash.
     3608     * @param BP_Invitation object $invitation Invitation hash was created from.
     3609     */
     3610    return apply_filters( 'bp_members_invitations_get_hash', $hash, $invitation );
     3611}
     3612
     3613/**
     3614 * Get the current invitation specified by the $_GET parameters.
     3615 *
     3616 * @since 8.0.0
     3617 *
     3618 * @return BP_Invitation $invite Invitation specified by the $_GET parameters.
     3619 */
     3620function bp_get_members_invitation_from_request() {
     3621    $invites_class = new BP_Members_Invitation_Manager();
     3622    $invite        = $invites_class->get_by_id( 0 );
     3623
     3624    if ( bp_get_members_invitations_allowed() && ! empty( $_GET['inv'] ) ) {
     3625        // Check to make sure the passed hash matches a calculated hash.
     3626        $maybe_invite = $invites_class->get_by_id( absint( $_GET['inv'] ) );
     3627        $hash = bp_members_invitations_get_hash( $maybe_invite );
     3628        if ( $_GET['ih'] === $hash ) {
     3629            $invite = $maybe_invite;
     3630        }
     3631    }
     3632
     3633    /**
     3634     * Filters the invitation specified by the $_GET parameters.
     3635     *
     3636     * @since 8.0.0
     3637     *
     3638     * @param BP_Invitation $invite Invitation specified by the $_GET parameters.
     3639     */
     3640    return apply_filters( 'bp_get_members_invitation_from_request', $invite );
     3641}
Note: See TracChangeset for help on using the changeset viewer.