<!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][7442] trunk: Improve UX flow around overlapping group invites and requests</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 { 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">
<dt>Revision</dt> <dd><a href="http://buddypress.trac.wordpress.org/changeset/7442">7442</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2013-10-17 19:00:46 +0000 (Thu, 17 Oct 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Improve UX flow around overlapping group invites and requests

This changeset introduces a number of improvements to the flow surrounding
invitations and membership requests to private groups:

- When a user has previously requested membership in a group, add a note to
  that effect when the user is being invited to the group via the Send Invites
  interface.
- When a user has previously requested membership in a group, automatically
  accept that invitation and add the user to the group when an invitation is
  sent by an existing group member.
- When a user has previously been invited to a group, buttons in the group
  directory and in the group header should read Accept Invitation instead of
  Request Membership

The end result is a more logical user experience with regard to joining private
groups.

Fixes <a href="http://buddypress.trac.wordpress.org/ticket/5032">#5032</a>

Props Mike_Cowobo</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpgroupsbpgroupsclassesphp">trunk/bp-groups/bp-groups-classes.php</a></li>
<li><a href="#trunkbpgroupsbpgroupsfunctionsphp">trunk/bp-groups/bp-groups-functions.php</a></li>
<li><a href="#trunkbpgroupsbpgroupsloaderphp">trunk/bp-groups/bp-groups-loader.php</a></li>
<li><a href="#trunkbpgroupsbpgroupsscreensphp">trunk/bp-groups/bp-groups-screens.php</a></li>
<li><a href="#trunkbpgroupsbpgroupstemplatephp">trunk/bp-groups/bp-groups-template.php</a></li>
<li><a href="#trunkbptemplatesbplegacybuddypressfunctionsphp">trunk/bp-templates/bp-legacy/buddypress-functions.php</a></li>
<li><a href="#trunkbpthemesbpdefault_incajaxphp">trunk/bp-themes/bp-default/_inc/ajax.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpgroupsbpgroupsclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-classes.php (7441 => 7442)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-classes.php    2013-10-17 19:00:42 UTC (rev 7441)
+++ trunk/bp-groups/bp-groups-classes.php       2013-10-17 19:00:46 UTC (rev 7442)
</span><span class="lines">@@ -33,6 +33,22 @@
</span><span class="cx">  public $is_member;
</span><span class="cx"> 
</span><span class="cx">  /**
</span><ins>+        * Does the current user have an outstanding invitation to this group?
+        *
+        * @since BuddyPress (1.9.0)
+        * @var bool
+        */
+       public $is_invited;
+
+       /**
+        * Does the current user have a pending membership request to this group?
+        *
+        * @since BuddyPress (1.9.0)
+        * @var bool
+        */
+       public $is_pending;
+
+       /**
</ins><span class="cx">    * Timestamp of the last activity that happened in this group.
</span><span class="cx">   *
</span><span class="cx">   * @since BuddyPress (1.2)
</span><span class="lines">@@ -72,6 +88,8 @@
</span><span class="cx">                  $this->last_activity      = groups_get_groupmeta( $this->id, 'last_activity' );
</span><span class="cx">                  $this->total_member_count = groups_get_groupmeta( $this->id, 'total_member_count' );
</span><span class="cx">                  $this->is_member          = BP_Groups_Member::check_is_member( bp_loggedin_user_id(), $this->id );
</span><ins>+                       $this->is_invited         = BP_Groups_Member::check_has_invite( bp_loggedin_user_id(), $this->id );
+                       $this->is_pending         = BP_Groups_Member::check_for_membership_request( bp_loggedin_user_id(), $this->id );
</ins><span class="cx"> 
</span><span class="cx">                  // If this is a private or hidden group, does the current user have access?
</span><span class="cx">                  if ( 'private' == $this->status || 'hidden' == $this->status ) {
</span><span class="lines">@@ -827,15 +845,32 @@
</span><span class="cx">          // Sanitize group IDs
</span><span class="cx">          $group_ids = implode( ',', wp_parse_id_list( $group_ids ) );
</span><span class="cx"> 
</span><del>-               // Fetch the logged in users status within each group
-               $user_status = $wpdb->get_col( $wpdb->prepare( "SELECT group_id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id IN ( {$group_ids} ) AND is_confirmed = 1 AND is_banned = 0", bp_loggedin_user_id() ) );
</del><ins>+                // Fetch the logged-in user's status within each group
+               $user_status = $wpdb->get_results( $wpdb->prepare( "SELECT group_id, is_confirmed, invite_sent FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id IN ( {$group_ids} ) AND is_banned = 0", bp_loggedin_user_id() ) );
+
</ins><span class="cx">           for ( $i = 0, $count = count( $paged_groups ); $i < $count; ++$i ) {
</span><span class="cx">                  $paged_groups[$i]->is_member = false;
</span><span class="cx"> 
</span><del>-                       foreach ( (array) $user_status as $group_id ) {
-                               if ( $group_id == $paged_groups[$i]->id ) {
-                                       $paged_groups[$i]->is_member = true;
</del><ins>+                        foreach ( (array) $user_status as $group ) {
+                               $is_member = $is_invited = $is_pending = false;
+                               if ( $group->group_id == $paged_groups[ $i ]->id ) {
+                                       // is_confirmed means the user is a member
+                                       if ( $group->is_confirmed ) {
+                                               $is_member = true;
+
+                                       // invite_sent means the user has been invited
+                                       } else if ( $group->invite_sent ) {
+                                               $is_invited = true;
+
+                                       // User has sent request, but has not been confirmed
+                                       } else {
+                                               $is_pending = true;
+                                       }
</ins><span class="cx">                           }
</span><ins>+
+                               $paged_groups[ $i ]->is_member = $is_member;
+                               $paged_groups[ $i ]->is_invited = $is_invited;
+                               $paged_groups[ $i ]->is_pending = $is_pending;
</ins><span class="cx">                   }
</span><span class="cx">          }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkbpgroupsbpgroupsfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-functions.php (7441 => 7442)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-functions.php  2013-10-17 19:00:42 UTC (rev 7441)
+++ trunk/bp-groups/bp-groups-functions.php     2013-10-17 19:00:46 UTC (rev 7442)
</span><span class="lines">@@ -654,7 +654,12 @@
</span><span class="cx">  if ( empty( $user_id ) || empty( $group_id ) )
</span><span class="cx">          return false;
</span><span class="cx"> 
</span><del>-       if ( !groups_is_user_member( $user_id, $group_id ) && !groups_check_user_has_invite( $user_id, $group_id, 'all' ) ) {
</del><ins>+        // if the user has already requested membership, accept the request
+       if ( $membership_id = groups_check_for_membership_request( $user_id, $group_id ) ) {
+               groups_accept_membership_request( $membership_id, $user_id, $group_id );
+
+       // Otherwise, create a new invitation
+       } else if ( ! groups_is_user_member( $user_id, $group_id ) && ! groups_check_user_has_invite( $user_id, $group_id, 'all' ) ) {
</ins><span class="cx">           $invite                = new BP_Groups_Member;
</span><span class="cx">          $invite->group_id      = $group_id;
</span><span class="cx">          $invite->user_id       = $user_id;
</span><span class="lines">@@ -872,6 +877,12 @@
</span><span class="cx">  if ( groups_is_user_member( $requesting_user_id, $group_id ) || groups_is_user_banned( $requesting_user_id, $group_id ) )
</span><span class="cx">          return false;
</span><span class="cx"> 
</span><ins>+       // Check if the user is already invited - if so, simply accept invite
+       if ( groups_check_user_has_invite( $requesting_user_id, $group_id ) ) {
+               groups_accept_invite( $requesting_user_id, $group_id );
+               return true;
+       }
+
</ins><span class="cx">   $requesting_user                = new BP_Groups_Member;
</span><span class="cx">  $requesting_user->group_id      = $group_id;
</span><span class="cx">  $requesting_user->user_id       = $requesting_user_id;
</span></span></pre></div>
<a id="trunkbpgroupsbpgroupsloaderphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-loader.php (7441 => 7442)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-loader.php     2013-10-17 19:00:42 UTC (rev 7441)
+++ trunk/bp-groups/bp-groups-loader.php        2013-10-17 19:00:46 UTC (rev 7442)
</span><span class="lines">@@ -412,13 +412,17 @@
</span><span class="cx">                          'item_css_id'     => 'home'
</span><span class="cx">                  );
</span><span class="cx"> 
</span><del>-                       // If this is a private group, and the user is not a member, show a "Request Membership" nav item.
</del><ins>+                        // If this is a private group, and the user is not a
+                       // member and does not have an outstanding invitation,
+                       // show a "Request Membership" nav item.
</ins><span class="cx">                   if ( is_user_logged_in() &&
</span><del>-                                !bp_current_user_can( 'bp_moderate' ) &&
-                                !$this->current_group->is_user_member &&
-                                !groups_check_for_membership_request( bp_loggedin_user_id(), $this->current_group->id ) &&
-                                $this->current_group->status == 'private'
</del><ins>+                                 ! bp_current_user_can( 'bp_moderate' ) &&
+                                ! $this->current_group->is_user_member &&
+                                ! groups_check_for_membership_request( bp_loggedin_user_id(), $this->current_group->id ) &&
+                                $this->current_group->status == 'private' &&
+                                ! groups_check_user_has_invite( bp_loggedin_user_id(), $this->current_group->id )
</ins><span class="cx">                           ) {
</span><ins>+
</ins><span class="cx">                           $sub_nav[] = array(
</span><span class="cx">                                  'name'               => __( 'Request Membership', 'buddypress' ),
</span><span class="cx">                                  'slug'               => 'request-membership',
</span></span></pre></div>
<a id="trunkbpgroupsbpgroupsscreensphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-screens.php (7441 => 7442)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-screens.php    2013-10-17 19:00:42 UTC (rev 7441)
+++ trunk/bp-groups/bp-groups-screens.php       2013-10-17 19:00:46 UTC (rev 7442)
</span><span class="lines">@@ -479,6 +479,15 @@
</span><span class="cx">  if ( 'private' != $bp->groups->current_group->status )
</span><span class="cx">          return false;
</span><span class="cx"> 
</span><ins>+       // If the user is already invited, accept invitation
+       if ( groups_check_user_has_invite( bp_loggedin_user_id(), $bp->groups->current_group->id ) ) {
+               if ( groups_accept_invite( bp_loggedin_user_id(), $bp->groups->current_group->id ) )
+                       bp_core_add_message( __( 'Group invite accepted', 'buddypress' ) );
+               else
+                       bp_core_add_message( __( 'There was an error accepting the group invitation, please try again.', 'buddypress' ), 'error' );
+               bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) );
+       }
+
</ins><span class="cx">   // If the user has submitted a request, send it.
</span><span class="cx">  if ( isset( $_POST['group-request-send']) ) {
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkbpgroupsbpgroupstemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-template.php (7441 => 7442)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-template.php   2013-10-17 19:00:42 UTC (rev 7441)
+++ trunk/bp-groups/bp-groups-template.php      2013-10-17 19:00:46 UTC (rev 7442)
</span><span class="lines">@@ -1780,23 +1780,25 @@
</span><span class="cx"> 
</span><span class="cx">                          case 'private' :
</span><span class="cx"> 
</span><del>-                                       // Member has not requested membership yet
-                                       if ( !bp_group_has_requested_membership( $group ) ) {
</del><ins>+                                        // Member has outstanding invitation -
+                                       // show an "Accept Invitation" button
+                                       if ( $group->is_invited ) {
</ins><span class="cx">                                           $button = array(
</span><del>-                                                       'id'                => 'request_membership',
</del><ins>+                                                        'id'                => 'accept_invite',
</ins><span class="cx">                                                   'component'         => 'groups',
</span><span class="cx">                                                  'must_be_logged_in' => true,
</span><span class="cx">                                                  'block_self'        => false,
</span><span class="cx">                                                  'wrapper_class'     => 'group-button ' . $group->status,
</span><span class="cx">                                                  'wrapper_id'        => 'groupbutton-' . $group->id,
</span><del>-                                                       'link_href'         => wp_nonce_url( bp_get_group_permalink( $group ) . 'request-membership', 'groups_request_membership' ),
-                                                       'link_text'         => __( 'Request Membership', 'buddypress' ),
-                                                       'link_title'        => __( 'Request Membership', 'buddypress' ),
-                                                       'link_class'        => 'group-button request-membership',
</del><ins>+                                                        'link_href'         => add_query_arg( 'redirect_to', bp_get_group_permalink( $group ), bp_get_group_accept_invite_link( $group ) ),
+                                                       'link_text'         => __( 'Accept Invitation', 'buddypress' ),
+                                                       'link_title'        => __( 'Accept Invitation', 'buddypress' ),
+                                                       'link_class'        => 'group-button accept-invite',
</ins><span class="cx">                                           );
</span><span class="cx"> 
</span><del>-                                       // Member has requested membership already
-                                       } else {
</del><ins>+                                        // Member has requested membership but request is pending -
+                                       // show a "Request Sent" button
+                                       } elseif ( $group->is_pending ) {
</ins><span class="cx">                                           $button = array(
</span><span class="cx">                                                  'id'                => 'membership_requested',
</span><span class="cx">                                                  'component'         => 'groups',
</span><span class="lines">@@ -1809,6 +1811,22 @@
</span><span class="cx">                                                  'link_title'        => __( 'Request Sent', 'buddypress' ),
</span><span class="cx">                                                  'link_class'        => 'group-button pending membership-requested',
</span><span class="cx">                                          );
</span><ins>+
+                                       // Member has not requested membership yet -
+                                       // show a "Request Membership" button
+                                       } else {
+                                               $button = array(
+                                                       'id'                => 'request_membership',
+                                                       'component'         => 'groups',
+                                                       'must_be_logged_in' => true,
+                                                       'block_self'        => false,
+                                                       'wrapper_class'     => 'group-button ' . $group->status,
+                                                       'wrapper_id'        => 'groupbutton-' . $group->id,
+                                                       'link_href'         => wp_nonce_url( bp_get_group_permalink( $group ) . 'request-membership', 'groups_request_membership' ),
+                                                       'link_text'         => __( 'Request Membership', 'buddypress' ),
+                                                       'link_title'        => __( 'Request Membership', 'buddypress' ),
+                                                       'link_class'        => 'group-button request-membership',
+                                               );
</ins><span class="cx">                                   }
</span><span class="cx"> 
</span><span class="cx">                                  break;
</span></span></pre></div>
<a id="trunkbptemplatesbplegacybuddypressfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-templates/bp-legacy/buddypress-functions.php (7441 => 7442)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-templates/bp-legacy/buddypress-functions.php    2013-10-17 19:00:42 UTC (rev 7441)
+++ trunk/bp-templates/bp-legacy/buddypress-functions.php       2013-10-17 19:00:46 UTC (rev 7442)
</span><span class="lines">@@ -906,11 +906,27 @@
</span><span class="cx">  if ( ! friends_check_friendship( bp_loggedin_user_id(), $_POST['friend_id'] ) )
</span><span class="cx">          return;
</span><span class="cx"> 
</span><ins>+       $group_id = (int) $_POST['group_id'];
+       $friend_id = (int) $_POST['friend_id'];
+
</ins><span class="cx">   if ( 'invite' == $_POST['friend_action'] ) {
</span><del>-               if ( ! groups_invite_user( array( 'user_id' => $_POST['friend_id'], 'group_id' => $_POST['group_id'] ) ) )
</del><ins>+                $group = groups_get_group( $group_id );
+
+               // Users who have previously requested membership do not need
+               // another invitation created for them
+               if ( BP_Groups_Member::check_for_membership_request( $friend_id, $group_id ) ) {
+                       $user_status = 'is_pending';
+
+               // Create the user invitation
+               } else if ( groups_invite_user( array( 'user_id' => $friend_id, 'group_id' => $group_id ) ) ) {
+                       $user_status = 'is_invited';
+
+               // Miscellaneous failure
+               } else {
</ins><span class="cx">                   return;
</span><ins>+               }
</ins><span class="cx"> 
</span><del>-               $user = new BP_Core_User( $_POST['friend_id'] );
</del><ins>+                $user = new BP_Core_User( $friend_id );
</ins><span class="cx"> 
</span><span class="cx">          echo '<li id="uid-' . $user->id . '">';
</span><span class="cx">          echo $user->avatar_thumb;
</span><span class="lines">@@ -919,13 +935,26 @@
</span><span class="cx">          echo '<div class="action">
</span><span class="cx">                          <a class="button remove" href="' . wp_nonce_url( bp_loggedin_user_domain() . bp_get_groups_slug() . '/' . $_POST['group_id'] . '/invites/remove/' . $user->id, 'groups_invite_uninvite_user' ) . '" id="uid-' . esc_attr( $user->id ) . '">' . __( 'Remove Invite', 'buddypress' ) . '</a>
</span><span class="cx">                    </div>';
</span><ins>+
+               if ( 'is_pending' == $user_status ) {
+                       echo '<p class="description">' . sprintf( __( '%s has previously requested to join this group. Sending an invitation will automatically add the member to the group.', 'buddypress' ), $user->user_link ) . '</p>';
+               }
+
</ins><span class="cx">           echo '</li>';
</span><span class="cx">          exit;
</span><span class="cx"> 
</span><span class="cx">  } elseif ( 'uninvite' == $_POST['friend_action'] ) {
</span><del>-               if ( ! groups_uninvite_user( $_POST['friend_id'], $_POST['group_id'] ) )
</del><ins>+                // Users who have previously requested membership should not
+               // have their requests deleted on the "uninvite" action
+               if ( BP_Groups_Member::check_for_membership_request( $friend_id, $group_id ) ) {
</ins><span class="cx">                   return;
</span><ins>+               }
</ins><span class="cx"> 
</span><ins>+               // Remove the unsent invitation
+               if ( ! groups_uninvite_user( $friend_id, $group_id ) ) {
+                       return;
+               }
+
</ins><span class="cx">           exit;
</span><span class="cx"> 
</span><span class="cx">  } else {
</span><span class="lines">@@ -1055,12 +1084,27 @@
</span><span class="cx">                  }
</span><span class="cx"> 
</span><span class="cx">          } elseif ( 'private' == $group->status ) {
</span><del>-                       check_ajax_referer( 'groups_request_membership' );
</del><span class="cx"> 
</span><del>-                       if ( ! groups_send_membership_request( bp_loggedin_user_id(), $group->id ) ) {
-                               _e( 'Error requesting membership', 'buddypress' );
</del><ins>+                        // If the user has already been invited, then this is
+                       // an Accept Invitation button
+                       if ( groups_check_user_has_invite( bp_loggedin_user_id(), $group->id ) ) {
+                               check_ajax_referer( 'groups_accept_invite' );
+
+                               if ( ! groups_accept_invite( bp_loggedin_user_id(), $group->id ) ) {
+                                       _e( 'Error requesting membership', 'buddypress' );
+                               } else {
+                                       echo '<a id="group-' . esc_attr( $group->id ) . '" class="leave-group" rel="leave" title="' . __( 'Leave Group', 'buddypress' ) . '" href="' . wp_nonce_url( bp_get_group_permalink( $group ) . 'leave-group', 'groups_leave_group' ) . '">' . __( 'Leave Group', 'buddypress' ) . '</a>';
+                               }
+
+                       // Otherwise, it's a Request Membership button
</ins><span class="cx">                   } else {
</span><del>-                               echo '<a id="group-' . esc_attr( $group->id ) . '" class="membership-requested" rel="membership-requested" title="' . __( 'Membership Requested', 'buddypress' ) . '" href="' . bp_get_group_permalink( $group ) . '">' . __( 'Membership Requested', 'buddypress' ) . '</a>';
</del><ins>+                                check_ajax_referer( 'groups_request_membership' );
+
+                               if ( ! groups_send_membership_request( bp_loggedin_user_id(), $group->id ) ) {
+                                       _e( 'Error requesting membership', 'buddypress' );
+                               } else {
+                                       echo '<a id="group-' . esc_attr( $group->id ) . '" class="membership-requested" rel="membership-requested" title="' . __( 'Membership Requested', 'buddypress' ) . '" href="' . bp_get_group_permalink( $group ) . '">' . __( 'Membership Requested', 'buddypress' ) . '</a>';
+                               }
</ins><span class="cx">                   }
</span><span class="cx">          }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkbpthemesbpdefault_incajaxphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-themes/bp-default/_inc/ajax.php (7441 => 7442)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-themes/bp-default/_inc/ajax.php 2013-10-17 19:00:42 UTC (rev 7441)
+++ trunk/bp-themes/bp-default/_inc/ajax.php    2013-10-17 19:00:46 UTC (rev 7442)
</span><span class="lines">@@ -568,9 +568,25 @@
</span><span class="cx">  if ( ! friends_check_friendship( bp_loggedin_user_id(), $_POST['friend_id'] ) )
</span><span class="cx">          return;
</span><span class="cx"> 
</span><ins>+       $group_id = (int) $_POST['group_id'];
+       $friend_id = (int) $_POST['friend_id'];
+
</ins><span class="cx">   if ( 'invite' == $_POST['friend_action'] ) {
</span><del>-               if ( ! groups_invite_user( array( 'user_id' => $_POST['friend_id'], 'group_id' => $_POST['group_id'] ) ) )
</del><ins>+                $group = groups_get_group( $group_id );
+
+               // Users who have previously requested membership do not need
+               // another invitation created for them
+               if ( BP_Groups_Member::check_for_membership_request( $friend_id, $group_id ) ) {
+                       $user_status = 'is_pending';
+
+               // Create the user invitation
+               } else if ( groups_invite_user( array( 'user_id' => $friend_id, 'group_id' => $group_id ) ) ) {
+                       $user_status = 'is_invited';
+
+               // Miscellaneous failure
+               } else {
</ins><span class="cx">                   return;
</span><ins>+               }
</ins><span class="cx"> 
</span><span class="cx">          $user = new BP_Core_User( $_POST['friend_id'] );
</span><span class="cx"> 
</span><span class="lines">@@ -581,13 +597,26 @@
</span><span class="cx">          echo '<div class="action">
</span><span class="cx">                          <a class="button remove" href="' . wp_nonce_url( bp_loggedin_user_domain() . bp_get_groups_slug() . '/' . $_POST['group_id'] . '/invites/remove/' . $user->id, 'groups_invite_uninvite_user' ) . '" id="uid-' . esc_attr( $user->id ) . '">' . __( 'Remove Invite', 'buddypress' ) . '</a>
</span><span class="cx">                    </div>';
</span><ins>+
+               if ( 'is_pending' == $user_status ) {
+                       echo '<p class="description">' . sprintf( __( '%s has previously requested to join this group. Sending an invitation will automatically add the member to the group.', 'buddypress' ), $user->user_link ) . '</p>';
+               }
+
</ins><span class="cx">           echo '</li>';
</span><span class="cx">          exit;
</span><span class="cx"> 
</span><span class="cx">  } elseif ( 'uninvite' == $_POST['friend_action'] ) {
</span><del>-               if ( ! groups_uninvite_user( $_POST['friend_id'], $_POST['group_id'] ) )
</del><ins>+                // Users who have previously requested membership should not
+               // have their requests deleted on the "uninvite" action
+               if ( BP_Groups_Member::check_for_membership_request( $friend_id, $group_id ) ) {
</ins><span class="cx">                   return;
</span><ins>+               }
</ins><span class="cx"> 
</span><ins>+               // Remove the unsent invitation
+               if ( ! groups_uninvite_user( $friend_id, $group_id ) ) {
+                       return;
+               }
+
</ins><span class="cx">           exit;
</span><span class="cx"> 
</span><span class="cx">  } else {
</span><span class="lines">@@ -716,12 +745,27 @@
</span><span class="cx">                  }
</span><span class="cx"> 
</span><span class="cx">          } elseif ( 'private' == $group->status ) {
</span><del>-                       check_ajax_referer( 'groups_request_membership' );
</del><span class="cx"> 
</span><del>-                       if ( ! groups_send_membership_request( bp_loggedin_user_id(), $group->id ) ) {
-                               _e( 'Error requesting membership', 'buddypress' );
</del><ins>+                        // If the user has already been invited, then this is
+                       // an Accept Invitation button
+                       if ( groups_check_user_has_invite( bp_loggedin_user_id(), $group->id ) ) {
+                               check_ajax_referer( 'groups_accept_invite' );
+
+                               if ( ! groups_accept_invite( bp_loggedin_user_id(), $group->id ) ) {
+                                       _e( 'Error requesting membership', 'buddypress' );
+                               } else {
+                                       echo '<a id="group-' . esc_attr( $group->id ) . '" class="leave-group" rel="leave" title="' . __( 'Leave Group', 'buddypress' ) . '" href="' . wp_nonce_url( bp_get_group_permalink( $group ) . 'leave-group', 'groups_leave_group' ) . '">' . __( 'Leave Group', 'buddypress' ) . '</a>';
+                               }
+
+                       // Otherwise, it's a Request Membership button
</ins><span class="cx">                   } else {
</span><del>-                               echo '<a id="group-' . esc_attr( $group->id ) . '" class="membership-requested" rel="membership-requested" title="' . __( 'Membership Requested', 'buddypress' ) . '" href="' . bp_get_group_permalink( $group ) . '">' . __( 'Membership Requested', 'buddypress' ) . '</a>';
</del><ins>+                                check_ajax_referer( 'groups_request_membership' );
+
+                               if ( ! groups_send_membership_request( bp_loggedin_user_id(), $group->id ) ) {
+                                       _e( 'Error requesting membership', 'buddypress' );
+                               } else {
+                                       echo '<a id="group-' . esc_attr( $group->id ) . '" class="membership-requested" rel="membership-requested" title="' . __( 'Membership Requested', 'buddypress' ) . '" href="' . bp_get_group_permalink( $group ) . '">' . __( 'Membership Requested', 'buddypress' ) . '</a>';
+                               }
</ins><span class="cx">                   }
</span><span class="cx">          }
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>