bbp_move_reply_handler

Move reply handler.

Description

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

Handles the front end move reply submission

Parameters (1)

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

Usage

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

Defined (1)

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

/includes/replies/functions.php  
  1. function bbp_move_reply_handler( $action = '' ) { 
  2.  
  3. // Bail if action is not 'bbp-move-reply' 
  4. if ( 'bbp-move-reply' !== $action ) 
  5. return; 
  6.  
  7. // Prevent debug notices 
  8. $move_reply_id = $destination_topic_id = 0; 
  9. $destination_topic_title = ''; 
  10. $destination_topic = $move_reply = $source_topic = ''; 
  11.  
  12. /** Move Reply ***********************************************************/ 
  13.  
  14. if ( empty( $_POST['bbp_reply_id'] ) ) { 
  15. bbp_add_error( 'bbp_move_reply_reply_id', __( '<strong>ERROR</strong>: Reply ID to move not found!', bbpress ) ); 
  16. } else { 
  17. $move_reply_id = (int) $_POST['bbp_reply_id']; 
  18.  
  19. $move_reply = bbp_get_reply( $move_reply_id ); 
  20.  
  21. // Reply exists 
  22. if ( empty( $move_reply ) ) 
  23. bbp_add_error( 'bbp_mover_reply_r_not_found', __( '<strong>ERROR</strong>: The reply you want to move was not found.', bbpress ) ); 
  24.  
  25. /** Topic to Move From ***************************************************/ 
  26.  
  27. // Get the reply's current topic 
  28. $source_topic = bbp_get_topic( $move_reply->post_parent ); 
  29.  
  30. // No topic 
  31. if ( empty( $source_topic ) ) { 
  32. bbp_add_error( 'bbp_move_reply_source_not_found', __( '<strong>ERROR</strong>: The topic you want to move from was not found.', bbpress ) ); 
  33.  
  34. // Nonce check failed 
  35. if ( ! bbp_verify_nonce_request( 'bbp-move-reply_' . $move_reply->ID ) ) { 
  36. bbp_add_error( 'bbp_move_reply_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', bbpress ) ); 
  37. return; 
  38.  
  39. // Use cannot edit topic 
  40. if ( !current_user_can( 'edit_topic', $source_topic->ID ) ) { 
  41. bbp_add_error( 'bbp_move_reply_source_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the source topic.', bbpress ) ); 
  42.  
  43. // How to move 
  44. if ( !empty( $_POST['bbp_reply_move_option'] ) ) { 
  45. $move_option = (string) trim( $_POST['bbp_reply_move_option'] ); 
  46.  
  47. // Invalid move option 
  48. if ( empty( $move_option ) || !in_array( $move_option, array( 'existing', 'topic' ) ) ) { 
  49. bbp_add_error( 'bbp_move_reply_option', __( '<strong>ERROR</strong>: You need to choose a valid move option.', bbpress ) ); 
  50.  
  51. // Valid move option 
  52. } else { 
  53.  
  54. // What kind of move 
  55. switch ( $move_option ) { 
  56.  
  57. // Into an existing topic 
  58. case 'existing' : 
  59.  
  60. // Get destination topic id 
  61. if ( empty( $_POST['bbp_destination_topic'] ) ) { 
  62. bbp_add_error( 'bbp_move_reply_destination_id', __( '<strong>ERROR</strong>: Destination topic ID not found!', bbpress ) ); 
  63. } else { 
  64. $destination_topic_id = (int) $_POST['bbp_destination_topic']; 
  65.  
  66. // Get the destination topic 
  67. $destination_topic = bbp_get_topic( $destination_topic_id ); 
  68.  
  69. // No destination topic 
  70. if ( empty( $destination_topic ) ) { 
  71. bbp_add_error( 'bbp_move_reply_destination_not_found', __( '<strong>ERROR</strong>: The topic you want to move to was not found!', bbpress ) ); 
  72.  
  73. // User cannot edit the destination topic 
  74. if ( !current_user_can( 'edit_topic', $destination_topic->ID ) ) { 
  75. bbp_add_error( 'bbp_move_reply_destination_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the destination topic!', bbpress ) ); 
  76.  
  77. // Bump the reply position 
  78. $reply_position = bbp_get_topic_reply_count( $destination_topic->ID ) + 1; 
  79.  
  80. // Update the reply 
  81. wp_update_post( array( 
  82. 'ID' => $move_reply->ID,  
  83. 'post_title' => sprintf( __( 'Reply To: %s', bbpress ), $destination_topic->post_title ),  
  84. 'post_name' => false, // will be automatically generated 
  85. 'post_parent' => $destination_topic->ID,  
  86. 'menu_order' => $reply_position,  
  87. 'guid' => '' 
  88. ) ); 
  89.  
  90. // Adjust reply meta values 
  91. bbp_update_reply_topic_id( $move_reply->ID, $destination_topic->ID ); 
  92. bbp_update_reply_forum_id( $move_reply->ID, bbp_get_topic_forum_id( $destination_topic->ID ) ); 
  93.  
  94. break; 
  95.  
  96. // Move reply to a new topic 
  97. case 'topic' : 
  98. default : 
  99.  
  100. // User needs to be able to publish topics 
  101. if ( current_user_can( 'publish_topics' ) ) { 
  102.  
  103. // Use the new title that was passed 
  104. if ( !empty( $_POST['bbp_reply_move_destination_title'] ) ) { 
  105. $destination_topic_title = esc_attr( strip_tags( $_POST['bbp_reply_move_destination_title'] ) ); 
  106.  
  107. // Use the source topic title 
  108. } else { 
  109. $destination_topic_title = $source_topic->post_title; 
  110.  
  111. // Update the topic 
  112. $destination_topic_id = wp_update_post( array( 
  113. 'ID' => $move_reply->ID,  
  114. 'post_title' => $destination_topic_title,  
  115. 'post_name' => false,  
  116. 'post_type' => bbp_get_topic_post_type(),  
  117. 'post_parent' => $source_topic->post_parent,  
  118. 'guid' => '' 
  119. ) ); 
  120. $destination_topic = bbp_get_topic( $destination_topic_id ); 
  121.  
  122. // Make sure the new topic knows its a topic 
  123. bbp_update_topic_topic_id( $move_reply->ID ); 
  124.  
  125. // Shouldn't happen 
  126. if ( false === $destination_topic_id || is_wp_error( $destination_topic_id ) || empty( $destination_topic ) ) { 
  127. bbp_add_error( 'bbp_move_reply_destination_reply', __( '<strong>ERROR</strong>: There was a problem converting the reply into the topic. Please try again.', bbpress ) ); 
  128.  
  129. // User cannot publish posts 
  130. } else { 
  131. bbp_add_error( 'bbp_move_reply_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 ) ); 
  132.  
  133. break; 
  134.  
  135. // Bail if there are errors 
  136. if ( bbp_has_errors() ) { 
  137. return; 
  138.  
  139. /** No Errors - Clean Up **************************************************/ 
  140.  
  141. // Update counts, etc... 
  142. do_action( 'bbp_pre_move_reply', $move_reply->ID, $source_topic->ID, $destination_topic->ID ); 
  143.  
  144. /** Date Check ************************************************************/ 
  145.  
  146. // Check if the destination topic is older than the move reply 
  147. if ( strtotime( $move_reply->post_date ) < strtotime( $destination_topic->post_date ) ) { 
  148.  
  149. // Set destination topic post_date to 1 second before from reply 
  150. $destination_post_date = date( 'Y-m-d H:i:s', strtotime( $move_reply->post_date ) - 1 ); 
  151.  
  152. // Update destination topic 
  153. wp_update_post( array( 
  154. 'ID' => $destination_topic_id,  
  155. 'post_date' => $destination_post_date,  
  156. 'post_date_gmt' => get_gmt_from_date( $destination_post_date ) 
  157. ) ); 
  158.  
  159. // Set the last reply ID and freshness to the move_reply 
  160. $last_reply_id = $move_reply->ID; 
  161. $freshness = $move_reply->post_date; 
  162.  
  163. // Get the reply to 
  164. $parent = bbp_get_reply_to( $move_reply->ID ); 
  165.  
  166. // Fix orphaned children 
  167. $children = get_posts( array( 
  168. 'post_type' => bbp_get_reply_post_type(),  
  169. 'meta_key' => '_bbp_reply_to',  
  170. 'meta_value' => $move_reply->ID,  
  171. ) ); 
  172. foreach ( $children as $child ) 
  173. bbp_update_reply_to( $child->ID, $parent ); 
  174.  
  175. // Remove reply_to from moved reply 
  176. delete_post_meta( $move_reply->ID, '_bbp_reply_to' ); 
  177.  
  178. // It is a new topic and we need to set some default metas to make 
  179. // the topic display in bbp_has_topics() list 
  180. if ( 'topic' === $move_option ) { 
  181. bbp_update_topic_last_reply_id ( $destination_topic->ID, $last_reply_id ); 
  182. bbp_update_topic_last_active_id ( $destination_topic->ID, $last_reply_id ); 
  183. bbp_update_topic_last_active_time( $destination_topic->ID, $freshness ); 
  184.  
  185. // Otherwise update the existing destination topic 
  186. } else { 
  187. bbp_update_topic_last_reply_id ( $destination_topic->ID ); 
  188. bbp_update_topic_last_active_id ( $destination_topic->ID ); 
  189. bbp_update_topic_last_active_time( $destination_topic->ID ); 
  190.  
  191. // Update source topic ID last active 
  192. bbp_update_topic_last_reply_id ( $source_topic->ID ); 
  193. bbp_update_topic_last_active_id ( $source_topic->ID ); 
  194. bbp_update_topic_last_active_time( $source_topic->ID ); 
  195.  
  196. /** Successful Move ******************************************************/ 
  197.  
  198. // Update counts, etc... 
  199. do_action( 'bbp_post_move_reply', $move_reply->ID, $source_topic->ID, $destination_topic->ID ); 
  200.  
  201. // Redirect back to the topic 
  202. wp_safe_redirect( bbp_get_topic_permalink( $destination_topic->ID ) ); 
  203.  
  204. // For good measure 
  205. exit();