/bp-forums/bbpress/bb-includes/functions.bb-posts.php

  1. <?php 
  2.  
  3. /** Posts */ 
  4.  
  5. /** 
  6. * Check to make sure that a user is not making too many posts in a short amount of time. 
  7. */ 
  8. function bb_check_post_flood() { 
  9. global $bbdb; 
  10. $user_id = (int) $user_id; 
  11. $throttle_time = bb_get_option( 'throttle_time' ); 
  12.  
  13. if ( bb_current_user_can( 'manage_options' ) || empty( $throttle_time ) ) 
  14. return; 
  15.  
  16. if ( bb_is_user_logged_in() ) { 
  17. $bb_current_user = bb_get_current_user(); 
  18.  
  19. if ( isset($bb_current_user->data->last_posted) && time() < $bb_current_user->data->last_posted + $throttle_time && ! bb_current_user_can( 'throttle' ) ) 
  20. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) 
  21. die( __( 'Slow down; you move too fast.' ) ); 
  22. else 
  23. bb_die( __( 'Slow down; you move too fast.' ) ); 
  24. } else { 
  25. if ( ( $last_posted = bb_get_transient($_SERVER['REMOTE_ADDR'] . '_last_posted') ) && time() < $last_posted + $throttle_time ) 
  26. if ( defined('DOING_AJAX') && DOING_AJAX ) 
  27. die( __( 'Slow down; you move too fast.' ) ); 
  28. else 
  29. bb_die( __( 'Slow down; you move too fast.' ) ); 
  30.  
  31. /** 
  32. * Get the current, non-logged-in poster data. 
  33. * @return array The associative array of author, email, and url data. 
  34. */ 
  35. function bb_get_current_poster() { 
  36. // Cookies should already be sanitized. 
  37. $post_author = ''; 
  38. if ( isset( $_COOKIE['post_author_' . BB_HASH] ) ) 
  39. $post_author = $_COOKIE['post_author_' . BB_HASH]; 
  40.  
  41. $post_author_email = ''; 
  42. if ( isset( $_COOKIE['post_author_email_' . BB_HASH] ) ) 
  43. $post_author_email = $_COOKIE['post_author_email_' . BB_HASH]; 
  44.  
  45. $post_author_url = ''; 
  46. if ( isset( $_COOKIE['post_author_url_' . BB_HASH] ) ) 
  47. $post_author_url = $_COOKIE['post_author_url_' . BB_HASH]; 
  48.  
  49. return compact( 'post_author', 'post_author_email', 'post_author_url' ); 
  50.  
  51. function bb_get_post( $post_id ) { 
  52. global $bbdb; 
  53. $post_id = (int) $post_id; 
  54. if ( false === $post = wp_cache_get( $post_id, 'bb_post' ) ) { 
  55. $post = $bbdb->get_row( $bbdb->prepare( "SELECT * FROM $bbdb->posts WHERE post_id = %d", $post_id ) ); 
  56. $post = bb_append_meta( $post, 'post' ); 
  57. wp_cache_set( $post_id, $post, 'bb_post' ); 
  58. return $post; 
  59.  
  60. // NOT bbdb::prepared 
  61. function bb_is_first( $post_id ) { // First post in thread 
  62. global $bbdb; 
  63. if ( !$bb_post = bb_get_post( $post_id ) ) 
  64. return false; 
  65. $post_id = (int) $bb_post->post_id; 
  66. $topic_id = (int) $bb_post->topic_id; 
  67.  
  68. static $first_post; 
  69. if ( !isset( $first_post ) ) { 
  70. $where = apply_filters('bb_is_first_where', 'AND post_status = 0'); 
  71. $first_post = (int) $bbdb->get_var("SELECT post_id FROM $bbdb->posts WHERE topic_id = $topic_id $where ORDER BY post_id ASC LIMIT 1"); 
  72.  
  73. return $post_id == $first_post; 
  74.  
  75. // Globalizes the result. 
  76. function bb_get_first_post( $_topic = false, $author_cache = true ) { 
  77. global $topic, $bb_first_post_cache, $bb_post; 
  78. if ( !$_topic ) 
  79. $topic_id = (int) $topic->topic_id; 
  80. else if ( is_object($_topic) ) 
  81. $topic_id = (int) $_topic->topic_id; 
  82. else if ( is_numeric($_topic) ) 
  83. $topic_id = (int) $_topic; 
  84.  
  85. if ( !$topic_id ) 
  86. return false; 
  87.  
  88. if ( isset($bb_first_post_cache[$topic_id]) ) { 
  89. $post = bb_get_post( $bb_first_post_cache[$topic_id] ); 
  90. } else { 
  91. $first_posts = bb_cache_first_posts( array($topic_id), $author_cache ); 
  92. if ( isset($first_posts[$topic_id]) ) 
  93. $post = $first_posts[$topic_id]; 
  94.  
  95. if ( $post ) { 
  96. $bb_post = $post; 
  97. return $bb_post; 
  98.  
  99. return false; 
  100.  
  101. // Ignore the return value. Cache first posts with this function and use bb_get_first_post to grab each. 
  102. // NOT bbdb::prepared 
  103. function bb_cache_first_posts( $_topics = false, $author_cache = true ) { 
  104. global $topics, $bb_first_post_cache, $bbdb; 
  105. if ( !$_topics ) 
  106. $_topics =& $topics; 
  107. if ( !is_array($_topics) ) 
  108. return false; 
  109.  
  110. $topic_ids = array(); 
  111. foreach ( $_topics as $topic ) 
  112. if ( is_object($topic) ) 
  113. $topic_ids[] = (int) $topic->topic_id; 
  114. else if ( is_numeric($topic) ) 
  115. $topic_ids[] = (int) $topic; 
  116.  
  117. $_topic_ids = join(', ', $topic_ids); 
  118.  
  119. $posts = (array) bb_cache_posts( "SELECT post_id FROM $bbdb->posts WHERE topic_id IN ($_topic_ids) AND post_position = 1", true ); 
  120.  
  121. $first_posts = array(); 
  122. foreach ( $posts as $post ) { 
  123. $bb_first_post_cache[(int) $post->topic_id] = (int) $post->post_id; 
  124. $first_posts[(int) $post->topic_id] = $post; 
  125.  
  126. if ( $author_cache ) 
  127. bb_post_author_cache( $posts ); 
  128.  
  129. return $first_posts; 
  130.  
  131. function bb_cache_posts( $query, $post_id_query = false ) { 
  132. global $bbdb; 
  133.  
  134. $_query = ''; 
  135. $_query_post_ids = array(); 
  136. $_query_posts = array(); 
  137. $_cached_posts = array(); 
  138. $ordered_post_ids = array(); 
  139.  
  140. if ( $post_id_query && is_string( $query ) ) { 
  141. // The query is a SQL query to retrieve post_ids only 
  142. $key = md5( $query ); 
  143. if ( false === $post_ids = wp_cache_get( $key, 'bb_cache_posts_post_ids' ) ) { 
  144. if ( !$post_ids = (array) $bbdb->get_col( $query, 0 ) ) { 
  145. return array(); 
  146. wp_cache_add( $key, $post_ids, 'bb_cache_posts_post_ids' ); 
  147. $query = $post_ids; 
  148.  
  149. if ( is_array( $query ) ) { 
  150. $get_order_from_query = false; 
  151.  
  152. foreach ( $query as $_post_id ) { 
  153. $ordered_post_ids[] = $_post_id; 
  154. if ( false === $_post = wp_cache_get( $_post_id, 'bb_post' ) ) { 
  155. $_query_post_ids[] = $_post_id; 
  156. } else { 
  157. $_cached_posts[$_post->post_id] = $_post; 
  158.  
  159. if ( count( $_query_post_ids ) ) { 
  160. // Escape the ids for the SQL query 
  161. $_query_post_ids = $bbdb->escape_deep( $_query_post_ids ); 
  162.  
  163. // Sort the ids so the MySQL will more consistently cache the query 
  164. sort( $_query_post_ids ); 
  165.  
  166. $_query = "SELECT * FROM $bbdb->posts WHERE post_id IN ('" . join( "', '", $_query_post_ids ) . "')"; 
  167. } else { 
  168. // The query is a full SQL query which needs to be executed 
  169. $get_order_from_query = true; 
  170. $_query = $query; 
  171.  
  172. if ( $_query_posts = (array) $bbdb->get_results( $_query ) ) { 
  173. $_appendable_posts = array(); 
  174. foreach ( $_query_posts as $_query_post ) { 
  175. if ( $get_order_from_query ) { 
  176. $ordered_post_ids[] = $_query_post->post_id; 
  177. if ( false === $_post = wp_cache_get( $_query_post->post_id, 'bb_post' ) ) { 
  178. $_appendable_posts[] = $_query_post; 
  179. } else { 
  180. $_cached_posts[$_query_post->post_id] = $_post; 
  181. if ( count( $_appendable_posts ) ) { 
  182. $_query_posts = bb_append_meta( $_appendable_posts, 'post' ); 
  183. foreach( $_query_posts as $_query_post ) { 
  184. wp_cache_add( $_query_post->post_id, $_query_post, 'bb_post' ); 
  185. } else { 
  186. $_query_posts = array(); 
  187. } else { 
  188. $_query_posts = array(); 
  189.  
  190. foreach ( array_merge( $_cached_posts, $_query_posts ) as $_post ) { 
  191. $keyed_posts[$_post->post_id] = $_post; 
  192.  
  193. $the_posts = array(); 
  194. foreach ( $ordered_post_ids as $ordered_post_id ) { 
  195. $the_posts[] = $keyed_posts[$ordered_post_id]; 
  196.  
  197. return $the_posts; 
  198.  
  199. // Globalizes the result 
  200. function bb_get_last_post( $_topic = false, $author_cache = true ) { 
  201. global $topic, $bb_post; 
  202. if ( !$_topic ) 
  203. $topic_id = (int) $topic->topic_id; 
  204. else if ( is_object($_topic) ) 
  205. $topic_id = (int) $_topic->topic_id; 
  206. else if ( is_numeric($_topic) ) 
  207. $topic_id = (int) $_topic; 
  208.  
  209. if ( !$topic_id ) 
  210. return false; 
  211.  
  212. $_topic = get_topic( $topic_id ); 
  213.  
  214. if ( $post = bb_get_post( $_topic->topic_last_post_id ) ) { 
  215. if ( $author_cache ) 
  216. bb_post_author_cache( array($post) ); 
  217. $bb_post = $post; 
  218.  
  219. return $post; 
  220.  
  221. // No return value. Cache last posts with this function and use bb_get_last_post to grab each. 
  222. // NOT bbdb::prepared 
  223. function bb_cache_last_posts( $_topics = false, $author_cache = true ) { 
  224. global $topics, $bbdb; 
  225. if ( !$_topics ) 
  226. $_topics =& $topics; 
  227. if ( !is_array($_topics) ) 
  228. return false; 
  229.  
  230. $last_post_ids = array(); 
  231. $topic_ids = array(); 
  232. foreach ( $_topics as $topic ) 
  233. if ( is_object($topic) ) 
  234. $last_post_ids[] = (int) $topic->topic_last_post_id; 
  235. else if ( is_numeric($topic) && false !== $cached_topic = wp_cache_get( $topic, 'bb_topic' ) ) 
  236. $last_post_ids[] = (int) $cached_topic->topic_last_post_id; 
  237. else if ( is_numeric($topic) ) 
  238. $topic_ids[] = (int) $topic; 
  239.  
  240. if ( !empty($last_post_ids) ) { 
  241. $_last_post_ids = join(', ', $last_post_ids); 
  242. $posts = (array) bb_cache_posts( "SELECT post_id FROM $bbdb->posts WHERE post_id IN ($_last_post_ids) AND post_status = 0", true ); 
  243. if ( $author_cache ) 
  244. bb_post_author_cache( $posts ); 
  245.  
  246. if ( !empty($topic_ids) ) {  
  247. $_topic_ids = join(', ', $topic_ids); 
  248. $posts = (array) bb_cache_posts( "SELECT p.post_id FROM $bbdb->topics AS t LEFT JOIN $bbdb->posts AS p ON ( t.topic_last_post_id = p.post_id ) WHERE t.topic_id IN ($_topic_ids) AND p.post_status = 0", true ); 
  249. if ( $author_cache ) 
  250. bb_post_author_cache( $posts ); 
  251.  
  252. // NOT bbdb::prepared 
  253. function bb_cache_post_topics( $posts ) { 
  254. global $bbdb; 
  255.  
  256. if ( !$posts ) 
  257. return; 
  258.  
  259. $topic_ids = array(); 
  260. foreach ( $posts as $post ) 
  261. if ( false === wp_cache_get( $post->topic_id, 'bb_topic' ) ) 
  262. $topic_ids[] = (int) $post->topic_id; 
  263.  
  264. if ( !$topic_ids ) 
  265. return; 
  266.  
  267. sort( $topic_ids ); 
  268. $topic_ids = join(', ', $topic_ids); 
  269.  
  270. if ( $topics = $bbdb->get_results( "SELECT * FROM $bbdb->topics WHERE topic_id IN($topic_ids)" ) ) 
  271. bb_append_meta( $topics, 'topic' ); 
  272.  
  273. function bb_get_latest_posts( $limit = 0, $page = 1 ) { 
  274. $limit = (int) $limit; 
  275. $post_query = new BB_Query( 'post', array( 'page' => $page, 'per_page' => $limit ), 'get_latest_posts' ); 
  276. return $post_query->results; 
  277.  
  278. function bb_get_latest_forum_posts( $forum_id, $limit = 0, $page = 1 ) { 
  279. $forum_id = (int) $forum_id; 
  280. $limit = (int) $limit; 
  281. $post_query = new BB_Query( 'post', array( 'forum_id' => $forum_id, 'page' => $page, 'per_page' => $limit ), 'get_latest_forum_posts' ); 
  282. return $post_query->results; 
  283.  
  284. function bb_insert_post( $args = null ) { 
  285. global $bbdb, $bb_current_user, $bb; 
  286.  
  287. if ( !$args = wp_parse_args( $args ) ) 
  288. return false; 
  289.  
  290. $fields = array_keys( $args ); 
  291.  
  292. if ( isset($args['post_id']) && false !== $args['post_id'] ) { 
  293. $update = true; 
  294. if ( !$post_id = (int) get_post_id( $args['post_id'] ) ) 
  295. return false; 
  296. // Get from db, not cache. Good idea? 
  297. $post = $bbdb->get_row( $bbdb->prepare( "SELECT * FROM $bbdb->posts WHERE post_id = %d", $post_id ) ); 
  298. $defaults = get_object_vars( $post ); 
  299. unset( $defaults['post_id'] ); 
  300.  
  301. // Only update the args we passed 
  302. $fields = array_intersect( $fields, array_keys($defaults) ); 
  303. if ( in_array( 'topic_id', $fields ) ) 
  304. $fields[] = 'forum_id'; 
  305.  
  306. // No need to run filters if these aren't changing 
  307. // bb_new_post() and bb_update_post() will always run filters 
  308. $run_filters = (bool) array_intersect( array( 'post_status', 'post_text' ), $fields ); 
  309. } else { 
  310. $post_id = false; 
  311. $update = false; 
  312. $now = bb_current_time( 'mysql' ); 
  313. $current_user_id = bb_get_current_user_info( 'id' ); 
  314. $ip_address = $_SERVER['REMOTE_ADDR']; 
  315.  
  316. $defaults = array( 
  317. 'topic_id' => 0,  
  318. 'post_text' => '',  
  319. 'post_time' => $now,  
  320. 'poster_id' => $current_user_id, // accepts ids or names 
  321. 'poster_ip' => $ip_address,  
  322. 'post_status' => 0, // use bb_delete_post() instead 
  323. 'post_position' => false 
  324. ); 
  325.  
  326. // Insert all args 
  327. $fields = array_keys($defaults); 
  328. $fields[] = 'forum_id'; 
  329.  
  330. $run_filters = true; 
  331.  
  332. $defaults['throttle'] = true; 
  333. extract( wp_parse_args( $args, $defaults ) ); 
  334.  
  335. // 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) 
  336.  
  337. if ( !$topic = get_topic( $topic_id ) ) 
  338. return false; 
  339.  
  340. if ( bb_is_login_required() && ! $user = bb_get_user( $poster_id ) ) 
  341. return false; 
  342.  
  343. $topic_id = (int) $topic->topic_id; 
  344. $forum_id = (int) $topic->forum_id; 
  345.  
  346. if ( $run_filters && !$post_text = apply_filters('pre_post', $post_text, $post_id, $topic_id) ) 
  347. return false; 
  348.  
  349. if ( $update ) // Don't change post_status with this function. Use bb_delete_post(). 
  350. $post_status = $post->post_status; 
  351.  
  352. if ( $run_filters ) 
  353. $post_status = (int) apply_filters('pre_post_status', $post_status, $post_id, $topic_id); 
  354.  
  355. if ( false === $post_position ) 
  356. $post_position = $topic_posts = intval( ( 0 == $post_status ) ? $topic->topic_posts + 1 : $topic->topic_posts ); 
  357.  
  358. unset($defaults['throttle']); 
  359.  
  360. if ( $update ) { 
  361. $bbdb->update( $bbdb->posts, compact( $fields ), compact( 'post_id' ) ); 
  362. wp_cache_delete( $post_id, 'bb_post' ); 
  363. } else { 
  364. $bbdb->insert( $bbdb->posts, compact( $fields ) ); 
  365. $post_id = $topic_last_post_id = (int) $bbdb->insert_id; 
  366.  
  367. if ( 0 == $post_status ) { 
  368. $topic_time = $post_time; 
  369. $topic_last_poster = ( ! bb_is_user_logged_in() && ! bb_is_login_required() ) ? -1 : $poster_id; 
  370. $topic_last_poster_name = ( ! bb_is_user_logged_in() && ! bb_is_login_required() ) ? $post_author : $user->user_login; 
  371.  
  372. $bbdb->query( $bbdb->prepare( "UPDATE $bbdb->forums SET posts = posts + 1 WHERE forum_id = %d;", $topic->forum_id ) ); 
  373. $bbdb->update( 
  374. $bbdb->topics,  
  375. compact( 'topic_time', 'topic_last_poster', 'topic_last_poster_name', 'topic_last_post_id', 'topic_posts' ),  
  376. compact ( 'topic_id' ) 
  377. ); 
  378.  
  379. $query = new BB_Query( 'post', array( 'post_author_id' => $poster_id, 'topic_id' => $topic_id, 'post_id' => "-$post_id" ) ); 
  380. if ( !$query->results ) { 
  381. $topics_replied_key = $bbdb->prefix . 'topics_replied'; 
  382. bb_update_usermeta( $poster_id, $topics_replied_key, $user->$topics_replied_key + 1 ); 
  383.  
  384. } else { 
  385. bb_update_topicmeta( $topic->topic_id, 'deleted_posts', isset($topic->deleted_posts) ? $topic->deleted_posts + 1 : 1 ); 
  386. bb_update_topic_voices( $topic_id ); 
  387.  
  388. // if user not logged in, save user data as meta data 
  389. if ( !$user ) { 
  390. bb_update_meta($post_id, 'post_author', $post_author, 'post'); 
  391. bb_update_meta($post_id, 'post_email', $post_email, 'post'); 
  392. bb_update_meta($post_id, 'post_url', $post_url, 'post'); 
  393.  
  394. if ( $throttle && !bb_current_user_can( 'throttle' ) ) { 
  395. if ( $user ) 
  396. bb_update_usermeta( $poster_id, 'last_posted', time() ); 
  397. else 
  398. bb_set_transient( $_SERVER['REMOTE_ADDR'] . '_last_posted', time() ); 
  399.  
  400. if ( !bb_is_login_required() && !$user = bb_get_user( $poster_id ) ) { 
  401. $post_cookie_lifetime = apply_filters( 'bb_post_cookie_lifetime', 30000000 ); 
  402. setcookie( 'post_author_' . BB_HASH, $post_author, time() + $post_cookie_lifetime, $bb->cookiepath, $bb->cookiedomain ); 
  403. setcookie( 'post_author_email_' . BB_HASH, $post_email, time() + $post_cookie_lifetime, $bb->cookiepath, $bb->cookiedomain ); 
  404. setcookie( 'post_author_url_' . BB_HASH, $post_url, time() + $post_cookie_lifetime, $bb->cookiepath, $bb->cookiedomain ); 
  405.  
  406. wp_cache_delete( $topic_id, 'bb_topic' ); 
  407. wp_cache_delete( $topic_id, 'bb_thread' ); 
  408. wp_cache_delete( $forum_id, 'bb_forum' ); 
  409. wp_cache_flush( 'bb_forums' ); 
  410. wp_cache_flush( 'bb_query' ); 
  411. wp_cache_flush( 'bb_cache_posts_post_ids' ); 
  412.  
  413. if ( $update ) // fire actions after cache is flushed 
  414. do_action( 'bb_update_post', $post_id ); 
  415. else 
  416. do_action( 'bb_new_post', $post_id ); 
  417.  
  418. do_action( 'bb_insert_post', $post_id, $args, compact( array_keys($args) ) ); // post_id, what was passed, what was used 
  419.  
  420. if (bb_get_option('enable_pingback')) { 
  421. bb_update_postmeta($post_id, 'pingback_queued', ''); 
  422. wp_schedule_single_event(time(), 'do_pingbacks'); 
  423.  
  424. return $post_id; 
  425.  
  426. // Deprecated: expects $post_text to be pre-escaped 
  427. function bb_new_post( $topic_id, $post_text ) { 
  428. $post_text = stripslashes( $post_text ); 
  429. return bb_insert_post( compact( 'topic_id', 'post_text' ) ); 
  430.  
  431. // Deprecated: expects $post_text to be pre-escaped 
  432. function bb_update_post( $post_text, $post_id, $topic_id ) { 
  433. $post_text = stripslashes( $post_text ); 
  434. return bb_insert_post( compact( 'post_text', 'post_id', 'topic_id' ) ); 
  435.  
  436. function bb_update_post_positions( $topic_id ) { 
  437. global $bbdb; 
  438. $topic_id = (int) $topic_id; 
  439. $posts = get_thread( $topic_id, array( 'per_page' => '-1' ) ); 
  440. if ( $posts ) { 
  441. foreach ( $posts as $i => $post ) { 
  442. $bbdb->query( $bbdb->prepare( "UPDATE $bbdb->posts SET post_position = %d WHERE post_id = %d", $i + 1, $post->post_id ) ); 
  443. wp_cache_delete( (int) $post->post_id, 'bb_post' ); 
  444. wp_cache_delete( $topic_id, 'bb_thread' ); 
  445. wp_cache_flush( 'bb_query' ); 
  446. wp_cache_flush( 'bb_cache_posts_post_ids' ); 
  447. return true; 
  448. } else { 
  449. return false; 
  450.  
  451. function bb_delete_post( $post_id, $new_status = 0 ) { 
  452. global $bbdb, $topic, $bb_post; 
  453. $post_id = (int) $post_id; 
  454. $bb_post = bb_get_post ( $post_id ); 
  455. $new_status = (int) $new_status; 
  456. $old_status = (int) $bb_post->post_status; 
  457. add_filter( 'get_topic_where', 'bb_no_where' ); 
  458. $topic = get_topic( $bb_post->topic_id ); 
  459. $topic_id = (int) $topic->topic_id; 
  460.  
  461. if ( $bb_post ) { 
  462. $uid = (int) $bb_post->poster_id; 
  463. if ( $new_status == $old_status ) 
  464. return; 
  465. _bb_delete_post( $post_id, $new_status ); 
  466. if ( 0 == $old_status ) { 
  467. bb_update_topicmeta( $topic_id, 'deleted_posts', $topic->deleted_posts + 1 ); 
  468. $bbdb->query( $bbdb->prepare( "UPDATE $bbdb->forums SET posts = posts - 1 WHERE forum_id = %d", $topic->forum_id ) ); 
  469. } else if ( 0 == $new_status ) { 
  470. bb_update_topicmeta( $topic_id, 'deleted_posts', $topic->deleted_posts - 1 ); 
  471. $bbdb->query( $bbdb->prepare( "UPDATE $bbdb->forums SET posts = posts + 1 WHERE forum_id = %d", $topic->forum_id ) ); 
  472. $posts = (int) $bbdb->get_var( $bbdb->prepare( "SELECT COUNT(*) FROM $bbdb->posts WHERE topic_id = %d AND post_status = 0", $topic_id ) ); 
  473. $bbdb->update( $bbdb->topics, array( 'topic_posts' => $posts ), compact( 'topic_id' ) ); 
  474.  
  475. if ( 0 == $posts ) { 
  476. if ( 0 == $topic->topic_status || 1 == $new_status ) 
  477. bb_delete_topic( $topic_id, $new_status ); 
  478. } else { 
  479. if ( 0 != $topic->topic_status ) { 
  480. $bbdb->update( $bbdb->topics, array( 'topic_status' => 0 ), compact( 'topic_id' ) ); 
  481. $bbdb->query( $bbdb->prepare( "UPDATE $bbdb->forums SET topics = topics + 1 WHERE forum_id = %d", $topic->forum_id ) ); 
  482. bb_topic_set_last_post( $topic_id ); 
  483. bb_update_post_positions( $topic_id ); 
  484. bb_update_topic_voices( $topic_id ); 
  485.  
  486. $user = bb_get_user( $uid ); 
  487.  
  488. $user_posts = new BB_Query( 'post', array( 'post_author_id' => $user->ID, 'topic_id' => $topic_id ) ); 
  489. if ( $new_status && !$user_posts->results ) { 
  490. $topics_replied_key = $bbdb->prefix . 'topics_replied'; 
  491. bb_update_usermeta( $user->ID, $topics_replied_key, $user->$topics_replied_key - 1 ); 
  492. wp_cache_delete( $topic_id, 'bb_topic' ); 
  493. wp_cache_delete( $topic_id, 'bb_thread' ); 
  494. wp_cache_flush( 'bb_forums' ); 
  495. wp_cache_flush( 'bb_query' ); 
  496. wp_cache_flush( 'bb_cache_posts_post_ids' ); 
  497. do_action( 'bb_delete_post', $post_id, $new_status, $old_status ); 
  498. return $post_id; 
  499. } else { 
  500. return false; 
  501.  
  502. function _bb_delete_post( $post_id, $post_status ) { 
  503. global $bbdb; 
  504. $post_id = (int) $post_id; 
  505. $post_status = (int) $post_status; 
  506. $bbdb->update( $bbdb->posts, compact( 'post_status' ), compact( 'post_id' ) ); 
  507. wp_cache_delete( $post_id, 'bb_post' ); 
  508. do_action( '_bb_delete_post', $post_id, $post_status ); 
  509.  
  510. function bb_topics_replied_on_undelete_post( $post_id ) { 
  511. global $bbdb; 
  512. $bb_post = bb_get_post( $post_id ); 
  513. $topic = get_topic( $bb_post->topic_id ); 
  514.  
  515. $user_posts = new BB_Query( 'post', array( 'post_author_id' => $bb_post->poster_id, 'topic_id' => $topic->topic_id ) ); 
  516.  
  517. if ( 1 == count($user_posts) && $user = bb_get_user( $bb_post->poster_id ) ) { 
  518. $topics_replied_key = $bbdb->prefix . 'topics_replied'; 
  519. bb_update_usermeta( $user->ID, $topics_replied_key, $user->$topics_replied_key + 1 ); 
  520.  
  521. function bb_post_author_cache($posts) { 
  522. if ( !$posts ) 
  523. return; 
  524.  
  525. $ids = array(); 
  526. foreach ($posts as $bb_post) 
  527. $ids[] = $bb_post->poster_id; 
  528.  
  529. if ( $ids ) 
  530. bb_cache_users(array_unique($ids)); 
  531.  
  532. // These two filters are lame. It'd be nice if we could do this in the query parameters 
  533. function bb_get_recent_user_replies_fields( $fields ) { 
  534. return 'MAX( p.post_id ) AS post_id'; 
  535.  
  536. function bb_get_recent_user_replies_group_by() { 
  537. return 'p.topic_id'; 
  538.  
  539. function bb_get_recent_user_replies( $user_id ) { 
  540. global $bbdb; 
  541. $user_id = (int) $user_id; 
  542.  
  543. $post_query = new BB_Query( 
  544. 'post',  
  545. array( 
  546. 'post_author_id' => $user_id,  
  547. 'order_by' => 'post_id',  
  548. 'post_id_only' => true,  
  549. ),  
  550. 'get_recent_user_replies' 
  551. ); 
  552.  
  553. return $post_query->results; 
  554.  
  555. /** 
  556. * Sends notification emails for new posts. 
  557. * 
  558. * Gets new post's ID and check if there are subscribed 
  559. * user to that topic, and if there are, send notifications 
  560. * 
  561. * @since 1.1 
  562. * 
  563. * @param int $post_id ID of new post 
  564. */ 
  565. function bb_notify_subscribers( $post_id ) { 
  566. global $bbdb; 
  567.  
  568. if ( !$post = bb_get_post( $post_id ) ) 
  569. return false; 
  570.  
  571. // bozo or spam 
  572. if ( 2 == $post->post_status ) 
  573. return false; 
  574.  
  575. if ( !$topic = get_topic( $post->topic_id ) ) 
  576. return false; 
  577.  
  578. $post_id = $post->post_id; 
  579. $topic_id = $topic->topic_id; 
  580.  
  581. if ( !$poster_name = get_post_author( $post_id ) ) 
  582. return false; 
  583.  
  584. do_action( 'bb_pre_notify_subscribers', $post_id, $topic_id ); 
  585.  
  586. if ( !$user_ids = $bbdb->get_col( $bbdb->prepare( "SELECT `$bbdb->term_relationships`.`object_id` 
  587. FROM $bbdb->term_relationships, $bbdb->term_taxonomy, $bbdb->terms 
  588. WHERE `$bbdb->term_relationships`.`term_taxonomy_id` = `$bbdb->term_taxonomy`.`term_taxonomy_id` 
  589. AND `$bbdb->term_taxonomy`.`term_id` = `$bbdb->terms`.`term_id` 
  590. AND `$bbdb->term_taxonomy`.`taxonomy` = 'bb_subscribe' 
  591. AND `$bbdb->terms`.`slug` = 'topic-%d'",  
  592. $topic_id ) ) ) 
  593. return false; 
  594.  
  595. foreach ( (array) $user_ids as $user_id ) { 
  596. if ( $user_id == $post->poster_id ) 
  597. continue; // don't send notifications to the person who made the post 
  598.  
  599. $user = bb_get_user( $user_id ); 
  600.  
  601. if ( !$message = apply_filters( 'bb_subscription_mail_message', __( "%1\$s wrote:\n\n%2\$s\n\nRead this post on the forums: %3\$s\n\nYou're getting this email because you subscribed to '%4\$s.'\nPlease click the link above, login, and click 'Unsubscribe' at the top of the page to stop receiving emails from this topic." ), $post_id, $topic_id ) ) 
  602. continue; /** For plugins */ 
  603.  
  604. bb_mail( 
  605. $user->user_email,  
  606. apply_filters( 'bb_subscription_mail_title', '[' . bb_get_option( 'name' ) . '] ' . $topic->topic_title, $post_id, $topic_id ),  
  607. sprintf( $message, $poster_name, strip_tags( $post->post_text ), get_post_link( $post_id ), strip_tags( $topic->topic_title ) ) 
  608. ); 
  609.  
  610. do_action( 'bb_post_notify_subscribers', $post_id, $topic_id ); 
  611.  
  612. /** 
  613. * Updates user's subscription status in database. 
  614. * 
  615. * Gets user's new subscription status for topic and 
  616. * adds new status to database. 
  617. * 
  618. * @since 1.1 
  619. * 
  620. * @param int $topic_id ID of topic for subscription 
  621. * @param string $new_status New subscription status 
  622. * @param int $user_id Optional. ID of user for subscription 
  623. */ 
  624. function bb_subscription_management( $topic_id, $new_status, $user_id = '' ) { 
  625. global $bbdb, $wp_taxonomy_object; 
  626.  
  627. $topic = get_topic( $topic_id ); 
  628. if (!$user_id) { 
  629. $user_id = bb_get_current_user_info( 'id' ); 
  630.  
  631. do_action( 'bb_subscripton_management', $topic_id, $new_status, $user_id ); 
  632.  
  633. switch ( $new_status ) { 
  634. case 'add': 
  635. $tt_ids = $wp_taxonomy_object->set_object_terms( $user_id, 'topic-' . $topic->topic_id, 'bb_subscribe', array( 'append' => true, 'user_id' => $user_id ) ); 
  636. break; 
  637. case 'remove': 
  638. // I hate this with the passion of a thousand suns 
  639. $term_id = $bbdb->get_var( "SELECT term_id FROM $bbdb->terms WHERE slug = 'topic-$topic->topic_id'" ); 
  640. $term_taxonomy_id = $bbdb->get_var( "SELECT term_taxonomy_id FROM $bbdb->term_taxonomy WHERE term_id = $term_id AND taxonomy = 'bb_subscribe'" ); 
  641. $bbdb->query( "DELETE FROM $bbdb->term_relationships WHERE object_id = $user_id AND term_taxonomy_id = $term_taxonomy_id" ); 
  642. $bbdb->query( "DELETE FROM $bbdb->term_taxonomy WHERE term_id = $term_id AND taxonomy = 'bb_subscribe'" ); 
  643. break; 
  644.  
  645.  
  646. /** 
  647. * Process subscription checkbox submission. 
  648. * 
  649. * Get ID of and new subscription status and pass values to 
  650. * bb_user_subscribe_checkbox_update function 
  651. * 
  652. * @since 1.1 
  653. * 
  654. * @param int $post_id ID of new/edited post 
  655. */ 
  656. function bb_user_subscribe_checkbox_update( $post_id ) { 
  657. if ( !bb_is_user_logged_in() ) 
  658. return false; 
  659.  
  660. $post = bb_get_post( $post_id ); 
  661. $topic_id = (int) $post->topic_id; 
  662. $subscribed = bb_is_user_subscribed( array( 'topic_id' => $topic_id, 'user_id' => $post->poster_id ) ) ? true : false; 
  663. $check = $_REQUEST['subscription_checkbox']; 
  664.  
  665. do_action( 'bb_user_subscribe_checkbox_update', $post_id, $topic_id, $subscribe, $check ); 
  666.  
  667. if ( 'subscribe' == $check && !$subscribed ) 
  668. bb_subscription_management( $topic_id, 'add' ); 
  669. elseif ( !$check && $subscribed ) 
  670. bb_subscription_management( $topic_id, 'remove' ); 
  671.  
.