bb_insert_post

The BuddyPress bb insert post function.

Description

bb_insert_post( (null) $args = null ); 

Parameters (1)

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

Usage

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

Defined (1)

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

/bp-forums/bbpress/bb-includes/functions.bb-posts.php  
  1. function bb_insert_post( $args = null ) { 
  2. global $bbdb, $bb_current_user, $bb; 
  3.  
  4. if ( !$args = wp_parse_args( $args ) ) 
  5. return false; 
  6.  
  7. $fields = array_keys( $args ); 
  8.  
  9. if ( isset($args['post_id']) && false !== $args['post_id'] ) { 
  10. $update = true; 
  11. if ( !$post_id = (int) get_post_id( $args['post_id'] ) ) 
  12. return false; 
  13. // Get from db, not cache. Good idea? 
  14. $post = $bbdb->get_row( $bbdb->prepare( "SELECT * FROM $bbdb->posts WHERE post_id = %d", $post_id ) ); 
  15. $defaults = get_object_vars( $post ); 
  16. unset( $defaults['post_id'] ); 
  17.  
  18. // Only update the args we passed 
  19. $fields = array_intersect( $fields, array_keys($defaults) ); 
  20. if ( in_array( 'topic_id', $fields ) ) 
  21. $fields[] = 'forum_id'; 
  22.  
  23. // No need to run filters if these aren't changing 
  24. // bb_new_post() and bb_update_post() will always run filters 
  25. $run_filters = (bool) array_intersect( array( 'post_status', 'post_text' ), $fields ); 
  26. } else { 
  27. $post_id = false; 
  28. $update = false; 
  29. $now = bb_current_time( 'mysql' ); 
  30. $current_user_id = bb_get_current_user_info( 'id' ); 
  31. $ip_address = $_SERVER['REMOTE_ADDR']; 
  32.  
  33. $defaults = array( 
  34. 'topic_id' => 0,  
  35. 'post_text' => '',  
  36. 'post_time' => $now,  
  37. 'poster_id' => $current_user_id, // accepts ids or names 
  38. 'poster_ip' => $ip_address,  
  39. 'post_status' => 0, // use bb_delete_post() instead 
  40. 'post_position' => false 
  41. ); 
  42.  
  43. // Insert all args 
  44. $fields = array_keys($defaults); 
  45. $fields[] = 'forum_id'; 
  46.  
  47. $run_filters = true; 
  48.  
  49. $defaults['throttle'] = true; 
  50. extract( wp_parse_args( $args, $defaults ) ); 
  51.  
  52. // If the user is not logged in and loginless posting is ON, then this function expects $post_author, $post_email and $post_url to be sanitized (check bb-post.php for example) 
  53.  
  54. if ( !$topic = get_topic( $topic_id ) ) 
  55. return false; 
  56.  
  57. if ( bb_is_login_required() && ! $user = bb_get_user( $poster_id ) ) 
  58. return false; 
  59.  
  60. $topic_id = (int) $topic->topic_id; 
  61. $forum_id = (int) $topic->forum_id; 
  62.  
  63. if ( $run_filters && !$post_text = apply_filters('pre_post', $post_text, $post_id, $topic_id) ) 
  64. return false; 
  65.  
  66. if ( $update ) // Don't change post_status with this function. Use bb_delete_post(). 
  67. $post_status = $post->post_status; 
  68.  
  69. if ( $run_filters ) 
  70. $post_status = (int) apply_filters('pre_post_status', $post_status, $post_id, $topic_id); 
  71.  
  72. if ( false === $post_position ) 
  73. $post_position = $topic_posts = intval( ( 0 == $post_status ) ? $topic->topic_posts + 1 : $topic->topic_posts ); 
  74.  
  75. unset($defaults['throttle']); 
  76.  
  77. if ( $update ) { 
  78. $bbdb->update( $bbdb->posts, compact( $fields ), compact( 'post_id' ) ); 
  79. wp_cache_delete( $post_id, 'bb_post' ); 
  80. } else { 
  81. $bbdb->insert( $bbdb->posts, compact( $fields ) ); 
  82. $post_id = $topic_last_post_id = (int) $bbdb->insert_id; 
  83.  
  84. if ( 0 == $post_status ) { 
  85. $topic_time = $post_time; 
  86. $topic_last_poster = ( ! bb_is_user_logged_in() && ! bb_is_login_required() ) ? -1 : $poster_id; 
  87. $topic_last_poster_name = ( ! bb_is_user_logged_in() && ! bb_is_login_required() ) ? $post_author : $user->user_login; 
  88.  
  89. $bbdb->query( $bbdb->prepare( "UPDATE $bbdb->forums SET posts = posts + 1 WHERE forum_id = %d;", $topic->forum_id ) ); 
  90. $bbdb->update( 
  91. $bbdb->topics,  
  92. compact( 'topic_time', 'topic_last_poster', 'topic_last_poster_name', 'topic_last_post_id', 'topic_posts' ),  
  93. compact ( 'topic_id' ) 
  94. ); 
  95.  
  96. $query = new BB_Query( 'post', array( 'post_author_id' => $poster_id, 'topic_id' => $topic_id, 'post_id' => "-$post_id" ) ); 
  97. if ( !$query->results ) { 
  98. $topics_replied_key = $bbdb->prefix . 'topics_replied'; 
  99. bb_update_usermeta( $poster_id, $topics_replied_key, $user->$topics_replied_key + 1 ); 
  100.  
  101. } else { 
  102. bb_update_topicmeta( $topic->topic_id, 'deleted_posts', isset($topic->deleted_posts) ? $topic->deleted_posts + 1 : 1 ); 
  103. bb_update_topic_voices( $topic_id ); 
  104.  
  105. // if user not logged in, save user data as meta data 
  106. if ( !$user ) { 
  107. bb_update_meta($post_id, 'post_author', $post_author, 'post'); 
  108. bb_update_meta($post_id, 'post_email', $post_email, 'post'); 
  109. bb_update_meta($post_id, 'post_url', $post_url, 'post'); 
  110.  
  111. if ( $throttle && !bb_current_user_can( 'throttle' ) ) { 
  112. if ( $user ) 
  113. bb_update_usermeta( $poster_id, 'last_posted', time() ); 
  114. else 
  115. bb_set_transient( $_SERVER['REMOTE_ADDR'] . '_last_posted', time() ); 
  116.  
  117. if ( !bb_is_login_required() && !$user = bb_get_user( $poster_id ) ) { 
  118. $post_cookie_lifetime = apply_filters( 'bb_post_cookie_lifetime', 30000000 ); 
  119. setcookie( 'post_author_' . BB_HASH, $post_author, time() + $post_cookie_lifetime, $bb->cookiepath, $bb->cookiedomain ); 
  120. setcookie( 'post_author_email_' . BB_HASH, $post_email, time() + $post_cookie_lifetime, $bb->cookiepath, $bb->cookiedomain ); 
  121. setcookie( 'post_author_url_' . BB_HASH, $post_url, time() + $post_cookie_lifetime, $bb->cookiepath, $bb->cookiedomain ); 
  122.  
  123. wp_cache_delete( $topic_id, 'bb_topic' ); 
  124. wp_cache_delete( $topic_id, 'bb_thread' ); 
  125. wp_cache_delete( $forum_id, 'bb_forum' ); 
  126. wp_cache_flush( bb_query ); 
  127. wp_cache_flush( 'bb_cache_posts_post_ids' ); 
  128.  
  129. if ( $update ) // fire actions after cache is flushed 
  130. do_action( 'bb_update_post', $post_id ); 
  131. else 
  132. do_action( 'bb_new_post', $post_id ); 
  133.  
  134. do_action( 'bb_insert_post', $post_id, $args, compact( array_keys($args) ) ); // post_id, what was passed, what was used 
  135.  
  136. if (bb_get_option('enable_pingback')) { 
  137. bb_update_postmeta($post_id, 'pingback_queued', ''); 
  138. wp_schedule_single_event(time(), 'do_pingbacks'); 
  139.  
  140. return $post_id;