/** * Constructor method. * * @since BuddyPress (1.9.0) * * @param array $args { * @type int $user_id ID of the user to whom the displayed * notifications belong. * @type bool $is_new Whether to limit the query to unread * notifications. Default: true. * @type int $page Number of the page of results to return. * Will be overridden by URL parameter. Default: 1. * @type int $per_page Number of results to return per page. * Will be overridden by URL parameter. Default: 25. * @type int $max Optional. Max results to display. * @type string $search_terms Optional. Term to match against * component_name and component_action. * @type string $page_arg URL argument to use for pagination. * Default: 'npage'. * } */ public function __construct($args = array()) { // Parse arguments $r = wp_parse_args($args, array('user_id' => 0, 'is_new' => true, 'page' => 1, 'per_page' => 25, 'order_by' => 'date_notified', 'sort_order' => 'DESC', 'max' => null, 'search_terms' => '', 'page_arg' => 'npage')); // Overrides // Set which pagination page if (isset($_GET[$r['page_arg']])) { $pag_page = intval($_GET[$r['page_arg']]); } else { $pag_page = $r['page']; } // Set the number to show per page if (isset($_GET['num'])) { $pag_num = intval($_GET['num']); } else { $pag_num = intval($r['per_page']); } // Sort order direction $orders = array('ASC', 'DESC'); if (!empty($_GET['sort_order']) && in_array($_GET['sort_order'], $orders)) { $sort_order = $_GET['sort_order']; } else { $sort_order = in_array($r['sort_order'], $orders) ? $r['sort_order'] : 'DESC'; } // Setup variables $this->pag_page = $pag_page; $this->pag_num = $pag_num; $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 = $sort_order; // Get the notifications $notifications = BP_Notifications_Notification::get_current_notifications_for_user(array('user_id' => $this->user_id, 'is_new' => $this->is_new, 'page' => $this->pag_page, 'per_page' => $this->pag_num, 'search_terms' => $this->search_terms, 'order_by' => $this->order_by, 'sort_order' => $this->sort_order)); // Setup the notifications to loop through $this->notifications = $notifications['notifications']; $this->total_notification_count = $notifications['total']; if (empty($this->notifications)) { $this->notification_count = 0; $this->total_notification_count = 0; } else { if (!empty($max)) { if ($max >= count($this->notifications)) { $this->notification_count = count($this->notifications); } else { $this->notification_count = (int) $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); } }