PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/codemen_iftekhar/codemen
PHP | 460 lines | 369 code | 76 blank | 15 comment | 71 complexity | 317e99210f96367df6a0c4f51e0de46b MD5 | raw file
  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. $user = bb_get_user( $topic_poster_name, array( 'by' => 'login' ) );
  144. if ( !empty( $user ) ) {
  145. $topic_poster = $user->ID;
  146. $topic_poster_name = $user->user_login;
  147. }
  148. if ( !$last_user = bb_get_user( $topic_last_poster ) )
  149. $last_user = bb_get_user( $topic_last_poster_name, array( 'by' => 'login' ) );
  150. if ( !empty( $last_user ) ) {
  151. $topic_last_poster = $last_user->ID;
  152. $topic_last_poster_name = $last_user->user_login;
  153. }
  154. if ( in_array( 'topic_title', $fields ) ) {
  155. $topic_title = apply_filters( 'pre_topic_title', $topic_title, $topic_id );
  156. if ( strlen($topic_title) < 1 )
  157. return false;
  158. }
  159. if ( in_array( 'topic_slug', $fields ) ) {
  160. $slug_sql = $update ?
  161. "SELECT topic_slug FROM $bbdb->topics WHERE topic_slug = %s AND topic_id != %d" :
  162. "SELECT topic_slug FROM $bbdb->topics WHERE topic_slug = %s";
  163. $topic_slug = $_topic_slug = bb_slug_sanitize( $topic_slug ? $topic_slug : wp_specialchars_decode( $topic_title, ENT_QUOTES ) );
  164. if ( strlen( $_topic_slug ) < 1 )
  165. $topic_slug = $_topic_slug = '0';
  166. while ( is_numeric($topic_slug) || $existing_slug = $bbdb->get_var( $bbdb->prepare( $slug_sql, $topic_slug, $topic_id ) ) )
  167. $topic_slug = bb_slug_increment( $_topic_slug, $existing_slug );
  168. }
  169. if ( $update ) {
  170. $bbdb->update( $bbdb->topics, compact( $fields ), compact( 'topic_id' ) );
  171. wp_cache_delete( $topic_id, 'bb_topic' );
  172. if ( in_array( 'topic_slug', $fields ) )
  173. wp_cache_delete( $topic->topic_slug, 'bb_topic_slug' );
  174. wp_cache_flush( 'bb_query' );
  175. wp_cache_flush( 'bb_cache_posts_post_ids' );
  176. do_action( 'bb_update_topic', $topic_id );
  177. } else {
  178. $bbdb->insert( $bbdb->topics, compact( $fields ) );
  179. $topic_id = $bbdb->insert_id;
  180. $bbdb->query( $bbdb->prepare( "UPDATE $bbdb->forums SET topics = topics + 1 WHERE forum_id = %d", $forum_id ) );
  181. wp_cache_delete( $forum_id, 'bb_forum' );
  182. wp_cache_flush( 'bb_forums' );
  183. wp_cache_flush( 'bb_query' );
  184. wp_cache_flush( 'bb_cache_posts_post_ids' );
  185. do_action( 'bb_new_topic', $topic_id );
  186. }
  187. if ( !empty( $tags ) )
  188. bb_add_topic_tags( $topic_id, $tags );
  189. do_action( 'bb_insert_topic', $topic_id, $args, compact( array_keys($args) ) ); // topic_id, what was passed, what was used
  190. return $topic_id;
  191. }
  192. // Deprecated: expects $title to be pre-escaped
  193. function bb_new_topic( $title, $forum, $tags = '', $args = '' ) {
  194. $title = stripslashes( $title );
  195. $tags = stripslashes( $tags );
  196. $forum = (int) $forum;
  197. return bb_insert_topic( wp_parse_args( $args ) + array( 'topic_title' => $title, 'forum_id' => $forum, 'tags' => $tags ) );
  198. }
  199. // Deprecated: expects $title to be pre-escaped
  200. function bb_update_topic( $title, $topic_id ) {
  201. $title = stripslashes( $title );
  202. return bb_insert_topic( array( 'topic_title' => $title, 'topic_id' => $topic_id ) );
  203. }
  204. function bb_delete_topic( $topic_id, $new_status = 0 ) {
  205. global $bbdb;
  206. $topic_id = (int) $topic_id;
  207. add_filter( 'get_topic_where', 'bb_no_where' );
  208. if ( $topic = get_topic( $topic_id ) ) {
  209. $new_status = (int) $new_status;
  210. $old_status = (int) $topic->topic_status;
  211. if ( $new_status == $old_status )
  212. return;
  213. $thread_args = array( 'per_page' => -1, 'order' => 'DESC' );
  214. if ( 0 != $old_status && 0 == $new_status )
  215. $thread_args['post_status'] = 'all';
  216. $poster_ids = array();
  217. $posts = get_thread( $topic_id, $thread_args );
  218. if ( $posts && count( $posts ) ) {
  219. foreach ( $posts as $post ) {
  220. _bb_delete_post( $post->post_id, $new_status );
  221. $poster_ids[] = $post->poster_id;
  222. }
  223. }
  224. if ( count( $poster_ids ) ) {
  225. foreach ( array_unique( $poster_ids ) as $id ) {
  226. if ( $user = bb_get_user( $id ) ) {
  227. $topics_replied_key = $bbdb->prefix . 'topics_replied';
  228. bb_update_usermeta( $user->ID, $topics_replied_key, ( $old_status ? $user->$topics_replied_key + 1 : $user->$topics_replied_key - 1 ) );
  229. }
  230. }
  231. }
  232. 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" ) )
  233. foreach ( $ids as $id )
  234. bb_remove_user_favorite( $id, $topic_id );
  235. switch ( $new_status ) {
  236. case 0: // Undeleting
  237. $bbdb->update( $bbdb->topics, array( 'topic_status' => $new_status ), compact( 'topic_id' ) );
  238. $topic_posts = (int) $bbdb->get_var( $bbdb->prepare(
  239. "SELECT COUNT(*) FROM $bbdb->posts WHERE topic_id = %d AND post_status = 0", $topic_id
  240. ) );
  241. $all_posts = (int) $bbdb->get_var( $bbdb->prepare(
  242. "SELECT COUNT(*) FROM $bbdb->posts WHERE topic_id = %d", $topic_id
  243. ) );
  244. bb_update_topicmeta( $topic_id, 'deleted_posts', $all_posts - $topic_posts );
  245. $bbdb->query( $bbdb->prepare(
  246. "UPDATE $bbdb->forums SET topics = topics + 1, posts = posts + %d WHERE forum_id = %d", $topic_posts, $topic->forum_id
  247. ) );
  248. $bbdb->update( $bbdb->topics, compact( 'topic_posts' ), compact( 'topic_id' ) );
  249. bb_topic_set_last_post( $topic_id );
  250. bb_update_post_positions( $topic_id );
  251. break;
  252. default: // Other statuses (like Delete and Bozo)
  253. bb_remove_topic_tags( $topic_id );
  254. $bbdb->update( $bbdb->topics, array( 'topic_status' => $new_status, 'tag_count' => 0 ), compact( 'topic_id' ) );
  255. $bbdb->query( $bbdb->prepare(
  256. "UPDATE $bbdb->forums SET topics = topics - 1, posts = posts - %d WHERE forum_id = %d", $topic->topic_posts, $topic->forum_id
  257. ) );
  258. break;
  259. }
  260. do_action( 'bb_delete_topic', $topic_id, $new_status, $old_status );
  261. wp_cache_delete( $topic_id, 'bb_topic' );
  262. wp_cache_delete( $topic->topic_slug, 'bb_topic_slug' );
  263. wp_cache_delete( $topic_id, 'bb_thread' );
  264. wp_cache_delete( $topic->forum_id, 'bb_forum' );
  265. wp_cache_flush( 'bb_forums' );
  266. wp_cache_flush( 'bb_query' );
  267. wp_cache_flush( 'bb_cache_posts_post_ids' );
  268. return $topic_id;
  269. } else {
  270. return false;
  271. }
  272. }
  273. function bb_move_topic( $topic_id, $forum_id ) {
  274. global $bbdb;
  275. $topic = get_topic( $topic_id );
  276. $forum = bb_get_forum( $forum_id );
  277. $topic_id = (int) $topic->topic_id;
  278. $forum_id = (int) $forum->forum_id;
  279. if ( $topic && $forum && $topic->forum_id != $forum_id ) {
  280. $bbdb->update( $bbdb->posts, compact( 'forum_id' ), compact( 'topic_id' ) );
  281. $bbdb->update( $bbdb->topics, compact( 'forum_id' ), compact( 'topic_id' ) );
  282. $bbdb->query( $bbdb->prepare(
  283. "UPDATE $bbdb->forums SET topics = topics + 1, posts = posts + %d WHERE forum_id = %d", $topic->topic_posts, $forum_id
  284. ) );
  285. $bbdb->query( $bbdb->prepare(
  286. "UPDATE $bbdb->forums SET topics = topics - 1, posts = posts - %d WHERE forum_id = %d", $topic->topic_posts, $topic->forum_id
  287. ) );
  288. wp_cache_flush( 'bb_post' );
  289. wp_cache_delete( $topic_id, 'bb_topic' );
  290. wp_cache_delete( $forum_id, 'bb_forum' );
  291. wp_cache_flush( 'bb_forums' );
  292. wp_cache_flush( 'bb_query' );
  293. wp_cache_flush( 'bb_cache_posts_post_ids' );
  294. do_action( 'bb_move_topic', $topic_id, $forum_id, $topic->forum_id );
  295. return $forum_id;
  296. }
  297. return false;
  298. }
  299. function bb_topic_set_last_post( $topic_id ) {
  300. global $bbdb;
  301. $topic_id = (int) $topic_id;
  302. $old_post = $bbdb->get_row( $bbdb->prepare(
  303. "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
  304. ) );
  305. $old_poster = bb_get_user( $old_post->poster_id );
  306. wp_cache_delete( $topic_id, 'bb_topic' );
  307. 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' ) );
  308. }
  309. function bb_close_topic( $topic_id ) {
  310. global $bbdb;
  311. $topic_id = (int) $topic_id;
  312. wp_cache_delete( $topic_id, 'bb_topic' );
  313. $r = $bbdb->update( $bbdb->topics, array( 'topic_open' => 0 ), compact( 'topic_id' ) );
  314. do_action('close_topic', $topic_id, $r);
  315. return $r;
  316. }
  317. function bb_open_topic( $topic_id ) {
  318. global $bbdb;
  319. $topic_id = (int) $topic_id;
  320. wp_cache_delete( $topic_id, 'bb_topic' );
  321. $r = $bbdb->update( $bbdb->topics, array( 'topic_open' => 1 ), compact( 'topic_id' ) );
  322. do_action('open_topic', $topic_id, $r);
  323. return $r;
  324. }
  325. function bb_stick_topic( $topic_id, $super = 0 ) {
  326. global $bbdb;
  327. $topic_id = (int) $topic_id;
  328. $stick = 1 + abs((int) $super);
  329. wp_cache_delete( $topic_id, 'bb_topic' );
  330. $r = $bbdb->update( $bbdb->topics, array( 'topic_sticky' => $stick ), compact( 'topic_id' ) );
  331. do_action('stick_topic', $topic_id, $r);
  332. return $r;
  333. }
  334. function bb_unstick_topic( $topic_id ) {
  335. global $bbdb;
  336. $topic_id = (int) $topic_id;
  337. wp_cache_delete( $topic_id, 'bb_topic' );
  338. $r = $bbdb->update( $bbdb->topics, array( 'topic_sticky' => 0 ), compact( 'topic_id' ) );
  339. do_action('unstick_topic', $topic_id, $r);
  340. return $r;
  341. }
  342. function topic_is_open( $topic_id = 0 ) {
  343. $topic = get_topic( get_topic_id( $topic_id ) );
  344. return 1 == $topic->topic_open;
  345. }
  346. function topic_is_sticky( $topic_id = 0 ) {
  347. $topic = get_topic( get_topic_id( $topic_id ) );
  348. return '0' !== $topic->topic_sticky;
  349. }
  350. function bb_update_topic_voices( $topic_id )
  351. {
  352. if ( !$topic_id ) {
  353. return;
  354. }
  355. $topic_id = abs( (int) $topic_id );
  356. global $bbdb;
  357. if ( $voices = $bbdb->get_col( $bbdb->prepare( "SELECT DISTINCT poster_id FROM $bbdb->posts WHERE topic_id = %s AND post_status = '0';", $topic_id ) ) ) {
  358. $voices = count( $voices );
  359. bb_update_topicmeta( $topic_id, 'voices_count', $voices );
  360. }
  361. }
  362. /* Thread */
  363. // Thread, topic? Guh-wah?
  364. // A topic is the container, the thread is it's contents (the posts)
  365. function get_thread( $topic_id, $args = null ) {
  366. $defaults = array( 'page' => 1, 'order' => 'ASC' );
  367. if ( is_numeric( $args ) )
  368. $args = array( 'page' => $args );
  369. if ( @func_get_arg(2) )
  370. $defaults['order'] = 'DESC';
  371. $args = wp_parse_args( $args, $defaults );
  372. $args['topic_id'] = $topic_id;
  373. $query = new BB_Query( 'post', $args, 'get_thread' );
  374. return $query->results;
  375. }
  376. // deprecated
  377. function get_thread_post_ids( $topic_id ) {
  378. $return = array( 'post' => array(), 'poster' => array() );
  379. foreach ( get_thread( $topic_id, array( 'per_page' => -1 ) ) as $post ) {
  380. $return['post'][] = $post->post_id;
  381. $return['poster'][] = $post->poster_id;
  382. }
  383. return $return;
  384. }