bbp_new_topic_handler

Handles the front end topic submission.

Description

bbp_new_topic_handler( (string) $action = '' ); 

Parameters (1)

0. $action — Optional. (string) => ''
The requested action to compare this function to

Usage

  1. if ( !function_exists( 'bbp_new_topic_handler' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/topics/functions.php'; 
  3.  
  4. // The requested action to compare this function to 
  5. $action = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bbp_new_topic_handler($action); 
  9.  

Defined (1)

The function is defined in the following location(s).

/includes/topics/functions.php  
  1. function bbp_new_topic_handler( $action = '' ) { 
  2.  
  3. // Bail if action is not bbp-new-topic 
  4. if ( 'bbp-new-topic' !== $action ) 
  5. return; 
  6.  
  7. // Nonce check 
  8. if ( ! bbp_verify_nonce_request( 'bbp-new-topic' ) ) { 
  9. bbp_add_error( 'bbp_new_topic_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', bbpress ) ); 
  10. return; 
  11.  
  12. // Define local variable(s) 
  13. $view_all = false; 
  14. $forum_id = $topic_author = $anonymous_data = 0; 
  15. $topic_title = $topic_content = ''; 
  16. $terms = array( bbp_get_topic_tag_tax_id() => array() ); 
  17.  
  18. /** Topic Author **********************************************************/ 
  19.  
  20. // User is anonymous 
  21. if ( bbp_is_anonymous() ) { 
  22.  
  23. // Filter anonymous data 
  24. $anonymous_data = bbp_filter_anonymous_post_data(); 
  25.  
  26. // Anonymous data checks out, so set cookies, etc... 
  27. if ( !empty( $anonymous_data ) && is_array( $anonymous_data ) ) { 
  28.  
  29. // User is logged in 
  30. } else { 
  31.  
  32. // User cannot create topics 
  33. if ( !current_user_can( 'publish_topics' ) ) { 
  34. bbp_add_error( 'bbp_topic_permissions', __( '<strong>ERROR</strong>: You do not have permission to create new topics.', bbpress ) ); 
  35. return; 
  36.  
  37. // Topic author is current user 
  38. $topic_author = bbp_get_current_user_id(); 
  39.  
  40. // Remove kses filters from title and content for capable users and if the nonce is verified 
  41. 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'] ) { 
  42. remove_filter( 'bbp_new_topic_pre_title', 'wp_filter_kses' ); 
  43. remove_filter( 'bbp_new_topic_pre_content', 'bbp_encode_bad', 10 ); 
  44. remove_filter( 'bbp_new_topic_pre_content', 'bbp_filter_kses', 30 ); 
  45.  
  46. /** Topic Title ***********************************************************/ 
  47.  
  48. if ( !empty( $_POST['bbp_topic_title'] ) ) 
  49. $topic_title = esc_attr( strip_tags( $_POST['bbp_topic_title'] ) ); 
  50.  
  51. // Filter and sanitize 
  52. $topic_title = apply_filters( 'bbp_new_topic_pre_title', $topic_title ); 
  53.  
  54. // No topic title 
  55. if ( empty( $topic_title ) ) 
  56. bbp_add_error( 'bbp_topic_title', __( '<strong>ERROR</strong>: Your topic needs a title.', bbpress ) ); 
  57.  
  58. /** Topic Content *********************************************************/ 
  59.  
  60. if ( !empty( $_POST['bbp_topic_content'] ) ) 
  61. $topic_content = $_POST['bbp_topic_content']; 
  62.  
  63. // Filter and sanitize 
  64. $topic_content = apply_filters( 'bbp_new_topic_pre_content', $topic_content ); 
  65.  
  66. // No topic content 
  67. if ( empty( $topic_content ) ) 
  68. bbp_add_error( 'bbp_topic_content', __( '<strong>ERROR</strong>: Your topic cannot be empty.', bbpress ) ); 
  69.  
  70. /** Topic Forum ***********************************************************/ 
  71.  
  72. // Error check the POST'ed topic id 
  73. if ( isset( $_POST['bbp_forum_id'] ) ) { 
  74.  
  75. // Empty Forum id was passed 
  76. if ( empty( $_POST['bbp_forum_id'] ) ) { 
  77. bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', bbpress ) ); 
  78.  
  79. // Forum id is not a number 
  80. } elseif ( ! is_numeric( $_POST['bbp_forum_id'] ) ) { 
  81. bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID must be a number.', bbpress ) ); 
  82.  
  83. // Forum id might be valid 
  84. } else { 
  85.  
  86. // Get the forum id 
  87. $posted_forum_id = intval( $_POST['bbp_forum_id'] ); 
  88.  
  89. // Forum id is empty 
  90. if ( 0 === $posted_forum_id ) { 
  91. bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', bbpress ) ); 
  92.  
  93. // Forum id is a negative number 
  94. } elseif ( 0 > $posted_forum_id ) { 
  95. bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID cannot be a negative number.', bbpress ) ); 
  96.  
  97. // Forum does not exist 
  98. } elseif ( ! bbp_get_forum( $posted_forum_id ) ) { 
  99. bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum does not exist.', bbpress ) ); 
  100.  
  101. // Use the POST'ed forum id 
  102. } else { 
  103. $forum_id = $posted_forum_id; 
  104.  
  105. // Forum exists 
  106. if ( !empty( $forum_id ) ) { 
  107.  
  108. // Forum is a category 
  109. if ( bbp_is_forum_category( $forum_id ) ) { 
  110. 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 ) ); 
  111.  
  112. // Forum is not a category 
  113. } else { 
  114.  
  115. // Forum is closed and user cannot access 
  116. if ( bbp_is_forum_closed( $forum_id ) && !current_user_can( 'edit_forum', $forum_id ) ) { 
  117. bbp_add_error( 'bbp_new_topic_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new topics.', bbpress ) ); 
  118.  
  119. // Forum is private and user cannot access 
  120. if ( bbp_is_forum_private( $forum_id ) ) { 
  121. if ( !current_user_can( 'read_private_forums' ) ) { 
  122. 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 ) ); 
  123.  
  124. // Forum is hidden and user cannot access 
  125. } elseif ( bbp_is_forum_hidden( $forum_id ) ) { 
  126. if ( !current_user_can( 'read_hidden_forums' ) ) { 
  127. 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 ) ); 
  128.  
  129. /** Topic Flooding ********************************************************/ 
  130.  
  131. if ( !bbp_check_for_flood( $anonymous_data, $topic_author ) ) 
  132. bbp_add_error( 'bbp_topic_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', bbpress ) ); 
  133.  
  134. /** Topic Duplicate *******************************************************/ 
  135.  
  136. if ( !bbp_check_for_duplicate( array( 'post_type' => bbp_get_topic_post_type(), 'post_author' => $topic_author, 'post_content' => $topic_content, 'anonymous_data' => $anonymous_data ) ) ) 
  137. bbp_add_error( 'bbp_topic_duplicate', __( '<strong>ERROR</strong>: Duplicate topic detected; it looks as though you’ve already said that!', bbpress ) ); 
  138.  
  139. /** Topic Blacklist *******************************************************/ 
  140.  
  141. if ( !bbp_check_for_blacklist( $anonymous_data, $topic_author, $topic_title, $topic_content ) ) 
  142. bbp_add_error( 'bbp_topic_blacklist', __( '<strong>ERROR</strong>: Your topic cannot be created at this time.', bbpress ) ); 
  143.  
  144. /** Topic Status **********************************************************/ 
  145.  
  146. // Maybe put into moderation 
  147. if ( !bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content ) ) { 
  148. $topic_status = bbp_get_pending_status_id(); 
  149.  
  150. // Check a whitelist of possible topic status ID's 
  151. } elseif ( !empty( $_POST['bbp_topic_status'] ) && in_array( $_POST['bbp_topic_status'], array_keys( bbp_get_topic_statuses() ) ) ) { 
  152. $topic_status = $_POST['bbp_topic_status']; 
  153.  
  154. // Default to published if nothing else 
  155. } else { 
  156. $topic_status = bbp_get_public_status_id(); 
  157.  
  158. /** Topic Tags ************************************************************/ 
  159.  
  160. if ( bbp_allow_topic_tags() && !empty( $_POST['bbp_topic_tags'] ) ) { 
  161.  
  162. // Escape tag input 
  163. $terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) ); 
  164.  
  165. // Explode by comma 
  166. if ( strstr( $terms, ', ' ) ) { 
  167. $terms = explode( ', ', $terms ); 
  168.  
  169. // Add topic tag ID as main key 
  170. $terms = array( bbp_get_topic_tag_tax_id() => $terms ); 
  171.  
  172. /** Additional Actions (Before Save) **************************************/ 
  173.  
  174. do_action( 'bbp_new_topic_pre_extras', $forum_id ); 
  175.  
  176. // Bail if errors 
  177. if ( bbp_has_errors() ) 
  178. return; 
  179.  
  180. /** No Errors *************************************************************/ 
  181.  
  182. // Add the content of the form to $topic_data as an array. 
  183. // Just in time manipulation of topic data before being created 
  184. $topic_data = apply_filters( 'bbp_new_topic_pre_insert', array( 
  185. 'post_author' => $topic_author,  
  186. 'post_title' => $topic_title,  
  187. 'post_content' => $topic_content,  
  188. 'post_status' => $topic_status,  
  189. 'post_parent' => $forum_id,  
  190. 'post_type' => bbp_get_topic_post_type(),  
  191. 'tax_input' => $terms,  
  192. 'comment_status' => 'closed' 
  193. ) ); 
  194.  
  195. // Insert topic 
  196. $topic_id = wp_insert_post( $topic_data ); 
  197.  
  198. /** No Errors *************************************************************/ 
  199.  
  200. if ( !empty( $topic_id ) && !is_wp_error( $topic_id ) ) { 
  201.  
  202. /** Trash Check *******************************************************/ 
  203.  
  204. // If the forum is trash, or the topic_status is switched to 
  205. // trash, trash it properly 
  206. if ( ( get_post_field( 'post_status', $forum_id ) === bbp_get_trash_status_id() ) || ( $topic_data['post_status'] === bbp_get_trash_status_id() ) ) { 
  207.  
  208. // Trash the reply 
  209. wp_trash_post( $topic_id ); 
  210.  
  211. // Force view=all 
  212. $view_all = true; 
  213.  
  214. /** Spam Check ********************************************************/ 
  215.  
  216. // If reply or topic are spam, officially spam this reply 
  217. if ( $topic_data['post_status'] === bbp_get_spam_status_id() ) { 
  218. add_post_meta( $topic_id, '_bbp_spam_meta_status', bbp_get_public_status_id() ); 
  219.  
  220. // Force view=all 
  221. $view_all = true; 
  222.  
  223. /** Update counts, etc... *********************************************/ 
  224.  
  225. do_action( 'bbp_new_topic', $topic_id, $forum_id, $anonymous_data, $topic_author ); 
  226.  
  227. /** Stickies **********************************************************/ 
  228.  
  229. // Sticky check after 'bbp_new_topic' action so forum ID meta is set 
  230. if ( !empty( $_POST['bbp_stick_topic'] ) && in_array( $_POST['bbp_stick_topic'], array( 'stick', 'super', 'unstick' ) ) ) { 
  231.  
  232. // What's the caps? 
  233. if ( current_user_can( 'moderate' ) ) { 
  234.  
  235. // What's the haps? 
  236. switch ( $_POST['bbp_stick_topic'] ) { 
  237.  
  238. // Sticky in this forum 
  239. case 'stick' : 
  240. bbp_stick_topic( $topic_id ); 
  241. break; 
  242.  
  243. // Super sticky in all forums 
  244. case 'super' : 
  245. bbp_stick_topic( $topic_id, true ); 
  246. break; 
  247.  
  248. // We can avoid this as it is a new topic 
  249. case 'unstick' : 
  250. default : 
  251. break; 
  252.  
  253. /** Additional Actions (After Save) ***********************************/ 
  254.  
  255. do_action( 'bbp_new_topic_post_extras', $topic_id ); 
  256.  
  257. /** Redirect **********************************************************/ 
  258.  
  259. // Redirect to 
  260. $redirect_to = bbp_get_redirect_to(); 
  261.  
  262. // Get the topic URL 
  263. $redirect_url = bbp_get_topic_permalink( $topic_id, $redirect_to ); 
  264.  
  265. // Add view all? 
  266. if ( bbp_get_view_all() || !empty( $view_all ) ) { 
  267.  
  268. // User can moderate, so redirect to topic with view all set 
  269. if ( current_user_can( 'moderate' ) ) { 
  270. $redirect_url = bbp_add_view_all( $redirect_url ); 
  271.  
  272. // User cannot moderate, so redirect to forum 
  273. } else { 
  274. $redirect_url = bbp_get_forum_permalink( $forum_id ); 
  275.  
  276. // Allow to be filtered 
  277. $redirect_url = apply_filters( 'bbp_new_topic_redirect_to', $redirect_url, $redirect_to, $topic_id ); 
  278.  
  279. /** Successful Save ***************************************************/ 
  280.  
  281. // Redirect back to new topic 
  282. wp_safe_redirect( $redirect_url ); 
  283.  
  284. // For good measure 
  285. exit(); 
  286.  
  287. // Errors 
  288. } else { 
  289. $append_error = ( is_wp_error( $topic_id ) && $topic_id->get_error_message() ) ? $topic_id->get_error_message() . ' ' : ''; 
  290. bbp_add_error( 'bbp_topic_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your topic:' . $append_error, bbpress ) );