bbp_edit_reply_handler

Handles the front end edit reply submission.

Description

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

Parameters (1)

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

Usage

  1. if ( !function_exists( 'bbp_edit_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_edit_reply_handler($action); 
  9.  

Defined (1)

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

/includes/replies/functions.php  
  1. function bbp_edit_reply_handler( $action = '' ) { 
  2.  
  3. // Bail if action is not bbp-edit-reply 
  4. if ( 'bbp-edit-reply' !== $action ) 
  5. return; 
  6.  
  7. // Define local variable(s) 
  8. $revisions_removed = false; 
  9. $reply = $reply_id = $reply_author = $topic_id = $forum_id = $anonymous_data = 0; 
  10. $reply_title = $reply_content = $reply_edit_reason = $terms = ''; 
  11.  
  12. /** Reply *****************************************************************/ 
  13.  
  14. // Reply id was not passed 
  15. if ( empty( $_POST['bbp_reply_id'] ) ) { 
  16. bbp_add_error( 'bbp_edit_reply_id', __( '<strong>ERROR</strong>: Reply ID not found.', bbpress ) ); 
  17. return; 
  18.  
  19. // Reply id was passed 
  20. } elseif ( is_numeric( $_POST['bbp_reply_id'] ) ) { 
  21. $reply_id = (int) $_POST['bbp_reply_id']; 
  22. $reply = bbp_get_reply( $reply_id ); 
  23.  
  24. // Nonce check 
  25. if ( ! bbp_verify_nonce_request( 'bbp-edit-reply_' . $reply_id ) ) { 
  26. bbp_add_error( 'bbp_edit_reply_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', bbpress ) ); 
  27. return; 
  28.  
  29. // Reply does not exist 
  30. if ( empty( $reply ) ) { 
  31. bbp_add_error( 'bbp_edit_reply_not_found', __( '<strong>ERROR</strong>: The reply you want to edit was not found.', bbpress ) ); 
  32. return; 
  33.  
  34. // Reply exists 
  35. } else { 
  36.  
  37. // Check users ability to create new reply 
  38. if ( ! bbp_is_reply_anonymous( $reply_id ) ) { 
  39.  
  40. // User cannot edit this reply 
  41. if ( !current_user_can( 'edit_reply', $reply_id ) ) { 
  42. bbp_add_error( 'bbp_edit_reply_permissions', __( '<strong>ERROR</strong>: You do not have permission to edit that reply.', bbpress ) ); 
  43. return; 
  44.  
  45. // Set reply author 
  46. $reply_author = bbp_get_reply_author_id( $reply_id ); 
  47.  
  48. // It is an anonymous post 
  49. } else { 
  50.  
  51. // Filter anonymous data 
  52. $anonymous_data = bbp_filter_anonymous_post_data(); 
  53.  
  54. // Remove kses filters from title and content for capable users and if the nonce is verified 
  55. if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_reply'] ) && wp_create_nonce( 'bbp-unfiltered-html-reply_' . $reply_id ) === $_POST['_bbp_unfiltered_html_reply'] ) { 
  56. remove_filter( 'bbp_edit_reply_pre_title', 'wp_filter_kses' ); 
  57. remove_filter( 'bbp_edit_reply_pre_content', 'bbp_encode_bad', 10 ); 
  58. remove_filter( 'bbp_edit_reply_pre_content', 'bbp_filter_kses', 30 ); 
  59.  
  60. /** Reply Topic ***********************************************************/ 
  61.  
  62. $topic_id = bbp_get_reply_topic_id( $reply_id ); 
  63.  
  64. /** Topic Forum ***********************************************************/ 
  65.  
  66. $forum_id = bbp_get_topic_forum_id( $topic_id ); 
  67.  
  68. // Forum exists 
  69. if ( !empty( $forum_id ) && ( $forum_id !== bbp_get_reply_forum_id( $reply_id ) ) ) { 
  70.  
  71. // Forum is a category 
  72. if ( bbp_is_forum_category( $forum_id ) ) { 
  73. bbp_add_error( 'bbp_edit_reply_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No replies can be created in this forum.', bbpress ) ); 
  74.  
  75. // Forum is not a category 
  76. } else { 
  77.  
  78. // Forum is closed and user cannot access 
  79. if ( bbp_is_forum_closed( $forum_id ) && !current_user_can( 'edit_forum', $forum_id ) ) { 
  80. bbp_add_error( 'bbp_edit_reply_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new replies.', bbpress ) ); 
  81.  
  82. // Forum is private and user cannot access 
  83. if ( bbp_is_forum_private( $forum_id ) ) { 
  84. if ( !current_user_can( 'read_private_forums' ) ) { 
  85. bbp_add_error( 'bbp_edit_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 ) ); 
  86.  
  87. // Forum is hidden and user cannot access 
  88. } elseif ( bbp_is_forum_hidden( $forum_id ) ) { 
  89. if ( !current_user_can( 'read_hidden_forums' ) ) { 
  90. bbp_add_error( 'bbp_edit_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 ) ); 
  91.  
  92. /** Reply Title ***********************************************************/ 
  93.  
  94. if ( !empty( $_POST['bbp_reply_title'] ) ) 
  95. $reply_title = esc_attr( strip_tags( $_POST['bbp_reply_title'] ) ); 
  96.  
  97. // Filter and sanitize 
  98. $reply_title = apply_filters( 'bbp_edit_reply_pre_title', $reply_title, $reply_id ); 
  99.  
  100. /** Reply Content *********************************************************/ 
  101.  
  102. if ( !empty( $_POST['bbp_reply_content'] ) ) 
  103. $reply_content = $_POST['bbp_reply_content']; 
  104.  
  105. // Filter and sanitize 
  106. $reply_content = apply_filters( 'bbp_edit_reply_pre_content', $reply_content, $reply_id ); 
  107.  
  108. // No reply content 
  109. if ( empty( $reply_content ) ) 
  110. bbp_add_error( 'bbp_edit_reply_content', __( '<strong>ERROR</strong>: Your reply cannot be empty.', bbpress ) ); 
  111.  
  112. /** Reply Blacklist *******************************************************/ 
  113.  
  114. if ( !bbp_check_for_blacklist( $anonymous_data, $reply_author, $reply_title, $reply_content ) ) 
  115. bbp_add_error( 'bbp_reply_blacklist', __( '<strong>ERROR</strong>: Your reply cannot be edited at this time.', bbpress ) ); 
  116.  
  117. /** Reply Status **********************************************************/ 
  118.  
  119. // Maybe put into moderation 
  120. if ( !bbp_check_for_moderation( $anonymous_data, $reply_author, $reply_title, $reply_content ) ) { 
  121.  
  122. // Set post status to pending if public 
  123. if ( bbp_get_public_status_id() === $reply->post_status ) { 
  124. $reply_status = bbp_get_pending_status_id(); 
  125.  
  126. // Use existing post_status 
  127. } else { 
  128. $reply_status = $reply->post_status; 
  129.  
  130. /** Reply To **************************************************************/ 
  131.  
  132. // Handle Reply To of the reply; $_REQUEST for non-JS submissions 
  133. if ( isset( $_REQUEST['bbp_reply_to'] ) ) { 
  134. $reply_to = bbp_validate_reply_to( $_REQUEST['bbp_reply_to'] ); 
  135.  
  136. /** Topic Tags ************************************************************/ 
  137.  
  138. // Either replace terms 
  139. if ( bbp_allow_topic_tags() && current_user_can( 'assign_topic_tags' ) && ! empty( $_POST['bbp_topic_tags'] ) ) { 
  140. $terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) ); 
  141.  
  142. // ...or remove them. 
  143. } elseif ( isset( $_POST['bbp_topic_tags'] ) ) { 
  144. $terms = ''; 
  145.  
  146. // Existing terms 
  147. } else { 
  148. $terms = bbp_get_topic_tag_names( $topic_id ); 
  149.  
  150. /** Additional Actions (Before Save) **************************************/ 
  151.  
  152. do_action( 'bbp_edit_reply_pre_extras', $reply_id ); 
  153.  
  154. // Bail if errors 
  155. if ( bbp_has_errors() ) 
  156. return; 
  157.  
  158. /** No Errors *************************************************************/ 
  159.  
  160. // Add the content of the form to $reply_data as an array 
  161. // Just in time manipulation of reply data before being edited 
  162. $reply_data = apply_filters( 'bbp_edit_reply_pre_insert', array( 
  163. 'ID' => $reply_id,  
  164. 'post_title' => $reply_title,  
  165. 'post_content' => $reply_content,  
  166. 'post_status' => $reply_status,  
  167. 'post_parent' => $topic_id,  
  168. 'post_author' => $reply_author,  
  169. 'post_type' => bbp_get_reply_post_type() 
  170. ) ); 
  171.  
  172. // Toggle revisions to avoid duplicates 
  173. if ( post_type_supports( bbp_get_reply_post_type(), 'revisions' ) ) { 
  174. $revisions_removed = true; 
  175. remove_post_type_support( bbp_get_reply_post_type(), 'revisions' ); 
  176.  
  177. // Insert topic 
  178. $reply_id = wp_update_post( $reply_data ); 
  179.  
  180. // Toggle revisions back on 
  181. if ( true === $revisions_removed ) { 
  182. $revisions_removed = false; 
  183. add_post_type_support( bbp_get_reply_post_type(), 'revisions' ); 
  184.  
  185. /** Topic Tags ************************************************************/ 
  186.  
  187. // Just in time manipulation of reply terms before being edited 
  188. $terms = apply_filters( 'bbp_edit_reply_pre_set_terms', $terms, $topic_id, $reply_id ); 
  189.  
  190. // Insert terms 
  191. $terms = wp_set_post_terms( $topic_id, $terms, bbp_get_topic_tag_tax_id(), false ); 
  192.  
  193. // Term error 
  194. if ( is_wp_error( $terms ) ) { 
  195. bbp_add_error( 'bbp_reply_tags', __( '<strong>ERROR</strong>: There was a problem adding the tags to the topic.', bbpress ) ); 
  196.  
  197. /** Revisions *************************************************************/ 
  198.  
  199. // Revision Reason 
  200. if ( !empty( $_POST['bbp_reply_edit_reason'] ) ) 
  201. $reply_edit_reason = esc_attr( strip_tags( $_POST['bbp_reply_edit_reason'] ) ); 
  202.  
  203. // Update revision log 
  204. if ( !empty( $_POST['bbp_log_reply_edit'] ) && ( "1" === $_POST['bbp_log_reply_edit'] ) ) { 
  205. $revision_id = wp_save_post_revision( $reply_id ); 
  206. if ( !empty( $revision_id ) ) { 
  207. 'reply_id' => $reply_id,  
  208. 'revision_id' => $revision_id,  
  209. 'author_id' => bbp_get_current_user_id(),  
  210. 'reason' => $reply_edit_reason 
  211. ) ); 
  212.  
  213. /** No Errors *************************************************************/ 
  214.  
  215. if ( !empty( $reply_id ) && !is_wp_error( $reply_id ) ) { 
  216.  
  217. // Update counts, etc... 
  218. do_action( 'bbp_edit_reply', $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author , true, $reply_to ); 
  219.  
  220. /** Additional Actions (After Save) ***********************************/ 
  221.  
  222. do_action( 'bbp_edit_reply_post_extras', $reply_id ); 
  223.  
  224. /** Redirect **********************************************************/ 
  225.  
  226. // Redirect to 
  227. $redirect_to = bbp_get_redirect_to(); 
  228.  
  229. // Get the reply URL 
  230. $reply_url = bbp_get_reply_url( $reply_id, $redirect_to ); 
  231.  
  232. // Allow to be filtered 
  233. $reply_url = apply_filters( 'bbp_edit_reply_redirect_to', $reply_url, $redirect_to ); 
  234.  
  235. /** Successful Edit ***************************************************/ 
  236.  
  237. // Redirect back to new reply 
  238. wp_safe_redirect( $reply_url ); 
  239.  
  240. // For good measure 
  241. exit(); 
  242.  
  243. /** Errors ****************************************************************/ 
  244.  
  245. } else { 
  246. $append_error = ( is_wp_error( $reply_id ) && $reply_id->get_error_message() ) ? $reply_id->get_error_message() . ' ' : ''; 
  247. 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 ) );