/** * @covers ::bbp_forum_query_subforum_ids */ public function test_bbp_forum_query_subforum_ids() { $f1 = $this->factory->forum->create(); $f2 = $this->factory->forum->create(array('post_parent' => $f1)); $f3 = $this->factory->forum->create(array('post_parent' => $f1)); $this->assertEqualSets(array($f2, $f3), bbp_forum_query_subforum_ids($f1)); }
/** * @covers ::bbp_forum_subforum_count * @covers ::bbp_get_forum_subforum_count */ public function test_bbp_get_forum_subforum_count() { $f1 = $this->factory->forum->create(); $int_value = 3; $formatted_value = bbp_number_format($int_value); $this->factory->forum->create_many($int_value, array('post_parent' => $f1)); bbp_update_forum_subforum_count($f1); // Output. $count = bbp_get_forum_subforum_count($f1, false); $this->expectOutputString($formatted_value); bbp_forum_subforum_count($f1); // Formatted string. $count = bbp_get_forum_subforum_count($f1, false); $this->assertSame($formatted_value, $count); // Integer. $count = bbp_get_forum_subforum_count($f1, true); $this->assertSame($int_value, $count); // Direct query. $count = count(bbp_forum_query_subforum_ids($f1)); $this->assertSame($int_value, $count); }
/** * Adjust the total reply count of a forum * * @since bbPress (r2464) * * @param int $forum_id Optional. Forum id or topic id. It is checked whether it * is a topic or a forum. If it's a topic, its parent, * i.e. the forum is automatically retrieved. * @param bool $total_count Optional. To return the total count or normal * count? * @uses bbp_get_forum_id() To get the forum id * @uses bbp_forum_query_subforum_ids() To get the subforum ids * @uses bbp_update_forum_reply_count() To update the forum reply count * @uses bbp_forum_query_topic_ids() To get the forum topic ids * @uses wpdb::prepare() To prepare the sql statement * @uses wpdb::get_var() To execute the query and get the var back * @uses update_post_meta() To update the forum's reply count meta * @uses apply_filters() Calls 'bbp_update_forum_topic_count' with the reply * count and forum id * @return int Forum reply count */ function bbp_update_forum_reply_count($forum_id = 0) { global $wpdb; $forum_id = bbp_get_forum_id($forum_id); $children_reply_count = 0; // Loop through children and add together forum reply counts $children = bbp_forum_query_subforum_ids($forum_id); if (!empty($children)) { foreach ((array) $children as $child) { $children_reply_count += bbp_update_forum_reply_count($child); } } // Don't count replies if the forum is a category $topic_ids = bbp_forum_query_topic_ids($forum_id); if (!empty($topic_ids)) { $topic_ids = implode(',', wp_parse_id_list($topic_ids)); $reply_count = (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent IN ( {$topic_ids} ) AND post_status = '%s' AND post_type = '%s';", bbp_get_public_status_id(), bbp_get_reply_post_type())); } else { $reply_count = 0; } // Calculate total replies in this forum $total_replies = (int) $reply_count + $children_reply_count; // Update the count update_post_meta($forum_id, '_bbp_reply_count', (int) $reply_count); update_post_meta($forum_id, '_bbp_total_reply_count', (int) $total_replies); return (int) apply_filters('bbp_update_forum_reply_count', (int) $total_replies, $forum_id); }
/** * @group canonical * @covers ::bbp_create_initial_content */ public function test_bbp_create_initial_content() { $category_id = $this->factory->forum->create(array('forum_meta' => array('_bbp_forum_type' => 'category', '_bbp_status' => 'open'))); bbp_create_initial_content(array('forum_parent' => $category_id)); $forum_id = bbp_forum_query_subforum_ids($category_id); $forum_id = (int) $forum_id[0]; $topic_id = bbp_get_forum_last_topic_id($forum_id); $reply_id = bbp_get_forum_last_reply_id($forum_id); // Forum post $this->assertSame('General', bbp_get_forum_title($forum_id)); $this->assertSame('General chit-chat', bbp_get_forum_content($forum_id)); $this->assertSame('open', bbp_get_forum_status($forum_id)); $this->assertTrue(bbp_is_forum_public($forum_id)); $this->assertSame($category_id, bbp_get_forum_parent_id($forum_id)); // Topic post $this->assertSame($forum_id, bbp_get_topic_forum_id($topic_id)); $this->assertSame('Hello World!', bbp_get_topic_title($topic_id)); remove_all_filters('bbp_get_topic_content'); $topic_content = "I am the first topic in your new forums."; $this->assertSame($topic_content, bbp_get_topic_content($topic_id)); $this->assertSame('publish', bbp_get_topic_status($topic_id)); $this->assertTrue(bbp_is_topic_published($topic_id)); // Reply post $this->assertSame($forum_id, bbp_get_reply_forum_id($reply_id)); $this->assertSame('Reply To: Hello World!', bbp_get_reply_title($reply_id)); $this->assertSame($reply_id, bbp_get_reply_title_fallback($reply_id)); remove_all_filters('bbp_get_reply_content'); $reply_content = "Oh, and this is what a reply looks like."; $this->assertSame($reply_content, bbp_get_reply_content($reply_id)); $this->assertSame('publish', bbp_get_reply_status($reply_id)); $this->assertTrue(bbp_is_reply_published($reply_id)); // Category meta $this->assertSame(1, bbp_get_forum_subforum_count($category_id, true)); $this->assertSame(0, bbp_get_forum_topic_count($category_id, false, true)); $this->assertSame(0, bbp_get_forum_topic_count_hidden($category_id, true)); $this->assertSame(0, bbp_get_forum_reply_count($category_id, false, true)); $this->assertSame(1, bbp_get_forum_topic_count($category_id, true, true)); $this->assertSame(1, bbp_get_forum_reply_count($category_id, true, true)); $this->assertSame(0, bbp_get_forum_post_count($category_id, false, true)); $this->assertSame(2, bbp_get_forum_post_count($category_id, true, true)); $this->assertSame($topic_id, bbp_get_forum_last_topic_id($category_id)); $this->assertSame('Hello World!', bbp_get_forum_last_topic_title($category_id)); $this->assertSame($reply_id, bbp_get_forum_last_reply_id($category_id)); $this->assertSame('Reply To: Hello World!', bbp_get_forum_last_reply_title($category_id)); $this->assertSame($reply_id, bbp_get_forum_last_active_id($category_id)); $this->assertSame('1 day, 16 hours ago', bbp_get_forum_last_active_time($category_id)); // Forum meta $this->assertSame(0, bbp_get_forum_subforum_count($forum_id, true)); $this->assertSame(1, bbp_get_forum_topic_count($forum_id, false, true)); $this->assertSame(0, bbp_get_forum_topic_count_hidden($forum_id, true)); $this->assertSame(1, bbp_get_forum_reply_count($forum_id, false, true)); $this->assertSame(1, bbp_get_forum_topic_count($forum_id, true, true)); $this->assertSame(1, bbp_get_forum_reply_count($forum_id, true, true)); $this->assertSame(2, bbp_get_forum_post_count($forum_id, false, true)); $this->assertSame(2, bbp_get_forum_post_count($forum_id, true, true)); $this->assertSame($topic_id, bbp_get_forum_last_topic_id($forum_id)); $this->assertSame('Hello World!', bbp_get_forum_last_topic_title($forum_id)); $this->assertSame($reply_id, bbp_get_forum_last_reply_id($forum_id)); $this->assertSame('Reply To: Hello World!', bbp_get_forum_last_reply_title($forum_id)); $this->assertSame($reply_id, bbp_get_forum_last_active_id($forum_id)); $this->assertSame('1 day, 16 hours ago', bbp_get_forum_last_active_time($forum_id)); // Topic meta $this->assertSame('127.0.0.1', bbp_current_author_ip($topic_id)); $this->assertSame($forum_id, bbp_get_topic_forum_id($topic_id)); $this->assertSame(1, bbp_get_topic_voice_count($topic_id, true)); $this->assertSame(1, bbp_get_topic_reply_count($topic_id, true)); $this->assertSame(0, bbp_get_topic_reply_count_hidden($topic_id, true)); $this->assertSame($reply_id, bbp_get_topic_last_reply_id($topic_id)); $this->assertSame($reply_id, bbp_get_topic_last_active_id($topic_id)); $this->assertSame('1 day, 16 hours ago', bbp_get_topic_last_active_time($topic_id)); // Reply Meta $this->assertSame('127.0.0.1', bbp_current_author_ip($reply_id)); $this->assertSame($forum_id, bbp_get_reply_forum_id($reply_id)); $this->assertSame($topic_id, bbp_get_reply_topic_id($reply_id)); }
/** * Adjust the total reply count of a forum * * @since 2.0.0 bbPress (r2464) * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects * * @param int $forum_id Optional. Forum id or topic id. It is checked whether it * is a topic or a forum. If it's a topic, its parent, * i.e. the forum is automatically retrieved. * @uses bbp_get_forum_id() To get the forum id * @uses bbp_forum_query_subforum_ids() To get the subforum ids * @uses bbp_update_forum_reply_count() To update the forum reply count * @uses bbp_forum_query_topic_ids() To get the forum 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 update_post_meta() To update the forum's reply count meta * @uses apply_filters() Calls 'bbp_update_forum_reply_count' with the reply * count and forum id * @return int Forum reply count */ function bbp_update_forum_reply_count($forum_id = 0) { $forum_id = bbp_get_forum_id($forum_id); $children_reply_count = 0; // Loop through children and add together forum reply counts $children = bbp_forum_query_subforum_ids($forum_id); if (!empty($children)) { foreach ((array) $children as $child) { $children_reply_count += bbp_update_forum_reply_count($child); } } // Don't count replies if the forum is a category $reply_count = 0; $topic_ids = bbp_forum_query_topic_ids($forum_id); if (!empty($topic_ids)) { $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_count = !empty($query->posts) ? count($query->posts) : 0; unset($query); } // Calculate total replies in this forum $total_replies = (int) ($reply_count + $children_reply_count); // Update the count update_post_meta($forum_id, '_bbp_reply_count', $reply_count); update_post_meta($forum_id, '_bbp_total_reply_count', $total_replies); return (int) apply_filters('bbp_update_forum_reply_count', $total_replies, $forum_id); }