Skip to:
Content

bbPress.org

Ticket #3506: new-topic.patch

File new-topic.patch, 26.1 KB (added by undemian, 2 years ago)
  • includes/common/classes.php

     
    509509        }
    510510}
    511511endif; // class_exists check
     512
     513if ( ! class_exists( 'BBP_Topic' ) ) :
     514        /**
     515         * bbPress Topic Class
     516         *
     517         * The bbPress component class is responsible for simplifying the creation
     518         * of topics
     519         *
     520         * @package bbPress
     521         * @subpackage Classes
     522         *
     523         * @since 2.7
     524         */
     525        class BBP_Topic {
     526          /**
     527           * The id of newly created topic
     528           * @var int
     529           */
     530          public $id;
     531
     532          /**
     533           * Topic data
     534           * @var array|mixed
     535           */
     536          private $data = [];
     537
     538          /**
     539           * Whether we want to bbp_redirect after saving
     540           * @var bool
     541           */
     542          private $should_redirect_after = true;
     543
     544          /** Methods ***************************************************************/
     545
     546          /**
     547           * bbPress create new Topic class
     548           *
     549           * @since 2.7 bbPress ()
     550           *
     551           * @param array   $data                  Optional, topic data (for bbPress frontend it should be $_POST)
     552           * @param boolean $should_redirect_after Optional. Whether we want to redirect after saving
     553           */
     554          public function __construct( $data = [], $should_redirect_after = true ) {
     555                $this->data                  = $data;
     556                $this->should_redirect_after = $should_redirect_after;
     557          }
     558
     559          /**
     560           * Get anonymous data if user is not logged in
     561           * @return array|bool
     562           */
     563          private function get_anonymous_data() {
     564                // Filter anonymous data (variable is used later)
     565                $anonymous_data = bbp_filter_anonymous_post_data( $this->data );
     566
     567                // Anonymous data checks out, so set cookies, etc...
     568                bbp_set_current_anonymous_user_data( $anonymous_data );
     569
     570                return $anonymous_data;
     571          }
     572
     573          /**
     574           * Get topic author id
     575           * @return int
     576           */
     577          private function get_topic_author_id() {
     578                // User cannot create topics
     579                if ( ! current_user_can( 'publish_topics' ) ) {
     580                  bbp_add_error( 'bbp_topic_permission', __( '<strong>Error</strong>: You do not have permission to create new topics.', 'bbpress' ) );
     581                  return 0;
     582                }
     583
     584                // Topic author is current user
     585                return bbp_get_current_user_id();
     586          }
     587
     588          /**
     589           * Get topic validated and sanitized title
     590           * @return string
     591           */
     592          private function get_topic_title() {
     593                $topic_title = '';
     594                if ( ! empty( $this->data['bbp_topic_title'] ) ) {
     595                  $topic_title = sanitize_text_field( $this->data['bbp_topic_title'] );
     596                }
     597
     598                // Filter and sanitize
     599                $topic_title = apply_filters( 'bbp_new_topic_pre_title', $topic_title );
     600
     601                // No topic title
     602                if ( empty( $topic_title ) ) {
     603                  bbp_add_error( 'bbp_topic_title', __( '<strong>Error</strong>: Your topic needs a title.', 'bbpress' ) );
     604                  return '';
     605                }
     606
     607                // Title too long
     608                if ( bbp_is_title_too_long( $topic_title ) ) {
     609                  bbp_add_error( 'bbp_topic_title', __( '<strong>Error</strong>: Your title is too long.', 'bbpress' ) );
     610                  return '';
     611                }
     612
     613                return $topic_title;
     614          }
     615
     616          /**
     617           * Get topic validated and sanitized content
     618           * @return string
     619           */
     620          private function get_topic_content() {
     621                $topic_content = '';
     622                if ( ! empty( $this->data['bbp_topic_content'] ) ) {
     623                  $topic_content = $this->data['bbp_topic_content'];
     624                }
     625
     626                // Filter and sanitize
     627                $topic_content = apply_filters( 'bbp_new_topic_pre_content', $topic_content );
     628
     629                // No topic content
     630                if ( empty( $topic_content ) ) {
     631                  bbp_add_error( 'bbp_topic_content', __( '<strong>Error</strong>: Your topic cannot be empty.', 'bbpress' ) );
     632                  return '';
     633                }
     634
     635                return $topic_content;
     636          }
     637
     638          /**
     639           * Get validated forum id
     640           * @return int
     641           */
     642          private function get_forum_id() {
     643                $forum_id = 0;
     644                // Error check the POST'ed topic id
     645                if ( isset( $this->data['bbp_forum_id'] ) ) {
     646
     647                  // Empty Forum id was passed
     648                  if ( empty( $this->data['bbp_forum_id'] ) ) {
     649                        bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID is missing.', 'bbpress' ) );
     650                        return 0;
     651
     652                        // Forum id is not a number
     653                  } elseif ( ! is_numeric( $this->data['bbp_forum_id'] ) ) {
     654                        bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID must be a number.', 'bbpress' ) );
     655                        return 0;
     656
     657                        // Forum id might be valid
     658                  } else {
     659
     660                        // Get the forum id
     661                        $posted_forum_id = intval( $this->data['bbp_forum_id'] );
     662
     663                        // Forum id is empty
     664                        if ( 0 === $posted_forum_id ) {
     665                          bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID is missing.', 'bbpress' ) );
     666                          return 0;
     667
     668                          // Forum id is a negative number
     669                        } elseif ( 0 > $posted_forum_id ) {
     670                          bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID cannot be a negative number.', 'bbpress' ) );
     671                          return 0;
     672
     673                          // Forum does not exist
     674                        } elseif ( ! bbp_get_forum( $posted_forum_id ) ) {
     675                          bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum does not exist.', 'bbpress' ) );
     676                          return 0;
     677
     678                          // Use the POST'ed forum id
     679                        } else {
     680                          $forum_id = $posted_forum_id;
     681                        }
     682                  }
     683                }
     684
     685                // Forum exists
     686                if ( ! empty( $forum_id ) ) {
     687
     688                  // Forum is a category
     689                  if ( bbp_is_forum_category( $forum_id ) ) {
     690                        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' ) );
     691                        return 0;
     692
     693                        // Forum is not a category
     694                  } else {
     695
     696                        // Forum is closed and user cannot access
     697                        if ( bbp_is_forum_closed( $forum_id ) && ! current_user_can( 'edit_forum', $forum_id ) ) {
     698                          bbp_add_error( 'bbp_new_topic_forum_closed', __( '<strong>Error</strong>: This forum has been closed to new topics.', 'bbpress' ) );
     699                          return 0;
     700                        }
     701
     702                        // Forum is private and user cannot access
     703                        if ( bbp_is_forum_private( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
     704                          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' ) );
     705                          return 0;
     706
     707                          // Forum is hidden and user cannot access
     708                        } elseif ( bbp_is_forum_hidden( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
     709                          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' ) );
     710                          return 0;
     711                        }
     712                  }
     713                }
     714
     715                return $forum_id;
     716          }
     717
     718          /**
     719           * Create and save the topic to db
     720           * @return boolean
     721           */
     722          public function save() {
     723
     724                // Define local variable(s)
     725                $view_all       = false;
     726                $anonymous_data = [];
     727                $topic_author   = 0;
     728                $terms          = [ bbp_get_topic_tag_tax_id() => [] ];
     729
     730                // User is anonymous
     731                if ( bbp_is_anonymous() ) {
     732                  $anonymous_data = $this->get_anonymous_data();
     733
     734                  // User is logged in
     735                } else {
     736                  $topic_author = $this->get_topic_author_id();
     737                  if ( empty( $topic_author ) ) {
     738                        return false;
     739                  }
     740
     741                  // Remove kses filters from title and content for capable users and if the nonce is verified
     742                  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'] ) {
     743                        remove_filter( 'bbp_new_topic_pre_title', 'wp_filter_kses' );
     744                        remove_filter( 'bbp_new_topic_pre_content', 'bbp_encode_bad', 10 );
     745                        remove_filter( 'bbp_new_topic_pre_content', 'bbp_filter_kses', 30 );
     746                  }
     747                }
     748
     749                $topic_title = $this->get_topic_title();
     750                if ( empty( $topic_title ) ) {
     751                  return false;
     752                }
     753
     754                $topic_content = $this->get_topic_content();
     755                if ( empty( $topic_content ) ) {
     756                  return false;
     757                }
     758
     759                $forum_id = $this->get_forum_id();
     760                if ( empty( $forum_id ) ) {
     761                  return false;
     762                }
     763
     764                /** Topic Flooding ********************************************************/
     765
     766                if ( ! bbp_check_for_flood( $anonymous_data, $topic_author ) ) {
     767                  bbp_add_error( 'bbp_topic_flood', __( '<strong>Error</strong>: Slow down; you move too fast.', 'bbpress' ) );
     768                  return false;
     769                }
     770
     771                /** Topic Duplicate *******************************************************/
     772
     773                $dupe_args = array(
     774                  'post_type'      => bbp_get_topic_post_type(),
     775                  'post_author'    => $topic_author,
     776                  'post_content'   => $topic_content,
     777                  'post_parent'    => $forum_id,
     778                  'anonymous_data' => $anonymous_data,
     779                );
     780
     781                if ( ! bbp_check_for_duplicate( $dupe_args ) ) {
     782                  bbp_add_error( 'bbp_topic_duplicate', __( '<strong>Error</strong>: Duplicate topic detected; it looks as though you&#8217;ve already said that.', 'bbpress' ) );
     783                  return false;
     784                }
     785
     786                /** Topic Bad Words *******************************************************/
     787
     788                if ( ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content, true ) ) {
     789                  bbp_add_error( 'bbp_topic_moderation', __( '<strong>Error</strong>: Your topic cannot be created at this time.', 'bbpress' ) );
     790                  return false;
     791                }
     792
     793                /** Topic Status **********************************************************/
     794
     795                // Get available topic statuses
     796                $topic_statuses = bbp_get_topic_statuses();
     797
     798                // Default to published
     799                $topic_status = bbp_get_public_status_id();
     800
     801                // Maybe force into pending
     802                if ( ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content ) ) {
     803                  $topic_status = bbp_get_pending_status_id();
     804
     805                  // Check for possible posted topic status
     806                } elseif ( ! empty( $this->data['bbp_topic_status'] ) && in_array( $this->data['bbp_topic_status'], array_keys( $topic_statuses ), true ) ) {
     807
     808                  // Allow capable users to explicitly override the status
     809                  if ( current_user_can( 'moderate', $forum_id ) ) {
     810                        $topic_status = sanitize_key( $this->data['bbp_topic_status'] );
     811
     812                        // Not capable
     813                  } else {
     814                        bbp_add_error( 'bbp_new_topic_status', __( '<strong>Error</strong>: You do not have permission to do that.', 'bbpress' ) );
     815                        return false;
     816                  }
     817                }
     818
     819                /** Topic Tags ************************************************************/
     820
     821                if ( bbp_allow_topic_tags() && ! empty( $this->data['bbp_topic_tags'] ) ) {
     822
     823                  // Escape tag input
     824                  $terms = sanitize_text_field( $this->data['bbp_topic_tags'] );
     825
     826                  // Explode by comma
     827                  if ( strstr( $terms, ',' ) ) {
     828                        $terms = explode( ',', $terms );
     829                  }
     830
     831                  // Add topic tag ID as main key
     832                  $terms = array( bbp_get_topic_tag_tax_id() => $terms );
     833                }
     834
     835                /** Additional Actions (Before Save) **************************************/
     836
     837                do_action( 'bbp_new_topic_pre_extras', $forum_id );
     838
     839                // Bail if errors
     840                if ( bbp_has_errors() ) {
     841                  return false;
     842                }
     843
     844                /** No Errors *************************************************************/
     845
     846                // Add the content of the form to $topic_data as an array.
     847                // Just in time manipulation of topic data before being created
     848                $topic_data = apply_filters(
     849                  'bbp_new_topic_pre_insert',
     850                  [
     851                        'post_author'    => $topic_author,
     852                        'post_title'     => $topic_title,
     853                        'post_content'   => $topic_content,
     854                        'post_status'    => $topic_status,
     855                        'post_parent'    => $forum_id,
     856                        'post_type'      => bbp_get_topic_post_type(),
     857                        'tax_input'      => $terms,
     858                        'comment_status' => 'closed',
     859                  ]
     860                );
     861
     862                // Insert topic
     863                $topic_id = wp_insert_post( $topic_data, true );
     864                $this->id = $topic_id;
     865
     866                /** No Errors *************************************************************/
     867
     868                if ( ! empty( $topic_id ) && ! is_wp_error( $topic_id ) ) {
     869
     870                  /** Close Check *******************************************************/
     871
     872                  // If the topic is closed, close it properly
     873                  if ( ( get_post_field( 'post_status', $topic_id ) === bbp_get_closed_status_id() ) || ( $topic_data['post_status'] === bbp_get_closed_status_id() ) ) {
     874
     875                        // Close the topic
     876                        bbp_close_topic( $topic_id );
     877                  }
     878
     879                  /** Trash Check *******************************************************/
     880
     881                  // If the forum is trash, or the topic_status is switched to
     882                  // trash, trash the topic properly
     883                  if ( ( get_post_field( 'post_status', $forum_id ) === bbp_get_trash_status_id() ) || ( $topic_data['post_status'] === bbp_get_trash_status_id() ) ) {
     884
     885                        // Trash the topic
     886                        wp_trash_post( $topic_id );
     887
     888                        // Force view=all
     889                        $view_all = true;
     890                  }
     891
     892                  /** Spam Check ********************************************************/
     893
     894                  // If the topic is spam, officially spam this topic
     895                  if ( $topic_data['post_status'] === bbp_get_spam_status_id() ) {
     896                        add_post_meta( $topic_id, '_bbp_spam_meta_status', bbp_get_public_status_id() );
     897
     898                        // Force view=all
     899                        $view_all = true;
     900                  }
     901
     902                  /** Update counts, etc... *********************************************/
     903
     904                  do_action( 'bbp_new_topic', $topic_id, $forum_id, $anonymous_data, $topic_author );
     905
     906                  /** Additional Actions (After Save) ***********************************/
     907
     908                  do_action( 'bbp_new_topic_post_extras', $topic_id );
     909
     910                  // If it is not the bbp frontend we do not need to redirect.
     911                  if ( ! $this->should_redirect_after ) {
     912                        return true;
     913                  }
     914
     915                  /** Redirect **********************************************************/
     916
     917                  // Redirect to
     918                  $redirect_to = bbp_get_redirect_to();
     919
     920                  // Get the topic URL
     921                  $redirect_url = bbp_get_topic_permalink( $topic_id, $redirect_to );
     922
     923                  // Add view all?
     924                  if ( bbp_get_view_all() || ! empty( $view_all ) ) {
     925
     926                        // User can moderate, so redirect to topic with view all set
     927                        if ( current_user_can( 'moderate', $topic_id ) ) {
     928                          $redirect_url = bbp_add_view_all( $redirect_url );
     929
     930                          // User cannot moderate, so redirect to forum
     931                        } else {
     932                          $redirect_url = bbp_get_forum_permalink( $forum_id );
     933                        }
     934                  }
     935
     936                  // Allow to be filtered
     937                  $redirect_url = apply_filters( 'bbp_new_topic_redirect_to', $redirect_url, $redirect_to, $topic_id );
     938
     939                  /** Successful Save ***************************************************/
     940
     941                  // Redirect back to new topic
     942                  bbp_redirect( $redirect_url );
     943
     944                  /** Errors ****************************************************************/
     945
     946                  // WP_Error
     947                } elseif ( is_wp_error( $topic_id ) ) {
     948                  /* translators: 1: Error message */
     949                  bbp_add_error( 'bbp_topic_error', sprintf( __( '<strong>Error</strong>: The following problem(s) occurred: %s', 'bbpress' ), $topic_id->get_error_message() ) );
     950                  return false;
     951                  // Generic error
     952                } else {
     953                  bbp_add_error( 'bbp_topic_error', __( '<strong>Error</strong>: The topic was not created.', 'bbpress' ) );
     954                  return false;
     955                }
     956          }
     957        }
     958  endif; // BBP_Topic
     959
  • includes/topics/functions.php

     
    106106                return;
    107107        }
    108108
    109         // Define local variable(s)
    110         $view_all = false;
    111         $forum_id = $topic_author = 0;
    112         $topic_title = $topic_content = '';
    113         $anonymous_data = array();
    114         $terms = array( bbp_get_topic_tag_tax_id() => array() );
    115 
    116         /** Topic Author **********************************************************/
    117 
    118         // User is anonymous
    119         if ( bbp_is_anonymous() ) {
    120 
    121                 // Filter anonymous data (variable is used later)
    122                 $anonymous_data = bbp_filter_anonymous_post_data();
    123 
    124                 // Anonymous data checks out, so set cookies, etc...
    125                 bbp_set_current_anonymous_user_data( $anonymous_data );
    126 
    127         // User is logged in
    128         } else {
    129 
    130                 // User cannot create topics
    131                 if ( ! current_user_can( 'publish_topics' ) ) {
    132                         bbp_add_error( 'bbp_topic_permission', __( '<strong>Error</strong>: You do not have permission to create new topics.', 'bbpress' ) );
    133                         return;
    134                 }
    135 
    136                 // Topic author is current user
    137                 $topic_author = bbp_get_current_user_id();
    138         }
    139 
    140         // Remove kses filters from title and content for capable users and if the nonce is verified
    141         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'] ) {
    142                 remove_filter( 'bbp_new_topic_pre_title',   'wp_filter_kses'      );
    143                 remove_filter( 'bbp_new_topic_pre_content', 'bbp_encode_bad',  10 );
    144                 remove_filter( 'bbp_new_topic_pre_content', 'bbp_filter_kses', 30 );
    145         }
    146 
    147         /** Topic Title ***********************************************************/
    148 
    149         if ( ! empty( $_POST['bbp_topic_title'] ) ) {
    150                 $topic_title = sanitize_text_field( $_POST['bbp_topic_title'] );
    151         }
    152 
    153         // Filter and sanitize
    154         $topic_title = apply_filters( 'bbp_new_topic_pre_title', $topic_title );
    155 
    156         // No topic title
    157         if ( empty( $topic_title ) ) {
    158                 bbp_add_error( 'bbp_topic_title', __( '<strong>Error</strong>: Your topic needs a title.', 'bbpress' ) );
    159         }
    160 
    161         // Title too long
    162         if ( bbp_is_title_too_long( $topic_title ) ) {
    163                 bbp_add_error( 'bbp_topic_title', __( '<strong>Error</strong>: Your title is too long.', 'bbpress' ) );
    164         }
    165 
    166         /** Topic Content *********************************************************/
    167 
    168         if ( ! empty( $_POST['bbp_topic_content'] ) ) {
    169                 $topic_content = $_POST['bbp_topic_content'];
    170         }
    171 
    172         // Filter and sanitize
    173         $topic_content = apply_filters( 'bbp_new_topic_pre_content', $topic_content );
    174 
    175         // No topic content
    176         if ( empty( $topic_content ) ) {
    177                 bbp_add_error( 'bbp_topic_content', __( '<strong>Error</strong>: Your topic cannot be empty.', 'bbpress' ) );
    178         }
    179 
    180         /** Topic Forum ***********************************************************/
    181 
    182         // Error check the POST'ed topic id
    183         if ( isset( $_POST['bbp_forum_id'] ) ) {
    184 
    185                 // Empty Forum id was passed
    186                 if ( empty( $_POST['bbp_forum_id'] ) ) {
    187                         bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID is missing.', 'bbpress' ) );
    188 
    189                 // Forum id is not a number
    190                 } elseif ( ! is_numeric( $_POST['bbp_forum_id'] ) ) {
    191                         bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID must be a number.', 'bbpress' ) );
    192 
    193                 // Forum id might be valid
    194                 } else {
    195 
    196                         // Get the forum id
    197                         $posted_forum_id = intval( $_POST['bbp_forum_id'] );
    198 
    199                         // Forum id is empty
    200                         if ( 0 === $posted_forum_id ) {
    201                                 bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID is missing.', 'bbpress' ) );
    202 
    203                         // Forum id is a negative number
    204                         } elseif ( 0 > $posted_forum_id ) {
    205                                 bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID cannot be a negative number.', 'bbpress' ) );
    206 
    207                         // Forum does not exist
    208                         } elseif ( ! bbp_get_forum( $posted_forum_id ) ) {
    209                                 bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum does not exist.', 'bbpress' ) );
    210 
    211                         // Use the POST'ed forum id
    212                         } else {
    213                                 $forum_id = $posted_forum_id;
    214                         }
    215                 }
    216         }
    217 
    218         // Forum exists
    219         if ( ! empty( $forum_id ) ) {
    220 
    221                 // Forum is a category
    222                 if ( bbp_is_forum_category( $forum_id ) ) {
    223                         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' ) );
    224 
    225                 // Forum is not a category
    226                 } else {
    227 
    228                         // Forum is closed and user cannot access
    229                         if ( bbp_is_forum_closed( $forum_id ) && ! current_user_can( 'edit_forum', $forum_id ) ) {
    230                                 bbp_add_error( 'bbp_new_topic_forum_closed', __( '<strong>Error</strong>: This forum has been closed to new topics.', 'bbpress' ) );
    231                         }
    232 
    233                         // Forum is private and user cannot access
    234                         if ( bbp_is_forum_private( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
    235                                 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' ) );
    236 
    237                         // Forum is hidden and user cannot access
    238                         } elseif ( bbp_is_forum_hidden( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
    239                                 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' ) );
    240                         }
    241                 }
    242         }
    243 
    244         /** Topic Flooding ********************************************************/
    245 
    246         if ( ! bbp_check_for_flood( $anonymous_data, $topic_author ) ) {
    247                 bbp_add_error( 'bbp_topic_flood', __( '<strong>Error</strong>: Slow down; you move too fast.', 'bbpress' ) );
    248         }
    249 
    250         /** Topic Duplicate *******************************************************/
    251 
    252         $dupe_args = array(
    253                 'post_type'      => bbp_get_topic_post_type(),
    254                 'post_author'    => $topic_author,
    255                 'post_content'   => $topic_content,
    256                 'post_parent'    => $forum_id,
    257                 'anonymous_data' => $anonymous_data
    258         );
    259 
    260         if ( ! bbp_check_for_duplicate( $dupe_args ) ) {
    261                 bbp_add_error( 'bbp_topic_duplicate', __( '<strong>Error</strong>: Duplicate topic detected; it looks as though you&#8217;ve already said that.', 'bbpress' ) );
    262         }
    263 
    264         /** Topic Bad Words *******************************************************/
    265 
    266         if ( ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content, true ) ) {
    267                 bbp_add_error( 'bbp_topic_moderation', __( '<strong>Error</strong>: Your topic cannot be created at this time.', 'bbpress' ) );
    268         }
    269 
    270         /** Topic Status **********************************************************/
    271 
    272         // Get available topic statuses
    273         $topic_statuses = bbp_get_topic_statuses();
    274 
    275         // Default to published
    276         $topic_status = bbp_get_public_status_id();
    277 
    278         // Maybe force into pending
    279         if ( ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content ) ) {
    280                 $topic_status = bbp_get_pending_status_id();
    281 
    282         // Check for possible posted topic status
    283         } elseif ( ! empty( $_POST['bbp_topic_status'] ) && in_array( $_POST['bbp_topic_status'], array_keys( $topic_statuses ), true ) ) {
    284 
    285                 // Allow capable users to explicitly override the status
    286                 if ( current_user_can( 'moderate', $forum_id ) ) {
    287                         $topic_status = sanitize_key( $_POST['bbp_topic_status'] );
    288 
    289                 // Not capable
    290                 } else {
    291                         bbp_add_error( 'bbp_new_topic_status', __( '<strong>Error</strong>: You do not have permission to do that.', 'bbpress' ) );
    292                 }
    293         }
    294 
    295         /** Topic Tags ************************************************************/
    296 
    297         if ( bbp_allow_topic_tags() && ! empty( $_POST['bbp_topic_tags'] ) ) {
    298 
    299                 // Escape tag input
    300                 $terms = sanitize_text_field( $_POST['bbp_topic_tags'] );
    301 
    302                 // Explode by comma
    303                 if ( strstr( $terms, ',' ) ) {
    304                         $terms = explode( ',', $terms );
    305                 }
    306 
    307                 // Add topic tag ID as main key
    308                 $terms = array( bbp_get_topic_tag_tax_id() => $terms );
    309         }
    310 
    311         /** Additional Actions (Before Save) **************************************/
    312 
    313         do_action( 'bbp_new_topic_pre_extras', $forum_id );
    314 
    315         // Bail if errors
    316         if ( bbp_has_errors() ) {
    317                 return;
    318         }
    319 
    320         /** No Errors *************************************************************/
    321 
    322         // Add the content of the form to $topic_data as an array.
    323         // Just in time manipulation of topic data before being created
    324         $topic_data = apply_filters( 'bbp_new_topic_pre_insert', array(
    325                 'post_author'    => $topic_author,
    326                 'post_title'     => $topic_title,
    327                 'post_content'   => $topic_content,
    328                 'post_status'    => $topic_status,
    329                 'post_parent'    => $forum_id,
    330                 'post_type'      => bbp_get_topic_post_type(),
    331                 'tax_input'      => $terms,
    332                 'comment_status' => 'closed'
    333         ) );
    334 
    335         // Insert topic
    336         $topic_id = wp_insert_post( $topic_data, true );
    337 
    338         /** No Errors *************************************************************/
    339 
    340         if ( ! empty( $topic_id ) && ! is_wp_error( $topic_id ) ) {
    341 
    342                 /** Close Check *******************************************************/
    343 
    344                 // If the topic is closed, close it properly
    345                 if ( ( get_post_field( 'post_status', $topic_id ) === bbp_get_closed_status_id() ) || ( $topic_data['post_status'] === bbp_get_closed_status_id() ) ) {
    346 
    347                         // Close the topic
    348                         bbp_close_topic( $topic_id );
    349                 }
    350 
    351                 /** Trash Check *******************************************************/
    352 
    353                 // If the forum is trash, or the topic_status is switched to
    354                 // trash, trash the topic properly
    355                 if ( ( get_post_field( 'post_status', $forum_id ) === bbp_get_trash_status_id() ) || ( $topic_data['post_status'] === bbp_get_trash_status_id() ) ) {
    356 
    357                         // Trash the topic
    358                         wp_trash_post( $topic_id );
    359 
    360                         // Force view=all
    361                         $view_all = true;
    362                 }
    363 
    364                 /** Spam Check ********************************************************/
    365 
    366                 // If the topic is spam, officially spam this topic
    367                 if ( $topic_data['post_status'] === bbp_get_spam_status_id() ) {
    368                         add_post_meta( $topic_id, '_bbp_spam_meta_status', bbp_get_public_status_id() );
    369 
    370                         // Force view=all
    371                         $view_all = true;
    372                 }
    373 
    374                 /** Update counts, etc... *********************************************/
    375 
    376                 do_action( 'bbp_new_topic', $topic_id, $forum_id, $anonymous_data, $topic_author );
    377 
    378                 /** Additional Actions (After Save) ***********************************/
    379 
    380                 do_action( 'bbp_new_topic_post_extras', $topic_id );
    381 
    382                 /** Redirect **********************************************************/
    383 
    384                 // Redirect to
    385                 $redirect_to = bbp_get_redirect_to();
    386 
    387                 // Get the topic URL
    388                 $redirect_url = bbp_get_topic_permalink( $topic_id, $redirect_to );
    389 
    390                 // Add view all?
    391                 if ( bbp_get_view_all() || ! empty( $view_all ) ) {
    392 
    393                         // User can moderate, so redirect to topic with view all set
    394                         if ( current_user_can( 'moderate', $topic_id ) ) {
    395                                 $redirect_url = bbp_add_view_all( $redirect_url );
    396 
    397                         // User cannot moderate, so redirect to forum
    398                         } else {
    399                                 $redirect_url = bbp_get_forum_permalink( $forum_id );
    400                         }
    401                 }
    402 
    403                 // Allow to be filtered
    404                 $redirect_url = apply_filters( 'bbp_new_topic_redirect_to', $redirect_url, $redirect_to, $topic_id );
    405 
    406                 /** Successful Save ***************************************************/
    407 
    408                 // Redirect back to new topic
    409                 bbp_redirect( $redirect_url );
    410 
    411         /** Errors ****************************************************************/
    412 
    413         // WP_Error
    414         } elseif ( is_wp_error( $topic_id ) ) {
    415                 bbp_add_error( 'bbp_topic_error', sprintf( __( '<strong>Error</strong>: The following problem(s) occurred: %s', 'bbpress' ), $topic_id->get_error_message() ) );
    416 
    417         // Generic error
    418         } else {
    419                 bbp_add_error( 'bbp_topic_error', __( '<strong>Error</strong>: The topic was not created.', 'bbpress' ) );
    420         }
     109        $new_topic = new BBP_Topic( $_POST );
     110        $new_topic->save();
    421111}
    422112
    423113/**