bbp_update_topic( int $topic_id, int $forum_id, array $anonymous_data = array(), int $author_id, bool $is_edit = false )

Handle all the extra meta stuff from posting a new topic


Description Description


Parameters Parameters

$topic_id

(Optional) Topic id

$forum_id

(Optional) Forum id

$anonymous_data

(Optional) - if it's an anonymous post. Do not supply if supplying $author_id. Should be sanitized (see bbp_filter_anonymous_post_data()

Default value: array()

$author_id

(Optional) Author id

$is_edit

(Optional) Is the post being edited? Defaults to false.

Default value: false


Top ↑

Source Source

File: includes/topics/functions.php

function bbp_update_topic( $topic_id = 0, $forum_id = 0, $anonymous_data = array(), $author_id = 0, $is_edit = false ) {

	// Validate the ID's passed from 'bbp_new_topic' action
	$topic_id = bbp_get_topic_id( $topic_id );
	$forum_id = bbp_get_forum_id( $forum_id );

	// Bail if there is no topic
	if ( empty( $topic_id ) ) {
		return;
	}

	// Check author_id
	if ( empty( $author_id ) ) {
		$author_id = bbp_get_current_user_id();
	}

	// Forum/Topic meta (early, for use in downstream functions)
	bbp_update_topic_forum_id( $topic_id, $forum_id );
	bbp_update_topic_topic_id( $topic_id, $topic_id );

	// Get the topic types
	$topic_types = bbp_get_topic_types( $topic_id );

	// Sticky check after 'bbp_new_topic' action so forum ID meta is set
	if ( ! empty( $_POST['bbp_stick_topic'] ) && in_array( $_POST['bbp_stick_topic'], array_keys( $topic_types ), true ) ) {

		// What's the caps?
		if ( current_user_can( 'moderate', $topic_id ) ) {

			// What's the haps?
			switch ( $_POST['bbp_stick_topic'] ) {

				// Sticky in this forum
				case 'stick'   :
					bbp_stick_topic( $topic_id );
					break;

				// Super sticky in all forums
				case 'super'   :
					bbp_stick_topic( $topic_id, true );
					break;

				// We can avoid this as it is a new topic
				case 'unstick' :
				default        :
					break;
			}
		}
	}

	// If anonymous post, store name, email, website and ip in post_meta.
	if ( ! empty( $anonymous_data ) ) {

		// Update anonymous meta data (not cookies)
		bbp_update_anonymous_post_author( $topic_id, $anonymous_data, bbp_get_topic_post_type() );

		// Set transient for throttle check (only on new, not edit)
		if ( empty( $is_edit ) ) {
			set_transient( '_bbp_' . bbp_current_author_ip() . '_last_posted', time() );
		}

	} else {
		if ( empty( $is_edit ) && ! current_user_can( 'throttle' ) ) {
			bbp_update_user_last_posted( $author_id );
		}
	}

	// Handle Subscription Checkbox
	if ( bbp_is_subscriptions_active() && ! empty( $author_id ) ) {

		// Check if subscribed
		$subscribed = bbp_is_user_subscribed( $author_id, $topic_id );

		// Check for action
		$subscheck  = ( ! empty( $_POST['bbp_topic_subscription'] ) && ( 'bbp_subscribe' === $_POST['bbp_topic_subscription'] ) )
			? true
			: false;

		// Subscribed and unsubscribing
		if ( ( true === $subscribed ) && ( false === $subscheck ) ) {
			bbp_remove_user_subscription( $author_id, $topic_id );

		// Not subscribed and subscribing
		} elseif ( ( false === $subscribed ) && ( true === $subscheck ) ) {
			bbp_add_user_subscription( $author_id, $topic_id );
		}
	}

	// Update associated topic values if this is a new topic
	if ( empty( $is_edit ) ) {

		// Update poster IP if not editing
		update_post_meta( $topic_id, '_bbp_author_ip', bbp_current_author_ip(), false );

		// Last active time
		$last_active = get_post_field( 'post_date', $topic_id );

		// Reply topic meta
		bbp_update_topic_last_reply_id      ( $topic_id, 0            );
		bbp_update_topic_last_active_id     ( $topic_id, $topic_id    );
		bbp_update_topic_last_active_time   ( $topic_id, $last_active );
		bbp_update_topic_reply_count        ( $topic_id, 0            );
		bbp_update_topic_reply_count_hidden ( $topic_id, 0            );
		bbp_update_topic_voice_count        ( $topic_id               );

		// Walk up ancestors and do the dirty work
		bbp_update_topic_walker( $topic_id, $last_active, $forum_id, 0, false );
	}

	// Bump the custom query cache
	wp_cache_set( 'last_changed', microtime(), 'bbpress_posts' );
}

Top ↑

User Contributed Notes User Contributed Notes

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