Beispiel #1
0
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;
}
/**
 * Delete a specific notification by its ID
 *
 * @deprecated Deprecated since BuddyPress 1.9.0. Use
 *  bp_notifications_delete_notification() instead.
 *
 * @since BuddyPress (1.0)
 * @param int $id
 * @return boolean True on success, false on fail
 */
function bp_core_delete_notification($id)
{
    // Bail if notifications is not active
    if (!bp_is_active('notifications')) {
        return false;
    }
    // Trigger the deprecated function notice
    _deprecated_function(__FUNCTION__, '1.9', 'bp_notifications_delete_notification()');
    return BP_Notifications_Notification::delete_by_id($id);
}
/**
 * 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');
    }
}
Beispiel #4
0
 /**
  * @group cache
  */
 public function test_cache_invalidation_all_for_user_on_update_id()
 {
     $u = $this->factory->user->create();
     $n1 = $this->factory->notification->create(array('component_name' => 'groups', 'user_id' => $u));
     $this->factory->notification->create(array('component_name' => 'messages', 'user_id' => $u));
     // prime cache
     $count = bp_notifications_get_unread_notification_count($u);
     // just to be sure...
     $this->assertEquals(2, $count, 'Cache count should be 2 before invalidation.');
     // mark one notification as read
     BP_Notifications_Notification::update(array('is_new' => false), array('id' => $n1));
     $this->assertFalse(wp_cache_get('all_for_user_' . $u, 'bp_notifications'));
 }
 /**
  * delete notification of a comment perticular commnet
  * @param   int     $comment_id
  */
 function remove_comment_notification($comment_id)
 {
     $comment_notification_id = (int) get_comment_meta($comment_id, 'comment_notification_id', true);
     BP_Notifications_Notification::delete(array('id' => $comment_notification_id));
     delete_comment_meta($comment_id, 'comment_notification_id');
 }
/**
 * Remove a user's notification data.
 *
 * @package Rendez Vous
 * @subpackage Notifications
 *
 * @since Rendez Vous (1.0.0)
 */
function rendez_vous_remove_all_user_notifications($user_id = 0)
{
    if (empty($user_id)) {
        return false;
    }
    // delete all notifications user sent to others
    BP_Notifications_Notification::delete(array('secondary_item_id' => $user_id, 'component_name' => buddypress()->rendez_vous->id));
}
Beispiel #7
0
function apoc_delete_all_notifications()
{
    // Get the user data
    $user_id = $_POST['id'];
    // Delete all notifications
    BP_Notifications_Notification::delete(array('user_id' => $user_id));
    die("1");
}
/**
 * Mark non-mention notifications as read when user visits our read permalink.
 *
 * In particular, 'update_reply' and 'comment_reply' notifications are handled
 * here. See {@link bp_activity_format_notifications()} for more info.
 *
 * @since 2.6.0
 */
function bp_activity_remove_screen_notifications_for_non_mentions()
{
    if (false === is_singular() || false === is_user_logged_in() || empty($_GET['nid'])) {
        return;
    }
    // Mark notification as read.
    BP_Notifications_Notification::update(array('is_new' => false), array('user_id' => bp_loggedin_user_id(), 'id' => (int) $_GET['nid']));
}
 /**
  * @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 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'));
 }
/**
 * 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');
        }
    }
}
 /**
  * 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('&larr;', 'Notifications pagination previous text', 'buddypress'), 'next_text' => _x('&rarr;', 'Notifications pagination next text', 'buddypress'), 'mid_size' => 1, 'add_args' => $add_args));
     }
 }
Beispiel #13
0
function kleo_bp_notification_mark_read()
{
    $response = array();
    if (BP_Notifications_Notification::mark_all_for_user(bp_loggedin_user_id())) {
        $response['status'] = 'success';
    } else {
        $response['status'] = 'failure';
    }
    $notifications = bp_notifications_get_notifications_for_user(bp_loggedin_user_id(), 'object');
    $count = !empty($notifications) ? count($notifications) : 0;
    $response['count'] = $count;
    $response['empty'] = '<li class="kleo-submenu-item">' . __('No new notifications', 'kleo_framework') . '</li>';
    echo json_encode($response);
    exit;
}
/**
 * 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);
}
 /**
  * Deletes all notifications connected to post if it is deleted.
  * @param array|int $post
  * @return void
  */
 public function delete_notifications($post)
 {
     if (is_object($post)) {
         $post_id = $post->ID;
     } elseif (is_array($post)) {
         $post_id = $post['ID'];
     } elseif (is_numeric($post)) {
         $post_id = $post;
     }
     if (isset($post_id)) {
         BP_Notifications_Notification::delete(array('item_id' => $post_id, 'secondary_item_id' => get_current_blog_id(), 'component_name' => $this->id, 'component_action' => 'new_post_' . $post_id));
     }
 }
Beispiel #16
0
/**
 * Delete a notification.
 *
 * @since 1.0.0
 *
 * @param int|bool $user_id The ID of the user associated with the notification.
 * @param int|bool $item_id The ID of the item associated with the notification.
 * @param int|bool $secondary_item_id The ID of the secondary item associated with the notification.
 * @param string|bool $component_action The action associated with the notification.
 */
function crowdmentions_delete_notification($user_id = false, $item_id = false, $secondary_item_id = false, $component_action = false)
{
    $where = array('user_id' => $user_id, 'item_id' => $item_id, 'secondary_item_id' => $secondary_item_id, 'component_action' => $component_action, 'component_name' => crowdmentions_get_component_name());
    BP_Notifications_Notification::delete($where);
}
 /**
  * @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));
 }
/**
 * Delete notifications the user "sent" in case his account was deleted
 *
 * @package WP Idea Stream
 * @subpackage buddypress/notifications
 *
 * @since  2.0.0
 *
 * @param  int $user_id the deleted user id
 * @uses   BP_Notifications_Notification::delete() to delete the notifications
 * @uses   buddypress() to get BuddyPress instance
 * @uses   wp_idea_stream_get_post_type() to get the ideas post type identifier
 */
function wp_idea_stream_buddypress_delete_notifications_by_user($user_id = 0)
{
    if (empty($user_id)) {
        return false;
    }
    // Delete all notification about the rates he gave
    return BP_Notifications_Notification::delete(array('component_name' => buddypress()->ideastream->id, 'component_action' => 'new_' . wp_idea_stream_get_post_type() . '_rate', 'secondary_item_id' => $user_id));
}
 /**
  * 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('&larr;', 'Notifications pagination previous text', 'buddypress'), 'next_text' => _x('&rarr;', '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('&#038;' . $r['page_arg'] . '=1', '', $this->pag_links);
     }
 }
/**
 * Check if a user has access to a specific notification.
 *
 * Used before deleting a notification for a user.
 *
 * @since BuddyPress (1.9.0)
 *
 * @param int $user_id ID of the user being checked.
 * @param int $notification_id ID of the notification being checked.
 * @return bool True if the notification belongs to the user, otherwise false.
 */
function bp_notifications_check_notification_access($user_id, $notification_id)
{
    return (bool) BP_Notifications_Notification::check_access($user_id, $notification_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);
 }
 /**
  * @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);
 }
 /**
  * @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);
 }