Skip to:
Content

BuddyPress.org

Changeset 12923


Ignore:
Timestamp:
04/28/2021 11:51:25 PM (3 months ago)
Author:
dcavins
Message:

Member Invites: Change behavior of registration form.

Add custom messages to the registration form when the user
is accepting an invitation.

See #8139.

Location:
trunk/src/bp-members
Files:
1 added
1 edited

Legend:

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

    r12919 r12923  
    173173}
    174174add_filter( 'bp_user_can', 'bp_members_user_can_filter', 10, 5 );
     175
     176/**
     177 * Do not allow the new user to change the email address
     178 * if they are accepting a community invitation.
     179 *
     180 * @since 8.0.0
     181 *
     182 * @param array  $attributes The field attributes.
     183 * @param string $name       The field name.
     184 *
     185 * @return array $attributes The field attributes.
     186 */
     187function bp_members_invitations_make_registration_email_input_readonly_if_invite( $attributes, $name ) {
     188    if ( 'email' === $name && bp_get_members_invitations_allowed() ) {
     189        $invite = bp_get_members_invitation_from_request();
     190        if ( $invite->id ) {
     191            $attributes['readonly'] = 'readonly';
     192        }
     193    }
     194    return $attributes;
     195}
     196add_filter( 'bp_get_form_field_attributes', 'bp_members_invitations_make_registration_email_input_readonly_if_invite', 10, 2 );
     197
     198/**
     199 * Provide a more-specific welcome message if the new user
     200 * is accepting a network invitation.
     201 *
     202 * @since 8.0.0
     203 *
     204 * @return string $message The message text.
     205 */
     206function bp_members_invitations_get_registration_welcome_message() {
     207    $message = '';
     208    if ( ! bp_get_members_invitations_allowed() ) {
     209        return $message;
     210    }
     211    $invite = bp_get_members_invitation_from_request();
     212    if ( ! $invite->id || ! $invite->invitee_email ) {
     213        return $message;
     214    }
     215
     216    // Check if the user is already a site member.
     217    $maybe_user = get_user_by( 'email', $invite->invitee_email );
     218
     219    // This user is already a member
     220    if ( $maybe_user ) {
     221        $message = sprintf(
     222            esc_html__( 'Welcome! You are already a member of this site. Please %1$s to continue. ', 'buddypress' ),
     223            sprintf( '<a href="%1$s">%2$s</a>', esc_url( wp_login_url( bp_get_root_domain() ) ), esc_html__( 'log in', 'buddypress' ) )
     224        );
     225    // This user can register!
     226    } else {
     227
     228        // Fetch the display names of all inviters to personalize the welcome message.
     229        $args = array(
     230            'invitee_email' => $invite->invitee_email,
     231            'invite_sent'   => 'sent',
     232        );
     233        $all_invites = bp_members_invitations_get_invites( $all_args );
     234        $inviters = array();
     235        foreach ( $all_invites as $inv ) {
     236            $inviters[] = bp_core_get_user_displayname( $inv->inviter_id );
     237        }
     238
     239        if ( ! empty( $inviters ) ) {
     240            $message = sprintf( _n( 'Welcome! You&#8217;ve been invited to join the site by the following user: %s. ', 'Welcome! You&#8217;ve been invited to join the site by the following users: %s. ', count( $inviters ), 'buddypress' ), implode( ', ', $inviters ) );
     241        } else {
     242            $message = __( 'Welcome! You&#8217;ve been invited to join the site. ', 'buddypress' );
     243        }
     244    }
     245
     246    return $message;
     247}
     248
     249/**
     250 * Provide a more-specific "registration is disabled" message
     251 * if registration is available by invitation only.
     252 * Also provide failure note if new user is trying to accept
     253 * a network invitation but there's a problem.
     254 *
     255 * @since 8.0.0
     256 *
     257 * @return string $message The message text.
     258 */
     259function bp_members_invitations_get_modified_registration_disabled_message() {
     260    $message = '';
     261    if ( bp_get_members_invitations_allowed() ) {
     262
     263        $invite = bp_get_members_invitation_from_request();
     264        if ( ! $invite->id || ! $invite->invitee_email ) {
     265            return $message;
     266        }
     267
     268        // Check if the user is already a site member.
     269        $maybe_user = get_user_by( 'email', $invite->invitee_email );
     270
     271        if ( ! $maybe_user ) {
     272            $message = __( 'Member registration is allowed by invitation only.', 'buddypress' );
     273            // Is the user trying to accept an invitation but something is wrong?
     274            if ( ! empty( $_GET['inv'] ) ) {
     275                $message .= __( ' It looks like there is a problem with your invitation. Please try again.', 'buddypress' );
     276            }
     277        } else if ( 'nouveau' === bp_get_theme_package_id() ) {
     278            $message = sprintf(
     279                esc_html__( 'Welcome! You are already a member of this site. Please %1$s to continue. If you have forgotten your password, you can %2$s.', 'buddypress' ),
     280                sprintf( '<a href="%1$s">%2$s</a>', esc_url( wp_login_url( bp_get_root_domain() ) ), esc_html__( 'log in', 'buddypress' ) ),
     281                sprintf( '<a href="%1$s">%2$s</a>', esc_url( wp_lostpassword_url(  bp_get_root_domain() ) ), esc_html__( 'reset it', 'buddypress' ) )
     282            );
     283        }
     284    }
     285    return $message;
     286}
     287
    175288/**
    176289 * Sanitize the invitation property output.
Note: See TracChangeset for help on using the changeset viewer.