PageRenderTime 66ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/buddypress/bp-forums.php

https://bitbucket.org/openfarmtech/weblog-content
PHP | 556 lines | 383 code | 146 blank | 27 comment | 63 complexity | 0cbd44e385f686825a8cedc23a615555 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.0, LGPL-3.0, BSD-3-Clause, GPL-3.0, LGPL-2.1, AGPL-3.0, CC-BY-SA-3.0
  1. <?php
  2. /* Define the parent forum ID */
  3. if ( !defined( 'BP_FORUMS_PARENT_FORUM_ID' ) )
  4. define( 'BP_FORUMS_PARENT_FORUM_ID', 1 );
  5. if ( !defined( 'BP_FORUMS_SLUG' ) )
  6. define( 'BP_FORUMS_SLUG', 'forums' );
  7. if ( !defined( 'BB_PATH' ) )
  8. require ( BP_PLUGIN_DIR . '/bp-forums/bp-forums-bbpress.php' );
  9. require ( BP_PLUGIN_DIR . '/bp-forums/bp-forums-templatetags.php' );
  10. require ( BP_PLUGIN_DIR . '/bp-forums/bp-forums-filters.php' );
  11. function bp_forums_setup() {
  12. global $bp;
  13. /* For internal identification */
  14. $bp->forums->id = 'forums';
  15. $bp->forums->image_base = BP_PLUGIN_URL . '/bp-forums/images';
  16. $bp->forums->bbconfig = $bp->site_options['bb-config-location'];
  17. $bp->forums->slug = BP_FORUMS_SLUG;
  18. /* Register this in the active components array */
  19. $bp->active_components[$bp->forums->slug] = $bp->forums->id;
  20. do_action( 'bp_forums_setup' );
  21. }
  22. add_action( 'bp_setup_globals', 'bp_forums_setup' );
  23. function bp_forums_is_installed_correctly() {
  24. global $bp;
  25. if ( file_exists( $bp->forums->bbconfig ) )
  26. return true;
  27. return false;
  28. }
  29. function bp_forums_setup_root_component() {
  30. /* Register 'forums' as a root component */
  31. bp_core_add_root_component( BP_FORUMS_SLUG );
  32. }
  33. add_action( 'bp_setup_root_components', 'bp_forums_setup_root_component' );
  34. function bp_forums_directory_forums_setup() {
  35. global $bp;
  36. if ( $bp->current_component == $bp->forums->slug ) {
  37. if ( (int) $bp->site_options['bp-disable-forum-directory'] || !function_exists( 'groups_install' ) )
  38. return false;
  39. if ( !bp_forums_is_installed_correctly() ) {
  40. bp_core_add_message( __( 'The forums component has not been set up yet.', 'buddypress' ), 'error' );
  41. bp_core_redirect( $bp->root_domain );
  42. }
  43. $bp->is_directory = true;
  44. do_action( 'bbpress_init' );
  45. /* Check to see if the user has posted a new topic from the forums page. */
  46. if ( isset( $_POST['submit_topic'] ) && function_exists( 'bp_forums_new_topic' ) ) {
  47. /* Check the nonce */
  48. check_admin_referer( 'bp_forums_new_topic' );
  49. if ( $bp->groups->current_group = groups_get_group( array( 'group_id' => $_POST['topic_group_id'] ) ) ) {
  50. /* Auto join this user if they are not yet a member of this group */
  51. if ( !is_site_admin() && 'public' == $bp->groups->current_group->status && !groups_is_user_member( $bp->loggedin_user->id, $bp->groups->current_group->id ) )
  52. groups_join_group( $bp->groups->current_group->id, $bp->groups->current_group->id );
  53. if ( $forum_id = groups_get_groupmeta( $bp->groups->current_group->id, 'forum_id' ) ) {
  54. if ( !$topic = groups_new_group_forum_topic( $_POST['topic_title'], $_POST['topic_text'], $_POST['topic_tags'], $forum_id ) )
  55. bp_core_add_message( __( 'There was an error when creating the topic', 'buddypress'), 'error' );
  56. else
  57. bp_core_add_message( __( 'The topic was created successfully', 'buddypress') );
  58. bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) . '/forum/topic/' . $topic->topic_slug . '/' );
  59. } else {
  60. bp_core_add_message( __( 'Please pick the group forum where you would like to post this topic.', 'buddypress' ), 'error' );
  61. }
  62. }
  63. }
  64. do_action( 'bp_forums_directory_forums_setup' );
  65. bp_core_load_template( apply_filters( 'bp_forums_template_directory_forums_setup', 'forums/index' ) );
  66. }
  67. }
  68. add_action( 'wp', 'bp_forums_directory_forums_setup', 2 );
  69. function bp_forums_add_admin_menu() {
  70. global $bp;
  71. if ( !is_site_admin() )
  72. return false;
  73. require ( BP_PLUGIN_DIR . '/bp-forums/bp-forums-admin.php' );
  74. /* Add the administration tab under the "Site Admin" tab for site administrators */
  75. add_submenu_page( 'bp-general-settings', __( 'Forums Setup', 'buddypress' ), __( 'Forums Setup', 'buddypress' ), 'manage_options', 'bb-forums-setup', "bp_forums_bbpress_admin" );
  76. }
  77. add_action( 'admin_menu', 'bp_forums_add_admin_menu' );
  78. /* Forum Functions */
  79. function bp_forums_get_forum( $forum_id ) {
  80. do_action( 'bbpress_init' );
  81. return bb_get_forum( $forum_id );
  82. }
  83. function bp_forums_new_forum( $args = '' ) {
  84. do_action( 'bbpress_init' );
  85. $defaults = array(
  86. 'forum_name' => '',
  87. 'forum_desc' => '',
  88. 'forum_parent_id' => BP_FORUMS_PARENT_FORUM_ID,
  89. 'forum_order' => false,
  90. 'forum_is_category' => 0
  91. );
  92. $r = wp_parse_args( $args, $defaults );
  93. extract( $r, EXTR_SKIP );
  94. return bb_new_forum( array( 'forum_name' => stripslashes( $forum_name ), 'forum_desc' => stripslashes( $forum_desc ), 'forum_parent' => $forum_parent_id, 'forum_order' => $forum_order, 'forum_is_category' => $forum_is_category ) );
  95. }
  96. function bp_forums_update_forum( $args = '' ) {
  97. do_action( 'bbpress_init' );
  98. $defaults = array(
  99. 'forum_id' => '',
  100. 'forum_name' => '',
  101. 'forum_desc' => '',
  102. 'forum_slug' => '',
  103. 'forum_parent_id' => BP_FORUMS_PARENT_FORUM_ID,
  104. 'forum_order' => false,
  105. 'forum_is_category' => 0
  106. );
  107. $r = wp_parse_args( $args, $defaults );
  108. extract( $r, EXTR_SKIP );
  109. return bb_update_forum( array( 'forum_id' => (int)$forum_id, 'forum_name' => stripslashes( $forum_name ), 'forum_desc' => stripslashes( $forum_desc ), 'forum_slug' => stripslashes( $forum_slug ), 'forum_parent' => $forum_parent_id, 'forum_order' => $forum_order, 'forum_is_category' => $forum_is_category ) );
  110. }
  111. /* Topic Functions */
  112. function bp_forums_get_forum_topics( $args = '' ) {
  113. global $bp;
  114. do_action( 'bbpress_init' );
  115. $defaults = array(
  116. 'type' => 'newest',
  117. 'forum_id' => false,
  118. 'user_id' => false,
  119. 'page' => 1,
  120. 'per_page' => 15,
  121. 'exclude' => false,
  122. 'show_stickies' => 'all',
  123. 'filter' => false // if $type = tag then filter is the tag name, otherwise it's terms to search on.
  124. );
  125. $r = wp_parse_args( $args, $defaults );
  126. extract( $r, EXTR_SKIP );
  127. switch ( $type ) {
  128. case 'newest':
  129. $query = new BB_Query( 'topic', array( 'forum_id' => $forum_id, 'topic_author_id' => $user_id, 'per_page' => $per_page, 'page' => $page, 'number' => $per_page, 'exclude' => $exclude, 'topic_title' => $filter, 'sticky' => $show_stickies ), 'get_latest_topics' );
  130. $topics =& $query->results;
  131. break;
  132. case 'popular':
  133. $query = new BB_Query( 'topic', array( 'forum_id' => $forum_id, 'topic_author_id' => $user_id, 'per_page' => $per_page, 'page' => $page, 'order_by' => 't.topic_posts', 'topic_title' => $filter, 'sticky' => $show_stickies ) );
  134. $topics =& $query->results;
  135. break;
  136. case 'unreplied':
  137. $query = new BB_Query( 'topic', array( 'forum_id' => $forum_id, 'topic_author_id' => $user_id, 'post_count' => 1, 'per_page' => $per_page, 'page' => $page, 'order_by' => 't.topic_time', 'topic_title' => $filter, 'sticky' => $show_stickies ) );
  138. $topics =& $query->results;
  139. break;
  140. case 'tags':
  141. $query = new BB_Query( 'topic', array( 'forum_id' => $forum_id, 'topic_author_id' => $user_id, 'tag' => $filter, 'per_page' => $per_page, 'page' => $page, 'order_by' => 't.topic_time', 'sticky' => $show_stickies ) );
  142. $topics =& $query->results;
  143. break;
  144. }
  145. return apply_filters( 'bp_forums_get_forum_topics', $topics, &$r );
  146. }
  147. function bp_forums_get_topic_details( $topic_id ) {
  148. do_action( 'bbpress_init' );
  149. $query = new BB_Query( 'topic', 'topic_id=' . $topic_id . '&page=1' /* Page override so bbPress doesn't use the URI */ );
  150. return $query->results[0];
  151. }
  152. function bp_forums_get_topic_id_from_slug( $topic_slug ) {
  153. do_action( 'bbpress_init' );
  154. return bb_get_id_from_slug( 'topic', $topic_slug );
  155. }
  156. function bp_forums_new_topic( $args = '' ) {
  157. global $bp;
  158. do_action( 'bbpress_init' );
  159. $defaults = array(
  160. 'topic_title' => '',
  161. 'topic_slug' => '',
  162. 'topic_text' => '',
  163. 'topic_poster' => $bp->loggedin_user->id, // accepts ids
  164. 'topic_poster_name' => $bp->loggedin_user->fullname, // accept names
  165. 'topic_last_poster' => $bp->loggedin_user->id, // accepts ids
  166. 'topic_last_poster_name' => $bp->loggedin_user->fullname, // accept names
  167. 'topic_start_time' => date( 'Y-m-d H:i:s' ),
  168. 'topic_time' => date( 'Y-m-d H:i:s' ),
  169. 'topic_open' => 1,
  170. 'topic_tags' => false, // accepts array or comma delim
  171. 'forum_id' => 0 // accepts ids or slugs
  172. );
  173. $r = wp_parse_args( $args, $defaults );
  174. extract( $r, EXTR_SKIP );
  175. $topic_title = strip_tags( $topic_title );
  176. if ( empty( $topic_title ) || !strlen( trim( $topic_title ) ) )
  177. return false;
  178. if ( empty( $topic_slug ) )
  179. $topic_slug = sanitize_title( $topic_title );
  180. if ( !$topic_id = bb_insert_topic( array( 'topic_title' => stripslashes( $topic_title ), 'topic_slug' => $topic_slug, 'topic_poster' => $topic_poster, 'topic_poster_name' => $topic_poster_name, 'topic_last_poster' => $topic_last_poster, 'topic_last_poster_name' => $topic_last_poster_name, 'topic_start_time' => $topic_start_time, 'topic_time' => $topic_time, 'topic_open' => $topic_open, 'forum_id' => (int)$forum_id, 'tags' => $topic_tags ) ) )
  181. return false;
  182. /* Now insert the first post. */
  183. if ( !bp_forums_insert_post( array( 'topic_id' => $topic_id, 'post_text' => $topic_text, 'post_time' => $topic_time, 'poster_id' => $topic_poster ) ) )
  184. return false;
  185. do_action( 'bp_forums_new_topic', $topic_id );
  186. return $topic_id;
  187. }
  188. function bp_forums_update_topic( $args = '' ) {
  189. global $bp;
  190. do_action( 'bbpress_init' );
  191. $defaults = array(
  192. 'topic_id' => false,
  193. 'topic_title' => '',
  194. 'topic_text' => ''
  195. );
  196. $r = wp_parse_args( $args, $defaults );
  197. extract( $r, EXTR_SKIP );
  198. if ( !$topic_id = bb_insert_topic( array( 'topic_id' => $topic_id, 'topic_title' => stripslashes( $topic_title ) ) ) )
  199. return false;
  200. if ( !$post = bb_get_first_post( $topic_id ) )
  201. return false;
  202. /* Update the first post */
  203. if ( !$post = bp_forums_insert_post( array( 'post_id' => $post->post_id, 'topic_id' => $topic_id, 'post_text' => $topic_text, 'post_time' => $post->post_time, 'poster_id' => $post->poster_id, 'poster_ip' => $post->poster_ip, 'post_status' => $post->post_status, 'post_position' => $post->post_position ) ) )
  204. return false;
  205. return bp_forums_get_topic_details( $topic_id );
  206. }
  207. function bp_forums_sticky_topic( $args = '' ) {
  208. global $bp;
  209. do_action( 'bbpress_init' );
  210. $defaults = array(
  211. 'topic_id' => false,
  212. 'mode' => 'stick' // stick/unstick
  213. );
  214. $r = wp_parse_args( $args, $defaults );
  215. extract( $r, EXTR_SKIP );
  216. if ( 'stick' == $mode )
  217. return bb_stick_topic( $topic_id );
  218. else if ( 'unstick' == $mode )
  219. return bb_unstick_topic( $topic_id );
  220. return false;
  221. }
  222. function bp_forums_openclose_topic( $args = '' ) {
  223. global $bp;
  224. do_action( 'bbpress_init' );
  225. $defaults = array(
  226. 'topic_id' => false,
  227. 'mode' => 'close' // stick/unstick
  228. );
  229. $r = wp_parse_args( $args, $defaults );
  230. extract( $r, EXTR_SKIP );
  231. if ( 'close' == $mode )
  232. return bb_close_topic( $topic_id );
  233. else if ( 'open' == $mode )
  234. return bb_open_topic( $topic_id );
  235. return false;
  236. }
  237. function bp_forums_delete_topic( $args = '' ) {
  238. global $bp;
  239. do_action( 'bbpress_init' );
  240. $defaults = array(
  241. 'topic_id' => false
  242. );
  243. $r = wp_parse_args( $args, $defaults );
  244. extract( $r, EXTR_SKIP );
  245. return bb_delete_topic( $topic_id, 1 );
  246. }
  247. function bp_forums_total_topic_count() {
  248. do_action( 'bbpress_init' );
  249. $query = new BB_Query( 'topic', array( 'page' => 1, 'per_page' => -1, 'count' => true ) );
  250. $count = $query->count;
  251. $query = null;
  252. return $count;
  253. }
  254. function bp_forums_total_topic_count_for_user( $user_id = false ) {
  255. global $bp;
  256. do_action( 'bbpress_init' );
  257. if ( !$user_id )
  258. $user_id = ( $bp->displayed_user->id ) ? $bp->displayed_user->id : $bp->loggedin_user->id;
  259. $query = new BB_Query( 'topic', array( 'topic_author_id' => $user_id, 'page' => 1, 'per_page' => -1, 'count' => true ) );
  260. $count = $query->count;
  261. $query = null;
  262. return $count;
  263. }
  264. function bp_forums_get_topic_extras( $topics ) {
  265. global $bp, $wpdb, $bbdb;
  266. if ( empty( $topics ) )
  267. return $topics;
  268. /* Get the topic ids */
  269. foreach ( (array)$topics as $topic ) $topic_ids[] = $topic->topic_id;
  270. $topic_ids = $wpdb->escape( join( ',', (array)$topic_ids ) );
  271. /* Fetch the topic's last poster details */
  272. $poster_details = $wpdb->get_results( $wpdb->prepare( "SELECT t.topic_id, t.topic_last_poster, u.user_login, u.user_nicename, u.user_email, u.display_name FROM {$wpdb->users} u, {$bbdb->topics} t WHERE u.ID = t.topic_last_poster AND t.topic_id IN ( {$topic_ids} )" ) );
  273. for ( $i = 0; $i < count( $topics ); $i++ ) {
  274. foreach ( (array)$poster_details as $poster ) {
  275. if ( $poster->topic_id == $topics[$i]->topic_id ) {
  276. $topics[$i]->topic_last_poster_email = $poster->user_email;
  277. $topics[$i]->topic_last_poster_nicename = $poster->user_nicename;
  278. $topics[$i]->topic_last_poster_login = $poster->user_login;
  279. $topics[$i]->topic_last_poster_displayname = $poster->display_name;
  280. }
  281. }
  282. }
  283. /* Fetch fullname for the topic's last poster */
  284. if ( function_exists( 'xprofile_install' ) ) {
  285. $poster_names = $wpdb->get_results( $wpdb->prepare( "SELECT t.topic_id, pd.value FROM {$bp->profile->table_name_data} pd, {$bbdb->topics} t WHERE pd.user_id = t.topic_last_poster AND pd.field_id = 1 AND t.topic_id IN ( {$topic_ids} )" ) );
  286. for ( $i = 0; $i < count( $topics ); $i++ ) {
  287. foreach ( (array)$poster_names as $name ) {
  288. if ( $name->topic_id == $topics[$i]->topic_id )
  289. $topics[$i]->topic_last_poster_displayname = $name->value;
  290. }
  291. }
  292. }
  293. return $topics;
  294. }
  295. /* Post Functions */
  296. function bp_forums_get_topic_posts( $args = '' ) {
  297. do_action( 'bbpress_init' );
  298. $defaults = array(
  299. 'topic_id' => false,
  300. 'page' => 1,
  301. 'per_page' => 15,
  302. 'order' => 'ASC'
  303. );
  304. $args = wp_parse_args( $args, $defaults );
  305. $query = new BB_Query( 'post', $args, 'get_thread' );
  306. return bp_forums_get_post_extras( $query->results );
  307. }
  308. function bp_forums_get_post( $post_id ) {
  309. do_action( 'bbpress_init' );
  310. return bb_get_post( $post_id );
  311. }
  312. function bp_forums_delete_post( $args = '' ) {
  313. global $bp;
  314. do_action( 'bbpress_init' );
  315. $defaults = array(
  316. 'post_id' => false
  317. );
  318. $r = wp_parse_args( $args, $defaults );
  319. extract( $r, EXTR_SKIP );
  320. return bb_delete_post( $post_id, 1 );
  321. }
  322. function bp_forums_insert_post( $args = '' ) {
  323. global $bp;
  324. do_action( 'bbpress_init' );
  325. $defaults = array(
  326. 'post_id' => false,
  327. 'topic_id' => false,
  328. 'post_text' => '',
  329. 'post_time' => date( 'Y-m-d H:i:s' ),
  330. 'poster_id' => $bp->loggedin_user->id, // accepts ids or names
  331. 'poster_ip' => $_SERVER['REMOTE_ADDR'],
  332. 'post_status' => 0, // use bb_delete_post() instead
  333. 'post_position' => false
  334. );
  335. $r = wp_parse_args( $args, $defaults );
  336. extract( $r, EXTR_SKIP );
  337. if ( !$post = bp_forums_get_post( $post_id ) )
  338. $post_id = false;
  339. if ( !isset( $topic_id ) )
  340. $topic_id = $post->topic_id;
  341. if ( empty( $post_text ) )
  342. $post_text = $post->post_text;
  343. if ( !isset( $post_time ) )
  344. $post_time = $post->post_time;
  345. if ( !isset( $post_position ) )
  346. $post_position = $post->post_position;
  347. $post_id = bb_insert_post( array( 'post_id' => $post_id, 'topic_id' => $topic_id, 'post_text' => stripslashes( trim( $post_text ) ), 'post_time' => $post_time, 'poster_id' => $poster_id, 'poster_ip' => $poster_ip, 'post_status' => $post_status, 'post_position' => $post_position ) );
  348. if ( $post_id )
  349. do_action( 'bp_forums_new_post', $post_id );
  350. return $post_id;
  351. }
  352. function bp_forums_get_post_extras( $posts ) {
  353. global $bp, $wpdb;
  354. if ( empty( $posts ) )
  355. return $posts;
  356. /* Get the user ids */
  357. foreach ( (array)$posts as $post ) $user_ids[] = $post->poster_id;
  358. $user_ids = $wpdb->escape( join( ',', (array)$user_ids ) );
  359. /* Fetch the poster's user_email, user_nicename and user_login */
  360. $poster_details = $wpdb->get_results( $wpdb->prepare( "SELECT u.ID as user_id, u.user_login, u.user_nicename, u.user_email, u.display_name FROM {$wpdb->users} u WHERE u.ID IN ( {$user_ids} )" ) );
  361. for ( $i = 0; $i < count( $posts ); $i++ ) {
  362. foreach ( (array)$poster_details as $poster ) {
  363. if ( $poster->user_id == $posts[$i]->poster_id ) {
  364. $posts[$i]->poster_email = $poster->user_email;
  365. $posts[$i]->poster_login = $poster->user_nicename;
  366. $posts[$i]->poster_nicename = $poster->user_login;
  367. $posts[$i]->poster_name = $poster->display_name;
  368. }
  369. }
  370. }
  371. /* Fetch fullname for each poster. */
  372. if ( function_exists( 'xprofile_install' ) ) {
  373. $poster_names = $wpdb->get_results( $wpdb->prepare( "SELECT pd.user_id, pd.value FROM {$bp->profile->table_name_data} pd WHERE pd.user_id IN ( {$user_ids} )" ) );
  374. for ( $i = 0; $i < count( $posts ); $i++ ) {
  375. foreach ( (array)$poster_names as $name ) {
  376. if ( $name->user_id == $topics[$i]->user_id )
  377. $posts[$i]->poster_name = $poster->value;
  378. }
  379. }
  380. }
  381. return $posts;
  382. }
  383. function bp_forums_get_forum_topicpost_count( $forum_id ) {
  384. global $wpdb, $bbdb;
  385. do_action( 'bbpress_init' );
  386. /* Need to find a bbPress function that does this */
  387. return $wpdb->get_results( $wpdb->prepare( "SELECT topics, posts from {$bbdb->forums} WHERE forum_id = %d", $forum_id ) );
  388. }
  389. function bp_forums_filter_caps( $allcaps ) {
  390. global $bp, $wp_roles, $bb_table_prefix;
  391. $bb_cap = get_usermeta( $bp->loggedin_user->id, $bb_table_prefix . 'capabilities' );
  392. if ( empty( $bb_cap ) )
  393. return $allcaps;
  394. $bb_cap = array_keys($bb_cap);
  395. $bb_cap = $wp_roles->get_role( $bb_cap[0] );
  396. $bb_cap = $bb_cap->capabilities;
  397. return array_merge( (array) $allcaps, (array) $bb_cap );
  398. }
  399. add_filter( 'user_has_cap', 'bp_forums_filter_caps' );
  400. /********************************************************************************
  401. * Caching
  402. *
  403. * Caching functions handle the clearing of cached objects and pages on specific
  404. * actions throughout BuddyPress.
  405. */
  406. // List actions to clear super cached pages on, if super cache is installed
  407. add_action( 'bp_forums_new_forum', 'bp_core_clear_cache' );
  408. add_action( 'bp_forums_new_topic', 'bp_core_clear_cache' );
  409. add_action( 'bp_forums_new_post', 'bp_core_clear_cache' );
  410. ?>