bbp_new_reply_handler

Handles the front end reply submission.

Description

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

Parameters (1)

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

Usage

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

Defined (1)

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

/includes/replies/functions.php  
  1. function bbp_new_reply_handler( $action = '' ) { 
  2.  
  3. // Bail if action is not bbp-new-reply 
  4. if ( 'bbp-new-reply' !== $action ) 
  5. return; 
  6.  
  7. // Nonce check 
  8. if ( ! bbp_verify_nonce_request( 'bbp-new-reply' ) ) { 
  9. bbp_add_error( 'bbp_new_reply_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', bbpress ) ); 
  10. return; 
  11.  
  12. // Define local variable(s) 
  13. $topic_id = $forum_id = $reply_author = $anonymous_data = $reply_to = 0; 
  14. $reply_title = $reply_content = $terms = ''; 
  15.  
  16. /** Reply Author **********************************************************/ 
  17.  
  18. // User is anonymous 
  19. if ( bbp_is_anonymous() ) { 
  20.  
  21. // Filter anonymous data 
  22. $anonymous_data = bbp_filter_anonymous_post_data(); 
  23.  
  24. // Anonymous data checks out, so set cookies, etc... 
  25. if ( !empty( $anonymous_data ) && is_array( $anonymous_data ) ) { 
  26.  
  27. // User is logged in 
  28. } else { 
  29.  
  30. // User cannot create replies 
  31. if ( !current_user_can( 'publish_replies' ) ) { 
  32. bbp_add_error( 'bbp_reply_permissions', __( '<strong>ERROR</strong>: You do not have permission to reply.', bbpress ) ); 
  33.  
  34. // Reply author is current user 
  35. $reply_author = bbp_get_current_user_id(); 
  36.  
  37.  
  38. /** Topic ID **************************************************************/ 
  39.  
  40. // Topic id was not passed 
  41. if ( empty( $_POST['bbp_topic_id'] ) ) { 
  42. bbp_add_error( 'bbp_reply_topic_id', __( '<strong>ERROR</strong>: Topic ID is missing.', bbpress ) ); 
  43.  
  44. // Topic id is not a number 
  45. } elseif ( ! is_numeric( $_POST['bbp_topic_id'] ) ) { 
  46. bbp_add_error( 'bbp_reply_topic_id', __( '<strong>ERROR</strong>: Topic ID must be a number.', bbpress ) ); 
  47.  
  48. // Topic id might be valid 
  49. } else { 
  50.  
  51. // Get the topic id 
  52. $posted_topic_id = intval( $_POST['bbp_topic_id'] ); 
  53.  
  54. // Topic id is a negative number 
  55. if ( 0 > $posted_topic_id ) { 
  56. bbp_add_error( 'bbp_reply_topic_id', __( '<strong>ERROR</strong>: Topic ID cannot be a negative number.', bbpress ) ); 
  57.  
  58. // Topic does not exist 
  59. } elseif ( ! bbp_get_topic( $posted_topic_id ) ) { 
  60. bbp_add_error( 'bbp_reply_topic_id', __( '<strong>ERROR</strong>: Topic does not exist.', bbpress ) ); 
  61.  
  62. // Use the POST'ed topic id 
  63. } else { 
  64. $topic_id = $posted_topic_id; 
  65.  
  66. /** Forum ID **************************************************************/ 
  67.  
  68. // Try to use the forum id of the topic 
  69. if ( !isset( $_POST['bbp_forum_id'] ) && !empty( $topic_id ) ) { 
  70. $forum_id = bbp_get_topic_forum_id( $topic_id ); 
  71.  
  72. // Error check the POST'ed forum id 
  73. } elseif ( isset( $_POST['bbp_forum_id'] ) ) { 
  74.  
  75. // Empty Forum id was passed 
  76. if ( empty( $_POST['bbp_forum_id'] ) ) { 
  77. bbp_add_error( 'bbp_reply_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_reply_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_reply_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No replies 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_reply_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new replies.', 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_reply_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new replies 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_reply_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new replies in it.', bbpress ) ); 
  128.  
  129. /** Unfiltered HTML *******************************************************/ 
  130.  
  131. // Remove kses filters from title and content for capable users and if the nonce is verified 
  132. if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_reply'] ) && wp_create_nonce( 'bbp-unfiltered-html-reply_' . $topic_id ) === $_POST['_bbp_unfiltered_html_reply'] ) { 
  133. remove_filter( 'bbp_new_reply_pre_title', 'wp_filter_kses' ); 
  134. remove_filter( 'bbp_new_reply_pre_content', 'bbp_encode_bad', 10 ); 
  135. remove_filter( 'bbp_new_reply_pre_content', 'bbp_filter_kses', 30 ); 
  136.  
  137. /** Reply Title ***********************************************************/ 
  138.  
  139. if ( !empty( $_POST['bbp_reply_title'] ) ) 
  140. $reply_title = esc_attr( strip_tags( $_POST['bbp_reply_title'] ) ); 
  141.  
  142. // Filter and sanitize 
  143. $reply_title = apply_filters( 'bbp_new_reply_pre_title', $reply_title ); 
  144.  
  145. /** Reply Content *********************************************************/ 
  146.  
  147. if ( !empty( $_POST['bbp_reply_content'] ) ) 
  148. $reply_content = $_POST['bbp_reply_content']; 
  149.  
  150. // Filter and sanitize 
  151. $reply_content = apply_filters( 'bbp_new_reply_pre_content', $reply_content ); 
  152.  
  153. // No reply content 
  154. if ( empty( $reply_content ) ) 
  155. bbp_add_error( 'bbp_reply_content', __( '<strong>ERROR</strong>: Your reply cannot be empty.', bbpress ) ); 
  156.  
  157. /** Reply Flooding ********************************************************/ 
  158.  
  159. if ( !bbp_check_for_flood( $anonymous_data, $reply_author ) ) 
  160. bbp_add_error( 'bbp_reply_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', bbpress ) ); 
  161.  
  162. /** Reply Duplicate *******************************************************/ 
  163.  
  164. if ( !bbp_check_for_duplicate( array( 'post_type' => bbp_get_reply_post_type(), 'post_author' => $reply_author, 'post_content' => $reply_content, 'post_parent' => $topic_id, 'anonymous_data' => $anonymous_data ) ) ) 
  165. bbp_add_error( 'bbp_reply_duplicate', __( '<strong>ERROR</strong>: Duplicate reply detected; it looks as though you’ve already said that!', bbpress ) ); 
  166.  
  167. /** Reply Blacklist *******************************************************/ 
  168.  
  169. if ( !bbp_check_for_blacklist( $anonymous_data, $reply_author, $reply_title, $reply_content ) ) 
  170. bbp_add_error( 'bbp_reply_blacklist', __( '<strong>ERROR</strong>: Your reply cannot be created at this time.', bbpress ) ); 
  171.  
  172. /** Reply Status **********************************************************/ 
  173.  
  174. // Maybe put into moderation 
  175. if ( !bbp_check_for_moderation( $anonymous_data, $reply_author, $reply_title, $reply_content ) ) { 
  176. $reply_status = bbp_get_pending_status_id(); 
  177.  
  178. // Default 
  179. } else { 
  180. $reply_status = bbp_get_public_status_id(); 
  181.  
  182. /** Reply To **************************************************************/ 
  183.  
  184. // Handle Reply To of the reply; $_REQUEST for non-JS submissions 
  185. if ( isset( $_REQUEST['bbp_reply_to'] ) ) { 
  186. $reply_to = bbp_validate_reply_to( $_REQUEST['bbp_reply_to'] ); 
  187.  
  188. /** Topic Closed **********************************************************/ 
  189.  
  190. // If topic is closed, moderators can still reply 
  191. if ( bbp_is_topic_closed( $topic_id ) && ! current_user_can( 'moderate' ) ) { 
  192. bbp_add_error( 'bbp_reply_topic_closed', __( '<strong>ERROR</strong>: Topic is closed.', bbpress ) ); 
  193.  
  194. /** Topic Tags ************************************************************/ 
  195.  
  196. // Either replace terms 
  197. if ( bbp_allow_topic_tags() && current_user_can( 'assign_topic_tags' ) && ! empty( $_POST['bbp_topic_tags'] ) ) { 
  198. $terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) ); 
  199.  
  200. // ...or remove them. 
  201. } elseif ( isset( $_POST['bbp_topic_tags'] ) ) { 
  202. $terms = ''; 
  203.  
  204. // Existing terms 
  205. } else { 
  206. $terms = bbp_get_topic_tag_names( $topic_id ); 
  207.  
  208. /** Additional Actions (Before Save) **************************************/ 
  209.  
  210. do_action( 'bbp_new_reply_pre_extras', $topic_id, $forum_id ); 
  211.  
  212. // Bail if errors 
  213. if ( bbp_has_errors() ) 
  214. return; 
  215.  
  216. /** No Errors *************************************************************/ 
  217.  
  218. // Add the content of the form to $reply_data as an array 
  219. // Just in time manipulation of reply data before being created 
  220. $reply_data = apply_filters( 'bbp_new_reply_pre_insert', array( 
  221. 'post_author' => $reply_author,  
  222. 'post_title' => $reply_title,  
  223. 'post_content' => $reply_content,  
  224. 'post_status' => $reply_status,  
  225. 'post_parent' => $topic_id,  
  226. 'post_type' => bbp_get_reply_post_type(),  
  227. 'comment_status' => 'closed',  
  228. 'menu_order' => bbp_get_topic_reply_count( $topic_id, false ) + 1 
  229. ) ); 
  230.  
  231. // Insert reply 
  232. $reply_id = wp_insert_post( $reply_data ); 
  233.  
  234. /** No Errors *************************************************************/ 
  235.  
  236. // Check for missing reply_id or error 
  237. if ( !empty( $reply_id ) && !is_wp_error( $reply_id ) ) { 
  238.  
  239. /** Topic Tags ********************************************************/ 
  240.  
  241. // Just in time manipulation of reply terms before being edited 
  242. $terms = apply_filters( 'bbp_new_reply_pre_set_terms', $terms, $topic_id, $reply_id ); 
  243.  
  244. // Insert terms 
  245. $terms = wp_set_post_terms( $topic_id, $terms, bbp_get_topic_tag_tax_id(), false ); 
  246.  
  247. // Term error 
  248. if ( is_wp_error( $terms ) ) { 
  249. bbp_add_error( 'bbp_reply_tags', __( '<strong>ERROR</strong>: There was a problem adding the tags to the topic.', bbpress ) ); 
  250.  
  251. /** Trash Check *******************************************************/ 
  252.  
  253. // If this reply starts as trash, add it to pre_trashed_replies 
  254. // for the topic, so it is properly restored. 
  255. if ( bbp_is_topic_trash( $topic_id ) || ( $reply_data['post_status'] === bbp_get_trash_status_id() ) ) { 
  256.  
  257. // Trash the reply 
  258. wp_trash_post( $reply_id ); 
  259.  
  260. // Only add to pre-trashed array if topic is trashed 
  261. if ( bbp_is_topic_trash( $topic_id ) ) { 
  262.  
  263. // Get pre_trashed_replies for topic 
  264. $pre_trashed_replies = get_post_meta( $topic_id, '_bbp_pre_trashed_replies', true ); 
  265.  
  266. // Add this reply to the end of the existing replies 
  267. $pre_trashed_replies[] = $reply_id; 
  268.  
  269. // Update the pre_trashed_reply post meta 
  270. update_post_meta( $topic_id, '_bbp_pre_trashed_replies', $pre_trashed_replies ); 
  271.  
  272. /** Spam Check ********************************************************/ 
  273.  
  274. // If reply or topic are spam, officially spam this reply 
  275. } elseif ( bbp_is_topic_spam( $topic_id ) || ( $reply_data['post_status'] === bbp_get_spam_status_id() ) ) { 
  276. add_post_meta( $reply_id, '_bbp_spam_meta_status', bbp_get_public_status_id() ); 
  277.  
  278. // Only add to pre-spammed array if topic is spam 
  279. if ( bbp_is_topic_spam( $topic_id ) ) { 
  280.  
  281. // Get pre_spammed_replies for topic 
  282. $pre_spammed_replies = get_post_meta( $topic_id, '_bbp_pre_spammed_replies', true ); 
  283.  
  284. // Add this reply to the end of the existing replies 
  285. $pre_spammed_replies[] = $reply_id; 
  286.  
  287. // Update the pre_spammed_replies post meta 
  288. update_post_meta( $topic_id, '_bbp_pre_spammed_replies', $pre_spammed_replies ); 
  289.  
  290. /** Update counts, etc... *********************************************/ 
  291.  
  292. do_action( 'bbp_new_reply', $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author, false, $reply_to ); 
  293.  
  294. /** Additional Actions (After Save) ***********************************/ 
  295.  
  296. do_action( 'bbp_new_reply_post_extras', $reply_id ); 
  297.  
  298. /** Redirect **********************************************************/ 
  299.  
  300. // Redirect to 
  301. $redirect_to = bbp_get_redirect_to(); 
  302.  
  303. // Get the reply URL 
  304. $reply_url = bbp_get_reply_url( $reply_id, $redirect_to ); 
  305.  
  306. // Allow to be filtered 
  307. $reply_url = apply_filters( 'bbp_new_reply_redirect_to', $reply_url, $redirect_to, $reply_id ); 
  308.  
  309. /** Successful Save ***************************************************/ 
  310.  
  311. // Redirect back to new reply 
  312. wp_safe_redirect( $reply_url ); 
  313.  
  314. // For good measure 
  315. exit(); 
  316.  
  317. /** Errors ****************************************************************/ 
  318.  
  319. } else { 
  320. $append_error = ( is_wp_error( $reply_id ) && $reply_id->get_error_message() ) ? $reply_id->get_error_message() . ' ' : ''; 
  321. bbp_add_error( 'bbp_reply_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your reply:' . $append_error . 'Please try again.', bbpress ) );