/** * @group get_total_count * @ticket BP5300 */ public function test_get_total_count_with_component_name() { $u = $this->factory->user->create(); $n1 = $this->factory->notification->create(array('component_name' => 'groups', 'user_id' => $u)); $n2 = $this->factory->notification->create(array('component_name' => 'groups', 'user_id' => $u)); $n3 = $this->factory->notification->create(array('component_name' => 'messages', 'user_id' => $u)); $n = BP_Notifications_Notification::get_total_count(array('user_id' => $u, 'component_name' => array('messages'))); $this->assertEquals(1, $n); }
/** * 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)); } }
/** * 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); } }