bbp_move_topic_handler

Handle the moving of a topic from one forum to another.

Description

bbp_move_topic_handler( (int) $topic_id, (int) $old_forum_id, (int) $new_forum_id ); 

This includes walking up the old and new branches and updating the counts.

Parameters (3)

0. $topic_id (int)
The topic id.
1. $old_forum_id (int)
The old forum id.
2. $new_forum_id (int)
The new forum id.

Usage

  1. if ( !function_exists( 'bbp_move_topic_handler' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/topics/functions.php'; 
  3.  
  4. // The topic id. 
  5. $topic_id = -1; 
  6.  
  7. // The old forum id. 
  8. $old_forum_id = -1; 
  9.  
  10. // The new forum id. 
  11. $new_forum_id = -1; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = bbp_move_topic_handler($topic_id, $old_forum_id, $new_forum_id); 
  15.  

Defined (1)

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

/includes/topics/functions.php  
  1. function bbp_move_topic_handler( $topic_id, $old_forum_id, $new_forum_id ) { 
  2.  
  3. // Validate parameters 
  4. $topic_id = bbp_get_topic_id( $topic_id ); 
  5. $old_forum_id = bbp_get_forum_id( $old_forum_id ); 
  6. $new_forum_id = bbp_get_forum_id( $new_forum_id ); 
  7.  
  8. // Update topic forum's ID 
  9. bbp_update_topic_forum_id( $topic_id, $new_forum_id ); 
  10.  
  11. /** Stickies **************************************************************/ 
  12.  
  13. // Get forum stickies 
  14. $old_stickies = bbp_get_stickies( $old_forum_id ); 
  15.  
  16. // Only proceed if stickies are found 
  17. if ( !empty( $old_stickies ) ) { 
  18.  
  19. // Define local variables 
  20. $updated_stickies = array(); 
  21.  
  22. // Loop through stickies of forum and add misses to the updated array 
  23. foreach ( (array) $old_stickies as $sticky_topic_id ) { 
  24. if ( $topic_id !== $sticky_topic_id ) { 
  25. $updated_stickies[] = $sticky_topic_id; 
  26.  
  27. // If stickies are different, update or delete them 
  28. if ( $updated_stickies !== $old_stickies ) { 
  29.  
  30. // No more stickies so delete the meta 
  31. if ( empty( $updated_stickies ) ) { 
  32. delete_post_meta( $old_forum_id, '_bbp_sticky_topics' ); 
  33.  
  34. // Still stickies so update the meta 
  35. } else { 
  36. update_post_meta( $old_forum_id, '_bbp_sticky_topics', $updated_stickies ); 
  37.  
  38. // Topic was sticky, so restick in new forum 
  39. bbp_stick_topic( $topic_id ); 
  40.  
  41. /** Topic Replies *********************************************************/ 
  42.  
  43. // Get the topics replies 
  44. $replies = bbp_get_all_child_ids( $topic_id, bbp_get_reply_post_type() ); 
  45.  
  46. // Update the forum_id of all replies in the topic 
  47. foreach ( $replies as $reply_id ) { 
  48. bbp_update_reply_forum_id( $reply_id, $new_forum_id ); 
  49.  
  50. /** Old forum_id **********************************************************/ 
  51.  
  52. // Get topic ancestors 
  53. $old_forum_ancestors = array_values( array_unique( array_merge( array( $old_forum_id ), (array) get_post_ancestors( $old_forum_id ) ) ) ); 
  54.  
  55. // Loop through ancestors and update them 
  56. if ( !empty( $old_forum_ancestors ) ) { 
  57. foreach ( $old_forum_ancestors as $ancestor ) { 
  58. if ( bbp_is_forum( $ancestor ) ) { 
  59. bbp_update_forum( array( 
  60. 'forum_id' => $ancestor,  
  61. ) ); 
  62.  
  63. /** New forum_id **********************************************************/ 
  64.  
  65. // Make sure we're not walking twice 
  66. if ( !in_array( $new_forum_id, $old_forum_ancestors ) ) { 
  67.  
  68. // Get topic ancestors 
  69. $new_forum_ancestors = array_values( array_unique( array_merge( array( $new_forum_id ), (array) get_post_ancestors( $new_forum_id ) ) ) ); 
  70.  
  71. // Make sure we're not walking twice 
  72. $new_forum_ancestors = array_diff( $new_forum_ancestors, $old_forum_ancestors ); 
  73.  
  74. // Loop through ancestors and update them 
  75. if ( !empty( $new_forum_ancestors ) ) { 
  76. foreach ( $new_forum_ancestors as $ancestor ) { 
  77. if ( bbp_is_forum( $ancestor ) ) { 
  78. bbp_update_forum( array( 
  79. 'forum_id' => $ancestor,  
  80. ) );