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


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