Index: includes/common/classes.php
===================================================================
--- includes/common/classes.php	(revision 2832582)
+++ includes/common/classes.php	(working copy)
@@ -509,3 +509,451 @@
 	}
 }
 endif; // class_exists check
+
+if ( ! class_exists( 'BBP_Topic' ) ) :
+	/**
+	 * bbPress Topic Class
+	 *
+	 * The bbPress component class is responsible for simplifying the creation
+	 * of topics
+	 *
+	 * @package bbPress
+	 * @subpackage Classes
+	 *
+	 * @since 2.7
+	 */
+	class BBP_Topic {
+	  /**
+	   * The id of newly created topic
+	   * @var int
+	   */
+	  public $id;
+
+	  /**
+	   * Topic data
+	   * @var array|mixed
+	   */
+	  private $data = [];
+
+	  /**
+	   * Whether we want to bbp_redirect after saving
+	   * @var bool
+	   */
+	  private $should_redirect_after = true;
+
+	  /** Methods ***************************************************************/
+
+	  /**
+	   * bbPress create new Topic class
+	   *
+	   * @since 2.7 bbPress ()
+	   *
+	   * @param array   $data                  Optional, topic data (for bbPress frontend it should be $_POST)
+	   * @param boolean $should_redirect_after Optional. Whether we want to redirect after saving
+	   */
+	  public function __construct( $data = [], $should_redirect_after = true ) {
+		$this->data                  = $data;
+		$this->should_redirect_after = $should_redirect_after;
+	  }
+
+	  /**
+	   * Get anonymous data if user is not logged in
+	   * @return array|bool
+	   */
+	  private function get_anonymous_data() {
+		// Filter anonymous data (variable is used later)
+		$anonymous_data = bbp_filter_anonymous_post_data( $this->data );
+
+		// Anonymous data checks out, so set cookies, etc...
+		bbp_set_current_anonymous_user_data( $anonymous_data );
+
+		return $anonymous_data;
+	  }
+
+	  /**
+	   * Get topic author id
+	   * @return int
+	   */
+	  private function get_topic_author_id() {
+		// User cannot create topics
+		if ( ! current_user_can( 'publish_topics' ) ) {
+		  bbp_add_error( 'bbp_topic_permission', __( '<strong>Error</strong>: You do not have permission to create new topics.', 'bbpress' ) );
+		  return 0;
+		}
+
+		// Topic author is current user
+		return bbp_get_current_user_id();
+	  }
+
+	  /**
+	   * Get topic validated and sanitized title
+	   * @return string
+	   */
+	  private function get_topic_title() {
+		$topic_title = '';
+		if ( ! empty( $this->data['bbp_topic_title'] ) ) {
+		  $topic_title = sanitize_text_field( $this->data['bbp_topic_title'] );
+		}
+
+		// Filter and sanitize
+		$topic_title = apply_filters( 'bbp_new_topic_pre_title', $topic_title );
+
+		// No topic title
+		if ( empty( $topic_title ) ) {
+		  bbp_add_error( 'bbp_topic_title', __( '<strong>Error</strong>: Your topic needs a title.', 'bbpress' ) );
+		  return '';
+		}
+
+		// Title too long
+		if ( bbp_is_title_too_long( $topic_title ) ) {
+		  bbp_add_error( 'bbp_topic_title', __( '<strong>Error</strong>: Your title is too long.', 'bbpress' ) );
+		  return '';
+		}
+
+		return $topic_title;
+	  }
+
+	  /**
+	   * Get topic validated and sanitized content
+	   * @return string
+	   */
+	  private function get_topic_content() {
+		$topic_content = '';
+		if ( ! empty( $this->data['bbp_topic_content'] ) ) {
+		  $topic_content = $this->data['bbp_topic_content'];
+		}
+
+		// Filter and sanitize
+		$topic_content = apply_filters( 'bbp_new_topic_pre_content', $topic_content );
+
+		// No topic content
+		if ( empty( $topic_content ) ) {
+		  bbp_add_error( 'bbp_topic_content', __( '<strong>Error</strong>: Your topic cannot be empty.', 'bbpress' ) );
+		  return '';
+		}
+
+		return $topic_content;
+	  }
+
+	  /**
+	   * Get validated forum id
+	   * @return int
+	   */
+	  private function get_forum_id() {
+		$forum_id = 0;
+		// Error check the POST'ed topic id
+		if ( isset( $this->data['bbp_forum_id'] ) ) {
+
+		  // Empty Forum id was passed
+		  if ( empty( $this->data['bbp_forum_id'] ) ) {
+			bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID is missing.', 'bbpress' ) );
+			return 0;
+
+			// Forum id is not a number
+		  } elseif ( ! is_numeric( $this->data['bbp_forum_id'] ) ) {
+			bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID must be a number.', 'bbpress' ) );
+			return 0;
+
+			// Forum id might be valid
+		  } else {
+
+			// Get the forum id
+			$posted_forum_id = intval( $this->data['bbp_forum_id'] );
+
+			// Forum id is empty
+			if ( 0 === $posted_forum_id ) {
+			  bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID is missing.', 'bbpress' ) );
+			  return 0;
+
+			  // Forum id is a negative number
+			} elseif ( 0 > $posted_forum_id ) {
+			  bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID cannot be a negative number.', 'bbpress' ) );
+			  return 0;
+
+			  // Forum does not exist
+			} elseif ( ! bbp_get_forum( $posted_forum_id ) ) {
+			  bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum does not exist.', 'bbpress' ) );
+			  return 0;
+
+			  // Use the POST'ed forum id
+			} else {
+			  $forum_id = $posted_forum_id;
+			}
+		  }
+		}
+
+		// Forum exists
+		if ( ! empty( $forum_id ) ) {
+
+		  // Forum is a category
+		  if ( bbp_is_forum_category( $forum_id ) ) {
+			bbp_add_error( 'bbp_new_topic_forum_category', __( '<strong>Error</strong>: This forum is a category. No topics can be created in this forum.', 'bbpress' ) );
+			return 0;
+
+			// Forum is not a category
+		  } else {
+
+			// Forum is closed and user cannot access
+			if ( bbp_is_forum_closed( $forum_id ) && ! current_user_can( 'edit_forum', $forum_id ) ) {
+			  bbp_add_error( 'bbp_new_topic_forum_closed', __( '<strong>Error</strong>: This forum has been closed to new topics.', 'bbpress' ) );
+			  return 0;
+			}
+
+			// Forum is private and user cannot access
+			if ( bbp_is_forum_private( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
+			  bbp_add_error( 'bbp_new_topic_forum_private', __( '<strong>Error</strong>: This forum is private and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
+			  return 0;
+
+			  // Forum is hidden and user cannot access
+			} elseif ( bbp_is_forum_hidden( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
+			  bbp_add_error( 'bbp_new_topic_forum_hidden', __( '<strong>Error</strong>: This forum is hidden and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
+			  return 0;
+			}
+		  }
+		}
+
+		return $forum_id;
+	  }
+
+	  /**
+	   * Create and save the topic to db
+	   * @return boolean
+	   */
+	  public function save() {
+
+		// Define local variable(s)
+		$view_all       = false;
+		$anonymous_data = [];
+		$topic_author   = 0;
+		$terms          = [ bbp_get_topic_tag_tax_id() => [] ];
+
+		// User is anonymous
+		if ( bbp_is_anonymous() ) {
+		  $anonymous_data = $this->get_anonymous_data();
+
+		  // User is logged in
+		} else {
+		  $topic_author = $this->get_topic_author_id();
+		  if ( empty( $topic_author ) ) {
+			return false;
+		  }
+
+		  // Remove kses filters from title and content for capable users and if the nonce is verified
+		  if ( current_user_can( 'unfiltered_html' ) && ! empty( $this->data['_bbp_unfiltered_html_topic'] ) && wp_create_nonce( 'bbp-unfiltered-html-topic_new' ) === $this->data['_bbp_unfiltered_html_topic'] ) {
+			remove_filter( 'bbp_new_topic_pre_title', 'wp_filter_kses' );
+			remove_filter( 'bbp_new_topic_pre_content', 'bbp_encode_bad', 10 );
+			remove_filter( 'bbp_new_topic_pre_content', 'bbp_filter_kses', 30 );
+		  }
+		}
+
+		$topic_title = $this->get_topic_title();
+		if ( empty( $topic_title ) ) {
+		  return false;
+		}
+
+		$topic_content = $this->get_topic_content();
+		if ( empty( $topic_content ) ) {
+		  return false;
+		}
+
+		$forum_id = $this->get_forum_id();
+		if ( empty( $forum_id ) ) {
+		  return false;
+		}
+
+		/** Topic Flooding ********************************************************/
+
+		if ( ! bbp_check_for_flood( $anonymous_data, $topic_author ) ) {
+		  bbp_add_error( 'bbp_topic_flood', __( '<strong>Error</strong>: Slow down; you move too fast.', 'bbpress' ) );
+		  return false;
+		}
+
+		/** Topic Duplicate *******************************************************/
+
+		$dupe_args = array(
+		  'post_type'      => bbp_get_topic_post_type(),
+		  'post_author'    => $topic_author,
+		  'post_content'   => $topic_content,
+		  'post_parent'    => $forum_id,
+		  'anonymous_data' => $anonymous_data,
+		);
+
+		if ( ! bbp_check_for_duplicate( $dupe_args ) ) {
+		  bbp_add_error( 'bbp_topic_duplicate', __( '<strong>Error</strong>: Duplicate topic detected; it looks as though you&#8217;ve already said that.', 'bbpress' ) );
+		  return false;
+		}
+
+		/** Topic Bad Words *******************************************************/
+
+		if ( ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content, true ) ) {
+		  bbp_add_error( 'bbp_topic_moderation', __( '<strong>Error</strong>: Your topic cannot be created at this time.', 'bbpress' ) );
+		  return false;
+		}
+
+		/** Topic Status **********************************************************/
+
+		// Get available topic statuses
+		$topic_statuses = bbp_get_topic_statuses();
+
+		// Default to published
+		$topic_status = bbp_get_public_status_id();
+
+		// Maybe force into pending
+		if ( ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content ) ) {
+		  $topic_status = bbp_get_pending_status_id();
+
+		  // Check for possible posted topic status
+		} elseif ( ! empty( $this->data['bbp_topic_status'] ) && in_array( $this->data['bbp_topic_status'], array_keys( $topic_statuses ), true ) ) {
+
+		  // Allow capable users to explicitly override the status
+		  if ( current_user_can( 'moderate', $forum_id ) ) {
+			$topic_status = sanitize_key( $this->data['bbp_topic_status'] );
+
+			// Not capable
+		  } else {
+			bbp_add_error( 'bbp_new_topic_status', __( '<strong>Error</strong>: You do not have permission to do that.', 'bbpress' ) );
+			return false;
+		  }
+		}
+
+		/** Topic Tags ************************************************************/
+
+		if ( bbp_allow_topic_tags() && ! empty( $this->data['bbp_topic_tags'] ) ) {
+
+		  // Escape tag input
+		  $terms = sanitize_text_field( $this->data['bbp_topic_tags'] );
+
+		  // Explode by comma
+		  if ( strstr( $terms, ',' ) ) {
+			$terms = explode( ',', $terms );
+		  }
+
+		  // Add topic tag ID as main key
+		  $terms = array( bbp_get_topic_tag_tax_id() => $terms );
+		}
+
+		/** Additional Actions (Before Save) **************************************/
+
+		do_action( 'bbp_new_topic_pre_extras', $forum_id );
+
+		// Bail if errors
+		if ( bbp_has_errors() ) {
+		  return false;
+		}
+
+		/** No Errors *************************************************************/
+
+		// Add the content of the form to $topic_data as an array.
+		// Just in time manipulation of topic data before being created
+		$topic_data = apply_filters(
+		  'bbp_new_topic_pre_insert',
+		  [
+			'post_author'    => $topic_author,
+			'post_title'     => $topic_title,
+			'post_content'   => $topic_content,
+			'post_status'    => $topic_status,
+			'post_parent'    => $forum_id,
+			'post_type'      => bbp_get_topic_post_type(),
+			'tax_input'      => $terms,
+			'comment_status' => 'closed',
+		  ]
+		);
+
+		// Insert topic
+		$topic_id = wp_insert_post( $topic_data, true );
+		$this->id = $topic_id;
+
+		/** No Errors *************************************************************/
+
+		if ( ! empty( $topic_id ) && ! is_wp_error( $topic_id ) ) {
+
+		  /** Close Check *******************************************************/
+
+		  // If the topic is closed, close it properly
+		  if ( ( get_post_field( 'post_status', $topic_id ) === bbp_get_closed_status_id() ) || ( $topic_data['post_status'] === bbp_get_closed_status_id() ) ) {
+
+			// Close the topic
+			bbp_close_topic( $topic_id );
+		  }
+
+		  /** Trash Check *******************************************************/
+
+		  // If the forum is trash, or the topic_status is switched to
+		  // trash, trash the topic properly
+		  if ( ( get_post_field( 'post_status', $forum_id ) === bbp_get_trash_status_id() ) || ( $topic_data['post_status'] === bbp_get_trash_status_id() ) ) {
+
+			// Trash the topic
+			wp_trash_post( $topic_id );
+
+			// Force view=all
+			$view_all = true;
+		  }
+
+		  /** Spam Check ********************************************************/
+
+		  // If the topic is spam, officially spam this topic
+		  if ( $topic_data['post_status'] === bbp_get_spam_status_id() ) {
+			add_post_meta( $topic_id, '_bbp_spam_meta_status', bbp_get_public_status_id() );
+
+			// Force view=all
+			$view_all = true;
+		  }
+
+		  /** Update counts, etc... *********************************************/
+
+		  do_action( 'bbp_new_topic', $topic_id, $forum_id, $anonymous_data, $topic_author );
+
+		  /** Additional Actions (After Save) ***********************************/
+
+		  do_action( 'bbp_new_topic_post_extras', $topic_id );
+
+		  // If it is not the bbp frontend we do not need to redirect.
+		  if ( ! $this->should_redirect_after ) {
+			return true;
+		  }
+
+		  /** Redirect **********************************************************/
+
+		  // Redirect to
+		  $redirect_to = bbp_get_redirect_to();
+
+		  // Get the topic URL
+		  $redirect_url = bbp_get_topic_permalink( $topic_id, $redirect_to );
+
+		  // Add view all?
+		  if ( bbp_get_view_all() || ! empty( $view_all ) ) {
+
+			// User can moderate, so redirect to topic with view all set
+			if ( current_user_can( 'moderate', $topic_id ) ) {
+			  $redirect_url = bbp_add_view_all( $redirect_url );
+
+			  // User cannot moderate, so redirect to forum
+			} else {
+			  $redirect_url = bbp_get_forum_permalink( $forum_id );
+			}
+		  }
+
+		  // Allow to be filtered
+		  $redirect_url = apply_filters( 'bbp_new_topic_redirect_to', $redirect_url, $redirect_to, $topic_id );
+
+		  /** Successful Save ***************************************************/
+
+		  // Redirect back to new topic
+		  bbp_redirect( $redirect_url );
+
+		  /** Errors ****************************************************************/
+
+		  // WP_Error
+		} elseif ( is_wp_error( $topic_id ) ) {
+		  /* translators: 1: Error message */
+		  bbp_add_error( 'bbp_topic_error', sprintf( __( '<strong>Error</strong>: The following problem(s) occurred: %s', 'bbpress' ), $topic_id->get_error_message() ) );
+		  return false;
+		  // Generic error
+		} else {
+		  bbp_add_error( 'bbp_topic_error', __( '<strong>Error</strong>: The topic was not created.', 'bbpress' ) );
+		  return false;
+		}
+	  }
+	}
+  endif; // BBP_Topic
+
Index: includes/topics/functions.php
===================================================================
--- includes/topics/functions.php	(revision 2832582)
+++ includes/topics/functions.php	(working copy)
@@ -106,318 +106,8 @@
 		return;
 	}
 
-	// Define local variable(s)
-	$view_all = false;
-	$forum_id = $topic_author = 0;
-	$topic_title = $topic_content = '';
-	$anonymous_data = array();
-	$terms = array( bbp_get_topic_tag_tax_id() => array() );
-
-	/** Topic Author **********************************************************/
-
-	// User is anonymous
-	if ( bbp_is_anonymous() ) {
-
-		// Filter anonymous data (variable is used later)
-		$anonymous_data = bbp_filter_anonymous_post_data();
-
-		// Anonymous data checks out, so set cookies, etc...
-		bbp_set_current_anonymous_user_data( $anonymous_data );
-
-	// User is logged in
-	} else {
-
-		// User cannot create topics
-		if ( ! current_user_can( 'publish_topics' ) ) {
-			bbp_add_error( 'bbp_topic_permission', __( '<strong>Error</strong>: You do not have permission to create new topics.', 'bbpress' ) );
-			return;
-		}
-
-		// Topic author is current user
-		$topic_author = bbp_get_current_user_id();
-	}
-
-	// Remove kses filters from title and content for capable users and if the nonce is verified
-	if ( current_user_can( 'unfiltered_html' ) && ! empty( $_POST['_bbp_unfiltered_html_topic'] ) && wp_create_nonce( 'bbp-unfiltered-html-topic_new' ) === $_POST['_bbp_unfiltered_html_topic'] ) {
-		remove_filter( 'bbp_new_topic_pre_title',   'wp_filter_kses'      );
-		remove_filter( 'bbp_new_topic_pre_content', 'bbp_encode_bad',  10 );
-		remove_filter( 'bbp_new_topic_pre_content', 'bbp_filter_kses', 30 );
-	}
-
-	/** Topic Title ***********************************************************/
-
-	if ( ! empty( $_POST['bbp_topic_title'] ) ) {
-		$topic_title = sanitize_text_field( $_POST['bbp_topic_title'] );
-	}
-
-	// Filter and sanitize
-	$topic_title = apply_filters( 'bbp_new_topic_pre_title', $topic_title );
-
-	// No topic title
-	if ( empty( $topic_title ) ) {
-		bbp_add_error( 'bbp_topic_title', __( '<strong>Error</strong>: Your topic needs a title.', 'bbpress' ) );
-	}
-
-	// Title too long
-	if ( bbp_is_title_too_long( $topic_title ) ) {
-		bbp_add_error( 'bbp_topic_title', __( '<strong>Error</strong>: Your title is too long.', 'bbpress' ) );
-	}
-
-	/** Topic Content *********************************************************/
-
-	if ( ! empty( $_POST['bbp_topic_content'] ) ) {
-		$topic_content = $_POST['bbp_topic_content'];
-	}
-
-	// Filter and sanitize
-	$topic_content = apply_filters( 'bbp_new_topic_pre_content', $topic_content );
-
-	// No topic content
-	if ( empty( $topic_content ) ) {
-		bbp_add_error( 'bbp_topic_content', __( '<strong>Error</strong>: Your topic cannot be empty.', 'bbpress' ) );
-	}
-
-	/** Topic Forum ***********************************************************/
-
-	// Error check the POST'ed topic id
-	if ( isset( $_POST['bbp_forum_id'] ) ) {
-
-		// Empty Forum id was passed
-		if ( empty( $_POST['bbp_forum_id'] ) ) {
-			bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID is missing.', 'bbpress' ) );
-
-		// Forum id is not a number
-		} elseif ( ! is_numeric( $_POST['bbp_forum_id'] ) ) {
-			bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID must be a number.', 'bbpress' ) );
-
-		// Forum id might be valid
-		} else {
-
-			// Get the forum id
-			$posted_forum_id = intval( $_POST['bbp_forum_id'] );
-
-			// Forum id is empty
-			if ( 0 === $posted_forum_id ) {
-				bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID is missing.', 'bbpress' ) );
-
-			// Forum id is a negative number
-			} elseif ( 0 > $posted_forum_id ) {
-				bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID cannot be a negative number.', 'bbpress' ) );
-
-			// Forum does not exist
-			} elseif ( ! bbp_get_forum( $posted_forum_id ) ) {
-				bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum does not exist.', 'bbpress' ) );
-
-			// Use the POST'ed forum id
-			} else {
-				$forum_id = $posted_forum_id;
-			}
-		}
-	}
-
-	// Forum exists
-	if ( ! empty( $forum_id ) ) {
-
-		// Forum is a category
-		if ( bbp_is_forum_category( $forum_id ) ) {
-			bbp_add_error( 'bbp_new_topic_forum_category', __( '<strong>Error</strong>: This forum is a category. No topics can be created in this forum.', 'bbpress' ) );
-
-		// Forum is not a category
-		} else {
-
-			// Forum is closed and user cannot access
-			if ( bbp_is_forum_closed( $forum_id ) && ! current_user_can( 'edit_forum', $forum_id ) ) {
-				bbp_add_error( 'bbp_new_topic_forum_closed', __( '<strong>Error</strong>: This forum has been closed to new topics.', 'bbpress' ) );
-			}
-
-			// Forum is private and user cannot access
-			if ( bbp_is_forum_private( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
-				bbp_add_error( 'bbp_new_topic_forum_private', __( '<strong>Error</strong>: This forum is private and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
-
-			// Forum is hidden and user cannot access
-			} elseif ( bbp_is_forum_hidden( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
-				bbp_add_error( 'bbp_new_topic_forum_hidden', __( '<strong>Error</strong>: This forum is hidden and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
-			}
-		}
-	}
-
-	/** Topic Flooding ********************************************************/
-
-	if ( ! bbp_check_for_flood( $anonymous_data, $topic_author ) ) {
-		bbp_add_error( 'bbp_topic_flood', __( '<strong>Error</strong>: Slow down; you move too fast.', 'bbpress' ) );
-	}
-
-	/** Topic Duplicate *******************************************************/
-
-	$dupe_args = array(
-		'post_type'      => bbp_get_topic_post_type(),
-		'post_author'    => $topic_author,
-		'post_content'   => $topic_content,
-		'post_parent'    => $forum_id,
-		'anonymous_data' => $anonymous_data
-	);
-
-	if ( ! bbp_check_for_duplicate( $dupe_args ) ) {
-		bbp_add_error( 'bbp_topic_duplicate', __( '<strong>Error</strong>: Duplicate topic detected; it looks as though you&#8217;ve already said that.', 'bbpress' ) );
-	}
-
-	/** Topic Bad Words *******************************************************/
-
-	if ( ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content, true ) ) {
-		bbp_add_error( 'bbp_topic_moderation', __( '<strong>Error</strong>: Your topic cannot be created at this time.', 'bbpress' ) );
-	}
-
-	/** Topic Status **********************************************************/
-
-	// Get available topic statuses
-	$topic_statuses = bbp_get_topic_statuses();
-
-	// Default to published
-	$topic_status = bbp_get_public_status_id();
-
-	// Maybe force into pending
-	if ( ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content ) ) {
-		$topic_status = bbp_get_pending_status_id();
-
-	// Check for possible posted topic status
-	} elseif ( ! empty( $_POST['bbp_topic_status'] ) && in_array( $_POST['bbp_topic_status'], array_keys( $topic_statuses ), true ) ) {
-
-		// Allow capable users to explicitly override the status
-		if ( current_user_can( 'moderate', $forum_id ) ) {
-			$topic_status = sanitize_key( $_POST['bbp_topic_status'] );
-
-		// Not capable
-		} else {
-			bbp_add_error( 'bbp_new_topic_status', __( '<strong>Error</strong>: You do not have permission to do that.', 'bbpress' ) );
-		}
-	}
-
-	/** Topic Tags ************************************************************/
-
-	if ( bbp_allow_topic_tags() && ! empty( $_POST['bbp_topic_tags'] ) ) {
-
-		// Escape tag input
-		$terms = sanitize_text_field( $_POST['bbp_topic_tags'] );
-
-		// Explode by comma
-		if ( strstr( $terms, ',' ) ) {
-			$terms = explode( ',', $terms );
-		}
-
-		// Add topic tag ID as main key
-		$terms = array( bbp_get_topic_tag_tax_id() => $terms );
-	}
-
-	/** Additional Actions (Before Save) **************************************/
-
-	do_action( 'bbp_new_topic_pre_extras', $forum_id );
-
-	// Bail if errors
-	if ( bbp_has_errors() ) {
-		return;
-	}
-
-	/** No Errors *************************************************************/
-
-	// Add the content of the form to $topic_data as an array.
-	// Just in time manipulation of topic data before being created
-	$topic_data = apply_filters( 'bbp_new_topic_pre_insert', array(
-		'post_author'    => $topic_author,
-		'post_title'     => $topic_title,
-		'post_content'   => $topic_content,
-		'post_status'    => $topic_status,
-		'post_parent'    => $forum_id,
-		'post_type'      => bbp_get_topic_post_type(),
-		'tax_input'      => $terms,
-		'comment_status' => 'closed'
-	) );
-
-	// Insert topic
-	$topic_id = wp_insert_post( $topic_data, true );
-
-	/** No Errors *************************************************************/
-
-	if ( ! empty( $topic_id ) && ! is_wp_error( $topic_id ) ) {
-
-		/** Close Check *******************************************************/
-
-		// If the topic is closed, close it properly
-		if ( ( get_post_field( 'post_status', $topic_id ) === bbp_get_closed_status_id() ) || ( $topic_data['post_status'] === bbp_get_closed_status_id() ) ) {
-
-			// Close the topic
-			bbp_close_topic( $topic_id );
-		}
-
-		/** Trash Check *******************************************************/
-
-		// If the forum is trash, or the topic_status is switched to
-		// trash, trash the topic properly
-		if ( ( get_post_field( 'post_status', $forum_id ) === bbp_get_trash_status_id() ) || ( $topic_data['post_status'] === bbp_get_trash_status_id() ) ) {
-
-			// Trash the topic
-			wp_trash_post( $topic_id );
-
-			// Force view=all
-			$view_all = true;
-		}
-
-		/** Spam Check ********************************************************/
-
-		// If the topic is spam, officially spam this topic
-		if ( $topic_data['post_status'] === bbp_get_spam_status_id() ) {
-			add_post_meta( $topic_id, '_bbp_spam_meta_status', bbp_get_public_status_id() );
-
-			// Force view=all
-			$view_all = true;
-		}
-
-		/** Update counts, etc... *********************************************/
-
-		do_action( 'bbp_new_topic', $topic_id, $forum_id, $anonymous_data, $topic_author );
-
-		/** Additional Actions (After Save) ***********************************/
-
-		do_action( 'bbp_new_topic_post_extras', $topic_id );
-
-		/** Redirect **********************************************************/
-
-		// Redirect to
-		$redirect_to = bbp_get_redirect_to();
-
-		// Get the topic URL
-		$redirect_url = bbp_get_topic_permalink( $topic_id, $redirect_to );
-
-		// Add view all?
-		if ( bbp_get_view_all() || ! empty( $view_all ) ) {
-
-			// User can moderate, so redirect to topic with view all set
-			if ( current_user_can( 'moderate', $topic_id ) ) {
-				$redirect_url = bbp_add_view_all( $redirect_url );
-
-			// User cannot moderate, so redirect to forum
-			} else {
-				$redirect_url = bbp_get_forum_permalink( $forum_id );
-			}
-		}
-
-		// Allow to be filtered
-		$redirect_url = apply_filters( 'bbp_new_topic_redirect_to', $redirect_url, $redirect_to, $topic_id );
-
-		/** Successful Save ***************************************************/
-
-		// Redirect back to new topic
-		bbp_redirect( $redirect_url );
-
-	/** Errors ****************************************************************/
-
-	// WP_Error
-	} elseif ( is_wp_error( $topic_id ) ) {
-		bbp_add_error( 'bbp_topic_error', sprintf( __( '<strong>Error</strong>: The following problem(s) occurred: %s', 'bbpress' ), $topic_id->get_error_message() ) );
-
-	// Generic error
-	} else {
-		bbp_add_error( 'bbp_topic_error', __( '<strong>Error</strong>: The topic was not created.', 'bbpress' ) );
-	}
+	$new_topic = new BBP_Topic( $_POST );
+	$new_topic->save();
 }
 
 /**
