[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/src/includes/extend/buddypress/ -> groups.php (source)

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


Generated: Tue Jul 16 01:01:32 2019 Cross-referenced by PHPXref 0.7.1