bbp_new_forum_handler

Handles the front end forum submission.

Description

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

Parameters (1)

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

Usage

  1. if ( !function_exists( 'bbp_new_forum_handler' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/forums/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_forum_handler($action); 
  9.  

Defined (1)

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

/includes/forums/functions.php  
  1. function bbp_new_forum_handler( $action = '' ) { 
  2.  
  3. // Bail if action is not bbp-new-forum 
  4. if ( 'bbp-new-forum' !== $action ) 
  5. return; 
  6.  
  7. // Nonce check 
  8. if ( ! bbp_verify_nonce_request( 'bbp-new-forum' ) ) { 
  9. bbp_add_error( 'bbp_new_forum_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', bbpress ) ); 
  10. return; 
  11.  
  12. // Define local variable(s) 
  13. $view_all = $anonymous_data = false; 
  14. $forum_parent_id = $forum_author = 0; 
  15. $forum_title = $forum_content = ''; 
  16.  
  17. /** Forum Author **********************************************************/ 
  18.  
  19. // User cannot create forums 
  20. if ( !current_user_can( 'publish_forums' ) ) { 
  21. bbp_add_error( 'bbp_forum_permissions', __( '<strong>ERROR</strong>: You do not have permission to create new forums.', bbpress ) ); 
  22. return; 
  23.  
  24. // Forum author is current user 
  25. $forum_author = bbp_get_current_user_id(); 
  26.  
  27. // Remove kses filters from title and content for capable users and if the nonce is verified 
  28. if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_forum'] ) && wp_create_nonce( 'bbp-unfiltered-html-forum_new' ) === $_POST['_bbp_unfiltered_html_forum'] ) { 
  29. remove_filter( 'bbp_new_forum_pre_title', 'wp_filter_kses' ); 
  30. remove_filter( 'bbp_new_forum_pre_content', 'bbp_encode_bad', 10 ); 
  31. remove_filter( 'bbp_new_forum_pre_content', 'bbp_filter_kses', 30 ); 
  32.  
  33. /** Forum Title ***********************************************************/ 
  34.  
  35. if ( !empty( $_POST['bbp_forum_title'] ) ) 
  36. $forum_title = esc_attr( strip_tags( $_POST['bbp_forum_title'] ) ); 
  37.  
  38. // Filter and sanitize 
  39. $forum_title = apply_filters( 'bbp_new_forum_pre_title', $forum_title ); 
  40.  
  41. // No forum title 
  42. if ( empty( $forum_title ) ) 
  43. bbp_add_error( 'bbp_forum_title', __( '<strong>ERROR</strong>: Your forum needs a title.', bbpress ) ); 
  44.  
  45. /** Forum Content *********************************************************/ 
  46.  
  47. if ( !empty( $_POST['bbp_forum_content'] ) ) 
  48. $forum_content = $_POST['bbp_forum_content']; 
  49.  
  50. // Filter and sanitize 
  51. $forum_content = apply_filters( 'bbp_new_forum_pre_content', $forum_content ); 
  52.  
  53. // No forum content 
  54. if ( empty( $forum_content ) ) 
  55. bbp_add_error( 'bbp_forum_content', __( '<strong>ERROR</strong>: Your forum description cannot be empty.', bbpress ) ); 
  56.  
  57. /** Forum Parent **********************************************************/ 
  58.  
  59. // Forum parent was passed (the norm) 
  60. if ( !empty( $_POST['bbp_forum_parent_id'] ) ) { 
  61. $forum_parent_id = bbp_get_forum_id( $_POST['bbp_forum_parent_id'] ); 
  62.  
  63. // Filter and sanitize 
  64. $forum_parent_id = apply_filters( 'bbp_new_forum_pre_parent_id', $forum_parent_id ); 
  65.  
  66. // No forum parent was passed (should never happen) 
  67. if ( empty( $forum_parent_id ) ) { 
  68. bbp_add_error( 'bbp_new_forum_missing_parent', __( '<strong>ERROR</strong>: Your forum must have a parent.', bbpress ) ); 
  69.  
  70. // Forum exists 
  71. } elseif ( !empty( $forum_parent_id ) ) { 
  72.  
  73. // Forum is a category 
  74. if ( bbp_is_forum_category( $forum_parent_id ) ) { 
  75. bbp_add_error( 'bbp_new_forum_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No forums can be created in this forum.', bbpress ) ); 
  76.  
  77. // Forum is closed and user cannot access 
  78. if ( bbp_is_forum_closed( $forum_parent_id ) && !current_user_can( 'edit_forum', $forum_parent_id ) ) { 
  79. bbp_add_error( 'bbp_new_forum_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new forums.', bbpress ) ); 
  80.  
  81. // Forum is private and user cannot access 
  82. if ( bbp_is_forum_private( $forum_parent_id ) && !current_user_can( 'read_private_forums' ) ) { 
  83. bbp_add_error( 'bbp_new_forum_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new forums in it.', bbpress ) ); 
  84.  
  85. // Forum is hidden and user cannot access 
  86. if ( bbp_is_forum_hidden( $forum_parent_id ) && !current_user_can( 'read_hidden_forums' ) ) { 
  87. bbp_add_error( 'bbp_new_forum_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new forums in it.', bbpress ) ); 
  88.  
  89. /** Forum Flooding ********************************************************/ 
  90.  
  91. if ( !bbp_check_for_flood( $anonymous_data, $forum_author ) ) 
  92. bbp_add_error( 'bbp_forum_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', bbpress ) ); 
  93.  
  94. /** Forum Duplicate *******************************************************/ 
  95.  
  96. if ( !bbp_check_for_duplicate( array( 'post_type' => bbp_get_forum_post_type(), 'post_author' => $forum_author, 'post_content' => $forum_content, 'anonymous_data' => $anonymous_data ) ) ) 
  97. bbp_add_error( 'bbp_forum_duplicate', __( '<strong>ERROR</strong>: This forum already exists.', bbpress ) ); 
  98.  
  99. /** Forum Blacklist *******************************************************/ 
  100.  
  101. if ( !bbp_check_for_blacklist( $anonymous_data, $forum_author, $forum_title, $forum_content ) ) 
  102. bbp_add_error( 'bbp_forum_blacklist', __( '<strong>ERROR</strong>: Your forum cannot be created at this time.', bbpress ) ); 
  103.  
  104. /** Forum Moderation ******************************************************/ 
  105.  
  106. $post_status = bbp_get_public_status_id(); 
  107. if ( !bbp_check_for_moderation( $anonymous_data, $forum_author, $forum_title, $forum_content ) ) 
  108. $post_status = bbp_get_pending_status_id(); 
  109.  
  110. /** Additional Actions (Before Save) **************************************/ 
  111.  
  112. do_action( 'bbp_new_forum_pre_extras', $forum_parent_id ); 
  113.  
  114. // Bail if errors 
  115. if ( bbp_has_errors() ) 
  116. return; 
  117.  
  118. /** No Errors *************************************************************/ 
  119.  
  120. // Add the content of the form to $forum_data as an array 
  121. // Just in time manipulation of forum data before being created 
  122. $forum_data = apply_filters( 'bbp_new_forum_pre_insert', array( 
  123. 'post_author' => $forum_author,  
  124. 'post_title' => $forum_title,  
  125. 'post_content' => $forum_content,  
  126. 'post_parent' => $forum_parent_id,  
  127. 'post_status' => $post_status,  
  128. 'post_type' => bbp_get_forum_post_type(),  
  129. 'comment_status' => 'closed' 
  130. ) ); 
  131.  
  132. // Insert forum 
  133. $forum_id = wp_insert_post( $forum_data ); 
  134.  
  135. /** No Errors *************************************************************/ 
  136.  
  137. if ( !empty( $forum_id ) && !is_wp_error( $forum_id ) ) { 
  138.  
  139. /** Trash Check *******************************************************/ 
  140.  
  141. // If the forum is trash, or the forum_status is switched to 
  142. // trash, trash it properly 
  143. if ( ( get_post_field( 'post_status', $forum_id ) === bbp_get_trash_status_id() ) || ( $forum_data['post_status'] === bbp_get_trash_status_id() ) ) { 
  144.  
  145. // Trash the reply 
  146. wp_trash_post( $forum_id ); 
  147.  
  148. // Force view=all 
  149. $view_all = true; 
  150.  
  151. /** Spam Check ********************************************************/ 
  152.  
  153. // If reply or forum are spam, officially spam this reply 
  154. if ( $forum_data['post_status'] === bbp_get_spam_status_id() ) { 
  155. add_post_meta( $forum_id, '_bbp_spam_meta_status', bbp_get_public_status_id() ); 
  156.  
  157. // Force view=all 
  158. $view_all = true; 
  159.  
  160. /** Update counts, etc... *********************************************/ 
  161.  
  162. do_action( 'bbp_new_forum', array( 
  163. 'forum_id' => $forum_id,  
  164. 'post_parent' => $forum_parent_id,  
  165. 'forum_author' => $forum_author,  
  166. 'last_topic_id' => 0,  
  167. 'last_reply_id' => 0,  
  168. 'last_active_id' => 0,  
  169. 'last_active_time' => 0,  
  170. 'last_active_status' => bbp_get_public_status_id() 
  171. ) ); 
  172.  
  173. /** Additional Actions (After Save) ***********************************/ 
  174.  
  175. do_action( 'bbp_new_forum_post_extras', $forum_id ); 
  176.  
  177. /** Redirect **********************************************************/ 
  178.  
  179. // Redirect to 
  180. $redirect_to = bbp_get_redirect_to(); 
  181.  
  182. // Get the forum URL 
  183. $redirect_url = bbp_get_forum_permalink( $forum_id, $redirect_to ); 
  184.  
  185. // Add view all? 
  186. if ( bbp_get_view_all() || !empty( $view_all ) ) { 
  187.  
  188. // User can moderate, so redirect to forum with view all set 
  189. if ( current_user_can( 'moderate' ) ) { 
  190. $redirect_url = bbp_add_view_all( $redirect_url ); 
  191.  
  192. // User cannot moderate, so redirect to forum 
  193. } else { 
  194. $redirect_url = bbp_get_forum_permalink( $forum_id ); 
  195.  
  196. // Allow to be filtered 
  197. $redirect_url = apply_filters( 'bbp_new_forum_redirect_to', $redirect_url, $redirect_to ); 
  198.  
  199. /** Successful Save ***************************************************/ 
  200.  
  201. // Redirect back to new forum 
  202. wp_safe_redirect( $redirect_url ); 
  203.  
  204. // For good measure 
  205. exit(); 
  206.  
  207. // Errors 
  208. } else { 
  209. $append_error = ( is_wp_error( $forum_id ) && $forum_id->get_error_message() ) ? $forum_id->get_error_message() . ' ' : ''; 
  210. bbp_add_error( 'bbp_forum_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your forum:' . $append_error, bbpress ) );