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

  1. <?php 
  2.  
  3. /** Topics */ 
  4.  
  5. function get_topic( $id, $cache = true ) { 
  6. global $bbdb; 
  7.  
  8. if ( !is_numeric($id) ) { 
  9. list($slug, $sql) = bb_get_sql_from_slug( 'topic', $id ); 
  10. $id = wp_cache_get( $slug, 'bb_topic_slug' ); 
  11.  
  12. // not else 
  13. if ( is_numeric($id) ) { 
  14. $id = (int) $id; 
  15. $sql = "topic_id = $id"; 
  16.  
  17. if ( 0 === $id || !$sql ) 
  18. return false; 
  19.  
  20. // &= not =& 
  21. $cache &= 'AND topic_status = 0' == $where = apply_filters( 'get_topic_where', 'AND topic_status = 0' ); 
  22.  
  23. if ( ( $cache || !$where ) && is_numeric($id) && false !== $topic = wp_cache_get( $id, 'bb_topic' ) ) 
  24. return $topic; 
  25.  
  26. // $where is NOT bbdb:prepared 
  27. $topic = $bbdb->get_row( "SELECT * FROM $bbdb->topics WHERE $sql $where" ); 
  28. $topic = bb_append_meta( $topic, 'topic' ); 
  29.  
  30. if ( $cache ) { 
  31. wp_cache_set( $topic->topic_id, $topic, 'bb_topic' ); 
  32. wp_cache_add( $topic->topic_slug, $topic->topic_id, 'bb_topic_slug' ); 
  33.  
  34. return $topic; 
  35.  
  36. function bb_get_topic_from_uri( $uri ) { 
  37. // Extract the topic id or slug of the uri 
  38. if ( 'topic' === bb_get_path(0, false, $uri) ) { 
  39. $topic_id_or_slug = bb_get_path(1, false, $uri); 
  40. } else { 
  41. if ($parsed_uri = parse_url($uri)) { 
  42. if (preg_match('@/topic\.php$@' , $parsed_uri['path'])) { 
  43. $args = wp_parse_args($parsed_uri['query']); 
  44. if (isset($args['id'])) { 
  45. $topic_id_or_slug = $args['id']; 
  46.  
  47. if ( !$topic_id_or_slug ) 
  48. return false; 
  49.  
  50. return get_topic( $topic_id_or_slug ); 
  51.  
  52. function get_latest_topics( $args = null ) { 
  53. $_args = func_get_args(); 
  54. $defaults = array( 'forum' => false, 'page' => 1, 'exclude' => false, 'number' => false ); 
  55. if ( is_numeric( $args ) ) 
  56. $args = array( 'forum' => $args ); 
  57. else 
  58. $args = wp_parse_args( $args ); // Make sure it's an array 
  59. if ( 1 < func_num_args() ) 
  60. $args['page'] = $_args[1]; 
  61. if ( 2 < func_num_args() ) 
  62. $args['exclude'] = $_args[2]; 
  63.  
  64. $args = wp_parse_args( $args, $defaults ); 
  65. extract( $args, EXTR_SKIP ); 
  66.  
  67. if ( $exclude ) { 
  68. $exclude = '-' . str_replace(', ', ', -', $exclude); 
  69. $exclude = str_replace('--', '-', $exclude); 
  70. if ( $forum ) 
  71. $forum = (string) $forum . ", $exclude"; 
  72. else 
  73. $forum = $exclude; 
  74.  
  75. $q = array( 
  76. 'forum_id' => $forum,  
  77. 'page' => $page,  
  78. 'per_page' => $number,  
  79. 'index_hint' => 'USE INDEX (`forum_time`)' 
  80. ); 
  81.  
  82. if ( bb_is_front() ) 
  83. $q['sticky'] = '-2'; 
  84. elseif ( bb_is_forum() || bb_is_view() ) 
  85. $q['sticky'] = 0; 
  86.  
  87. // Last param makes filters back compat 
  88. $query = new BB_Query( 'topic', $q, 'get_latest_topics' ); 
  89. return $query->results; 
  90.  
  91. function get_sticky_topics( $forum = false, $display = 1 ) { 
  92. if ( 1 != $display ) // Why is this even here? 
  93. return false; 
  94.  
  95. $q = array( 
  96. 'forum_id' => $forum,  
  97. 'sticky' => bb_is_front() ? 'super' : 'sticky' 
  98. ); 
  99.  
  100. $query = new BB_Query( 'topic', $q, 'get_sticky_topics' ); 
  101. return $query->results; 
  102.  
  103. function get_recent_user_threads( $user_id ) { 
  104. global $page; 
  105. $q = array( 'page' => $page, 'topic_author_id' => $user_id, 'order_by' => 't.topic_time'); 
  106.  
  107. $query = new BB_Query( 'topic', $q, 'get_recent_user_threads' ); 
  108.  
  109. return $query->results; 
  110.  
  111. function bb_insert_topic( $args = null ) { 
  112. global $bbdb; 
  113.  
  114. if ( !$args = wp_parse_args( $args ) ) 
  115. return false; 
  116.  
  117. $fields = array_keys( $args ); 
  118.  
  119. if ( isset($args['topic_id']) && false !== $args['topic_id'] ) { 
  120. $update = true; 
  121. if ( !$topic_id = (int) get_topic_id( $args['topic_id'] ) ) 
  122. return false; 
  123. // Get from db, not cache. Good idea? Prevents trying to update meta_key names in the topic table (get_topic() returns appended topic obj) 
  124. $topic = $bbdb->get_row( $bbdb->prepare( "SELECT * FROM $bbdb->topics WHERE topic_id = %d", $topic_id ) ); 
  125. $defaults = get_object_vars( $topic ); 
  126. unset($defaults['topic_id']); 
  127.  
  128. // Only update the args we passed 
  129. $fields = array_intersect( $fields, array_keys($defaults) ); 
  130. if ( in_array( 'topic_poster', $fields ) ) 
  131. $fields[] = 'topic_poster_name'; 
  132. if ( in_array( 'topic_last_poster', $fields ) ) 
  133. $fields[] = 'topic_last_poster_name'; 
  134. } else { 
  135. $topic_id = false; 
  136. $update = false; 
  137.  
  138. $now = bb_current_time('mysql'); 
  139. $current_user_id = bb_get_current_user_info( 'id' ); 
  140.  
  141. $defaults = array( 
  142. 'topic_title' => '',  
  143. 'topic_slug' => '',  
  144. 'topic_poster' => $current_user_id, // accepts ids 
  145. 'topic_poster_name' => '', // accept names 
  146. 'topic_last_poster' => $current_user_id, // accepts ids 
  147. 'topic_last_poster_name' => '', // accept names 
  148. 'topic_start_time' => $now,  
  149. 'topic_time' => $now,  
  150. 'topic_open' => 1,  
  151. 'forum_id' => 0 // accepts ids or slugs 
  152. ); 
  153.  
  154. // Insert all args 
  155. $fields = array_keys($defaults); 
  156.  
  157. $defaults['tags'] = false; // accepts array or comma delimited string 
  158. extract( wp_parse_args( $args, $defaults ) ); 
  159. unset($defaults['tags']); 
  160.  
  161. if ( !$forum = bb_get_forum( $forum_id ) ) 
  162. return false; 
  163. $forum_id = (int) $forum->forum_id; 
  164.  
  165. if ( !$user = bb_get_user( $topic_poster ) ) 
  166. $user = bb_get_user( $topic_poster_name, array( 'by' => 'login' ) ); 
  167.  
  168. if ( !empty( $user ) ) { 
  169. $topic_poster = $user->ID; 
  170. $topic_poster_name = $user->user_login; 
  171.  
  172. if ( !$last_user = bb_get_user( $topic_last_poster ) ) 
  173. $last_user = bb_get_user( $topic_last_poster_name, array( 'by' => 'login' ) ); 
  174.  
  175. if ( !empty( $last_user ) ) { 
  176. $topic_last_poster = $last_user->ID; 
  177. $topic_last_poster_name = $last_user->user_login; 
  178.  
  179. if ( in_array( 'topic_title', $fields ) ) { 
  180. $topic_title = apply_filters( 'pre_topic_title', $topic_title, $topic_id ); 
  181. if ( strlen($topic_title) < 1 ) 
  182. return false; 
  183.  
  184. if ( in_array( 'topic_slug', $fields ) ) { 
  185. $slug_sql = $update ? 
  186. "SELECT topic_slug FROM $bbdb->topics WHERE topic_slug = %s AND topic_id != %d" : 
  187. "SELECT topic_slug FROM $bbdb->topics WHERE topic_slug = %s"; 
  188.  
  189. $topic_slug = $_topic_slug = bb_slug_sanitize( $topic_slug ? $topic_slug : wp_specialchars_decode( $topic_title, ENT_QUOTES ) ); 
  190. if ( strlen( $_topic_slug ) < 1 ) 
  191. $topic_slug = $_topic_slug = '0'; 
  192.  
  193. while ( is_numeric($topic_slug) || $existing_slug = $bbdb->get_var( $bbdb->prepare( $slug_sql, $topic_slug, $topic_id ) ) ) 
  194. $topic_slug = bb_slug_increment( $_topic_slug, $existing_slug ); 
  195.  
  196. if ( $update ) { 
  197. $bbdb->update( $bbdb->topics, compact( $fields ), compact( 'topic_id' ) ); 
  198. wp_cache_delete( $topic_id, 'bb_topic' ); 
  199. if ( in_array( 'topic_slug', $fields ) ) 
  200. wp_cache_delete( $topic->topic_slug, 'bb_topic_slug' ); 
  201. wp_cache_flush( 'bb_query' ); 
  202. wp_cache_flush( 'bb_cache_posts_post_ids' ); 
  203. do_action( 'bb_update_topic', $topic_id ); 
  204. } else { 
  205. $bbdb->insert( $bbdb->topics, compact( $fields ) ); 
  206. $topic_id = $bbdb->insert_id; 
  207. $bbdb->query( $bbdb->prepare( "UPDATE $bbdb->forums SET topics = topics + 1 WHERE forum_id = %d", $forum_id ) ); 
  208. wp_cache_delete( $forum_id, 'bb_forum' ); 
  209. wp_cache_flush( 'bb_forums' ); 
  210. wp_cache_flush( 'bb_query' ); 
  211. wp_cache_flush( 'bb_cache_posts_post_ids' ); 
  212. do_action( 'bb_new_topic', $topic_id ); 
  213.  
  214. if ( !empty( $tags ) ) 
  215. bb_add_topic_tags( $topic_id, $tags ); 
  216.  
  217. do_action( 'bb_insert_topic', $topic_id, $args, compact( array_keys($args) ) ); // topic_id, what was passed, what was used 
  218.  
  219. return $topic_id; 
  220.  
  221. // Deprecated: expects $title to be pre-escaped 
  222. function bb_new_topic( $title, $forum, $tags = '', $args = '' ) { 
  223. $title = stripslashes( $title ); 
  224. $tags = stripslashes( $tags ); 
  225. $forum = (int) $forum; 
  226. return bb_insert_topic( wp_parse_args( $args ) + array( 'topic_title' => $title, 'forum_id' => $forum, 'tags' => $tags ) ); 
  227.  
  228. // Deprecated: expects $title to be pre-escaped 
  229. function bb_update_topic( $title, $topic_id ) { 
  230. $title = stripslashes( $title ); 
  231. return bb_insert_topic( array( 'topic_title' => $title, 'topic_id' => $topic_id ) ); 
  232.  
  233. function bb_delete_topic( $topic_id, $new_status = 0 ) { 
  234. global $bbdb; 
  235. $topic_id = (int) $topic_id; 
  236. add_filter( 'get_topic_where', 'bb_no_where' ); 
  237. if ( $topic = get_topic( $topic_id ) ) { 
  238. $new_status = (int) $new_status; 
  239. $old_status = (int) $topic->topic_status; 
  240. if ( $new_status == $old_status ) 
  241. return; 
  242.  
  243. $thread_args = array( 'per_page' => -1, 'order' => 'DESC' ); 
  244. if ( 0 != $old_status && 0 == $new_status ) 
  245. $thread_args['post_status'] = 'all'; 
  246. $poster_ids = array(); 
  247. $posts = get_thread( $topic_id, $thread_args ); 
  248. if ( $posts && count( $posts ) ) { 
  249. foreach ( $posts as $post ) { 
  250. _bb_delete_post( $post->post_id, $new_status ); 
  251. $poster_ids[] = $post->poster_id; 
  252.  
  253. if ( count( $poster_ids ) ) { 
  254. foreach ( array_unique( $poster_ids ) as $id ) { 
  255. if ( $user = bb_get_user( $id ) ) { 
  256. $topics_replied_key = $bbdb->prefix . 'topics_replied'; 
  257. bb_update_usermeta( $user->ID, $topics_replied_key, ( $old_status ? $user->$topics_replied_key + 1 : $user->$topics_replied_key - 1 ) ); 
  258.  
  259. 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" ) ) 
  260. foreach ( $ids as $id ) 
  261. bb_remove_user_favorite( $id, $topic_id ); 
  262.  
  263. switch ( $new_status ) { 
  264. case 0: // Undeleting 
  265. $bbdb->update( $bbdb->topics, array( 'topic_status' => $new_status ), compact( 'topic_id' ) ); 
  266. $topic_posts = (int) $bbdb->get_var( $bbdb->prepare( 
  267. "SELECT COUNT(*) FROM $bbdb->posts WHERE topic_id = %d AND post_status = 0", $topic_id 
  268. ) ); 
  269. $all_posts = (int) $bbdb->get_var( $bbdb->prepare( 
  270. "SELECT COUNT(*) FROM $bbdb->posts WHERE topic_id = %d", $topic_id 
  271. ) ); 
  272. bb_update_topicmeta( $topic_id, 'deleted_posts', $all_posts - $topic_posts ); 
  273. $bbdb->query( $bbdb->prepare( 
  274. "UPDATE $bbdb->forums SET topics = topics + 1, posts = posts + %d WHERE forum_id = %d", $topic_posts, $topic->forum_id 
  275. ) ); 
  276. $bbdb->update( $bbdb->topics, compact( 'topic_posts' ), compact( 'topic_id' ) ); 
  277. bb_topic_set_last_post( $topic_id ); 
  278. bb_update_post_positions( $topic_id ); 
  279. break; 
  280.  
  281. default: // Other statuses (like Delete and Bozo) 
  282. bb_remove_topic_tags( $topic_id ); 
  283. $bbdb->update( $bbdb->topics, array( 'topic_status' => $new_status, 'tag_count' => 0 ), compact( 'topic_id' ) ); 
  284. $bbdb->query( $bbdb->prepare( 
  285. "UPDATE $bbdb->forums SET topics = topics - 1, posts = posts - %d WHERE forum_id = %d", $topic->topic_posts, $topic->forum_id 
  286. ) ); 
  287. break; 
  288.  
  289. do_action( 'bb_delete_topic', $topic_id, $new_status, $old_status ); 
  290. wp_cache_delete( $topic_id, 'bb_topic' ); 
  291. wp_cache_delete( $topic->topic_slug, 'bb_topic_slug' ); 
  292. wp_cache_delete( $topic_id, 'bb_thread' ); 
  293. wp_cache_delete( $topic->forum_id, 'bb_forum' ); 
  294. wp_cache_flush( 'bb_forums' ); 
  295. wp_cache_flush( 'bb_query' ); 
  296. wp_cache_flush( 'bb_cache_posts_post_ids' ); 
  297. return $topic_id; 
  298. } else { 
  299. return false; 
  300.  
  301. function bb_move_topic( $topic_id, $forum_id ) { 
  302. global $bbdb; 
  303. $topic = get_topic( $topic_id ); 
  304. $forum = bb_get_forum( $forum_id ); 
  305. $topic_id = (int) $topic->topic_id; 
  306. $forum_id = (int) $forum->forum_id; 
  307.  
  308. if ( $topic && $forum && $topic->forum_id != $forum_id ) { 
  309. $bbdb->update( $bbdb->posts, compact( 'forum_id' ), compact( 'topic_id' ) ); 
  310. $bbdb->update( $bbdb->topics, compact( 'forum_id' ), compact( 'topic_id' ) ); 
  311. $bbdb->query( $bbdb->prepare( 
  312. "UPDATE $bbdb->forums SET topics = topics + 1, posts = posts + %d WHERE forum_id = %d", $topic->topic_posts, $forum_id 
  313. ) ); 
  314. $bbdb->query( $bbdb->prepare(  
  315. "UPDATE $bbdb->forums SET topics = topics - 1, posts = posts - %d WHERE forum_id = %d", $topic->topic_posts, $topic->forum_id 
  316. ) ); 
  317. wp_cache_flush( 'bb_post' ); 
  318. wp_cache_delete( $topic_id, 'bb_topic' ); 
  319. wp_cache_delete( $forum_id, 'bb_forum' ); 
  320. wp_cache_flush( 'bb_forums' ); 
  321. wp_cache_flush( 'bb_query' ); 
  322. wp_cache_flush( 'bb_cache_posts_post_ids' ); 
  323.  
  324. do_action( 'bb_move_topic', $topic_id, $forum_id, $topic->forum_id ); 
  325.  
  326. return $forum_id; 
  327. return false; 
  328.  
  329. function bb_topic_set_last_post( $topic_id ) { 
  330. global $bbdb; 
  331. $topic_id = (int) $topic_id; 
  332. $old_post = $bbdb->get_row( $bbdb->prepare( 
  333. "SELECT post_id, poster_id, post_time FROM $bbdb->posts WHERE topic_id = %d AND post_status = 0 ORDER BY post_time DESC LIMIT 1", $topic_id 
  334. ) ); 
  335. $old_poster = bb_get_user( $old_post->poster_id ); 
  336. wp_cache_delete( $topic_id, 'bb_topic' ); 
  337. return $bbdb->update( $bbdb->topics, array( 'topic_time' => $old_post->post_time, 'topic_last_poster' => $old_post->poster_id, 'topic_last_poster_name' => $old_poster->login_name, 'topic_last_post_id' => $old_post->post_id ), compact( 'topic_id' ) ); 
  338.  
  339. function bb_close_topic( $topic_id ) { 
  340. global $bbdb; 
  341. $topic_id = (int) $topic_id; 
  342. wp_cache_delete( $topic_id, 'bb_topic' ); 
  343. $r = $bbdb->update( $bbdb->topics, array( 'topic_open' => 0 ), compact( 'topic_id' ) ); 
  344. do_action('close_topic', $topic_id, $r); 
  345. return $r; 
  346.  
  347. function bb_open_topic( $topic_id ) { 
  348. global $bbdb; 
  349. $topic_id = (int) $topic_id; 
  350. wp_cache_delete( $topic_id, 'bb_topic' ); 
  351. $r = $bbdb->update( $bbdb->topics, array( 'topic_open' => 1 ), compact( 'topic_id' ) ); 
  352. do_action('open_topic', $topic_id, $r); 
  353. return $r; 
  354.  
  355. function bb_stick_topic( $topic_id, $super = 0 ) { 
  356. global $bbdb; 
  357. $topic_id = (int) $topic_id; 
  358. $stick = 1 + abs((int) $super); 
  359. wp_cache_delete( $topic_id, 'bb_topic' ); 
  360. $r = $bbdb->update( $bbdb->topics, array( 'topic_sticky' => $stick ), compact( 'topic_id' ) ); 
  361. do_action('stick_topic', $topic_id, $r); 
  362. return $r; 
  363.  
  364. function bb_unstick_topic( $topic_id ) { 
  365. global $bbdb; 
  366. $topic_id = (int) $topic_id; 
  367. wp_cache_delete( $topic_id, 'bb_topic' ); 
  368. $r = $bbdb->update( $bbdb->topics, array( 'topic_sticky' => 0 ), compact( 'topic_id' ) ); 
  369. do_action('unstick_topic', $topic_id, $r); 
  370. return $r; 
  371.  
  372. function topic_is_open( $topic_id = 0 ) { 
  373. $topic = get_topic( get_topic_id( $topic_id ) ); 
  374. return 1 == $topic->topic_open; 
  375.  
  376. function topic_is_sticky( $topic_id = 0 ) { 
  377. $topic = get_topic( get_topic_id( $topic_id ) ); 
  378. return '0' !== $topic->topic_sticky; 
  379.  
  380. function bb_update_topic_voices( $topic_id ) 
  381. if ( !$topic_id ) { 
  382. return; 
  383.  
  384. $topic_id = abs( (int) $topic_id ); 
  385.  
  386. global $bbdb; 
  387. if ( $voices = $bbdb->get_col( $bbdb->prepare( "SELECT DISTINCT poster_id FROM $bbdb->posts WHERE topic_id = %s AND post_status = '0';", $topic_id ) ) ) { 
  388. $voices = count( $voices ); 
  389. bb_update_topicmeta( $topic_id, 'voices_count', $voices ); 
  390.  
  391. /** Thread */ 
  392.  
  393. // Thread, topic? Guh-wah? 
  394. // A topic is the container, the thread is it's contents (the posts) 
  395.  
  396. function get_thread( $topic_id, $args = null ) { 
  397. $_args = func_get_args(); 
  398. $defaults = array( 'page' => 1, 'order' => 'ASC' ); 
  399. if ( is_numeric( $args ) ) 
  400. $args = array( 'page' => $args ); 
  401. if ( @$_args[2] ) 
  402. $defaults['order'] = 'DESC'; 
  403.  
  404. $args = wp_parse_args( $args, $defaults ); 
  405. $args['topic_id'] = $topic_id; 
  406.  
  407. $query = new BB_Query( 'post', $args, 'get_thread' ); 
  408. return $query->results; 
  409.  
  410. // deprecated 
  411. function get_thread_post_ids( $topic_id ) { 
  412. $return = array( 'post' => array(), 'poster' => array() ); 
  413. foreach ( get_thread( $topic_id, array( 'per_page' => -1 ) ) as $post ) { 
  414. $return['post'][] = $post->post_id; 
  415. $return['poster'][] = $post->poster_id; 
  416. return $return; 
.