bbp_edit_topic_handler

Handles the front end edit topic submission.

Description

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

Parameters (1)

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

Usage

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

Defined (1)

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

/includes/topics/functions.php  
  1. function bbp_edit_topic_handler( $action = '' ) { 
  2.  
  3. // Bail if action is not bbp-edit-topic 
  4. if ( 'bbp-edit-topic' !== $action ) 
  5. return; 
  6.  
  7. // Define local variable(s) 
  8. $revisions_removed = false; 
  9. $topic = $topic_id = $topic_author = $forum_id = $anonymous_data = 0; 
  10. $topic_title = $topic_content = $topic_edit_reason = ''; 
  11.  
  12. /** Topic *****************************************************************/ 
  13.  
  14. // Topic id was not passed 
  15. if ( empty( $_POST['bbp_topic_id'] ) ) { 
  16. bbp_add_error( 'bbp_edit_topic_id', __( '<strong>ERROR</strong>: Topic ID not found.', bbpress ) ); 
  17. return; 
  18.  
  19. // Topic id was passed 
  20. } elseif ( is_numeric( $_POST['bbp_topic_id'] ) ) { 
  21. $topic_id = (int) $_POST['bbp_topic_id']; 
  22. $topic = bbp_get_topic( $topic_id ); 
  23.  
  24. // Topic does not exist 
  25. if ( empty( $topic ) ) { 
  26. bbp_add_error( 'bbp_edit_topic_not_found', __( '<strong>ERROR</strong>: The topic you want to edit was not found.', bbpress ) ); 
  27. return; 
  28.  
  29. // Topic exists 
  30. } else { 
  31.  
  32. // Check users ability to create new topic 
  33. if ( ! bbp_is_topic_anonymous( $topic_id ) ) { 
  34.  
  35. // User cannot edit this topic 
  36. if ( !current_user_can( 'edit_topic', $topic_id ) ) { 
  37. bbp_add_error( 'bbp_edit_topic_permissions', __( '<strong>ERROR</strong>: You do not have permission to edit that topic.', bbpress ) ); 
  38.  
  39. // Set topic author 
  40. $topic_author = bbp_get_topic_author_id( $topic_id ); 
  41.  
  42. // It is an anonymous post 
  43. } else { 
  44.  
  45. // Filter anonymous data 
  46. $anonymous_data = bbp_filter_anonymous_post_data( array(), true ); 
  47.  
  48. // Nonce check 
  49. if ( ! bbp_verify_nonce_request( 'bbp-edit-topic_' . $topic_id ) ) { 
  50. bbp_add_error( 'bbp_edit_topic_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', bbpress ) ); 
  51. return; 
  52.  
  53. // Remove kses filters from title and content for capable users and if the nonce is verified 
  54. if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_topic'] ) && ( wp_create_nonce( 'bbp-unfiltered-html-topic_' . $topic_id ) === $_POST['_bbp_unfiltered_html_topic'] ) ) { 
  55. remove_filter( 'bbp_edit_topic_pre_title', 'wp_filter_kses' ); 
  56. remove_filter( 'bbp_edit_topic_pre_content', 'bbp_encode_bad', 10 ); 
  57. remove_filter( 'bbp_edit_topic_pre_content', 'bbp_filter_kses', 30 ); 
  58.  
  59. /** Topic Forum ***********************************************************/ 
  60.  
  61. // Forum id was not passed 
  62. if ( empty( $_POST['bbp_forum_id'] ) ) { 
  63. bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', bbpress ) ); 
  64.  
  65. // Forum id was passed 
  66. } elseif ( is_numeric( $_POST['bbp_forum_id'] ) ) { 
  67. $forum_id = (int) $_POST['bbp_forum_id']; 
  68.  
  69. // Current forum this topic is in 
  70. $current_forum_id = bbp_get_topic_forum_id( $topic_id ); 
  71.  
  72. // Forum exists 
  73. if ( !empty( $forum_id ) && ( $forum_id !== $current_forum_id ) ) { 
  74.  
  75. // Forum is a category 
  76. if ( bbp_is_forum_category( $forum_id ) ) { 
  77. bbp_add_error( 'bbp_edit_topic_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No topics can be created in it.', bbpress ) ); 
  78.  
  79. // Forum is not a category 
  80. } else { 
  81.  
  82. // Forum is closed and user cannot access 
  83. if ( bbp_is_forum_closed( $forum_id ) && !current_user_can( 'edit_forum', $forum_id ) ) { 
  84. bbp_add_error( 'bbp_edit_topic_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new topics.', bbpress ) ); 
  85.  
  86. // Forum is private and user cannot access 
  87. if ( bbp_is_forum_private( $forum_id ) ) { 
  88. if ( !current_user_can( 'read_private_forums' ) ) { 
  89. bbp_add_error( 'bbp_edit_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 ) ); 
  90.  
  91. // Forum is hidden and user cannot access 
  92. } elseif ( bbp_is_forum_hidden( $forum_id ) ) { 
  93. if ( !current_user_can( 'read_hidden_forums' ) ) { 
  94. bbp_add_error( 'bbp_edit_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 ) ); 
  95.  
  96. /** Topic Title ***********************************************************/ 
  97.  
  98. if ( !empty( $_POST['bbp_topic_title'] ) ) 
  99. $topic_title = esc_attr( strip_tags( $_POST['bbp_topic_title'] ) ); 
  100.  
  101. // Filter and sanitize 
  102. $topic_title = apply_filters( 'bbp_edit_topic_pre_title', $topic_title, $topic_id ); 
  103.  
  104. // No topic title 
  105. if ( empty( $topic_title ) ) 
  106. bbp_add_error( 'bbp_edit_topic_title', __( '<strong>ERROR</strong>: Your topic needs a title.', bbpress ) ); 
  107.  
  108. /** Topic Content *********************************************************/ 
  109.  
  110. if ( !empty( $_POST['bbp_topic_content'] ) ) 
  111. $topic_content = $_POST['bbp_topic_content']; 
  112.  
  113. // Filter and sanitize 
  114. $topic_content = apply_filters( 'bbp_edit_topic_pre_content', $topic_content, $topic_id ); 
  115.  
  116. // No topic content 
  117. if ( empty( $topic_content ) ) 
  118. bbp_add_error( 'bbp_edit_topic_content', __( '<strong>ERROR</strong>: Your topic cannot be empty.', bbpress ) ); 
  119.  
  120. /** Topic Blacklist *******************************************************/ 
  121.  
  122. if ( !bbp_check_for_blacklist( $anonymous_data, $topic_author, $topic_title, $topic_content ) ) 
  123. bbp_add_error( 'bbp_topic_blacklist', __( '<strong>ERROR</strong>: Your topic cannot be edited at this time.', bbpress ) ); 
  124.  
  125. /** Topic Status **********************************************************/ 
  126.  
  127. // Maybe put into moderation 
  128. if ( !bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content ) ) { 
  129.  
  130. // Set post status to pending if public or closed 
  131. if ( in_array( $topic->post_status, array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ) ) { 
  132. $topic_status = bbp_get_pending_status_id(); 
  133.  
  134. // Check a whitelist of possible topic status ID's 
  135. } elseif ( !empty( $_POST['bbp_topic_status'] ) && in_array( $_POST['bbp_topic_status'], array_keys( bbp_get_topic_statuses() ) ) ) { 
  136. $topic_status = $_POST['bbp_topic_status']; 
  137.  
  138. // Use existing post_status 
  139. } else { 
  140. $topic_status = $topic->post_status; 
  141.  
  142. /** Topic Tags ************************************************************/ 
  143.  
  144. // Either replace terms 
  145. if ( bbp_allow_topic_tags() && current_user_can( 'assign_topic_tags' ) && ! empty( $_POST['bbp_topic_tags'] ) ) { 
  146.  
  147. // Escape tag input 
  148. $terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) ); 
  149.  
  150. // Explode by comma 
  151. if ( strstr( $terms, ', ' ) ) 
  152. $terms = explode( ', ', $terms ); 
  153.  
  154. // Add topic tag ID as main key 
  155. $terms = array( bbp_get_topic_tag_tax_id() => $terms ); 
  156.  
  157. // ...or remove them. 
  158. } elseif ( isset( $_POST['bbp_topic_tags'] ) ) { 
  159. $terms = array( bbp_get_topic_tag_tax_id() => array() ); 
  160.  
  161. // Existing terms 
  162. } else { 
  163. $terms = array( bbp_get_topic_tag_tax_id() => explode( ', ', bbp_get_topic_tag_names( $topic_id, ', ' ) ) ); 
  164.  
  165. /** Additional Actions (Before Save) **************************************/ 
  166.  
  167. do_action( 'bbp_edit_topic_pre_extras', $topic_id ); 
  168.  
  169. // Bail if errors 
  170. if ( bbp_has_errors() ) 
  171. return; 
  172.  
  173. /** No Errors *************************************************************/ 
  174.  
  175. // Add the content of the form to $topic_data as an array 
  176. // Just in time manipulation of topic data before being edited 
  177. $topic_data = apply_filters( 'bbp_edit_topic_pre_insert', array( 
  178. 'ID' => $topic_id,  
  179. 'post_title' => $topic_title,  
  180. 'post_content' => $topic_content,  
  181. 'post_status' => $topic_status,  
  182. 'post_parent' => $forum_id,  
  183. 'post_author' => $topic_author,  
  184. 'post_type' => bbp_get_topic_post_type(),  
  185. 'tax_input' => $terms,  
  186. ) ); 
  187.  
  188. // Toggle revisions to avoid duplicates 
  189. if ( post_type_supports( bbp_get_topic_post_type(), 'revisions' ) ) { 
  190. $revisions_removed = true; 
  191. remove_post_type_support( bbp_get_topic_post_type(), 'revisions' ); 
  192.  
  193. // Insert topic 
  194. $topic_id = wp_update_post( $topic_data ); 
  195.  
  196. // Toggle revisions back on 
  197. if ( true === $revisions_removed ) { 
  198. $revisions_removed = false; 
  199. add_post_type_support( bbp_get_topic_post_type(), 'revisions' ); 
  200.  
  201. /** No Errors *************************************************************/ 
  202.  
  203. if ( !empty( $topic_id ) && !is_wp_error( $topic_id ) ) { 
  204.  
  205. // Update counts, etc... 
  206. do_action( 'bbp_edit_topic', $topic_id, $forum_id, $anonymous_data, $topic_author , true /** Is edit */ ); 
  207.  
  208. /** Revisions *********************************************************/ 
  209.  
  210. // Revision Reason 
  211. if ( !empty( $_POST['bbp_topic_edit_reason'] ) ) { 
  212. $topic_edit_reason = esc_attr( strip_tags( $_POST['bbp_topic_edit_reason'] ) ); 
  213.  
  214. // Update revision log 
  215. if ( !empty( $_POST['bbp_log_topic_edit'] ) && ( "1" === $_POST['bbp_log_topic_edit'] ) ) { 
  216. $revision_id = wp_save_post_revision( $topic_id ); 
  217. if ( ! empty( $revision_id ) ) { 
  218. 'topic_id' => $topic_id,  
  219. 'revision_id' => $revision_id,  
  220. 'author_id' => bbp_get_current_user_id(),  
  221. 'reason' => $topic_edit_reason 
  222. ) ); 
  223.  
  224. /** Move Topic ********************************************************/ 
  225.  
  226. // If the new forum id is not equal to the old forum id, run the 
  227. // bbp_move_topic action and pass the topic's forum id as the 
  228. // first arg and topic id as the second to update counts. 
  229. if ( $forum_id !== $topic->post_parent ) { 
  230. bbp_move_topic_handler( $topic_id, $topic->post_parent, $forum_id ); 
  231.  
  232. /** Stickies **********************************************************/ 
  233.  
  234. if ( !empty( $_POST['bbp_stick_topic'] ) && in_array( $_POST['bbp_stick_topic'], array_keys( bbp_get_topic_types() ) ) ) { 
  235.  
  236. // What's the caps? 
  237. if ( current_user_can( 'moderate' ) ) { 
  238.  
  239. // What's the haps? 
  240. switch ( $_POST['bbp_stick_topic'] ) { 
  241.  
  242. // Sticky in forum 
  243. case 'stick' : 
  244. bbp_stick_topic( $topic_id ); 
  245. break; 
  246.  
  247. // Sticky in all forums 
  248. case 'super' : 
  249. bbp_stick_topic( $topic_id, true ); 
  250. break; 
  251.  
  252. // Normal 
  253. case 'unstick' : 
  254. default : 
  255. bbp_unstick_topic( $topic_id ); 
  256. break; 
  257.  
  258. /** Additional Actions (After Save) ***********************************/ 
  259.  
  260. do_action( 'bbp_edit_topic_post_extras', $topic_id ); 
  261.  
  262. /** Redirect **********************************************************/ 
  263.  
  264. // Redirect to 
  265. $redirect_to = bbp_get_redirect_to(); 
  266.  
  267. // View all? 
  268. $view_all = bbp_get_view_all(); 
  269.  
  270. // Get the topic URL 
  271. $topic_url = bbp_get_topic_permalink( $topic_id, $redirect_to ); 
  272.  
  273. // Add view all? 
  274. if ( !empty( $view_all ) ) 
  275. $topic_url = bbp_add_view_all( $topic_url ); 
  276.  
  277. // Allow to be filtered 
  278. $topic_url = apply_filters( 'bbp_edit_topic_redirect_to', $topic_url, $view_all, $redirect_to ); 
  279.  
  280. /** Successful Edit ***************************************************/ 
  281.  
  282. // Redirect back to new topic 
  283. wp_safe_redirect( $topic_url ); 
  284.  
  285. // For good measure 
  286. exit(); 
  287.  
  288. /** Errors ****************************************************************/ 
  289.  
  290. } else { 
  291. $append_error = ( is_wp_error( $topic_id ) && $topic_id->get_error_message() ) ? $topic_id->get_error_message() . ' ' : ''; 
  292. bbp_add_error( 'bbp_topic_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your topic:' . $append_error . 'Please try again.', bbpress ) );