Guest User

Untitled

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