BBP_Forums_Group_Extension

Loads Group Extension for Forums Component.

Defined (1)

The class is defined in the following location(s).

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