[ 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 forum 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 status should be changed
  94          add_action( 'groups_group_after_save',        array( $this, 'update_group_forum_visibility'   ) );
  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       * Set forums' status to match the privacy status of the associated group
 864       *
 865       * Fired whenever a group is saved
 866       *
 867       * @param BP_Groups_Group $group Group object.
 868       */
 869  	public static function update_group_forum_visibility( BP_Groups_Group $group ) {
 870  
 871          // Get group forum IDs
 872          $forum_ids = bbp_get_group_forum_ids( $group->id );
 873  
 874          // Bail if no forum IDs available
 875          if ( empty( $forum_ids ) ) {
 876              return;
 877          }
 878  
 879          // Loop through forum IDs
 880          foreach ( $forum_ids as $forum_id ) {
 881  
 882              // Get forum from ID
 883              $forum = bbp_get_forum( $forum_id );
 884  
 885              // Check for change
 886              if ( $group->status !== $forum->post_status ) {
 887                  switch ( $group->status ) {
 888  
 889                      // Changed to hidden
 890                      case 'hidden' :
 891                          bbp_hide_forum( $forum_id, $forum->post_status );
 892                          break;
 893  
 894                      // Changed to private
 895                      case 'private' :
 896                          bbp_privatize_forum( $forum_id, $forum->post_status );
 897                          break;
 898  
 899                      // Changed to public
 900                      case 'public' :
 901                      default :
 902                          bbp_publicize_forum( $forum_id, $forum->post_status );
 903                          break;
 904                  }
 905              }
 906          }
 907      }
 908  
 909      /**
 910       * Toggle the enable_forum group setting on or off
 911       *
 912       * @since 2.3.0 bbPress (r4612)
 913       *
 914       * @param int $group_id The group to toggle
 915       * @param bool $enabled True for on, false for off
 916       * @return False if group is not found, otherwise return the group
 917       */
 918  	public function toggle_group_forum( $group_id = 0, $enabled = false ) {
 919  
 920          // Get the group
 921          $group = groups_get_group( array( 'group_id' => $group_id ) );
 922  
 923          // Bail if group cannot be found
 924          if ( empty( $group ) ) {
 925              return false;
 926          }
 927  
 928          // Set forum enabled status
 929          $group->enable_forum = (int) $enabled;
 930  
 931          // Save the group
 932          $group->save();
 933  
 934          // Maybe disconnect forum from group
 935          if ( empty( $enabled ) ) {
 936              $this->disconnect_forum_from_group( $group_id );
 937          }
 938  
 939          // Update internal private and forum ID variables
 940          bbp_repair_forum_visibility();
 941  
 942          // Return the group
 943          return $group;
 944      }
 945  
 946      /** Display Methods *******************************************************/
 947  
 948      /**
 949       * Output the forums for a group in the edit screens
 950       *
 951       * As of right now, bbPress only supports 1-to-1 group forum relationships.
 952       * In the future, many-to-many should be allowed.
 953       *
 954       * @since 2.1.0 bbPress (r3653)
 955       */
 956  	public function display_forums( $offset = 0 ) {
 957  
 958          // Allow actions immediately before group forum output
 959          do_action( 'bbp_before_group_forum_display' );
 960  
 961          // Load up bbPress once
 962          $bbp = bbpress();
 963  
 964          /** Query Resets ******************************************************/
 965  
 966          // Forum data
 967          $forum_action = bp_action_variable( $offset );
 968          $forum_ids    = bbp_get_group_forum_ids( bp_get_current_group_id() );
 969          $forum_id     = array_shift( $forum_ids );
 970  
 971          // Always load up the group forum
 972          bbp_has_forums( array(
 973              'p'           => $forum_id,
 974              'post_parent' => null
 975          ) );
 976  
 977          // Set the global forum ID
 978          $bbp->current_forum_id = $forum_id;
 979  
 980          // Assume forum query
 981          bbp_set_query_name( 'bbp_single_forum' ); ?>
 982  
 983          <div id="bbpress-forums" class="bbpress-wrapper">
 984  
 985              <?php switch ( $forum_action ) :
 986  
 987                  /** Single Forum **********************************************/
 988  
 989                  case false  :
 990                  case 'page' :
 991  
 992                      // Strip the super stickies from topic query
 993                      add_filter( 'bbp_get_super_stickies', array( $this, 'no_super_stickies'  ), 10, 1 );
 994  
 995                      // Unset the super sticky option on topic form
 996                      add_filter( 'bbp_get_topic_types',    array( $this, 'unset_super_sticky' ), 10, 1 );
 997  
 998                      // Query forums and show them if they exist
 999                      if ( bbp_forums() ) :
1000  
1001                          // Setup the forum
1002                          bbp_the_forum();
1003  
1004                          // Only wrap title in H2 if not empty
1005                          $title = bbp_get_forum_title();
1006                          if ( ! empty( $title ) ) {
1007                              echo '<h2>' . $title . '</h2>';
1008                          }
1009  
1010                          // Output forum
1011                          bbp_get_template_part( 'content', 'single-forum' );
1012  
1013                      // No forums found
1014                      else : ?>
1015  
1016                          <div id="message" class="info">
1017                              <p><?php esc_html_e( 'This group does not currently have a forum.', 'bbpress' ); ?></p>
1018                          </div>
1019  
1020                      <?php endif;
1021  
1022                      break;
1023  
1024                  /** Single Topic **********************************************/
1025  
1026                  case $this->topic_slug :
1027  
1028                      // hide the 'to front' admin links
1029                      add_filter( 'bbp_get_topic_stick_link', array( $this, 'hide_super_sticky_admin_link' ), 10, 2 );
1030  
1031                      // Get the topic
1032                      bbp_has_topics( array(
1033                          'name'           => bp_action_variable( $offset + 1 ),
1034                          'posts_per_page' => 1,
1035                          'show_stickies'  => false
1036                      ) );
1037  
1038                      // If no topic, 404
1039                      if ( ! bbp_topics() ) {
1040                          bp_do_404( bbp_get_forum_permalink( $forum_id ) );
1041  
1042                          // Only wrap title in H2 if not empty
1043                          $title = bbp_get_forum_title();
1044                          if ( ! empty( $title ) ) {
1045                              echo '<h2>' . $title . '</h2>';
1046                          }
1047  
1048                          // No topics
1049                          bbp_get_template_part( 'feedback', 'no-topics' );
1050                          break;
1051                      }
1052  
1053                      // Setup the topic
1054                      bbp_the_topic();
1055  
1056                      // Only wrap title in H2 if not empty
1057                      $title = bbp_get_topic_title();
1058                      if ( ! empty( $title ) ) {
1059                          echo '<h2>' . $title . '</h2>';
1060                      }
1061  
1062                      // Topic edit
1063                      if ( bp_action_variable( $offset + 2 ) === bbp_get_edit_slug() ) :
1064  
1065                          // Unset the super sticky link on edit topic template
1066                          add_filter( 'bbp_get_topic_types', array( $this, 'unset_super_sticky' ), 10, 1 );
1067  
1068                          // Get the main query object
1069                          $wp_query = bbp_get_wp_query();
1070  
1071                          // Set the edit switches
1072                          $wp_query->bbp_is_edit       = true;
1073                          $wp_query->bbp_is_topic_edit = true;
1074  
1075                          // Setup the global forum ID
1076                          $bbp->current_topic_id       = get_the_ID();
1077  
1078                          // Merge
1079                          if ( ! empty( $_GET['action'] ) && 'merge' === $_GET['action'] ) :
1080                              bbp_set_query_name( 'bbp_topic_merge' );
1081                              bbp_get_template_part( 'form', 'topic-merge' );
1082  
1083                          // Split
1084                          elseif ( ! empty( $_GET['action'] ) && 'split' === $_GET['action'] ) :
1085                              bbp_set_query_name( 'bbp_topic_split' );
1086                              bbp_get_template_part( 'form', 'topic-split' );
1087  
1088                          // Edit
1089                          else :
1090                              bbp_set_query_name( 'bbp_topic_form' );
1091                              bbp_get_template_part( 'form', 'topic' );
1092  
1093                          endif;
1094  
1095                      // Single Topic
1096                      else :
1097                          bbp_set_query_name( 'bbp_single_topic' );
1098                          bbp_get_template_part( 'content', 'single-topic' );
1099                      endif;
1100                      break;
1101  
1102                  /** Single Reply **********************************************/
1103  
1104                  case $this->reply_slug :
1105  
1106                      // Get the reply
1107                      bbp_has_replies( array(
1108                          'name'           => bp_action_variable( $offset + 1 ),
1109                          'posts_per_page' => 1
1110                      ) );
1111  
1112                      // If no topic, 404
1113                      if ( ! bbp_replies() ) {
1114                          bp_do_404( bbp_get_forum_permalink( $forum_id ) );
1115  
1116                          // Only wrap title in H2 if not empty
1117                          $title = bbp_get_forum_title();
1118                          if ( ! empty( $title ) ) {
1119                              echo '<h2>' . $title . '</h2>';
1120                          }
1121  
1122                          // No replies
1123                          bbp_get_template_part( 'feedback', 'no-replies' );
1124                          break;
1125                      }
1126  
1127                      // Setup the reply
1128                      bbp_the_reply();
1129  
1130                      // Only wrap title in H2 if not empty
1131                      $title = bbp_get_reply_title();
1132                      if ( ! empty( $title ) ) {
1133                          echo '<h2>' . $title . '</h2>';
1134                      }
1135  
1136                      if ( bp_action_variable( $offset + 2 ) === bbp_get_edit_slug() ) :
1137  
1138                          // Get the main query object
1139                          $wp_query = bbp_get_wp_query();
1140  
1141                          // Set the edit switches
1142                          $wp_query->bbp_is_edit       = true;
1143                          $wp_query->bbp_is_reply_edit = true;
1144  
1145                          // Setup the global reply ID
1146                          $bbp->current_reply_id       = get_the_ID();
1147  
1148                          // Move
1149                          if ( ! empty( $_GET['action'] ) && ( 'move' === $_GET['action'] ) ) :
1150                              bbp_set_query_name( 'bbp_reply_move' );
1151                              bbp_get_template_part( 'form', 'reply-move' );
1152  
1153                          // Edit
1154                          else :
1155                              bbp_set_query_name( 'bbp_reply_form' );
1156                              bbp_get_template_part( 'form', 'reply' );
1157                          endif;
1158                      endif;
1159                      break;
1160              endswitch;
1161  
1162              // Reset the query
1163              wp_reset_query(); ?>
1164  
1165          </div><!-- #bbpress-forums -->
1166  
1167          <?php
1168  
1169          // Allow actions immediately after group forum output
1170          do_action( 'bbp_after_group_forum_display' );
1171      }
1172  
1173      /** Super sticky filters ***************************************************/
1174  
1175      /**
1176       * Strip super stickies from the topic query
1177       *
1178       * @since 2.3.0 bbPress (r4810)
1179       *
1180       * @access private
1181       * @param array $super the super sticky post ID's
1182       * @return array (empty)
1183       */
1184  	public function no_super_stickies( $super = array() ) {
1185          $super = array();
1186          return $super;
1187      }
1188  
1189      /**
1190       * Unset the type super sticky from topic type
1191       *
1192       * @since 2.3.0 bbPress (r4810)
1193       *
1194       * @access private
1195       * @param array $args
1196       * @return array $args without the to-front link
1197       */
1198  	public function unset_super_sticky( $args = array() ) {
1199          if ( isset( $args['super'] ) ) {
1200              unset( $args['super'] );
1201          }
1202          return $args;
1203      }
1204  
1205      /**
1206       * Ugly preg_replace to hide the to front admin link
1207       *
1208       * @since 2.3.0 bbPress (r4810)
1209       *
1210       * @access private
1211       * @param string $retval
1212       * @param array $args
1213       * @return string $retval without the to-front link
1214       */
1215  	public function hide_super_sticky_admin_link( $retval = '', $args = array() ) {
1216          if ( strpos( $retval, '(' ) ) {
1217              $retval = preg_replace( '/(\(.+?)+(\))/i', '', $retval );
1218          }
1219  
1220          return $retval;
1221      }
1222  
1223      /** Redirect Helpers ******************************************************/
1224  
1225      /**
1226       * Redirect to the group forum screen
1227       *
1228       * @since 2.1.0 bbPress (r3653)
1229       *
1230       * @param str $redirect_url
1231       * @param str $redirect_to
1232       */
1233  	public function new_topic_redirect_to( $redirect_url = '', $redirect_to = '', $topic_id = 0 ) {
1234          if ( bp_is_group() ) {
1235              $topic        = bbp_get_topic( $topic_id );
1236              $topic_hash   = '#post-' . $topic_id;
1237              $redirect_url = trailingslashit( bp_get_group_permalink( groups_get_current_group() ) ) . trailingslashit( $this->slug ) . trailingslashit( $this->topic_slug ) . trailingslashit( $topic->post_name ) . $topic_hash;
1238          }
1239  
1240          return $redirect_url;
1241      }
1242  
1243      /**
1244       * Redirect to the group forum screen
1245       *
1246       * @since 2.1.0 bbPress (r3653)
1247       */
1248  	public function new_reply_redirect_to( $redirect_url = '', $redirect_to = '', $reply_id = 0 ) {
1249  
1250          if ( bp_is_group() ) {
1251              $topic_id       = bbp_get_reply_topic_id( $reply_id );
1252              $topic          = bbp_get_topic( $topic_id );
1253              $reply_position = bbp_get_reply_position( $reply_id, $topic_id );
1254              $reply_page     = ceil( (int) $reply_position / (int) bbp_get_replies_per_page() );
1255              $reply_hash     = '#post-' . $reply_id;
1256              $topic_url      = trailingslashit( bp_get_group_permalink( groups_get_current_group() ) ) . trailingslashit( $this->slug ) . trailingslashit( $this->topic_slug ) . trailingslashit( $topic->post_name );
1257  
1258              // Don't include pagination if on first page
1259              if ( 1 >= $reply_page ) {
1260                  $redirect_url = trailingslashit( $topic_url ) . $reply_hash;
1261  
1262              // Include pagination
1263              } else {
1264                  $redirect_url = trailingslashit( $topic_url ) . trailingslashit( bbp_get_paged_slug() ) . trailingslashit( $reply_page ) . $reply_hash;
1265              }
1266  
1267              // Add topic view query arg back to end if it is set
1268              if ( bbp_get_view_all() ) {
1269                  $redirect_url = bbp_add_view_all( $redirect_url );
1270              }
1271          }
1272  
1273          return $redirect_url;
1274      }
1275  
1276      /**
1277       * Redirect to the group admin forum edit screen
1278       *
1279       * @since 2.1.0 bbPress (r3653)
1280       */
1281  	public function edit_redirect_to( $redirect_url = '' ) {
1282  
1283          // Get the current group, if there is one
1284          $group = groups_get_current_group();
1285  
1286          // If this is a group of any kind, empty out the redirect URL
1287          if ( bp_is_group_admin_screen( $this->slug ) ) {
1288              $redirect_url = trailingslashit( bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/' . $group->slug . '/admin/' . $this->slug );
1289          }
1290  
1291          return $redirect_url;
1292      }
1293  
1294      /** Form Helpers **********************************************************/
1295  
1296      /**
1297       * Make Forum Parent a hidden field instead of a selectable one.
1298       *
1299       * @since 2.1.0 bbPress (r3746)
1300       */
1301  	public function forum_parent() {
1302      ?>
1303  
1304          <input type="hidden" name="bbp_forum_parent_id" id="bbp_forum_parent_id" value="<?php bbp_group_forums_root_id(); ?>" />
1305  
1306      <?php
1307      }
1308  
1309      /**
1310       * Output a dropdown for picking which group forum this topic is for.
1311       *
1312       * @since 2.1.0 bbPress (r3746)
1313       */
1314  	public function topic_parent() {
1315  
1316          // Get the group ID
1317          $gid       = bp_get_current_group_id();
1318  
1319          // Get the forum IDs for this group
1320          $forum_ids = bbp_get_group_forum_ids( $gid );
1321  
1322          // Attempt to get the current topic forum ID
1323          $topic_id  = bbp_get_topic_id();
1324          $forum_id  = bbp_get_topic_forum_id( $topic_id );
1325  
1326          // Setup the query arguments - note that these may be overridden later
1327          // by various bbPress visibility and capability filters.
1328          $args = array(
1329              'post_type'   => bbp_get_forum_post_type(),
1330              'post_status' => bbp_get_public_status_id(),
1331              'include'     => $forum_ids,
1332              'numberposts' => -1,
1333              'orderby'     => 'menu_order',
1334              'order'       => 'ASC',
1335          );
1336  
1337          // Get the forum objects for these forum IDs
1338          $forums = get_posts( $args );
1339  
1340          // Setup the dropdown arguments
1341          $dd_args = array(
1342              'posts'    => $forums,
1343              'selected' => $forum_id,
1344          ); ?>
1345  
1346          <p>
1347              <label for="bbp_forum_id"><?php esc_html_e( 'Forum:', 'bbpress' ); ?></label><br />
1348              <?php bbp_dropdown( $dd_args ); ?>
1349          </p>
1350  
1351      <?php
1352      }
1353  
1354      /**
1355       * Permissions to view the 'New Topic'/'Reply To' form in a BuddyPress group.
1356       *
1357       * @since 2.3.0 bbPress (r4608)
1358       *
1359       * @param bool $retval Are we allowed to view the reply form?
1360       *
1361       * @return bool
1362       */
1363  	public function form_permissions( $retval = false ) {
1364  
1365          // Bail if user is not logged in
1366          if ( ! is_user_logged_in() ) {
1367              return $retval;
1368  
1369          // Keymasters can always pass go
1370          } elseif ( bbp_is_user_keymaster() ) {
1371              $retval = true;
1372  
1373          // Non-members cannot see forms
1374          } elseif ( ! bbp_group_is_member() ) {
1375              $retval = false;
1376  
1377          // Banned users cannot see forms
1378          } elseif ( bbp_group_is_banned() ) {
1379              $retval = false;
1380          }
1381  
1382          return $retval;
1383      }
1384  
1385      /**
1386       * Add a hidden input field on the group settings page if the group forum is
1387       * enabled.
1388       *
1389       * Due to the way BuddyPress' group admin settings page saves its settings,
1390       * we need to let BP know that bbPress added a forum.
1391       *
1392       * @since 2.4.0 bbPress (r5026)
1393       *
1394       * @link https://bbpress.trac.wordpress.org/ticket/2339/
1395       * @see groups_screen_group_admin_settings()
1396       */
1397  	public function group_settings_hidden_field() {
1398  
1399          // if a forum is not enabled, we don't need to add this field
1400          if ( ! bp_group_is_forum_enabled() ) {
1401              return;
1402          } ?>
1403  
1404          <input type="hidden" name="group-show-forum" id="group-show-forum" value="1" />
1405  
1406      <?php
1407      }
1408  
1409      /** Permalink Mappers *****************************************************/
1410  
1411      /**
1412       * Maybe map a bbPress forum/topic/reply permalink to the corresponding group
1413       *
1414       * @since 2.2.0 bbPress (r4266)
1415       *
1416       * @param int $post_id
1417       * @return Bail early if not a group forum post
1418       * @return string
1419       */
1420  	private function maybe_map_permalink_to_group( $post_id = 0, $url = false ) {
1421  
1422          switch ( get_post_type( $post_id ) ) {
1423  
1424              // Reply
1425              case bbp_get_reply_post_type() :
1426                  $topic_id = bbp_get_reply_topic_id( $post_id );
1427                  $forum_id = bbp_get_reply_forum_id( $post_id );
1428                  $url_end  = trailingslashit( $this->reply_slug ) . get_post_field( 'post_name', $post_id );
1429                  break;
1430  
1431              // Topic
1432              case bbp_get_topic_post_type() :
1433                  $topic_id = $post_id;
1434                  $forum_id = bbp_get_topic_forum_id( $post_id );
1435                  $url_end  = trailingslashit( $this->topic_slug ) . get_post_field( 'post_name', $post_id );
1436                  break;
1437  
1438              // Forum
1439              case bbp_get_forum_post_type() :
1440                  $forum_id = $post_id;
1441                  $url_end  = ''; //get_post_field( 'post_name', $post_id );
1442                  break;
1443  
1444              // Unknown
1445              default :
1446                  return $url;
1447          }
1448  
1449          // Get group ID's for this forum
1450          $group_ids = bbp_get_forum_group_ids( $forum_id );
1451  
1452          // Bail if the post isn't associated with a group
1453          if ( empty( $group_ids ) ) {
1454              return $url;
1455          }
1456  
1457          // @todo Multiple group forums/forum groups
1458          $group_id = $group_ids[0];
1459          $group    = groups_get_group( array( 'group_id' => $group_id ) );
1460  
1461          if ( bp_is_group_admin_screen( $this->slug ) ) {
1462              $group_permalink = trailingslashit( bp_get_group_admin_permalink( $group ) );
1463          } else {
1464              $group_permalink = trailingslashit( bp_get_group_permalink( $group ) );
1465          }
1466  
1467          return trailingslashit( trailingslashit( $group_permalink . $this->slug ) . $url_end );
1468      }
1469  
1470      /**
1471       * Map a forum permalink to its corresponding group
1472       *
1473       * @since 2.1.0 bbPress (r3802)
1474       *
1475       * @param string $url
1476       * @param int $forum_id
1477       * @return string
1478       */
1479  	public function map_forum_permalink_to_group( $url, $forum_id ) {
1480          return $this->maybe_map_permalink_to_group( $forum_id, $url );
1481      }
1482  
1483      /**
1484       * Map a topic permalink to its group forum
1485       *
1486       * @since 2.1.0 bbPress (r3802)
1487       *
1488       * @param string $url
1489       * @param int $topic_id
1490       * @return string
1491       */
1492  	public function map_topic_permalink_to_group( $url, $topic_id ) {
1493          return $this->maybe_map_permalink_to_group( $topic_id, $url );
1494      }
1495  
1496      /**
1497       * Map a reply permalink to its group forum
1498       *
1499       * @since 2.1.0 bbPress (r3802)
1500       *
1501       * @param string $url
1502       * @param int $reply_id
1503       * @return string
1504       */
1505  	public function map_reply_permalink_to_group( $url, $reply_id ) {
1506          return $this->maybe_map_permalink_to_group( bbp_get_reply_topic_id( $reply_id ), $url );
1507      }
1508  
1509      /**
1510       * Map a reply edit link to its group forum
1511       *
1512       * @since 2.2.0 bbPress (r4266)
1513       *
1514       * @param string $url
1515       * @param int $reply_id
1516       * @return string
1517       */
1518  	public function map_reply_edit_url_to_group( $url, $reply_id ) {
1519          $new = $this->maybe_map_permalink_to_group( $reply_id );
1520  
1521          if ( empty( $new ) ) {
1522              return $url;
1523          }
1524  
1525          return trailingslashit( $new ) . bbpress()->edit_id  . '/';
1526      }
1527  
1528      /**
1529       * Map a post link to its group forum
1530       *
1531       * @since 2.2.0 bbPress (r4266)
1532       *
1533       * @param string $url
1534       * @param obj $post
1535       * @param boolean $leavename
1536       * @return string
1537       */
1538  	public function post_link( $url, $post ) {
1539          return $this->maybe_map_permalink_to_group( $post->ID, $url );
1540      }
1541  
1542      /**
1543       * Map a page link to its group forum
1544       *
1545       * @since 2.2.0 bbPress (r4266)
1546       *
1547       * @param string $url
1548       * @param int $post_id
1549       * @param $sample
1550       * @return string
1551       */
1552  	public function page_link( $url, $post_id ) {
1553          return $this->maybe_map_permalink_to_group( $post_id, $url );
1554      }
1555  
1556      /**
1557       * Map a custom post type link to its group forum
1558       *
1559       * @since 2.2.0 bbPress (r4266)
1560       *
1561       * @param string $url
1562       * @param obj $post
1563       * @param $leavename
1564       * @param $sample
1565       * @return string
1566       */
1567  	public function post_type_link( $url, $post ) {
1568          return $this->maybe_map_permalink_to_group( $post->ID, $url );
1569      }
1570  
1571      /**
1572       * Fix pagination of topics on forum view
1573       *
1574       * @since 2.2.0 bbPress (r4266)
1575       *
1576       * @param array $args
1577       * @return array
1578       */
1579  	public function topic_pagination( $args ) {
1580          $new = $this->maybe_map_permalink_to_group( bbp_get_forum_id() );
1581  
1582          if ( empty( $new ) ) {
1583              return $args;
1584          }
1585  
1586          $args['base'] = trailingslashit( $new ) . bbp_get_paged_slug() . '/%#%/';
1587  
1588          return $args;
1589      }
1590  
1591      /**
1592       * Fix pagination of replies on topic view
1593       *
1594       * @since 2.2.0 bbPress (r4266)
1595       *
1596       * @param array $args
1597       * @return array
1598       */
1599  	public function replies_pagination( $args ) {
1600          $new = $this->maybe_map_permalink_to_group( bbp_get_topic_id() );
1601          if ( empty( $new ) ) {
1602              return $args;
1603          }
1604  
1605          $args['base'] = trailingslashit( $new ) . bbp_get_paged_slug() . '/%#%/';
1606  
1607          return $args;
1608      }
1609  
1610      /**
1611       * Ensure that forum content associated with a BuddyPress group can only be
1612       * viewed via the group URL.
1613       *
1614       * @since 2.1.0 bbPress (r3802)
1615       */
1616  	public function redirect_canonical() {
1617  
1618          // Bail if on a RSS feed
1619          if ( is_feed() ) {
1620              return;
1621          }
1622  
1623          // Viewing a single forum
1624          if ( bbp_is_single_forum() ) {
1625              $forum_id  = get_the_ID();
1626              $group_ids = bbp_get_forum_group_ids( $forum_id );
1627  
1628          // Viewing a single topic
1629          } elseif ( bbp_is_single_topic() ) {
1630              $topic_id  = get_the_ID();
1631              $slug      = get_post_field( 'post_name', $topic_id );
1632              $forum_id  = bbp_get_topic_forum_id( $topic_id );
1633              $group_ids = bbp_get_forum_group_ids( $forum_id );
1634  
1635          // Not a forum or topic
1636          } else {
1637              return;
1638          }
1639  
1640          // Bail if not a group forum
1641          if ( empty( $group_ids ) ) {
1642              return;
1643          }
1644  
1645          // Use the first group ID
1646          $group_id      = $group_ids[0];
1647          $group         = groups_get_group( array( 'group_id' => $group_id ) );
1648          $group_link  = trailingslashit( bp_get_group_permalink( $group ) );
1649          $redirect_to = trailingslashit( $group_link . $this->slug );
1650  
1651          // Add topic slug to URL
1652          if ( bbp_is_single_topic() ) {
1653              $redirect_to  = trailingslashit( $redirect_to . $this->topic_slug . '/' . $slug );
1654          }
1655  
1656          bp_core_redirect( $redirect_to );
1657      }
1658  
1659      /** Activity **************************************************************/
1660  
1661      /**
1662       * Map a forum post to its corresponding group in the group activity stream.
1663       *
1664       * @since 2.2.0 bbPress (r4396)
1665       *
1666       * @param array $args Arguments from BBP_BuddyPress_Activity::record_activity()
1667       *
1668       * @return array
1669       */
1670  	public function map_activity_to_group( $args = array() ) {
1671  
1672          // Get current BP group
1673          $group = groups_get_current_group();
1674  
1675          // Not posting from a BuddyPress group? stop now!
1676          if ( empty( $group ) ) {
1677              return $args;
1678          }
1679  
1680          // Set the component to 'groups' so the activity item shows up in the group
1681          $args['component']         = buddypress()->groups->id;
1682  
1683          // Move the forum post ID to the secondary item ID
1684          $args['secondary_item_id'] = $args['item_id'];
1685  
1686          // Set the item ID to the group ID so the activity item shows up in the group
1687          $args['item_id']           = $group->id;
1688  
1689          // Update the group's last activity
1690          groups_update_last_activity( $group->id );
1691  
1692          return $args;
1693      }
1694  }
1695  endif;


Generated: Thu Sep 24 01:03:34 2020 Cross-referenced by PHPXref 0.7.1