/includes/extend/buddypress/groups.php

  1. <?php 
  2.  
  3. /** 
  4. * bbPress BuddyPress Group Extension Class 
  5. * 
  6. * This file is responsible for connecting bbPress to BuddyPress's Groups 
  7. * Component. It's a great example of how to perform both simple and advanced 
  8. * techniques to manipulate bbPress's default output. 
  9. * 
  10. * @package bbPress 
  11. * @subpackage BuddyPress 
  12. * @todo maybe move to BuddyPress Forums once bbPress 1.1 can be removed 
  13. */ 
  14.  
  15. // Exit if accessed directly 
  16. if ( !defined( 'ABSPATH' ) ) exit; 
  17.  
  18. if ( !class_exists( 'BBP_Forums_Group_Extension' ) && class_exists( 'BP_Group_Extension' ) ) : 
  19. /** 
  20. * Loads Group Extension for Forums Component 
  21. * 
  22. * @since bbPress (r3552) 
  23. * 
  24. * @package bbPress 
  25. * @subpackage BuddyPress 
  26. * @todo Everything 
  27. */ 
  28. class BBP_Forums_Group_Extension extends BP_Group_Extension { 
  29.  
  30. /** Methods ***************************************************************/ 
  31.  
  32. /** 
  33. * Setup bbPress group extension variables 
  34. * 
  35. * @since bbPress (r3552) 
  36. */ 
  37. public function __construct() { 
  38. $this->setup_variables(); 
  39. $this->setup_actions(); 
  40. $this->setup_filters(); 
  41. $this->maybe_unset_forum_menu(); 
  42.  
  43. /** 
  44. * Setup the group forums class variables 
  45. * 
  46. * @since bbPress () 
  47. */ 
  48. private function setup_variables() { 
  49.  
  50. // Component Name 
  51. $this->name = __( 'Forum', 'bbpress' ); 
  52. $this->nav_item_name = __( 'Forum', 'bbpress' ); 
  53.  
  54. // Component slugs (hardcoded to match bbPress 1.x functionality) 
  55. $this->slug = 'forum'; 
  56. $this->topic_slug = 'topic'; 
  57. $this->reply_slug = 'reply'; 
  58.  
  59. // Forum component is visible 
  60. $this->visibility = 'public'; 
  61.  
  62. // Set positions towards end 
  63. $this->create_step_position = 15; 
  64. $this->nav_item_position = 10; 
  65.  
  66. // Allow create step and show in nav 
  67. $this->enable_create_step = true; 
  68. $this->enable_nav_item = true; 
  69. $this->enable_edit_item = true; 
  70.  
  71. // Template file to load, and action to hook display on to 
  72. $this->template_file = 'groups/single/plugins'; 
  73. $this->display_hook = 'bp_template_content'; 
  74.  
  75. /** 
  76. * Setup the group forums class actions 
  77. * 
  78. * @since bbPress (r4552) 
  79. */ 
  80. private function setup_actions() { 
  81.  
  82. // Possibly redirect 
  83. add_action( 'bbp_template_redirect', array( $this, 'redirect_canonical' ) ); 
  84.  
  85. // Remove group forum cap map when view is done 
  86. add_action( 'bbp_after_group_forum_display', array( $this, 'remove_group_forum_meta_cap_map' ) ); 
  87.  
  88. // bbPress needs to listen to BuddyPress group deletion. 
  89. add_action( 'groups_before_delete_group', array( $this, 'disconnect_forum_from_group' ) ); 
  90.  
  91. // Adds a bbPress metabox to the new BuddyPress Group Admin UI 
  92. add_action( 'bp_groups_admin_meta_boxes', array( $this, 'group_admin_ui_edit_screen' ) ); 
  93.  
  94. // Saves the bbPress options if they come from the BuddyPress Group Admin UI 
  95. add_action( 'bp_group_admin_edit_after', array( $this, 'edit_screen_save' ) ); 
  96.  
  97. // Adds a hidden input value to the "Group Settings" page 
  98. add_action( 'bp_before_group_settings_admin', array( $this, 'group_settings_hidden_field' ) ); 
  99.  
  100. /** 
  101. * Setup the group forums class filters 
  102. * 
  103. * @since bbPress (r4552) 
  104. */ 
  105. private function setup_filters() { 
  106.  
  107. // Group forum pagination 
  108. add_filter( 'bbp_topic_pagination', array( $this, 'topic_pagination' ) ); 
  109. add_filter( 'bbp_replies_pagination', array( $this, 'replies_pagination' ) ); 
  110.  
  111. // Tweak the redirect field 
  112. add_filter( 'bbp_new_topic_redirect_to', array( $this, 'new_topic_redirect_to' ), 10, 3 ); 
  113. add_filter( 'bbp_new_reply_redirect_to', array( $this, 'new_reply_redirect_to' ), 10, 3 ); 
  114.  
  115. // Map forum/topic/replys permalinks to their groups 
  116. add_filter( 'bbp_get_forum_permalink', array( $this, 'map_forum_permalink_to_group' ), 10, 2 ); 
  117. add_filter( 'bbp_get_topic_permalink', array( $this, 'map_topic_permalink_to_group' ), 10, 2 ); 
  118. add_filter( 'bbp_get_reply_permalink', array( $this, 'map_reply_permalink_to_group' ), 10, 2 ); 
  119.  
  120. // Map reply edit links to their groups 
  121. add_filter( 'bbp_get_reply_edit_url', array( $this, 'map_reply_edit_url_to_group' ), 10, 2 ); 
  122.  
  123. // Map assorted template function permalinks 
  124. add_filter( 'post_link', array( $this, 'post_link' ), 10, 2 ); 
  125. add_filter( 'page_link', array( $this, 'page_link' ), 10, 2 ); 
  126. add_filter( 'post_type_link', array( $this, 'post_type_link' ), 10, 2 ); 
  127.  
  128. // Map group forum activity items to groups 
  129. add_filter( 'bbp_before_record_activity_parse_args', array( $this, 'map_activity_to_group' ) ); 
  130.  
  131. /** Caps **************************************************************/ 
  132.  
  133. // Only add these filters if inside a group forum 
  134. if ( bp_is_single_item() && bp_is_groups_component() && bp_is_current_action( 'forum' ) ) { 
  135.  
  136. // Allow group member to view private/hidden forums 
  137. add_filter( 'bbp_map_meta_caps', array( $this, 'map_group_forum_meta_caps' ), 10, 4 ); 
  138.  
  139. // Group member permissions to view the topic and reply forms 
  140. add_filter( 'bbp_current_user_can_access_create_topic_form', array( $this, 'form_permissions' ) ); 
  141. add_filter( 'bbp_current_user_can_access_create_reply_form', array( $this, 'form_permissions' ) ); 
  142.  
  143. /** 
  144. * The primary display function for group forums 
  145. * 
  146. * @since bbPress (r3746) 
  147. * 
  148. * @param int $group_id ID of the current group. Available only on BP 2.2+. 
  149. */ 
  150. public function display( $group_id = null ) { 
  151.  
  152. // Prevent Topic Parent from appearing 
  153. add_action( 'bbp_theme_before_topic_form_forum', array( $this, 'ob_start' ) ); 
  154. add_action( 'bbp_theme_after_topic_form_forum', array( $this, 'ob_end_clean' ) ); 
  155. add_action( 'bbp_theme_after_topic_form_forum', array( $this, 'topic_parent' ) ); 
  156.  
  157. // Prevent Forum Parent from appearing 
  158. add_action( 'bbp_theme_before_forum_form_parent', array( $this, 'ob_start' ) ); 
  159. add_action( 'bbp_theme_after_forum_form_parent', array( $this, 'ob_end_clean' ) ); 
  160. add_action( 'bbp_theme_after_forum_form_parent', array( $this, 'forum_parent' ) ); 
  161.  
  162. // Hide breadcrumb 
  163. add_filter( 'bbp_no_breadcrumb', '__return_true' ); 
  164.  
  165. $this->display_forums( 0 ); 
  166.  
  167. /** 
  168. * Maybe unset the group forum nav item if group does not have a forum 
  169. * 
  170. * @since bbPress (r4552) 
  171. * 
  172. * @return If not viewing a single group 
  173. */ 
  174. public function maybe_unset_forum_menu() { 
  175.  
  176. // Bail if not viewing a single group 
  177. if ( ! bp_is_group() ) 
  178. return; 
  179.  
  180. // Are forums enabled for this group? 
  181. $checked = bp_get_new_group_enable_forum() || groups_get_groupmeta( bp_get_new_group_id(), 'forum_id' ); 
  182.  
  183. // Tweak the nav item variable based on if group has forum or not 
  184. $this->enable_nav_item = (bool) $checked; 
  185.  
  186. /** 
  187. * Allow group members to have advanced priviledges in group forum topics. 
  188. * 
  189. * @since bbPress (r4434) 
  190. * 
  191. * @param array $caps 
  192. * @param string $cap 
  193. * @param int $user_id 
  194. * @param array $args 
  195. * @return array 
  196. */ 
  197. public function map_group_forum_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) { 
  198.  
  199. switch ( $cap ) { 
  200.  
  201. // If user is a group mmember, allow them to create content. 
  202. case 'read_forum' : 
  203. case 'publish_replies' : 
  204. case 'publish_topics' : 
  205. case 'read_hidden_forums' : 
  206. case 'read_private_forums' : 
  207. if ( bbp_group_is_member() || bbp_group_is_mod() || bbp_group_is_admin() ) { 
  208. $caps = array( 'participate' ); 
  209. break; 
  210.  
  211. // If user is a group mod ar admin, map to participate cap. 
  212. case 'moderate' : 
  213. case 'edit_topic' : 
  214. case 'edit_reply' : 
  215. case 'view_trash' : 
  216. case 'edit_others_replies' : 
  217. case 'edit_others_topics' : 
  218. if ( bbp_group_is_mod() || bbp_group_is_admin() ) { 
  219. $caps = array( 'participate' ); 
  220. break; 
  221.  
  222. // If user is a group admin, allow them to delete topics and replies. 
  223. case 'delete_topic' : 
  224. case 'delete_reply' : 
  225. if ( bbp_group_is_admin() ) { 
  226. $caps = array( 'participate' ); 
  227. break; 
  228.  
  229. return apply_filters( 'bbp_map_group_forum_topic_meta_caps', $caps, $cap, $user_id, $args ); 
  230.  
  231. /** 
  232. * Remove the topic meta cap map, so it doesn't interfere with sidebars. 
  233. * 
  234. * @since bbPress (r4434) 
  235. */ 
  236. public function remove_group_forum_meta_cap_map() { 
  237. remove_filter( 'bbp_map_meta_caps', array( $this, 'map_group_forum_meta_caps' ), 99, 4 ); 
  238.  
  239. /** Edit ******************************************************************/ 
  240.  
  241. /** 
  242. * Show forums and new forum form when editing a group 
  243. * 
  244. * @since bbPress (r3563) 
  245. * @param object $group (the group to edit if in Group Admin UI) 
  246. * @uses is_admin() To check if we're in the Group Admin UI 
  247. * @uses bbp_get_template_part() 
  248. */ 
  249. public function edit_screen( $group = false ) { 
  250. $forum_id = 0; 
  251. $group_id = empty( $group->id ) ? bp_get_new_group_id() : $group->id ; 
  252. $forum_ids = bbp_get_group_forum_ids( $group_id ); 
  253.  
  254. // Get the first forum ID 
  255. if ( !empty( $forum_ids ) ) { 
  256. $forum_id = (int) is_array( $forum_ids ) ? $forum_ids[0] : $forum_ids; 
  257.  
  258. // Should box be checked already? 
  259. $checked = is_admin() ? bp_group_is_forum_enabled( $group ) : bp_get_new_group_enable_forum() || bp_group_is_forum_enabled( bp_get_group_id() ); ?> 
  260.  
  261. <h4><?php esc_html_e( 'Group Forum Settings', 'bbpress' ); ?></h4> 
  262.  
  263. <fieldset> 
  264. <legend class="screen-reader-text"><?php esc_html_e( 'Group Forum Settings', 'bbpress' ); ?></legend> 
  265. <p><?php esc_html_e( 'Create a discussion forum to allow members of this group to communicate in a structured, bulletin-board style fashion.', 'bbpress' ); ?></p> 
  266.  
  267. <div class="field-group"> 
  268. <div class="checkbox"> 
  269. <label><input type="checkbox" name="bbp-edit-group-forum" id="bbp-edit-group-forum" value="1"<?php checked( $checked ); ?> /> <?php esc_html_e( 'Yes. I want this group to have a forum.', 'bbpress' ); ?></label> 
  270. </div> 
  271.  
  272. <p class="description"><?php esc_html_e( 'Saying no will not delete existing forum content.', 'bbpress' ); ?></p> 
  273. </div> 
  274.  
  275. <?php if ( bbp_is_user_keymaster() ) : ?> 
  276. <div class="field-group"> 
  277. <label for="bbp_group_forum_id"><?php esc_html_e( 'Group Forum:', 'bbpress' ); ?></label> 
  278. <?php 
  279. bbp_dropdown( array( 
  280. 'select_id' => 'bbp_group_forum_id',  
  281. 'show_none' => __( '(No Forum)', 'bbpress' ),  
  282. 'selected' => $forum_id 
  283. ) ); 
  284. ?> 
  285. <p class="description"><?php esc_html_e( 'Network administrators can reconfigure which forum belongs to this group.', 'bbpress' ); ?></p> 
  286. </div> 
  287. <?php endif; ?> 
  288.  
  289. <?php if ( !is_admin() ) : ?> 
  290. <input type="submit" value="<?php esc_attr_e( 'Save Settings', 'bbpress' ); ?>" /> 
  291. <?php endif; ?> 
  292.  
  293. </fieldset> 
  294.  
  295. <?php 
  296.  
  297. // Verify intent 
  298. if ( is_admin() ) { 
  299. wp_nonce_field( 'groups_edit_save_' . $this->slug, 'forum_group_admin_ui' ); 
  300. } else { 
  301. wp_nonce_field( 'groups_edit_save_' . $this->slug ); 
  302.  
  303. /** 
  304. * Save the Group Forum data on edit 
  305. * 
  306. * @since bbPress (r3465) 
  307. * @param int $group_id (to handle Group Admin UI hook bp_group_admin_edit_after ) 
  308. * @uses bbp_new_forum_handler() To check for forum creation 
  309. * @uses bbp_edit_forum_handler() To check for forum edit 
  310. */ 
  311. public function edit_screen_save( $group_id = 0 ) { 
  312.  
  313. // Bail if not a POST action 
  314. if ( ! bbp_is_post_request() ) 
  315. return; 
  316.  
  317. // Admin Nonce check 
  318. if ( is_admin() ) { 
  319. check_admin_referer( 'groups_edit_save_' . $this->slug, 'forum_group_admin_ui' ); 
  320.  
  321. // Theme-side Nonce check 
  322. } elseif ( ! bbp_verify_nonce_request( 'groups_edit_save_' . $this->slug ) ) { 
  323. bbp_add_error( 'bbp_edit_group_forum_screen_save', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); 
  324. return; 
  325.  
  326. $edit_forum = !empty( $_POST['bbp-edit-group-forum'] ) ? true : false; 
  327. $forum_id = 0; 
  328. $group_id = !empty( $group_id ) ? $group_id : bp_get_current_group_id(); 
  329.  
  330. // Keymasters have the ability to reconfigure forums 
  331. if ( bbp_is_user_keymaster() ) { 
  332. $forum_ids = ! empty( $_POST['bbp_group_forum_id'] ) ? (array) (int) $_POST['bbp_group_forum_id'] : array(); 
  333.  
  334. // Use the existing forum IDs 
  335. } else { 
  336. $forum_ids = array_values( bbp_get_group_forum_ids( $group_id ) ); 
  337.  
  338. // Normalize group forum relationships now 
  339. if ( !empty( $forum_ids ) ) { 
  340.  
  341. // Loop through forums, and make sure they exist 
  342. foreach ( $forum_ids as $forum_id ) { 
  343.  
  344. // Look for forum 
  345. $forum = bbp_get_forum( $forum_id ); 
  346.  
  347. // No forum exists, so break the relationship 
  348. if ( empty( $forum ) ) { 
  349. $this->remove_forum( array( 'forum_id' => $forum_id ) ); 
  350. unset( $forum_ids[$forum_id] ); 
  351.  
  352. // No support for multiple forums yet 
  353. $forum_id = (int) ( is_array( $forum_ids ) ? $forum_ids[0] : $forum_ids ); 
  354.  
  355. // Update the group ID and forum ID relationships 
  356. bbp_update_group_forum_ids( $group_id, (array) $forum_ids ); 
  357. bbp_update_forum_group_ids( $forum_id, (array) $group_id ); 
  358.  
  359. // Update the group forum setting 
  360. $group = $this->toggle_group_forum( $group_id, $edit_forum ); 
  361.  
  362. // Create a new forum 
  363. if ( empty( $forum_id ) && ( true === $edit_forum ) ) { 
  364.  
  365. // Set the default forum status 
  366. switch ( $group->status ) { 
  367. case 'hidden' : 
  368. $status = bbp_get_hidden_status_id(); 
  369. break; 
  370. case 'private' : 
  371. $status = bbp_get_private_status_id(); 
  372. break; 
  373. case 'public' : 
  374. default : 
  375. $status = bbp_get_public_status_id(); 
  376. break; 
  377.  
  378. // Create the initial forum 
  379. $forum_id = bbp_insert_forum( array( 
  380. 'post_parent' => bbp_get_group_forums_root_id(),  
  381. 'post_title' => $group->name,  
  382. 'post_content' => $group->description,  
  383. 'post_status' => $status 
  384. ) ); 
  385.  
  386. // Setup forum args with forum ID 
  387. $new_forum_args = array( 'forum_id' => $forum_id ); 
  388.  
  389. // If in admin, also include the group ID 
  390. if ( is_admin() && !empty( $group_id ) ) { 
  391. $new_forum_args['group_id'] = $group_id; 
  392.  
  393. // Run the BP-specific functions for new groups 
  394. $this->new_forum( $new_forum_args ); 
  395.  
  396. // Redirect after save when not in admin 
  397. if ( !is_admin() ) { 
  398. bp_core_redirect( trailingslashit( bp_get_group_permalink( buddypress()->groups->current_group ) . '/admin/' . $this->slug ) ); 
  399.  
  400. /** 
  401. * Adds a metabox to BuddyPress Group Admin UI 
  402. * 
  403. * @since bbPress (r4814) 
  404. * 
  405. * @uses add_meta_box 
  406. * @uses BBP_Forums_Group_Extension::group_admin_ui_display_metabox() To display the edit screen 
  407. */ 
  408. public function group_admin_ui_edit_screen() { 
  409. add_meta_box( 
  410. 'bbpress_group_admin_ui_meta_box',  
  411. _x( 'Discussion Forum', 'group admin edit screen', 'bbpress' ),  
  412. array( $this, 'group_admin_ui_display_metabox' ),  
  413. get_current_screen()->id,  
  414. 'side',  
  415. 'core' 
  416. ); 
  417.  
  418. /** 
  419. * Displays the bbPress metabox in BuddyPress Group Admin UI 
  420. * 
  421. * @since bbPress (r4814) 
  422. * 
  423. * @param object $item (group object) 
  424. * @uses add_meta_box 
  425. * @uses BBP_Forums_Group_Extension::edit_screen() To get the html 
  426. */ 
  427. public function group_admin_ui_display_metabox( $item ) { 
  428. $this->edit_screen( $item ); 
  429.  
  430. /** Create ****************************************************************/ 
  431.  
  432. /** 
  433. * Show forums and new forum form when creating a group 
  434. * 
  435. * @since bbPress (r3465) 
  436. */ 
  437. public function create_screen( $group_id = 0 ) { 
  438.  
  439. // Bail if not looking at this screen 
  440. if ( !bp_is_group_creation_step( $this->slug ) ) 
  441. return false; 
  442.  
  443. // Check for possibly empty group_id 
  444. if ( empty( $group_id ) ) { 
  445. $group_id = bp_get_new_group_id(); 
  446.  
  447. $checked = bp_get_new_group_enable_forum() || groups_get_groupmeta( $group_id, 'forum_id' ); ?> 
  448.  
  449. <h4><?php esc_html_e( 'Group Forum', 'bbpress' ); ?></h4> 
  450.  
  451. <p><?php esc_html_e( 'Create a discussion forum to allow members of this group to communicate in a structured, bulletin-board style fashion.', 'bbpress' ); ?></p> 
  452.  
  453. <div class="checkbox"> 
  454. <label><input type="checkbox" name="bbp-create-group-forum" id="bbp-create-group-forum" value="1"<?php checked( $checked ); ?> /> <?php esc_html_e( 'Yes. I want this group to have a forum.', 'bbpress' ); ?></label> 
  455. </div> 
  456.  
  457. <?php 
  458.  
  459. /** 
  460. * Save the Group Forum data on create 
  461. * 
  462. * @since bbPress (r3465) 
  463. */ 
  464. public function create_screen_save( $group_id = 0 ) { 
  465.  
  466. // Nonce check 
  467. if ( ! bbp_verify_nonce_request( 'groups_create_save_' . $this->slug ) ) { 
  468. bbp_add_error( 'bbp_create_group_forum_screen_save', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); 
  469. return; 
  470.  
  471. // Check for possibly empty group_id 
  472. if ( empty( $group_id ) ) { 
  473. $group_id = bp_get_new_group_id(); 
  474.  
  475. $create_forum = !empty( $_POST['bbp-create-group-forum'] ) ? true : false; 
  476. $forum_id = 0; 
  477. $forum_ids = bbp_get_group_forum_ids( $group_id ); 
  478.  
  479. if ( !empty( $forum_ids ) ) 
  480. $forum_id = (int) is_array( $forum_ids ) ? $forum_ids[0] : $forum_ids; 
  481.  
  482. // Create a forum, or not 
  483. switch ( $create_forum ) { 
  484. case true : 
  485.  
  486. // Bail if initial content was already created 
  487. if ( !empty( $forum_id ) ) 
  488. return; 
  489.  
  490. // Set the default forum status 
  491. switch ( bp_get_new_group_status() ) { 
  492. case 'hidden' : 
  493. $status = bbp_get_hidden_status_id(); 
  494. break; 
  495. case 'private' : 
  496. $status = bbp_get_private_status_id(); 
  497. break; 
  498. case 'public' : 
  499. default : 
  500. $status = bbp_get_public_status_id(); 
  501. break; 
  502.  
  503. // Create the initial forum 
  504. $forum_id = bbp_insert_forum( array( 
  505. 'post_parent' => bbp_get_group_forums_root_id(),  
  506. 'post_title' => bp_get_new_group_name(),  
  507. 'post_content' => bp_get_new_group_description(),  
  508. 'post_status' => $status 
  509. ) ); 
  510.  
  511. // Run the BP-specific functions for new groups 
  512. $this->new_forum( array( 'forum_id' => $forum_id ) ); 
  513.  
  514. // Update forum active 
  515. groups_update_groupmeta( bp_get_new_group_id(), '_bbp_forum_enabled_' . $forum_id, true ); 
  516.  
  517. // Toggle forum on 
  518. $this->toggle_group_forum( bp_get_new_group_id(), true ); 
  519.  
  520. break; 
  521. case false : 
  522.  
  523. // Forum was created but is now being undone 
  524. if ( !empty( $forum_id ) ) { 
  525.  
  526. // Delete the forum 
  527. wp_delete_post( $forum_id, true ); 
  528.  
  529. // Delete meta values 
  530. groups_delete_groupmeta( bp_get_new_group_id(), 'forum_id' ); 
  531. groups_delete_groupmeta( bp_get_new_group_id(), '_bbp_forum_enabled_' . $forum_id ); 
  532.  
  533. // Toggle forum off 
  534. $this->toggle_group_forum( bp_get_new_group_id(), false ); 
  535.  
  536. break; 
  537.  
  538. /** 
  539. * Used to start an output buffer 
  540. */ 
  541. public function ob_start() { 
  542. ob_start(); 
  543.  
  544. /** 
  545. * Used to end an output buffer 
  546. */ 
  547. public function ob_end_clean() { 
  548. ob_end_clean(); 
  549.  
  550. /** 
  551. * Creating a group forum or category (including root for group) 
  552. * 
  553. * @since bbPress (r3653) 
  554. * @param type $forum_args 
  555. * @uses bbp_get_forum_id() 
  556. * @uses bp_get_current_group_id() 
  557. * @uses bbp_add_forum_id_to_group() 
  558. * @uses bbp_add_group_id_to_forum() 
  559. * @return if no forum_id is available 
  560. */ 
  561. public function new_forum( $forum_args = array() ) { 
  562.  
  563. // Bail if no forum_id was passed 
  564. if ( empty( $forum_args['forum_id'] ) ) 
  565. return; 
  566.  
  567. // Validate forum_id 
  568. $forum_id = bbp_get_forum_id( $forum_args['forum_id'] ); 
  569. $group_id = !empty( $forum_args['group_id'] ) ? $forum_args['group_id'] : bp_get_current_group_id(); 
  570.  
  571. bbp_add_forum_id_to_group( $group_id, $forum_id ); 
  572. bbp_add_group_id_to_forum( $forum_id, $group_id ); 
  573.  
  574. /** 
  575. * Removing a group forum or category (including root for group) 
  576. * 
  577. * @since bbPress (r3653) 
  578. * @param type $forum_args 
  579. * @uses bbp_get_forum_id() 
  580. * @uses bp_get_current_group_id() 
  581. * @uses bbp_add_forum_id_to_group() 
  582. * @uses bbp_add_group_id_to_forum() 
  583. * @return if no forum_id is available 
  584. */ 
  585. public function remove_forum( $forum_args = array() ) { 
  586.  
  587. // Bail if no forum_id was passed 
  588. if ( empty( $forum_args['forum_id'] ) ) 
  589. return; 
  590.  
  591. // Validate forum_id 
  592. $forum_id = bbp_get_forum_id( $forum_args['forum_id'] ); 
  593. $group_id = !empty( $forum_args['group_id'] ) ? $forum_args['group_id'] : bp_get_current_group_id(); 
  594.  
  595. bbp_remove_forum_id_from_group( $group_id, $forum_id ); 
  596. bbp_remove_group_id_from_forum( $forum_id, $group_id ); 
  597.  
  598. /** 
  599. * Listening to BuddyPress Group deletion to remove the forum 
  600. * 
  601. * @param int $group_id The group ID 
  602. * @uses bbp_get_group_forum_ids() 
  603. * @uses BBP_Forums_Group_Extension::remove_forum() 
  604. */ 
  605. public function disconnect_forum_from_group( $group_id = 0 ) { 
  606.  
  607. // Bail if no group ID available 
  608. if ( empty( $group_id ) ) { 
  609. return; 
  610.  
  611. // Get the forums for the current group 
  612. $forum_ids = bbp_get_group_forum_ids( $group_id ); 
  613.  
  614. // Use the first forum ID 
  615. if ( empty( $forum_ids ) ) 
  616. return; 
  617.  
  618. // Get the first forum ID 
  619. $forum_id = (int) is_array( $forum_ids ) ? $forum_ids[0] : $forum_ids; 
  620. $this->remove_forum( array( 
  621. 'forum_id' => $forum_id,  
  622. 'group_id' => $group_id 
  623. ) ); 
  624.  
  625. /** 
  626. * Toggle the enable_forum group setting on or off 
  627. * 
  628. * @since bbPress (r4612) 
  629. * 
  630. * @param int $group_id The group to toggle 
  631. * @param bool $enabled True for on, false for off 
  632. * @uses groups_get_group() To get the group to toggle 
  633. * @return False if group is not found, otherwise return the group 
  634. */ 
  635. public function toggle_group_forum( $group_id = 0, $enabled = false ) { 
  636.  
  637. // Get the group 
  638. $group = groups_get_group( array( 'group_id' => $group_id ) ); 
  639.  
  640. // Bail if group cannot be found 
  641. if ( empty( $group ) ) 
  642. return false; 
  643.  
  644. // Set forum enabled status 
  645. $group->enable_forum = (int) $enabled; 
  646.  
  647. // Save the group 
  648. $group->save(); 
  649.  
  650. // Maybe disconnect forum from group 
  651. if ( empty( $enabled ) ) { 
  652. $this->disconnect_forum_from_group( $group_id ); 
  653.  
  654. // Update bbPress' internal private and forum ID variables 
  655. bbp_repair_forum_visibility(); 
  656.  
  657. // Return the group 
  658. return $group; 
  659.  
  660. /** Display Methods *******************************************************/ 
  661.  
  662. /** 
  663. * Output the forums for a group in the edit screens 
  664. * 
  665. * As of right now, bbPress only supports 1-to-1 group forum relationships. 
  666. * In the future, many-to-many should be allowed. 
  667. * 
  668. * @since bbPress (r3653) 
  669. * @uses bp_get_current_group_id() 
  670. * @uses bbp_get_group_forum_ids() 
  671. * @uses bbp_has_forums() 
  672. * @uses bbp_get_template_part() 
  673. */ 
  674. public function display_forums( $offset = 0 ) { 
  675. global $wp_query; 
  676.  
  677. // Allow actions immediately before group forum output 
  678. do_action( 'bbp_before_group_forum_display' ); 
  679.  
  680. // Load up bbPress once 
  681. $bbp = bbpress(); 
  682.  
  683. /** Query Resets ******************************************************/ 
  684.  
  685. // Forum data 
  686. $forum_action = bp_action_variable( $offset ); 
  687. $forum_ids = bbp_get_group_forum_ids( bp_get_current_group_id() ); 
  688. $forum_id = array_shift( $forum_ids ); 
  689.  
  690. // Always load up the group forum 
  691. bbp_has_forums( array( 
  692. 'p' => $forum_id,  
  693. 'post_parent' => null 
  694. ) ); 
  695.  
  696. // Set the global forum ID 
  697. $bbp->current_forum_id = $forum_id; 
  698.  
  699. // Assume forum query 
  700. bbp_set_query_name( 'bbp_single_forum' ); ?> 
  701.  
  702. <div id="bbpress-forums"> 
  703.  
  704. <?php switch ( $forum_action ) : 
  705.  
  706. /** Single Forum **********************************************/ 
  707.  
  708. case false : 
  709. case 'page' : 
  710.  
  711. // Strip the super stickies from topic query 
  712. add_filter( 'bbp_get_super_stickies', array( $this, 'no_super_stickies' ), 10, 1 ); 
  713.  
  714. // Unset the super sticky option on topic form 
  715. add_filter( 'bbp_get_topic_types', array( $this, 'unset_super_sticky' ), 10, 1 ); 
  716.  
  717. // Query forums and show them if they exist 
  718. if ( bbp_forums() ) : 
  719.  
  720. // Setup the forum 
  721. bbp_the_forum(); ?> 
  722.  
  723. <h3><?php bbp_forum_title(); ?></h3> 
  724.  
  725. <?php bbp_get_template_part( 'content', 'single-forum' ); 
  726.  
  727. // No forums found 
  728. else : ?> 
  729.  
  730. <div id="message" class="info"> 
  731. <p><?php esc_html_e( 'This group does not currently have a forum.', 'bbpress' ); ?></p> 
  732. </div> 
  733.  
  734. <?php endif; 
  735.  
  736. break; 
  737.  
  738. /** Single Topic **********************************************/ 
  739.  
  740. case $this->topic_slug : 
  741.  
  742. // hide the 'to front' admin links 
  743. add_filter( 'bbp_get_topic_stick_link', array( $this, 'hide_super_sticky_admin_link' ), 10, 2 ); 
  744.  
  745. // Get the topic 
  746. bbp_has_topics( array( 
  747. 'name' => bp_action_variable( $offset + 1 ),  
  748. 'posts_per_page' => 1,  
  749. 'show_stickies' => false 
  750. ) ); 
  751.  
  752. // If no topic, 404 
  753. if ( ! bbp_topics() ) { 
  754. bp_do_404( bbp_get_forum_permalink( $forum_id ) ); ?> 
  755. <h3><?php bbp_forum_title(); ?></h3> 
  756. <?php bbp_get_template_part( 'feedback', 'no-topics' ); 
  757. return; 
  758.  
  759. // Setup the topic 
  760. bbp_the_topic(); ?> 
  761.  
  762. <h3><?php bbp_topic_title(); ?></h3> 
  763.  
  764. <?php 
  765.  
  766. // Topic edit 
  767. if ( bp_action_variable( $offset + 2 ) === bbp_get_edit_rewrite_id() ) : 
  768.  
  769. // Unset the super sticky link on edit topic template 
  770. add_filter( 'bbp_get_topic_types', array( $this, 'unset_super_sticky' ), 10, 1 ); 
  771.  
  772. // Set the edit switches 
  773. $wp_query->bbp_is_edit = true; 
  774. $wp_query->bbp_is_topic_edit = true; 
  775.  
  776. // Setup the global forum ID 
  777. $bbp->current_topic_id = get_the_ID(); 
  778.  
  779. // Merge 
  780. if ( !empty( $_GET['action'] ) && 'merge' === $_GET['action'] ) : 
  781. bbp_set_query_name( 'bbp_topic_merge' ); 
  782. bbp_get_template_part( 'form', 'topic-merge' ); 
  783.  
  784. // Split 
  785. elseif ( !empty( $_GET['action'] ) && 'split' === $_GET['action'] ) : 
  786. bbp_set_query_name( 'bbp_topic_split' ); 
  787. bbp_get_template_part( 'form', 'topic-split' ); 
  788.  
  789. // Edit 
  790. else : 
  791. bbp_set_query_name( 'bbp_topic_form' ); 
  792. bbp_get_template_part( 'form', 'topic' ); 
  793.  
  794. endif; 
  795.  
  796. // Single Topic 
  797. else: 
  798. bbp_set_query_name( 'bbp_single_topic' ); 
  799. bbp_get_template_part( 'content', 'single-topic' ); 
  800. endif; 
  801. break; 
  802.  
  803. /** Single Reply **********************************************/ 
  804.  
  805. case $this->reply_slug : 
  806.  
  807. // Get the reply 
  808. bbp_has_replies( array( 
  809. 'name' => bp_action_variable( $offset + 1 ),  
  810. 'posts_per_page' => 1 
  811. ) ); 
  812.  
  813. // If no topic, 404 
  814. if ( ! bbp_replies() ) { 
  815. bp_do_404( bbp_get_forum_permalink( $forum_id ) ); ?> 
  816. <h3><?php bbp_forum_title(); ?></h3> 
  817. <?php bbp_get_template_part( 'feedback', 'no-replies' ); 
  818. return; 
  819.  
  820. // Setup the reply 
  821. bbp_the_reply(); ?> 
  822.  
  823. <h3><?php bbp_reply_title(); ?></h3> 
  824.  
  825. <?php if ( bp_action_variable( $offset + 2 ) === bbp_get_edit_rewrite_id() ) : 
  826.  
  827. // Set the edit switches 
  828. $wp_query->bbp_is_edit = true; 
  829. $wp_query->bbp_is_reply_edit = true; 
  830.  
  831. // Setup the global reply ID 
  832. $bbp->current_reply_id = get_the_ID(); 
  833.  
  834. // Move 
  835. if ( !empty( $_GET['action'] ) && ( 'move' === $_GET['action'] ) ) : 
  836. bbp_set_query_name( 'bbp_reply_move' ); 
  837. bbp_get_template_part( 'form', 'reply-move' ); 
  838.  
  839. // Edit 
  840. else : 
  841. bbp_set_query_name( 'bbp_reply_form' ); 
  842. bbp_get_template_part( 'form', 'reply' ); 
  843. endif; 
  844. endif; 
  845. break; 
  846. endswitch; 
  847.  
  848. // Reset the query 
  849. wp_reset_query(); ?> 
  850.  
  851. </div> 
  852.  
  853. <?php 
  854.  
  855. // Allow actions immediately after group forum output 
  856. do_action( 'bbp_after_group_forum_display' ); 
  857.  
  858. /** Super sticky filters ***************************************************/ 
  859.  
  860. /** 
  861. * Strip super stickies from the topic query 
  862. * 
  863. * @since bbPress (r4810) 
  864. * @access private 
  865. * @param array $super the super sticky post ID's 
  866. * @return array (empty) 
  867. */ 
  868. public function no_super_stickies( $super = array() ) { 
  869. $super = array(); 
  870. return $super; 
  871.  
  872. /** 
  873. * Unset the type super sticky from topic type 
  874. * 
  875. * @since bbPress (r4810) 
  876. * @access private 
  877. * @param array $args 
  878. * @return array $args without the to-front link 
  879. */ 
  880. public function unset_super_sticky( $args = array() ) { 
  881. if ( isset( $args['super'] ) ) { 
  882. unset( $args['super'] ); 
  883. return $args; 
  884.  
  885. /** 
  886. * Ugly preg_replace to hide the to front admin link 
  887. * 
  888. * @since bbPress (r4810) 
  889. * @access private 
  890. * @param string $retval 
  891. * @param array $args 
  892. * @return string $retval without the to-front link 
  893. */ 
  894. public function hide_super_sticky_admin_link( $retval = '', $args = array() ) { 
  895. if ( strpos( $retval, '(' ) ) { 
  896. $retval = preg_replace( '/(\(.+?)+(\))/i', '', $retval ); 
  897.  
  898. return $retval; 
  899.  
  900. /** Redirect Helpers ******************************************************/ 
  901.  
  902. /** 
  903. * Redirect to the group forum screen 
  904. * 
  905. * @since bbPress (r3653) 
  906. * @param str $redirect_url 
  907. * @param str $redirect_to 
  908. */ 
  909. public function new_topic_redirect_to( $redirect_url = '', $redirect_to = '', $topic_id = 0 ) { 
  910. if ( bp_is_group() ) { 
  911. $topic = bbp_get_topic( $topic_id ); 
  912. $topic_hash = '#post-' . $topic_id; 
  913. $redirect_url = trailingslashit( bp_get_group_permalink( groups_get_current_group() ) ) . trailingslashit( $this->slug ) . trailingslashit( $this->topic_slug ) . trailingslashit( $topic->post_name ) . $topic_hash; 
  914.  
  915. return $redirect_url; 
  916.  
  917. /** 
  918. * Redirect to the group forum screen 
  919. * 
  920. * @since bbPress (r3653) 
  921. */ 
  922. public function new_reply_redirect_to( $redirect_url = '', $redirect_to = '', $reply_id = 0 ) { 
  923. global $wp_rewrite; 
  924.  
  925. if ( bp_is_group() ) { 
  926. $topic_id = bbp_get_reply_topic_id( $reply_id ); 
  927. $topic = bbp_get_topic( $topic_id ); 
  928. $reply_position = bbp_get_reply_position( $reply_id, $topic_id ); 
  929. $reply_page = ceil( (int) $reply_position / (int) bbp_get_replies_per_page() ); 
  930. $reply_hash = '#post-' . $reply_id; 
  931. $topic_url = trailingslashit( bp_get_group_permalink( groups_get_current_group() ) ) . trailingslashit( $this->slug ) . trailingslashit( $this->topic_slug ) . trailingslashit( $topic->post_name ); 
  932.  
  933. // Don't include pagination if on first page 
  934. if ( 1 >= $reply_page ) { 
  935. $redirect_url = trailingslashit( $topic_url ) . $reply_hash; 
  936.  
  937. // Include pagination 
  938. } else { 
  939. $redirect_url = trailingslashit( $topic_url ) . trailingslashit( $wp_rewrite->pagination_base ) . trailingslashit( $reply_page ) . $reply_hash; 
  940.  
  941. // Add topic view query arg back to end if it is set 
  942. if ( bbp_get_view_all() ) { 
  943. $redirect_url = bbp_add_view_all( $redirect_url ); 
  944.  
  945. return $redirect_url; 
  946.  
  947. /** 
  948. * Redirect to the group admin forum edit screen 
  949. * 
  950. * @since bbPress (r3653) 
  951. * @uses groups_get_current_group() 
  952. * @uses bp_is_group_admin_screen() 
  953. * @uses trailingslashit() 
  954. * @uses bp_get_root_domain() 
  955. * @uses bp_get_groups_root_slug() 
  956. */ 
  957. public function edit_redirect_to( $redirect_url = '' ) { 
  958.  
  959. // Get the current group, if there is one 
  960. $group = groups_get_current_group(); 
  961.  
  962. // If this is a group of any kind, empty out the redirect URL 
  963. if ( bp_is_group_admin_screen( $this->slug ) ) 
  964. $redirect_url = trailingslashit( bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/' . $group->slug . '/admin/' . $this->slug ); 
  965.  
  966. return $redirect_url; 
  967.  
  968. /** Form Helpers **********************************************************/ 
  969.  
  970. public function forum_parent() { 
  971. ?> 
  972.  
  973. <input type="hidden" name="bbp_forum_parent_id" id="bbp_forum_parent_id" value="<?php bbp_group_forums_root_id(); ?>" /> 
  974.  
  975. <?php 
  976.  
  977. public function topic_parent() { 
  978.  
  979. $forum_ids = bbp_get_group_forum_ids( bp_get_current_group_id() ); ?> 
  980.  
  981. <p> 
  982. <label for="bbp_forum_id"><?php esc_html_e( 'Forum:', 'bbpress' ); ?></label><br /> 
  983. <?php bbp_dropdown( array( 'include' => $forum_ids, 'selected' => bbp_get_form_topic_forum() ) ); ?> 
  984. </p> 
  985.  
  986. <?php 
  987.  
  988. /** 
  989. * Permissions to view the 'New Topic'/'Reply To' form in a BuddyPress group. 
  990. * 
  991. * @since bbPress (r4608) 
  992. * 
  993. * @param bool $retval Are we allowed to view the reply form? 
  994. * @uses bp_is_group() To determine if we're on a group page 
  995. * @uses is_user_logged_in() To determine if a user is logged in. 
  996. * @uses bbp_is_user_keymaster() Is the current user a keymaster? 
  997. * @uses bbp_group_is_member() Is the current user a member of the group? 
  998. * @uses bbp_group_is_user_banned() Is the current user banned from the group? 
  999. * 
  1000. * @return bool 
  1001. */ 
  1002. public function form_permissions( $retval = false ) { 
  1003.  
  1004. // Bail if not a group 
  1005. if ( ! bp_is_group() ) { 
  1006. return $retval; 
  1007.  
  1008. // Bail if user is not logged in 
  1009. if ( ! is_user_logged_in() ) { 
  1010. return $retval; 
  1011.  
  1012. // Keymasters can always pass go 
  1013. } elseif ( bbp_is_user_keymaster() ) { 
  1014. $retval = true; 
  1015.  
  1016. // Non-members cannot see forms 
  1017. } elseif ( ! bbp_group_is_member() ) { 
  1018. $retval = false; 
  1019.  
  1020. // Banned users cannot see forms 
  1021. } elseif ( bbp_group_is_banned() ) { 
  1022. $retval = false; 
  1023.  
  1024. return $retval; 
  1025.  
  1026. /** 
  1027. * Add a hidden input field on the group settings page if the group forum is 
  1028. * enabled. 
  1029. * 
  1030. * Due to the way BuddyPress' group admin settings page saves its settings,  
  1031. * we need to let BP know that bbPress added a forum. 
  1032. * 
  1033. * @since bbPress (r5026) 
  1034. * 
  1035. * @link http://bbpress.trac.wordpress.org/ticket/2339/ 
  1036. * @see groups_screen_group_admin_settings() 
  1037. */ 
  1038. public function group_settings_hidden_field() { 
  1039.  
  1040. // if a forum is not enabled, we don't need to add this field 
  1041. if ( ! bp_group_is_forum_enabled() ) 
  1042. return; ?> 
  1043.  
  1044. <input type="hidden" name="group-show-forum" id="group-show-forum" value="1" /> 
  1045.  
  1046. <?php 
  1047.  
  1048. /** Permalink Mappers *****************************************************/ 
  1049.  
  1050. /** 
  1051. * Maybe map a bbPress forum/topic/reply permalink to the corresponding group 
  1052. * 
  1053. * @param int $post_id 
  1054. * @uses get_post() 
  1055. * @uses bbp_is_reply() 
  1056. * @uses bbp_get_reply_topic_id() 
  1057. * @uses bbp_get_reply_forum_id() 
  1058. * @uses bbp_is_topic() 
  1059. * @uses bbp_get_topic_forum_id() 
  1060. * @uses bbp_is_forum() 
  1061. * @uses get_post_field() 
  1062. * @uses bbp_get_forum_group_ids() 
  1063. * @uses groups_get_group() 
  1064. * @uses bp_get_group_admin_permalink() 
  1065. * @uses bp_get_group_permalink() 
  1066. * @return Bail early if not a group forum post 
  1067. * @return string 
  1068. */ 
  1069. private function maybe_map_permalink_to_group( $post_id = 0, $url = false ) { 
  1070.  
  1071. switch ( get_post_type( $post_id ) ) { 
  1072.  
  1073. // Reply 
  1074. case bbp_get_reply_post_type() : 
  1075. $topic_id = bbp_get_reply_topic_id( $post_id ); 
  1076. $forum_id = bbp_get_reply_forum_id( $post_id ); 
  1077. $url_end = trailingslashit( $this->reply_slug ) . get_post_field( 'post_name', $post_id ); 
  1078. break; 
  1079.  
  1080. // Topic 
  1081. case bbp_get_topic_post_type() : 
  1082. $topic_id = $post_id; 
  1083. $forum_id = bbp_get_topic_forum_id( $post_id ); 
  1084. $url_end = trailingslashit( $this->topic_slug ) . get_post_field( 'post_name', $post_id ); 
  1085. break; 
  1086.  
  1087. // Forum 
  1088. case bbp_get_forum_post_type() : 
  1089. $forum_id = $post_id; 
  1090. $url_end = ''; //get_post_field( 'post_name', $post_id ); 
  1091. break; 
  1092.  
  1093. // Unknown 
  1094. default : 
  1095. return $url; 
  1096. break; 
  1097.  
  1098. // Get group ID's for this forum 
  1099. $group_ids = bbp_get_forum_group_ids( $forum_id ); 
  1100.  
  1101. // Bail if the post isn't associated with a group 
  1102. if ( empty( $group_ids ) ) 
  1103. return $url; 
  1104.  
  1105. // @todo Multiple group forums/forum groups 
  1106. $group_id = $group_ids[0]; 
  1107. $group = groups_get_group( array( 'group_id' => $group_id ) ); 
  1108.  
  1109. if ( bp_is_group_admin_screen( $this->slug ) ) { 
  1110. $group_permalink = trailingslashit( bp_get_group_admin_permalink( $group ) ); 
  1111. } else { 
  1112. $group_permalink = trailingslashit( bp_get_group_permalink( $group ) ); 
  1113.  
  1114. return trailingslashit( trailingslashit( $group_permalink . $this->slug ) . $url_end ); 
  1115.  
  1116. /** 
  1117. * Map a forum permalink to its corresponding group 
  1118. * 
  1119. * @since bbPress (r3802) 
  1120. * @param string $url 
  1121. * @param int $forum_id 
  1122. * @uses maybe_map_permalink_to_group() 
  1123. * @return string 
  1124. */ 
  1125. public function map_forum_permalink_to_group( $url, $forum_id ) { 
  1126. return $this->maybe_map_permalink_to_group( $forum_id, $url ); 
  1127.  
  1128. /** 
  1129. * Map a topic permalink to its group forum 
  1130. * 
  1131. * @since bbPress (r3802) 
  1132. * @param string $url 
  1133. * @param int $topic_id 
  1134. * @uses maybe_map_permalink_to_group() 
  1135. * @return string 
  1136. */ 
  1137. public function map_topic_permalink_to_group( $url, $topic_id ) { 
  1138. return $this->maybe_map_permalink_to_group( $topic_id, $url ); 
  1139.  
  1140. /** 
  1141. * Map a reply permalink to its group forum 
  1142. * 
  1143. * @since bbPress (r3802) 
  1144. * @param string $url 
  1145. * @param int $reply_id 
  1146. * @uses maybe_map_permalink_to_group() 
  1147. * @return string 
  1148. */ 
  1149. public function map_reply_permalink_to_group( $url, $reply_id ) { 
  1150. return $this->maybe_map_permalink_to_group( bbp_get_reply_topic_id( $reply_id ), $url ); 
  1151.  
  1152. /** 
  1153. * Map a reply edit link to its group forum 
  1154. * 
  1155. * @param string $url 
  1156. * @param int $reply_id 
  1157. * @uses maybe_map_permalink_to_group() 
  1158. * @return string 
  1159. */ 
  1160. public function map_reply_edit_url_to_group( $url, $reply_id ) { 
  1161. $new = $this->maybe_map_permalink_to_group( $reply_id ); 
  1162.  
  1163. if ( empty( $new ) ) 
  1164. return $url; 
  1165.  
  1166. return trailingslashit( $new ) . bbpress()->edit_id . '/'; 
  1167.  
  1168. /** 
  1169. * Map a post link to its group forum 
  1170. * 
  1171. * @param string $url 
  1172. * @param obj $post 
  1173. * @param boolean $leavename 
  1174. * @uses maybe_map_permalink_to_group() 
  1175. * @return string 
  1176. */ 
  1177. public function post_link( $url, $post ) { 
  1178. return $this->maybe_map_permalink_to_group( $post->ID, $url ); 
  1179.  
  1180. /** 
  1181. * Map a page link to its group forum 
  1182. * 
  1183. * @param string $url 
  1184. * @param int $post_id 
  1185. * @param $sample 
  1186. * @uses maybe_map_permalink_to_group() 
  1187. * @return string 
  1188. */ 
  1189. public function page_link( $url, $post_id ) { 
  1190. return $this->maybe_map_permalink_to_group( $post_id, $url ); 
  1191.  
  1192. /** 
  1193. * Map a custom post type link to its group forum 
  1194. * 
  1195. * @param string $url 
  1196. * @param obj $post 
  1197. * @param $leavename 
  1198. * @param $sample 
  1199. * @uses maybe_map_permalink_to_group() 
  1200. * @return string 
  1201. */ 
  1202. public function post_type_link( $url, $post ) { 
  1203. return $this->maybe_map_permalink_to_group( $post->ID, $url ); 
  1204.  
  1205. /** 
  1206. * Fix pagination of topics on forum view 
  1207. * 
  1208. * @param array $args 
  1209. * @global $wp_rewrite 
  1210. * @uses bbp_get_forum_id() 
  1211. * @uses maybe_map_permalink_to_group 
  1212. * @return array 
  1213. */ 
  1214. public function topic_pagination( $args ) { 
  1215. $new = $this->maybe_map_permalink_to_group( bbp_get_forum_id() ); 
  1216.  
  1217. if ( empty( $new ) ) 
  1218. return $args; 
  1219.  
  1220. global $wp_rewrite; 
  1221.  
  1222. $args['base'] = trailingslashit( $new ) . $wp_rewrite->pagination_base . '/%#%/'; 
  1223.  
  1224. return $args; 
  1225.  
  1226. /** 
  1227. * Fix pagination of replies on topic view 
  1228. * 
  1229. * @param array $args 
  1230. * @global $wp_rewrite 
  1231. * @uses bbp_get_topic_id() 
  1232. * @uses maybe_map_permalink_to_group 
  1233. * @return array 
  1234. */ 
  1235. public function replies_pagination( $args ) { 
  1236. $new = $this->maybe_map_permalink_to_group( bbp_get_topic_id() ); 
  1237. if ( empty( $new ) ) 
  1238. return $args; 
  1239.  
  1240. global $wp_rewrite; 
  1241.  
  1242. $args['base'] = trailingslashit( $new ) . $wp_rewrite->pagination_base . '/%#%/'; 
  1243.  
  1244. return $args; 
  1245.  
  1246. /** 
  1247. * Ensure that forum content associated with a BuddyPress group can only be 
  1248. * viewed via the group URL. 
  1249. * 
  1250. * @since bbPress (r3802) 
  1251. */ 
  1252. public function redirect_canonical() { 
  1253.  
  1254. // Viewing a single forum 
  1255. if ( bbp_is_single_forum() ) { 
  1256. $forum_id = get_the_ID(); 
  1257. $group_ids = bbp_get_forum_group_ids( $forum_id ); 
  1258.  
  1259. // Viewing a single topic 
  1260. } elseif ( bbp_is_single_topic() ) { 
  1261. $topic_id = get_the_ID(); 
  1262. $slug = get_post_field( 'post_name', $topic_id ); 
  1263. $forum_id = bbp_get_topic_forum_id( $topic_id ); 
  1264. $group_ids = bbp_get_forum_group_ids( $forum_id ); 
  1265.  
  1266. // Not a forum or topic 
  1267. } else { 
  1268. return; 
  1269.  
  1270. // Bail if not a group forum 
  1271. if ( empty( $group_ids ) ) 
  1272. return; 
  1273.  
  1274. // Use the first group ID 
  1275. $group_id = $group_ids[0]; 
  1276. $group = groups_get_group( array( 'group_id' => $group_id ) ); 
  1277. $group_link = trailingslashit( bp_get_group_permalink( $group ) ); 
  1278. $redirect_to = trailingslashit( $group_link . $this->slug ); 
  1279.  
  1280. // Add topic slug to URL 
  1281. if ( bbp_is_single_topic() ) { 
  1282. $redirect_to = trailingslashit( $redirect_to . $this->topic_slug . '/' . $slug ); 
  1283.  
  1284. bp_core_redirect( $redirect_to ); 
  1285.  
  1286. /** Activity **************************************************************/ 
  1287.  
  1288. /** 
  1289. * Map a forum post to its corresponding group in the group activity stream. 
  1290. * 
  1291. * @since bbPress (r4396) 
  1292. * 
  1293. * @param array $args Arguments from BBP_BuddyPress_Activity::record_activity() 
  1294. * @uses groups_get_current_group() To see if we're posting from a BP group 
  1295. * 
  1296. * @return array 
  1297. */ 
  1298. public function map_activity_to_group( $args = array() ) { 
  1299.  
  1300. // Get current BP group 
  1301. $group = groups_get_current_group(); 
  1302.  
  1303. // Not posting from a BuddyPress group? stop now! 
  1304. if ( empty( $group ) ) 
  1305. return $args; 
  1306.  
  1307. // Set the component to 'groups' so the activity item shows up in the group 
  1308. $args['component'] = buddypress()->groups->id; 
  1309.  
  1310. // Move the forum post ID to the secondary item ID 
  1311. $args['secondary_item_id'] = $args['item_id']; 
  1312.  
  1313. // Set the item ID to the group ID so the activity item shows up in the group 
  1314. $args['item_id'] = $group->id; 
  1315.  
  1316. // Update the group's last activity 
  1317. groups_update_last_activity( $group->id ); 
  1318.  
  1319. return $args; 
  1320. endif; 
.