/** * @covers ::bbp_forum_query_topic_ids */ public function test_bbp_forum_query_topic_ids() { $f = $this->factory->forum->create(); $t1 = $this->factory->topic->create(array('post_parent' => $f, 'topic_meta' => array('forum_id' => $f))); $t2 = $this->factory->topic->create(array('post_parent' => $f, 'topic_meta' => array('forum_id' => $f))); $t3 = $this->factory->topic->create(array('post_parent' => $f, 'topic_meta' => array('forum_id' => $f))); $this->assertEqualSets(array($t1, $t2, $t3), bbp_forum_query_topic_ids($f)); }
/** * Returns the forum's last reply id * * @since bbPress (r2908) * * @param int $forum_id Forum id * @param int $topic_ids Optional. Topic ids * @uses wp_cache_get() To check for cache and retrieve it * @uses bbp_forum_query_topic_ids() To get the forum's topic ids * @uses wpdb::prepare() To prepare the query * @uses wpdb::get_var() To execute the query and get the var back * @uses bbp_get_reply_post_type() To get the reply post type * @uses wp_cache_set() To set the cache for future use * @uses apply_filters() Calls 'bbp_forum_query_last_reply_id' with the reply id * and forum id */ function bbp_forum_query_last_reply_id($forum_id, $topic_ids = 0) { global $wpdb; $cache_id = 'bbp_get_forum_' . $forum_id . '_reply_id'; $reply_id = wp_cache_get($cache_id, 'bbpress_posts'); if (false === $reply_id) { if (empty($topic_ids)) { $topic_ids = bbp_forum_query_topic_ids($forum_id); } if (!empty($topic_ids)) { $topic_ids = implode(',', wp_parse_id_list($topic_ids)); $reply_id = (int) $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_parent IN ( {$topic_ids} ) AND post_status = '%s' AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", bbp_get_public_status_id(), bbp_get_reply_post_type())); wp_cache_set($cache_id, $reply_id, 'bbpress_posts'); // May be (int) 0 } else { wp_cache_set($cache_id, '0', 'bbpress_posts'); } } return (int) apply_filters('bbp_get_forum_last_reply_id', (int) $reply_id, $forum_id); }
/** * Returns the forum's last reply id * * @since 2.0.0 bbPress (r2908) * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects * * @param int $forum_id Forum id. * @param int $topic_ids Optional. Topic ids. * @uses bbp_get_forum_id() To validate the forum id * @uses bbp_forum_query_topic_ids() To get the forum's topic ids * @uses bbp_get_public_status_id() To get the public status id * @uses bbp_get_reply_post_type() To get the reply post type * @uses apply_filters() Calls 'bbp_forum_query_last_reply_id' with the reply id * and forum id */ function bbp_forum_query_last_reply_id($forum_id = 0, $topic_ids = 0) { // Validate forum $forum_id = bbp_get_forum_id($forum_id); // Get topic ID's if none were passed if (empty($topic_ids)) { $topic_ids = bbp_forum_query_topic_ids($forum_id); } $query = new WP_Query(array('fields' => 'ids', 'post_parent__in' => $topic_ids, 'post_status' => bbp_get_public_status_id(), 'post_type' => bbp_get_reply_post_type(), 'posts_per_page' => 1, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'ignore_sticky_posts' => true)); $reply_id = array_shift($query->posts); unset($query); return (int) apply_filters('bbp_forum_query_last_reply_id', $reply_id, $forum_id); }