/** * 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)); } } } } }
/** * @covers ::bbp_decrease_forum_topic_count */ public function test_bbp_decrease_forum_topic_count() { $f = $this->factory->forum->create(); $count = bbp_get_forum_topic_count($f); $this->assertSame('0', $count); $t = $this->factory->topic->create_many(2, array('post_parent' => $f)); bbp_update_forum_topic_count($f); $count = bbp_get_forum_topic_count($f); $this->assertSame('2', $count); bbp_update_forum_topic_count($f); bbp_decrease_forum_topic_count($f); $count = bbp_get_forum_topic_count($f); $this->assertSame('1', $count); }