bbp_split_topic_handler

Split topic handler.

Description

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

Handles the front end split topic submission

Parameters (1)

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

Usage

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

Defined (1)

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

/includes/topics/functions.php  
  1. function bbp_split_topic_handler( $action = '' ) { 
  2.  
  3. // Bail if action is not 'bbp-split-topic' 
  4. if ( 'bbp-split-topic' !== $action ) 
  5. return; 
  6.  
  7. global $wpdb; 
  8.  
  9. // Prevent debug notices 
  10. $from_reply_id = $destination_topic_id = 0; 
  11. $destination_topic_title = ''; 
  12. $destination_topic = $from_reply = $source_topic = ''; 
  13. $split_option = false; 
  14.  
  15. /** Split Reply ***********************************************************/ 
  16.  
  17. if ( empty( $_POST['bbp_reply_id'] ) ) 
  18. bbp_add_error( 'bbp_split_topic_reply_id', __( '<strong>ERROR</strong>: Reply ID to split the topic from not found!', bbpress ) ); 
  19. else 
  20. $from_reply_id = (int) $_POST['bbp_reply_id']; 
  21.  
  22. $from_reply = bbp_get_reply( $from_reply_id ); 
  23.  
  24. // Reply exists 
  25. if ( empty( $from_reply ) ) 
  26. bbp_add_error( 'bbp_split_topic_r_not_found', __( '<strong>ERROR</strong>: The reply you want to split from was not found.', bbpress ) ); 
  27.  
  28. /** Topic to Split ********************************************************/ 
  29.  
  30. // Get the topic being split 
  31. $source_topic = bbp_get_topic( $from_reply->post_parent ); 
  32.  
  33. // No topic 
  34. if ( empty( $source_topic ) ) 
  35. bbp_add_error( 'bbp_split_topic_source_not_found', __( '<strong>ERROR</strong>: The topic you want to split was not found.', bbpress ) ); 
  36.  
  37. // Nonce check failed 
  38. if ( ! bbp_verify_nonce_request( 'bbp-split-topic_' . $source_topic->ID ) ) { 
  39. bbp_add_error( 'bbp_split_topic_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', bbpress ) ); 
  40. return; 
  41.  
  42. // Use cannot edit topic 
  43. if ( !current_user_can( 'edit_topic', $source_topic->ID ) ) 
  44. bbp_add_error( 'bbp_split_topic_source_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the source topic.', bbpress ) ); 
  45.  
  46. // How to Split 
  47. if ( !empty( $_POST['bbp_topic_split_option'] ) ) 
  48. $split_option = (string) trim( $_POST['bbp_topic_split_option'] ); 
  49.  
  50. // Invalid split option 
  51. if ( empty( $split_option ) || !in_array( $split_option, array( 'existing', 'reply' ) ) ) { 
  52. bbp_add_error( 'bbp_split_topic_option', __( '<strong>ERROR</strong>: You need to choose a valid split option.', bbpress ) ); 
  53.  
  54. // Valid Split Option 
  55. } else { 
  56.  
  57. // What kind of split 
  58. switch ( $split_option ) { 
  59.  
  60. // Into an existing topic 
  61. case 'existing' : 
  62.  
  63. // Get destination topic id 
  64. if ( empty( $_POST['bbp_destination_topic'] ) ) 
  65. bbp_add_error( 'bbp_split_topic_destination_id', __( '<strong>ERROR</strong>: Destination topic ID not found!', bbpress ) ); 
  66. else 
  67. $destination_topic_id = (int) $_POST['bbp_destination_topic']; 
  68.  
  69. // Get the destination topic 
  70. $destination_topic = bbp_get_topic( $destination_topic_id ); 
  71.  
  72. // No destination topic 
  73. if ( empty( $destination_topic ) ) 
  74. bbp_add_error( 'bbp_split_topic_destination_not_found', __( '<strong>ERROR</strong>: The topic you want to split to was not found!', bbpress ) ); 
  75.  
  76. // User cannot edit the destination topic 
  77. if ( !current_user_can( 'edit_topic', $destination_topic->ID ) ) 
  78. bbp_add_error( 'bbp_split_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the destination topic!', bbpress ) ); 
  79.  
  80. break; 
  81.  
  82. // Split at reply into a new topic 
  83. case 'reply' : 
  84. default : 
  85.  
  86. // User needs to be able to publish topics 
  87. if ( current_user_can( 'publish_topics' ) ) { 
  88.  
  89. // Use the new title that was passed 
  90. if ( !empty( $_POST['bbp_topic_split_destination_title'] ) ) { 
  91. $destination_topic_title = esc_attr( strip_tags( $_POST['bbp_topic_split_destination_title'] ) ); 
  92.  
  93. // Use the source topic title 
  94. } else { 
  95. $destination_topic_title = $source_topic->post_title; 
  96.  
  97. // Update the topic 
  98. $destination_topic_id = wp_update_post( array( 
  99. 'ID' => $from_reply->ID,  
  100. 'post_title' => $destination_topic_title,  
  101. 'post_name' => false,  
  102. 'post_type' => bbp_get_topic_post_type(),  
  103. 'post_parent' => $source_topic->post_parent,  
  104. 'menu_order' => 0,  
  105. 'guid' => '' 
  106. ) ); 
  107. $destination_topic = bbp_get_topic( $destination_topic_id ); 
  108.  
  109. // Make sure the new topic knows its a topic 
  110. bbp_update_topic_topic_id( $from_reply->ID ); 
  111.  
  112. // Shouldn't happen 
  113. if ( false === $destination_topic_id || is_wp_error( $destination_topic_id ) || empty( $destination_topic ) ) { 
  114. bbp_add_error( 'bbp_split_topic_destination_reply', __( '<strong>ERROR</strong>: There was a problem converting the reply into the topic. Please try again.', bbpress ) ); 
  115.  
  116. // User cannot publish posts 
  117. } else { 
  118. bbp_add_error( 'bbp_split_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have the permissions to create new topics. The reply could not be converted into a topic.', bbpress ) ); 
  119.  
  120. break; 
  121.  
  122. // Bail if there are errors 
  123. if ( bbp_has_errors() ) 
  124. return; 
  125.  
  126. /** No Errors - Do the Spit ***********************************************/ 
  127.  
  128. // Update counts, etc... 
  129. do_action( 'bbp_pre_split_topic', $from_reply->ID, $source_topic->ID, $destination_topic->ID ); 
  130.  
  131. /** Date Check ************************************************************/ 
  132.  
  133. // Check if the destination topic is older than the from reply 
  134. if ( strtotime( $from_reply->post_date ) < strtotime( $destination_topic->post_date ) ) { 
  135.  
  136. // Set destination topic post_date to 1 second before from reply 
  137. $destination_post_date = date( 'Y-m-d H:i:s', strtotime( $from_reply->post_date ) - 1 ); 
  138.  
  139. // Update destination topic 
  140. wp_update_post( array( 
  141. 'ID' => $destination_topic_id,  
  142. 'post_date' => $destination_post_date,  
  143. 'post_date_gmt' => get_gmt_from_date( $destination_post_date ) 
  144. ) ); 
  145.  
  146. /** Subscriptions *********************************************************/ 
  147.  
  148. // Copy the subscribers 
  149. if ( !empty( $_POST['bbp_topic_subscribers'] ) && "1" === $_POST['bbp_topic_subscribers'] && bbp_is_subscriptions_active() ) { 
  150.  
  151. // Get the subscribers 
  152. $subscribers = bbp_get_topic_subscribers( $source_topic->ID ); 
  153.  
  154. if ( !empty( $subscribers ) ) { 
  155.  
  156. // Add subscribers to new topic 
  157. foreach ( (array) $subscribers as $subscriber ) { 
  158. bbp_add_user_subscription( $subscriber, $destination_topic->ID ); 
  159.  
  160. /** Favorites *************************************************************/ 
  161.  
  162. // Copy the favoriters if told to 
  163. if ( !empty( $_POST['bbp_topic_favoriters'] ) && ( "1" === $_POST['bbp_topic_favoriters'] ) ) { 
  164.  
  165. // Get the favoriters 
  166. $favoriters = bbp_get_topic_favoriters( $source_topic->ID ); 
  167.  
  168. if ( !empty( $favoriters ) ) { 
  169.  
  170. // Add the favoriters to new topic 
  171. foreach ( (array) $favoriters as $favoriter ) { 
  172. bbp_add_user_favorite( $favoriter, $destination_topic->ID ); 
  173.  
  174. /** Tags ******************************************************************/ 
  175.  
  176. // Copy the tags if told to 
  177. if ( !empty( $_POST['bbp_topic_tags'] ) && ( "1" === $_POST['bbp_topic_tags'] ) ) { 
  178.  
  179. // Get the source topic tags 
  180. $source_topic_tags = wp_get_post_terms( $source_topic->ID, bbp_get_topic_tag_tax_id(), array( 'fields' => 'names' ) ); 
  181.  
  182. if ( !empty( $source_topic_tags ) ) { 
  183. wp_set_post_terms( $destination_topic->ID, $source_topic_tags, bbp_get_topic_tag_tax_id(), true ); 
  184.  
  185. /** Split Replies *********************************************************/ 
  186.  
  187. // get_posts() is not used because it doesn't allow us to use '>=' 
  188. // comparision without a filter. 
  189. $replies = (array) $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_date >= %s AND {$wpdb->posts}.post_parent = %d AND {$wpdb->posts}.post_type = %s ORDER BY {$wpdb->posts}.post_date ASC", $from_reply->post_date, $source_topic->ID, bbp_get_reply_post_type() ) ); 
  190.  
  191. // Make sure there are replies to loop through 
  192. if ( !empty( $replies ) && !is_wp_error( $replies ) ) { 
  193.  
  194. // Calculate starting point for reply positions 
  195. switch ( $split_option ) { 
  196.  
  197. // Get topic reply count for existing topic 
  198. case 'existing' : 
  199. $reply_position = bbp_get_topic_reply_count( $destination_topic->ID ); 
  200. break; 
  201.  
  202. // Account for new lead topic 
  203. case 'reply' : 
  204. $reply_position = 1; 
  205. break; 
  206.  
  207. // Save reply ids 
  208. $reply_ids = array(); 
  209.  
  210. // Change the post_parent of each reply to the destination topic id 
  211. foreach ( $replies as $reply ) { 
  212.  
  213. // Bump the reply position each iteration through the loop 
  214. $reply_position++; 
  215.  
  216. // Update the reply 
  217. wp_update_post( array( 
  218. 'ID' => $reply->ID,  
  219. 'post_title' => sprintf( __( 'Reply To: %s', bbpress ), $destination_topic->post_title ),  
  220. 'post_name' => false, // will be automatically generated 
  221. 'post_parent' => $destination_topic->ID,  
  222. 'menu_order' => $reply_position,  
  223. 'guid' => '' 
  224. ) ); 
  225.  
  226. // Gather reply ids 
  227. $reply_ids[] = $reply->ID; 
  228.  
  229. // Adjust reply meta values 
  230. bbp_update_reply_topic_id( $reply->ID, $destination_topic->ID ); 
  231. bbp_update_reply_forum_id( $reply->ID, bbp_get_topic_forum_id( $destination_topic->ID ) ); 
  232.  
  233. // Adjust reply to values 
  234. $reply_to = bbp_get_reply_to( $reply->ID ); 
  235.  
  236. // Not a reply to a reply that moved over 
  237. if ( !in_array( $reply_to, $reply_ids ) ) { 
  238. bbp_update_reply_to( $reply->ID, 0 ); 
  239.  
  240. // New topic from reply can't be a reply to 
  241. if ( ( $from_reply->ID === $destination_topic->ID ) && ( $from_reply->ID === $reply_to ) ) { 
  242. bbp_update_reply_to( $reply->ID, 0 ); 
  243.  
  244. // Do additional actions per split reply 
  245. do_action( 'bbp_split_topic_reply', $reply->ID, $destination_topic->ID ); 
  246.  
  247. // Remove reply to from new topic 
  248. if ( $from_reply->ID === $destination_topic->ID ) { 
  249. delete_post_meta( $from_reply->ID, '_bbp_reply_to' ); 
  250.  
  251. // Set the last reply ID and freshness 
  252. $last_reply_id = $reply->ID; 
  253. $freshness = $reply->post_date; 
  254.  
  255. // Set the last reply ID and freshness to the from_reply 
  256. } else { 
  257. $last_reply_id = $from_reply->ID; 
  258. $freshness = $from_reply->post_date; 
  259.  
  260. // It is a new topic and we need to set some default metas to make 
  261. // the topic display in bbp_has_topics() list 
  262. if ( 'reply' === $split_option ) { 
  263. bbp_update_topic_last_reply_id ( $destination_topic->ID, $last_reply_id ); 
  264. bbp_update_topic_last_active_id ( $destination_topic->ID, $last_reply_id ); 
  265. bbp_update_topic_last_active_time( $destination_topic->ID, $freshness ); 
  266.  
  267. // Update source topic ID last active 
  268. bbp_update_topic_last_reply_id ( $source_topic->ID ); 
  269. bbp_update_topic_last_active_id ( $source_topic->ID ); 
  270. bbp_update_topic_last_active_time( $source_topic->ID ); 
  271.  
  272. /** Successful Split ******************************************************/ 
  273.  
  274. // Update counts, etc... 
  275. do_action( 'bbp_post_split_topic', $from_reply->ID, $source_topic->ID, $destination_topic->ID ); 
  276.  
  277. // Redirect back to the topic 
  278. wp_safe_redirect( bbp_get_topic_permalink( $destination_topic->ID ) ); 
  279.  
  280. // For good measure 
  281. exit();