PageRenderTime 81ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/forum/bb-includes/functions.bb-topics.php

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