<!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][8069] trunk: Replace internals of BP_Group_Membership_Requests_Template with BP_Group_Member_Query</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/8069">8069</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2014-03-06 19:58:49 +0000 (Thu, 06 Mar 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Replace internals of BP_Group_Membership_Requests_Template with BP_Group_Member_Query

This brings us more in line with the Invites template functions, and is another
step toward centralizing all of our group queries.

The change also allows for true pagination. This changeset also adds the 'page'
parameter to the stack.

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

Props dcavins</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpgroupsbpgroupsclassesphp">trunk/bp-groups/bp-groups-classes.php</a></li>
<li><a href="#trunkbpgroupsbpgroupstemplatephp">trunk/bp-groups/bp-groups-template.php</a></li>
<li><a href="#trunkteststestcasesgroupstemplatephp">trunk/tests/testcases/groups/template.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 (8068 => 8069)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-classes.php    2014-03-06 19:50:21 UTC (rev 8068)
+++ trunk/bp-groups/bp-groups-classes.php       2014-03-06 19:58:49 UTC (rev 8069)
</span><span class="lines">@@ -1454,6 +1454,8 @@
</span><span class="cx">  *   inviter_id != 0 and invite_sent = 1
</span><span class="cx">  * - *Pending and unsent invitations* are entries with is_confirmed = 0 and
</span><span class="cx">  *   inviter_id != 0 and invite_sent = 0
</span><ins>+ * - *Membership requests* are entries with is_confirmed = 0 and
+ *   inviter_id = 0 (and invite_sent = 0)
</ins><span class="cx">  *
</span><span class="cx">  * @since BuddyPress (1.8.0)
</span><span class="cx">  *
</span><span class="lines">@@ -1722,14 +1724,22 @@
</span><span class="cx">          global $wpdb;
</span><span class="cx"> 
</span><span class="cx">          $bp     = buddypress();
</span><del>-               $extras = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, date_modified, is_banned FROM {$bp->groups->table_name_members} WHERE user_id IN ({$user_ids_sql}) AND group_id = %d", $this->query_vars['group_id'] ) );
</del><ins>+                $extras = $wpdb->get_results( $wpdb->prepare( "SELECT id, user_id, date_modified, is_admin, is_mod, comments, user_title, invite_sent, is_confirmed, inviter_id, is_banned FROM {$bp->groups->table_name_members} WHERE user_id IN ({$user_ids_sql}) AND group_id = %d", $this->query_vars['group_id'] ) );
</ins><span class="cx"> 
</span><span class="cx">          foreach ( (array) $extras as $extra ) {
</span><span class="cx">                  if ( isset( $this->results[ $extra->user_id ] ) ) {
</span><span class="cx">                          // user_id is provided for backward compatibility
</span><span class="cx">                          $this->results[ $extra->user_id ]->user_id       = (int) $extra->user_id;
</span><ins>+                               $this->results[ $extra->user_id ]->is_admin      = (int) $extra->is_admin;
+                               $this->results[ $extra->user_id ]->is_mod        = (int) $extra->is_mod;
</ins><span class="cx">                           $this->results[ $extra->user_id ]->is_banned     = (int) $extra->is_banned;
</span><span class="cx">                          $this->results[ $extra->user_id ]->date_modified = $extra->date_modified;
</span><ins>+                               $this->results[ $extra->user_id ]->user_title    = $extra->user_title;
+                               $this->results[ $extra->user_id ]->comments      = $extra->comments;
+                               $this->results[ $extra->user_id ]->invite_sent   = (int) $extra->invite_sent;
+                               $this->results[ $extra->user_id ]->inviter_id    = (int) $extra->inviter_id;
+                               $this->results[ $extra->user_id ]->is_confirmed  = (int) $extra->is_confirmed;
+                               $this->results[ $extra->user_id ]->membership_id = (int) $extra->id;
</ins><span class="cx">                   }
</span><span class="cx">          }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkbpgroupsbpgroupstemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-template.php (8068 => 8069)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-template.php   2014-03-06 19:50:21 UTC (rev 8068)
+++ trunk/bp-groups/bp-groups-template.php      2014-03-06 19:58:49 UTC (rev 8069)
</span><span class="lines">@@ -2936,6 +2936,7 @@
</span><span class="cx">   *           are being queried. Default: current group id.
</span><span class="cx">   *     @type int $per_page Number of records to return per page of
</span><span class="cx">   *           results. Default: 10.
</span><ins>+        *     @type int $page Page of results to show. Default: 1.
</ins><span class="cx">    *     @type int $max Max items to return. Default: false (show all)
</span><span class="cx">   * }
</span><span class="cx">   */
</span><span class="lines">@@ -2958,20 +2959,45 @@
</span><span class="cx">          $r = wp_parse_args( $args, array(
</span><span class="cx">                  'group_id' => bp_get_current_group_id(),
</span><span class="cx">                  'per_page' => 10,
</span><del>-                       'max' => false,
</del><ins>+                        'page'     => 1,
+                       'max'      => false,
+                       'type'     => 'first_joined',
</ins><span class="cx">           ) );
</span><span class="cx"> 
</span><del>-               $this->pag_page = isset( $_REQUEST['mrpage'] ) ? intval( $_REQUEST['mrpage'] ) : 1;
</del><ins>+                $this->pag_page = isset( $_REQUEST['mrpage'] ) ? intval( $_REQUEST['mrpage'] ) : $r['page'];
</ins><span class="cx">           $this->pag_num  = isset( $_REQUEST['num'] ) ? intval( $_REQUEST['num'] ) : $r['per_page'];
</span><del>-               $this->requests = BP_Groups_Group::get_membership_requests( $r['group_id'], $this->pag_num, $this->pag_page );
</del><span class="cx"> 
</span><del>-               if ( !$r['max'] || $r['max'] >= (int) $this->requests['total'] )
-                       $this->total_request_count = (int) $this->requests['total'];
</del><ins>+                $mquery = new BP_Group_Member_Query( array(
+                       'group_id' => $r['group_id'],
+                       'type'     => $r['type'],
+                       'per_page' => $this->pag_num,
+                       'page'     => $this->pag_page,
+
+                       // These filters ensure we only get pending requests
+                       'is_confirmed' => false,
+                       'inviter_id'   => 0,
+               ) );
+
+               $this->requests      = array_values( $mquery->results );
+               $this->request_count = count( $this->requests );
+
+               // Compatibility with legacy format of request data objects
+               foreach ( $this->requests as $rk => $rv ) {
+                       // For legacy reasons, the 'id' property of each
+                       // request must match the membership id, not the ID of
+                       // the user (as it's returned by BP_Group_Member_Query)
+                       $this->requests[ $rk ]->user_id = $rv->ID;
+                       $this->requests[ $rk ]->id      = $rv->membership_id;
+
+                       // Miscellaneous values
+                       $this->requests[ $rk ]->group_id   = $r['group_id'];
+               }
+
+               if ( !$r['max'] || $r['max'] >= (int) $mquery->total_users )
+                       $this->total_request_count = (int) $mquery->total_users;
</ins><span class="cx">           else
</span><span class="cx">                  $this->total_request_count = (int) $r['max'];
</span><span class="cx"> 
</span><del>-               $this->requests = $this->requests['requests'];
-
</del><span class="cx">           if ( $r['max'] ) {
</span><span class="cx">                  if ( $r['max'] >= count($this->requests) )
</span><span class="cx">                          $this->request_count = count($this->requests);
</span><span class="lines">@@ -3035,12 +3061,24 @@
</span><span class="cx">  }
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+/**
+ * Initialize a group membership request template loop.
+ *
+ * @param array $args {
+ *     @type int $group_id ID of the group. Defaults to current group.
+ *     @type int $per_page Number of records to return per page. Default: 10.
+ *     @type int $page Page of results to return. Default: 1.
+ *     @type int $max Max number of items to return. Default: false.
+ * }
+ * @return bool True if there are requests, otherwise false.
+ */
</ins><span class="cx"> function bp_group_has_membership_requests( $args = '' ) {
</span><span class="cx">  global $requests_template, $groups_template;
</span><span class="cx"> 
</span><span class="cx">  $defaults = array(
</span><span class="cx">          'group_id' => $groups_template->group->id,
</span><span class="cx">          'per_page' => 10,
</span><ins>+               'page'     => 1,
</ins><span class="cx">           'max'      => false
</span><span class="cx">  );
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkteststestcasesgroupstemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/tests/testcases/groups/template.php (8068 => 8069)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/testcases/groups/template.php        2014-03-06 19:50:21 UTC (rev 8068)
+++ trunk/tests/testcases/groups/template.php   2014-03-06 19:58:49 UTC (rev 8069)
</span><span class="lines">@@ -547,11 +547,14 @@
</span><span class="cx">          $memberships = array();
</span><span class="cx">          for ( $i = 1; $i < 15; $i++ ) {
</span><span class="cx">                  $users[ $i ] = $this->create_user( array(
</span><del>-                               'last_activity' => gmdate( 'Y-m-d H:i:s', time() - $i ),
</del><ins>+                                'last_activity' => gmdate( 'Y-m-d H:i:s', time() - ( 100 - $i ) ),
</ins><span class="cx">                   ) );
</span><span class="cx"> 
</span><span class="cx">                  $memberships[ $i ] = $this->add_user_to_group( $users[ $i ], $g, array(
</span><del>-                               'date_modified' => gmdate( 'Y-m-d H:i:s', time() - $i ),
</del><ins>+                                // this date_modified ensures that order will match
+                               // id order. necessary due to a quirk in the legacy
+                               // implementation
+                               'date_modified' => gmdate( 'Y-m-d H:i:s', time() - ( 100 - $i ) ),
</ins><span class="cx">                           'is_confirmed' => 0,
</span><span class="cx">                          'inviter_id' => 0,
</span><span class="cx">                          'invite_sent' => false,
</span></span></pre>
</div>
</div>

</body>
</html>