/** * Get notifications, based on provided filter parameters. * * @since 1.9.0 * * @param array $args { * Associative array of arguments. All arguments but $page and * $per_page can be treated as filter values for get_where_sql() * and get_query_clauses(). All items are optional. * @type int|array $id ID of notification being updated. Can be an * array of IDs. * @type int|array $user_id ID of user being queried. Can be an * array of user IDs. * @type int|array $item_id ID of associated item. Can be an array * of multiple item IDs. * @type int|array $secondary_item_id ID of secondary associated * item. Can be an array of multiple IDs. * @type string|array $component_name Name of the component to * filter by. Can be an array of component names. * @type string|array $component_action Name of the action to * filter by. Can be an array of actions. * @type bool $is_new Whether to limit to new notifications. True * returns only new notifications, false returns only non-new * notifications. 'both' returns all. Default: true. * @type string $search_terms Term to match against component_name * or component_action fields. * @type string $order_by Database column to order notifications by. * @type string $sort_order Either 'ASC' or 'DESC'. * @type string $order_by Field to order results by. * @type string $sort_order ASC or DESC. * @type int $page Number of the current page of results. Default: * false (no pagination - all items). * @type int $per_page Number of items to show per page. Default: * false (no pagination - all items). * @type array $meta_query Array of meta_query conditions. See WP_Meta_Query::queries. * @type array $date_query Array of date_query conditions. See first parameter of * WP_Date_Query::__construct(). * @type bool $update_meta_cache Whether to update meta cache. Default: true. * } * @return array Located notifications. */ public static function get($args = array()) { global $wpdb; // Parse the arguments. $r = self::parse_args($args); // Get BuddyPress. $bp = buddypress(); // METADATA. $meta_query_sql = self::get_meta_query_sql($r['meta_query']); // SELECT. $select_sql = "SELECT *"; // FROM. $from_sql = "FROM {$bp->notifications->table_name} n "; // JOIN. $join_sql = $meta_query_sql['join']; // WHERE. $where_sql = self::get_where_sql(array('id' => $r['id'], 'user_id' => $r['user_id'], 'item_id' => $r['item_id'], 'secondary_item_id' => $r['secondary_item_id'], 'component_name' => $r['component_name'], 'component_action' => $r['component_action'], 'is_new' => $r['is_new'], 'search_terms' => $r['search_terms'], 'date_query' => $r['date_query']), $select_sql, $from_sql, $join_sql, $meta_query_sql); // ORDER BY. $order_sql = self::get_order_by_sql(array('order_by' => $r['order_by'], 'sort_order' => $r['sort_order'])); // LIMIT %d, %d. $pag_sql = self::get_paged_sql(array('page' => $r['page'], 'per_page' => $r['per_page'])); // Concatenate query parts. $sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} {$order_sql} {$pag_sql}"; $results = $wpdb->get_results($sql); // Integer casting. foreach ($results as $key => $result) { $results[$key]->id = (int) $results[$key]->id; $results[$key]->user_id = (int) $results[$key]->user_id; $results[$key]->item_id = (int) $results[$key]->item_id; $results[$key]->secondary_item_id = (int) $results[$key]->secondary_item_id; $results[$key]->is_new = (int) $results[$key]->is_new; } // Update meta cache. if (true === $r['update_meta_cache']) { bp_notifications_update_meta_cache(wp_list_pluck($results, 'id')); } return $results; }
/** * @group bp_notifications_update_meta_cache */ public function test_bp_notifications_update_meta_cache() { $u = $this->factory->user->create(); $n1 = $this->factory->notification->create(array('component_name' => 'messages', 'user_id' => $u)); $n2 = $this->factory->notification->create(array('component_name' => 'groups', 'user_id' => $u)); // Add cache for each notification. bp_notifications_update_meta($n1, 'meta', 'data'); bp_notifications_update_meta($n1, 'data', 'meta'); bp_notifications_update_meta($n2, 'meta', 'human'); // Prime cache. bp_notifications_get_meta($n1, 'meta'); // Ensure an empty cache for second notification. wp_cache_delete($n2, 'notification_meta'); // Update notification meta cache. bp_notifications_update_meta_cache(array($n1, $n2)); $expected = array($n1 => array('meta' => array('data'), 'data' => array('meta')), $n2 => array('meta' => array('human'))); $found = array($n1 => wp_cache_get($n1, 'notification_meta'), $n2 => wp_cache_get($n2, 'notification_meta')); $this->assertEquals($expected, $found); }