<!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][8045] trunk: Swap bp_group_has_invites() internals for BP_Group_Member_Query, and introduce pagination arguments</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/8045">8045</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2014-03-05 14:37:30 +0000 (Wed, 05 Mar 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Swap bp_group_has_invites() internals for BP_Group_Member_Query, and introduce pagination arguments

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

Props boonebgorges, dcavins</pre>

<h3>Modified Paths</h3>
<ul>
<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="trunkbpgroupsbpgroupstemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-template.php (8044 => 8045)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-template.php   2014-03-05 14:37:27 UTC (rev 8044)
+++ trunk/bp-groups/bp-groups-template.php      2014-03-05 14:37:30 UTC (rev 8045)
</span><span class="lines">@@ -3094,9 +3094,56 @@
</span><span class="cx">  var $pag_links;
</span><span class="cx">  var $total_invite_count;
</span><span class="cx"> 
</span><del>-       function __construct( $user_id, $group_id ) {
-               $this->invites      = groups_get_invites_for_group( $user_id, $group_id );
-               $this->invite_count = count( $this->invites );
</del><ins>+        public function __construct( $args = array() ) {
+
+               // Backward compatibility with old method of passing arguments
+               if ( ! is_array( $args ) || func_num_args() > 1 ) {
+                       _deprecated_argument( __METHOD__, '2.0.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
+
+                       $old_args_keys = array(
+                               0  => 'user_id',
+                               1  => 'group_id',
+                       );
+
+                       $func_args = func_get_args();
+                       $args      = bp_core_parse_args_array( $old_args_keys, $func_args );
+               }
+
+               $r = wp_parse_args( $args, array(
+                       'user_id'  => bp_loggedin_user_id(),
+                       'group_id' => bp_get_current_group_id(),
+                       'page'     => 1,
+                       'per_page' => 10,
+               ) );
+
+               $this->pag_num  = intval( $r['per_page'] );
+               $this->pag_page = isset( $_REQUEST['invitepage'] ) ? intval( $_REQUEST['invitepage'] ) : $r['page'];
+
+               $iquery = new BP_Group_Member_Query( array(
+                       'group_id' => $r['group_id'],
+                       'type'     => 'first_joined',
+                       'per_page' => $this->pag_num,
+                       'page'     => $this->pag_page,
+
+                       // These filters ensure we get only pending invites
+                       'is_confirmed' => false,
+                       'inviter_id'   => $r['user_id'],
+               ) );
+               $this->invite_data = $iquery->results;
+
+               $this->total_invite_count = $iquery->total_users;
+               $this->invites                 = array_values( wp_list_pluck( $this->invite_data, 'ID' ) );
+               $this->invite_count       = count( $this->invites );
+
+               $this->pag_links = paginate_links( array(
+                       'base'      => add_query_arg( 'invitepage', '%#%' ),
+                       'format'    => '',
+                       'total'     => ceil( $this->total_invite_count / $this->pag_num ),
+                       'current'   => $this->pag_page,
+                       'prev_text' => '&larr;',
+                       'next_text' => '&rarr;',
+                       'mid_size'  => 1,
+               ) );
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx">  function has_invites() {
</span><span class="lines">@@ -3134,11 +3181,42 @@
</span><span class="cx"> 
</span><span class="cx">  function the_invite() {
</span><span class="cx">          global $group_id;
</span><del>-
</del><span class="cx">           $this->in_the_loop      = true;
</span><span class="cx">          $user_id                = $this->next_invite();
</span><ins>+
</ins><span class="cx">           $this->invite           = new stdClass;
</span><del>-               $this->invite->user     = new BP_Core_User( $user_id );
</del><ins>+                $this->invite->user     = $this->invite_data[ $user_id ];
+
+               // This method previously populated the user object with
+               // BP_Core_User. We manually configure BP_Core_User data for
+               // backward compatibility.
+               if ( bp_is_active( 'xprofile' ) ) {
+                       $this->invite->user->profile_data = BP_XProfile_ProfileData::get_all_for_user( $user_id );
+               }
+
+               $this->invite->user->avatar       = bp_core_fetch_avatar( array( 'item_id' => $user_id, 'type' => 'full', 'alt' => sprintf( __( 'Avatar of %s', 'buddypress' ), $this->invite->user->fullname ) ) );
+               $this->invite->user->avatar_thumb = bp_core_fetch_avatar( array( 'item_id' => $user_id, 'type' => 'thumb', 'alt' => sprintf( __( 'Avatar of %s', 'buddypress' ), $this->invite->user->fullname ) ) );
+               $this->invite->user->avatar_mini  = bp_core_fetch_avatar( array( 'item_id' => $user_id, 'type' => 'thumb', 'alt' => sprintf( __( 'Avatar of %s', 'buddypress' ), $this->invite->user->fullname ), 'width' => 30, 'height' => 30 ) );
+               $this->invite->user->email        = $this->invite->user->user_email;
+               $this->invite->user->user_url     = bp_core_get_user_domain( $user_id, $this->invite->user->user_nicename, $this->invite->user->user_login );
+               $this->invite->user->user_link    = "<a href='{$this->invite->user->user_url}' title='{$this->invite->user->fullname}'>{$this->invite->user->fullname}</a>";
+               $this->invite->user->last_active  = bp_core_get_last_activity( $this->invite->user->last_activity, __( 'active %s', 'buddypress' ) );
+
+               if ( bp_is_active( 'groups' ) ) {
+                       $total_groups = BP_Groups_Member::total_group_count( $user_id );
+                       $this->invite->user->total_groups = sprintf( _n( '%d group', '%d groups', $total_groups, 'buddypress' ), $total_groups );
+               }
+
+               if ( bp_is_active( 'friends' ) ) {
+                       $this->invite->user->total_friends = BP_Friends_Friendship::total_friend_count( $user_id );
+               }
+
+               if ( bp_is_active( 'friends' ) ) {
+                       $this->invite->user->total_friends = BP_Friends_Friendship::total_friend_count( $user_id );
+               }
+
+               $this->invite->user->total_blogs = null;
+
</ins><span class="cx">           $this->invite->group_id = $group_id; // Globaled in bp_group_has_invites()
</span><span class="cx"> 
</span><span class="cx">          if ( 0 == $this->current_invite ) // loop has just started
</span><span class="lines">@@ -3151,7 +3229,9 @@
</span><span class="cx"> 
</span><span class="cx">  $r = wp_parse_args( $args, array(
</span><span class="cx">          'group_id' => false,
</span><del>-               'user_id' => bp_loggedin_user_id()
</del><ins>+                'user_id'  => bp_loggedin_user_id(),
+               'per_page' => 10,
+               'page'     => 1,
</ins><span class="cx">   ) );
</span><span class="cx"> 
</span><span class="cx">  if ( empty( $r['group_id'] ) ) {
</span><span class="lines">@@ -3171,7 +3251,7 @@
</span><span class="cx">          return false;
</span><span class="cx">  }
</span><span class="cx"> 
</span><del>-       $invites_template = new BP_Groups_Invite_Template( $r['user_id'], $r['group_id'] );
</del><ins>+        $invites_template = new BP_Groups_Invite_Template( $r );
</ins><span class="cx">   return apply_filters( 'bp_group_has_invites', $invites_template->has_invites(), $invites_template );
</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 (8044 => 8045)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/testcases/groups/template.php        2014-03-05 14:37:27 UTC (rev 8044)
+++ trunk/tests/testcases/groups/template.php   2014-03-05 14:37:30 UTC (rev 8045)
</span><span class="lines">@@ -475,9 +475,56 @@
</span><span class="cx"> 
</span><span class="cx">                  $this_user = new BP_Core_User( $found_users[ $counter ] );
</span><span class="cx">                  foreach ( get_object_vars( $this_user ) as $k => $v ) {
</span><ins>+                               // Doesn't matter if the backpat provides *more*
+                               // details than the old method, so we skip cases
+                               // where the BP_Core_User value is empty
+                               if ( empty( $v ) ) {
+                                       continue;
+                               }
+
</ins><span class="cx">                           $this->assertEquals( $v, $invites_template->invite->user->{$k} );
</span><span class="cx">                  }
</span><span class="cx">                  $counter++;
</span><span class="cx">          endwhile;
</span><span class="cx">  }
</span><ins>+
+       /**
+        * @group bp_group_has_invites
+        * @group BP_Groups_Invite_Template
+        */
+       public function test_bp_group_has_invites_pagination() {
+               $u1 = $this->create_user( array(
+                       'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 60 ),
+               ) );
+
+               $g = $this->factory->group->create( array(
+                       'creator_id' => $u1,
+               ) );
+
+               $users = array();
+               for ( $i = 1; $i < 15; $i++ ) {
+                       $users[ $i ] = $this->create_user( array(
+                               'last_activity' => gmdate( 'Y-m-d H:i:s', time() - $i ),
+                       ) );
+
+                       $this->add_user_to_group( $users[ $i ], $g, array(
+                               'date_modified' => gmdate( 'Y-m-d H:i:s', time() - $i ),
+                               'is_confirmed' => 0,
+                               'inviter_id' => $u1,
+                               'invite_sent' => true,
+                       ) );
+               }
+
+               // Populate the global
+               bp_group_has_invites( array(
+                       'group_id' => $g,
+                       'user_id' => $u1,
+                       'page' => 2,
+                       'per_page' => 5,
+               ) );
+
+               global $invites_template;
+
+               $this->assertEquals( array( $users[ 9 ], $users[ 8 ], $users[ 7 ], $users[ 6 ], $users[ 5 ], ), $invites_template->invites );
+       }
</ins><span class="cx"> }
</span></span></pre>
</div>
</div>

</body>
</html>