[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-groups/classes/ -> class-bp-groups-member.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Groups Classes.
   4   *
   5   * @package BuddyPress
   6   * @subpackage GroupsClasses
   7   * @since 1.6.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * BuddyPress Group Membership object.
  15   */
  16  class BP_Groups_Member {
  17  
  18      /**
  19       * ID of the membership.
  20       *
  21       * @since 1.6.0
  22       * @var int
  23       */
  24      var $id;
  25  
  26      /**
  27       * ID of the group associated with the membership.
  28       *
  29       * @since 1.6.0
  30       * @var int
  31       */
  32      var $group_id;
  33  
  34      /**
  35       * ID of the user associated with the membership.
  36       *
  37       * @since 1.6.0
  38       * @var int
  39       */
  40      var $user_id;
  41  
  42      /**
  43       * ID of the user whose invitation initiated the membership.
  44       *
  45       * @since 1.6.0
  46       * @var int
  47       */
  48      var $inviter_id;
  49  
  50      /**
  51       * Whether the member is an admin of the group.
  52       *
  53       * @since 1.6.0
  54       * @var int
  55       */
  56      var $is_admin;
  57  
  58      /**
  59       * Whether the member is a mod of the group.
  60       *
  61       * @since 1.6.0
  62       * @var int
  63       */
  64      var $is_mod;
  65  
  66      /**
  67       * Whether the member is banned from the group.
  68       *
  69       * @since 1.6.0
  70       * @var int
  71       */
  72      var $is_banned;
  73  
  74      /**
  75       * Title used to describe the group member's role in the group.
  76       *
  77       * Eg, 'Group Admin'.
  78       *
  79       * @since 1.6.0
  80       * @var int
  81       */
  82      var $user_title;
  83  
  84      /**
  85       * Last modified date of the membership.
  86       *
  87       * This value is updated when, eg, invitations are accepted.
  88       *
  89       * @since 1.6.0
  90       * @var string
  91       */
  92      var $date_modified;
  93  
  94      /**
  95       * Whether the membership has been confirmed.
  96       *
  97       * @since 1.6.0
  98       * @var int
  99       */
 100      var $is_confirmed;
 101  
 102      /**
 103       * Comments associated with the membership.
 104       *
 105       * In BP core, these are limited to the optional message users can
 106       * include when requesting membership to a private group.
 107       *
 108       * @since 1.6.0
 109       * @var string
 110       */
 111      var $comments;
 112  
 113      /**
 114       * Whether an invitation has been sent for this membership.
 115       *
 116       * The purpose of this flag is to mark when an invitation has been
 117       * "drafted" (the user has been added via the interface at Send
 118       * Invites), but the Send button has not been pressed, so the
 119       * invitee has not yet been notified.
 120       *
 121       * @since 1.6.0
 122       * @var int
 123       */
 124      var $invite_sent;
 125  
 126      /**
 127       * WP_User object representing the membership's user.
 128       *
 129       * @since 1.6.0
 130       * @var WP_User
 131       */
 132      protected $user;
 133  
 134      /**
 135       * Constructor method.
 136       *
 137       * @since 1.6.0
 138       *
 139       * @param int      $user_id  Optional. Along with $group_id, can be used to
 140       *                           look up a membership.
 141       * @param int      $group_id Optional. Along with $user_id, can be used to
 142       *                           look up a membership.
 143       * @param int|bool $id       Optional. The unique ID of the membership object.
 144       * @param bool     $populate Whether to populate the properties of the
 145       *                           located membership. Default: true.
 146       */
 147  	public function __construct( $user_id = 0, $group_id = 0, $id = false, $populate = true ) {
 148  
 149          // User and group are not empty, and ID is.
 150          if ( !empty( $user_id ) && !empty( $group_id ) && empty( $id ) ) {
 151              $this->user_id  = $user_id;
 152              $this->group_id = $group_id;
 153  
 154              if ( !empty( $populate ) ) {
 155                  $this->populate();
 156              }
 157          }
 158  
 159          // ID is not empty.
 160          if ( !empty( $id ) ) {
 161              $this->id = $id;
 162  
 163              if ( !empty( $populate ) ) {
 164                  $this->populate();
 165              }
 166          }
 167      }
 168  
 169      /**
 170       * Populate the object's properties.
 171       *
 172       * @since 1.6.0
 173       */
 174  	public function populate() {
 175          global $wpdb;
 176  
 177          $bp = buddypress();
 178  
 179          if ( $this->user_id && $this->group_id && !$this->id )
 180              $sql = $wpdb->prepare( "SELECT * FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $this->user_id, $this->group_id );
 181  
 182          if ( !empty( $this->id ) )
 183              $sql = $wpdb->prepare( "SELECT * FROM {$bp->groups->table_name_members} WHERE id = %d", $this->id );
 184  
 185          $member = $wpdb->get_row($sql);
 186  
 187          if ( !empty( $member ) ) {
 188              $this->id            = (int) $member->id;
 189              $this->group_id      = (int) $member->group_id;
 190              $this->user_id       = (int) $member->user_id;
 191              $this->inviter_id    = (int) $member->inviter_id;
 192              $this->is_admin      = (int) $member->is_admin;
 193              $this->is_mod        = (int) $member->is_mod;
 194              $this->is_banned     = (int) $member->is_banned;
 195              $this->user_title    = $member->user_title;
 196              $this->date_modified = $member->date_modified;
 197              $this->is_confirmed  = (int) $member->is_confirmed;
 198              $this->comments      = $member->comments;
 199              $this->invite_sent   = (int) $member->invite_sent;
 200          }
 201      }
 202  
 203      /**
 204       * Magic getter.
 205       *
 206       * @since 2.8.0
 207       *
 208       * @param string $key Key.
 209       * @return BP_Core_User|null
 210       */
 211  	public function __get( $key ) {
 212          switch ( $key ) {
 213              case 'user' :
 214                  return $this->get_user_object( $this->user_id );
 215          }
 216      }
 217  
 218      /**
 219       * Magic issetter.
 220       *
 221       * @since 2.8.0
 222       *
 223       * @param string $key Key.
 224       * @return bool
 225       */
 226  	public function __isset( $key ) {
 227          switch ( $key ) {
 228              case 'user' :
 229                  return true;
 230  
 231              default :
 232                  return isset( $this->{$key} );
 233          }
 234      }
 235  
 236      /**
 237       * Get the user object corresponding to this membership.
 238       *
 239       * Used for lazyloading the protected `user` property.
 240       *
 241       * @since 2.8.0
 242       *
 243       * @return BP_Core_User
 244       */
 245  	protected function get_user_object() {
 246          if ( empty( $this->user ) ) {
 247              $this->user = new BP_Core_User( $this->user_id );
 248          }
 249  
 250          return $this->user;
 251      }
 252  
 253      /**
 254       * Save the membership data to the database.
 255       *
 256       * @since 1.6.0
 257       *
 258       * @return bool True on success, false on failure.
 259       */
 260  	public function save() {
 261          global $wpdb;
 262  
 263          $bp = buddypress();
 264  
 265          $this->user_id       = apply_filters( 'groups_member_user_id_before_save',       $this->user_id,       $this->id );
 266          $this->group_id      = apply_filters( 'groups_member_group_id_before_save',      $this->group_id,      $this->id );
 267          $this->inviter_id    = apply_filters( 'groups_member_inviter_id_before_save',    $this->inviter_id,    $this->id );
 268          $this->is_admin      = apply_filters( 'groups_member_is_admin_before_save',      $this->is_admin,      $this->id );
 269          $this->is_mod        = apply_filters( 'groups_member_is_mod_before_save',        $this->is_mod,        $this->id );
 270          $this->is_banned     = apply_filters( 'groups_member_is_banned_before_save',     $this->is_banned,     $this->id );
 271          $this->user_title    = apply_filters( 'groups_member_user_title_before_save',    $this->user_title,    $this->id );
 272          $this->date_modified = apply_filters( 'groups_member_date_modified_before_save', $this->date_modified, $this->id );
 273          $this->is_confirmed  = apply_filters( 'groups_member_is_confirmed_before_save',  $this->is_confirmed,  $this->id );
 274          $this->comments      = apply_filters( 'groups_member_comments_before_save',      $this->comments,      $this->id );
 275          $this->invite_sent   = apply_filters( 'groups_member_invite_sent_before_save',   $this->invite_sent,   $this->id );
 276  
 277          /**
 278           * Fires before the current group membership item gets saved.
 279           *
 280           * Please use this hook to filter the properties above. Each part will be passed in.
 281           *
 282           * @since 1.0.0
 283           *
 284           * @param BP_Groups_Member $this Current instance of the group membership item being saved. Passed by reference.
 285           */
 286          do_action_ref_array( 'groups_member_before_save', array( &$this ) );
 287  
 288          // The following properties are required; bail if not met.
 289          if ( empty( $this->user_id ) || empty( $this->group_id ) ) {
 290              return false;
 291          }
 292  
 293          if ( !empty( $this->id ) ) {
 294              $sql = $wpdb->prepare( "UPDATE {$bp->groups->table_name_members} SET inviter_id = %d, is_admin = %d, is_mod = %d, is_banned = %d, user_title = %s, date_modified = %s, is_confirmed = %d, comments = %s, invite_sent = %d WHERE id = %d", $this->inviter_id, $this->is_admin, $this->is_mod, $this->is_banned, $this->user_title, $this->date_modified, $this->is_confirmed, $this->comments, $this->invite_sent, $this->id );
 295          } else {
 296              // Ensure that user is not already a member of the group before inserting.
 297              if ( $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 1 LIMIT 1", $this->user_id, $this->group_id ) ) ) {
 298                  return false;
 299              }
 300  
 301              $sql = $wpdb->prepare( "INSERT INTO {$bp->groups->table_name_members} ( user_id, group_id, inviter_id, is_admin, is_mod, is_banned, user_title, date_modified, is_confirmed, comments, invite_sent ) VALUES ( %d, %d, %d, %d, %d, %d, %s, %s, %d, %s, %d )", $this->user_id, $this->group_id, $this->inviter_id, $this->is_admin, $this->is_mod, $this->is_banned, $this->user_title, $this->date_modified, $this->is_confirmed, $this->comments, $this->invite_sent );
 302          }
 303  
 304          if ( !$wpdb->query( $sql ) )
 305              return false;
 306  
 307          $this->id = $wpdb->insert_id;
 308  
 309          // Update the user's group count.
 310          self::refresh_total_group_count_for_user( $this->user_id );
 311  
 312          // Update the group's member count.
 313          self::refresh_total_member_count_for_group( $this->group_id );
 314  
 315          /**
 316           * Fires after the current group membership item has been saved.
 317           *
 318           * Please use this hook to filter the properties above. Each part will be passed in.
 319           *
 320           * @since 1.0.0
 321           *
 322           * @param BP_Groups_Member $this Current instance of the group membership item has been saved. Passed by reference.
 323           */
 324          do_action_ref_array( 'groups_member_after_save', array( &$this ) );
 325  
 326          return true;
 327      }
 328  
 329      /**
 330       * Promote a member to a new status.
 331       *
 332       * @since 1.6.0
 333       *
 334       * @param string $status The new status. 'mod' or 'admin'.
 335       * @return bool True on success, false on failure.
 336       */
 337  	public function promote( $status = 'mod' ) {
 338          if ( 'mod' == $status ) {
 339              $this->is_admin   = 0;
 340              $this->is_mod     = 1;
 341              $this->user_title = __( 'Group Mod', 'buddypress' );
 342          }
 343  
 344          if ( 'admin' == $status ) {
 345              $this->is_admin   = 1;
 346              $this->is_mod     = 0;
 347              $this->user_title = __( 'Group Admin', 'buddypress' );
 348          }
 349  
 350          return $this->save();
 351      }
 352  
 353      /**
 354       * Demote membership to Member status (non-admin, non-mod).
 355       *
 356       * @since 1.6.0
 357       *
 358       * @return bool True on success, false on failure.
 359       */
 360  	public function demote() {
 361          $this->is_mod     = 0;
 362          $this->is_admin   = 0;
 363          $this->user_title = false;
 364  
 365          return $this->save();
 366      }
 367  
 368      /**
 369       * Ban the user from the group.
 370       *
 371       * @since 1.6.0
 372       *
 373       * @return bool True on success, false on failure.
 374       */
 375  	public function ban() {
 376          if ( !empty( $this->is_admin ) )
 377              return false;
 378  
 379          $this->is_mod = 0;
 380          $this->is_banned = 1;
 381  
 382          return $this->save();
 383      }
 384  
 385      /**
 386       * Unban the user from the group.
 387       *
 388       * @since 1.6.0
 389       *
 390       * @return bool True on success, false on failure.
 391       */
 392  	public function unban() {
 393          if ( !empty( $this->is_admin ) )
 394              return false;
 395  
 396          $this->is_banned = 0;
 397  
 398          return $this->save();
 399      }
 400  
 401      /**
 402       * Mark a pending invitation as accepted.
 403       *
 404       * @since 1.6.0
 405       */
 406  	public function accept_invite() {
 407          $this->inviter_id    = 0;
 408          $this->is_confirmed  = 1;
 409          $this->date_modified = bp_core_current_time();
 410      }
 411  
 412      /**
 413       * Confirm a membership request.
 414       *
 415       * @since 1.6.0
 416       */
 417  	public function accept_request() {
 418          $this->is_confirmed = 1;
 419          $this->date_modified = bp_core_current_time();
 420      }
 421  
 422      /**
 423       * Remove the current membership.
 424       *
 425       * @since 1.6.0
 426       *
 427       * @return bool True on success, false on failure.
 428       */
 429  	public function remove() {
 430          global $wpdb;
 431  
 432          /**
 433           * Fires before a member is removed from a group.
 434           *
 435           * @since 2.3.0
 436           *
 437           * @param BP_Groups_Member $this Current group membership object.
 438           */
 439          do_action_ref_array( 'groups_member_before_remove', array( $this ) );
 440  
 441          $bp  = buddypress();
 442          $sql = $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $this->user_id, $this->group_id );
 443  
 444          if ( !$result = $wpdb->query( $sql ) )
 445              return false;
 446  
 447          // Update the user's group count.
 448          self::refresh_total_group_count_for_user( $this->user_id );
 449  
 450          // Update the group's member count.
 451          self::refresh_total_member_count_for_group( $this->group_id );
 452  
 453          /**
 454           * Fires after a member is removed from a group.
 455           *
 456           * @since 2.3.0
 457           *
 458           * @param BP_Groups_Member $this Current group membership object.
 459           */
 460          do_action_ref_array( 'groups_member_after_remove', array( $this ) );
 461  
 462          return $result;
 463      }
 464  
 465      /** Static Methods ****************************************************/
 466  
 467      /**
 468       * Refresh the `total_group_count` for a user.
 469       *
 470       * @since 1.8.0
 471       *
 472       * @param int $user_id ID of the user.
 473       */
 474  	public static function refresh_total_group_count_for_user( $user_id ) {
 475          bp_update_user_meta( $user_id, 'total_group_count', (int) self::total_group_count( $user_id ) );
 476      }
 477  
 478      /**
 479       * Refresh the `total_member_count` for a group.
 480       *
 481       * The request skip the current cache so that we always grab the lastest total count.
 482       *
 483       * @since 1.8.0
 484       * @since 10.0.0 Updated to use `BP_Groups_Group::get_total_member_count`
 485       *
 486       * @param int $group_id ID of the group.
 487       */
 488  	public static function refresh_total_member_count_for_group( $group_id ) {
 489          BP_Groups_Group::get_total_member_count( $group_id, true );
 490      }
 491  
 492      /**
 493       * Delete a membership, based on user + group IDs.
 494       *
 495       * @since 1.6.0
 496       *
 497       * @param int $user_id  ID of the user.
 498       * @param int $group_id ID of the group.
 499       * @return bool True on success, false on failure.
 500       */
 501  	public static function delete( $user_id, $group_id ) {
 502          global $wpdb;
 503  
 504          /**
 505           * Fires before a group membership is deleted.
 506           *
 507           * @since 2.3.0
 508           *
 509           * @param int $user_id  ID of the user.
 510           * @param int $group_id ID of the group.
 511           */
 512          do_action( 'bp_groups_member_before_delete', $user_id, $group_id );
 513  
 514          $bp     = buddypress();
 515          $remove = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $user_id, $group_id ) );
 516  
 517          // Update the user's group count.
 518          self::refresh_total_group_count_for_user( $user_id );
 519  
 520          // Update the group's member count.
 521          self::refresh_total_member_count_for_group( $group_id );
 522  
 523          /**
 524           * Fires after a member is removed from a group.
 525           *
 526           * @since 2.3.0
 527           *
 528           * @param int $user_id  ID of the user.
 529           * @param int $group_id ID of the group.
 530           */
 531          do_action( 'bp_groups_member_after_delete', $user_id, $group_id );
 532  
 533          return (bool) $remove;
 534      }
 535  
 536      /**
 537       * Get the IDs of the groups of which a specified user is a member.
 538       *
 539       * @since 1.6.0
 540       *
 541       * @param int      $user_id ID of the user.
 542       * @param int|bool $limit   Optional. Max number of results to return.
 543       *                          Default: false (no limit).
 544       * @param int|bool $page    Optional. Page offset of results to return.
 545       *                          Default: false (no limit).
 546       * @return array {
 547       *     @type array $groups Array of groups returned by paginated query.
 548       *     @type int   $total  Count of groups matching query.
 549       * }
 550       */
 551  	public static function get_group_ids( $user_id, $limit = false, $page = false ) {
 552          global $wpdb;
 553  
 554          $pag_sql = '';
 555          if ( !empty( $limit ) && !empty( $page ) )
 556              $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
 557  
 558          $bp = buddypress();
 559  
 560          // If the user is logged in and viewing their own groups, we can show hidden and private groups.
 561          if ( $user_id != bp_loggedin_user_id() ) {
 562              $group_sql = $wpdb->prepare( "SELECT DISTINCT m.group_id FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.status != 'hidden' AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0{$pag_sql}", $user_id );
 563              $total_groups = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.status != 'hidden' AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0", $user_id ) );
 564          } else {
 565              $group_sql = $wpdb->prepare( "SELECT DISTINCT group_id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND is_confirmed = 1 AND is_banned = 0{$pag_sql}", $user_id );
 566              $total_groups = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT group_id) FROM {$bp->groups->table_name_members} WHERE user_id = %d AND is_confirmed = 1 AND is_banned = 0", $user_id ) );
 567          }
 568  
 569          $groups = $wpdb->get_col( $group_sql );
 570  
 571          return array( 'groups' => $groups, 'total' => (int) $total_groups );
 572      }
 573  
 574      /**
 575       * Get the IDs of the groups of which a specified user is a member, sorted by the date joined.
 576       *
 577       * @since 1.6.0
 578       *
 579       * @param int         $user_id ID of the user.
 580       * @param int|bool    $limit   Optional. Max number of results to return.
 581       *                             Default: false (no limit).
 582       * @param int|bool    $page    Optional. Page offset of results to return.
 583       *                             Default: false (no limit).
 584       * @param string|bool $filter  Optional. Limit results to groups whose name or
 585       *                             description field matches search terms.
 586       * @return array {
 587       *     @type array $groups Array of groups returned by paginated query.
 588       *     @type int   $total  Count of groups matching query.
 589       * }
 590       */
 591  	public static function get_recently_joined( $user_id, $limit = false, $page = false, $filter = false ) {
 592          global $wpdb;
 593  
 594          $user_id_sql = $pag_sql = $hidden_sql = $filter_sql = '';
 595  
 596          $user_id_sql = $wpdb->prepare( 'm.user_id = %d', $user_id );
 597  
 598          if ( !empty( $limit ) && !empty( $page ) )
 599              $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
 600  
 601          if ( !empty( $filter ) ) {
 602              $search_terms_like = '%' . bp_esc_like( $filter ) . '%';
 603              $filter_sql = $wpdb->prepare( " AND ( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like );
 604          }
 605  
 606          if ( $user_id != bp_loggedin_user_id() )
 607              $hidden_sql = " AND g.status != 'hidden'";
 608  
 609          $bp = buddypress();
 610  
 611          $paged_groups = $wpdb->get_results( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count'{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_confirmed = 1 AND m.is_banned = 0 ORDER BY m.date_modified DESC {$pag_sql}" );
 612          $total_groups = $wpdb->get_var( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_banned = 0 AND m.is_confirmed = 1 ORDER BY m.date_modified DESC" );
 613  
 614          return array( 'groups' => $paged_groups, 'total' => $total_groups );
 615      }
 616  
 617      /**
 618       * Get the IDs of the groups of which a specified user is an admin.
 619       *
 620       * @since 1.6.0
 621       *
 622       * @param int         $user_id ID of the user.
 623       * @param int|bool    $limit   Optional. Max number of results to return.
 624       *                             Default: false (no limit).
 625       * @param int|bool    $page    Optional. Page offset of results to return.
 626       *                             Default: false (no limit).
 627       * @param string|bool $filter  Optional. Limit results to groups whose name or
 628       *                             description field matches search terms.
 629       * @return array {
 630       *     @type array $groups Array of groups returned by paginated query.
 631       *     @type int   $total  Count of groups matching query.
 632       * }
 633       */
 634  	public static function get_is_admin_of( $user_id, $limit = false, $page = false, $filter = false ) {
 635          global $wpdb;
 636  
 637          $user_id_sql = $pag_sql = $hidden_sql = $filter_sql = '';
 638  
 639          $user_id_sql = $wpdb->prepare( 'm.user_id = %d', $user_id );
 640  
 641          if ( !empty( $limit ) && !empty( $page ) )
 642              $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
 643  
 644          if ( !empty( $filter ) ) {
 645              $search_terms_like = '%' . bp_esc_like( $filter ) . '%';
 646              $filter_sql = $wpdb->prepare( " AND ( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like );
 647          }
 648  
 649          if ( $user_id != bp_loggedin_user_id() )
 650              $hidden_sql = " AND g.status != 'hidden'";
 651  
 652          $bp = buddypress();
 653  
 654          $paged_groups = $wpdb->get_results( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count'{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_confirmed = 1 AND m.is_banned = 0 AND m.is_admin = 1 ORDER BY m.date_modified ASC {$pag_sql}" );
 655          $total_groups = $wpdb->get_var( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_confirmed = 1 AND m.is_banned = 0 AND m.is_admin = 1 ORDER BY date_modified ASC" );
 656  
 657          return array( 'groups' => $paged_groups, 'total' => $total_groups );
 658      }
 659  
 660      /**
 661       * Get the IDs of the groups of which a specified user is a moderator.
 662       *
 663       * @since 1.6.0
 664       *
 665       * @param int         $user_id ID of the user.
 666       * @param int|bool    $limit   Optional. Max number of results to return.
 667       *                             Default: false (no limit).
 668       * @param int|bool    $page    Optional. Page offset of results to return.
 669       *                             Default: false (no limit).
 670       * @param string|bool $filter  Optional. Limit results to groups whose name or
 671       *                             description field matches search terms.
 672       * @return array {
 673       *     @type array $groups Array of groups returned by paginated query.
 674       *     @type int   $total  Count of groups matching query.
 675       * }
 676       */
 677  	public static function get_is_mod_of( $user_id, $limit = false, $page = false, $filter = false ) {
 678          global $wpdb;
 679  
 680          $user_id_sql = $pag_sql = $hidden_sql = $filter_sql = '';
 681  
 682          $user_id_sql = $wpdb->prepare( 'm.user_id = %d', $user_id );
 683  
 684          if ( !empty( $limit ) && !empty( $page ) )
 685              $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
 686  
 687          if ( !empty( $filter ) ) {
 688              $search_terms_like = '%' . bp_esc_like( $filter ) . '%';
 689              $filter_sql = $wpdb->prepare( " AND ( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like );
 690          }
 691  
 692          if ( $user_id != bp_loggedin_user_id() )
 693              $hidden_sql = " AND g.status != 'hidden'";
 694  
 695          $bp = buddypress();
 696  
 697          $paged_groups = $wpdb->get_results( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count'{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_confirmed = 1 AND m.is_banned = 0 AND m.is_mod = 1 ORDER BY m.date_modified ASC {$pag_sql}" );
 698          $total_groups = $wpdb->get_var( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_confirmed = 1 AND m.is_banned = 0 AND m.is_mod = 1 ORDER BY date_modified ASC" );
 699  
 700          return array( 'groups' => $paged_groups, 'total' => $total_groups );
 701      }
 702  
 703      /**
 704       * Get the groups of which a specified user is banned from.
 705       *
 706       * @since 2.4.0
 707       *
 708       * @param int         $user_id ID of the user.
 709       * @param int|bool    $limit   Optional. Max number of results to return.
 710       *                             Default: false (no limit).
 711       * @param int|bool    $page    Optional. Page offset of results to return.
 712       *                             Default: false (no limit).
 713       * @param string|bool $filter  Optional. Limit results to groups whose name or
 714       *                             description field matches search terms.
 715       * @return array {
 716       *     @type array $groups Array of groups returned by paginated query.
 717       *     @type int   $total  Count of groups matching query.
 718       * }
 719       */
 720  	public static function get_is_banned_of( $user_id, $limit = false, $page = false, $filter = false ) {
 721          global $wpdb;
 722  
 723          $bp = buddypress();
 724  
 725          $user_id_sql = $pag_sql = $hidden_sql = $filter_sql = '';
 726          $user_id_sql = $wpdb->prepare( 'm.user_id = %d', $user_id );
 727  
 728          if ( $limit && $page ) {
 729              $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit ), intval( $limit ) );
 730          }
 731  
 732          if ( $filter ) {
 733              $search_terms_like = '%' . bp_esc_like( $filter ) . '%';
 734              $filter_sql        = $wpdb->prepare( " AND ( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like );
 735          }
 736  
 737          if ( $user_id !== bp_loggedin_user_id() && ! bp_current_user_can( 'bp_moderate' ) ) {
 738              $hidden_sql = " AND g.status != 'hidden'";
 739          }
 740  
 741          $paged_groups = $wpdb->get_results( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count'{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_banned = 1  ORDER BY m.date_modified ASC {$pag_sql}" );
 742          $total_groups = $wpdb->get_var( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_banned = 1 ORDER BY date_modified ASC" );
 743  
 744          return array( 'groups' => $paged_groups, 'total' => $total_groups );
 745      }
 746  
 747      /**
 748       * Get the count of groups of which the specified user is a member.
 749       *
 750       * @since 1.6.0
 751       *
 752       * @param int $user_id Optional. Default: ID of the displayed user.
 753       * @return int Group count.
 754       */
 755  	public static function total_group_count( $user_id = 0 ) {
 756          global $wpdb;
 757  
 758          if ( empty( $user_id ) )
 759              $user_id = bp_displayed_user_id();
 760  
 761          $bp = buddypress();
 762  
 763          if ( $user_id != bp_loggedin_user_id() && !bp_current_user_can( 'bp_moderate' ) ) {
 764              return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id AND g.status != 'hidden' AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0", $user_id ) );
 765          } else {
 766              return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0", $user_id ) );
 767          }
 768      }
 769  
 770      /**
 771       * Get group objects for groups that a user is currently invited to.
 772       *
 773       * @since 1.6.0
 774       *
 775       * @param int               $user_id ID of the invitee.
 776       * @param int|bool          $limit   Optional. Max number of results to return.
 777       *                                   Default: false (no limit).
 778       * @param int|bool          $page    Optional. Page offset of results to return.
 779       *                                   Default: false (no limit).
 780       * @param string|array|bool $exclude Optional. Array or comma-separated list
 781       *                                   of group IDs to exclude from results.
 782       * @return array {
 783       *     @type array $groups Array of groups returned by paginated query.
 784       *     @type int   $total  Count of groups matching query.
 785       * }
 786       */
 787  	public static function get_invites( $user_id, $limit = false, $page = false, $exclude = false ) {
 788          return groups_get_invites_for_user( $user_id, $limit, $page, $exclude );
 789      }
 790  
 791      /**
 792       * Gets the total group invite count for a user.
 793       *
 794       * @since 2.0.0
 795       *
 796       * @param int $user_id The user ID.
 797       * @return int
 798       */
 799  	public static function get_invite_count_for_user( $user_id = 0 ) {
 800          return groups_get_invite_count_for_user( $user_id );
 801      }
 802  
 803      /**
 804       * Gets memberships of a user for purposes of a personal data export.
 805       *
 806       * @since 4.0.0
 807       *
 808       * @param int $user_id ID of the user.
 809       * @param array $args {
 810       *    Array of optional arguments.
 811       *    @type int    $page     Page of memberships being requested. Default 1.
 812       *    @type int    $per_page Memberships to return per page. Default 20.
 813       *    @type string $type     Membership type being requested. Accepts 'membership',
 814       *                           'pending_request', 'pending_received_invitation',
 815       *                           'pending_sent_invitation'. Default 'membership'.
 816       * }
 817       *
 818       * @return array
 819       */
 820  	public static function get_user_memberships( $user_id, $args = array() ) {
 821          global $wpdb;
 822  
 823          $bp = buddypress();
 824  
 825          $r = array_merge( array(
 826              'page'     => 1,
 827              'per_page' => 20,
 828              'type'     => 'membership',
 829          ), $args );
 830  
 831          $sql = array(
 832              'select' => 'SELECT *',
 833              'from'   => "FROM {$bp->groups->table_name_members}",
 834              'where'  => '',
 835              'limits' => '',
 836          );
 837  
 838          switch ( $r['type'] ) {
 839              case 'pending_request' :
 840                  return groups_get_requests( array(
 841                      'user_id'  => $user_id,
 842                      'page'     => $r['page'],
 843                      'per_page' => $r['per_page'],
 844                  ) );
 845              break;
 846  
 847              case 'pending_received_invitation' :
 848                  return groups_get_invites( array(
 849                      'user_id'  => $user_id,
 850                      'page'     => $r['page'],
 851                      'per_page' => $r['per_page'],
 852                  ) );
 853              break;
 854  
 855              case 'pending_sent_invitation' :
 856                  return groups_get_invites( array(
 857                      'inviter_id'  => $user_id,
 858                      'page'        => $r['page'],
 859                      'per_page'    => $r['per_page'],
 860                  ) );
 861              break;
 862  
 863              case 'membership' :
 864              default :
 865                  $sql['where'] = $wpdb->prepare( "user_id = %d AND is_confirmed = 1", $user_id );
 866              break;
 867          }
 868  
 869          if ( $r['page'] && $r['per_page'] ) {
 870              $sql['limits'] = $wpdb->prepare( "LIMIT %d, %d", ( $r['page'] - 1 ) * $r['per_page'], $r['per_page'] );
 871          }
 872  
 873          $memberships = $wpdb->get_results( "{$sql['select']} {$sql['from']} WHERE {$sql['where']} {$sql['limits']}" );
 874  
 875          foreach ( $memberships as &$membership ) {
 876              $membership->id           = (int) $membership->id;
 877              $membership->group_id     = (int) $membership->group_id;
 878              $membership->user_id      = (int) $membership->user_id;
 879              $membership->inviter_id   = (int) $membership->inviter_id;
 880              $membership->is_admin     = (int) $membership->is_admin;
 881              $membership->is_mod       = (int) $membership->is_mod;
 882              $membership->is_banned    = (int) $membership->is_banned;
 883              $membership->is_confirmed = (int) $membership->is_confirmed;
 884              $membership->invite_sent  = (int) $membership->invite_sent;
 885          }
 886  
 887          return $memberships;
 888      }
 889  
 890      /**
 891       * Check whether a user has an outstanding invitation to a given group.
 892       *
 893       * @since 1.6.0
 894       *
 895       * @param int    $user_id  ID of the potential invitee.
 896       * @param int    $group_id ID of the group.
 897       * @param string $type     If 'sent', results are limited to those invitations
 898       *                         that have actually been sent (non-draft). Default: 'sent'.
 899       * @return int|null The ID of the invitation if found; null if not found.
 900       */
 901  	public static function check_has_invite( $user_id, $group_id, $type = 'sent' ) {
 902          return groups_is_user_invited( $user_id, $group_id, $type );
 903      }
 904  
 905      /**
 906       * Delete an invitation, by specifying user ID and group ID.
 907       *
 908       * @since 1.6.0
 909       *
 910       * @global WPDB $wpdb
 911       *
 912       * @param  int $user_id    ID of the user.
 913       * @param  int $group_id   ID of the group.
 914       * @param  int $inviter_id ID of the inviter. Specify if you want to delete
 915       *                         a specific invite. Leave false if you want to
 916       *                         delete all invites to this group.
 917       * @return int Number of records deleted.
 918       */
 919  	public static function delete_invite( $user_id, $group_id, $inviter_id = false ) {
 920          /**
 921           * Fires before a group invitation is deleted.
 922           *
 923           * @since 2.6.0
 924           * @since 5.0.0 Added $inviter_id
 925           *
 926           * @param int $user_id  ID of the user.
 927           * @param int $group_id ID of the group.
 928           * @param  int $inviter_id ID of the inviter.
 929           */
 930          do_action( 'bp_groups_member_before_delete_invite', $user_id, $group_id, $inviter_id );
 931  
 932          return groups_delete_invite( $user_id, $group_id, $inviter_id );
 933      }
 934  
 935      /**
 936       * Delete an unconfirmed membership request, by user ID and group ID.
 937       *
 938       * @since 1.6.0
 939       *
 940       * @param int $user_id  ID of the user.
 941       * @param int $group_id ID of the group.
 942       * @return int Number of records deleted.
 943       */
 944  	public static function delete_request( $user_id, $group_id ) {
 945          return groups_delete_membership_request( false, $user_id, $group_id );
 946      }
 947  
 948      /**
 949       * Check whether a user is an admin of a given group.
 950       *
 951       * @since 1.6.0
 952       *
 953       * @param int $user_id  ID of the user.
 954       * @param int $group_id ID of the group.
 955       * @return mixed
 956       */
 957  	public static function check_is_admin( $user_id, $group_id ) {
 958          global $wpdb;
 959  
 960          if ( empty( $user_id ) )
 961              return false;
 962  
 963          $bp = buddypress();
 964  
 965          return $wpdb->query( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_admin = 1 AND is_banned = 0", $user_id, $group_id ) );
 966      }
 967  
 968      /**
 969       * Check whether a user is a mod of a given group.
 970       *
 971       * @since 1.6.0
 972       *
 973       * @param int $user_id  ID of the user.
 974       * @param int $group_id ID of the group.
 975       * @return mixed
 976       */
 977  	public static function check_is_mod( $user_id, $group_id ) {
 978          global $wpdb;
 979  
 980          if ( empty( $user_id ) )
 981              return false;
 982  
 983          $bp = buddypress();
 984  
 985          return $wpdb->query( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_mod = 1 AND is_banned = 0", $user_id, $group_id ) );
 986      }
 987  
 988      /**
 989       * Check whether a user is a member of a given group.
 990       *
 991       * @since 1.6.0
 992       *
 993       * @param int $user_id  ID of the user.
 994       * @param int $group_id ID of the group.
 995       * @return mixed
 996       */
 997  	public static function check_is_member( $user_id, $group_id ) {
 998          global $wpdb;
 999  
1000          if ( empty( $user_id ) )
1001              return false;
1002  
1003          $bp = buddypress();
1004  
1005          return $wpdb->query( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 1 AND is_banned = 0", $user_id, $group_id ) );
1006      }
1007  
1008      /**
1009       * Check whether a user is banned from a given group.
1010       *
1011       * @since 1.6.0
1012       *
1013       * @param int $user_id  ID of the user.
1014       * @param int $group_id ID of the group.
1015       * @return int|null int 1 if user is banned; int 0 if user is not banned;
1016       *                  null if user is not part of the group or if group doesn't exist.
1017       */
1018  	public static function check_is_banned( $user_id, $group_id ) {
1019          global $wpdb;
1020  
1021          if ( empty( $user_id ) )
1022              return false;
1023  
1024          $bp = buddypress();
1025  
1026          $query = $wpdb->get_var( $wpdb->prepare( "SELECT is_banned FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $user_id, $group_id ) );
1027  
1028          return is_numeric( $query ) ? (int) $query : $query;
1029      }
1030  
1031      /**
1032       * Is the specified user the creator of the group?
1033       *
1034       * @since 1.2.6
1035       *
1036       * @param int $user_id  ID of the user.
1037       * @param int $group_id ID of the group.
1038       * @return int|null int of group ID if user is the creator; null on failure.
1039       */
1040  	public static function check_is_creator( $user_id, $group_id ) {
1041          global $wpdb;
1042  
1043          if ( empty( $user_id ) )
1044              return false;
1045  
1046          $bp = buddypress();
1047  
1048          $query = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name} WHERE creator_id = %d AND id = %d", $user_id, $group_id ) );
1049  
1050          return is_numeric( $query ) ? (int) $query : $query;
1051      }
1052  
1053      /**
1054       * Check whether a user has an outstanding membership request for a given group.
1055       *
1056       * @since 1.6.0
1057       *
1058       * @param int $user_id  ID of the user.
1059       * @param int $group_id ID of the group.
1060       * @return int Database ID of the membership if found; int 0 on failure.
1061       */
1062  	public static function check_for_membership_request( $user_id, $group_id ) {
1063          return groups_is_user_pending( $user_id, $group_id );
1064      }
1065  
1066      /**
1067       * Get a list of randomly selected IDs of groups that the member belongs to.
1068       *
1069       * @since 1.6.0
1070       *
1071       * @param int $user_id      ID of the user.
1072       * @param int $total_groups Max number of group IDs to return. Default: 5.
1073       * @return array Group IDs.
1074       */
1075  	public static function get_random_groups( $user_id = 0, $total_groups = 5 ) {
1076          global $wpdb;
1077  
1078          $bp = buddypress();
1079  
1080          // If the user is logged in and viewing their random groups, we can show hidden and private groups.
1081          if ( bp_is_my_profile() ) {
1082              return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT group_id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND is_confirmed = 1 AND is_banned = 0 ORDER BY rand() LIMIT %d", $user_id, $total_groups ) ) );
1083          } else {
1084              return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT m.group_id FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id AND g.status != 'hidden' AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0 ORDER BY rand() LIMIT %d", $user_id, $total_groups ) ) );
1085          }
1086      }
1087  
1088      /**
1089       * Get the IDs of all a given group's members.
1090       *
1091       * @since 1.6.0
1092       *
1093       * @param int $group_id ID of the group.
1094       * @return array IDs of all group members.
1095       */
1096  	public static function get_group_member_ids( $group_id ) {
1097          global $wpdb;
1098  
1099          $bp = buddypress();
1100  
1101          return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 1 AND is_banned = 0", $group_id ) ) );
1102      }
1103  
1104      /**
1105       * Get a list of all a given group's admins.
1106       *
1107       * @since 1.6.0
1108       *
1109       * @param int $group_id ID of the group.
1110       * @return array Info about group admins (user_id + date_modified).
1111       */
1112  	public static function get_group_administrator_ids( $group_id ) {
1113          global $wpdb;
1114  
1115          if ( empty( $group_id ) ) {
1116              return array();
1117          }
1118  
1119          $group_admins = wp_cache_get( $group_id, 'bp_group_admins' );
1120  
1121          if ( false === $group_admins ) {
1122              self::prime_group_admins_mods_cache( array( $group_id ) );
1123              $group_admins = wp_cache_get( $group_id, 'bp_group_admins' );
1124          }
1125  
1126          if ( false === $group_admins ) {
1127              // The wp_cache_get is still coming up empty. Return an empty array.
1128              $group_admins = array();
1129          } else {
1130              // Cast the user_id property as an integer.
1131              foreach ( (array) $group_admins as $key => $data ) {
1132                  $group_admins[ $key ]->user_id = (int) $group_admins[ $key ]->user_id;
1133              }
1134          }
1135  
1136          return $group_admins;
1137      }
1138  
1139      /**
1140       * Prime the bp_group_admins cache for one or more groups.
1141       *
1142       * @since 2.7.0
1143       *
1144       * @param array $group_ids IDs of the groups.
1145       * @return bool True on success.
1146       */
1147  	public static function prime_group_admins_mods_cache( $group_ids ) {
1148          global $wpdb;
1149  
1150          $uncached = bp_get_non_cached_ids( $group_ids, 'bp_group_admins' );
1151  
1152          if ( $uncached ) {
1153              $bp = buddypress();
1154              $uncached_sql = implode( ',', array_map( 'intval', $uncached ) );
1155              $group_admin_mods = $wpdb->get_results( "SELECT user_id, group_id, date_modified, is_admin, is_mod FROM {$bp->groups->table_name_members} WHERE group_id IN ({$uncached_sql}) AND ( is_admin = 1 OR is_mod = 1 ) AND is_banned = 0" );
1156  
1157              $admins = $mods = array();
1158              if ( $group_admin_mods ) {
1159                  foreach ( $group_admin_mods as $group_admin_mod ) {
1160                      $obj = new stdClass();
1161                      $obj->user_id = $group_admin_mod->user_id;
1162                      $obj->date_modified = $group_admin_mod->date_modified;
1163  
1164                      if ( $group_admin_mod->is_admin ) {
1165                          $admins[ $group_admin_mod->group_id ][] = $obj;
1166                      } else {
1167                          $mods[ $group_admin_mod->group_id ][] = $obj;
1168                      }
1169                  }
1170              }
1171  
1172              // Prime cache for all groups, even those with no matches.
1173              foreach ( $uncached as $group_id ) {
1174                  $group_admins = isset( $admins[ $group_id ] ) ? $admins[ $group_id ] : array();
1175                  wp_cache_set( $group_id, $group_admins, 'bp_group_admins' );
1176  
1177                  $group_mods = isset( $mods[ $group_id ] ) ? $mods[ $group_id ] : array();
1178                  wp_cache_set( $group_id, $group_mods, 'bp_group_mods' );
1179              }
1180          }
1181      }
1182  
1183      /**
1184       * Get a list of all a given group's moderators.
1185       *
1186       * @since 1.6.0
1187       *
1188       * @param int $group_id ID of the group.
1189       * @return array Info about group mods (user_id + date_modified).
1190       */
1191  	public static function get_group_moderator_ids( $group_id ) {
1192  
1193          if ( empty( $group_id ) ) {
1194              return array();
1195          }
1196  
1197          $group_mods = wp_cache_get( $group_id, 'bp_group_mods' );
1198  
1199          if ( false === $group_mods ) {
1200              self::prime_group_admins_mods_cache( array( $group_id ) );
1201              $group_mods = wp_cache_get( $group_id, 'bp_group_mods' );
1202          }
1203  
1204          if ( false === $group_mods ) {
1205              // The wp_cache_get is still coming up empty. Return an empty array.
1206              $group_mods = array();
1207          } else {
1208              // Cast the user_id property as an integer.
1209              foreach ( (array) $group_mods as $key => $data ) {
1210                  $group_mods[ $key ]->user_id = (int) $group_mods[ $key ]->user_id;
1211              }
1212          }
1213  
1214          return $group_mods;
1215      }
1216  
1217      /**
1218       * Get group membership objects by ID (or an array of IDs).
1219       *
1220       * @since 2.6.0
1221       *
1222       * @param int|string|array $membership_ids Single membership ID or comma-separated/array list of membership IDs.
1223       * @return array
1224       */
1225  	public static function get_memberships_by_id( $membership_ids ) {
1226          global $wpdb;
1227  
1228          $bp = buddypress();
1229  
1230          $membership_ids = implode( ',', wp_parse_id_list( $membership_ids ) );
1231          return $wpdb->get_results( "SELECT * FROM {$bp->groups->table_name_members} WHERE id IN ({$membership_ids})" );
1232      }
1233  
1234      /**
1235       * Get the IDs users with outstanding membership requests to the group.
1236       *
1237       * @since 1.6.0
1238       *
1239       * @param int $group_id ID of the group.
1240       * @return array IDs of users with outstanding membership requests.
1241       */
1242  	public static function get_all_membership_request_user_ids( $group_id ) {
1243          return groups_get_membership_requested_user_ids( $group_id );
1244      }
1245  
1246      /**
1247       * Get members of a group.
1248       *
1249       * @deprecated 1.6.0
1250       *
1251       * @param int        $group_id            ID of the group being queried for.
1252       * @param bool|int   $limit               Max amount to return.
1253       * @param bool|int   $page                Pagination value.
1254       * @param bool       $exclude_admins_mods Whether or not to exclude admins and moderators.
1255       * @param bool       $exclude_banned      Whether or not to exclude banned members.
1256       * @param bool|array $exclude             Array of user IDs to exclude.
1257       * @return false|array
1258       */
1259  	public static function get_all_for_group( $group_id, $limit = false, $page = false, $exclude_admins_mods = true, $exclude_banned = true, $exclude = false ) {
1260          global $wpdb;
1261  
1262          _deprecated_function( __METHOD__, '1.8', 'BP_Group_Member_Query' );
1263  
1264          $pag_sql = '';
1265          if ( !empty( $limit ) && !empty( $page ) )
1266              $pag_sql = $wpdb->prepare( "LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
1267  
1268          $exclude_admins_sql = '';
1269          if ( !empty( $exclude_admins_mods ) )
1270              $exclude_admins_sql = "AND is_admin = 0 AND is_mod = 0";
1271  
1272          $banned_sql = '';
1273          if ( !empty( $exclude_banned ) )
1274              $banned_sql = " AND is_banned = 0";
1275  
1276          $exclude_sql = '';
1277          if ( !empty( $exclude ) ) {
1278              $exclude     = implode( ',', wp_parse_id_list( $exclude ) );
1279              $exclude_sql = " AND m.user_id NOT IN ({$exclude})";
1280          }
1281  
1282          $bp = buddypress();
1283  
1284          if ( bp_is_active( 'xprofile' ) ) {
1285  
1286              /**
1287               * Filters the SQL prepared statement used to fetch group members.
1288               *
1289               * @since 1.5.0
1290               *
1291               * @param string $value SQL prepared statement for fetching group members.
1292               */
1293              $members = $wpdb->get_results( apply_filters( 'bp_group_members_user_join_filter', $wpdb->prepare( "SELECT m.user_id, m.date_modified, m.is_banned, u.user_login, u.user_nicename, u.user_email, pd.value as display_name FROM {$bp->groups->table_name_members} m, {$wpdb->users} u, {$bp->profile->table_name_data} pd WHERE u.ID = m.user_id AND u.ID = pd.user_id AND pd.field_id = 1 AND group_id = %d AND is_confirmed = 1 {$banned_sql} {$exclude_admins_sql} {$exclude_sql} ORDER BY m.date_modified DESC {$pag_sql}", $group_id ) ) );
1294          } else {
1295  
1296              /** This filter is documented in bp-groups/bp-groups-classes */
1297              $members = $wpdb->get_results( apply_filters( 'bp_group_members_user_join_filter', $wpdb->prepare( "SELECT m.user_id, m.date_modified, m.is_banned, u.user_login, u.user_nicename, u.user_email, u.display_name FROM {$bp->groups->table_name_members} m, {$wpdb->users} u WHERE u.ID = m.user_id AND group_id = %d AND is_confirmed = 1 {$banned_sql} {$exclude_admins_sql} {$exclude_sql} ORDER BY m.date_modified DESC {$pag_sql}", $group_id ) ) );
1298          }
1299  
1300          if ( empty( $members ) ) {
1301              return false;
1302          }
1303  
1304          if ( empty( $pag_sql ) ) {
1305              $total_member_count = count( $members );
1306          } else {
1307  
1308              /**
1309               * Filters the SQL prepared statement used to fetch group members total count.
1310               *
1311               * @since 1.5.0
1312               *
1313               * @param string $value SQL prepared statement for fetching group member count.
1314               */
1315              $total_member_count = $wpdb->get_var( apply_filters( 'bp_group_members_count_user_join_filter', $wpdb->prepare( "SELECT COUNT(user_id) FROM {$bp->groups->table_name_members} m WHERE group_id = %d AND is_confirmed = 1 {$banned_sql} {$exclude_admins_sql} {$exclude_sql}", $group_id ) ) );
1316          }
1317  
1318          // Fetch whether or not the user is a friend.
1319          foreach ( (array) $members as $user )
1320              $user_ids[] = $user->user_id;
1321  
1322          $user_ids = implode( ',', wp_parse_id_list( $user_ids ) );
1323  
1324          if ( bp_is_active( 'friends' ) ) {
1325              $friend_status = $wpdb->get_results( $wpdb->prepare( "SELECT initiator_user_id, friend_user_id, is_confirmed FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d AND friend_user_id IN ( {$user_ids} ) ) OR (initiator_user_id IN ( {$user_ids} ) AND friend_user_id = %d )", bp_loggedin_user_id(), bp_loggedin_user_id() ) );
1326              for ( $i = 0, $count = count( $members ); $i < $count; ++$i ) {
1327                  foreach ( (array) $friend_status as $status ) {
1328                      if ( $status->initiator_user_id == $members[$i]->user_id || $status->friend_user_id == $members[$i]->user_id ) {
1329                          $members[$i]->is_friend = $status->is_confirmed;
1330                      }
1331                  }
1332              }
1333          }
1334  
1335          return array( 'members' => $members, 'count' => $total_member_count );
1336      }
1337  
1338      /**
1339       * Get all membership IDs for a user.
1340       *
1341       * @since 2.6.0
1342       *
1343       * @param int $user_id ID of the user.
1344       * @return array
1345       */
1346  	public static function get_membership_ids_for_user( $user_id ) {
1347          global $wpdb;
1348  
1349          $bp = buddypress();
1350  
1351          $group_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d ORDER BY id ASC", $user_id ) );
1352  
1353          return $group_ids;
1354      }
1355  
1356      /**
1357       * Delete all memberships for a given group.
1358       *
1359       * @since 1.6.0
1360       *
1361       * @param int $group_id ID of the group.
1362       * @return int Number of records deleted.
1363       */
1364  	public static function delete_all( $group_id ) {
1365          global $wpdb;
1366  
1367          $bp = buddypress();
1368  
1369          return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE group_id = %d", $group_id ) );
1370      }
1371  
1372      /**
1373       * Delete all group membership information for the specified user.
1374       *
1375       * In cases where the user is the sole member of a group, a site administrator is
1376       * assigned to be the group's administrator. Unhook `groups_remove_data_for_user()`
1377       * to modify this behavior.
1378       *
1379       * @since 1.0.0
1380       * @since 4.0.0 The method behavior was changed so that single-member groups are not deleted.
1381       *
1382       * @param int $user_id ID of the user.
1383       * @return bool
1384       */
1385  	public static function delete_all_for_user( $user_id ) {
1386          $group_ids = BP_Groups_Member::get_group_ids( $user_id );
1387  
1388          foreach ( $group_ids['groups'] as $group_id ) {
1389              if ( groups_is_user_admin( $user_id, $group_id ) ) {
1390                  // If the user is a sole group admin, install a site admin as their replacement.
1391                  if ( count( groups_get_group_admins( $group_id ) ) < 2 ) {
1392                      $admin = get_users( array(
1393                          'blog_id' => bp_get_root_blog_id(),
1394                          'fields'  => 'id',
1395                          'number'  => 1,
1396                          'orderby' => 'ID',
1397                          'role'    => 'administrator',
1398                      ) );
1399  
1400                      if ( ! empty( $admin ) ) {
1401                          groups_join_group( $group_id, $admin[0] );
1402  
1403                          $member = new BP_Groups_Member( $admin[0], $group_id );
1404                          $member->promote( 'admin' );
1405                      }
1406                  }
1407              }
1408  
1409              BP_Groups_Member::delete( $user_id, $group_id );
1410          }
1411  
1412          return true;
1413      }
1414  }


Generated: Sat Apr 27 01:00:55 2024 Cross-referenced by PHPXref 0.7.1