<!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][8761] trunk: Allow notifications loop to accept all available parameters.</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/8761">8761</a></dd>
<dt>Author</dt> <dd>r-a-y</dd>
<dt>Date</dt> <dd>2014-08-05 22:40:49 +0000 (Tue, 05 Aug 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Allow notifications loop to accept all available parameters.

Previously, the notifications loop accepted only a subset of parameters
from the `BP_Notification_Notification::get()` method.  This made
notification loop filtering very restrictive.

This commit opens up the notifications loop so all parameters are available
as per the `BP_Notifications_Notification::get()` fetch method.

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

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpnotificationsbpnotificationsclassesphp">trunk/src/bp-notifications/bp-notifications-classes.php</a></li>
<li><a href="#trunksrcbpnotificationsbpnotificationstemplatephp">trunk/src/bp-notifications/bp-notifications-template.php</a></li>
<li><a href="#trunktestsphpunittestcasesnotificationsfunctionsphp">trunk/tests/phpunit/testcases/notifications/functions.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpnotificationsbpnotificationsclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/src/bp-notifications/bp-notifications-classes.php (8760 => 8761)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/bp-notifications/bp-notifications-classes.php  2014-08-05 16:26:40 UTC (rev 8760)
+++ trunk/src/bp-notifications/bp-notifications-classes.php     2014-08-05 22:40:49 UTC (rev 8761)
</span><span class="lines">@@ -542,6 +542,8 @@
</span><span class="cx">   *           notifications. 'both' returns all. Default: true.
</span><span class="cx">   *     @type string $search_terms Term to match against component_name
</span><span class="cx">   *           or component_action fields.
</span><ins>+        *     @type string $order_by Database column to order notifications by.
+        *     @type string $sort_order Either 'ASC' or 'DESC'.
</ins><span class="cx">    *     @type string $order_by Field to order results by.
</span><span class="cx">   *     @type string $sort_order ASC or DESC.
</span><span class="cx">   *     @type int $page Number of the current page of results. Default:
</span></span></pre></div>
<a id="trunksrcbpnotificationsbpnotificationstemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/src/bp-notifications/bp-notifications-template.php (8760 => 8761)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/bp-notifications/bp-notifications-template.php 2014-08-05 16:26:40 UTC (rev 8760)
+++ trunk/src/bp-notifications/bp-notifications-template.php    2014-08-05 22:40:49 UTC (rev 8761)
</span><span class="lines">@@ -220,87 +220,69 @@
</span><span class="cx">  /**
</span><span class="cx">   * Constructor method.
</span><span class="cx">   *
</span><ins>+        * @see bp_has_notifications() For information on the array format.
+        *
</ins><span class="cx">    * @since BuddyPress (1.9.0)
</span><span class="cx">   *
</span><span class="cx">   * @param array $args {
</span><del>-        *     @type int $user_id ID of the user to whom the displayed
-        *           notifications belong.
-        *     @type bool $is_new Whether to limit the query to unread
-        *           notifications. Default: true.
-        *     @type int $page Number of the page of results to return.
-        *           Will be overridden by URL parameter. Default: 1.
-        *     @type int $per_page Number of results to return per page.
-        *           Will be overridden by URL parameter. Default: 25.
-        *     @type int $max Optional. Max results to display.
-        *     @type string $search_terms Optional. Term to match against
-        *           component_name and component_action.
-        *     @type string $page_arg URL argument to use for pagination.
-        *           Default: 'npage'.
</del><ins>+         *     An array of arguments. See {@link bp_has_notifications()}
+        *     for more details.
</ins><span class="cx">    * }
</span><span class="cx">   */
</span><span class="cx">  public function __construct( $args = array() ) {
</span><span class="cx"> 
</span><span class="cx">          // Parse arguments
</span><span class="cx">          $r = wp_parse_args( $args, array(
</span><del>-                       'user_id'      => 0,
-                       'is_new'       => true,
-                       'page'         => 1,
-                       'per_page'     => 25,
-                       'order_by'     => 'date_notified',
-                       'sort_order'   => 'DESC',
-                       'max'          => null,
-                       'search_terms' => '',
-                       'page_arg'     => 'npage',
</del><ins>+                        'id'                => false,
+                       'user_id'           => 0,
+                       'secondary_item_id' => false,
+                       'component_name'    => bp_notifications_get_registered_components(),
+                       'component_action'  => false,
+                       'is_new'            => true,
+                       'search_terms'      => '',
+                       'order_by'          => 'date_notified',
+                       'sort_order'        => 'DESC',
+                       'page'              => 1,
+                       'per_page'          => 25,
+                       'max'               => null,
+                       'page_arg'          => 'npage',
</ins><span class="cx">           ) );
</span><span class="cx"> 
</span><span class="cx">          // Overrides
</span><span class="cx"> 
</span><span class="cx">          // Set which pagination page
</span><span class="cx">          if ( isset( $_GET[ $r['page_arg'] ] ) ) {
</span><del>-                       $pag_page = intval( $_GET[ $r['page_arg'] ] );
-               } else {
-                       $pag_page = $r['page'];
</del><ins>+                        $r['page'] = intval( $_GET[ $r['page_arg'] ] );
</ins><span class="cx">           }
</span><span class="cx"> 
</span><span class="cx">          // Set the number to show per page
</span><span class="cx">          if ( isset( $_GET['num'] ) ) {
</span><del>-                       $pag_num = intval( $_GET['num'] );
</del><ins>+                        $r['per_page'] = intval( $_GET['num'] );
</ins><span class="cx">           } else {
</span><del>-                       $pag_num = intval( $r['per_page'] );
</del><ins>+                        $r['per_page'] = intval( $r['per_page'] );
</ins><span class="cx">           }
</span><span class="cx"> 
</span><span class="cx">          // Sort order direction
</span><span class="cx">          $orders = array( 'ASC', 'DESC' );
</span><span class="cx">          if ( ! empty( $_GET['sort_order'] ) && in_array( $_GET['sort_order'], $orders ) ) {
</span><del>-                       $sort_order = $_GET['sort_order'];
</del><ins>+                        $r['sort_order'] = $_GET['sort_order'];
</ins><span class="cx">           } else {
</span><del>-                       $sort_order = in_array( $r['sort_order'], $orders ) ? $r['sort_order'] : 'DESC';
</del><ins>+                        $r['sort_order'] = in_array( $r['sort_order'], $orders ) ? $r['sort_order'] : 'DESC';
</ins><span class="cx">           }
</span><span class="cx"> 
</span><span class="cx">          // Setup variables
</span><del>-               $this->pag_page     = $pag_page;
-               $this->pag_num      = $pag_num;
</del><ins>+                $this->pag_page     = $r['page'];
+               $this->pag_num      = $r['per_page'];
</ins><span class="cx">           $this->user_id      = $r['user_id'];
</span><span class="cx">          $this->is_new       = $r['is_new'];
</span><span class="cx">          $this->search_terms = $r['search_terms'];
</span><span class="cx">          $this->page_arg     = $r['page_arg'];
</span><span class="cx">          $this->order_by     = $r['order_by'];
</span><del>-               $this->sort_order   = $sort_order;
</del><ins>+                $this->sort_order   = $r['sort_order'];
</ins><span class="cx"> 
</span><del>-               // Get the notifications
-               $notifications      = BP_Notifications_Notification::get_current_notifications_for_user( array(
-                       'user_id'      => $this->user_id,
-                       'is_new'       => $this->is_new,
-                       'page'         => $this->pag_page,
-                       'per_page'     => $this->pag_num,
-                       'search_terms' => $this->search_terms,
-                       'order_by'     => $this->order_by,
-                       'sort_order'   => $this->sort_order,
-               ) );
-
</del><span class="cx">           // Setup the notifications to loop through
</span><del>-               $this->notifications            = $notifications['notifications'];
-               $this->total_notification_count = $notifications['total'];
</del><ins>+                $this->notifications            = BP_Notifications_Notification::get( $r );
+               $this->total_notification_count = BP_Notifications_Notification::get_total_count( $r );
</ins><span class="cx"> 
</span><span class="cx">          if ( empty( $this->notifications ) ) {
</span><span class="cx">                  $this->notification_count       = 0;
</span><span class="lines">@@ -447,16 +429,11 @@
</span><span class="cx">  * @param array $args {
</span><span class="cx">  *     Arguments for limiting the contents of the notifications loop. Can be
</span><span class="cx">  *     passed as an associative array, or as a URL query string.
</span><del>- *     @type int $user_id ID of the user to whom notifications belong. Default:
- *           ID of the logged-in user.
- *     @type bool $is_new Whether to limit query to unread notifications.
- *           Default: when viewing the 'unread' tab, defaults to true; when
- *           viewing the 'read' tab, defaults to false.
- *     @type int $page The page of notifications being fetched. Default: 1.
- *     @type int $per_page Number of items to display on a page. Default: 25.
</del><ins>+ *
+ *     See {@link BP_Notifications_Notification::get()} for detailed
+ *     information on the arguments.  In addition, also supports:
+ *
</ins><span class="cx">  *     @type int $max Optional. Max items to display. Default: false.
</span><del>- *     @type string $search_terms Optional. Term to match against
- *           component_name and component_action.
</del><span class="cx">  *     @type string $page_arg URL argument to use for pagination.
</span><span class="cx">  *           Default: 'npage'.
</span><span class="cx">  * }
</span><span class="lines">@@ -468,6 +445,10 @@
</span><span class="cx">          $is_new = 1;
</span><span class="cx">  } elseif ( bp_is_current_action( 'read' ) ) {
</span><span class="cx">          $is_new = 0;
</span><ins>+
+       // not on a notifications page? default to fetch new notifications
+       } else {
+               $is_new = 1;
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx">  // Get the user ID
</span><span class="lines">@@ -479,13 +460,22 @@
</span><span class="cx"> 
</span><span class="cx">  // Parse the args
</span><span class="cx">  $r = bp_parse_args( $args, array(
</span><del>-               'user_id'      => $user_id,
-               'is_new'       => $is_new,
-               'page'         => 1,
-               'per_page'     => 25,
-               'max'          => false,
-               'search_terms' => isset( $_REQUEST['s'] ) ? stripslashes( $_REQUEST['s'] ) : '',
-               'page_arg'     => 'npage',
</del><ins>+                'id'                => false,
+               'user_id'           => $user_id,
+               'secondary_item_id' => false,
+               'component_name'    => bp_notifications_get_registered_components(),
+               'component_action'  => false,
+               'is_new'            => $is_new,
+               'search_terms'      => isset( $_REQUEST['s'] ) ? stripslashes( $_REQUEST['s'] ) : '',
+               'order_by'          => 'date_notified',
+               'sort_order'        => 'DESC',
+               'page'              => 1,
+               'per_page'          => 25,
+
+               // these are additional arguments that are not available in
+               // BP_Notifications_Notification::get()
+               'max'               => false,
+               'page_arg'          => 'npage',
</ins><span class="cx">   ), 'has_notifications' );
</span><span class="cx"> 
</span><span class="cx">  // Get the notifications
</span></span></pre></div>
<a id="trunktestsphpunittestcasesnotificationsfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/testcases/notifications/functions.php (8760 => 8761)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/testcases/notifications/functions.php        2014-08-05 16:26:40 UTC (rev 8760)
+++ trunk/tests/phpunit/testcases/notifications/functions.php   2014-08-05 22:40:49 UTC (rev 8761)
</span><span class="lines">@@ -120,4 +120,48 @@
</span><span class="cx">          // assert
</span><span class="cx">          $this->assertEquals( 0, $n );
</span><span class="cx">  }
</span><ins>+
+       /**
+        * @group bp_has_notifications
+        */
+       public function test_bp_has_notifications_filtering() {
+               $u1 = $this->create_user();
+               $u2 = $this->create_user();
+
+               // create a mixture of different notifications
+               $n1 = $this->factory->notification->create( array(
+                       'component_name'    => 'messages',
+                       'component_action'  => 'new_message',
+                       'item_id'           => 99,
+                       'user_id'           => $u2,
+                       'secondary_item_id' => $u1,
+                       'is_new'            => true,
+               ) );
+
+               $n2 = $this->factory->notification->create( array(
+                       'component_name'    => 'activity',
+                       'component_action'  => 'new_at_mention',
+                       'item_id'           => 99,
+                       'user_id'           => $u2,
+                       'secondary_item_id' => $u1,
+                       'is_new'            => true,
+               ) );
+
+               $n3 = $this->factory->notification->create( array(
+                       'component_name'    => 'activity',
+                       'component_action'  => 'new_at_mention',
+                       'item_id'           => 100,
+                       'user_id'           => $u2,
+                       'secondary_item_id' => $u1,
+                       'is_new'            => true,
+               ) );
+
+               // now fetch only activity notifications
+               bp_has_notifications( array(
+                       'component_name' => 'activity',
+               ) );
+
+               // assert
+               $this->assertEquals( 2, buddypress()->notifications->query_loop->total_notification_count );
+       }
</ins><span class="cx"> }
</span></span></pre>
</div>
</div>

</body>
</html>