[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Main bbPress BuddyPress Class
   5   *
   6   * @package bbPress
   7   * @subpackage BuddyPress
   8   */
   9  
  10  // Exit if accessed directly
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  // Hooks
  14  add_filter( 'bp_modify_page_title', 'bbp_filter_modify_page_title', 10, 3 );
  15  add_filter( 'bbp_get_user_id',      'bbp_filter_user_id',           10, 3 );
  16  add_filter( 'bbp_is_single_user',   'bbp_filter_is_single_user',    10, 1 );
  17  add_filter( 'bbp_is_user_home',     'bbp_filter_is_user_home',      10, 1 );
  18  
  19  // Group Forum Root
  20  add_action( 'load-settings_page_bbpress', 'bbp_maybe_create_group_forum_root' );
  21  add_action( 'bbp_delete_forum',           'bbp_maybe_delete_group_forum_root' );
  22  
  23  /** BuddyPress Helpers ********************************************************/
  24  
  25  /**
  26   * Return component name/ID ('forums' by default)
  27   *
  28   * This is used primarily for Notifications integration.
  29   *
  30   * @since 2.6.0 bbPress (r5232)
  31   *
  32   * @return string
  33   */
  34  function bbp_get_component_name() {
  35  
  36      // Use existing ID or default
  37      $retval = ! empty( bbpress()->extend->buddypress->id )
  38          ? bbpress()->extend->buddypress->id
  39          : 'forums';
  40  
  41      // Filter & return
  42      return apply_filters( 'bbp_get_component_name', $retval );
  43  }
  44  
  45  /**
  46   * Filter the current bbPress user ID with the current BuddyPress user ID
  47   *
  48   * @since 2.1.0 bbPress (r3552)
  49   *
  50   * @param int $user_id
  51   * @param bool $displayed_user_fallback
  52   * @param bool $current_user_fallback
  53   *
  54   * @return int User ID
  55   */
  56  function bbp_filter_user_id( $user_id = 0, $displayed_user_fallback = true, $current_user_fallback = false ) {
  57  
  58      // Define local variable
  59      $bbp_user_id = 0;
  60  
  61      // Get possible user ID's
  62      $did = bp_displayed_user_id();
  63      $lid = bp_loggedin_user_id();
  64  
  65      // Easy empty checking
  66      if ( ! empty( $user_id ) && is_numeric( $user_id ) ) {
  67          $bbp_user_id = $user_id;
  68  
  69      // Currently viewing or editing a user
  70      } elseif ( ( true === $displayed_user_fallback ) && ! empty( $did ) ) {
  71          $bbp_user_id = $did;
  72  
  73      // Maybe fallback on the current_user ID
  74      } elseif ( ( true === $current_user_fallback ) && ! empty( $lid ) ) {
  75          $bbp_user_id = $lid;
  76      }
  77  
  78      return $bbp_user_id;
  79  }
  80  
  81  /**
  82   * Filter the bbPress is_single_user function with BuddyPress equivalent
  83   *
  84   * @since 2.1.0 bbPress (r3552)
  85   *
  86   * @param bool $is Optional. Default false
  87   * @return bool True if viewing single user, false if not
  88   */
  89  function bbp_filter_is_single_user( $is = false ) {
  90      if ( ! empty( $is ) ) {
  91          return $is;
  92      }
  93  
  94      return bp_is_user();
  95  }
  96  
  97  /**
  98   * Filter the bbPress is_user_home function with BuddyPress equivalent
  99   *
 100   * @since 2.1.0 bbPress (r3552)
 101   *
 102   * @param bool $is Optional. Default false
 103   * @return bool True if viewing single user, false if not
 104   */
 105  function bbp_filter_is_user_home( $is = false ) {
 106      if ( ! empty( $is ) ) {
 107          return $is;
 108      }
 109  
 110      return bp_is_my_profile();
 111  }
 112  
 113  /**
 114   * Add the topic title to the <title> if viewing a single group forum topic
 115   *
 116   * @since 2.5.0 bbPress (r5161)
 117   *
 118   * @param string $new_title The title to filter
 119   * @param string $old_title (Not used)
 120   * @param string $sep The separator to use
 121   * @return string The possibly modified title
 122   */
 123  function bbp_filter_modify_page_title( $new_title = '', $old_title = '', $sep = '' ) {
 124  
 125      // Only filter if group forums are active
 126      if ( bbp_is_group_forums_active() ) {
 127  
 128          // Only filter for single group forum topics
 129          if ( bp_is_group_forum_topic() || bp_is_group_forum_topic_edit() ) {
 130  
 131              // Get the topic
 132              $topic = get_posts( array(
 133                  'name'        => bp_action_variable( 1 ),
 134                  'post_status' => array_keys( bbp_get_topic_statuses() ),
 135                  'post_type'   => bbp_get_topic_post_type(),
 136                  'numberposts' => 1
 137              ) );
 138  
 139              // Add the topic title to the <title>
 140              $new_title .= bbp_get_topic_title( $topic[0]->ID ) . ' ' . $sep . ' ';
 141          }
 142      }
 143  
 144      // Return the title
 145      return $new_title;
 146  }
 147  
 148  /** BuddyPress Screens ********************************************************/
 149  
 150  /**
 151   * Hook bbPress topics template into plugins template
 152   *
 153   * @since 2.1.0 bbPress (r3552)
 154   */
 155  function bbp_member_forums_screen_topics() {
 156      add_action( 'bp_template_content', 'bbp_member_forums_topics_content' );
 157      bp_core_load_template( apply_filters( 'bbp_member_forums_screen_topics', 'members/single/plugins' ) );
 158  }
 159  
 160  /**
 161   * Hook bbPress replies template into plugins template
 162   *
 163   * @since 2.1.0 bbPress (r3552)
 164   */
 165  function bbp_member_forums_screen_replies() {
 166      add_action( 'bp_template_content', 'bbp_member_forums_replies_content' );
 167      bp_core_load_template( apply_filters( 'bbp_member_forums_screen_replies', 'members/single/plugins' ) );
 168  }
 169  
 170  /**
 171   * Hook bbPress engagements template into plugins template
 172   *
 173   * @since 2.6.0 bbPress (r6320)
 174   */
 175  function bbp_member_forums_screen_engagements() {
 176      add_action( 'bp_template_content', 'bbp_member_forums_engagements_content' );
 177      bp_core_load_template( apply_filters( 'bbp_member_forums_screen_engagements', 'members/single/plugins' ) );
 178  }
 179  
 180  /**
 181   * Hook bbPress favorites template into plugins template
 182   *
 183   * @since 2.1.0 bbPress (r3552)
 184   */
 185  function bbp_member_forums_screen_favorites() {
 186      add_action( 'bp_template_content', 'bbp_member_forums_favorites_content' );
 187      bp_core_load_template( apply_filters( 'bbp_member_forums_screen_favorites', 'members/single/plugins' ) );
 188  }
 189  
 190  /**
 191   * Hook bbPress subscriptions template into plugins template
 192   *
 193   * @since 2.1.0 bbPress (r3552)
 194   */
 195  function bbp_member_forums_screen_subscriptions() {
 196      add_action( 'bp_template_content', 'bbp_member_forums_subscriptions_content' );
 197      bp_core_load_template( apply_filters( 'bbp_member_forums_screen_subscriptions', 'members/single/plugins' ) );
 198  }
 199  
 200  /** BuddyPress Templates ******************************************************/
 201  
 202  /**
 203   * Get the topics created template part
 204   *
 205   * @since 2.1.0 bbPress (r3552)
 206   */
 207  function bbp_member_forums_topics_content() {
 208  ?>
 209  
 210      <div id="bbpress-forums" class="bbpress-wrapper">
 211  
 212          <?php bbp_get_template_part( 'user', 'topics-created' ); ?>
 213  
 214      </div>
 215  
 216  <?php
 217  }
 218  
 219  /**
 220   * Get the topics replied to template part
 221   *
 222   * @since 2.1.0 bbPress (r3552)
 223   */
 224  function bbp_member_forums_replies_content() {
 225  ?>
 226  
 227      <div id="bbpress-forums" class="bbpress-wrapper">
 228  
 229          <?php bbp_get_template_part( 'user', 'replies-created' ); ?>
 230  
 231      </div>
 232  
 233  <?php
 234  }
 235  
 236  /**
 237   * Get the topic engagements template part
 238   *
 239   * @since 2.6.0 bbPress (r6320)
 240   */
 241  function bbp_member_forums_engagements_content() {
 242  ?>
 243  
 244      <div id="bbpress-forums" class="bbpress-wrapper">
 245  
 246          <?php bbp_get_template_part( 'user', 'engagements' ); ?>
 247  
 248      </div>
 249  
 250  <?php
 251  }
 252  
 253  /**
 254   * Get the topics favorited template part
 255   *
 256   * @since 2.1.0 bbPress (r3552)
 257   */
 258  function bbp_member_forums_favorites_content() {
 259  ?>
 260  
 261      <div id="bbpress-forums" class="bbpress-wrapper">
 262  
 263          <?php bbp_get_template_part( 'user', 'favorites' ); ?>
 264  
 265      </div>
 266  
 267  <?php
 268  }
 269  
 270  /**
 271   * Get the topics subscribed template part
 272   *
 273   * @since 2.1.0 bbPress (r3552)
 274   */
 275  function bbp_member_forums_subscriptions_content() {
 276  ?>
 277  
 278      <div id="bbpress-forums" class="bbpress-wrapper">
 279  
 280          <?php bbp_get_template_part( 'user', 'subscriptions' ); ?>
 281  
 282      </div>
 283  
 284  <?php
 285  }
 286  
 287  /** Forum Group Root **********************************************************/
 288  
 289  /**
 290   * Clean up the group root setting if the forum is being deleted
 291   *
 292   * @since 2.6.0 bbPress (r6479)
 293   *
 294   * @param int $forum_id The forum ID being deleted
 295   */
 296  function bbp_maybe_delete_group_forum_root( $forum_id = 0 ) {
 297  
 298      // Bail if no forum ID
 299      $forum_id = bbp_get_forum_id();
 300      if ( empty( $forum_id ) ) {
 301          return;
 302      }
 303  
 304      // Get the group root
 305      $group_root = (int) get_option( '_bbp_group_forums_root_id', 0 );
 306  
 307      // Delete the group root if the forum just got deleted
 308      if ( $group_root === $forum_id ) {
 309          delete_option( '_bbp_group_forums_root_id' );
 310      }
 311  }
 312  
 313  /**
 314   * Handle the new group forum root creation
 315   *
 316   * @since 2.6.0 bbPress (r6479)
 317   *
 318   * @return
 319   */
 320  function bbp_maybe_create_group_forum_root() {
 321  
 322      // Bail if no nonce
 323      if ( empty( $_GET['_wpnonce'] ) || ( empty( $_GET['create'] ) || ( 'bbp-group-forum-root' !== $_GET['create'] ) ) ) {
 324          return;
 325      }
 326  
 327      // Bail if user cannot publish forums
 328      if ( ! current_user_can( 'publish_forums' ) ) {
 329          return;
 330      }
 331  
 332      // Bail if nonce check fails
 333      if ( ! wp_verify_nonce( $_GET['_wpnonce'], '_bbp_group_forums_root_id' ) ) {
 334          return;
 335      }
 336  
 337      // Create new forum
 338      $forum_id = bbp_insert_forum(
 339  
 340          // Post
 341          array( 'post_title' => esc_html__( 'Group Forums', 'bbpress' ) ),
 342  
 343          // Meta
 344          array( 'forum_type' => 'category' )
 345      );
 346  
 347      // Update & redirect
 348      if ( ! empty( $forum_id ) ) {
 349  
 350          // Create
 351          update_option( '_bbp_group_forums_root_id', $forum_id );
 352  
 353          // Redirect
 354          bbp_redirect( add_query_arg( array(
 355              'page'    => 'bbpress',
 356              'updated' => true
 357          ), admin_url( 'options-general.php' ) ) );
 358      }
 359  }
 360  
 361  /** Forum/Group Sync **********************************************************/
 362  
 363  /**
 364   * These functions are used to keep the many-to-many relationships between
 365   * groups and forums synchronized. Each forum and group stores pointers to each
 366   * other in their respective meta. This way if a group or forum is deleted
 367   * their associations can be updated without much effort.
 368   */
 369  
 370  /**
 371   * Get forum ID's for a group
 372   *
 373   * @since 2.1.0 bbPress (r3653)
 374   *
 375   * @param int $group_id
 376   */
 377  function bbp_get_group_forum_ids( $group_id = 0 ) {
 378  
 379      // Assume no forums
 380      $forum_ids = array();
 381  
 382      // Use current group if none is set
 383      if ( empty( $group_id ) ) {
 384          $group_id = bp_get_current_group_id();
 385      }
 386  
 387      // Get the forums
 388      if ( ! empty( $group_id ) ) {
 389          $forum_ids = groups_get_groupmeta( $group_id, 'forum_id' );
 390      }
 391  
 392      // Make sure result is an array of ints
 393      $forum_ids = array_filter( wp_parse_id_list( $forum_ids ) );
 394  
 395      // Filter & return
 396      return (array) apply_filters( 'bbp_get_group_forum_ids', $forum_ids, $group_id );
 397  }
 398  
 399  /**
 400   * Get group ID's for a forum
 401   *
 402   * @since 2.1.0 bbPress (r3653)
 403   *
 404   * @param int $forum_id
 405   */
 406  function bbp_get_forum_group_ids( $forum_id = 0 ) {
 407  
 408      // Assume no forums
 409      $group_ids = array();
 410  
 411      // Use current group if none is set
 412      if ( empty( $forum_id ) ) {
 413          $forum_id = bbp_get_forum_id();
 414      }
 415  
 416      // Get the forums
 417      if ( ! empty( $forum_id ) ) {
 418          $group_ids = get_post_meta( $forum_id, '_bbp_group_ids', true );
 419      }
 420  
 421      // Make sure result is an array of ints
 422      $group_ids = array_filter( wp_parse_id_list( $group_ids ) );
 423  
 424      // Filter & return
 425      return (array) apply_filters( 'bbp_get_forum_group_ids', $group_ids, $forum_id );
 426  }
 427  
 428  /**
 429   * Get forum ID's for a group
 430   *
 431   * @since 2.1.0 bbPress (r3653)
 432   *
 433   * @param int $group_id
 434   */
 435  function bbp_update_group_forum_ids( $group_id = 0, $forum_ids = array() ) {
 436  
 437      // Use current group if none is set
 438      if ( empty( $group_id ) ) {
 439          $group_id = bp_get_current_group_id();
 440      }
 441  
 442      // Trim out any empties
 443      $forum_ids = array_filter( wp_parse_id_list( $forum_ids ) );
 444  
 445      // Get the forums
 446      return groups_update_groupmeta( $group_id, 'forum_id', $forum_ids );
 447  }
 448  
 449  /**
 450   * Update group ID's for a forum
 451   *
 452   * @since 2.1.0 bbPress (r3653)
 453   *
 454   * @param int $forum_id
 455   */
 456  function bbp_update_forum_group_ids( $forum_id = 0, $group_ids = array() ) {
 457      $forum_id = bbp_get_forum_id( $forum_id );
 458  
 459      // Trim out any empties
 460      $group_ids = array_filter( wp_parse_id_list( $group_ids ) );
 461  
 462      // Get the forums
 463      return update_post_meta( $forum_id, '_bbp_group_ids', $group_ids );
 464  }
 465  
 466  /**
 467   * Add a group to a forum
 468   *
 469   * @since 2.1.0 bbPress (r3653)
 470   *
 471   * @param int $group_id
 472   */
 473  function bbp_add_group_id_to_forum( $forum_id = 0, $group_id = 0 ) {
 474  
 475      // Validate forum_id
 476      $forum_id = bbp_get_forum_id( $forum_id );
 477  
 478      // Use current group if none is set
 479      if ( empty( $group_id ) ) {
 480          $group_id = bp_get_current_group_id();
 481      }
 482  
 483      // Get current group IDs
 484      $group_ids = bbp_get_forum_group_ids( $forum_id );
 485  
 486      // Maybe update the groups forums
 487      if ( ! in_array( $group_id, $group_ids, true ) ) {
 488          $group_ids[] = $group_id;
 489          return bbp_update_forum_group_ids( $forum_id, $group_ids );
 490      }
 491  }
 492  
 493  /**
 494   * Remove a forum from a group
 495   *
 496   * @since 2.1.0 bbPress (r3653)
 497   *
 498   * @param int $group_id
 499   */
 500  function bbp_add_forum_id_to_group( $group_id = 0, $forum_id = 0 ) {
 501  
 502      // Validate forum_id
 503      $forum_id = bbp_get_forum_id( $forum_id );
 504  
 505      // Use current group if none is set
 506      if ( empty( $group_id ) ) {
 507          $group_id = bp_get_current_group_id();
 508      }
 509  
 510      // Get current group IDs
 511      $forum_ids = bbp_get_group_forum_ids( $group_id );
 512  
 513      // Maybe update the groups forums
 514      if ( ! in_array( $forum_id, $forum_ids, true ) ) {
 515          $forum_ids[] = $forum_id;
 516          return bbp_update_group_forum_ids( $group_id, $forum_ids );
 517      }
 518  }
 519  
 520  /**
 521   * Remove a group from a forum
 522   *
 523   * @since 2.1.0 bbPress (r3653)
 524   *
 525   * @param int $group_id
 526   */
 527  function bbp_remove_group_id_from_forum( $forum_id = 0, $group_id = 0 ) {
 528  
 529      // Validate forum_id
 530      $forum_id = bbp_get_forum_id( $forum_id );
 531  
 532      // Use current group if none is set
 533      if ( empty( $group_id ) ) {
 534          $group_id = bp_get_current_group_id();
 535      }
 536  
 537      // Get current group IDs
 538      $group_ids = bbp_get_forum_group_ids( $forum_id );
 539  
 540      // Maybe update the groups forums
 541      if ( in_array( $group_id, $group_ids, true ) ) {
 542          $group_ids = array_diff( array_values( $group_ids ), (array) $group_id );
 543          return bbp_update_forum_group_ids( $forum_id, $group_ids );
 544      }
 545  }
 546  
 547  /**
 548   * Remove a forum from a group
 549   *
 550   * @since 2.1.0 bbPress (r3653)
 551   *
 552   * @param int $group_id
 553   */
 554  function bbp_remove_forum_id_from_group( $group_id = 0, $forum_id = 0 ) {
 555  
 556      // Validate forum_id
 557      $forum_id = bbp_get_forum_id( $forum_id );
 558  
 559      // Use current group if none is set
 560      if ( empty( $group_id ) ) {
 561          $group_id = bp_get_current_group_id();
 562      }
 563  
 564      // Get current group IDs
 565      $forum_ids = bbp_get_group_forum_ids( $group_id );
 566  
 567      // Maybe update the groups forums
 568      if ( in_array( $forum_id, $forum_ids, true ) ) {
 569          $forum_ids = array_diff( array_values( $forum_ids ), (array) $forum_id );
 570          return bbp_update_group_forum_ids( $group_id, $forum_ids );
 571      }
 572  }
 573  
 574  /**
 575   * Remove a group from all forums
 576   *
 577   * @since 2.1.0 bbPress (r3653)
 578   *
 579   * @param int $group_id
 580   */
 581  function bbp_remove_group_id_from_all_forums( $group_id = 0 ) {
 582  
 583      // Use current group if none is set
 584      if ( empty( $group_id ) ) {
 585          $group_id = bp_get_current_group_id();
 586      }
 587  
 588      // Get current group IDs
 589      $forum_ids = bbp_get_group_forum_ids( $group_id );
 590  
 591      // Loop through forums and remove this group from each one
 592      foreach ( $forum_ids as $forum_id ) {
 593          bbp_remove_group_id_from_forum( $group_id, $forum_id );
 594      }
 595  }
 596  
 597  /**
 598   * Remove a forum from all groups
 599   *
 600   * @since 2.1.0 bbPress (r3653)
 601   *
 602   * @param int $forum_id
 603   */
 604  function bbp_remove_forum_id_from_all_groups( $forum_id = 0 ) {
 605  
 606      // Validate
 607      $forum_id  = bbp_get_forum_id( $forum_id );
 608      $group_ids = bbp_get_forum_group_ids( $forum_id );
 609  
 610      // Loop through groups and remove this forum from each one
 611      foreach ( $group_ids as $group_id ) {
 612          bbp_remove_forum_id_from_group( $forum_id, $group_id );
 613      }
 614  }
 615  
 616  /**
 617   * Return true if a forum is a group forum
 618   *
 619   * @since 2.3.0 bbPress (r4571)
 620   *
 621   * @param int $forum_id
 622   * @return bool True if it is a group forum, false if not
 623   */
 624  function bbp_is_forum_group_forum( $forum_id = 0 ) {
 625  
 626      // Validate
 627      $forum_id  = bbp_get_forum_id( $forum_id );
 628  
 629      // Check for group ID's
 630      $group_ids = bbp_get_forum_group_ids( $forum_id );
 631  
 632      // Check if the forum has groups
 633      $retval    = (bool) ! empty( $group_ids );
 634  
 635      // Filter & return
 636      return (bool) apply_filters( 'bbp_is_forum_group_forum', $retval, $forum_id, $group_ids );
 637  }
 638  
 639  /*** Group Member Status ******************************************************/
 640  
 641  /**
 642   * Is the current user an admin of the current group
 643   *
 644   * @since 2.3.0 bbPress (r4632)
 645   *
 646   * @return bool If current user is an admin of the current group
 647   */
 648  function bbp_group_is_admin() {
 649  
 650      // Bail if user is not logged in or not looking at a group
 651      if ( ! is_user_logged_in() || ! bp_is_group() ) {
 652          return false;
 653      }
 654  
 655      $bbp = bbpress();
 656  
 657      // Set the global if not set
 658      if ( ! isset( $bbp->current_user->is_group_admin ) ) {
 659          $bbp->current_user->is_group_admin = groups_is_user_admin( bp_loggedin_user_id(), bp_get_current_group_id() );
 660      }
 661  
 662      // Return the value
 663      return (bool) $bbp->current_user->is_group_admin;
 664  }
 665  
 666  /**
 667   * Is the current user a moderator of the current group
 668   *
 669   * @since 2.3.0 bbPress (r4632)
 670   *
 671   * @return bool If current user is a moderator of the current group
 672   */
 673  function bbp_group_is_mod() {
 674  
 675      // Bail if user is not logged in or not looking at a group
 676      if ( ! is_user_logged_in() || ! bp_is_group() ) {
 677          return false;
 678      }
 679  
 680      $bbp = bbpress();
 681  
 682      // Set the global if not set
 683      if ( ! isset( $bbp->current_user->is_group_mod ) ) {
 684          $bbp->current_user->is_group_mod = groups_is_user_mod( bp_loggedin_user_id(), bp_get_current_group_id() );
 685      }
 686  
 687      // Return the value
 688      return (bool) $bbp->current_user->is_group_mod;
 689  }
 690  
 691  /**
 692   * Is the current user a member of the current group
 693   *
 694   * @since 2.3.0 bbPress (r4632)
 695   *
 696   * @return bool If current user is a member of the current group
 697   */
 698  function bbp_group_is_member() {
 699  
 700      // Bail if user is not logged in or not looking at a group
 701      if ( ! is_user_logged_in() || ! bp_is_group() ) {
 702          return false;
 703      }
 704  
 705      $bbp = bbpress();
 706  
 707      // Set the global if not set
 708      if ( ! isset( $bbp->current_user->is_group_member ) ) {
 709          $bbp->current_user->is_group_member = groups_is_user_member( bp_loggedin_user_id(), bp_get_current_group_id() );
 710      }
 711  
 712      // Return the value
 713      return (bool) $bbp->current_user->is_group_member;
 714  }
 715  
 716  /**
 717   * Is the current user banned from the current group
 718   *
 719   * @since 2.3.0 bbPress (r4632)
 720   *
 721   * @return bool If current user is banned from the current group
 722   */
 723  function bbp_group_is_banned() {
 724  
 725      // Bail if user is not logged in or not looking at a group
 726      if ( ! is_user_logged_in() || ! bp_is_group() ) {
 727          return false;
 728      }
 729  
 730      $bbp = bbpress();
 731  
 732      // Set the global if not set
 733      if ( ! isset( $bbp->current_user->is_group_banned ) ) {
 734          $bbp->current_user->is_group_banned = groups_is_user_banned( bp_loggedin_user_id(), bp_get_current_group_id() );
 735      }
 736  
 737      // Return the value
 738      return (bool) $bbp->current_user->is_group_banned;
 739  }
 740  
 741  /**
 742   * Is the current user the creator of the current group
 743   *
 744   * @since 2.3.0 bbPress (r4632)
 745   *
 746   * @return bool If current user the creator of the current group
 747   */
 748  function bbp_group_is_creator() {
 749  
 750      // Bail if user is not logged in or not looking at a group
 751      if ( ! is_user_logged_in() || ! bp_is_group() ) {
 752          return false;
 753      }
 754  
 755      $bbp = bbpress();
 756  
 757      // Set the global if not set
 758      if ( ! isset( $bbp->current_user->is_group_creator ) ) {
 759          $bbp->current_user->is_group_creator = groups_is_user_creator( bp_loggedin_user_id(), bp_get_current_group_id() );
 760      }
 761  
 762      // Return the value
 763      return (bool) $bbp->current_user->is_group_creator;
 764  }
 765  
 766  /* BuddyPress Activity Action Callbacks ***************************************/
 767  
 768  /**
 769   * Return an array of allowed activity actions
 770   *
 771   * @since 2.6.0 bbPress (r6370)
 772   *
 773   * @return array
 774   */
 775  function bbp_get_activity_actions() {
 776  
 777      // Filter & return
 778      return (array) apply_filters( 'bbp_get_activity_actions', array(
 779          'topic' => esc_html__( '%1$s started the topic %2$s in the forum %3$s',    'bbpress' ),
 780          'reply' => esc_html__( '%1$s replied to the topic %2$s in the forum %3$s', 'bbpress' )
 781      ) );
 782  }
 783  
 784  /**
 785   * Generic function to format the dynamic BuddyPress activity action for new
 786   * topics/replies.
 787   *
 788   * @since 2.6.0 bbPress (r6370)
 789   *
 790   * @param string               $type     The type of post. Expects `topic` or `reply`.
 791   * @param string               $action   The current action string.
 792   * @param BP_Activity_Activity $activity The BuddyPress activity object.
 793   *
 794   * @return string The formatted activity action.
 795   */
 796  function bbp_format_activity_action_new_post( $type = '', $action = '', $activity = false ) {
 797  
 798      // Get actions
 799      $actions = bbp_get_activity_actions();
 800  
 801      // Bail early if we don't have a valid type
 802      if ( ! in_array( $type, array_keys( $actions ), true ) ) {
 803          return $action;
 804      }
 805  
 806      // Bail if intercepted
 807      $intercept = bbp_maybe_intercept( __FUNCTION__, func_get_args() );
 808      if ( bbp_is_intercepted( $intercept ) ) {
 809          return $intercept;
 810      }
 811  
 812      // Groups component
 813      if ( 'groups' === $activity->component ) {
 814          if ( 'topic' === $type ) {
 815              $topic_id = bbp_get_topic_id( $activity->secondary_item_id );
 816              $forum_id = bbp_get_topic_forum_id( $topic_id );
 817          } else {
 818              $topic_id = bbp_get_reply_topic_id( $activity->secondary_item_id );
 819              $forum_id = bbp_get_topic_forum_id( $topic_id );
 820          }
 821  
 822      // General component (bbpress/forums/other)
 823      } else {
 824          if ( 'topic' === $type ) {
 825              $topic_id = bbp_get_topic_id( $activity->item_id );
 826              $forum_id = bbp_get_forum_id( $activity->secondary_item_id );
 827          } else {
 828              $topic_id = bbp_get_topic_id( $activity->secondary_item_id );
 829              $forum_id = bbp_get_topic_forum_id( $topic_id );
 830          }
 831      }
 832  
 833      // User link for topic author
 834      $user_link = bbp_get_user_profile_link( $activity->user_id );
 835  
 836      // Topic link
 837      $topic_permalink = bbp_get_topic_permalink( $topic_id );
 838      $topic_title     = get_post_field( 'post_title', $topic_id, 'raw' );
 839      $topic_link      = '<a href="' . esc_url( $topic_permalink ) . '">' . esc_html( $topic_title ) . '</a>';
 840  
 841      // Forum link
 842      $forum_permalink = bbp_get_forum_permalink( $forum_id );
 843      $forum_title     = get_post_field( 'post_title', $forum_id, 'raw' );
 844      $forum_link      = '<a href="' . esc_url( $forum_permalink ) . '">' . esc_html( $forum_title ) . '</a>';
 845  
 846      // Format
 847      $activity_action = sprintf( $actions[ $type ], $user_link, $topic_link, $forum_link );
 848  
 849      /**
 850       * Filters the formatted activity action new activity string.
 851       *
 852       * @since 2.6.0 bbPress (r6370)
 853       *
 854       * @param string               $activity_action Activity action string value
 855       * @param string               $type            The type of post. Expects `topic` or `reply`.
 856       * @param string               $action          The current action string.
 857       * @param BP_Activity_Activity $activity        The BuddyPress activity object.
 858       */
 859      return apply_filters( 'bbp_format_activity_action_new_post', $activity_action, $type, $action, $activity );
 860  }
 861  
 862  /**
 863   * Formats the dynamic BuddyPress activity action for new topics.
 864   *
 865   * @since 2.6.0 bbPress (r6370)
 866   *
 867   * @param string $action   The current action string
 868   * @param object $activity The BuddyPress activity object
 869   *
 870   * @return string The formatted activity action.
 871   */
 872  function bbp_format_activity_action_new_topic( $action, $activity ) {
 873      $action = bbp_format_activity_action_new_post( bbp_get_topic_post_type(), $action, $activity );
 874  
 875      /**
 876       * Filters the formatted activity action new topic string.
 877       *
 878       * @since 2.6.0 bbPress (r6370)
 879       *
 880       * @param string               $action   Activity action string value
 881       * @param BP_Activity_Activity $activity Activity item object
 882       */
 883      return apply_filters( 'bbp_format_activity_action_new_topic', $action, $activity );
 884  }
 885  
 886  /**
 887   * Formats the dynamic BuddyPress activity action for new replies.
 888   *
 889   * @since 2.6.0 bbPress (r6370)
 890   *
 891   * @param string $action   The current action string
 892   * @param object $activity The BuddyPress activity object
 893   *
 894   * @return string The formatted activity action
 895   */
 896  function bbp_format_activity_action_new_reply( $action, $activity ) {
 897      $action = bbp_format_activity_action_new_post( bbp_get_reply_post_type(), $action, $activity );
 898  
 899      /**
 900       * Filters the formatted activity action new reply string.
 901       *
 902       * @since 2.6.0 bbPress (r6370)
 903       *
 904       * @param string               $action   Activity action string value
 905       * @param BP_Activity_Activity $activity Activity item object
 906       */
 907      return apply_filters( 'bbp_format_activity_action_new_reply', $action, $activity );
 908  }


Generated: Thu Apr 25 01:01:05 2024 Cross-referenced by PHPXref 0.7.1