[ 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       * @return bool True on success, false on failure.
 474       */
 475  	public static function refresh_total_group_count_for_user( $user_id ) {
 476          return bp_update_user_meta( $user_id, 'total_group_count', (int) self::total_group_count( $user_id ) );
 477      }
 478  
 479      /**
 480       * Refresh the total_member_count for a group.
 481       *
 482       * @since 1.8.0
 483       *
 484       * @param int $group_id ID of the group.
 485       * @return bool|int True on success, false on failure.
 486       */
 487  	public static function refresh_total_member_count_for_group( $group_id ) {
 488          return groups_update_groupmeta( $group_id, 'total_member_count', (int) BP_Groups_Group::get_total_member_count( $group_id ) );
 489      }
 490  
 491      /**
 492       * Delete a membership, based on user + group IDs.
 493       *
 494       * @since 1.6.0
 495       *
 496       * @param int $user_id  ID of the user.
 497       * @param int $group_id ID of the group.
 498       * @return True on success, false on failure.
 499       */
 500  	public static function delete( $user_id, $group_id ) {
 501          global $wpdb;
 502  
 503          /**
 504           * Fires before a group membership is deleted.
 505           *
 506           * @since 2.3.0
 507           *
 508           * @param int $user_id  ID of the user.
 509           * @param int $group_id ID of the group.
 510           */
 511          do_action( 'bp_groups_member_before_delete', $user_id, $group_id );
 512  
 513          $bp = buddypress();
 514          $remove = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $user_id, $group_id ) );
 515  
 516          // Update the user's group count.
 517          self::refresh_total_group_count_for_user( $user_id );
 518  
 519          // Update the group's member count.
 520          self::refresh_total_member_count_for_group( $group_id );
 521  
 522          /**
 523           * Fires after a member is removed from a group.
 524           *
 525           * @since 2.3.0
 526           *
 527           * @param int $user_id  ID of the user.
 528           * @param int $group_id ID of the group.
 529           */
 530          do_action( 'bp_groups_member_after_delete', $user_id, $group_id );
 531  
 532          return $remove;
 533      }
 534  
 535      /**
 536       * Get the IDs of the groups of which a specified user is a member.
 537       *
 538       * @since 1.6.0
 539       *
 540       * @param int      $user_id ID of the user.
 541       * @param int|bool $limit   Optional. Max number of results to return.
 542       *                          Default: false (no limit).
 543       * @param int|bool $page    Optional. Page offset of results to return.
 544       *                          Default: false (no limit).
 545       * @return array {
 546       *     @type array $groups Array of groups returned by paginated query.
 547       *     @type int   $total  Count of groups matching query.
 548       * }
 549       */
 550  	public static function get_group_ids( $user_id, $limit = false, $page = false ) {
 551          global $wpdb;
 552  
 553          $pag_sql = '';
 554          if ( !empty( $limit ) && !empty( $page ) )
 555              $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
 556  
 557          $bp = buddypress();
 558  
 559          // If the user is logged in and viewing their own groups, we can show hidden and private groups.
 560          if ( $user_id != bp_loggedin_user_id() ) {
 561              $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 );
 562              $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 ) );
 563          } else {
 564              $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 );
 565              $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 ) );
 566          }
 567  
 568          $groups = $wpdb->get_col( $group_sql );
 569  
 570          return array( 'groups' => $groups, 'total' => (int) $total_groups );
 571      }
 572  
 573      /**
 574       * Get the IDs of the groups of which a specified user is a member, sorted by the date joined.
 575       *
 576       * @since 1.6.0
 577       *
 578       * @param int         $user_id ID of the user.
 579       * @param int|bool    $limit   Optional. Max number of results to return.
 580       *                             Default: false (no limit).
 581       * @param int|bool    $page    Optional. Page offset of results to return.
 582       *                             Default: false (no limit).
 583       * @param string|bool $filter  Optional. Limit results to groups whose name or
 584       *                             description field matches search terms.
 585       * @return array {
 586       *     @type array $groups Array of groups returned by paginated query.
 587       *     @type int   $total  Count of groups matching query.
 588       * }
 589       */
 590  	public static function get_recently_joined( $user_id, $limit = false, $page = false, $filter = false ) {
 591          global $wpdb;
 592  
 593          $user_id_sql = $pag_sql = $hidden_sql = $filter_sql = '';
 594  
 595          $user_id_sql = $wpdb->prepare( 'm.user_id = %d', $user_id );
 596  
 597          if ( !empty( $limit ) && !empty( $page ) )
 598              $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
 599  
 600          if ( !empty( $filter ) ) {
 601              $search_terms_like = '%' . bp_esc_like( $filter ) . '%';
 602              $filter_sql = $wpdb->prepare( " AND ( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like );
 603          }
 604  
 605          if ( $user_id != bp_loggedin_user_id() )
 606              $hidden_sql = " AND g.status != 'hidden'";
 607  
 608          $bp = buddypress();
 609  
 610          $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}" );
 611          $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" );
 612  
 613          return array( 'groups' => $paged_groups, 'total' => $total_groups );
 614      }
 615  
 616      /**
 617       * Get the IDs of the groups of which a specified user is an admin.
 618       *
 619       * @since 1.6.0
 620       *
 621       * @param int         $user_id ID of the user.
 622       * @param int|bool    $limit   Optional. Max number of results to return.
 623       *                             Default: false (no limit).
 624       * @param int|bool    $page    Optional. Page offset of results to return.
 625       *                             Default: false (no limit).
 626       * @param string|bool $filter  Optional. Limit results to groups whose name or
 627       *                             description field matches search terms.
 628       * @return array {
 629       *     @type array $groups Array of groups returned by paginated query.
 630       *     @type int   $total  Count of groups matching query.
 631       * }
 632       */
 633  	public static function get_is_admin_of( $user_id, $limit = false, $page = false, $filter = false ) {
 634          global $wpdb;
 635  
 636          $user_id_sql = $pag_sql = $hidden_sql = $filter_sql = '';
 637  
 638          $user_id_sql = $wpdb->prepare( 'm.user_id = %d', $user_id );
 639  
 640          if ( !empty( $limit ) && !empty( $page ) )
 641              $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
 642  
 643          if ( !empty( $filter ) ) {
 644              $search_terms_like = '%' . bp_esc_like( $filter ) . '%';
 645              $filter_sql = $wpdb->prepare( " AND ( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like );
 646          }
 647  
 648          if ( $user_id != bp_loggedin_user_id() )
 649              $hidden_sql = " AND g.status != 'hidden'";
 650  
 651          $bp = buddypress();
 652  
 653          $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}" );
 654          $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" );
 655  
 656          return array( 'groups' => $paged_groups, 'total' => $total_groups );
 657      }
 658  
 659      /**
 660       * Get the IDs of the groups of which a specified user is a moderator.
 661       *
 662       * @since 1.6.0
 663       *
 664       * @param int         $user_id ID of the user.
 665       * @param int|bool    $limit   Optional. Max number of results to return.
 666       *                             Default: false (no limit).
 667       * @param int|bool    $page    Optional. Page offset of results to return.
 668       *                             Default: false (no limit).
 669       * @param string|bool $filter  Optional. Limit results to groups whose name or
 670       *                             description field matches search terms.
 671       * @return array {
 672       *     @type array $groups Array of groups returned by paginated query.
 673       *     @type int   $total  Count of groups matching query.
 674       * }
 675       */
 676  	public static function get_is_mod_of( $user_id, $limit = false, $page = false, $filter = false ) {
 677          global $wpdb;
 678  
 679          $user_id_sql = $pag_sql = $hidden_sql = $filter_sql = '';
 680  
 681          $user_id_sql = $wpdb->prepare( 'm.user_id = %d', $user_id );
 682  
 683          if ( !empty( $limit ) && !empty( $page ) )
 684              $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
 685  
 686          if ( !empty( $filter ) ) {
 687              $search_terms_like = '%' . bp_esc_like( $filter ) . '%';
 688              $filter_sql = $wpdb->prepare( " AND ( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like );
 689          }
 690  
 691          if ( $user_id != bp_loggedin_user_id() )
 692              $hidden_sql = " AND g.status != 'hidden'";
 693  
 694          $bp = buddypress();
 695  
 696          $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}" );
 697          $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" );
 698  
 699          return array( 'groups' => $paged_groups, 'total' => $total_groups );
 700      }
 701  
 702      /**
 703       * Get the groups of which a specified user is banned from.
 704       *
 705       * @since 2.4.0
 706       *
 707       * @param int         $user_id ID of the user.
 708       * @param int|bool    $limit   Optional. Max number of results to return.
 709       *                             Default: false (no limit).
 710       * @param int|bool    $page    Optional. Page offset of results to return.
 711       *                             Default: false (no limit).
 712       * @param string|bool $filter  Optional. Limit results to groups whose name or
 713       *                             description field matches search terms.
 714       * @return array {
 715       *     @type array $groups Array of groups returned by paginated query.
 716       *     @type int   $total  Count of groups matching query.
 717       * }
 718       */
 719  	public static function get_is_banned_of( $user_id, $limit = false, $page = false, $filter = false ) {
 720          global $wpdb;
 721  
 722          $bp = buddypress();
 723  
 724          $user_id_sql = $pag_sql = $hidden_sql = $filter_sql = '';
 725          $user_id_sql = $wpdb->prepare( 'm.user_id = %d', $user_id );
 726  
 727          if ( $limit && $page ) {
 728              $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit ), intval( $limit ) );
 729          }
 730  
 731          if ( $filter ) {
 732              $search_terms_like = '%' . bp_esc_like( $filter ) . '%';
 733              $filter_sql        = $wpdb->prepare( " AND ( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like );
 734          }
 735  
 736          if ( $user_id !== bp_loggedin_user_id() && ! bp_current_user_can( 'bp_moderate' ) ) {
 737              $hidden_sql = " AND g.status != 'hidden'";
 738          }
 739  
 740          $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}" );
 741          $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" );
 742  
 743          return array( 'groups' => $paged_groups, 'total' => $total_groups );
 744      }
 745  
 746      /**
 747       * Get the count of groups of which the specified user is a member.
 748       *
 749       * @since 1.6.0
 750       *
 751       * @param int $user_id Optional. Default: ID of the displayed user.
 752       * @return int Group count.
 753       */
 754  	public static function total_group_count( $user_id = 0 ) {
 755          global $wpdb;
 756  
 757          if ( empty( $user_id ) )
 758              $user_id = bp_displayed_user_id();
 759  
 760          $bp = buddypress();
 761  
 762          if ( $user_id != bp_loggedin_user_id() && !bp_current_user_can( 'bp_moderate' ) ) {
 763              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 ) );
 764          } else {
 765              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 ) );
 766          }
 767      }
 768  
 769      /**
 770       * Get a user's outstanding group invitations.
 771       *
 772       * @since 1.6.0
 773       *
 774       * @param int               $user_id ID of the invitee.
 775       * @param int|bool          $limit   Optional. Max number of results to return.
 776       *                                   Default: false (no limit).
 777       * @param int|bool          $page    Optional. Page offset of results to return.
 778       *                                   Default: false (no limit).
 779       * @param string|array|bool $exclude Optional. Array or comma-separated list
 780       *                                   of group IDs to exclude from results.
 781       * @return array {
 782       *     @type array $groups Array of groups returned by paginated query.
 783       *     @type int   $total  Count of groups matching query.
 784       * }
 785       */
 786  	public static function get_invites( $user_id, $limit = false, $page = false, $exclude = false ) {
 787          global $wpdb;
 788  
 789          $pag_sql = ( !empty( $limit ) && !empty( $page ) ) ? $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ) : '';
 790  
 791          if ( !empty( $exclude ) ) {
 792              $exclude     = implode( ',', wp_parse_id_list( $exclude ) );
 793              $exclude_sql = " AND g.id NOT IN ({$exclude})";
 794          } else {
 795              $exclude_sql = '';
 796          }
 797  
 798          $bp = buddypress();
 799  
 800          $paged_groups = $wpdb->get_results( $wpdb->prepare( "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' AND m.is_confirmed = 0 AND m.inviter_id != 0 AND m.invite_sent = 1 AND m.user_id = %d {$exclude_sql} ORDER BY m.date_modified ASC {$pag_sql}", $user_id ) );
 801  
 802          return array( 'groups' => $paged_groups, 'total' => self::get_invite_count_for_user( $user_id ) );
 803      }
 804  
 805      /**
 806       * Gets the total group invite count for a user.
 807       *
 808       * @since 2.0.0
 809       *
 810       * @param int $user_id The user ID.
 811       * @return int
 812       */
 813  	public static function get_invite_count_for_user( $user_id = 0 ) {
 814          global $wpdb;
 815  
 816          $bp = buddypress();
 817  
 818          $count = wp_cache_get( $user_id, 'bp_group_invite_count' );
 819  
 820          if ( false === $count ) {
 821              $count = (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.is_confirmed = 0 AND m.inviter_id != 0 AND m.invite_sent = 1 AND m.user_id = %d", $user_id ) );
 822              wp_cache_set( $user_id, $count, 'bp_group_invite_count' );
 823          }
 824  
 825          return $count;
 826      }
 827  
 828      /**
 829       * Gets memberships of a user for purposes of a personal data export.
 830       *
 831       * @since 4.0.0
 832       *
 833       * @param int $user_id ID of the user.
 834       * @param array $args {
 835       *    Array of optional arguments.
 836       *    @type int    $page     Page of memberships being requested. Default 1.
 837       *    @type int    $per_page Memberships to return per page. Default 20.
 838       *    @type string $type     Membership type being requested. Accepts 'membership',
 839       *                           'pending_request', 'pending_received_invitation',
 840       *                           'pending_sent_invitation'. Default 'membership'.
 841       * }
 842       *
 843       * @return array
 844       */
 845  	public static function get_user_memberships( $user_id, $args = array() ) {
 846          global $wpdb;
 847  
 848          $bp = buddypress();
 849  
 850          $r = array_merge( array(
 851              'page'     => 1,
 852              'per_page' => 20,
 853              'type'     => 'membership',
 854          ), $args );
 855  
 856          $sql = array(
 857              'select' => 'SELECT *',
 858              'from'   => "FROM {$bp->groups->table_name_members}",
 859              'where'  => '',
 860              'limits' => '',
 861          );
 862  
 863          switch ( $r['type'] ) {
 864              case 'pending_request' :
 865                  $sql['where'] = $wpdb->prepare( "user_id = %d AND is_confirmed = 0 AND inviter_id = 0", $user_id );
 866              break;
 867  
 868              case 'pending_received_invitation' :
 869                  $sql['where'] = $wpdb->prepare( "user_id = %d AND is_confirmed = 0 AND inviter_id != 0", $user_id );
 870              break;
 871  
 872              case 'pending_sent_invitation' :
 873                  $sql['where'] = $wpdb->prepare( "inviter_id = %d AND is_confirmed = 0", $user_id );
 874              break;
 875  
 876              case 'membership' :
 877              default :
 878                  $sql['where'] = $wpdb->prepare( "user_id = %d AND is_confirmed = 1", $user_id );
 879              break;
 880          }
 881  
 882          if ( $r['page'] && $r['per_page'] ) {
 883              $sql['limits'] = $wpdb->prepare( "LIMIT %d, %d", ( $r['page'] - 1 ) * $r['per_page'], $r['per_page'] );
 884          }
 885  
 886          $memberships = $wpdb->get_results( "{$sql['select']} {$sql['from']} WHERE {$sql['where']} {$sql['limits']}" );
 887  
 888          foreach ( $memberships as &$membership ) {
 889              $membership->id           = (int) $membership->id;
 890              $membership->group_id     = (int) $membership->group_id;
 891              $membership->user_id      = (int) $membership->user_id;
 892              $membership->inviter_id   = (int) $membership->inviter_id;
 893              $membership->is_admin     = (int) $membership->is_admin;
 894              $membership->is_mod       = (int) $membership->is_mod;
 895              $membership->is_banned    = (int) $membership->is_banned;
 896              $membership->is_confirmed = (int) $membership->is_confirmed;
 897              $membership->invite_sent  = (int) $membership->invite_sent;
 898          }
 899  
 900          return $memberships;
 901      }
 902  
 903      /**
 904       * Check whether a user has an outstanding invitation to a given group.
 905       *
 906       * @since 1.6.0
 907       *
 908       * @param int    $user_id  ID of the potential invitee.
 909       * @param int    $group_id ID of the group.
 910       * @param string $type     If 'sent', results are limited to those invitations
 911       *                         that have actually been sent (non-draft). Default: 'sent'.
 912       * @return int|null The ID of the invitation if found; null if not found.
 913       */
 914  	public static function check_has_invite( $user_id, $group_id, $type = 'sent' ) {
 915          global $wpdb;
 916  
 917          if ( empty( $user_id ) )
 918              return false;
 919  
 920          $bp  = buddypress();
 921          $sql = "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 0 AND inviter_id != 0";
 922  
 923          if ( 'sent' == $type )
 924              $sql .= " AND invite_sent = 1";
 925  
 926          $query = $wpdb->get_var( $wpdb->prepare( $sql, $user_id, $group_id ) );
 927  
 928          return is_numeric( $query ) ? (int) $query : $query;
 929      }
 930  
 931      /**
 932       * Delete an invitation, by specifying user ID and group ID.
 933       *
 934       * @since 1.6.0
 935       *
 936       * @global WPDB $wpdb
 937       *
 938       * @param  int $user_id  ID of the user.
 939       * @param  int $group_id ID of the group.
 940       * @return int Number of records deleted.
 941       */
 942  	public static function delete_invite( $user_id, $group_id ) {
 943          global $wpdb;
 944  
 945          if ( empty( $user_id ) ) {
 946              return false;
 947          }
 948  
 949          /**
 950           * Fires before a group invitation is deleted.
 951           *
 952           * @since 2.6.0
 953           *
 954           * @param int $user_id  ID of the user.
 955           * @param int $group_id ID of the group.
 956           */
 957          do_action( 'bp_groups_member_before_delete_invite', $user_id, $group_id );
 958  
 959          $table_name = buddypress()->groups->table_name_members;
 960  
 961          $sql = "DELETE FROM {$table_name}
 962                  WHERE user_id = %d
 963                      AND group_id = %d
 964                      AND is_confirmed = 0
 965                      AND inviter_id != 0";
 966  
 967          $prepared = $wpdb->prepare( $sql, $user_id, $group_id );
 968  
 969          return $wpdb->query( $prepared );
 970      }
 971  
 972      /**
 973       * Delete an unconfirmed membership request, by user ID and group ID.
 974       *
 975       * @since 1.6.0
 976       *
 977       * @param int $user_id  ID of the user.
 978       * @param int $group_id ID of the group.
 979       * @return int Number of records deleted.
 980       */
 981  	public static function delete_request( $user_id, $group_id ) {
 982          global $wpdb;
 983  
 984          if ( empty( $user_id ) )
 985              return false;
 986  
 987          $bp = buddypress();
 988  
 989          return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 0 AND inviter_id = 0 AND invite_sent = 0", $user_id, $group_id ) );
 990      }
 991  
 992      /**
 993       * Check whether a user is an admin of a given group.
 994       *
 995       * @since 1.6.0
 996       *
 997       * @param int $user_id  ID of the user.
 998       * @param int $group_id ID of the group.
 999       * @return mixed
1000       */
1001  	public static function check_is_admin( $user_id, $group_id ) {
1002          global $wpdb;
1003  
1004          if ( empty( $user_id ) )
1005              return false;
1006  
1007          $bp = buddypress();
1008  
1009          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 ) );
1010      }
1011  
1012      /**
1013       * Check whether a user is a mod of a given group.
1014       *
1015       * @since 1.6.0
1016       *
1017       * @param int $user_id  ID of the user.
1018       * @param int $group_id ID of the group.
1019       * @return mixed
1020       */
1021  	public static function check_is_mod( $user_id, $group_id ) {
1022          global $wpdb;
1023  
1024          if ( empty( $user_id ) )
1025              return false;
1026  
1027          $bp = buddypress();
1028  
1029          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 ) );
1030      }
1031  
1032      /**
1033       * Check whether a user is a member of a given group.
1034       *
1035       * @since 1.6.0
1036       *
1037       * @param int $user_id  ID of the user.
1038       * @param int $group_id ID of the group.
1039       * @return mixed
1040       */
1041  	public static function check_is_member( $user_id, $group_id ) {
1042          global $wpdb;
1043  
1044          if ( empty( $user_id ) )
1045              return false;
1046  
1047          $bp = buddypress();
1048  
1049          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 ) );
1050      }
1051  
1052      /**
1053       * Check whether a user is banned from a given group.
1054       *
1055       * @since 1.6.0
1056       *
1057       * @param int $user_id  ID of the user.
1058       * @param int $group_id ID of the group.
1059       * @return int|null int 1 if user is banned; int 0 if user is not banned;
1060       *                  null if user is not part of the group or if group doesn't exist.
1061       */
1062  	public static function check_is_banned( $user_id, $group_id ) {
1063          global $wpdb;
1064  
1065          if ( empty( $user_id ) )
1066              return false;
1067  
1068          $bp = buddypress();
1069  
1070          $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 ) );
1071  
1072          return is_numeric( $query ) ? (int) $query : $query;
1073      }
1074  
1075      /**
1076       * Is the specified user the creator of the group?
1077       *
1078       * @since 1.2.6
1079       *
1080       * @param int $user_id  ID of the user.
1081       * @param int $group_id ID of the group.
1082       * @return int|null int of group ID if user is the creator; null on failure.
1083       */
1084  	public static function check_is_creator( $user_id, $group_id ) {
1085          global $wpdb;
1086  
1087          if ( empty( $user_id ) )
1088              return false;
1089  
1090          $bp = buddypress();
1091  
1092          $query = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name} WHERE creator_id = %d AND id = %d", $user_id, $group_id ) );
1093  
1094          return is_numeric( $query ) ? (int) $query : $query;
1095      }
1096  
1097      /**
1098       * Check whether a user has an outstanding membership request for a given group.
1099       *
1100       * @since 1.6.0
1101       *
1102       * @param int $user_id  ID of the user.
1103       * @param int $group_id ID of the group.
1104       * @return int Database ID of the membership if found; int 0 on failure.
1105       */
1106  	public static function check_for_membership_request( $user_id, $group_id ) {
1107          global $wpdb;
1108  
1109          if ( empty( $user_id ) )
1110              return false;
1111  
1112          $bp = buddypress();
1113  
1114          return $wpdb->query( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 0 AND is_banned = 0 AND inviter_id = 0", $user_id, $group_id ) );
1115      }
1116  
1117      /**
1118       * Get a list of randomly selected IDs of groups that the member belongs to.
1119       *
1120       * @since 1.6.0
1121       *
1122       * @param int $user_id      ID of the user.
1123       * @param int $total_groups Max number of group IDs to return. Default: 5.
1124       * @return array Group IDs.
1125       */
1126  	public static function get_random_groups( $user_id = 0, $total_groups = 5 ) {
1127          global $wpdb;
1128  
1129          $bp = buddypress();
1130  
1131          // If the user is logged in and viewing their random groups, we can show hidden and private groups.
1132          if ( bp_is_my_profile() ) {
1133              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 ) ) );
1134          } else {
1135              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 ) ) );
1136          }
1137      }
1138  
1139      /**
1140       * Get the IDs of all a given group's members.
1141       *
1142       * @since 1.6.0
1143       *
1144       * @param int $group_id ID of the group.
1145       * @return array IDs of all group members.
1146       */
1147  	public static function get_group_member_ids( $group_id ) {
1148          global $wpdb;
1149  
1150          $bp = buddypress();
1151  
1152          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 ) ) );
1153      }
1154  
1155      /**
1156       * Get a list of all a given group's admins.
1157       *
1158       * @since 1.6.0
1159       *
1160       * @param int $group_id ID of the group.
1161       * @return array Info about group admins (user_id + date_modified).
1162       */
1163  	public static function get_group_administrator_ids( $group_id ) {
1164          global $wpdb;
1165  
1166          if ( empty( $group_id ) ) {
1167              return array();
1168          }
1169  
1170          $group_admins = wp_cache_get( $group_id, 'bp_group_admins' );
1171  
1172          if ( false === $group_admins ) {
1173              self::prime_group_admins_mods_cache( array( $group_id ) );
1174              $group_admins = wp_cache_get( $group_id, 'bp_group_admins' );
1175          }
1176  
1177          if ( false === $group_admins ) {
1178              // The wp_cache_get is still coming up empty. Return an empty array.
1179              $group_admins = array();
1180          } else {
1181              // Cast the user_id property as an integer.
1182              foreach ( (array) $group_admins as $key => $data ) {
1183                  $group_admins[ $key ]->user_id = (int) $group_admins[ $key ]->user_id;
1184              }
1185          }
1186  
1187          return $group_admins;
1188      }
1189  
1190      /**
1191       * Prime the bp_group_admins cache for one or more groups.
1192       *
1193       * @since 2.7.0
1194       *
1195       * @param array $group_ids IDs of the groups.
1196       * @return bool True on success.
1197       */
1198  	public static function prime_group_admins_mods_cache( $group_ids ) {
1199          global $wpdb;
1200  
1201          $uncached = bp_get_non_cached_ids( $group_ids, 'bp_group_admins' );
1202  
1203          if ( $uncached ) {
1204              $bp = buddypress();
1205              $uncached_sql = implode( ',', array_map( 'intval', $uncached ) );
1206              $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" );
1207  
1208              $admins = $mods = array();
1209              if ( $group_admin_mods ) {
1210                  foreach ( $group_admin_mods as $group_admin_mod ) {
1211                      $obj = new stdClass();
1212                      $obj->user_id = $group_admin_mod->user_id;
1213                      $obj->date_modified = $group_admin_mod->date_modified;
1214  
1215                      if ( $group_admin_mod->is_admin ) {
1216                          $admins[ $group_admin_mod->group_id ][] = $obj;
1217                      } else {
1218                          $mods[ $group_admin_mod->group_id ][] = $obj;
1219                      }
1220                  }
1221              }
1222  
1223              // Prime cache for all groups, even those with no matches.
1224              foreach ( $uncached as $group_id ) {
1225                  $group_admins = isset( $admins[ $group_id ] ) ? $admins[ $group_id ] : array();
1226                  wp_cache_set( $group_id, $group_admins, 'bp_group_admins' );
1227  
1228                  $group_mods = isset( $mods[ $group_id ] ) ? $mods[ $group_id ] : array();
1229                  wp_cache_set( $group_id, $group_mods, 'bp_group_mods' );
1230              }
1231          }
1232      }
1233  
1234      /**
1235       * Get a list of all a given group's moderators.
1236       *
1237       * @since 1.6.0
1238       *
1239       * @param int $group_id ID of the group.
1240       * @return array Info about group mods (user_id + date_modified).
1241       */
1242  	public static function get_group_moderator_ids( $group_id ) {
1243          global $wpdb;
1244  
1245          if ( empty( $group_id ) ) {
1246              return array();
1247          }
1248  
1249          $group_mods = wp_cache_get( $group_id, 'bp_group_mods' );
1250  
1251          if ( false === $group_mods ) {
1252              self::prime_group_admins_mods_cache( array( $group_id ) );
1253              $group_mods = wp_cache_get( $group_id, 'bp_group_mods' );
1254          }
1255  
1256          if ( false === $group_mods ) {
1257              // The wp_cache_get is still coming up empty. Return an empty array.
1258              $group_mods = array();
1259          } else {
1260              // Cast the user_id property as an integer.
1261              foreach ( (array) $group_mods as $key => $data ) {
1262                  $group_mods[ $key ]->user_id = (int) $group_mods[ $key ]->user_id;
1263              }
1264          }
1265  
1266          return $group_mods;
1267      }
1268  
1269      /**
1270       * Get group membership objects by ID (or an array of IDs).
1271       *
1272       * @since 2.6.0
1273       *
1274       * @param int|string|array $membership_ids Single membership ID or comma-separated/array list of membership IDs.
1275       * @return array
1276       */
1277  	public static function get_memberships_by_id( $membership_ids ) {
1278          global $wpdb;
1279  
1280          $bp = buddypress();
1281  
1282          $membership_ids = implode( ',', wp_parse_id_list( $membership_ids ) );
1283          return $wpdb->get_results( "SELECT * FROM {$bp->groups->table_name_members} WHERE id IN ({$membership_ids})" );
1284      }
1285  
1286      /**
1287       * Get the IDs users with outstanding membership requests to the group.
1288       *
1289       * @since 1.6.0
1290       *
1291       * @param int $group_id ID of the group.
1292       * @return array IDs of users with outstanding membership requests.
1293       */
1294  	public static function get_all_membership_request_user_ids( $group_id ) {
1295          global $wpdb;
1296  
1297          $bp = buddypress();
1298  
1299          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 = 0 AND inviter_id = 0", $group_id ) ) );
1300      }
1301  
1302      /**
1303       * Get members of a group.
1304       *
1305       * @deprecated 1.6.0
1306       *
1307       * @param int        $group_id            ID of the group being queried for.
1308       * @param bool|int   $limit               Max amount to return.
1309       * @param bool|int   $page                Pagination value.
1310       * @param bool       $exclude_admins_mods Whether or not to exclude admins and moderators.
1311       * @param bool       $exclude_banned      Whether or not to exclude banned members.
1312       * @param bool|array $exclude             Array of user IDs to exclude.
1313       * @return false|array
1314       */
1315  	public static function get_all_for_group( $group_id, $limit = false, $page = false, $exclude_admins_mods = true, $exclude_banned = true, $exclude = false ) {
1316          global $wpdb;
1317  
1318          _deprecated_function( __METHOD__, '1.8', 'BP_Group_Member_Query' );
1319  
1320          $pag_sql = '';
1321          if ( !empty( $limit ) && !empty( $page ) )
1322              $pag_sql = $wpdb->prepare( "LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
1323  
1324          $exclude_admins_sql = '';
1325          if ( !empty( $exclude_admins_mods ) )
1326              $exclude_admins_sql = "AND is_admin = 0 AND is_mod = 0";
1327  
1328          $banned_sql = '';
1329          if ( !empty( $exclude_banned ) )
1330              $banned_sql = " AND is_banned = 0";
1331  
1332          $exclude_sql = '';
1333          if ( !empty( $exclude ) ) {
1334              $exclude     = implode( ',', wp_parse_id_list( $exclude ) );
1335              $exclude_sql = " AND m.user_id NOT IN ({$exclude})";
1336          }
1337  
1338          $bp = buddypress();
1339  
1340          if ( bp_is_active( 'xprofile' ) ) {
1341  
1342              /**
1343               * Filters the SQL prepared statement used to fetch group members.
1344               *
1345               * @since 1.5.0
1346               *
1347               * @param string $value SQL prepared statement for fetching group members.
1348               */
1349              $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 ) ) );
1350          } else {
1351  
1352              /** This filter is documented in bp-groups/bp-groups-classes */
1353              $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 ) ) );
1354          }
1355  
1356          if ( empty( $members ) ) {
1357              return false;
1358          }
1359  
1360          if ( empty( $pag_sql ) ) {
1361              $total_member_count = count( $members );
1362          } else {
1363  
1364              /**
1365               * Filters the SQL prepared statement used to fetch group members total count.
1366               *
1367               * @since 1.5.0
1368               *
1369               * @param string $value SQL prepared statement for fetching group member count.
1370               */
1371              $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 ) ) );
1372          }
1373  
1374          // Fetch whether or not the user is a friend.
1375          foreach ( (array) $members as $user )
1376              $user_ids[] = $user->user_id;
1377  
1378          $user_ids = implode( ',', wp_parse_id_list( $user_ids ) );
1379  
1380          if ( bp_is_active( 'friends' ) ) {
1381              $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() ) );
1382              for ( $i = 0, $count = count( $members ); $i < $count; ++$i ) {
1383                  foreach ( (array) $friend_status as $status ) {
1384                      if ( $status->initiator_user_id == $members[$i]->user_id || $status->friend_user_id == $members[$i]->user_id ) {
1385                          $members[$i]->is_friend = $status->is_confirmed;
1386                      }
1387                  }
1388              }
1389          }
1390  
1391          return array( 'members' => $members, 'count' => $total_member_count );
1392      }
1393  
1394      /**
1395       * Get all membership IDs for a user.
1396       *
1397       * @since 2.6.0
1398       *
1399       * @param int $user_id ID of the user.
1400       * @return array
1401       */
1402  	public static function get_membership_ids_for_user( $user_id ) {
1403          global $wpdb;
1404  
1405          $bp = buddypress();
1406  
1407          $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 ) );
1408  
1409          return $group_ids;
1410      }
1411  
1412      /**
1413       * Delete all memberships for a given group.
1414       *
1415       * @since 1.6.0
1416       *
1417       * @param int $group_id ID of the group.
1418       * @return int Number of records deleted.
1419       */
1420  	public static function delete_all( $group_id ) {
1421          global $wpdb;
1422  
1423          $bp = buddypress();
1424  
1425          return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE group_id = %d", $group_id ) );
1426      }
1427  
1428      /**
1429       * Delete all group membership information for the specified user.
1430       *
1431       * In cases where the user is the sole member of a group, a site administrator is
1432       * assigned to be the group's administrator. Unhook `groups_remove_data_for_user()`
1433       * to modify this behavior.
1434       *
1435       * @since 1.0.0
1436       * @since 4.0.0 The method behavior was changed so that single-member groups are not deleted.
1437       *
1438       * @param int $user_id ID of the user.
1439       * @return bool
1440       */
1441  	public static function delete_all_for_user( $user_id ) {
1442          $group_ids = BP_Groups_Member::get_group_ids( $user_id );
1443  
1444          foreach ( $group_ids['groups'] as $group_id ) {
1445              if ( groups_is_user_admin( $user_id, $group_id ) ) {
1446                  // If the user is a sole group admin, install a site admin as their replacement.
1447                  if ( count( groups_get_group_admins( $group_id ) ) < 2 ) {
1448                      $admin = get_users( array(
1449                          'blog_id' => bp_get_root_blog_id(),
1450                          'fields'  => 'id',
1451                          'number'  => 1,
1452                          'orderby' => 'ID',
1453                          'role'    => 'administrator',
1454                      ) );
1455  
1456                      if ( ! empty( $admin ) ) {
1457                          groups_join_group( $group_id, $admin[0] );
1458  
1459                          $member = new BP_Groups_Member( $admin[0], $group_id );
1460                          $member->promote( 'admin' );
1461                      }
1462                  }
1463              }
1464  
1465              BP_Groups_Member::delete( $user_id, $group_id );
1466          }
1467  
1468          return true;
1469      }
1470  }


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