bb_delete_topic

The BuddyPress bb delete topic function.

Description

bb_delete_topic( $topic_id, (int) $new_status = 0 ); 

Parameters (2)

0. $topic_id
The topic id.
1. $new_status — Optional. (int)
The new status.

Usage

  1. if ( !function_exists( 'bb_delete_topic' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-includes/functions.bb-topics.php'; 
  3.  
  4. // The topic id. 
  5. $topic_id = null; 
  6.  
  7. // The new status. 
  8. $new_status = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bb_delete_topic($topic_id, $new_status); 
  12.  

Defined (1)

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

/bp-forums/bbpress/bb-includes/functions.bb-topics.php  
  1. function bb_delete_topic( $topic_id, $new_status = 0 ) { 
  2. global $bbdb; 
  3. $topic_id = (int) $topic_id; 
  4. if ( $topic = get_topic( $topic_id ) ) { 
  5. $new_status = (int) $new_status; 
  6. $old_status = (int) $topic->topic_status; 
  7. if ( $new_status == $old_status ) 
  8. return; 
  9.  
  10. $thread_args = array( 'per_page' => -1, 'order' => 'DESC' ); 
  11. if ( 0 != $old_status && 0 == $new_status ) 
  12. $thread_args['post_status'] = 'all'; 
  13. $poster_ids = array(); 
  14. $posts = get_thread( $topic_id, $thread_args ); 
  15. if ( $posts && count( $posts ) ) { 
  16. foreach ( $posts as $post ) { 
  17. _bb_delete_post( $post->post_id, $new_status ); 
  18. $poster_ids[] = $post->poster_id; 
  19.  
  20. if ( count( $poster_ids ) ) { 
  21. foreach ( array_unique( $poster_ids ) as $id ) { 
  22. if ( $user = bb_get_user( $id ) ) { 
  23. $topics_replied_key = $bbdb->prefix . 'topics_replied'; 
  24. bb_update_usermeta( $user->ID, $topics_replied_key, ( $old_status ? $user->$topics_replied_key + 1 : $user->$topics_replied_key - 1 ) ); 
  25.  
  26. if ( $ids = $bbdb->get_col( "SELECT user_id, meta_value FROM $bbdb->usermeta WHERE meta_key = 'favorites' and FIND_IN_SET('$topic_id', meta_value) > 0" ) ) 
  27. foreach ( $ids as $id ) 
  28. bb_remove_user_favorite( $id, $topic_id ); 
  29.  
  30. switch ( $new_status ) { 
  31. case 0: // Undeleting 
  32. $bbdb->update( $bbdb->topics, array( 'topic_status' => $new_status ), compact( 'topic_id' ) ); 
  33. $topic_posts = (int) $bbdb->get_var( $bbdb->prepare( 
  34. "SELECT COUNT(*) FROM $bbdb->posts WHERE topic_id = %d AND post_status = 0", $topic_id 
  35. ) ); 
  36. $all_posts = (int) $bbdb->get_var( $bbdb->prepare( 
  37. "SELECT COUNT(*) FROM $bbdb->posts WHERE topic_id = %d", $topic_id 
  38. ) ); 
  39. bb_update_topicmeta( $topic_id, 'deleted_posts', $all_posts - $topic_posts ); 
  40. $bbdb->query( $bbdb->prepare( 
  41. "UPDATE $bbdb->forums SET topics = topics + 1, posts = posts + %d WHERE forum_id = %d", $topic_posts, $topic->forum_id 
  42. ) ); 
  43. $bbdb->update( $bbdb->topics, compact( 'topic_posts' ), compact( 'topic_id' ) ); 
  44. bb_topic_set_last_post( $topic_id ); 
  45. bb_update_post_positions( $topic_id ); 
  46. break; 
  47.  
  48. default: // Other statuses (like Delete and Bozo) 
  49. bb_remove_topic_tags( $topic_id ); 
  50. $bbdb->update( $bbdb->topics, array( 'topic_status' => $new_status, 'tag_count' => 0 ), compact( 'topic_id' ) ); 
  51. $bbdb->query( $bbdb->prepare( 
  52. "UPDATE $bbdb->forums SET topics = topics - 1, posts = posts - %d WHERE forum_id = %d", $topic->topic_posts, $topic->forum_id 
  53. ) ); 
  54. break; 
  55.  
  56. do_action( 'bb_delete_topic', $topic_id, $new_status, $old_status ); 
  57. wp_cache_delete( $topic_id, 'bb_topic' ); 
  58. wp_cache_delete( $topic->topic_slug, 'bb_topic_slug' ); 
  59. wp_cache_delete( $topic_id, 'bb_thread' ); 
  60. wp_cache_delete( $topic->forum_id, 'bb_forum' ); 
  61. wp_cache_flush( bb_query ); 
  62. wp_cache_flush( 'bb_cache_posts_post_ids' ); 
  63. return $topic_id; 
  64. } else { 
  65. return false;