/** * Returns the forum's topic ids * * Only topics with published and closed statuses are returned * * @since bbPress (r2908) * * @param int $forum_id Forum id * @uses bbp_get_topic_post_type() To get the topic post type * @uses bbp_get_public_child_ids() To get the topic ids * @uses apply_filters() Calls 'bbp_forum_query_topic_ids' with the topic ids * and forum id */ function bbp_forum_query_topic_ids($forum_id) { $topic_ids = bbp_get_public_child_ids($forum_id, bbp_get_topic_post_type()); return apply_filters('bbp_forum_query_topic_ids', $topic_ids, $forum_id); }
/** * Returns the forum's subforum ids * * Only forums with published status are returned * * @since bbPress (r2908) * * @param int $forum_id Forum id * @uses bbp_get_forum_post_type() To get the forum post type * @uses bbp_get_public_child_ids() To get the forum ids * @uses apply_filters() Calls 'bbp_forum_query_subforum_ids' with the subforum * ids and forum id */ function bbp_forum_query_subforum_ids($forum_id) { $subforum_ids = bbp_get_public_child_ids($forum_id, bbp_get_forum_post_type()); //usort( $subforum_ids, '_bbp_forum_query_usort_subforum_ids' ); return apply_filters('bbp_get_forum_subforum_ids', $subforum_ids, $forum_id); }
/** * @covers ::bbp_get_public_child_ids */ public function test_bbp_get_public_child_ids() { $f = $this->factory->forum->create(); // Test initial forum public child counts $count = count(bbp_get_public_child_ids($f, bbp_get_forum_post_type())); $this->assertSame(0, $count); $count = count(bbp_get_public_child_ids($f, bbp_get_topic_post_type())); $this->assertSame(0, $count); /* Sub-Forums *********************************************************/ $this->factory->forum->create_many(3, array('post_parent' => $f)); $this->factory->forum->create(array('post_parent' => $f, 'post_status' => bbp_get_private_status_id())); $count = count(bbp_get_public_child_ids($f, bbp_get_forum_post_type())); $this->assertSame(3, $count); $this->factory->forum->create_many(2, array('post_parent' => $f)); $count = count(bbp_get_public_child_ids($f, bbp_get_forum_post_type())); $this->assertSame(5, $count); /* Topics *************************************************************/ $t1 = $this->factory->topic->create_many(3, array('post_parent' => $f, 'topic_meta' => array('forum_id' => $f))); $this->factory->topic->create(array('post_parent' => $f, 'post_status' => bbp_get_spam_status_id(), 'topic_meta' => array('forum_id' => $f))); $count = count(bbp_get_public_child_ids($f, bbp_get_topic_post_type())); $this->assertSame(3, $count); $this->factory->topic->create_many(2, array('post_parent' => $f, 'topic_meta' => array('forum_id' => $f))); $count = count(bbp_get_public_child_ids($f, bbp_get_topic_post_type())); $this->assertSame(5, $count); /* Replies ************************************************************/ $this->factory->reply->create_many(3, array('post_parent' => $t1[0], 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t1[0]))); $this->factory->reply->create(array('post_parent' => $t1[0], 'post_status' => bbp_get_spam_status_id(), 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t1[0]))); $count = count(bbp_get_public_child_ids($t1[0], bbp_get_reply_post_type())); $this->assertSame(3, $count); $this->factory->reply->create_many(2, array('post_parent' => $t1[0], 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t1[0]))); $count = count(bbp_get_public_child_ids($t1[0], bbp_get_reply_post_type())); $this->assertSame(5, $count); }
/** * Handle the moving of a topic from one forum to another. This includes walking * up the old and new branches and updating the counts. * * @since 2.0.0 bbPress (r2907) * * @param int $topic_id The topic id. * @param int $old_forum_id Old forum id. * @param int $new_forum_id New forum id. * @uses bbp_get_topic_id() To get the topic id * @uses bbp_get_forum_id() To get the forum id * @uses bbp_clean_post_cache() To clean the old and new forum post caches * @uses bbp_update_topic_forum_id() To update the topic forum id * @uses wp_update_post() To update the topic post parent` * @uses bbp_get_stickies() To get the old forums sticky topics * @uses delete_post_meta() To delete the forum sticky meta * @uses update_post_meta() To update the old forum sticky meta * @uses bbp_stick_topic() To stick the topic in the new forum * @uses bbp_get_reply_post_type() To get the reply post type * @uses bbp_get_all_child_ids() To get all the child ids * @uses bbp_update_reply_forum_id() To update the reply forum id * @uses get_post_ancestors() To get the topic's ancestors * @uses bbp_get_public_child_ids() To get all the public child ids * @uses get_post_field() To get the topic's post status * @uses bbp_get_public_status_id() To get the public status id * @uses bbp_decrease_forum_topic_count() To bump the forum topic count by -1 * @uses bbp_bump_forum_reply_count() To bump the forum reply count * @uses bbp_increase_forum_topic_count() To bump the forum topic count by 1 * @uses bbp_decrease_forum_topic_count_hidden() To bump the forum topic hidden count by -1 * @uses bbp_increase_forum_topic_count_hidden() To bump the forum topic hidden count by 1 * @uses bbp_is_forum() To check if the ancestor is a forum * @uses bbp_update_forum() To update the forum */ function bbp_move_topic_handler($topic_id, $old_forum_id, $new_forum_id) { // Validate parameters $topic_id = bbp_get_topic_id($topic_id); $old_forum_id = bbp_get_forum_id($old_forum_id); $new_forum_id = bbp_get_forum_id($new_forum_id); // Clean old and new forum caches before proceeding, to ensure subsequent // calls to forum objects are using updated data. bbp_clean_post_cache($old_forum_id); bbp_clean_post_cache($new_forum_id); // Update topic forum's ID bbp_update_topic_forum_id($topic_id, $new_forum_id); // Update topic post parent with the new forum ID wp_update_post(array('ID' => $topic_id, 'post_parent' => $new_forum_id)); /** Stickies **************************************************************/ // Get forum stickies $old_stickies = bbp_get_stickies($old_forum_id); // Only proceed if stickies are found if (!empty($old_stickies)) { // Define local variables $updated_stickies = array(); // Loop through stickies of forum and add misses to the updated array foreach ((array) $old_stickies as $sticky_topic_id) { if ($topic_id !== $sticky_topic_id) { $updated_stickies[] = $sticky_topic_id; } } // If stickies are different, update or delete them if ($updated_stickies !== $old_stickies) { // No more stickies so delete the meta if (empty($updated_stickies)) { delete_post_meta($old_forum_id, '_bbp_sticky_topics'); // Still stickies so update the meta } else { update_post_meta($old_forum_id, '_bbp_sticky_topics', $updated_stickies); } // Topic was sticky, so restick in new forum bbp_stick_topic($topic_id); } } /** Topic Replies *********************************************************/ // Get the topics replies $replies = bbp_get_all_child_ids($topic_id, bbp_get_reply_post_type()); // Update the forum_id of all replies in the topic foreach ($replies as $reply_id) { bbp_update_reply_forum_id($reply_id, $new_forum_id); } /** Old forum_id **********************************************************/ // Get topic ancestors $old_forum_ancestors = array_values(array_unique(array_merge(array($old_forum_id), (array) get_post_ancestors($old_forum_id)))); // Get reply count. $public_reply_count = count(bbp_get_public_child_ids($topic_id, bbp_get_reply_post_type())); // Topic status. $topic_status = get_post_field('post_status', $topic_id); // Update old/new forum counts. if ($topic_status === bbp_get_public_status_id()) { // Update old forum counts. bbp_decrease_forum_topic_count($old_forum_id); bbp_bump_forum_reply_count($old_forum_id, -$public_reply_count); // Update new forum counts. bbp_increase_forum_topic_count($new_forum_id); bbp_bump_forum_reply_count($new_forum_id, $public_reply_count); } else { // Update old forum counts. bbp_decrease_forum_topic_count_hidden($old_forum_id); // Update new forum counts. bbp_increase_forum_topic_count_hidden($new_forum_id); } // Loop through ancestors and update them if (!empty($old_forum_ancestors)) { foreach ($old_forum_ancestors as $ancestor) { if (bbp_is_forum($ancestor)) { bbp_update_forum(array('forum_id' => $ancestor)); } } } /** New forum_id **********************************************************/ // Make sure we're not walking twice if (!in_array($new_forum_id, $old_forum_ancestors)) { // Get topic ancestors $new_forum_ancestors = array_values(array_unique(array_merge(array($new_forum_id), (array) get_post_ancestors($new_forum_id)))); // Make sure we're not walking twice $new_forum_ancestors = array_diff($new_forum_ancestors, $old_forum_ancestors); // Loop through ancestors and update them if (!empty($new_forum_ancestors)) { foreach ($new_forum_ancestors as $ancestor) { if (bbp_is_forum($ancestor)) { bbp_update_forum(array('forum_id' => $ancestor)); } } } } }