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; }
/** * Get the last notification id for the user * * @global type $wpdb * @param type $user_id * @return type */ function bpln_get_latest_notification_id($user_id = false) { if (!$user_id) { $user_id = get_current_user_id(); } global $wpdb; $bp = buddypress(); $table = $bp->notifications->table_name; $registered_components = bp_notifications_get_registered_components(); $components_list = array(); foreach ($registered_components as $component) { $components_list[] = $wpdb->prepare('%s', $component); } $components_list = implode(',', $components_list); $query = "SELECT MAX(id) FROM {$table} WHERE user_id = %d AND component_name IN ({$components_list}) AND is_new = %d "; $query = $wpdb->prepare($query, $user_id, 1); return (int) $wpdb->get_var($query); }
/** * 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)); } }
/** * Initialize the notifications loop. * * Based on the $args passed, bp_has_notifications() populates * buddypress()->notifications->query_loop global, enabling the use of BP * templates and template functions to display a list of notifications. * * @since 1.9.0 * * @param array|string $args { * Arguments for limiting the contents of the notifications loop. Can be * passed as an associative array, or as a URL query string. * * See {@link BP_Notifications_Notification::get()} for detailed * information on the arguments. In addition, also supports: * * @type int $max Optional. Max items to display. Default: false. * @type string $page_arg URL argument to use for pagination. * Default: 'npage'. * } * @return bool */ function bp_has_notifications($args = '') { // Get the default is_new argument. if (bp_is_current_action('unread')) { $is_new = 1; } elseif (bp_is_current_action('read')) { $is_new = 0; // Not on a notifications page? default to fetch new notifications. } else { $is_new = 1; } // Get the user ID. if (bp_displayed_user_id()) { $user_id = bp_displayed_user_id(); } else { $user_id = bp_loggedin_user_id(); } // Parse the args. $r = bp_parse_args($args, array('id' => false, 'user_id' => $user_id, 'secondary_item_id' => false, 'component_name' => bp_notifications_get_registered_components(), 'component_action' => false, 'is_new' => $is_new, 'search_terms' => isset($_REQUEST['s']) ? stripslashes($_REQUEST['s']) : '', 'order_by' => 'date_notified', 'sort_order' => 'DESC', 'meta_query' => false, 'date_query' => false, 'page' => 1, 'per_page' => 25, 'max' => false, 'page_arg' => 'npage'), 'has_notifications'); // Get the notifications. $query_loop = new BP_Notifications_Template($r); // Setup the global query loop. buddypress()->notifications->query_loop = $query_loop; /** * Filters whether or not the user has notifications to display. * * @since 1.9.0 * * @param bool $value Whether or not there are notifications to display. * @param BP_Notifications_Template $query_loop BP_Notifications_Template object instance. */ return apply_filters('bp_has_notifications', $query_loop->has_notifications(), $query_loop); }
/** * Parse notifications query arguments. * * @since 2.3.0 * * @param mixed $args Args to parse. * @return array */ public static function parse_args($args = '') { return wp_parse_args($args, array('id' => false, 'user_id' => false, 'item_id' => false, 'secondary_item_id' => false, 'component_name' => bp_notifications_get_registered_components(), 'component_action' => false, 'is_new' => true, 'search_terms' => '', 'order_by' => false, 'sort_order' => false, 'page' => false, 'per_page' => false, 'meta_query' => false, 'date_query' => false, 'update_meta_cache' => true)); }
/** * Get a count of total notifications matching a set of arguments. * * @since BuddyPress (1.9.0) * * @see BP_Notifications_Notification::get() for a description of * arguments. * * @param array $args See {@link BP_Notifications_Notification::get()}. * @return int Count of located items. */ public static function get_total_count($args) { global $wpdb; /** * Default component_name to active_components * * @see http://buddypress.trac.wordpress.org/ticket/5300 */ $args = wp_parse_args($args, array('component_name' => bp_notifications_get_registered_components())); // Load BuddyPress $bp = buddypress(); // Build the query $select_sql = "SELECT COUNT(*)"; $from_sql = "FROM {$bp->notifications->table_name}"; $where_sql = self::get_where_sql($args); $sql = "{$select_sql} {$from_sql} {$where_sql}"; // Return the queried results return $wpdb->get_var($sql); }
/** * 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); } }