<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][12923] trunk/src/bp-members: Member Invites: Change behavior of registration form.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/12923">12923</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/12923","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dcavins</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-04-28 23:51:25 +0000 (Wed, 28 Apr 2021)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Member Invites: Change behavior of registration form.

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

See <a href="http://buddypress.trac.wordpress.org/ticket/8139">#8139</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpmembersbpmembersfiltersphp">trunk/src/bp-members/bp-members-filters.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcbpmembersbpmembersinvitationsphp">trunk/src/bp-members/bp-members-invitations.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpmembersbpmembersfiltersphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-members/bp-members-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-members/bp-members-filters.php       2021-04-28 23:51:13 UTC (rev 12922)
+++ trunk/src/bp-members/bp-members-filters.php 2021-04-28 23:51:25 UTC (rev 12923)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -172,7 +172,120 @@
</span><span class="cx" style="display: block; padding: 0 10px">        return $retval;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'bp_user_can', 'bp_members_user_can_filter', 10, 5 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Do not allow the new user to change the email address
+ * if they are accepting a community invitation.
+ *
+ * @since 8.0.0
+ *
+ * @param array  $attributes The field attributes.
+ * @param string $name       The field name.
+ *
+ * @return array $attributes The field attributes.
+ */
+function bp_members_invitations_make_registration_email_input_readonly_if_invite( $attributes, $name ) {
+       if ( 'email' === $name && bp_get_members_invitations_allowed() ) {
+               $invite = bp_get_members_invitation_from_request();
+               if ( $invite->id ) {
+                       $attributes['readonly'] = 'readonly';
+               }
+       }
+       return $attributes;
+}
+add_filter( 'bp_get_form_field_attributes', 'bp_members_invitations_make_registration_email_input_readonly_if_invite', 10, 2 );
+
+/**
+ * Provide a more-specific welcome message if the new user
+ * is accepting a network invitation.
+ *
+ * @since 8.0.0
+ *
+ * @return string $message The message text.
+ */
+function bp_members_invitations_get_registration_welcome_message() {
+       $message = '';
+       if ( ! bp_get_members_invitations_allowed() ) {
+               return $message;
+       }
+       $invite = bp_get_members_invitation_from_request();
+       if ( ! $invite->id || ! $invite->invitee_email ) {
+               return $message;
+       }
+
+       // Check if the user is already a site member.
+       $maybe_user = get_user_by( 'email', $invite->invitee_email );
+
+       // This user is already a member
+       if ( $maybe_user ) {
+               $message = sprintf(
+                       esc_html__( 'Welcome! You are already a member of this site. Please %1$s to continue. ', 'buddypress' ),
+                       sprintf( '<a href="%1$s">%2$s</a>', esc_url( wp_login_url( bp_get_root_domain() ) ), esc_html__( 'log in', 'buddypress' ) )
+               );
+       // This user can register!
+       } else {
+
+               // Fetch the display names of all inviters to personalize the welcome message.
+               $args = array(
+                       'invitee_email' => $invite->invitee_email,
+                       'invite_sent'   => 'sent',
+               );
+               $all_invites = bp_members_invitations_get_invites( $all_args );
+               $inviters = array();
+               foreach ( $all_invites as $inv ) {
+                       $inviters[] = bp_core_get_user_displayname( $inv->inviter_id );
+               }
+
+               if ( ! empty( $inviters ) ) {
+                       $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 ) );
+               } else {
+                       $message = __( 'Welcome! You&#8217;ve been invited to join the site. ', 'buddypress' );
+               }
+       }
+
+       return $message;
+}
+
+/**
+ * Provide a more-specific "registration is disabled" message
+ * if registration is available by invitation only.
+ * Also provide failure note if new user is trying to accept
+ * a network invitation but there's a problem.
+ *
+ * @since 8.0.0
+ *
+ * @return string $message The message text.
+ */
+function bp_members_invitations_get_modified_registration_disabled_message() {
+       $message = '';
+       if ( bp_get_members_invitations_allowed() ) {
+
+               $invite = bp_get_members_invitation_from_request();
+               if ( ! $invite->id || ! $invite->invitee_email ) {
+                       return $message;
+               }
+
+               // Check if the user is already a site member.
+               $maybe_user = get_user_by( 'email', $invite->invitee_email );
+
+               if ( ! $maybe_user ) {
+                       $message = __( 'Member registration is allowed by invitation only.', 'buddypress' );
+                       // Is the user trying to accept an invitation but something is wrong?
+                       if ( ! empty( $_GET['inv'] ) ) {
+                               $message .= __( ' It looks like there is a problem with your invitation. Please try again.', 'buddypress' );
+                       }
+               } else if ( 'nouveau' === bp_get_theme_package_id() ) {
+                       $message = sprintf(
+                               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' ),
+                               sprintf( '<a href="%1$s">%2$s</a>', esc_url( wp_login_url( bp_get_root_domain() ) ), esc_html__( 'log in', 'buddypress' ) ),
+                               sprintf( '<a href="%1$s">%2$s</a>', esc_url( wp_lostpassword_url(  bp_get_root_domain() ) ), esc_html__( 'reset it', 'buddypress' ) )
+                       );
+               }
+       }
+       return $message;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Sanitize the invitation property output.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 8.0.0
</span></span></pre></div>
<a id="trunksrcbpmembersbpmembersinvitationsphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/bp-members/bp-members-invitations.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-members/bp-members-invitations.php                           (rev 0)
+++ trunk/src/bp-members/bp-members-invitations.php     2021-04-28 23:51:25 UTC (rev 12923)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,156 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * BuddyPress Membersip Invitations
+ *
+ * @package BuddyPress
+ * @subpackage MembersInvitations
+ * @since 8.0.0
+ */
+
+// Exit if accessed directly.
+defined( 'ABSPATH' ) || exit;
+
+function bp_members_invitations_setup_nav() {
+       if ( ! bp_get_members_invitations_allowed() ) {
+               return;
+       }
+
+       $user_has_access  = bp_user_has_access();
+       $user_can_send    = bp_user_can( bp_displayed_user_id(), 'bp_members_send_invitation' );
+       $user_has_invites = bp_members_invitations_user_has_sent_invites( bp_displayed_user_id() );
+
+       /* Add 'Invitations' to the main user profile navigation */
+       bp_core_new_nav_item(
+               array(
+                       'name'                    => __( 'Invitations', 'buddypress' ),
+                       'slug'                    => bp_get_members_invitations_slug(),
+                       'position'                => 80,
+                       'screen_function'         => 'members_screen_send_invites',
+                       'default_subnav_slug'     => ( $user_can_send && bp_is_my_profile() ) ? 'send-invites' : 'list-invites',
+                       'show_for_displayed_user' => $user_has_access && ( $user_can_send || $user_has_invites )
+               )
+       );
+
+       $parent_link = trailingslashit( bp_displayed_user_domain() . bp_get_members_invitations_slug() );
+
+       /* Create two subnav items for community invitations */
+       bp_core_new_subnav_item(
+               array(
+                       'name'            => __( 'Send Invites', 'buddypress' ),
+                       'slug'            => 'send-invites',
+                       'parent_slug'     => bp_get_members_invitations_slug(),
+                       'parent_url'      => $parent_link,
+                       'screen_function' => 'members_screen_send_invites',
+                       'position'        => 10,
+                       'user_has_access' => $user_has_access && $user_can_send && bp_is_my_profile()
+               )
+       );
+
+       bp_core_new_subnav_item(
+               array(
+                       'name'            => __( 'Pending Invites', 'buddypress' ),
+                       'slug'            => 'list-invites',
+                       'parent_slug'     => bp_get_members_invitations_slug(),
+                       'parent_url'      => $parent_link,
+                       'screen_function' => 'members_screen_list_sent_invites',
+                       'position'        => 20,
+                       'user_has_access' => $user_has_access && ( $user_can_send || $user_has_invites )
+               )
+       );
+}
+add_action( 'bp_setup_nav', 'bp_members_invitations_setup_nav' );
+
+/**
+ * When a user joins the network via an invitation, skip sending the activation email.
+ *
+ * @param bool   $send           Whether or not to send the activation key.
+ * @param int    $user_id        User ID to send activation key to.
+ * @param string $user_email     User email to send activation key to.
+ * @param string $activation_key Activation key to be sent.
+ * @param array  $usermeta       Miscellaneous metadata about the user (blog-specific
+ *                               signup data, xprofile data, etc).
+ */
+function bp_members_invitations_cancel_activation_email( $send, $user_id, $user_email, $activation_key, $usermeta ) {
+       $invite = bp_members_invitations_get_invites(
+               array(
+                       'invitee_email' => $user_email,
+                       'invite_sent'   => 'sent'
+               )
+       );
+
+       if ( $invite ) {
+               $send = false;
+       }
+
+       return $send;
+}
+add_filter( 'bp_core_signup_send_activation_key', 'bp_members_invitations_cancel_activation_email', 10, 5 );
+
+/**
+ * When a user joins the network via an invitation:
+ * - mark all invitations and requests as accepted
+ * - activate the user upon signup
+ *
+ * @param bool|WP_Error   $user_id       True on success, WP_Error on failure.
+ * @param string          $user_login    Login name requested by the user.
+ * @param string          $user_password Password requested by the user.
+ * @param string          $user_email    Email address requested by the user.
+ */
+function bp_members_invitations_complete_signup( $user_id, $user_login, $user_password, $user_email ) {
+       if ( ! $user_id ) {
+               return;
+       }
+
+       // Check to see if this signup is the result of a valid invitation.
+       $invite = bp_get_members_invitation_from_request();
+       if ( ! $invite->id ) {
+               return;
+       }
+
+       // Accept the invitation.
+       $invites_class = new BP_Members_Invitation_Manager();
+       $args          = array(
+               'id' => $invite->id,
+       );
+       $invites_class->accept_invitation( $args );
+
+       // User has already verified their email by responding to the invitation, so we can activate.
+       $key = bp_get_user_meta( $user_id, 'activation_key', true );
+       if ( $key ) {
+               /**
+                * Filters the activation signup.
+                *
+                * @since 1.1.0
+                *
+                * @param bool|int $value Value returned by activation.
+                *                        Integer on success, boolean on failure.
+                */
+               $user = apply_filters( 'bp_core_activate_account', bp_core_activate_signup( $key ) );
+
+               // If there were errors, add a message and redirect.
+               if ( ! empty( $user->errors ) ) {
+                       bp_core_add_message( $user->get_error_message(), 'error' );
+                       bp_core_redirect( trailingslashit( bp_get_root_domain() . '/' . $bp->pages->activate->slug ) );
+               }
+
+               bp_core_add_message( __( 'Your account is now active!', 'buddypress' ) );
+               bp_core_redirect( add_query_arg( 'activated', '1', bp_get_activation_page() ) );
+       }
+}
+add_action( 'bp_core_signup_user', 'bp_members_invitations_complete_signup', 10, 4 );
+
+/**
+ * Delete site membership invitations when an opt-out request is saved.
+ *
+ * @since 8.0.0
+ *
+ * @param BP_Optout $optout Characteristics of the opt-out just saved.
+ */
+function bp_members_invitations_delete_optedout_invites( $optout ) {
+
+       $args = array(
+               'invitee_email' => $optout->email_address,
+       );
+       bp_members_invitations_delete_invites( $args );
+}
+add_action( 'bp_optout_after_save', 'bp_members_invitations_delete_optedout_invites' );
</ins></span></pre>
</div>
</div>

</body>
</html>