bbp_merge_topic_handler

Merge topic handler.

Description

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

Handles the front end merge topic submission

Parameters (1)

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

Usage

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

Defined (1)

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

/includes/topics/functions.php  
  1. function bbp_merge_topic_handler( $action = '' ) { 
  2.  
  3. // Bail if action is not bbp-merge-topic 
  4. if ( 'bbp-merge-topic' !== $action ) 
  5. return; 
  6.  
  7. // Define local variable(s) 
  8. $source_topic_id = $destination_topic_id = 0; 
  9. $source_topic = $destination_topic = 0; 
  10. $subscribers = $favoriters = $replies = array(); 
  11.  
  12. /** Source Topic **********************************************************/ 
  13.  
  14. // Topic id 
  15. if ( empty( $_POST['bbp_topic_id'] ) ) { 
  16. bbp_add_error( 'bbp_merge_topic_source_id', __( '<strong>ERROR</strong>: Topic ID not found.', bbpress ) ); 
  17. } else { 
  18. $source_topic_id = (int) $_POST['bbp_topic_id']; 
  19.  
  20. // Nonce check 
  21. if ( ! bbp_verify_nonce_request( 'bbp-merge-topic_' . $source_topic_id ) ) { 
  22. bbp_add_error( 'bbp_merge_topic_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', bbpress ) ); 
  23. return; 
  24.  
  25. // Source topic not found 
  26. } elseif ( !$source_topic = bbp_get_topic( $source_topic_id ) ) { 
  27. bbp_add_error( 'bbp_merge_topic_source_not_found', __( '<strong>ERROR</strong>: The topic you want to merge was not found.', bbpress ) ); 
  28. return; 
  29.  
  30. // Cannot edit source topic 
  31. if ( !current_user_can( 'edit_topic', $source_topic->ID ) ) { 
  32. bbp_add_error( 'bbp_merge_topic_source_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the source topic.', bbpress ) ); 
  33. return; 
  34.  
  35. /** Destination Topic *****************************************************/ 
  36.  
  37. // Topic id 
  38. if ( empty( $_POST['bbp_destination_topic'] ) ) 
  39. bbp_add_error( 'bbp_merge_topic_destination_id', __( '<strong>ERROR</strong>: Destination topic ID not found.', bbpress ) ); 
  40. else 
  41. $destination_topic_id = (int) $_POST['bbp_destination_topic']; 
  42.  
  43. // Destination topic not found 
  44. if ( !$destination_topic = bbp_get_topic( $destination_topic_id ) ) 
  45. bbp_add_error( 'bbp_merge_topic_destination_not_found', __( '<strong>ERROR</strong>: The topic you want to merge to was not found.', bbpress ) ); 
  46.  
  47. // Cannot edit destination topic 
  48. if ( !current_user_can( 'edit_topic', $destination_topic->ID ) ) 
  49. bbp_add_error( 'bbp_merge_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the destination topic.', bbpress ) ); 
  50.  
  51. // Bail if errors 
  52. if ( bbp_has_errors() ) 
  53. return; 
  54.  
  55. /** No Errors *************************************************************/ 
  56.  
  57. // Update counts, etc... 
  58. do_action( 'bbp_merge_topic', $destination_topic->ID, $source_topic->ID ); 
  59.  
  60. /** Date Check ************************************************************/ 
  61.  
  62. // Check if the destination topic is older than the source topic 
  63. if ( strtotime( $source_topic->post_date ) < strtotime( $destination_topic->post_date ) ) { 
  64.  
  65. // Set destination topic post_date to 1 second before source topic 
  66. $destination_post_date = date( 'Y-m-d H:i:s', strtotime( $source_topic->post_date ) - 1 ); 
  67.  
  68. // Update destination topic 
  69. wp_update_post( array( 
  70. 'ID' => $destination_topic_id,  
  71. 'post_date' => $destination_post_date,  
  72. 'post_date_gmt' => get_gmt_from_date( $destination_post_date ) 
  73. ) ); 
  74.  
  75. /** Subscriptions *********************************************************/ 
  76.  
  77. // Get subscribers from source topic 
  78. $subscribers = bbp_get_topic_subscribers( $source_topic->ID ); 
  79.  
  80. // Remove the topic from everybody's subscriptions 
  81. if ( !empty( $subscribers ) ) { 
  82.  
  83. // Loop through each user 
  84. foreach ( (array) $subscribers as $subscriber ) { 
  85.  
  86. // Shift the subscriber if told to 
  87. if ( !empty( $_POST['bbp_topic_subscribers'] ) && ( "1" === $_POST['bbp_topic_subscribers'] ) && bbp_is_subscriptions_active() ) 
  88. bbp_add_user_subscription( $subscriber, $destination_topic->ID ); 
  89.  
  90. // Remove old subscription 
  91. bbp_remove_user_subscription( $subscriber, $source_topic->ID ); 
  92.  
  93. /** Favorites *************************************************************/ 
  94.  
  95. // Get favoriters from source topic 
  96. $favoriters = bbp_get_topic_favoriters( $source_topic->ID ); 
  97.  
  98. // Remove the topic from everybody's favorites 
  99. if ( !empty( $favoriters ) ) { 
  100.  
  101. // Loop through each user 
  102. foreach ( (array) $favoriters as $favoriter ) { 
  103.  
  104. // Shift the favoriter if told to 
  105. if ( !empty( $_POST['bbp_topic_favoriters'] ) && "1" === $_POST['bbp_topic_favoriters'] ) 
  106. bbp_add_user_favorite( $favoriter, $destination_topic->ID ); 
  107.  
  108. // Remove old favorite 
  109. bbp_remove_user_favorite( $favoriter, $source_topic->ID ); 
  110.  
  111. /** Tags ******************************************************************/ 
  112.  
  113. // Get the source topic tags 
  114. $source_topic_tags = wp_get_post_terms( $source_topic->ID, bbp_get_topic_tag_tax_id(), array( 'fields' => 'names' ) ); 
  115.  
  116. // Tags to possibly merge 
  117. if ( !empty( $source_topic_tags ) && !is_wp_error( $source_topic_tags ) ) { 
  118.  
  119. // Shift the tags if told to 
  120. if ( !empty( $_POST['bbp_topic_tags'] ) && ( "1" === $_POST['bbp_topic_tags'] ) ) 
  121. wp_set_post_terms( $destination_topic->ID, $source_topic_tags, bbp_get_topic_tag_tax_id(), true ); 
  122.  
  123. // Delete the tags from the source topic 
  124. wp_delete_object_term_relationships( $source_topic->ID, bbp_get_topic_tag_tax_id() ); 
  125.  
  126. /** Source Topic **********************************************************/ 
  127.  
  128. // Status 
  129. bbp_open_topic( $source_topic->ID ); 
  130.  
  131. // Sticky 
  132. bbp_unstick_topic( $source_topic->ID ); 
  133.  
  134. // Get the replies of the source topic 
  135. $replies = (array) get_posts( array( 
  136. 'post_parent' => $source_topic->ID,  
  137. 'post_type' => bbp_get_reply_post_type(),  
  138. 'posts_per_page' => -1,  
  139. 'order' => 'ASC' 
  140. ) ); 
  141.  
  142. // Prepend the source topic to its replies array for processing 
  143. array_unshift( $replies, $source_topic ); 
  144.  
  145. if ( !empty( $replies ) ) { 
  146.  
  147. /** Merge Replies *****************************************************/ 
  148.  
  149. // Change the post_parent of each reply to the destination topic id 
  150. foreach ( $replies as $reply ) { 
  151.  
  152. // Update the reply 
  153. wp_update_post( array( 
  154. 'ID' => $reply->ID,  
  155. 'post_title' => sprintf( __( 'Reply To: %s', bbpress ), $destination_topic->post_title ),  
  156. 'post_name' => false,  
  157. 'post_type' => bbp_get_reply_post_type(),  
  158. 'post_parent' => $destination_topic->ID,  
  159. 'guid' => '' 
  160. ) ); 
  161.  
  162. // Adjust reply meta values 
  163. bbp_update_reply_topic_id( $reply->ID, $destination_topic->ID ); 
  164. bbp_update_reply_forum_id( $reply->ID, bbp_get_topic_forum_id( $destination_topic->ID ) ); 
  165.  
  166. // Adjust reply to values 
  167. $reply_to = bbp_get_reply_to( $reply->ID ); 
  168. if ( empty( $reply_to ) ) { 
  169. bbp_update_reply_to( $reply->ID, $source_topic->ID ); 
  170.  
  171. // Do additional actions per merged reply 
  172. do_action( 'bbp_merged_topic_reply', $reply->ID, $destination_topic->ID ); 
  173.  
  174. /** Successful Merge ******************************************************/ 
  175.  
  176. // Update topic's last meta data 
  177. bbp_update_topic_last_reply_id ( $destination_topic->ID ); 
  178. bbp_update_topic_last_active_id ( $destination_topic->ID ); 
  179. bbp_update_topic_last_active_time( $destination_topic->ID ); 
  180.  
  181. // Send the post parent of the source topic as it has been shifted 
  182. // (possibly to a new forum) so we need to update the counts of the 
  183. // old forum as well as the new one 
  184. do_action( 'bbp_merged_topic', $destination_topic->ID, $source_topic->ID, $source_topic->post_parent ); 
  185.  
  186. // Redirect back to new topic 
  187. wp_safe_redirect( bbp_get_topic_permalink( $destination_topic->ID ) ); 
  188.  
  189. // For good measure 
  190. exit();