function dln_get_notifications_objects() { if (!is_user_logged_in()) { return false; } $notifications = BP_Notifications_Notification::get(array('user_id' => bp_loggedin_user_id(), 'is_new' => '1', 'component_name' => bp_notifications_get_registered_components())); if (!$notifications) { return; } $count_m = $count_f = $count_a = 0; $arr_m = $arr_f = $arr_a = array(); foreach ($notifications as $i => $nof) { switch ($nof->component_name) { case 'messages': $count_m++; $arr_m[] = $nof; break; case 'friends': $count_f++; $arr_f[] = $nof; break; } } $result = array(); $result['messages'] = array('nof_count' => $count_m, 'nof_arr' => $arr_m); $result['friends'] = array('nof_type' => 'friends', 'nof_count' => $count_f, 'nof_arr' => $arr_f); return $result; }
/** * Invalidate the 'all_for_user_' cache when deleting. * * @since BuddyPress (2.0.0) * * @param int $args Notification deletion arguments. */ function bp_notifications_clear_all_for_user_cache_before_delete($args) { // Pull up a list of items matching the args (those about te be deleted) $ns = BP_Notifications_Notification::get($args); $user_ids = array(); foreach ($ns as $n) { $user_ids[] = $n->user_id; } foreach (array_unique($user_ids) as $user_id) { wp_cache_delete('all_for_user_' . $user_id, 'bp_notifications'); } }
/** * @group bp_notifications_delete_all_notifications_by_type * @group bp_activity_at_mention_delete_notification */ public function test_bp_activity_at_mention_delete_notification() { $this->create_notifications(); $notifications = BP_Notifications_Notification::get(array('item_id' => $this->a1)); // Double check it's there $this->assertEquals(array($this->a1), wp_list_pluck($notifications, 'item_id')); bp_activity_delete(array('id' => $this->a1)); $notifications = BP_Notifications_Notification::get(array('item_id' => $this->a1)); $this->assertEmpty($notifications); }
/** * Constructor method. * * @see bp_has_notifications() For information on the array format. * * @since BuddyPress (1.9.0) * * @param array $args { * An array of arguments. See {@link bp_has_notifications()} * for more details. * } */ public function __construct($args = array()) { // Parse arguments $r = wp_parse_args($args, array('id' => false, 'user_id' => 0, 'item_id' => false, '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_arg' => 'npage', 'page' => 1, 'per_page' => 25, 'max' => null, 'meta_query' => false, 'date_query' => false)); // Overrides // Sort order direction $orders = array('ASC', 'DESC'); if (!empty($_GET['sort_order']) && in_array($_GET['sort_order'], $orders)) { $r['sort_order'] = $_GET['sort_order']; } else { $r['sort_order'] = in_array($r['sort_order'], $orders) ? $r['sort_order'] : 'DESC'; } // Setup variables $this->pag_arg = sanitize_key($r['page_arg']); $this->pag_page = bp_sanitize_pagination_arg($this->pag_arg, $r['page']); $this->pag_num = bp_sanitize_pagination_arg('num', $r['per_page']); $this->user_id = $r['user_id']; $this->is_new = $r['is_new']; $this->search_terms = $r['search_terms']; $this->order_by = $r['order_by']; $this->sort_order = $r['sort_order']; $this->query_vars = array('id' => $r['id'], 'user_id' => $this->user_id, 'item_id' => $r['item_id'], 'secondary_item_id' => $r['secondary_item_id'], 'component_name' => $r['component_name'], 'component_action' => $r['component_action'], 'meta_query' => $r['meta_query'], 'date_query' => $r['date_query'], 'is_new' => $this->is_new, 'search_terms' => $this->search_terms, 'order_by' => $this->order_by, 'sort_order' => $this->sort_order, 'page' => $this->pag_page, 'per_page' => $this->pag_num); // Setup the notifications to loop through $this->notifications = BP_Notifications_Notification::get($this->query_vars); $this->total_notification_count = BP_Notifications_Notification::get_total_count($this->query_vars); if (empty($this->notifications)) { $this->notification_count = 0; $this->total_notification_count = 0; } else { if (!empty($r['max'])) { if ($r['max'] >= count($this->notifications)) { $this->notification_count = count($this->notifications); } else { $this->notification_count = (int) $r['max']; } } else { $this->notification_count = count($this->notifications); } } if ((int) $this->total_notification_count && (int) $this->pag_num) { $add_args = array('sort_order' => $this->sort_order); $this->pag_links = paginate_links(array('base' => add_query_arg($this->pag_arg, '%#%'), 'format' => '', 'total' => ceil((int) $this->total_notification_count / (int) $this->pag_num), 'current' => $this->pag_page, 'prev_text' => _x('←', 'Notifications pagination previous text', 'buddypress'), 'next_text' => _x('→', 'Notifications pagination next text', 'buddypress'), 'mid_size' => 1, 'add_args' => $add_args)); } }
/** * Get all notifications for a user and cache them. * * @since BuddyPress (2.1.0) * * @param int $user_id ID of the user whose notifications are being fetched. * @return array */ function bp_notifications_get_all_notifications_for_user($user_id = 0) { // Default to displayed user if no ID is passed if (empty($user_id)) { $user_id = bp_displayed_user_id() ? bp_displayed_user_id() : bp_loggedin_user_id(); } // Get notifications out of the cache, or query if necessary $notifications = wp_cache_get('all_for_user_' . $user_id, 'bp_notifications'); if (false === $notifications) { $notifications = BP_Notifications_Notification::get(array('user_id' => $user_id)); wp_cache_set('all_for_user_' . $user_id, $notifications, 'bp_notifications'); } /** * Filters all notifications for a user. * * @since BuddyPress (2.1.0) * * @param array $notifications Array of notifications for user. * @param int $user_id ID of the user being fetched. */ return apply_filters('bp_notifications_get_all_notifications_for_user', $notifications, $user_id); }
/** * Mark new message notification when member reads a message thread directly. * * @since BuddyPress (1.9.0) */ function bp_messages_screen_conversation_mark_notifications() { if (bp_is_active('notifications')) { global $thread_template; // get unread PM notifications for the user $new_pm_notifications = BP_Notifications_Notification::get(array('user_id' => bp_loggedin_user_id(), 'component_name' => buddypress()->messages->id, 'component_action' => 'new_message', 'is_new' => 1)); $unread_message_ids = wp_list_pluck($new_pm_notifications, 'item_id'); // no unread PMs, so stop! if (empty($unread_message_ids)) { return; } // get the unread message ids for this thread only $message_ids = array_intersect($unread_message_ids, wp_list_pluck($thread_template->thread->messages, 'id')); // mark each notification for each PM message as read foreach ($message_ids as $message_id) { bp_notifications_mark_notifications_by_item_id(bp_loggedin_user_id(), (int) $message_id, buddypress()->messages->id, 'new_message'); } } }
/** * Get all notifications for a user and cache them. * * @since BuddyPress (2.1.0) * * @param int $user_id ID of the user whose notifications are being fetched. * @return array */ function bp_notifications_get_all_notifications_for_user($user_id = 0) { // Default to displayed user if no ID is passed if (empty($user_id)) { $user_id = bp_displayed_user_id() ? bp_displayed_user_id() : bp_loggedin_user_id(); } // Get notifications out of the cache, or query if necessary $notifications = wp_cache_get('all_for_user_' . $user_id, 'bp_notifications'); if (false === $notifications) { $notifications = BP_Notifications_Notification::get(array('user_id' => $user_id)); wp_cache_set('all_for_user_' . $user_id, $notifications, 'bp_notifications'); } // Filter and return return apply_filters('bp_notifications_get_all_notifications_for_user', $notifications, $user_id); }
/** * @group pagination * @group BP6229 */ public function test_get_paged_sql() { $u = $this->factory->user->create(); $notifications = array(); for ($i = 1; $i <= 6; $i++) { $notifications[] = $this->factory->notification->create(array('component_name' => 'activity', 'secondary_item_id' => $i, 'user_id' => $u, 'is_new' => true)); } $found = BP_Notifications_Notification::get(array('user_id' => $u, 'is_new' => true, 'page' => 2, 'per_page' => 2, 'order_by' => 'id')); // Check that the correct number of items are pulled up $expected = array($notifications[2], $notifications[3]); $this->assertEquals($expected, wp_list_pluck($found, 'id')); }
/** * @group BP7375 */ public function test_membership_request_notifications_should_be_cleared_when_request_is_accepted() { $users = $this->factory->user->create_many(3); $this->add_user_to_group($users[0], $this->group, array('is_admin' => 1)); $this->add_user_to_group($users[1], $this->group, array('is_admin' => 1)); groups_send_membership_request($users[2], $this->group); // Both admins should get a notification. $get_args = array('user_id' => $users[0], 'item_id' => $this->group, 'secondary_item_id' => $users[2], 'component_action' => 'new_membership_request', 'is_new' => true); $u0_notifications = BP_Notifications_Notification::get($get_args); $u1_notifications = BP_Notifications_Notification::get($get_args); $this->assertNotEmpty($u0_notifications); $this->assertNotEmpty($u1_notifications); $this->assertTrue(groups_invite_user(array('user_id' => $users[2], 'group_id' => $this->group))); $u0_notifications = BP_Notifications_Notification::get($get_args); $u1_notifications = BP_Notifications_Notification::get($get_args); $this->assertEmpty($u0_notifications); $this->assertEmpty($u1_notifications); }
/** * Constructor method. * * @see bp_has_notifications() For information on the array format. * * @since BuddyPress (1.9.0) * * @param array $args { * An array of arguments. See {@link bp_has_notifications()} * for more details. * } */ public function __construct($args = array()) { // Parse arguments $r = wp_parse_args($args, array('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')); // Overrides // Set which pagination page if (isset($_GET[$r['page_arg']])) { $r['page'] = intval($_GET[$r['page_arg']]); } // Set the number to show per page if (isset($_GET['num'])) { $r['per_page'] = intval($_GET['num']); } else { $r['per_page'] = intval($r['per_page']); } // Sort order direction $orders = array('ASC', 'DESC'); if (!empty($_GET['sort_order']) && in_array($_GET['sort_order'], $orders)) { $r['sort_order'] = $_GET['sort_order']; } else { $r['sort_order'] = in_array($r['sort_order'], $orders) ? $r['sort_order'] : 'DESC'; } // Setup variables $this->pag_page = $r['page']; $this->pag_num = $r['per_page']; $this->user_id = $r['user_id']; $this->is_new = $r['is_new']; $this->search_terms = $r['search_terms']; $this->page_arg = $r['page_arg']; $this->order_by = $r['order_by']; $this->sort_order = $r['sort_order']; // Setup the notifications to loop through $this->notifications = BP_Notifications_Notification::get($r); $this->total_notification_count = BP_Notifications_Notification::get_total_count($r); if (empty($this->notifications)) { $this->notification_count = 0; $this->total_notification_count = 0; } else { if (!empty($r['max'])) { if ($r['max'] >= count($this->notifications)) { $this->notification_count = count($this->notifications); } else { $this->notification_count = (int) $r['max']; } } else { $this->notification_count = count($this->notifications); } } if ((int) $this->total_notification_count && (int) $this->pag_num) { $this->pag_links = paginate_links(array('base' => add_query_arg($this->page_arg, '%#%'), 'format' => '', 'total' => ceil((int) $this->total_notification_count / (int) $this->pag_num), 'current' => $this->pag_page, 'prev_text' => _x('←', 'Notifications pagination previous text', 'buddypress'), 'next_text' => _x('→', 'Notifications pagination next text', 'buddypress'), 'mid_size' => 1)); // Remove first page from pagination $this->pag_links = str_replace('?' . $r['page_arg'] . '=1', '', $this->pag_links); $this->pag_links = str_replace('&' . $r['page_arg'] . '=1', '', $this->pag_links); } }
/** * Get a count of unread notification items for a user. * * @since BuddyPress (1.9.0) * * @param int $user_id ID of the user whose unread notifications are being * counted. * @return int Unread notification count. */ function bp_notifications_get_unread_notification_count($user_id = 0) { // Default to displayed user if no ID is passed if (empty($user_id)) { $user_id = bp_displayed_user_id() ? bp_displayed_user_id() : bp_loggedin_user_id(); } // Get the notifications, and count them $notifications = wp_cache_get('all_for_user_' . $user_id, 'bp_notifications'); if (false === $notifications) { $notifications = BP_Notifications_Notification::get(array('user_id' => $user_id)); wp_cache_set('all_for_user_' . $user_id, $notifications, 'bp_notifications'); } $count = !empty($notifications) ? count($notifications) : 0; return apply_filters('bp_notifications_get_total_notification_count', $count); }
/** * @group bp_activity_remove_screen_notifications * @group mentions * @ticket BP6687 */ public function test_bp_activity_remove_screen_notifications_on_new_mentions_cleared() { $this->create_notifications(); $notifications = BP_Notifications_Notification::get(array('item_id' => $this->a1)); // Double check it's there $this->assertEquals(array($this->a1), wp_list_pluck($notifications, 'item_id')); $this->assertEquals(1, bp_get_total_mention_count_for_user($this->u1)); // Clear notifications for $this->u1 bp_activity_clear_new_mentions($this->u1); $notifications = BP_Notifications_Notification::get(array('item_id' => $this->a1)); $this->assertEmpty($notifications, 'Notifications should be cleared when new mention metas are removed'); $this->assertEmpty(bp_get_total_mention_count_for_user($this->u1)); }
/** * @ticket BP6329 */ public function test_messages_notifications_should_be_deleted_when_corresponding_message_is_deleted() { if (!bp_is_active('messages')) { $this->markTestSkipped(__METHOD__ . ' requires the Messages component.'); } $u1 = $this->factory->user->create(); $u2 = $this->factory->user->create(); $t1 = messages_new_message(array('sender_id' => $u1, 'recipients' => array($u2), 'subject' => 'A new message', 'content' => 'Hey there!')); // Verify that a notification has been created for the message. $n1 = BP_Notifications_Notification::get(array('component' => 'messages', 'user_id' => $u2)); $this->assertNotEmpty($n1); $this->assertTrue(messages_delete_thread($t1)); $n2 = BP_Notifications_Notification::get(array('component' => 'messages', 'user_id' => $u2)); $this->assertSame(array(), $n2); }
/** * @group bp_groups_delete_promotion_notifications */ public function test_bp_groups_delete_promotion_notifications() { // Dummy group and user IDs $u = 5; $g = 12; // Admin $n = $this->factory->notification->create(array('component_name' => 'groups', 'user_id' => $u, 'item_id' => $g, 'component_action' => 'member_promoted_to_admin')); $notifications = BP_Notifications_Notification::get(array('user_id' => $u)); // Double check it's there $this->assertEquals(array($n), wp_list_pluck($notifications, 'id')); // fire the hook do_action('groups_demoted_member', $u, $g); $notifications = BP_Notifications_Notification::get(array('user_id' => $u)); $this->assertEmpty($notifications); // Mod $n = $this->factory->notification->create(array('component_name' => 'groups', 'user_id' => $u, 'item_id' => $g, 'component_action' => 'member_promoted_to_mod')); $notifications = BP_Notifications_Notification::get(array('user_id' => $u)); // Double check it's there $this->assertEquals(array($n), wp_list_pluck($notifications, 'id')); // fire the hook do_action('groups_demoted_member', $u, $g); $notifications = BP_Notifications_Notification::get(array('user_id' => $u)); $this->assertEmpty($notifications); }