bb_insert_topic

The BuddyPress bb insert topic function.

Description

bb_insert_topic( (null) $args = null ); 

Parameters (1)

0. $args — Optional. (null) => null
The args.

Usage

  1. if ( !function_exists( 'bb_insert_topic' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-includes/functions.bb-topics.php'; 
  3.  
  4. // The args. 
  5. $args = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bb_insert_topic($args); 
  9.  

Defined (1)

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

/bp-forums/bbpress/bb-includes/functions.bb-topics.php  
  1. function bb_insert_topic( $args = null ) { 
  2. global $bbdb; 
  3.  
  4. if ( !$args = wp_parse_args( $args ) ) 
  5. return false; 
  6.  
  7. $fields = array_keys( $args ); 
  8.  
  9. if ( isset($args['topic_id']) && false !== $args['topic_id'] ) { 
  10. $update = true; 
  11. if ( !$topic_id = (int) get_topic_id( $args['topic_id'] ) ) 
  12. return false; 
  13. // Get from db, not cache. Good idea? Prevents trying to update meta_key names in the topic table (get_topic() returns appended topic obj) 
  14. $topic = $bbdb->get_row( $bbdb->prepare( "SELECT * FROM $bbdb->topics WHERE topic_id = %d", $topic_id ) ); 
  15. $defaults = get_object_vars( $topic ); 
  16. unset($defaults['topic_id']); 
  17.  
  18. // Only update the args we passed 
  19. $fields = array_intersect( $fields, array_keys($defaults) ); 
  20. if ( in_array( 'topic_poster', $fields ) ) 
  21. $fields[] = 'topic_poster_name'; 
  22. if ( in_array( 'topic_last_poster', $fields ) ) 
  23. $fields[] = 'topic_last_poster_name'; 
  24. } else { 
  25. $topic_id = false; 
  26. $update = false; 
  27.  
  28. $now = bb_current_time('mysql'); 
  29. $current_user_id = bb_get_current_user_info( 'id' ); 
  30.  
  31. $defaults = array( 
  32. 'topic_title' => '',  
  33. 'topic_slug' => '',  
  34. 'topic_poster' => $current_user_id, // accepts ids 
  35. 'topic_poster_name' => '', // accept names 
  36. 'topic_last_poster' => $current_user_id, // accepts ids 
  37. 'topic_last_poster_name' => '', // accept names 
  38. 'topic_start_time' => $now,  
  39. 'topic_time' => $now,  
  40. 'topic_open' => 1,  
  41. 'forum_id' => 0 // accepts ids or slugs 
  42. ); 
  43.  
  44. // Insert all args 
  45. $fields = array_keys($defaults); 
  46.  
  47. $defaults['tags'] = false; // accepts array or comma delimited string 
  48. extract( wp_parse_args( $args, $defaults ) ); 
  49. unset($defaults['tags']); 
  50.  
  51. if ( !$forum = bb_get_forum( $forum_id ) ) 
  52. return false; 
  53. $forum_id = (int) $forum->forum_id; 
  54.  
  55. if ( !$user = bb_get_user( $topic_poster ) ) 
  56. $user = bb_get_user( $topic_poster_name, array( 'by' => 'login' ) ); 
  57.  
  58. if ( !empty( $user ) ) { 
  59. $topic_poster = $user->ID; 
  60. $topic_poster_name = $user->user_login; 
  61.  
  62. if ( !$last_user = bb_get_user( $topic_last_poster ) ) 
  63. $last_user = bb_get_user( $topic_last_poster_name, array( 'by' => 'login' ) ); 
  64.  
  65. if ( !empty( $last_user ) ) { 
  66. $topic_last_poster = $last_user->ID; 
  67. $topic_last_poster_name = $last_user->user_login; 
  68.  
  69. if ( in_array( 'topic_title', $fields ) ) { 
  70. $topic_title = apply_filters( 'pre_topic_title', $topic_title, $topic_id ); 
  71. if ( strlen($topic_title) < 1 ) 
  72. return false; 
  73.  
  74. if ( in_array( 'topic_slug', $fields ) ) { 
  75. $slug_sql = $update ? 
  76. "SELECT topic_slug FROM $bbdb->topics WHERE topic_slug = %s AND topic_id != %d" : 
  77. "SELECT topic_slug FROM $bbdb->topics WHERE topic_slug = %s"; 
  78.  
  79. $topic_slug = $_topic_slug = bb_slug_sanitize( $topic_slug ? $topic_slug : wp_specialchars_decode( $topic_title, ENT_QUOTES ) ); 
  80. if ( strlen( $_topic_slug ) < 1 ) 
  81. $topic_slug = $_topic_slug = '0'; 
  82.  
  83. while ( is_numeric($topic_slug) || $existing_slug = $bbdb->get_var( $bbdb->prepare( $slug_sql, $topic_slug, $topic_id ) ) ) 
  84. $topic_slug = bb_slug_increment( $_topic_slug, $existing_slug ); 
  85.  
  86. if ( $update ) { 
  87. $bbdb->update( $bbdb->topics, compact( $fields ), compact( 'topic_id' ) ); 
  88. wp_cache_delete( $topic_id, 'bb_topic' ); 
  89. if ( in_array( 'topic_slug', $fields ) ) 
  90. wp_cache_delete( $topic->topic_slug, 'bb_topic_slug' ); 
  91. wp_cache_flush( bb_query ); 
  92. wp_cache_flush( 'bb_cache_posts_post_ids' ); 
  93. do_action( 'bb_update_topic', $topic_id ); 
  94. } else { 
  95. $bbdb->insert( $bbdb->topics, compact( $fields ) ); 
  96. $topic_id = $bbdb->insert_id; 
  97. $bbdb->query( $bbdb->prepare( "UPDATE $bbdb->forums SET topics = topics + 1 WHERE forum_id = %d", $forum_id ) ); 
  98. wp_cache_delete( $forum_id, 'bb_forum' ); 
  99. wp_cache_flush( bb_query ); 
  100. wp_cache_flush( 'bb_cache_posts_post_ids' ); 
  101. do_action( 'bb_new_topic', $topic_id ); 
  102.  
  103. if ( !empty( $tags ) ) 
  104. bb_add_topic_tags( $topic_id, $tags ); 
  105.  
  106. do_action( 'bb_insert_topic', $topic_id, $args, compact( array_keys($args) ) ); // topic_id, what was passed, what was used 
  107.  
  108. return $topic_id;