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


Generated: Fri Apr 26 01:01:04 2024 Cross-referenced by PHPXref 0.7.1