bbp_move_topic_handler( int $topic_id, int $old_forum_id, int $new_forum_id )

Handle the moving of a topic from one forum to another. This includes walking up the old and new branches and updating the counts.


Description Description


Parameters Parameters

$topic_id

(Required) The topic id.

$old_forum_id

(Required) Old forum id.

$new_forum_id

(Required) New forum id.


Top ↑

Source Source

File: includes/topics/functions.php

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.
	clean_post_cache( $old_forum_id );
	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 ) ) ) );

	// Public counts
	if ( bbp_is_topic_public( $topic_id ) ) {

		// Update old forum counts.
		bbp_decrease_forum_topic_count( $old_forum_id );

		// Update new forum counts.
		bbp_increase_forum_topic_count( $new_forum_id );

	// Non-public counts
	} 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 );
	}

	// Get reply counts.
	$public_reply_count = bbp_get_public_child_count( $topic_id, bbp_get_reply_post_type() );
	$hidden_reply_count = bbp_get_non_public_child_count( $topic_id, bbp_get_reply_post_type() );

	// Bump reply counts.
	bbp_bump_forum_reply_count( $old_forum_id, -$public_reply_count );
	bbp_bump_forum_reply_count( $new_forum_id, $public_reply_count );
	bbp_bump_forum_reply_count_hidden( $old_forum_id, -$hidden_reply_count );
	bbp_bump_forum_reply_count_hidden( $new_forum_id, $hidden_reply_count );

	// 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, true ) ) {

		// 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,
					) );
				}
			}
		}
	}
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.