<!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][5680] trunk/bp-members: Clean up notifications code, and prevent notification for inactive components from appearing.</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, #logmsg > ol { margin-left: 0; 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/5680">5680</a></dd>
<dt>Author</dt> <dd>johnjamesjacoby</dd>
<dt>Date</dt> <dd>2012-02-09 21:06:00 +0000 (Thu, 09 Feb 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Clean up notifications code, and prevent notification for inactive components from appearing. Fixes <a href="http://buddypress.trac.wordpress.org/ticket/3650">#3650</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpmembersbpmembersadminbarphp">trunk/bp-members/bp-members-adminbar.php</a></li>
<li><a href="#trunkbpmembersbpmembersnotificationsphp">trunk/bp-members/bp-members-notifications.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpmembersbpmembersadminbarphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-members/bp-members-adminbar.php (5679 => 5680)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-members/bp-members-adminbar.php        2012-02-07 02:31:16 UTC (rev 5679)
+++ trunk/bp-members/bp-members-adminbar.php        2012-02-09 21:06:00 UTC (rev 5680)
</span><span class="lines">@@ -18,7 +18,7 @@
</span><span class="cx">  * @todo Deprecate WP 3.2 admin bar compatibility when we drop 3.2 support
</span><span class="cx">  */
</span><span class="cx"> function bp_members_admin_bar_my_account_menu() {
</span><del>-        global $bp, $wp_admin_bar, $wp_version;
</del><ins>+        global $bp, $wp_admin_bar;
</ins><span class="cx"> 
</span><span class="cx">         // Bail if this is an ajax request
</span><span class="cx">         if ( defined( 'DOING_AJAX' ) )
</span><span class="lines">@@ -32,7 +32,6 @@
</span><span class="cx"> 
</span><span class="cx">                 // Create the main 'My Account' menu
</span><span class="cx">                 $wp_admin_bar-&gt;add_menu( array(
</span><del>-                        //'parent' =&gt; 'my-account',
</del><span class="cx">                         'id'     =&gt; $bp-&gt;my_account_menu_id,
</span><span class="cx">                         'group'  =&gt; true,
</span><span class="cx">                         'title'  =&gt; __( 'Edit My Profile', 'buddypress' ),
</span><span class="lines">@@ -145,13 +144,13 @@
</span><span class="cx">  * @since 1.5
</span><span class="cx">  */
</span><span class="cx"> function bp_members_admin_bar_notifications_menu() {
</span><del>-        global $bp, $wp_admin_bar;
</del><ins>+        global $wp_admin_bar;
</ins><span class="cx"> 
</span><span class="cx">         if ( !is_user_logged_in() )
</span><span class="cx">                 return false;
</span><span class="cx"> 
</span><span class="cx">         $notifications = bp_core_get_notifications_for_user( bp_loggedin_user_id(), 'object' );
</span><del>-        $count         = !empty( $notifications ) ? count( $notifications ) : '0';
</del><ins>+        $count         = !empty( $notifications ) ? count( $notifications ) : 0;
</ins><span class="cx">         $alert_class   = (int) $count &gt; 0 ? 'pending-count alert' : 'count no-alert';
</span><span class="cx">         $menu_title    = '&lt;span id=&quot;ab-pending-notifications&quot; class=&quot;' . $alert_class . '&quot;&gt;' . $count . '&lt;/span&gt;';
</span><span class="cx"> 
</span><span class="lines">@@ -164,7 +163,7 @@
</span><span class="cx">         ) );
</span><span class="cx"> 
</span><span class="cx">         if ( !empty( $notifications ) ) {
</span><del>-                foreach ( (array)$notifications as $notification ) {
</del><ins>+                foreach ( (array) $notifications as $notification ) {
</ins><span class="cx">                         $wp_admin_bar-&gt;add_menu( array(
</span><span class="cx">                                 'parent' =&gt; 'bp-notifications',
</span><span class="cx">                                 'id'     =&gt; 'notification-' . $notification-&gt;id,
</span></span></pre></div>
<a id="trunkbpmembersbpmembersnotificationsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-members/bp-members-notifications.php (5679 => 5680)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-members/bp-members-notifications.php        2012-02-07 02:31:16 UTC (rev 5679)
+++ trunk/bp-members/bp-members-notifications.php        2012-02-09 21:06:00 UTC (rev 5680)
</span><span class="lines">@@ -11,29 +11,47 @@
</span><span class="cx"> // Exit if accessed directly
</span><span class="cx"> if ( !defined( 'ABSPATH' ) ) exit;
</span><span class="cx"> 
</span><ins>+/**
+ * Add a notification for a specific user, from a specific component
+ *
+ * @since BuddyPress (1.0)
+ * @param string $item_id
+ * @param int $user_id
+ * @param string $component_name
+ * @param string $component_action
+ * @param string $secondary_item_id
+ * @param string $date_notified
+ * @return boolean True on success, false on fail
+ */
</ins><span class="cx"> function bp_core_add_notification( $item_id, $user_id, $component_name, $component_action, $secondary_item_id = 0, $date_notified = false ) {
</span><del>-        global $bp;
</del><span class="cx"> 
</span><span class="cx">         if ( empty( $date_notified ) )
</span><span class="cx">                 $date_notified = bp_core_current_time();
</span><span class="cx"> 
</span><del>-        $notification                        = new BP_Core_Notification;
-        $notification-&gt;item_id               = $item_id;
-        $notification-&gt;user_id               = $user_id;
-        $notification-&gt;component_name        = $component_name;
-        $notification-&gt;component_action      = $component_action;
-        $notification-&gt;date_notified         = $date_notified;
-        $notification-&gt;is_new                = 1;
</del><ins>+        $notification                   = new BP_Core_Notification;
+        $notification-&gt;item_id          = $item_id;
+        $notification-&gt;user_id          = $user_id;
+        $notification-&gt;component_name   = $component_name;
+        $notification-&gt;component_action = $component_action;
+        $notification-&gt;date_notified    = $date_notified;
+        $notification-&gt;is_new           = 1;
</ins><span class="cx"> 
</span><span class="cx">         if ( !empty( $secondary_item_id ) )
</span><span class="cx">                 $notification-&gt;secondary_item_id = $secondary_item_id;
</span><span class="cx"> 
</span><del>-        if ( !$notification-&gt;save() )
-                return false;
</del><ins>+        if ( $notification-&gt;save() )
+                return true;
</ins><span class="cx"> 
</span><del>-        return true;
</del><ins>+        return false;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+/**
+ * Delete a specific notification by its ID
+ *
+ * @since BuddyPress (1.0)
+ * @param int $id
+ * @return boolean True on success, false on fail
+ */
</ins><span class="cx"> function bp_core_delete_notification( $id ) {
</span><span class="cx">         if ( !bp_core_check_notification_access( bp_loggedin_user_id(), $id ) )
</span><span class="cx">                 return false;
</span><span class="lines">@@ -41,14 +59,32 @@
</span><span class="cx">         return BP_Core_Notification::delete( $id );
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+/**
+ * Get a specific notification by its ID
+ * 
+ * @since BuddyPress (1.0)
+ * @param int $id
+ * @return BP_Core_Notification 
+ */
</ins><span class="cx"> function bp_core_get_notification( $id ) {
</span><span class="cx">         return new BP_Core_Notification( $id );
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+/**
+ * Get notifications for a specific user
+ * 
+ * @since BuddyPress (1.0)
+ * @global BuddyPress $bp
+ * @param int $user_id
+ * @param string $format
+ * @return boolean Object or array on success, false on fail
+ */
</ins><span class="cx"> function bp_core_get_notifications_for_user( $user_id, $format = 'simple' ) {
</span><span class="cx">         global $bp;
</span><span class="cx"> 
</span><del>-        $notifications = BP_Core_Notification::get_all_for_user( $user_id );
</del><ins>+        $notifications         = BP_Core_Notification::get_all_for_user( $user_id );
+        $grouped_notifications = array(); // Notification groups
+        $renderable            = array(); // Nederable notifications
</ins><span class="cx"> 
</span><span class="cx">         // Group notifications by component and component_action and provide totals
</span><span class="cx">         for ( $i = 0, $count = count( $notifications ); $i &lt; $count; ++$i ) {
</span><span class="lines">@@ -56,29 +92,48 @@
</span><span class="cx">                 $grouped_notifications[$notification-&gt;component_name][$notification-&gt;component_action][] = $notification;
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        // Bail if no notification groups
</ins><span class="cx">         if ( empty( $grouped_notifications ) )
</span><span class="cx">                 return false;
</span><span class="cx"> 
</span><del>-        $renderable = array();
</del><ins>+        // Calculate a renderable output for each notification type
+        foreach ( $grouped_notifications as $component_name =&gt; $action_arrays ) {
</ins><span class="cx"> 
</span><del>-        // Calculate a renderable output for each notification type
-        foreach ( (array)$grouped_notifications as $component_name =&gt; $action_arrays ) {
-                if ( !$action_arrays )
</del><ins>+                // Skip if group is empty
+                if ( empty( $action_arrays ) )
</ins><span class="cx">                         continue;
</span><span class="cx"> 
</span><del>-                foreach ( (array)$action_arrays as $component_action_name =&gt; $component_action_items ) {
-                        $action_item_count = count($component_action_items);
</del><ins>+                // Skip inactive components
+                if ( !bp_is_active( $component_name ) )
+                        continue;
</ins><span class="cx"> 
</span><ins>+                // Loop through each actionable item and try to map it to a component
+                foreach ( (array) $action_arrays as $component_action_name =&gt; $component_action_items ) {
+
+                        // Get the number of actionable items
+                        $action_item_count = count( $component_action_items );
+
+                        // Skip if the count is less than 1
</ins><span class="cx">                         if ( $action_item_count &lt; 1 )
</span><span class="cx">                                 continue;
</span><span class="cx"> 
</span><del>-                        // @deprecated format_notification_function - 1.5
-                        if ( isset( $bp-&gt;{$component_name}-&gt;format_notification_function ) &amp;&amp; function_exists( $bp-&gt;{$component_name}-&gt;format_notification_function ) ) {
-                                $renderable[] = call_user_func( $bp-&gt;{$component_name}-&gt;format_notification_function, $component_action_name, $component_action_items[0]-&gt;item_id, $component_action_items[0]-&gt;secondary_item_id, $action_item_count );
-                        } elseif ( isset( $bp-&gt;{$component_name}-&gt;notification_callback ) &amp;&amp; function_exists( $bp-&gt;{$component_name}-&gt;notification_callback ) ) {
</del><ins>+                        // Callback function exists
+                        if ( isset( $bp-&gt;{$component_name}-&gt;notification_callback ) &amp;&amp; function_exists( $bp-&gt;{$component_name}-&gt;notification_callback ) ) {
+
+                                // Function should return an object
</ins><span class="cx">                                 if ( 'object' == $format ) {
</span><del>-                                        $content = call_user_func( $bp-&gt;{$component_name}-&gt;notification_callback, $component_action_name, $component_action_items[0]-&gt;item_id, $component_action_items[0]-&gt;secondary_item_id, $action_item_count, 'array' );
</del><span class="cx"> 
</span><ins>+                                        // Retrieve the content of the notification using the callback
+                                        $content = call_user_func(
+                                                $bp-&gt;{$component_name}-&gt;notification_callback,
+                                                $component_action_name,
+                                                $component_action_items[0]-&gt;item_id,
+                                                $component_action_items[0]-&gt;secondary_item_id,
+                                                $action_item_count,
+                                                'array'
+                                        );
+
+                                        // Create the object to be returned
</ins><span class="cx">                                         $notification_object = new stdClass;
</span><span class="cx"> 
</span><span class="cx">                                         // Minimal backpat with non-compatible notification
</span><span class="lines">@@ -92,35 +147,101 @@
</span><span class="cx">                                         }
</span><span class="cx"> 
</span><span class="cx">                                         $notification_object-&gt;id = $component_action_items[0]-&gt;id;
</span><ins>+                                        $renderable[]            = $notification_object;
</ins><span class="cx"> 
</span><del>-                                        $renderable[]           = $notification_object;
</del><ins>+                                // Return an array of content strings
</ins><span class="cx">                                 } else {
</span><del>-                                        $content = call_user_func( $bp-&gt;{$component_name}-&gt;notification_callback, $component_action_name, $component_action_items[0]-&gt;item_id, $component_action_items[0]-&gt;secondary_item_id, $action_item_count );
</del><ins>+                                        $content      = call_user_func( $bp-&gt;{$component_name}-&gt;notification_callback, $component_action_name, $component_action_items[0]-&gt;item_id, $component_action_items[0]-&gt;secondary_item_id, $action_item_count );
</ins><span class="cx">                                         $renderable[] = $content;
</span><span class="cx">                                 }
</span><ins>+
+                        // @deprecated format_notification_function - 1.5
+                        } elseif ( isset( $bp-&gt;{$component_name}-&gt;format_notification_function ) &amp;&amp; function_exists( $bp-&gt;{$component_name}-&gt;format_notification_function ) ) {
+                                $renderable[] = call_user_func( $bp-&gt;{$component_name}-&gt;format_notification_function, $component_action_name, $component_action_items[0]-&gt;item_id, $component_action_items[0]-&gt;secondary_item_id, $action_item_count );
</ins><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        return isset( $renderable ) ? $renderable : false;
</del><ins>+        // If renderable is empty array, set to false
+        if ( empty( $renderable ) )
+                $renderable = false;
+
+        // Filter and return
+        return apply_filters( 'bp_core_get_notifications_for_user', $renderable, $user_id, $format );
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+/**
+ * Delete notifications for a user by type
+ * 
+ * Used when clearing out notifications for a specific component when the user
+ * has visited that component.
+ *
+ * @since BuddyPress (1.0)
+ * @param int $user_id
+ * @param string $component_name
+ * @param string $component_action
+ * @return boolean True on success, false on fail
+ */
</ins><span class="cx"> function bp_core_delete_notifications_by_type( $user_id, $component_name, $component_action ) {
</span><span class="cx">         return BP_Core_Notification::delete_for_user_by_type( $user_id, $component_name, $component_action );
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+/**
+ * Delete notifications for an item ID
+ * 
+ * Used when clearing out notifications for a specific component when the user
+ * has visited that component.
+ *
+ * @since BuddyPress (1.0)
+ * @param int $user_id
+ * @param string $component_name
+ * @param string $component_action
+ * @return boolean True on success, false on fail
+ */
</ins><span class="cx"> function bp_core_delete_notifications_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id = false ) {
</span><span class="cx">         return BP_Core_Notification::delete_for_user_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id );
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+/**
+ * Delete all notifications for by type
+ *
+ * Used when clearing out notifications for an entire component
+ *
+ * @since BuddyPress (1.0)
+ * @param int $user_id
+ * @param string $component_name
+ * @param string $component_action
+ * @return boolean True on success, false on fail
+ */
</ins><span class="cx"> function bp_core_delete_all_notifications_by_type( $item_id, $component_name, $component_action = false, $secondary_item_id = false ) {
</span><span class="cx">         return BP_Core_Notification::delete_all_by_type( $item_id, $component_name, $component_action, $secondary_item_id );
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+/**
+ * Delete all notifications for a user
+ *
+ * Used when clearing out all notifications for a user, whene deleted or spammed
+ *
+ * @since BuddyPress (1.0)
+ * @param int $user_id
+ * @param string $component_name
+ * @param string $component_action
+ * @return boolean True on success, false on fail
+ */
</ins><span class="cx"> function bp_core_delete_notifications_from_user( $user_id, $component_name, $component_action ) {
</span><span class="cx">         return BP_Core_Notification::delete_from_user_by_type( $user_id, $component_name, $component_action );
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+/**
+ * Check if a user has access to a specific notification
+ *
+ * Used before deleting a notification for a user
+ *
+ * @since BuddyPress (1.0)
+ * @param int $user_id
+ * @param int $notification_id
+ * @return boolean True on success, false on fail
+ */
</ins><span class="cx"> function bp_core_check_notification_access( $user_id, $notification_id ) {
</span><span class="cx">         if ( !BP_Core_Notification::check_access( $user_id, $notification_id ) )
</span><span class="cx">                 return false;
</span></span></pre>
</div>
</div>

</body>
</html>