/** * Get the notifications from BuddyPress */ function get_notifications() { // Setup notification array $nots = array(); // Count each type $nots['counts'] = array('activity' => 0, 'messages' => 0, 'friends' => 0, 'groups' => 0); // Configure query parameters $args = array('user_id' => $this->user_id, 'is_new' => true, 'page' => '', 'per_page' => '', 'max' => '', 'search_terms' => ''); // Loop through notifications, sorting them by type if (bp_has_notifications($args)) { while (bp_the_notifications()) { bp_the_notification(); // Get the notification global $bp; $not = $bp->notifications->query_loop->notification; // Consolidate action components if ($not->component_name == "forums") { $not->component_name = "activity"; } if ($not->component_name == "events") { $not->component_name = "groups"; } $comp = $not->component_name; // Add a count for each notification type $not->count = 1; // Get the default description $not->desc = 'activity' != $not->component_name ? bp_get_the_notification_description() : ""; // Add notifications to the array $nots[$comp][] = $not; // Increment the count $nots['counts'][$comp]++; } } // Group activity notifications if ($nots['counts']['activity'] > 0) { // Get the activities and setup a combined array $acts = $nots['activity']; $activities = array(); // Loop over activities and group them by item_id for ($i = 0; $i < count($acts); $i++) { // Mentions get grouped, other activities use their item_id $item_id = $acts[$i]->component_action == 'new_at_mention' ? 0 : $acts[$i]->item_id; $acts[$i]->id = $item_id; // Add the activity, or increment the count if (!isset($activities[$item_id])) { $activities[$item_id] = $acts[$i]; } else { $activities[$item_id]->count++; } } // Loop over grouped notifications, apply custom formatting for activities foreach ($nots['activity'] as $id => $not) { // Get the formatted description $nots['activity'][$id]->desc = $this->format_notification($not); } // Replace activities with the grouped ones $nots['activity'] = $activities; } // Return the notifications return $nots; }
<?php if (bp_has_notifications()) { ?> <div id="pag-top" class="pagination no-ajax"> <div class="pag-count" id="notifications-count-top"> <?php bp_notifications_pagination_count(); ?> </div> <div class="pagination-links" id="notifications-pag-top"> <?php bp_notifications_pagination_links(); ?> </div> </div> <div class="read-notifications"> <?php bp_get_template_part('members/single/notifications/notifications-loop'); ?> </div> <div id="pag-bottom" class="pagination no-ajax"> <div class="pag-count" id="notifications-count-bottom"> <?php bp_notifications_pagination_count(); ?> </div>
/** * @group bp_has_notifications */ public function test_bp_has_notifications_filtering() { $u1 = $this->factory->user->create(); $u2 = $this->factory->user->create(); // create a mixture of different notifications $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)); $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)); $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', 'user_id' => $u2)); // assert $this->assertEquals(2, buddypress()->notifications->query_loop->total_notification_count); }