[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-groups/ -> bp-groups-functions.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Groups Functions.
   4   *
   5   * Functions are where all the magic happens in BuddyPress. They will
   6   * handle the actual saving or manipulation of information. Usually they will
   7   * hand off to a database class for data access, then return
   8   * true or false on success or failure.
   9   *
  10   * @package BuddyPress
  11   * @subpackage GroupsFunctions
  12   * @since 1.5.0
  13   */
  14  
  15  // Exit if accessed directly.
  16  defined( 'ABSPATH' ) || exit;
  17  
  18  /**
  19   * Check whether there is a Groups directory page in the $bp global.
  20   *
  21   * @since 1.5.0
  22   *
  23   * @return bool True if set, False if empty.
  24   */
  25  function bp_groups_has_directory() {
  26      $bp = buddypress();
  27  
  28      return (bool) !empty( $bp->pages->groups->id );
  29  }
  30  
  31  /**
  32   * Fetch a single group object.
  33   *
  34   * When calling up a group object, you should always use this function instead
  35   * of instantiating BP_Groups_Group directly, so that you will inherit cache
  36   * support and pass through the groups_get_group filter.
  37   *
  38   * @since 1.2.0
  39   * @since 2.7.0 The function signature was changed to accept a group ID only,
  40   *              instead of an array containing the group ID.
  41   *
  42   * @param int $group_id ID of the group.
  43   * @return BP_Groups_Group $group The group object.
  44   */
  45  function groups_get_group( $group_id ) {
  46      /*
  47       * Backward compatibilty.
  48       * Old-style arguments take the form of an array or a query string.
  49       */
  50      if ( ! is_numeric( $group_id ) ) {
  51          $r = bp_parse_args( $group_id, array(
  52              'group_id'        => false,
  53              'load_users'      => false,
  54              'populate_extras' => false,
  55          ), 'groups_get_group' );
  56  
  57          $group_id = $r['group_id'];
  58      }
  59  
  60      $group = new BP_Groups_Group( $group_id );
  61  
  62      /**
  63       * Filters a single group object.
  64       *
  65       * @since 1.2.0
  66       *
  67       * @param BP_Groups_Group $group Single group object.
  68       */
  69      return apply_filters( 'groups_get_group', $group );
  70  }
  71  
  72  /** Group Creation, Editing & Deletion ****************************************/
  73  
  74  /**
  75   * Create a group.
  76   *
  77   * @since 1.0.0
  78   *
  79   * @param array|string $args {
  80   *     An array of arguments.
  81   *     @type int|bool $group_id     Pass a group ID to update an existing item, or
  82   *                                  0 / false to create a new group. Default: 0.
  83   *     @type int      $creator_id   The user ID that creates the group.
  84   *     @type string   $name         The group name.
  85   *     @type string   $description  Optional. The group's description.
  86   *     @type string   $slug         The group slug.
  87   *     @type string   $status       The group's status. Accepts 'public', 'private' or
  88   *                                  'hidden'. Defaults to 'public'.
  89   *     @type int      $parent_id    The ID of the parent group. Default: 0.
  90   *     @type int      $enable_forum Optional. Whether the group has a forum enabled.
  91   *                                  If a bbPress forum is enabled for the group,
  92   *                                  set this to 1. Default: 0.
  93   *     @type string   $date_created The GMT time, in Y-m-d h:i:s format, when the group
  94   *                                  was created. Defaults to the current time.
  95   * }
  96   * @return int|bool The ID of the group on success. False on error.
  97   */
  98  function groups_create_group( $args = '' ) {
  99  
 100      $args = bp_parse_args( $args, array(
 101          'group_id'     => 0,
 102          'creator_id'   => 0,
 103          'name'         => '',
 104          'description'  => '',
 105          'slug'         => '',
 106          'status'       => null,
 107          'parent_id'    => null,
 108          'enable_forum' => null,
 109          'date_created' => null
 110      ), 'groups_create_group' );
 111  
 112      extract( $args, EXTR_SKIP );
 113  
 114      // Pass an existing group ID.
 115      if ( ! empty( $group_id ) ) {
 116          $group = groups_get_group( $group_id );
 117          $name  = ! empty( $name ) ? $name : $group->name;
 118          $slug  = ! empty( $slug ) ? $slug : $group->slug;
 119          $creator_id  = ! empty( $creator_id ) ? $creator_id : $group->creator_id;
 120          $description = ! empty( $description ) ? $description : $group->description;
 121          $status = ! is_null( $status ) ? $status : $group->status;
 122          $parent_id = ! is_null( $parent_id ) ? $parent_id : $group->parent_id;
 123          $enable_forum = ! is_null( $enable_forum ) ? $enable_forum : $group->enable_forum;
 124          $date_created = ! is_null( $date_created ) ? $date_created : $group->date_created;
 125  
 126          // Groups need at least a name.
 127          if ( empty( $name ) ) {
 128              return false;
 129          }
 130  
 131      // Create a new group.
 132      } else {
 133          // Instantiate new group object.
 134          $group = new BP_Groups_Group;
 135  
 136          // Check for null values, reset to sensible defaults.
 137          $status = ! is_null( $status ) ? $status : 'public';
 138          $parent_id = ! is_null( $parent_id ) ? $parent_id : 0;
 139          $enable_forum = ! is_null( $enable_forum ) ? $enable_forum : 0;
 140          $date_created = ! is_null( $date_created ) ? $date_created : bp_core_current_time();
 141      }
 142  
 143      // Set creator ID.
 144      if ( $creator_id ) {
 145          $group->creator_id = (int) $creator_id;
 146      } elseif ( is_user_logged_in() ) {
 147          $group->creator_id = bp_loggedin_user_id();
 148      }
 149  
 150      if ( ! $group->creator_id ) {
 151          return false;
 152      }
 153  
 154      // Validate status.
 155      if ( ! groups_is_valid_status( $status ) ) {
 156          return false;
 157      }
 158  
 159      // Set group name.
 160      $group->name         = $name;
 161      $group->description  = $description;
 162      $group->slug         = $slug;
 163      $group->status       = $status;
 164      $group->parent_id    = $parent_id;
 165      $group->enable_forum = (int) $enable_forum;
 166      $group->date_created = $date_created;
 167  
 168      // Save group.
 169      if ( ! $group->save() ) {
 170          return false;
 171      }
 172  
 173      // If this is a new group, set up the creator as the first member and admin.
 174      if ( empty( $group_id ) ) {
 175          $member                = new BP_Groups_Member;
 176          $member->group_id      = $group->id;
 177          $member->user_id       = $group->creator_id;
 178          $member->is_admin      = 1;
 179          $member->user_title    = __( 'Group Admin', 'buddypress' );
 180          $member->is_confirmed  = 1;
 181          $member->date_modified = bp_core_current_time();
 182          $member->save();
 183  
 184          /**
 185           * Fires after the creation of a new group and a group creator needs to be made.
 186           *
 187           * @since 1.5.0
 188           *
 189           * @param int              $id     ID of the newly created group.
 190           * @param BP_Groups_Member $member Instance of the member who is assigned
 191           *                                 as group creator.
 192           * @param BP_Groups_Group  $group  Instance of the group being created.
 193           */
 194          do_action( 'groups_create_group', $group->id, $member, $group );
 195  
 196      } else {
 197  
 198          /**
 199           * Fires after the update of a group.
 200           *
 201           * @since 1.5.0
 202           *
 203           * @param int             $id    ID of the updated group.
 204           * @param BP_Groups_Group $group Instance of the group being updated.
 205           */
 206          do_action( 'groups_update_group', $group->id, $group );
 207      }
 208  
 209      /**
 210       * Fires after the creation or update of a group.
 211       *
 212       * @since 1.0.0
 213       *
 214       * @param int             $id    ID of the newly created group.
 215       * @param BP_Groups_Group $group Instance of the group being updated.
 216       */
 217      do_action( 'groups_created_group', $group->id, $group );
 218  
 219      return $group->id;
 220  }
 221  
 222  /**
 223   * Edit the base details for a group.
 224   *
 225   * These are the settings that appear on the first page of the group's Admin
 226   * section (Name, Description, and "Notify members...").
 227   *
 228   * @since 1.0.0
 229   *
 230   * @param array $args {
 231   *     An array of optional arguments.
 232   *     @type int    $group_id       ID of the group.
 233   *     @type string $name           Name of the group.
 234   *     @type string $slug           Slug of the group.
 235   *     @type string $description    Description of the group.
 236   *     @type bool   $notify_members Whether to send an email notification to group
 237   *                                  members about changes in these details.
 238   * }
 239   * @return bool True on success, false on failure.
 240   */
 241  function groups_edit_base_group_details( $args = array() ) {
 242      $function_args = func_get_args();
 243  
 244      // Backward compatibility with old method of passing arguments.
 245      if ( ! is_array( $args ) || count( $function_args ) > 1 ) {
 246          _deprecated_argument( __METHOD__, '2.9.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
 247  
 248          $old_args_keys = array(
 249              0 => 'group_id',
 250              1 => 'name',
 251              2 => 'description',
 252              3 => 'notify_members',
 253          );
 254  
 255          $args = bp_core_parse_args_array( $old_args_keys, $function_args );
 256      }
 257  
 258      $r = bp_parse_args( $args, array(
 259          'group_id'       => bp_get_current_group_id(),
 260          'name'           => null,
 261          'slug'           => null,
 262          'description'    => null,
 263          'notify_members' => false,
 264      ), 'groups_edit_base_group_details' );
 265  
 266      if ( ! $r['group_id'] ) {
 267          return false;
 268      }
 269  
 270      $group     = groups_get_group( $r['group_id'] );
 271      $old_group = clone $group;
 272  
 273      // Group name, slug and description can never be empty. Update only if provided.
 274      if ( $r['name'] ) {
 275          $group->name = $r['name'];
 276      }
 277      if ( $r['slug'] && $r['slug'] != $group->slug ) {
 278          $group->slug = groups_check_slug( $r['slug'] );
 279      }
 280      if ( $r['description'] ) {
 281          $group->description = $r['description'];
 282      }
 283  
 284      if ( ! $group->save() ) {
 285          return false;
 286      }
 287  
 288      // Maybe update the "previous_slug" groupmeta.
 289      if ( $group->slug != $old_group->slug ) {
 290          /*
 291           * If the old slug exists in this group's past, delete that entry.
 292           * Recent previous_slugs are preferred when selecting the current group
 293           * from an old group slug, so we want the previous slug to be
 294           * saved "now" in the groupmeta table and don't need the old record.
 295           */
 296          groups_delete_groupmeta( $group->id, 'previous_slug', $old_group->slug );
 297          groups_add_groupmeta( $group->id, 'previous_slug', $old_group->slug );
 298      }
 299  
 300      if ( $r['notify_members'] ) {
 301          groups_notification_group_updated( $group->id, $old_group );
 302      }
 303  
 304      /**
 305       * Fired after a group's details are updated.
 306       *
 307       * @since 2.2.0
 308       *
 309       * @param int             $value          ID of the group.
 310       * @param BP_Groups_Group $old_group      Group object, before being modified.
 311       * @param bool            $notify_members Whether to send an email notification to members about the change.
 312       */
 313      do_action( 'groups_details_updated', $group->id, $old_group, $r['notify_members'] );
 314  
 315      return true;
 316  }
 317  
 318  /**
 319   * Edit the base details for a group.
 320   *
 321   * These are the settings that appear on the Settings page of the group's Admin
 322   * section (privacy settings, "enable forum", invitation status).
 323   *
 324   * @since 1.0.0
 325   *
 326   * @param int         $group_id      ID of the group.
 327   * @param bool        $enable_forum  Whether to enable a forum for the group.
 328   * @param string      $status        Group status. 'public', 'private', 'hidden'.
 329   * @param string|bool $invite_status Optional. Who is allowed to send invitations
 330   *                                   to the group. 'members', 'mods', or 'admins'.
 331   * @param int|bool    $parent_id     Parent group ID.
 332   * @return bool True on success, false on failure.
 333   */
 334  function groups_edit_group_settings( $group_id, $enable_forum, $status, $invite_status = false, $parent_id = false ) {
 335  
 336      $group = groups_get_group( $group_id );
 337      $group->enable_forum = $enable_forum;
 338  
 339      /**
 340       * Before we potentially switch the group status, if it has been changed to public
 341       * from private and there are outstanding membership requests, auto-accept those requests.
 342       */
 343      if ( 'private' == $group->status && 'public' == $status )
 344          groups_accept_all_pending_membership_requests( $group->id );
 345  
 346      // Now update the status.
 347      $group->status = $status;
 348  
 349      // Update the parent ID if necessary.
 350      if ( false !== $parent_id ) {
 351          $group->parent_id = $parent_id;
 352      }
 353  
 354      if ( !$group->save() )
 355          return false;
 356  
 357      // Set the invite status.
 358      if ( $invite_status )
 359          groups_update_groupmeta( $group->id, 'invite_status', $invite_status );
 360  
 361      groups_update_groupmeta( $group->id, 'last_activity', bp_core_current_time() );
 362  
 363      /**
 364       * Fires after the update of a groups settings.
 365       *
 366       * @since 1.0.0
 367       *
 368       * @param int $id ID of the group that was updated.
 369       */
 370      do_action( 'groups_settings_updated', $group->id );
 371  
 372      return true;
 373  }
 374  
 375  /**
 376   * Delete a group and all of its associated metadata.
 377   *
 378   * @since 1.0.0
 379   *
 380   * @param int $group_id ID of the group to delete.
 381   * @return bool True on success, false on failure.
 382   */
 383  function groups_delete_group( $group_id ) {
 384  
 385      /**
 386       * Fires before the deletion of a group.
 387       *
 388       * @since 1.5.0
 389       *
 390       * @param int $group_id ID of the group to be deleted.
 391       */
 392      do_action( 'groups_before_delete_group', $group_id );
 393  
 394      // Get the group object.
 395      $group = groups_get_group( $group_id );
 396  
 397      // Bail if group cannot be deleted.
 398      if ( ! $group->delete() ) {
 399          return false;
 400      }
 401  
 402      // Remove all outstanding invites for this group.
 403      groups_delete_all_group_invites( $group_id );
 404  
 405      /**
 406       * Fires after the deletion of a group.
 407       *
 408       * @since 1.0.0
 409       *
 410       * @param int $group_id ID of the group that was deleted.
 411       */
 412      do_action( 'groups_delete_group', $group_id );
 413  
 414      return true;
 415  }
 416  
 417  /**
 418   * Check a group status (eg 'private') against the whitelist of registered statuses.
 419   *
 420   * @since 1.1.0
 421   *
 422   * @param string $status Status to check.
 423   * @return bool True if status is allowed, otherwise false.
 424   */
 425  function groups_is_valid_status( $status ) {
 426      $bp = buddypress();
 427  
 428      return in_array( $status, (array) $bp->groups->valid_status );
 429  }
 430  
 431  /**
 432   * Provide a unique, sanitized version of a group slug.
 433   *
 434   * @since 1.0.0
 435   *
 436   * @param string $slug Group slug to check.
 437   * @return string $slug A unique and sanitized slug.
 438   */
 439  function groups_check_slug( $slug ) {
 440      $bp = buddypress();
 441  
 442      // First, make the proposed slug work in a URL.
 443      $slug = sanitize_title( $slug );
 444  
 445      if ( 'wp' == substr( $slug, 0, 2 ) )
 446          $slug = substr( $slug, 2, strlen( $slug ) - 2 );
 447  
 448      if ( in_array( $slug, (array) $bp->groups->forbidden_names ) )
 449          $slug = $slug . '-' . rand();
 450  
 451      if ( BP_Groups_Group::check_slug( $slug ) ) {
 452          do {
 453              $slug = $slug . '-' . rand();
 454          }
 455          while ( BP_Groups_Group::check_slug( $slug ) );
 456      }
 457  
 458      return $slug;
 459  }
 460  
 461  /**
 462   * Get a group slug by its ID.
 463   *
 464   * @since 1.0.0
 465   *
 466   * @param int $group_id The numeric ID of the group.
 467   * @return string The group's slug.
 468   */
 469  function groups_get_slug( $group_id ) {
 470      $group = groups_get_group( $group_id );
 471      return !empty( $group->slug ) ? $group->slug : '';
 472  }
 473  
 474  /**
 475   * Get a group ID by its slug.
 476   *
 477   * @since 1.6.0
 478   *
 479   * @param string $group_slug The group's slug.
 480   * @return int|null The group ID on success; null on failure.
 481   */
 482  function groups_get_id( $group_slug ) {
 483      return BP_Groups_Group::group_exists( $group_slug );
 484  }
 485  
 486  /**
 487   * Get a group ID by checking against old (not currently active) slugs.
 488   *
 489   * @since 2.9.0
 490   *
 491   * @param string $group_slug The group's slug.
 492   * @return int|null The group ID on success; null on failure.
 493   */
 494  function groups_get_id_by_previous_slug( $group_slug ) {
 495      return BP_Groups_Group::get_id_by_previous_slug( $group_slug );
 496  }
 497  
 498  /** User Actions **************************************************************/
 499  
 500  /**
 501   * Remove a user from a group.
 502   *
 503   * @since 1.0.0
 504   *
 505   * @param int $group_id ID of the group.
 506   * @param int $user_id  Optional. ID of the user. Defaults to the currently
 507   *                      logged-in user.
 508   * @return bool True on success, false on failure.
 509   */
 510  function groups_leave_group( $group_id, $user_id = 0 ) {
 511  
 512      if ( empty( $user_id ) )
 513          $user_id = bp_loggedin_user_id();
 514  
 515      // Don't let single admins leave the group.
 516      if ( count( groups_get_group_admins( $group_id ) ) < 2 ) {
 517          if ( groups_is_user_admin( $user_id, $group_id ) ) {
 518              bp_core_add_message( __( 'As the only admin, you cannot leave the group.', 'buddypress' ), 'error' );
 519              return false;
 520          }
 521      }
 522  
 523      if ( ! BP_Groups_Member::delete( $user_id, $group_id ) ) {
 524          return false;
 525      }
 526  
 527      bp_core_add_message( __( 'You successfully left the group.', 'buddypress' ) );
 528  
 529      /**
 530       * Fires after a user leaves a group.
 531       *
 532       * @since 1.0.0
 533       *
 534       * @param int $group_id ID of the group.
 535       * @param int $user_id  ID of the user leaving the group.
 536       */
 537      do_action( 'groups_leave_group', $group_id, $user_id );
 538  
 539      return true;
 540  }
 541  
 542  /**
 543   * Add a user to a group.
 544   *
 545   * @since 1.0.0
 546   *
 547   * @param int $group_id ID of the group.
 548   * @param int $user_id  Optional. ID of the user. Defaults to the currently
 549   *                      logged-in user.
 550   * @return bool True on success, false on failure.
 551   */
 552  function groups_join_group( $group_id, $user_id = 0 ) {
 553  
 554      if ( empty( $user_id ) )
 555          $user_id = bp_loggedin_user_id();
 556  
 557      // Check if the user has an outstanding invite. If so, delete it.
 558      if ( groups_check_user_has_invite( $user_id, $group_id ) )
 559          groups_delete_invite( $user_id, $group_id );
 560  
 561      // Check if the user has an outstanding request. If so, delete it.
 562      if ( groups_check_for_membership_request( $user_id, $group_id ) )
 563          groups_delete_membership_request( null, $user_id, $group_id );
 564  
 565      // User is already a member, just return true.
 566      if ( groups_is_user_member( $user_id, $group_id ) )
 567          return true;
 568  
 569      $new_member                = new BP_Groups_Member;
 570      $new_member->group_id      = $group_id;
 571      $new_member->user_id       = $user_id;
 572      $new_member->inviter_id    = 0;
 573      $new_member->is_admin      = 0;
 574      $new_member->user_title    = '';
 575      $new_member->date_modified = bp_core_current_time();
 576      $new_member->is_confirmed  = 1;
 577  
 578      if ( !$new_member->save() )
 579          return false;
 580  
 581      $bp = buddypress();
 582  
 583      if ( !isset( $bp->groups->current_group ) || !$bp->groups->current_group || $group_id != $bp->groups->current_group->id )
 584          $group = groups_get_group( $group_id );
 585      else
 586          $group = $bp->groups->current_group;
 587  
 588      // Record this in activity streams.
 589      if ( bp_is_active( 'activity' ) ) {
 590          groups_record_activity( array(
 591              'type'    => 'joined_group',
 592              'item_id' => $group_id,
 593              'user_id' => $user_id,
 594          ) );
 595      }
 596  
 597      /**
 598       * Fires after a user joins a group.
 599       *
 600       * @since 1.0.0
 601       *
 602       * @param int $group_id ID of the group.
 603       * @param int $user_id  ID of the user joining the group.
 604       */
 605      do_action( 'groups_join_group', $group_id, $user_id );
 606  
 607      return true;
 608  }
 609  
 610  /**
 611   * Update the last_activity meta value for a given group.
 612   *
 613   * @since 1.0.0
 614   *
 615   * @param int $group_id Optional. The ID of the group whose last_activity is
 616   *                      being updated. Default: the current group's ID.
 617   * @return false|null False on failure.
 618   */
 619  function groups_update_last_activity( $group_id = 0 ) {
 620  
 621      if ( empty( $group_id ) ) {
 622          $group_id = buddypress()->groups->current_group->id;
 623      }
 624  
 625      if ( empty( $group_id ) ) {
 626          return false;
 627      }
 628  
 629      groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
 630  }
 631  add_action( 'groups_join_group',           'groups_update_last_activity' );
 632  add_action( 'groups_leave_group',          'groups_update_last_activity' );
 633  add_action( 'groups_created_group',        'groups_update_last_activity' );
 634  
 635  /** General Group Functions ***************************************************/
 636  
 637  /**
 638   * Get a list of group administrators.
 639   *
 640   * @since 1.0.0
 641   *
 642   * @param int $group_id ID of the group.
 643   * @return array Info about group admins (user_id + date_modified).
 644   */
 645  function groups_get_group_admins( $group_id ) {
 646      return BP_Groups_Member::get_group_administrator_ids( $group_id );
 647  }
 648  
 649  /**
 650   * Get a list of group moderators.
 651   *
 652   * @since 1.0.0
 653   *
 654   * @param int $group_id ID of the group.
 655   * @return array Info about group admins (user_id + date_modified).
 656   */
 657  function groups_get_group_mods( $group_id ) {
 658      return BP_Groups_Member::get_group_moderator_ids( $group_id );
 659  }
 660  
 661  /**
 662   * Fetch the members of a group.
 663   *
 664   * Since BuddyPress 1.8, a procedural wrapper for BP_Group_Member_Query.
 665   * Previously called BP_Groups_Member::get_all_for_group().
 666   *
 667   * To use the legacy query, filter 'bp_use_legacy_group_member_query',
 668   * returning true.
 669   *
 670   * @since 1.0.0
 671   * @since 3.0.0 $group_id now supports multiple values. Only works if legacy query is not
 672   *              in use.
 673   *
 674   * @param array $args {
 675   *     An array of optional arguments.
 676   *     @type int|array|string $group_id            ID of the group to limit results to. Also accepts multiple values
 677   *                                                 either as an array or as a comma-delimited string.
 678   *     @type int              $page                Page of results to be queried. Default: 1.
 679   *     @type int              $per_page            Number of items to return per page of results. Default: 20.
 680   *     @type int              $max                 Optional. Max number of items to return.
 681   *     @type array            $exclude             Optional. Array of user IDs to exclude.
 682   *     @type bool|int         $exclude_admins_mods True (or 1) to exclude admins and mods from results. Default: 1.
 683   *     @type bool|int         $exclude_banned      True (or 1) to exclude banned users from results. Default: 1.
 684   *     @type array            $group_role          Optional. Array of group roles to include.
 685   *     @type string           $search_terms        Optional. Filter results by a search string.
 686   *     @type string           $type                Optional. Sort the order of results. 'last_joined', 'first_joined', or
 687   *                                                 any of the $type params available in {@link BP_User_Query}. Default:
 688   *                                                 'last_joined'.
 689   * }
 690   * @return false|array Multi-d array of 'members' list and 'count'.
 691   */
 692  function groups_get_group_members( $args = array() ) {
 693      $function_args = func_get_args();
 694  
 695      // Backward compatibility with old method of passing arguments.
 696      if ( ! is_array( $args ) || count( $function_args ) > 1 ) {
 697          _deprecated_argument( __METHOD__, '2.0.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
 698  
 699          $old_args_keys = array(
 700              0 => 'group_id',
 701              1 => 'per_page',
 702              2 => 'page',
 703              3 => 'exclude_admins_mods',
 704              4 => 'exclude_banned',
 705              5 => 'exclude',
 706              6 => 'group_role',
 707          );
 708  
 709          $args = bp_core_parse_args_array( $old_args_keys, $function_args );
 710      }
 711  
 712      $r = bp_parse_args( $args, array(
 713          'group_id'            => bp_get_current_group_id(),
 714          'per_page'            => false,
 715          'page'                => false,
 716          'exclude_admins_mods' => true,
 717          'exclude_banned'      => true,
 718          'exclude'             => false,
 719          'group_role'          => array(),
 720          'search_terms'        => false,
 721          'type'                => 'last_joined',
 722      ), 'groups_get_group_members' );
 723  
 724      // For legacy users. Use of BP_Groups_Member::get_all_for_group() is deprecated.
 725      if ( apply_filters( 'bp_use_legacy_group_member_query', false, __FUNCTION__, $function_args ) ) {
 726          $retval = BP_Groups_Member::get_all_for_group( $r['group_id'], $r['per_page'], $r['page'], $r['exclude_admins_mods'], $r['exclude_banned'], $r['exclude'] );
 727      } else {
 728  
 729          // Both exclude_admins_mods and exclude_banned are legacy arguments.
 730          // Convert to group_role.
 731          if ( empty( $r['group_role'] ) ) {
 732              $r['group_role'] = array( 'member' );
 733  
 734              if ( ! $r['exclude_admins_mods'] ) {
 735                  $r['group_role'][] = 'mod';
 736                  $r['group_role'][] = 'admin';
 737              }
 738  
 739              if ( ! $r['exclude_banned'] ) {
 740                  $r['group_role'][] = 'banned';
 741              }
 742          }
 743  
 744          // Perform the group member query (extends BP_User_Query).
 745          $members = new BP_Group_Member_Query( array(
 746              'group_id'       => $r['group_id'],
 747              'per_page'       => $r['per_page'],
 748              'page'           => $r['page'],
 749              'group_role'     => $r['group_role'],
 750              'exclude'        => $r['exclude'],
 751              'search_terms'   => $r['search_terms'],
 752              'type'           => $r['type'],
 753          ) );
 754  
 755          // Structure the return value as expected by the template functions.
 756          $retval = array(
 757              'members' => array_values( $members->results ),
 758              'count'   => $members->total_users,
 759          );
 760      }
 761  
 762      return $retval;
 763  }
 764  
 765  /**
 766   * Get the member count for a group.
 767   *
 768   * @since 1.2.3
 769   *
 770   * @param int $group_id Group ID.
 771   * @return int Count of confirmed members for the group.
 772   */
 773  function groups_get_total_member_count( $group_id ) {
 774      return BP_Groups_Group::get_total_member_count( $group_id );
 775  }
 776  
 777  /** Group Fetching, Filtering & Searching  ************************************/
 778  
 779  /**
 780   * Get a collection of groups, based on the parameters passed.
 781   *
 782   * @since 1.2.0
 783   * @since 2.6.0 Added `$group_type`, `$group_type__in`, and `$group_type__not_in` parameters.
 784   * @since 2.7.0 Added `$update_admin_cache` and `$parent_id` parameters.
 785   *
 786   * @param array|string $args {
 787   *     Array of arguments. Supports all arguments of
 788   *     {@link BP_Groups_Group::get()}. Where the default values differ, they
 789   *     have been described here.
 790   *     @type int $per_page Default: 20.
 791   *     @type int $page Default: 1.
 792   * }
 793   * @return array See {@link BP_Groups_Group::get()}.
 794   */
 795  function groups_get_groups( $args = '' ) {
 796  
 797      $defaults = array(
 798          'type'               => false,          // Active, newest, alphabetical, random, popular.
 799          'order'              => 'DESC',         // 'ASC' or 'DESC'
 800          'orderby'            => 'date_created', // date_created, last_activity, total_member_count, name, random, meta_id.
 801          'user_id'            => false,          // Pass a user_id to limit to only groups that this user is a member of.
 802          'include'            => false,          // Only include these specific groups (group_ids).
 803          'exclude'            => false,          // Do not include these specific groups (group_ids).
 804          'parent_id'          => null,           // Get groups that are children of the specified group(s).
 805          'slug'               => array(),        // Find a group or groups by slug.
 806          'search_terms'       => false,          // Limit to groups that match these search terms.
 807          'search_columns'     => array(),        // Select which columns to search.
 808          'group_type'         => '',             // Array or comma-separated list of group types to limit results to.
 809          'group_type__in'     => '',             // Array or comma-separated list of group types to limit results to.
 810          'group_type__not_in' => '',             // Array or comma-separated list of group types that will be excluded from results.
 811          'meta_query'         => false,          // Filter by groupmeta. See WP_Meta_Query for syntax.
 812          'show_hidden'        => false,          // Show hidden groups to non-admins.
 813          'status'             => array(),        // Array or comma-separated list of group statuses to limit results to.
 814          'per_page'           => 20,             // The number of results to return per page.
 815          'page'               => 1,              // The page to return if limiting per page.
 816          'update_meta_cache'  => true,           // Pre-fetch groupmeta for queried groups.
 817          'update_admin_cache' => false,
 818          'fields'             => 'all',          // Return BP_Groups_Group objects or a list of ids.
 819      );
 820  
 821      $r = bp_parse_args( $args, $defaults, 'groups_get_groups' );
 822  
 823      $groups = BP_Groups_Group::get( array(
 824          'type'               => $r['type'],
 825          'user_id'            => $r['user_id'],
 826          'include'            => $r['include'],
 827          'exclude'            => $r['exclude'],
 828          'slug'               => $r['slug'],
 829          'parent_id'          => $r['parent_id'],
 830          'search_terms'       => $r['search_terms'],
 831          'search_columns'     => $r['search_columns'],
 832          'group_type'         => $r['group_type'],
 833          'group_type__in'     => $r['group_type__in'],
 834          'group_type__not_in' => $r['group_type__not_in'],
 835          'meta_query'         => $r['meta_query'],
 836          'show_hidden'        => $r['show_hidden'],
 837          'status'             => $r['status'],
 838          'per_page'           => $r['per_page'],
 839          'page'               => $r['page'],
 840          'update_meta_cache'  => $r['update_meta_cache'],
 841          'update_admin_cache' => $r['update_admin_cache'],
 842          'order'              => $r['order'],
 843          'orderby'            => $r['orderby'],
 844          'fields'             => $r['fields'],
 845      ) );
 846  
 847      /**
 848       * Filters the collection of groups based on parsed parameters.
 849       *
 850       * @since 1.2.0
 851       *
 852       * @param BP_Groups_Group $groups Object of found groups based on parameters.
 853       *                                Passed by reference.
 854       * @param array           $r      Array of parsed arguments used for group query.
 855       *                                Passed by reference.
 856       */
 857      return apply_filters_ref_array( 'groups_get_groups', array( &$groups, &$r ) );
 858  }
 859  
 860  /**
 861   * Get the total group count for the site.
 862   *
 863   * @since 1.2.0
 864   *
 865   * @return int
 866   */
 867  function groups_get_total_group_count() {
 868      $count = wp_cache_get( 'bp_total_group_count', 'bp' );
 869  
 870      if ( false === $count ) {
 871          $count = BP_Groups_Group::get_total_group_count();
 872          wp_cache_set( 'bp_total_group_count', $count, 'bp' );
 873      }
 874  
 875      return $count;
 876  }
 877  
 878  /**
 879   * Get the IDs of the groups of which a specified user is a member.
 880   *
 881   * @since 1.0.0
 882   *
 883   * @param int $user_id  ID of the user.
 884   * @param int $pag_num  Optional. Max number of results to return.
 885   *                      Default: false (no limit).
 886   * @param int $pag_page Optional. Page offset of results to return.
 887   *                      Default: false (no limit).
 888   * @return array {
 889   *     @type array $groups Array of groups returned by paginated query.
 890   *     @type int   $total Count of groups matching query.
 891   * }
 892   */
 893  function groups_get_user_groups( $user_id = 0, $pag_num = 0, $pag_page = 0 ) {
 894  
 895      if ( empty( $user_id ) )
 896          $user_id = bp_displayed_user_id();
 897  
 898      return BP_Groups_Member::get_group_ids( $user_id, $pag_num, $pag_page );
 899  }
 900  
 901  /**
 902   * Get a list of groups of which the specified user is a member.
 903   *
 904   * Get a list of the groups to which this member belongs,
 905   * filtered by group membership status and role.
 906   * Usage examples: Used with no arguments specified,
 907   *
 908   *    bp_get_user_groups( bp_loggedin_user_id() );
 909   *
 910   * returns an array of the groups in which the logged-in user
 911   * is an unpromoted member. To fetch an array of all groups that
 912   * the current user belongs to, in any membership role,
 913   * member, moderator or administrator, use
 914   *
 915   *    bp_get_user_groups( $user_id, array(
 916   *        'is_admin' => null,
 917   *        'is_mod' => null,
 918   *    ) );
 919   *
 920   * @since 2.6.0
 921   *
 922   * @param int $user_id ID of the user.
 923   * @param array $args {
 924   *     Array of optional args.
 925   *     @param bool|null   $is_confirmed Whether to return only confirmed memberships. Pass `null` to disable this
 926   *                                      filter. Default: true.
 927   *     @param bool|null   $is_banned    Whether to return only banned memberships. Pass `null` to disable this filter.
 928   *                                      Default: false.
 929   *     @param bool|null   $is_admin     Whether to return only admin memberships. Pass `null` to disable this filter.
 930   *                                      Default: false.
 931   *     @param bool|null   $is_mod       Whether to return only mod memberships. Pass `null` to disable this filter.
 932   *                                      Default: false.
 933   *     @param bool|null   $invite_sent  Whether to return only memberships with 'invite_sent'. Pass `null` to disable
 934   *                                      this filter. Default: false.
 935   *     @param string      $orderby      Field to order by. Accepts 'id' (membership ID), 'group_id', 'date_modified'.
 936   *                                      Default: 'group_id'.
 937   *     @param string      $order        Sort order. Accepts 'ASC' or 'DESC'. Default: 'ASC'.
 938   * }
 939   * @return array Array of matching group memberships, keyed by group ID.
 940   */
 941  function bp_get_user_groups( $user_id, $args = array() ) {
 942      $r = bp_parse_args( $args, array(
 943          'is_confirmed' => true,
 944          'is_banned'    => false,
 945          'is_admin'     => false,
 946          'is_mod'       => false,
 947          'invite_sent'  => null,
 948          'orderby'      => 'group_id',
 949          'order'        => 'ASC',
 950      ), 'get_user_groups' );
 951  
 952      $user_id = intval( $user_id );
 953  
 954      // Standard memberships
 955      $membership_ids = wp_cache_get( $user_id, 'bp_groups_memberships_for_user' );
 956      if ( false === $membership_ids ) {
 957          $membership_ids = BP_Groups_Member::get_membership_ids_for_user( $user_id );
 958          wp_cache_set( $user_id, $membership_ids, 'bp_groups_memberships_for_user' );
 959      }
 960  
 961      // Prime the membership cache.
 962      $uncached_membership_ids = bp_get_non_cached_ids( $membership_ids, 'bp_groups_memberships' );
 963      if ( ! empty( $uncached_membership_ids ) ) {
 964          $uncached_memberships = BP_Groups_Member::get_memberships_by_id( $uncached_membership_ids );
 965  
 966          foreach ( $uncached_memberships as $uncached_membership ) {
 967              wp_cache_set( $uncached_membership->id, $uncached_membership, 'bp_groups_memberships' );
 968          }
 969      }
 970  
 971      // Prime the invitations- and requests-as-memberships cache
 972      $invitation_ids = array();
 973      if ( true !== $r['is_confirmed'] || false !== $r['invite_sent'] ) {
 974          $invitation_ids = groups_get_invites( array(
 975              'user_id'     => $user_id,
 976              'invite_sent' => 'all',
 977              'type'        => 'all',
 978              'fields'      => 'ids'
 979          ) );
 980  
 981          // Prime the invitations cache.
 982          $uncached_invitation_ids = bp_get_non_cached_ids( $invitation_ids, 'bp_groups_invitations_as_memberships' );
 983  
 984          $uncached_invitations = groups_get_invites( array(
 985              'ids'         => $uncached_invitation_ids,
 986              'invite_sent' => 'all',
 987              'type'        => 'all'
 988          ) );
 989          foreach ( $uncached_invitations as $uncached_invitation ) {
 990              // Reshape the result as a membership db entry.
 991              $invitation = new StdClass;
 992              $invitation->id            = $uncached_invitation->id;
 993              $invitation->group_id      = $uncached_invitation->item_id;
 994              $invitation->user_id       = $uncached_invitation->user_id;
 995              $invitation->inviter_id    = $uncached_invitation->inviter_id;
 996              $invitation->is_admin      = false;
 997              $invitation->is_mod           = false;
 998              $invitation->user_title    = '';
 999              $invitation->date_modified = $uncached_invitation->date_modified;
1000              $invitation->comments      = $uncached_invitation->content;
1001              $invitation->is_confirmed  = false;
1002              $invitation->is_banned     = false;
1003              $invitation->invite_sent   = $uncached_invitation->invite_sent;
1004              wp_cache_set( $uncached_invitation->id, $invitation, 'bp_groups_invitations_as_memberships' );
1005          }
1006  
1007      }
1008  
1009  
1010      // Assemble filter array for use in `wp_list_filter()`.
1011      $filters = wp_array_slice_assoc( $r, array( 'is_confirmed', 'is_banned', 'is_admin', 'is_mod', 'invite_sent' ) );
1012      foreach ( $filters as $filter_name => $filter_value ) {
1013          if ( is_null( $filter_value ) ) {
1014              unset( $filters[ $filter_name ] );
1015          }
1016      }
1017  
1018      // Populate group membership array from cache, and normalize.
1019      $groups    = array();
1020      $int_keys  = array( 'id', 'group_id', 'user_id', 'inviter_id' );
1021      $bool_keys = array( 'is_admin', 'is_mod', 'is_confirmed', 'is_banned', 'invite_sent' );
1022      foreach ( $membership_ids as $membership_id ) {
1023          $membership = wp_cache_get( $membership_id, 'bp_groups_memberships' );
1024  
1025          // Sanity check.
1026          if ( ! isset( $membership->group_id ) ) {
1027              continue;
1028          }
1029  
1030          // Integer values.
1031          foreach ( $int_keys as $index ) {
1032              $membership->{$index} = intval( $membership->{$index} );
1033          }
1034  
1035          // Boolean values.
1036          foreach ( $bool_keys as $index ) {
1037              $membership->{$index} = (bool) $membership->{$index};
1038          }
1039  
1040          foreach ( $filters as $filter_name => $filter_value ) {
1041              if ( ! isset( $membership->{$filter_name} ) || $filter_value != $membership->{$filter_name} ) {
1042                  continue 2;
1043              }
1044          }
1045  
1046          $group_id = (int) $membership->group_id;
1047  
1048          $groups[ $group_id ] = $membership;
1049      }
1050  
1051      // Populate group invitations array from cache, and normalize.
1052      foreach ( $invitation_ids as $invitation_id ) {
1053          $invitation = wp_cache_get( $invitation_id, 'bp_groups_invitations_as_memberships' );
1054  
1055          // Sanity check.
1056          if ( ! isset( $invitation->group_id ) ) {
1057              continue;
1058          }
1059  
1060          // Integer values.
1061          foreach ( $int_keys as $index ) {
1062              $invitation->{$index} = intval( $invitation->{$index} );
1063          }
1064  
1065          // Boolean values.
1066          foreach ( $bool_keys as $index ) {
1067              $invitation->{$index} = (bool) $invitation->{$index};
1068          }
1069  
1070          foreach ( $filters as $filter_name => $filter_value ) {
1071              if ( ! isset( $invitation->{$filter_name} ) || $filter_value != $invitation->{$filter_name} ) {
1072                  continue 2;
1073              }
1074          }
1075  
1076          $group_id = (int) $invitation->group_id;
1077  
1078          $groups[ $group_id ] = $invitation;
1079      }
1080  
1081      // By default, results are ordered by membership id.
1082      if ( 'group_id' === $r['orderby'] ) {
1083          ksort( $groups );
1084      } elseif ( in_array( $r['orderby'], array( 'id', 'date_modified' ) ) ) {
1085          $groups = bp_sort_by_key( $groups, $r['orderby'] );
1086      }
1087  
1088      // By default, results are ordered ASC.
1089      if ( 'DESC' === strtoupper( $r['order'] ) ) {
1090          // `true` to preserve keys.
1091          $groups = array_reverse( $groups, true );
1092      }
1093  
1094      return $groups;
1095  }
1096  
1097  /**
1098   * Get the count of groups of which the specified user is a member.
1099   *
1100   * @since 1.0.0
1101   *
1102   * @param int $user_id Optional. Default: ID of the displayed user.
1103   * @return int Group count.
1104   */
1105  function groups_total_groups_for_user( $user_id = 0 ) {
1106  
1107      if ( empty( $user_id ) )
1108          $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();
1109  
1110      $count = wp_cache_get( 'bp_total_groups_for_user_' . $user_id, 'bp' );
1111  
1112      if ( false === $count ) {
1113          $count = BP_Groups_Member::total_group_count( $user_id );
1114          wp_cache_set( 'bp_total_groups_for_user_' . $user_id, $count, 'bp' );
1115      }
1116  
1117      return (int) $count;
1118  }
1119  
1120  /**
1121   * Get the BP_Groups_Group object corresponding to the current group.
1122   *
1123   * @since 1.5.0
1124   *
1125   * @return BP_Groups_Group The current group object.
1126   */
1127  function groups_get_current_group() {
1128      $bp = buddypress();
1129  
1130      $current_group = isset( $bp->groups->current_group )
1131          ? $bp->groups->current_group
1132          : false;
1133  
1134      /**
1135       * Filters the BP_Groups_Group object corresponding to the current group.
1136       *
1137       * @since 1.5.0
1138       *
1139       * @param BP_Groups_Group $current_group Current BP_Groups_Group object.
1140       */
1141      return apply_filters( 'groups_get_current_group', $current_group );
1142  }
1143  
1144  /** Group Avatars *************************************************************/
1145  
1146  /**
1147   * Generate the avatar upload directory path for a given group.
1148   *
1149   * @since 1.1.0
1150   *
1151   * @param int $group_id Optional. ID of the group. Default: ID of the current group.
1152   * @return string
1153   */
1154  function groups_avatar_upload_dir( $group_id = 0 ) {
1155  
1156      if ( empty( $group_id ) ) {
1157          $group_id = bp_get_current_group_id();
1158      }
1159  
1160      $directory = 'group-avatars';
1161      $path      = bp_core_avatar_upload_path() . '/' . $directory . '/' . $group_id;
1162      $newbdir   = $path;
1163      $newurl    = bp_core_avatar_url() . '/' . $directory . '/' . $group_id;
1164      $newburl   = $newurl;
1165      $newsubdir = '/' . $directory . '/' . $group_id;
1166  
1167      /**
1168       * Filters the avatar upload directory path for a given group.
1169       *
1170       * @since 1.1.0
1171       *
1172       * @param array $value Array of parts related to the groups avatar upload directory.
1173       */
1174      return apply_filters( 'groups_avatar_upload_dir', array(
1175          'path'    => $path,
1176          'url'     => $newurl,
1177          'subdir'  => $newsubdir,
1178          'basedir' => $newbdir,
1179          'baseurl' => $newburl,
1180          'error'   => false
1181      ) );
1182  }
1183  
1184  /** Group Member Status Checks ************************************************/
1185  
1186  /**
1187   * Get the Group roles.
1188   *
1189   * @since 5.0.0
1190   *
1191   * @return array The list of Group role objects.
1192   */
1193  function bp_groups_get_group_roles() {
1194      return array(
1195          'admin' => (object) array(
1196              'id'           => 'admin',
1197              'name'         => __( 'Administrator', 'buddypress' ),
1198              'is_admin'     => true,
1199              'is_banned'    => false,
1200              'is_confirmed' => true,
1201              'is_mod'       => false,
1202          ),
1203          'mod' => (object) array(
1204              'id'           => 'mod',
1205              'name'         => __( 'Moderator', 'buddypress' ),
1206              'is_admin'     => false,
1207              'is_banned'    => false,
1208              'is_confirmed' => true,
1209              'is_mod'       => true,
1210          ),
1211          'member' => (object) array(
1212              'id'           => 'member',
1213              'name'         => __( 'Member', 'buddypress' ),
1214              'is_admin'     => false,
1215              'is_banned'    => false,
1216              'is_confirmed' => true,
1217              'is_mod'       => false,
1218          ),
1219          'banned' => (object) array(
1220              'id'           => 'banned',
1221              'name'         => __( 'Banned', 'buddypress' ),
1222              'is_admin'     => false,
1223              'is_banned'    => true,
1224              'is_confirmed' => true,
1225              'is_mod'       => false,
1226          ),
1227      );
1228  }
1229  
1230  /**
1231   * Check whether a user is an admin of a given group.
1232   *
1233   * @since 1.0.0
1234   *
1235   * @param int $user_id ID of the user.
1236   * @param int $group_id ID of the group.
1237   * @return int|bool ID of the membership if the user is admin, otherwise false.
1238   */
1239  function groups_is_user_admin( $user_id, $group_id ) {
1240      $is_admin = false;
1241  
1242      $user_groups = bp_get_user_groups( $user_id, array(
1243          'is_admin' => true,
1244      ) );
1245  
1246      if ( isset( $user_groups[ $group_id ] ) ) {
1247          $is_admin = $user_groups[ $group_id ]->id;
1248      }
1249  
1250      return $is_admin;
1251  }
1252  
1253  /**
1254   * Check whether a user is a mod of a given group.
1255   *
1256   * @since 1.0.0
1257   *
1258   * @param int $user_id ID of the user.
1259   * @param int $group_id ID of the group.
1260   * @return int|bool ID of the membership if the user is mod, otherwise false.
1261   */
1262  function groups_is_user_mod( $user_id, $group_id ) {
1263      $is_mod = false;
1264  
1265      $user_groups = bp_get_user_groups( $user_id, array(
1266          'is_mod' => true,
1267      ) );
1268  
1269      if ( isset( $user_groups[ $group_id ] ) ) {
1270          $is_mod = $user_groups[ $group_id ]->id;
1271      }
1272  
1273      return $is_mod;
1274  }
1275  
1276  /**
1277   * Check whether a user is a member of a given group.
1278   *
1279   * @since 1.0.0
1280   *
1281   * @param int $user_id ID of the user.
1282   * @param int $group_id ID of the group.
1283   * @return int|bool ID of the membership if the user is member, otherwise false.
1284   */
1285  function groups_is_user_member( $user_id, $group_id ) {
1286      $is_member = false;
1287  
1288      $user_groups = bp_get_user_groups( $user_id, array(
1289          'is_admin' => null,
1290          'is_mod' => null,
1291      ) );
1292  
1293      if ( isset( $user_groups[ $group_id ] ) ) {
1294          $is_member = $user_groups[ $group_id ]->id;
1295      }
1296  
1297      return $is_member;
1298  }
1299  
1300  /**
1301   * Check whether a user is banned from a given group.
1302   *
1303   * @since 1.0.0
1304   *
1305   * @param int $user_id  ID of the user.
1306   * @param int $group_id ID of the group.
1307   * @return int|bool ID of the membership if the user is banned, otherwise false.
1308   */
1309  function groups_is_user_banned( $user_id, $group_id ) {
1310      $is_banned = false;
1311  
1312      $user_groups = bp_get_user_groups( $user_id, array(
1313          'is_confirmed' => null,
1314          'is_banned' => true,
1315      ) );
1316  
1317      if ( isset( $user_groups[ $group_id ] ) ) {
1318          $is_banned = $user_groups[ $group_id ]->id;
1319      }
1320  
1321      return $is_banned;
1322  }
1323  
1324  /**
1325   * Check whether a user has an outstanding invitation to a group.
1326   *
1327   * @since 2.6.0
1328   * @since 5.0.0 Added $type parameter.
1329   *
1330   * @param int    $user_id  ID of the user.
1331   * @param int    $group_id ID of the group.
1332   * @param string $type     If 'sent', results are limited to those invitations
1333   *                         that have actually been sent (non-draft).
1334   *                         Possible values: 'sent', 'draft', or 'all' Default: 'sent'.
1335   * @return int|bool ID of the membership if the user is invited, otherwise false.
1336   */
1337  function groups_is_user_invited( $user_id, $group_id, $type = 'sent' ) {
1338      return groups_check_has_invite_from_user( $user_id, $group_id, false, $type );
1339  }
1340  
1341  /**
1342   * Check whether a user has a pending membership request for a group.
1343   *
1344   * @since 2.6.0
1345   *
1346   * @param int $user_id ID of the user.
1347   * @param int $group_id ID of the group.
1348   * @return int|bool ID of the membership if the user is pending, otherwise false.
1349   */
1350  function groups_is_user_pending( $user_id, $group_id ) {
1351      if ( empty( $user_id ) || empty( $group_id ) ) {
1352          return false;
1353      }
1354  
1355      $args = array(
1356          'user_id'     => $user_id,
1357          'item_id'     => $group_id,
1358      );
1359      $invites_class = new BP_Groups_Invitation_Manager();
1360  
1361      return $invites_class->request_exists( $args );
1362  }
1363  
1364  /**
1365   * Is the specified user the creator of the group?
1366   *
1367   * @since 1.2.6
1368   *
1369   * @param int $user_id ID of the user.
1370   * @param int $group_id ID of the group.
1371   * @return int|null
1372   */
1373  function groups_is_user_creator( $user_id, $group_id ) {
1374      return BP_Groups_Member::check_is_creator( $user_id, $group_id );
1375  }
1376  
1377  /** Group Invitations *********************************************************/
1378  
1379  /**
1380   * Get group objects for groups that a user is currently invited to.
1381   *
1382   * @since 1.0.0
1383   *
1384   * @param int               $user_id ID of the invited user.
1385   * @param int|bool          $limit   Limit to restrict to.
1386   * @param int|bool          $page    Optional. Page offset of results to return.
1387   * @param string|array|bool $exclude Array of comma-separated list of group IDs
1388   *                                   to exclude from results.
1389   * @return array {
1390   *     @type array $groups Array of groups returned by paginated query.
1391   *     @type int   $total  Count of groups matching query.
1392   * }
1393   */
1394  function groups_get_invites_for_user( $user_id = 0, $limit = false, $page = false, $exclude = false ) {
1395      if ( empty( $user_id ) ) {
1396          $user_id = bp_loggedin_user_id();
1397      }
1398  
1399      $group_ids = groups_get_invited_to_group_ids( $user_id );
1400  
1401      // Remove excluded groups.
1402      if ( $exclude ) {
1403          $group_ids = array_diff( $group_ids, wp_parse_id_list( $exclude ) );
1404      }
1405  
1406      // Avoid passing an empty array.
1407      if ( ! $group_ids ) {
1408          $group_ids = array( 0 );
1409      }
1410  
1411      // Get a filtered list of groups.
1412      $args = array(
1413          'include'     => $group_ids,
1414          'show_hidden' => true,
1415          'per_page'    => $limit,
1416          'page'        => $page,
1417      );
1418      $groups = groups_get_groups( $args );
1419  
1420      return array( 'groups' => $groups['groups'], 'total' => groups_get_invite_count_for_user( $user_id ) );
1421  }
1422  
1423  /**
1424   * Get the total group invite count for a user.
1425   *
1426   * @since 2.0.0
1427   *
1428   * @param int $user_id The user ID.
1429   * @return int
1430   */
1431  function groups_get_invite_count_for_user( $user_id = 0 ) {
1432      if ( empty( $user_id ) ) {
1433          $user_id = bp_loggedin_user_id();
1434      }
1435  
1436      return count( groups_get_invited_to_group_ids( $user_id ) );
1437  }
1438  
1439  /**
1440   * Get an array of group IDs to which a user is invited.
1441   *
1442   * @since 5.0.0
1443   *
1444   * @param int $user_id The user ID.
1445   *
1446   * @return array Array of group IDs.
1447   */
1448   function groups_get_invited_to_group_ids( $user_id = 0 ) {
1449      if ( empty( $user_id ) ) {
1450          $user_id = bp_loggedin_user_id();
1451      }
1452  
1453      $group_ids = groups_get_invites( array(
1454          'user_id'     => $user_id,
1455          'invite_sent' => 'sent',
1456          'fields'      => 'item_ids'
1457      ) );
1458  
1459      return array_unique( $group_ids );
1460  }
1461  
1462  /**
1463   * Invite a user to a group.
1464   *
1465   * @since 1.0.0
1466   *
1467   * @param array|string $args {
1468   *     Array of arguments.
1469   *     @type int    $user_id       ID of the user being invited.
1470   *     @type int    $group_id      ID of the group to which the user is being invited.
1471   *     @type int    $inviter_id    Optional. ID of the inviting user. Default:
1472   *                                 ID of the logged-in user.
1473   *     @type string $date_modified Optional. Modified date for the invitation.
1474   *                                 Default: current date/time.
1475   *     @type string $content       Optional. Message to invitee.
1476   *     @type bool   $send_invite   Optional. Whether the invitation should be
1477   *                                 sent now. Default: false.
1478   * }
1479   * @return bool True on success, false on failure.
1480   */
1481  function groups_invite_user( $args = '' ) {
1482  
1483      $r = bp_parse_args( $args, array(
1484          'user_id'       => false,
1485          'group_id'      => false,
1486          'inviter_id'    => bp_loggedin_user_id(),
1487          'date_modified' => bp_core_current_time(),
1488          'content'       => '',
1489          'send_invite'   => 0
1490      ), 'groups_invite_user' );
1491  
1492      $inv_args = array(
1493          'user_id'       => $r['user_id'],
1494          'item_id'       => $r['group_id'],
1495          'inviter_id'    => $r['inviter_id'],
1496          'date_modified' => $r['date_modified'],
1497          'content'       => $r['content'],
1498          'send_invite'   => $r['send_invite']
1499      );
1500  
1501      // Create the unsent invitataion.
1502      $invites_class = new BP_Groups_Invitation_Manager();
1503      $created       = $invites_class->add_invitation( $inv_args );
1504  
1505      /**
1506       * Fires after the creation of a new group invite.
1507       *
1508       * @since 1.0.0
1509       *
1510       * @param array    $r       Array of parsed arguments for the group invite.
1511       * @param int|bool $created The ID of the invitation or false if it couldn't be created.
1512       */
1513      do_action( 'groups_invite_user', $r, $created );
1514  
1515      return $created;
1516  }
1517  
1518  /**
1519   * Uninvite a user from a group.
1520   *
1521   * @since 1.0.0
1522   *
1523   * @param int $user_id  ID of the user.
1524   * @param int $group_id ID of the group.
1525   * @param int $inviter_id ID of the inviter.
1526   * @return bool True on success, false on failure.
1527   */
1528  function groups_uninvite_user( $user_id, $group_id, $inviter_id = false ) {
1529      if ( empty( $user_id ) || empty( $group_id ) ) {
1530          return false;
1531      }
1532  
1533      $invites_class = new BP_Groups_Invitation_Manager();
1534      $success       = $invites_class->delete( array(
1535          'user_id'    => $user_id,
1536          'item_id'    => $group_id,
1537          'inviter_id' => $inviter_id,
1538      ) );
1539  
1540      if ( $success ) {
1541          /**
1542           * Fires after uninviting a user from a group.
1543           *
1544           * @since 1.0.0
1545           * @since 2.7.0 Added $inviter_id parameter
1546           *
1547           * @param int $group_id    ID of the group being uninvited from.
1548           * @param int $user_id     ID of the user being uninvited.
1549           * @param int $inviter_id  ID of the inviter.
1550           */
1551          do_action( 'groups_uninvite_user', $group_id, $user_id, $inviter_id );
1552      }
1553  
1554      return $success;
1555  }
1556  
1557  /**
1558   * Process the acceptance of a group invitation.
1559   *
1560   * Returns true if a user is already a member of the group.
1561   *
1562   * @since 1.0.0
1563   *
1564   * @param int $user_id  ID of the user.
1565   * @param int $group_id ID of the group.
1566   * @return bool True when the user is a member of the group, otherwise false.
1567   */
1568  function groups_accept_invite( $user_id, $group_id ) {
1569      $invites_class = new BP_Groups_Invitation_Manager();
1570      $args = array(
1571          'user_id'     => $user_id,
1572          'item_id'     => $group_id,
1573          'invite_sent' => 'sent',
1574      );
1575  
1576      return $invites_class->accept_invitation( $args );
1577  }
1578  
1579  /**
1580   * Reject a group invitation.
1581   *
1582   * @since 1.0.0
1583   * @since 5.0.0 The $inviter_id arg was added.
1584   *
1585   * @param int $user_id    ID of the user.
1586   * @param int $group_id   ID of the group.
1587   * @param int $inviter_id ID of the inviter.
1588   *
1589   * @return bool True on success, false on failure.
1590   */
1591  function groups_reject_invite( $user_id, $group_id, $inviter_id = false ) {
1592      if ( empty( $user_id ) || empty( $group_id ) ) {
1593          return false;
1594      }
1595  
1596      $invites_class = new BP_Groups_Invitation_Manager();
1597      $success       = $invites_class->delete( array(
1598          'user_id'    => $user_id,
1599          'item_id'    => $group_id,
1600          'inviter_id' => $inviter_id,
1601      ) );
1602  
1603      /**
1604       * Fires after a user rejects a group invitation.
1605       *
1606       * @since 1.0.0
1607       * @since 5.0.0 The $inviter_id arg was added.
1608       *
1609       * @param int $user_id    ID of the user rejecting the invite.
1610       * @param int $group_id   ID of the group being rejected.
1611       * @param int $inviter_id ID of the inviter.
1612       */
1613      do_action( 'groups_reject_invite', $user_id, $group_id, $inviter_id );
1614  
1615      return $success;
1616  }
1617  
1618  /**
1619   * Delete a group invitation.
1620   *
1621   * @since 1.0.0
1622   * @since 5.0.0 The $inviter_id arg was added.
1623   *
1624   * @param int $user_id  ID of the invited user.
1625   * @param int $group_id ID of the group.
1626   * @param int $inviter_id ID of the inviter.
1627   *
1628   * @return bool True on success, false on failure.
1629   */
1630  function groups_delete_invite( $user_id, $group_id, $inviter_id = false ) {
1631      if ( empty( $user_id ) || empty( $group_id ) ) {
1632          return false;
1633      }
1634  
1635      $invites_class = new BP_Groups_Invitation_Manager();
1636      $success       = $invites_class->delete( array(
1637          'user_id'    => $user_id,
1638          'item_id'    => $group_id,
1639          'inviter_id' => $inviter_id,
1640      ) );
1641  
1642      /**
1643       * Fires after the deletion of a group invitation.
1644       *
1645       * @since 1.9.0
1646       * @since 5.0.0 The $inviter_id arg was added.
1647       *
1648       * @param int $user_id  ID of the user whose invitation is being deleted.
1649       * @param int $group_id ID of the group whose invitation is being deleted.
1650       * @param int $inviter_id ID of the inviter.
1651       */
1652      do_action( 'groups_delete_invite', $user_id, $group_id, $inviter_id );
1653  
1654      return true;
1655  }
1656  
1657  /**
1658   * Send some or all pending invites by a single user to a specific group.
1659   *
1660   * @since 1.0.0
1661   * @since 5.0.0 Parameters changed to associative array.
1662   *
1663   * @param array $args {
1664   *     An array of optional arguments.
1665   *     @type int    $user_id       ID of the invited user.
1666   *     @type string $invitee_email Email address of the invited user, if not a member of the site.
1667   *     @type string $group_id      ID of the group or an array of group IDs.
1668   *     @type string $inviter_id    ID of the user extending the invitation.
1669   *     @type bool   $force_resend  Whether to resend the email & notification if one has already been sent.
1670   * }
1671   */
1672  function groups_send_invites( $args = array() ) {
1673      // Backward compatibility with old method of passing arguments.
1674      if ( ! is_array( $args ) || func_num_args() > 1 ) {
1675          _deprecated_argument( __METHOD__, '5.0.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
1676  
1677          $old_args_keys = array(
1678              0 => 'inviter_id',
1679              1 => 'group_id',
1680          );
1681  
1682          $args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
1683      }
1684  
1685      $r = bp_parse_args( $args, array(
1686          'user_id'       => false,
1687          'invitee_email' => '',
1688          'group_id'      => 0,
1689          'inviter_id'    => bp_loggedin_user_id(),
1690          'force_resend'  => false,
1691      ), 'groups_send_invitation' );
1692  
1693      /*
1694       * We will generally only want to fetch unsent invitations.
1695       * If force_resend is true, then we need to fetch both sent and draft invites.
1696       */
1697      if ( $r['force_resend'] ) {
1698          $args['invite_sent'] = 'all';
1699      } else {
1700          $args['invite_sent'] = 'draft';
1701      }
1702  
1703      $args = array(
1704          'user_id'       => $r['user_id'],
1705          'invitee_email' => $r['invitee_email'],
1706          'item_id'       => $r['group_id'],
1707          'inviter_id'    => $r['inviter_id'],
1708      );
1709      $invites = groups_get_invites( $args );
1710  
1711      $invited_users = array();
1712  
1713      $invites_class = new BP_Groups_Invitation_Manager();
1714      foreach ( $invites as $invite ) {
1715          $invited_users[] = $invite->user_id;
1716          $invites_class->send_invitation_by_id( $invite->id );
1717      }
1718  
1719      /**
1720       * Fires after the sending of invites for a group.
1721       *
1722       * @since 1.0.0
1723       * @since 2.5.0 Added $user_id to passed parameters.
1724       *
1725       * @param int   $group_id      ID of the group who's being invited to.
1726       * @param array $invited_users Array of users being invited to the group.
1727       * @param int   $user_id       ID of the inviting user.
1728       */
1729      do_action( 'groups_send_invites', $r['group_id'], $invited_users, $r['inviter_id'] );
1730  }
1731  
1732  /**
1733   * Get IDs of users with outstanding invites to a given group.
1734   *
1735   * @since 1.0.0
1736   * @since 2.9.0 Added $sent as a parameter.
1737   *
1738   * @param  int      $user_id  ID of the inviting user.
1739   * @param  int      $group_id ID of the group.
1740   * @param  int|null $sent     Query for a specific invite sent status. If 0, this will query for users
1741   *                            that haven't had an invite sent to them yet. If 1, this will query for
1742   *                            users that have had an invite sent to them. If null, no invite status will
1743   *                            queried. Default: null.
1744   * @return array    IDs of users who have been invited to the group by the user but have not
1745   *                  yet accepted.
1746   */
1747  function groups_get_invites_for_group( $user_id, $group_id, $sent = null ) {
1748      return BP_Groups_Group::get_invites( $user_id, $group_id, $sent );
1749  }
1750  
1751  /**
1752   * Get invitations to a given group filtered by arguments.
1753   *
1754   * @since 5.0.0
1755   *
1756   * @param int   $group_id ID of the group.
1757   * @param array $args     Invitation arguments.
1758   *                        See BP_Invitation::get() for list.
1759   *
1760   * @return array $invites     Matching BP_Invitation objects.
1761   */
1762  function groups_get_invites( $args = array() ) {
1763      $invites_class = new BP_Groups_Invitation_Manager();
1764      return $invites_class->get_invitations( $args );
1765  }
1766  
1767  /**
1768   * Check to see whether a user has already been invited to a group.
1769   *
1770   * By default, the function checks for invitations that have been sent.
1771   * Entering 'all' as the $type parameter will return unsent invitations as
1772   * well (useful to make sure AJAX requests are not duplicated).
1773   *
1774   * @since 1.0.0
1775   *
1776   * @param int    $user_id  ID of potential group member.
1777   * @param int    $group_id ID of potential group.
1778   * @param string $type     Optional. Use 'sent' to check for sent invites,
1779   *                         'all' to check for all. Default: 'sent'.
1780   * @return int|bool ID of the first found membership if found, otherwise false.
1781   */
1782  function groups_check_user_has_invite( $user_id, $group_id, $type = 'sent' ) {
1783      return groups_check_has_invite_from_user( $user_id, $group_id, false, $type );
1784  }
1785  
1786  /**
1787   * Check to see whether a user has already been invited to a group by a particular user.
1788   *
1789   * By default, the function checks for invitations that have been sent.
1790   * Entering 'all' as the $type parameter will return unsent invitations as
1791   * well (useful to make sure AJAX requests are not duplicated).
1792   *
1793   * @since 5.0.0
1794   *
1795   * @param int    $user_id    ID of potential group member.
1796   * @param int    $group_id   ID of potential group.
1797   * @param string $inviter_id Optional. Use 'sent' to check for sent invites,
1798   *                           'all' to check for all. Default: 'sent'.
1799   * @param string $type       Optional. Specify a user ID to limit to only invited from that user.
1800   *                           Default: 'false'.
1801   * @return int|bool ID of the first found membership if found, otherwise false.
1802   */
1803   function groups_check_has_invite_from_user( $user_id, $group_id, $inviter_id = false, $type = 'sent' ) {
1804      if ( empty( $user_id ) || empty( $group_id ) ) {
1805          return false;
1806      }
1807  
1808      $args = array(
1809          'user_id'     => $user_id,
1810          'item_id'     => $group_id,
1811          'invite_sent' => 'sent',
1812      );
1813      if ( $inviter_id ) {
1814          $args['inviter_id'] = $inviter_id;
1815      }
1816      if ( $type === 'draft' || $type === 'all' ) {
1817          $args['invite_sent'] = $type;
1818      }
1819  
1820      $invites_class = new BP_Groups_Invitation_Manager();
1821  
1822      return $invites_class->invitation_exists( $args );
1823  }
1824  
1825  /**
1826   * Delete all invitations to a given group.
1827   *
1828   * @since 1.0.0
1829   *
1830   * @param int $group_id ID of the group whose invitations are being deleted.
1831   * @return int|null Number of rows records deleted on success, null on failure.
1832   */
1833  function groups_delete_all_group_invites( $group_id ) {
1834      return BP_Groups_Group::delete_all_invites( $group_id );
1835  }
1836  
1837  /** Group Promotion & Banning *************************************************/
1838  
1839  /**
1840   * Promote a member to a new status within a group.
1841   *
1842   * @since 1.0.0
1843   *
1844   * @param int    $user_id  ID of the user.
1845   * @param int    $group_id ID of the group.
1846   * @param string $status   The new status. 'mod' or 'admin'.
1847   * @return bool True on success, false on failure.
1848   */
1849  function groups_promote_member( $user_id, $group_id, $status ) {
1850  
1851      if ( ! bp_is_item_admin() )
1852          return false;
1853  
1854      $member = new BP_Groups_Member( $user_id, $group_id );
1855  
1856      // Don't use this action. It's deprecated as of BuddyPress 1.6.
1857      do_action( 'groups_premote_member', $group_id, $user_id, $status );
1858  
1859      /**
1860       * Fires before the promotion of a user to a new status.
1861       *
1862       * @since 1.6.0
1863       *
1864       * @param int    $group_id ID of the group being promoted in.
1865       * @param int    $user_id  ID of the user being promoted.
1866       * @param string $status   New status being promoted to.
1867       */
1868      do_action( 'groups_promote_member', $group_id, $user_id, $status );
1869  
1870      return $member->promote( $status );
1871  }
1872  
1873  /**
1874   * Demote a user to 'member' status within a group.
1875   *
1876   * @since 1.0.0
1877   *
1878   * @param int $user_id  ID of the user.
1879   * @param int $group_id ID of the group.
1880   * @return bool True on success, false on failure.
1881   */
1882  function groups_demote_member( $user_id, $group_id ) {
1883  
1884      if ( ! bp_is_item_admin() )
1885          return false;
1886  
1887      $member = new BP_Groups_Member( $user_id, $group_id );
1888  
1889      /**
1890       * Fires before the demotion of a user to 'member'.
1891       *
1892       * @since 1.0.0
1893       *
1894       * @param int $group_id ID of the group being demoted in.
1895       * @param int $user_id  ID of the user being demoted.
1896       */
1897      do_action( 'groups_demote_member', $group_id, $user_id );
1898  
1899      return $member->demote();
1900  }
1901  
1902  /**
1903   * Ban a member from a group.
1904   *
1905   * @since 1.0.0
1906   *
1907   * @param int $user_id  ID of the user.
1908   * @param int $group_id ID of the group.
1909   * @return bool True on success, false on failure.
1910   */
1911  function groups_ban_member( $user_id, $group_id ) {
1912  
1913      if ( ! bp_is_item_admin() )
1914          return false;
1915  
1916      $member = new BP_Groups_Member( $user_id, $group_id );
1917  
1918      /**
1919       * Fires before the banning of a member from a group.
1920       *
1921       * @since 1.0.0
1922       *
1923       * @param int $group_id ID of the group being banned from.
1924       * @param int $user_id  ID of the user being banned.
1925       */
1926      do_action( 'groups_ban_member', $group_id, $user_id );
1927  
1928      return $member->ban();
1929  }
1930  
1931  /**
1932   * Unban a member from a group.
1933   *
1934   * @since 1.0.0
1935   *
1936   * @param int $user_id  ID of the user.
1937   * @param int $group_id ID of the group.
1938   * @return bool True on success, false on failure.
1939   */
1940  function groups_unban_member( $user_id, $group_id ) {
1941  
1942      if ( ! bp_is_item_admin() )
1943          return false;
1944  
1945      $member = new BP_Groups_Member( $user_id, $group_id );
1946  
1947      /**
1948       * Fires before the unbanning of a member from a group.
1949       *
1950       * @since 1.0.0
1951       *
1952       * @param int $group_id ID of the group being unbanned from.
1953       * @param int $user_id  ID of the user being unbanned.
1954       */
1955      do_action( 'groups_unban_member', $group_id, $user_id );
1956  
1957      return $member->unban();
1958  }
1959  
1960  /** Group Removal *************************************************************/
1961  
1962  /**
1963   * Remove a member from a group.
1964   *
1965   * @since 1.2.6
1966   *
1967   * @param int $user_id  ID of the user.
1968   * @param int $group_id ID of the group.
1969   * @return bool True on success, false on failure.
1970   */
1971  function groups_remove_member( $user_id, $group_id ) {
1972  
1973      if ( ! bp_is_item_admin() ) {
1974          return false;
1975      }
1976  
1977      $member = new BP_Groups_Member( $user_id, $group_id );
1978  
1979      /**
1980       * Fires before the removal of a member from a group.
1981       *
1982       * @since 1.2.6
1983       *
1984       * @param int $group_id ID of the group being removed from.
1985       * @param int $user_id  ID of the user being removed.
1986       */
1987      do_action( 'groups_remove_member', $group_id, $user_id );
1988  
1989      return $member->remove();
1990  }
1991  
1992  /** Group Membership **********************************************************/
1993  
1994  /**
1995   * Create a group membership request.
1996   *
1997   * @since 1.0.0
1998   *
1999   * @param array|string $args {
2000   *     Array of arguments.
2001   *     @type int    $user_id       ID of the user being invited.
2002   *     @type int    $group_id      ID of the group to which the user is being invited.
2003   *     @type string $content       Optional. Message to invitee.
2004   *     @type string $date_modified Optional. Modified date for the invitation.
2005   *                                 Default: current date/time.
2006   * }
2007   * @return bool True on success, false on failure.
2008   */
2009  function groups_send_membership_request( $args = array() ) {
2010      // Backward compatibility with old method of passing arguments.
2011      if ( ! is_array( $args ) || func_num_args() > 1 ) {
2012          _deprecated_argument( __METHOD__, '5.0.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
2013  
2014          $old_args_keys = array(
2015              0 => 'user_id',
2016              1 => 'group_id',
2017          );
2018  
2019          $args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
2020      }
2021  
2022      $r = bp_parse_args( $args, array(
2023          'user_id'       => false,
2024          'group_id'      => false,
2025          'content'       => '',
2026          'date_modified' => bp_core_current_time(),
2027      ), 'groups_send_membership_request' );
2028  
2029      $inv_args = array(
2030          'user_id'       => $r['user_id'],
2031          'item_id'       => $r['group_id'],
2032          'content'       => $r['content'],
2033          'date_modified' => $r['date_modified'],
2034      );
2035  
2036      $invites_class = new BP_Groups_Invitation_Manager();
2037      $request_id = $invites_class->add_request( $inv_args );
2038  
2039      // If a new request was created, send the emails.
2040      if ( $request_id && is_int( $request_id ) ) {
2041          $invites_class->send_request_notification_by_id( $request_id );
2042          $admins = groups_get_group_admins( $r['group_id'] );
2043  
2044          /**
2045           * Fires after the creation of a new membership request.
2046           *
2047           * @since 1.0.0
2048           *
2049           * @param int   $requesting_user_id  ID of the user requesting membership.
2050           * @param array $admins              Array of group admins.
2051           * @param int   $group_id            ID of the group being requested to.
2052           * @param int   $request_id          ID of the request.
2053           */
2054          do_action( 'groups_membership_requested', $r['user_id'], $admins, $r['group_id'], $request_id );
2055  
2056          return $request_id;
2057      }
2058  
2059      return false;
2060  }
2061  
2062  /**
2063   * Accept a pending group membership request.
2064   *
2065   * @since 1.0.0
2066   * @since 5.0.0 Deprecated $membership_id argument.
2067   *
2068   * @param int $membership_id Deprecated 5.0.0.
2069   * @param int $user_id       Required. ID of the user who requested membership.
2070   *                           Provide this value along with $group_id to override
2071   *                           $membership_id.
2072   * @param int $group_id      Required. ID of the group to which membership is being
2073   *                           requested. Provide this value along with $user_id to
2074   *                           override $membership_id.
2075   * @return bool True on success, false on failure.
2076   */
2077  function groups_accept_membership_request( $membership_id, $user_id = 0, $group_id = 0 ) {
2078  
2079      if ( ! empty( $membership_id ) ) {
2080          _deprecated_argument( __METHOD__, '5.0.0', sprintf( __( 'Argument `membership_id` passed to %1$s  is deprecated. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
2081      }
2082  
2083      if ( ! $user_id || ! $group_id ) {
2084          return false;
2085      }
2086  
2087      $invites_class = new BP_Groups_Invitation_Manager();
2088      $args = array(
2089          'user_id' => $user_id,
2090          'item_id' => $group_id,
2091      );
2092  
2093      return $invites_class->accept_request( $args );
2094  }
2095  
2096  /**
2097   * Reject a pending group membership request.
2098   *
2099   * @since 1.0.0
2100   *
2101   * @param int $membership_id Deprecated 5.0.0.
2102   * @param int $user_id       Optional. ID of the user who requested membership.
2103   *                           Provide this value along with $group_id to override
2104   *                           $membership_id.
2105   * @param int $group_id      Optional. ID of the group to which membership is being
2106   *                           requested. Provide this value along with $user_id to
2107   *                           override $membership_id.
2108   * @return bool True on success, false on failure.
2109   */
2110  function groups_reject_membership_request( $membership_id, $user_id = 0, $group_id = 0 ) {
2111  
2112      if ( ! empty( $membership_id ) ){
2113          _deprecated_argument( __METHOD__, '5.0.0', sprintf( __( 'Argument `membership_id` passed to %1$s  is deprecated. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
2114      }
2115  
2116      if ( ! groups_delete_membership_request( false, $user_id, $group_id ) ) {
2117          return false;
2118      }
2119  
2120      /**
2121       * Fires after a group membership request has been rejected.
2122       *
2123       * @since 1.0.0
2124       *
2125       * @param int  $user_id  ID of the user who rejected membership.
2126       * @param int  $group_id ID of the group that was rejected membership to.
2127       * @param bool $value    If membership was accepted.
2128       */
2129      do_action( 'groups_membership_rejected', $user_id, $group_id, false );
2130  
2131      return true;
2132  }
2133  
2134  /**
2135   * Delete a pending group membership request.
2136   *
2137   * @since 1.2.0
2138   *
2139   * @param int $membership_id Deprecated 5.0.0.
2140   * @param int $user_id       Optional. ID of the user who requested membership.
2141   *                           Provide this value along with $group_id to override
2142   *                           $membership_id.
2143   * @param int $group_id      Optional. ID of the group to which membership is being
2144   *                           requested. Provide this value along with $user_id to
2145   *                           override $membership_id.
2146   * @return false|BP_Groups_Member True on success, false on failure.
2147   */
2148  function groups_delete_membership_request( $membership_id, $user_id = 0, $group_id = 0 ) {
2149      if ( ! empty( $membership_id ) ){
2150          _deprecated_argument( __METHOD__, '5.0.0', sprintf( __( 'Argument `membership_id` passed to %1$s  is deprecated. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
2151      }
2152  
2153      if ( empty( $user_id ) || empty( $group_id ) ) {
2154          return false;
2155      }
2156  
2157      $invites_class = new BP_Groups_Invitation_Manager();
2158      $success       = $invites_class->delete_requests( array(
2159          'user_id' => $user_id,
2160          'item_id' => $group_id
2161      ) );
2162  
2163      return $success;
2164  }
2165  
2166  /**
2167   * Get group membership requests filtered by arguments.
2168   *
2169   * @since 5.0.0
2170   *
2171   * @param int   $group_id ID of the group.
2172   * @param array $args     Invitation arguments.
2173   *                        See BP_Invitation::get() for list.
2174   *
2175   * @return array $requests Matching BP_Invitation objects.
2176   */
2177  function groups_get_requests( $args = array() ) {
2178      $invites_class = new BP_Groups_Invitation_Manager();
2179      return $invites_class->get_requests( $args );
2180  }
2181  
2182  /**
2183   * Check whether a user has an outstanding membership request for a given group.
2184   *
2185   * @since 1.0.0
2186   *
2187   * @param int $user_id  ID of the user.
2188   * @param int $group_id ID of the group.
2189   * @return int|bool ID of the request if found, otherwise false.
2190   */
2191  function groups_check_for_membership_request( $user_id, $group_id ) {
2192      if ( empty( $user_id ) || empty( $group_id ) ) {
2193          return false;
2194      }
2195  
2196      $args = array(
2197          'user_id' => $user_id,
2198          'item_id' => $group_id,
2199      );
2200      $invites_class = new BP_Groups_Invitation_Manager();
2201  
2202      return $invites_class->request_exists( $args );
2203  }
2204  
2205   /**
2206    * Get an array of group IDs to which a user has requested membership.
2207    *
2208    * @since 5.0.0
2209    *
2210    * @param int $user_id The user ID.
2211    *
2212    * @return array Array of group IDs.
2213    */
2214   function groups_get_membership_requested_group_ids( $user_id = 0 ) {
2215      if ( ! $user_id ) {
2216          $user_id = bp_loggedin_user_id();
2217      }
2218  
2219      $group_ids     = groups_get_requests( array(
2220          'user_id' => $user_id,
2221          'fields'  => 'item_ids'
2222      ) );
2223  
2224      return $group_ids;
2225  }
2226  
2227   /**
2228    * Get an array of group IDs to which a user has requested membership.
2229    *
2230    * @since 5.0.0
2231    *
2232    * @param int $user_id The user ID.
2233    *
2234    * @return array Array of group IDs.
2235    */
2236   function groups_get_membership_requested_user_ids( $group_id = 0 ) {
2237      if ( ! $group_id ) {
2238          $group_id = bp_get_current_group_id();
2239      }
2240  
2241      $requests = groups_get_requests( array(
2242          'item_id' => $group_id,
2243          'fields'  => 'user_ids'
2244      ) );
2245  
2246      return $requests;
2247  }
2248  
2249  /**
2250   * Accept all pending membership requests to a group.
2251   *
2252   * @since 1.0.2
2253   *
2254   * @param int $group_id ID of the group.
2255   * @return bool True on success, false on failure.
2256   */
2257  function groups_accept_all_pending_membership_requests( $group_id = 0 ) {
2258      if ( ! $group_id ) {
2259          $group_id = bp_get_current_group_id();
2260      }
2261  
2262      $user_ids = groups_get_membership_requested_user_ids( $group_id );
2263  
2264      if ( ! $user_ids ) {
2265          return false;
2266      }
2267  
2268      foreach ( (array) $user_ids as $user_id ) {
2269          groups_accept_membership_request( false, $user_id, $group_id );
2270      }
2271  
2272      /**
2273       * Fires after the acceptance of all pending membership requests to a group.
2274       *
2275       * @since 1.0.2
2276       *
2277       * @param int $group_id ID of the group whose pending memberships were accepted.
2278       */
2279      do_action( 'groups_accept_all_pending_membership_requests', $group_id );
2280  
2281      return true;
2282  }
2283  
2284  /** Group Meta ****************************************************************/
2285  
2286  /**
2287   * Delete metadata for a group.
2288   *
2289   * @since 1.0.0
2290   *
2291   * @param int         $group_id   ID of the group.
2292   * @param string|bool $meta_key   The key of the row to delete.
2293   * @param string|bool $meta_value Optional. Metadata value. If specified, only delete
2294   *                                metadata entries with this value.
2295   * @param bool        $delete_all Optional. If true, delete matching metadata entries
2296   *                                for all groups. Otherwise, only delete matching
2297   *                                metadata entries for the specified group.
2298   *                                Default: false.
2299   * @return bool True on success, false on failure.
2300   */
2301  function groups_delete_groupmeta( $group_id, $meta_key = false, $meta_value = false, $delete_all = false ) {
2302      global $wpdb;
2303  
2304      // Legacy - if no meta_key is passed, delete all for the item.
2305      if ( empty( $meta_key ) ) {
2306          $table_name = buddypress()->groups->table_name_groupmeta;
2307          $sql        = "SELECT meta_key FROM {$table_name} WHERE group_id = %d";
2308          $query      = $wpdb->prepare( $sql, $group_id );
2309          $keys       = $wpdb->get_col( $query );
2310  
2311          // With no meta_key, ignore $delete_all.
2312          $delete_all = false;
2313      } else {
2314          $keys = array( $meta_key );
2315      }
2316  
2317      add_filter( 'query', 'bp_filter_metaid_column_name' );
2318  
2319      $retval = true;
2320      foreach ( $keys as $key ) {
2321          $retval = delete_metadata( 'group', $group_id, $key, $meta_value, $delete_all );
2322      }
2323  
2324      remove_filter( 'query', 'bp_filter_metaid_column_name' );
2325  
2326      return $retval;
2327  }
2328  
2329  /**
2330   * Get a piece of group metadata.
2331   *
2332   * @since 1.0.0
2333   *
2334   * @param int    $group_id ID of the group.
2335   * @param string $meta_key Metadata key.
2336   * @param bool   $single   Optional. If true, return only the first value of the
2337   *                         specified meta_key. This parameter has no effect if
2338   *                         meta_key is empty.
2339   * @return mixed Metadata value.
2340   */
2341  function groups_get_groupmeta( $group_id, $meta_key = '', $single = true ) {
2342      add_filter( 'query', 'bp_filter_metaid_column_name' );
2343      $retval = get_metadata( 'group', $group_id, $meta_key, $single );
2344      remove_filter( 'query', 'bp_filter_metaid_column_name' );
2345  
2346      return $retval;
2347  }
2348  
2349  /**
2350   * Update a piece of group metadata.
2351   *
2352   * @since 1.0.0
2353   *
2354   * @param int    $group_id   ID of the group.
2355   * @param string $meta_key   Metadata key.
2356   * @param mixed  $meta_value Value to store.
2357   * @param mixed  $prev_value Optional. If specified, only update existing
2358   *                           metadata entries with the specified value.
2359   *                           Otherwise, update all entries.
2360   * @return bool|int $retval Returns false on failure. On successful update of existing
2361   *                          metadata, returns true. On successful creation of new metadata,
2362   *                          returns the integer ID of the new metadata row.
2363   */
2364  function groups_update_groupmeta( $group_id, $meta_key, $meta_value, $prev_value = '' ) {
2365      add_filter( 'query', 'bp_filter_metaid_column_name' );
2366      $retval = update_metadata( 'group', $group_id, $meta_key, $meta_value, $prev_value );
2367      remove_filter( 'query', 'bp_filter_metaid_column_name' );
2368  
2369      return $retval;
2370  }
2371  
2372  /**
2373   * Add a piece of group metadata.
2374   *
2375   * @since 2.0.0
2376   *
2377   * @param int    $group_id   ID of the group.
2378   * @param string $meta_key   Metadata key.
2379   * @param mixed  $meta_value Metadata value.
2380   * @param bool   $unique     Optional. Whether to enforce a single metadata value
2381   *                           for the given key. If true, and the object already
2382   *                           has a value for the key, no change will be made.
2383   *                           Default: false.
2384   * @return int|bool The meta ID on successful update, false on failure.
2385   */
2386  function groups_add_groupmeta( $group_id, $meta_key, $meta_value, $unique = false ) {
2387      add_filter( 'query', 'bp_filter_metaid_column_name' );
2388      $retval = add_metadata( 'group', $group_id, $meta_key, $meta_value, $unique );
2389      remove_filter( 'query', 'bp_filter_metaid_column_name' );
2390  
2391      return $retval;
2392  }
2393  
2394  /** Group Cleanup Functions ***************************************************/
2395  
2396  /**
2397   * Delete all group membership information for the specified user.
2398   *
2399   * @since 1.0.0
2400   *
2401   * @param int $user_id ID of the user.
2402   */
2403  function groups_remove_data_for_user( $user_id ) {
2404      BP_Groups_Member::delete_all_for_user( $user_id );
2405  
2406      /**
2407       * Fires after the deletion of all data for a user.
2408       *
2409       * @since 1.1.0
2410       *
2411       * @param int $user_id ID of the user whose data is being deleted.
2412       */
2413      do_action( 'groups_remove_data_for_user', $user_id );
2414  }
2415  add_action( 'wpmu_delete_user',  'groups_remove_data_for_user' );
2416  add_action( 'delete_user',       'groups_remove_data_for_user' );
2417  add_action( 'bp_make_spam_user', 'groups_remove_data_for_user' );
2418  
2419  /**
2420   * Update orphaned child groups when the parent is deleted.
2421   *
2422   * @since 2.7.0
2423   *
2424   * @param BP_Groups_Group $group Instance of the group item being deleted.
2425   */
2426  function bp_groups_update_orphaned_groups_on_group_delete( $group ) {
2427      // Get child groups and set the parent to the deleted parent's parent.
2428      $grandparent_group_id = $group->parent_id;
2429      $child_args = array(
2430          'parent_id'         => $group->id,
2431          'show_hidden'       => true,
2432          'per_page'          => false,
2433          'update_meta_cache' => false,
2434      );
2435      $children = groups_get_groups( $child_args );
2436      $children = $children['groups'];
2437  
2438      foreach ( $children as $cgroup ) {
2439          $cgroup->parent_id = $grandparent_group_id;
2440          $cgroup->save();
2441      }
2442  }
2443  add_action( 'bp_groups_delete_group', 'bp_groups_update_orphaned_groups_on_group_delete', 10, 2 );
2444  
2445  /** Group Types ***************************************************************/
2446  
2447  /**
2448   * Fire the 'bp_groups_register_group_types' action.
2449   *
2450   * @since 2.6.0
2451   */
2452  function bp_groups_register_group_types() {
2453      /**
2454       * Fires when it's appropriate to register group types.
2455       *
2456       * @since 2.6.0
2457       */
2458      do_action( 'bp_groups_register_group_types' );
2459  }
2460  add_action( 'bp_register_taxonomies', 'bp_groups_register_group_types' );
2461  
2462  /**
2463   * Register a group type.
2464   *
2465   * @since 2.6.0
2466   * @since 2.7.0 Introduce $has_directory, $show_in_create_screen, $show_in_list, and
2467   *              $description, $create_screen_checked as $args parameters.
2468   *
2469   * @param string $group_type Unique string identifier for the group type.
2470   * @param array  $args {
2471   *     Array of arguments describing the group type.
2472   *
2473   *     @type string|bool $has_directory         Set the slug to be used for custom group directory page. eg.
2474   *                                              example.com/groups/type/MY_SLUG. Default: false.
2475   *     @type bool        $show_in_create_screen Whether this group type is allowed to be selected on the group creation
2476   *                                              page. Default: false.
2477   *     @type bool|null   $show_in_list          Whether this group type should be shown in lists rendered by
2478   *                                              bp_group_type_list(). Default: null. If $show_in_create_screen is true,
2479   *                                              this will default to true, unless this is set explicitly to false.
2480   *     @type string      $description           A short descriptive summary of what the group type is. Currently shown
2481   *                                              on a group's "Manage > Settings" page when selecting group types.
2482   *     @type bool        $create_screen_checked If $show_in_create_screen is true, whether we should have our group type
2483   *                                              checkbox checked by default. Handy if you want to imply that the group
2484   *                                              type should be enforced, but decision lies with the group creator.
2485   *                                              Default: false.
2486   *     @type array       $labels {
2487   *         Array of labels to use in various parts of the interface.
2488   *
2489   *         @type string $name          Default name. Should typically be plural.
2490   *         @type string $singular_name Singular name.
2491   *     }
2492   * }
2493   * @return object|WP_Error Group type object on success, WP_Error object on failure.
2494   */
2495  function bp_groups_register_group_type( $group_type, $args = array() ) {
2496      $bp = buddypress();
2497  
2498      if ( isset( $bp->groups->types[ $group_type ] ) ) {
2499          return new WP_Error( 'bp_group_type_exists', __( 'Group type already exists.', 'buddypress' ), $group_type );
2500      }
2501  
2502      $r = bp_parse_args( $args, array(
2503          'has_directory'         => false,
2504          'show_in_create_screen' => false,
2505          'show_in_list'          => null,
2506          'description'           => '',
2507          'create_screen_checked' => false,
2508          'labels'                => array(),
2509      ), 'register_group_type' );
2510  
2511      $group_type = sanitize_key( $group_type );
2512  
2513      /**
2514       * Filters the list of illegal group type names.
2515       *
2516       * - 'any' is a special pseudo-type, representing items unassociated with any group type.
2517       * - 'null' is a special pseudo-type, representing users without any type.
2518       * - '_none' is used internally to denote an item that should not apply to any group types.
2519       *
2520       * @since 2.6.0
2521       *
2522       * @param array $illegal_names Array of illegal names.
2523       */
2524      $illegal_names = apply_filters( 'bp_group_type_illegal_names', array( 'any', 'null', '_none' ) );
2525      if ( in_array( $group_type, $illegal_names, true ) ) {
2526          return new WP_Error( 'bp_group_type_illegal_name', __( 'You may not register a group type with this name.', 'buddypress' ), $group_type );
2527      }
2528  
2529      // Store the group type name as data in the object (not just as the array key).
2530      $r['name'] = $group_type;
2531  
2532      // Make sure the relevant labels have been filled in.
2533      $default_name = isset( $r['labels']['name'] ) ? $r['labels']['name'] : ucfirst( $r['name'] );
2534      $r['labels'] = array_merge( array(
2535          'name'          => $default_name,
2536          'singular_name' => $default_name,
2537      ), $r['labels'] );
2538  
2539      // Directory slug.
2540      if ( ! empty( $r['has_directory'] ) ) {
2541          // A string value is intepreted as the directory slug.
2542          if ( is_string( $r['has_directory'] ) ) {
2543              $directory_slug = $r['has_directory'];
2544  
2545          // Otherwise fall back on group type.
2546          } else {
2547              $directory_slug = $group_type;
2548          }
2549  
2550          // Sanitize for use in URLs.
2551          $r['directory_slug'] = sanitize_title( $directory_slug );
2552          $r['has_directory']  = true;
2553      } else {
2554          $r['directory_slug'] = '';
2555          $r['has_directory']  = false;
2556      }
2557  
2558      // Type lists.
2559      if ( true === $r['show_in_create_screen'] && is_null( $r['show_in_list'] ) ) {
2560          $r['show_in_list'] = true;
2561      } else {
2562          $r['show_in_list'] = (bool) $r['show_in_list'];
2563      }
2564  
2565      $bp->groups->types[ $group_type ] = $type = (object) $r;
2566  
2567      /**
2568       * Fires after a group type is registered.
2569       *
2570       * @since 2.6.0
2571       *
2572       * @param string $group_type Group type identifier.
2573       * @param object $type       Group type object.
2574       */
2575      do_action( 'bp_groups_register_group_type', $group_type, $type );
2576  
2577      return $type;
2578  }
2579  
2580  /**
2581   * Get a list of all registered group type objects.
2582   *
2583   * @since 2.6.0
2584   *
2585   * @see bp_groups_register_group_type() for accepted arguments.
2586   *
2587   * @param array|string $args     Optional. An array of key => value arguments to match against
2588   *                               the group type objects. Default empty array.
2589   * @param string       $output   Optional. The type of output to return. Accepts 'names'
2590   *                               or 'objects'. Default 'names'.
2591   * @param string       $operator Optional. The logical operation to perform. 'or' means only one
2592   *                               element from the array needs to match; 'and' means all elements
2593   *                               must match. Accepts 'or' or 'and'. Default 'and'.
2594   * @return array       $types    A list of groups type names or objects.
2595   */
2596  function bp_groups_get_group_types( $args = array(), $output = 'names', $operator = 'and' ) {
2597      $types = buddypress()->groups->types;
2598  
2599      $types = wp_filter_object_list( $types, $args, $operator );
2600  
2601      /**
2602       * Filters the array of group type objects.
2603       *
2604       * This filter is run before the $output filter has been applied, so that
2605       * filtering functions have access to the entire group type objects.
2606       *
2607       * @since 2.6.0
2608       *
2609       * @param array  $types     group type objects, keyed by name.
2610       * @param array  $args      Array of key=>value arguments for filtering.
2611       * @param string $operator  'or' to match any of $args, 'and' to require all.
2612       */
2613      $types = apply_filters( 'bp_groups_get_group_types', $types, $args, $operator );
2614  
2615      if ( 'names' === $output ) {
2616          $types = wp_list_pluck( $types, 'name' );
2617      }
2618  
2619      return $types;
2620  }
2621  
2622  /**
2623   * Retrieve a group type object by name.
2624   *
2625   * @since 2.6.0
2626   *
2627   * @param string $group_type The name of the group type.
2628   * @return object A group type object.
2629   */
2630  function bp_groups_get_group_type_object( $group_type ) {
2631      $types = bp_groups_get_group_types( array(), 'objects' );
2632  
2633      if ( empty( $types[ $group_type ] ) ) {
2634          return null;
2635      }
2636  
2637      return $types[ $group_type ];
2638  }
2639  
2640  /**
2641   * Set type for a group.
2642   *
2643   * @since 2.6.0
2644   * @since 2.7.0 $group_type parameter also accepts an array of group types now.
2645   *
2646   * @param int          $group_id   ID of the group.
2647   * @param string|array $group_type Group type or array of group types to set.
2648   * @param bool         $append     Optional. True to append this to existing types for group,
2649   *                                 false to replace. Default: false.
2650   * @return false|array $retval See bp_set_object_terms().
2651   */
2652  function bp_groups_set_group_type( $group_id, $group_type, $append = false ) {
2653      // Pass an empty group type to remove group's type.
2654      if ( ! empty( $group_type ) && is_string( $group_type ) && ! bp_groups_get_group_type_object( $group_type ) ) {
2655          return false;
2656      }
2657  
2658      // Cast as array.
2659      $group_type = (array) $group_type;
2660  
2661      // Validate group types.
2662      foreach ( $group_type as $type ) {
2663          // Remove any invalid group types.
2664          if ( is_null( bp_groups_get_group_type_object( $type ) ) ) {
2665              unset( $group_type[ $type ] );
2666          }
2667      }
2668  
2669      $retval = bp_set_object_terms( $group_id, $group_type, 'bp_group_type', $append );
2670  
2671      // Bust the cache if the type has been updated.
2672      if ( ! is_wp_error( $retval ) ) {
2673          wp_cache_delete( $group_id, 'bp_groups_group_type' );
2674  
2675          /**
2676           * Fires just after a group type has been changed.
2677           *
2678           * @since 2.6.0
2679           *
2680           * @param int          $group_id   ID of the group whose group type has been updated.
2681           * @param string|array $group_type Group type or array of group types.
2682           * @param bool         $append     Whether the type is being appended to existing types.
2683           */
2684          do_action( 'bp_groups_set_group_type', $group_id, $group_type, $append );
2685      }
2686  
2687      return $retval;
2688  }
2689  
2690  /**
2691   * Get type for a group.
2692   *
2693   * @since 2.6.0
2694   *
2695   * @param int  $group_id ID of the group.
2696   * @param bool $single   Optional. Whether to return a single type string. If multiple types are found
2697   *                       for the group, the oldest one will be returned. Default: true.
2698   * @return string|array|bool On success, returns a single group type (if `$single` is true) or an array of group
2699   *                           types (if `$single` is false). Returns false on failure.
2700   */
2701  function bp_groups_get_group_type( $group_id, $single = true ) {
2702      $types = wp_cache_get( $group_id, 'bp_groups_group_type' );
2703  
2704      if ( false === $types ) {
2705          $raw_types = bp_get_object_terms( $group_id, 'bp_group_type' );
2706  
2707          if ( ! is_wp_error( $raw_types ) ) {
2708              $types = array();
2709  
2710              // Only include currently registered group types.
2711              foreach ( $raw_types as $gtype ) {
2712                  if ( bp_groups_get_group_type_object( $gtype->name ) ) {
2713                      $types[] = $gtype->name;
2714                  }
2715              }
2716  
2717              wp_cache_set( $group_id, $types, 'bp_groups_group_type' );
2718          }
2719      }
2720  
2721      $type = false;
2722      if ( ! empty( $types ) ) {
2723          if ( $single ) {
2724              $type = end( $types );
2725          } else {
2726              $type = $types;
2727          }
2728      }
2729  
2730      /**
2731       * Filters a groups's group type(s).
2732       *
2733       * @since 2.6.0
2734       *
2735       * @param string|array $type     Group type.
2736       * @param int          $group_id ID of the group.
2737       * @param bool         $single   Whether to return a single type string, or an array.
2738       */
2739      return apply_filters( 'bp_groups_get_group_type', $type, $group_id, $single );
2740  }
2741  
2742  /**
2743   * Remove type for a group.
2744   *
2745   * @since 2.6.0
2746   *
2747   * @param int            $group_id   ID of the user.
2748   * @param string         $group_type Group type.
2749   * @return bool|WP_Error $deleted    True on success. False or WP_Error on failure.
2750   */
2751  function bp_groups_remove_group_type( $group_id, $group_type ) {
2752      if ( empty( $group_type ) || ! bp_groups_get_group_type_object( $group_type ) ) {
2753          return false;
2754      }
2755  
2756      $deleted = bp_remove_object_terms( $group_id, $group_type, 'bp_group_type' );
2757  
2758      // Bust the case, if the type has been removed.
2759      if ( ! is_wp_error( $deleted ) ) {
2760          wp_cache_delete( $group_id, 'bp_groups_group_type' );
2761  
2762          /**
2763           * Fires just after a group's group type has been removed.
2764           *
2765           * @since 2.6.0
2766           *
2767           * @param int    $group      ID of the group whose group type has been removed.
2768           * @param string $group_type Group type.
2769           */
2770          do_action( 'bp_groups_remove_group_type', $group_id, $group_type );
2771      }
2772  
2773      return $deleted;
2774  }
2775  
2776  /**
2777   * Check whether the given group has a certain group type.
2778   *
2779   * @since 2.6.0
2780   *
2781   * @param  int    $group_id   ID of the group.
2782   * @param  string $group_type Group type.
2783   * @return bool   Whether the group has the give group type.
2784   */
2785  function bp_groups_has_group_type( $group_id, $group_type ) {
2786      if ( empty( $group_type ) || ! bp_groups_get_group_type_object( $group_type ) ) {
2787          return false;
2788      }
2789  
2790      // Get all group's group types.
2791      $types = bp_groups_get_group_type( $group_id, false );
2792  
2793      if ( ! is_array( $types ) ) {
2794          return false;
2795      }
2796  
2797      return in_array( $group_type, $types );
2798  }
2799  
2800  /**
2801   * Get the "current" group type, if one is provided, in group directories.
2802   *
2803   * @since 2.7.0
2804   *
2805   * @return string
2806   */
2807  function bp_get_current_group_directory_type() {
2808  
2809      /**
2810       * Filters the "current" group type, if one is provided, in group directories.
2811       *
2812       * @since 2.7.0
2813       *
2814       * @param string $value "Current" group type.
2815       */
2816      return apply_filters( 'bp_get_current_group_directory_type', buddypress()->groups->current_directory_type );
2817  }
2818  
2819  /**
2820   * Delete a group's type when the group is deleted.
2821   *
2822   * @since 2.6.0
2823   *
2824   * @param  int   $group_id ID of the group.
2825   * @return array|null $value    See {@see bp_groups_set_group_type()}.
2826   */
2827  function bp_remove_group_type_on_group_delete( $group_id = 0 ) {
2828      bp_groups_set_group_type( $group_id, '' );
2829  }
2830  add_action( 'groups_delete_group', 'bp_remove_group_type_on_group_delete' );
2831  
2832  /**
2833   * Finds and exports group membership data associated with an email address.
2834   *
2835   * @since 4.0.0
2836   *
2837   * @param string $email_address  The user's email address.
2838   * @param int    $page           Batch number.
2839   * @return array An array of personal data.
2840   */
2841  function bp_groups_memberships_personal_data_exporter( $email_address, $page ) {
2842      $number = 20;
2843  
2844      $email_address = trim( $email_address );
2845  
2846      $data_to_export = array();
2847  
2848      $user = get_user_by( 'email', $email_address );
2849  
2850      if ( ! $user ) {
2851          return array(
2852              'data' => array(),
2853              'done' => true,
2854          );
2855      }
2856  
2857      $memberships = BP_Groups_Member::get_user_memberships( $user->ID, array(
2858          'type'     => 'membership',
2859          'page'     => $page,
2860          'per_page' => $number,
2861      ) );
2862  
2863      foreach ( $memberships as $membership ) {
2864          $group = groups_get_group( $membership->group_id );
2865  
2866          $item_data = array(
2867              array(
2868                  'name'  => __( 'Group Name', 'buddypress' ),
2869                  'value' => bp_get_group_name( $group ),
2870              ),
2871              array(
2872                  'name'  => __( 'Group URL', 'buddypress' ),
2873                  'value' => bp_get_group_permalink( $group ),
2874              ),
2875          );
2876  
2877          if ( $membership->inviter_id ) {
2878              $item_data[] = array(
2879                  'name'  => __( 'Invited By', 'buddypress' ),
2880                  'value' => bp_core_get_userlink( $membership->inviter_id ),
2881              );
2882          }
2883  
2884          if ( $group->creator_id === $user->ID ) {
2885              $group_role = __( 'Creator', 'buddypress' );
2886          } elseif ( $membership->is_admin ) {
2887              $group_role = __( 'Admin', 'buddypress' );
2888          } elseif ( $membership->is_mod ) {
2889              $group_role = __( 'Moderator', 'buddypress' );
2890          } else {
2891              $group_role = __( 'Member', 'buddypress' );
2892          }
2893  
2894          $item_data[] = array(
2895              'name'  => __( 'Group Role', 'buddypress' ),
2896              'value' => $group_role,
2897          );
2898  
2899          $item_data[] = array(
2900              'name'  => __( 'Date Joined', 'buddypress' ),
2901              'value' => $membership->date_modified,
2902          );
2903  
2904          $data_to_export[] = array(
2905              'group_id'    => 'bp_groups_memberships',
2906              'group_label' => __( 'Group Memberships', 'buddypress' ),
2907              'item_id'     => "bp-group-membership-{$group->id}",
2908              'data'        => $item_data,
2909          );
2910      }
2911  
2912      // Tell core if we have more items to process.
2913      $done = count( $memberships ) < $number;
2914  
2915      return array(
2916          'data' => $data_to_export,
2917          'done' => $done,
2918      );
2919  }
2920  
2921  /**
2922   * Finds and exports data on pending group membership requests associated with an email address.
2923   *
2924   * @since 4.0.0
2925   *
2926   * @param string $email_address  The user's email address.
2927   * @param int    $page           Batch number.
2928   * @return array An array of personal data.
2929   */
2930  function bp_groups_pending_requests_personal_data_exporter( $email_address, $page ) {
2931      $number = 20;
2932  
2933      $email_address = trim( $email_address );
2934  
2935      $data_to_export = array();
2936  
2937      $user = get_user_by( 'email', $email_address );
2938  
2939      if ( ! $user ) {
2940          return array(
2941              'data' => array(),
2942              'done' => true,
2943          );
2944      }
2945  
2946      $requests = groups_get_requests( array(
2947          'user_id'  => $user->ID,
2948          'page'     => $page,
2949          'per_page' => $number,
2950      ) );
2951  
2952      foreach ( $requests as $request ) {
2953          $group = groups_get_group( $request->item_id );
2954  
2955          $item_data = array(
2956              array(
2957                  'name'  => __( 'Group Name', 'buddypress' ),
2958                  'value' => bp_get_group_name( $group ),
2959              ),
2960              array(
2961                  'name'  => __( 'Group URL', 'buddypress' ),
2962                  'value' => bp_get_group_permalink( $group ),
2963              ),
2964              array(
2965                  'name'  => __( 'Date Sent', 'buddypress' ),
2966                  'value' => $request->date_modified,
2967              ),
2968          );
2969  
2970          $data_to_export[] = array(
2971              'group_id'    => 'bp_groups_pending_requests',
2972              'group_label' => __( 'Pending Group Membership Requests', 'buddypress' ),
2973              'item_id'     => "bp-group-pending-request-{$group->id}",
2974              'data'        => $item_data,
2975          );
2976      }
2977  
2978      // Tell core if we have more items to process.
2979      $done = count( $requests ) < $number;
2980  
2981      return array(
2982          'data' => $data_to_export,
2983          'done' => $done,
2984      );
2985  }
2986  
2987  /**
2988   * Finds and exports data on pending group invitations sent by a user associated with an email address.
2989   *
2990   * @since 4.0.0
2991   *
2992   * @param string $email_address  The user's email address.
2993   * @param int    $page           Batch number.
2994   * @return array An array of personal data.
2995   */
2996  function bp_groups_pending_sent_invitations_personal_data_exporter( $email_address, $page ) {
2997      $number = 20;
2998  
2999      $email_address = trim( $email_address );
3000  
3001      $data_to_export = array();
3002  
3003      $user = get_user_by( 'email', $email_address );
3004  
3005      if ( ! $user ) {
3006          return array(
3007              'data' => array(),
3008              'done' => true,
3009          );
3010      }
3011  
3012      $invitations = groups_get_invites( array(
3013          'inviter_id'  => $user->ID,
3014          'page'        => $page,
3015          'per_page'    => $number,
3016      ) );
3017  
3018      foreach ( $invitations as $invitation ) {
3019          $group = groups_get_group( $invitation->item_id );
3020  
3021          $item_data = array(
3022              array(
3023                  'name'  => __( 'Group Name', 'buddypress' ),
3024                  'value' => bp_get_group_name( $group ),
3025              ),
3026              array(
3027                  'name'  => __( 'Group URL', 'buddypress' ),
3028                  'value' => bp_get_group_permalink( $group ),
3029              ),
3030              array(
3031                  'name'  => __( 'Sent To', 'buddypress' ),
3032                  'value' => bp_core_get_userlink( $invitation->user_id ),
3033              ),
3034              array(
3035                  'name'  => __( 'Date Sent', 'buddypress' ),
3036                  'value' => $invitation->date_modified,
3037              ),
3038          );
3039  
3040          $data_to_export[] = array(
3041              'group_id'    => 'bp_groups_pending_sent_invitations',
3042              'group_label' => __( 'Pending Group Invitations (Sent)', 'buddypress' ),
3043              'item_id'     => "bp-group-pending-sent-invitation-{$group->id}",
3044              'data'        => $item_data,
3045          );
3046      }
3047  
3048      // Tell core if we have more items to process.
3049      $done = count( $invitations ) < $number;
3050  
3051      return array(
3052          'data' => $data_to_export,
3053          'done' => $done,
3054      );
3055  }
3056  
3057  /**
3058   * Finds and exports data on pending group invitations received by a user associated with an email address.
3059   *
3060   * @since 4.0.0
3061   *
3062   * @param string $email_address  The user's email address.
3063   * @param int    $page           Batch number.
3064   * @return array An array of personal data.
3065   */
3066  function bp_groups_pending_received_invitations_personal_data_exporter( $email_address, $page ) {
3067      $number = 20;
3068  
3069      $email_address = trim( $email_address );
3070  
3071      $data_to_export = array();
3072  
3073      $user = get_user_by( 'email', $email_address );
3074  
3075      if ( ! $user ) {
3076          return array(
3077              'data' => array(),
3078              'done' => true,
3079          );
3080      }
3081  
3082      $invitations = groups_get_invites( array(
3083          'user_id'  => $user->ID,
3084          'page'     => $page,
3085          'per_page' => $number,
3086      ) );
3087  
3088      foreach ( $invitations as $invitation ) {
3089          $group = groups_get_group( $invitation->item_id );
3090  
3091          $item_data = array(
3092              array(
3093                  'name'  => __( 'Group Name', 'buddypress' ),
3094                  'value' => bp_get_group_name( $group ),
3095              ),
3096              array(
3097                  'name'  => __( 'Group URL', 'buddypress' ),
3098                  'value' => bp_get_group_permalink( $group ),
3099              ),
3100              array(
3101                  'name'  => __( 'Invited By', 'buddypress' ),
3102                  'value' => bp_core_get_userlink( $invitation->inviter_id ),
3103              ),
3104              array(
3105                  'name'  => __( 'Date Sent', 'buddypress' ),
3106                  'value' => $invitation->date_modified,
3107              ),
3108          );
3109  
3110          $data_to_export[] = array(
3111              'group_id'    => 'bp_groups_pending_received_invitations',
3112              'group_label' => __( 'Pending Group Invitations (Received)', 'buddypress' ),
3113              'item_id'     => "bp-group-pending-received-invitation-{$group->id}",
3114              'data'        => $item_data,
3115          );
3116      }
3117  
3118      // Tell core if we have more items to process.
3119      $done = count( $invitations ) < $number;
3120  
3121      return array(
3122          'data' => $data_to_export,
3123          'done' => $done,
3124      );
3125  }
3126  
3127  /**
3128   * Migrate invitations and requests from pre-5.0 group_members table to invitations table.
3129   *
3130   * @since 5.0.0
3131   */
3132  function bp_groups_migrate_invitations() {
3133      global $wpdb;
3134      $bp = buddypress();
3135  
3136      $records = $wpdb->get_results( "SELECT id, group_id, user_id, inviter_id, date_modified, comments, invite_sent FROM {$bp->groups->table_name_members} WHERE is_confirmed = 0 AND is_banned = 0" );
3137      if ( empty( $records ) ) {
3138          return;
3139      }
3140  
3141      $processed = array();
3142      $values = array();
3143      foreach ( $records as $record ) {
3144          $values[] = $wpdb->prepare(
3145              "(%d, %d, %s, %s, %d, %d, %s, %s, %s, %d, %d)",
3146              (int) $record->user_id,
3147              (int) $record->inviter_id,
3148              '',
3149              'bp_groups_invitation_manager',
3150              (int) $record->group_id,
3151              0,
3152              ( 0 === (int) $record->inviter_id ) ? 'request' : 'invite',
3153              $record->comments,
3154              $record->date_modified,
3155              (int) $record->invite_sent,
3156              0
3157          );
3158          $processed[] = (int) $record->id;
3159      }
3160  
3161      $table_name = BP_Invitation_Manager::get_table_name();
3162      $query = "INSERT INTO {$table_name} (user_id, inviter_id, invitee_email, class, item_id, secondary_item_id, type, content, date_modified, invite_sent, accepted) VALUES ";
3163      $query .= implode(', ', $values );
3164      $query .= ';';
3165      $wpdb->query( $query );
3166  
3167      $ids_to_delete = implode( ',', $processed );
3168      if ( $ids_to_delete ) {
3169          $wpdb->query( "DELETE FROM {$bp->groups->table_name_members} WHERE ID IN ($ids_to_delete)" );
3170      }
3171  }


Generated: Sat Sep 21 01:01:46 2019 Cross-referenced by PHPXref 0.7.1