[ 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          $function_args = func_get_args();
  97  
  98          // Backward compatibility with old method of passing arguments.
  99          if ( ! is_array( $args ) || count( $function_args ) > 1 ) {
 100              _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__ ) );
 101  
 102              $old_args_keys = array(
 103                  0 => 'group_id',
 104                  1 => 'per_page',
 105                  2 => 'max',
 106                  3 => 'exclude_admins_mods',
 107                  4 => 'exclude_banned',
 108                  5 => 'exclude',
 109                  6 => 'group_role',
 110              );
 111  
 112              $args = bp_core_parse_args_array( $old_args_keys, $function_args );
 113          }
 114  
 115          $r = bp_parse_args( $args, array(
 116              'group_id'            => bp_get_current_group_id(),
 117              'page'                => 1,
 118              'per_page'            => 20,
 119              'page_arg'            => 'mlpage',
 120              'max'                 => false,
 121              'exclude'             => false,
 122              'exclude_admins_mods' => 1,
 123              'exclude_banned'      => 1,
 124              'group_role'          => false,
 125              'search_terms'        => false,
 126              'type'                => 'last_joined',
 127          ), 'group_members_template' );
 128  
 129          $this->pag_arg  = sanitize_key( $r['page_arg'] );
 130          $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page']     );
 131          $this->pag_num  = bp_sanitize_pagination_arg( 'num',          $r['per_page'] );
 132  
 133          /**
 134           * Check the current group is the same as the supplied group ID.
 135           * It can differ when using {@link bp_group_has_members()} outside the Groups screens.
 136           */
 137          $current_group = groups_get_current_group();
 138          if ( empty( $current_group ) || ( $current_group && $current_group->id !== bp_get_current_group_id() ) ) {
 139              $current_group = groups_get_group( $r['group_id'] );
 140          }
 141  
 142          // Assemble the base URL for pagination.
 143          $base_url = trailingslashit( bp_get_group_permalink( $current_group ) . bp_current_action() );
 144          if ( bp_action_variable() ) {
 145              $base_url = trailingslashit( $base_url . bp_action_variable() );
 146          }
 147  
 148          $members_args = $r;
 149  
 150          $members_args['page']     = $this->pag_page;
 151          $members_args['per_page'] = $this->pag_num;
 152  
 153          // Get group members for this loop.
 154          $this->members = groups_get_group_members( $members_args );
 155  
 156          if ( empty( $r['max'] ) || ( $r['max'] >= (int) $this->members['count'] ) ) {
 157              $this->total_member_count = (int) $this->members['count'];
 158          } else {
 159              $this->total_member_count = (int) $r['max'];
 160          }
 161  
 162          // Reset members array for subsequent looping.
 163          $this->members = $this->members['members'];
 164  
 165          if ( empty( $r['max'] ) || ( $r['max'] >= count( $this->members ) ) ) {
 166              $this->member_count = (int) count( $this->members );
 167          } else {
 168              $this->member_count = (int) $r['max'];
 169          }
 170  
 171          $this->pag_links = paginate_links( array(
 172              'base'      => add_query_arg( array( $this->pag_arg => '%#%' ), $base_url ),
 173              'format'    => '',
 174              'total'     => ! empty( $this->pag_num ) ? ceil( $this->total_member_count / $this->pag_num ) : $this->total_member_count,
 175              'current'   => $this->pag_page,
 176              'prev_text' => '&larr;',
 177              'next_text' => '&rarr;',
 178              'mid_size'  => 1,
 179              'add_args'  => array(),
 180          ) );
 181      }
 182  
 183      /**
 184       * Whether or not there are members to display.
 185       *
 186       * @since 1.0.0
 187       *
 188       * @return bool
 189       */
 190  	public function has_members() {
 191          if ( ! empty( $this->member_count ) ) {
 192              return true;
 193          }
 194  
 195          return false;
 196      }
 197  
 198      /**
 199       * Increments to the next member to display.
 200       *
 201       * @since 1.0.0
 202       *
 203       * @return object
 204       */
 205  	public function next_member() {
 206          $this->current_member++;
 207          $this->member = $this->members[ $this->current_member ];
 208  
 209          return $this->member;
 210      }
 211  
 212      /**
 213       * Rewinds to the first member to display.
 214       *
 215       * @since 1.0.0
 216       */
 217  	public function rewind_members() {
 218          $this->current_member = -1;
 219          if ( $this->member_count > 0 ) {
 220              $this->member = $this->members[0];
 221          }
 222      }
 223  
 224      /**
 225       * Finishes up the members for display.
 226       *
 227       * @since 1.0.0
 228       *
 229       * @return bool
 230       */
 231  	public function members() {
 232          $tick = intval( $this->current_member + 1 );
 233          if ( $tick < $this->member_count ) {
 234              return true;
 235          } elseif ( $tick == $this->member_count ) {
 236  
 237              /**
 238               * Fires right before the rewinding of members list.
 239               *
 240               * @since 1.0.0
 241               * @since 2.3.0 `$this` parameter added.
 242               * @since 2.7.0 Action renamed from `loop_end`.
 243               *
 244               * @param BP_Groups_Group_Members_Template $this Instance of the current Members template.
 245               */
 246              do_action( 'group_members_loop_end', $this );
 247  
 248              // Do some cleaning up after the loop.
 249              $this->rewind_members();
 250          }
 251  
 252          $this->in_the_loop = false;
 253          return false;
 254      }
 255  
 256      /**
 257       * Sets up the member to display.
 258       *
 259       * @since 1.0.0
 260       */
 261  	public function the_member() {
 262          $this->in_the_loop = true;
 263          $this->member      = $this->next_member();
 264  
 265          // Loop has just started.
 266          if ( 0 == $this->current_member ) {
 267  
 268              /**
 269               * Fires if the current member item is the first in the members list.
 270               *
 271               * @since 1.0.0
 272               * @since 2.3.0 `$this` parameter added.
 273               * @since 2.7.0 Action renamed from `loop_start`.
 274               *
 275               * @param BP_Groups_Group_Members_Template $this Instance of the current Members template.
 276               */
 277              do_action( 'group_members_loop_start', $this );
 278          }
 279      }
 280  }


Generated: Fri Oct 18 01:01:36 2019 Cross-referenced by PHPXref 0.7.1