[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-groups/classes/ -> class-bp-groups-group-members-template.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Groups group members loop template class.
   4   *
   5   * @package BuddyPress
   6   * @since 1.1.0
   7   */
   8  
   9  // Exit if accessed directly.
  10  defined( 'ABSPATH' ) || exit;
  11  
  12  /**
  13   * Group Members Loop template class.
  14   *
  15   * @since 1.0.0
  16   */
  17  class BP_Groups_Group_Members_Template {
  18  
  19      /**
  20       * @since 1.0.0
  21       * @var int
  22       */
  23      public $current_member = -1;
  24  
  25      /**
  26       * @since 1.0.0
  27       * @var int
  28       */
  29      public $member_count;
  30  
  31      /**
  32       * @since 1.0.0
  33       * @var array
  34       */
  35      public $members;
  36  
  37      /**
  38       * @since 1.0.0
  39       * @var object
  40       */
  41      public $member;
  42  
  43      /**
  44       * @since 1.0.0
  45       * @var bool
  46       */
  47      public $in_the_loop;
  48  
  49      /**
  50       * @since 1.0.0
  51       * @var int
  52       */
  53      public $pag_page;
  54  
  55      /**
  56       * @since 1.0.0
  57       * @var int
  58       */
  59      public $pag_num;
  60  
  61      /**
  62       * @since 1.0.0
  63       * @var array|string|void
  64       */
  65      public $pag_links;
  66  
  67      /**
  68       * @since 1.0.0
  69       * @var int
  70       */
  71      public $total_group_count;
  72  
  73      /**
  74       * Constructor.
  75       *
  76       * @since 1.5.0
  77       *
  78       * @param array $args {
  79       *     An array of optional arguments.
  80       *     @type int      $group_id           ID of the group whose members are being
  81       *                                        queried. Default: current group ID.
  82       *     @type int      $page               Page of results to be queried. Default: 1.
  83       *     @type int      $per_page           Number of items to return per page of
  84       *                                        results. Default: 20.
  85       *     @type int      $max                Optional. Max number of items to return.
  86       *     @type array    $exclude            Optional. Array of user IDs to exclude.
  87       *     @type bool|int $exclude_admin_mods True (or 1) to exclude admins and mods from
  88       *                                        results. Default: 1.
  89       *     @type bool|int $exclude_banned     True (or 1) to exclude banned users from results.
  90       *                                        Default: 1.
  91       *     @type array    $group_role         Optional. Array of group roles to include.
  92       *     @type string   $search_terms       Optional. Search terms to match.
  93       * }
  94       */
  95  	public function __construct( $args = array() ) {
  96  
  97          // Backward compatibility with old method of passing arguments.
  98          if ( ! is_array( $args ) || func_num_args() > 1 ) {
  99              _deprecated_argument( __METHOD__, '2.0.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
 100  
 101              $old_args_keys = array(
 102                  0 => 'group_id',
 103                  1 => 'per_page',
 104                  2 => 'max',
 105                  3 => 'exclude_admins_mods',
 106                  4 => 'exclude_banned',
 107                  5 => 'exclude',
 108                  6 => 'group_role',
 109              );
 110  
 111              $args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
 112          }
 113  
 114          $r = bp_parse_args( $args, array(
 115              'group_id'            => bp_get_current_group_id(),
 116              'page'                => 1,
 117              'per_page'            => 20,
 118              'page_arg'            => 'mlpage',
 119              'max'                 => false,
 120              'exclude'             => false,
 121              'exclude_admins_mods' => 1,
 122              'exclude_banned'      => 1,
 123              'group_role'          => false,
 124              'search_terms'        => false,
 125              'type'                => 'last_joined',
 126          ), 'group_members_template' );
 127  
 128          $this->pag_arg  = sanitize_key( $r['page_arg'] );
 129          $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page']     );
 130          $this->pag_num  = bp_sanitize_pagination_arg( 'num',          $r['per_page'] );
 131  
 132          /**
 133           * Check the current group is the same as the supplied group ID.
 134           * It can differ when using {@link bp_group_has_members()} outside the Groups screens.
 135           */
 136          $current_group = groups_get_current_group();
 137          if ( empty( $current_group ) || ( $current_group && $current_group->id !== bp_get_current_group_id() ) ) {
 138              $current_group = groups_get_group( $r['group_id'] );
 139          }
 140  
 141          // Assemble the base URL for pagination.
 142          $base_url = trailingslashit( bp_get_group_permalink( $current_group ) . bp_current_action() );
 143          if ( bp_action_variable() ) {
 144              $base_url = trailingslashit( $base_url . bp_action_variable() );
 145          }
 146  
 147          $members_args = $r;
 148  
 149          $members_args['page']     = $this->pag_page;
 150          $members_args['per_page'] = $this->pag_num;
 151  
 152          // Get group members for this loop.
 153          $this->members = groups_get_group_members( $members_args );
 154  
 155          if ( empty( $r['max'] ) || ( $r['max'] >= (int) $this->members['count'] ) ) {
 156              $this->total_member_count = (int) $this->members['count'];
 157          } else {
 158              $this->total_member_count = (int) $r['max'];
 159          }
 160  
 161          // Reset members array for subsequent looping.
 162          $this->members = $this->members['members'];
 163  
 164          if ( empty( $r['max'] ) || ( $r['max'] >= count( $this->members ) ) ) {
 165              $this->member_count = (int) count( $this->members );
 166          } else {
 167              $this->member_count = (int) $r['max'];
 168          }
 169  
 170          $this->pag_links = paginate_links( array(
 171              'base'      => add_query_arg( array( $this->pag_arg => '%#%' ), $base_url ),
 172              'format'    => '',
 173              'total'     => ! empty( $this->pag_num ) ? ceil( $this->total_member_count / $this->pag_num ) : $this->total_member_count,
 174              'current'   => $this->pag_page,
 175              'prev_text' => '&larr;',
 176              'next_text' => '&rarr;',
 177              'mid_size'  => 1,
 178              'add_args'  => array(),
 179          ) );
 180      }
 181  
 182      /**
 183       * Whether or not there are members to display.
 184       *
 185       * @since 1.0.0
 186       *
 187       * @return bool
 188       */
 189  	public function has_members() {
 190          if ( ! empty( $this->member_count ) ) {
 191              return true;
 192          }
 193  
 194          return false;
 195      }
 196  
 197      /**
 198       * Increments to the next member to display.
 199       *
 200       * @since 1.0.0
 201       *
 202       * @return object
 203       */
 204  	public function next_member() {
 205          $this->current_member++;
 206          $this->member = $this->members[ $this->current_member ];
 207  
 208          return $this->member;
 209      }
 210  
 211      /**
 212       * Rewinds to the first member to display.
 213       *
 214       * @since 1.0.0
 215       */
 216  	public function rewind_members() {
 217          $this->current_member = -1;
 218          if ( $this->member_count > 0 ) {
 219              $this->member = $this->members[0];
 220          }
 221      }
 222  
 223      /**
 224       * Finishes up the members for display.
 225       *
 226       * @since 1.0.0
 227       *
 228       * @return bool
 229       */
 230  	public function members() {
 231          $tick = intval( $this->current_member + 1 );
 232          if ( $tick < $this->member_count ) {
 233              return true;
 234          } elseif ( $tick == $this->member_count ) {
 235  
 236              /**
 237               * Fires right before the rewinding of members list.
 238               *
 239               * @since 1.0.0
 240               * @since 2.3.0 `$this` parameter added.
 241               * @since 2.7.0 Action renamed from `loop_end`.
 242               *
 243               * @param BP_Groups_Group_Members_Template $this Instance of the current Members template.
 244               */
 245              do_action( 'group_members_loop_end', $this );
 246  
 247              // Do some cleaning up after the loop.
 248              $this->rewind_members();
 249          }
 250  
 251          $this->in_the_loop = false;
 252          return false;
 253      }
 254  
 255      /**
 256       * Sets up the member to display.
 257       *
 258       * @since 1.0.0
 259       */
 260  	public function the_member() {
 261          $this->in_the_loop = true;
 262          $this->member      = $this->next_member();
 263  
 264          // Loop has just started.
 265          if ( 0 == $this->current_member ) {
 266  
 267              /**
 268               * Fires if the current member item is the first in the members list.
 269               *
 270               * @since 1.0.0
 271               * @since 2.3.0 `$this` parameter added.
 272               * @since 2.7.0 Action renamed from `loop_start`.
 273               *
 274               * @param BP_Groups_Group_Members_Template $this Instance of the current Members template.
 275               */
 276              do_action( 'group_members_loop_start', $this );
 277          }
 278      }
 279  }


Generated: Mon Jul 22 01:01:43 2019 Cross-referenced by PHPXref 0.7.1