[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Member Template loop class.
   4   *
   5   * @package BuddyPress
   6   * @subpackage Members
   7   */
   8  
   9  // Exit if accessed directly.
  10  defined( 'ABSPATH' ) || exit;
  11  
  12  /**
  13   * The main member template loop class.
  14   *
  15   * Responsible for loading a group of members into a loop for display.
  16   *
  17   * @since 1.0.0
  18   */
  19  class BP_Core_Members_Template {
  20  
  21      /**
  22       * The loop iterator.
  23       *
  24       * @since 1.0.0
  25       * @var int
  26       */
  27      public $current_member = -1;
  28  
  29      /**
  30       * The number of members returned by the paged query.
  31       *
  32       * @since 1.0.0
  33       * @var int
  34       */
  35      public $member_count;
  36  
  37      /**
  38       * Array of members located by the query.
  39       *
  40       * @since 1.0.0
  41       * @var array
  42       */
  43      public $members;
  44  
  45      /**
  46       * The member object currently being iterated on.
  47       *
  48       * @since 1.0.0
  49       * @var object
  50       */
  51      public $member;
  52  
  53      /**
  54       * A flag for whether the loop is currently being iterated.
  55       *
  56       * @since 1.0.0
  57       * @var bool
  58       */
  59      public $in_the_loop;
  60  
  61      /**
  62       * The type of member being requested. Used for ordering results.
  63       *
  64       * @since 2.3.0
  65       * @var string
  66       */
  67      public $type;
  68  
  69      /**
  70       * The unique string used for pagination queries.
  71       *
  72       * @since 2.2.0
  73       * @var string
  74       */
  75      public $pag_arg;
  76  
  77      /**
  78       * The page number being requested.
  79       *
  80       * @since 1.0.0
  81       * @var string
  82       */
  83      public $pag_page;
  84  
  85      /**
  86       * The number of items being requested per page.
  87       *
  88       * @since 1.0.0
  89       * @var string
  90       */
  91      public $pag_num;
  92  
  93      /**
  94       * An HTML string containing pagination links.
  95       *
  96       * @since 1.0.0
  97       * @var string
  98       */
  99      public $pag_links;
 100  
 101      /**
 102       * The total number of members matching the query parameters.
 103       *
 104       * @since 1.0.0
 105       * @var int
 106       */
 107      public $total_member_count;
 108  
 109      /**
 110       * Constructor method.
 111       *
 112       * @since 1.5.0
 113       * @since 7.0.0 Added $xprofile_query parameter. Added $user_ids parameter.
 114       *
 115       * @see BP_User_Query for an in-depth description of parameters.
 116       *
 117       * @param array $args {
 118       *     Array of arguments. Supports all arguments of BP_User_Query. Additional
 119       *     arguments, or those with different defaults, are described below.
 120       *
 121       *     @type int    $page_number Page of results. Accepted for legacy reasons. Use 'page' instead.
 122       *     @type int    $max         Max number of results to return.
 123       *     @type string $page_arg    Optional. The string used as a query parameter in pagination links.
 124       * }
 125       */
 126  	public function __construct( $args ) {
 127          // Backward compatibility with old method of passing arguments.
 128          if ( ! is_array( $args ) || func_num_args() > 1 ) {
 129              _deprecated_argument( __METHOD__, '7.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__ ) );
 130  
 131              $old_args_keys = array(
 132                  0  => 'type',
 133                  1  => 'page_number',
 134                  2  => 'per_page',
 135                  3  => 'max',
 136                  4  => 'user_id',
 137                  5  => 'search_terms',
 138                  6  => 'include',
 139                  7  => 'populate_extras',
 140                  8  => 'exclude',
 141                  9  => 'meta_key',
 142                  10 => 'meta_value',
 143                  11 => 'page_arg',
 144                  12 => 'member_type',
 145                  13 => 'member_type__in',
 146                  14 => 'member_type__not_in'
 147              );
 148  
 149              $args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
 150          }
 151  
 152          // Support both 'page_number' and 'page' for backward compatibility.
 153          $args['page_number'] = isset( $args['page_number'] ) ? $args['page_number'] : $args['page'];
 154  
 155          $defaults = array(
 156              'type'                => 'active',
 157              'page_number'         => 1,
 158              'per_page'            => 20,
 159              'max'                 => false,
 160              'user_id'             => false,
 161              'search_terms'        => null,
 162              'include'             => false,
 163              'populate_extras'     => true,
 164              'exclude'             => false,
 165              'user_ids'            => false,
 166              'meta_key'            => false,
 167              'meta_value'          => false,
 168              'page_arg'            => 'upage',
 169              'member_type'         => '',
 170              'member_type__in'     => '',
 171              'member_type__not_in' => '',
 172              'xprofile_query'      => false,
 173          );
 174          $r = wp_parse_args( $args, $defaults );
 175  
 176          $this->pag_arg  = sanitize_key( $r['page_arg'] );
 177          $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page_number'] );
 178          $this->pag_num  = bp_sanitize_pagination_arg( 'num',          $r['per_page']    );
 179          $this->type     = $r['type'];
 180  
 181          if ( ! empty( $_REQUEST['letter'] ) ) {
 182              $this->members = BP_Core_User::get_users_by_letter( $_REQUEST['letter'], $this->pag_num, $this->pag_page, $r['populate_extras'], $r['exclude'] );
 183          } else {
 184              $this->members = bp_core_get_users(
 185                  array(
 186                      'type'                => $this->type,
 187                      'per_page'            => $this->pag_num,
 188                      'page'                => $this->pag_page,
 189                      'user_id'             => $r['user_id'],
 190                      'include'             => $r['include'],
 191                      'search_terms'        => $r['search_terms'],
 192                      'populate_extras'     => $r['populate_extras'],
 193                      'exclude'             => $r['exclude'],
 194                      'user_ids'            => $r['user_ids'],
 195                      'meta_key'            => $r['meta_key'],
 196                      'meta_value'          => $r['meta_value'],
 197                      'member_type'         => $r['member_type'],
 198                      'member_type__in'     => $r['member_type__in'],
 199                      'member_type__not_in' => $r['member_type__not_in'],
 200                      'xprofile_query'      => $r['xprofile_query'],
 201                  )
 202              );
 203          }
 204  
 205          if ( ! $r['max'] || $r['max'] >= (int) $this->members['total'] ) {
 206              $this->total_member_count = (int) $this->members['total'];
 207          } else {
 208              $this->total_member_count = (int) $r['max'];
 209          }
 210  
 211          $this->members = $this->members['users'];
 212  
 213          if ( $r['max'] ) {
 214              if ( $r['max'] >= count( $this->members ) ) {
 215                  $this->member_count = count( $this->members );
 216              } else {
 217                  $this->member_count = (int) $r['max'];
 218              }
 219          } else {
 220              $this->member_count = count( $this->members );
 221          }
 222  
 223          if ( (int) $this->total_member_count && (int) $this->pag_num ) {
 224              $pag_args = array(
 225                  $this->pag_arg => '%#%',
 226              );
 227  
 228              if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) {
 229                  $base = remove_query_arg( 's', wp_get_referer() );
 230              } else {
 231                  $base = '';
 232              }
 233  
 234              /**
 235               * Defaults to an empty array to make sure paginate_links()
 236               * won't add the $page_arg to the links which would break
 237               * pagination in case JavaScript is disabled.
 238               */
 239              $add_args = array();
 240  
 241              if ( ! empty( $r['search_terms'] ) ) {
 242                  $query_arg = bp_core_get_component_search_query_arg( 'members' );
 243                  $add_args[ $query_arg ] = urlencode( $r['search_terms'] );
 244              }
 245  
 246              $this->pag_links = paginate_links( array(
 247                  'base'      => add_query_arg( $pag_args, $base ),
 248                  'format'    => '',
 249                  'total'     => ceil( (int) $this->total_member_count / (int) $this->pag_num ),
 250                  'current'   => (int) $this->pag_page,
 251                  'prev_text' => _x( '&larr;', 'Member pagination previous text', 'buddypress' ),
 252                  'next_text' => _x( '&rarr;', 'Member pagination next text', 'buddypress' ),
 253                  'mid_size'  => 1,
 254                  'add_args'  => $add_args,
 255              ) );
 256          }
 257      }
 258  
 259      /**
 260       * Whether there are members available in the loop.
 261       *
 262       * @since 1.0.0
 263       *
 264       * @see bp_has_members()
 265       *
 266       * @return bool True if there are items in the loop, otherwise false.
 267       */
 268  	function has_members() {
 269          if ( $this->member_count )
 270              return true;
 271  
 272          return false;
 273      }
 274  
 275      /**
 276       * Set up the next member and iterate index.
 277       *
 278       * @since 1.0.0
 279       *
 280       * @return object The next member to iterate over.
 281       */
 282  	function next_member() {
 283          $this->current_member++;
 284          $this->member = $this->members[$this->current_member];
 285  
 286          return $this->member;
 287      }
 288  
 289      /**
 290       * Rewind the members and reset member index.
 291       *
 292       * @since 1.0.0
 293       */
 294  	function rewind_members() {
 295          $this->current_member = -1;
 296          if ( $this->member_count > 0 ) {
 297              $this->member = $this->members[0];
 298          }
 299      }
 300  
 301      /**
 302       * Whether there are members left in the loop to iterate over.
 303       *
 304       * This method is used by {@link bp_members()} as part of the while loop
 305       * that controls iteration inside the members loop, eg:
 306       *     while ( bp_members() ) { ...
 307       *
 308       * @since 1.2.0
 309       *
 310       * @see bp_members()
 311       *
 312       * @return bool True if there are more members to show, otherwise false.
 313       */
 314  	function members() {
 315          if ( $this->current_member + 1 < $this->member_count ) {
 316              return true;
 317          } elseif ( $this->current_member + 1 == $this->member_count ) {
 318  
 319              /**
 320               * Fires right before the rewinding of members listing.
 321               *
 322               * @since 1.5.0
 323               */
 324              do_action('member_loop_end');
 325              // Do some cleaning up after the loop.
 326              $this->rewind_members();
 327          }
 328  
 329          $this->in_the_loop = false;
 330          return false;
 331      }
 332  
 333      /**
 334       * Set up the current member inside the loop.
 335       *
 336       * Used by {@link bp_the_member()} to set up the current member data
 337       * while looping, so that template tags used during that iteration make
 338       * reference to the current member.
 339       *
 340       * @since 1.0.0
 341       *
 342       * @see bp_the_member()
 343       */
 344  	function the_member() {
 345  
 346          $this->in_the_loop = true;
 347          $this->member      = $this->next_member();
 348  
 349          // Loop has just started.
 350          if ( 0 == $this->current_member ) {
 351  
 352              /**
 353               * Fires if the current member is the first in the loop.
 354               *
 355               * @since 1.5.0
 356               */
 357              do_action( 'member_loop_start' );
 358          }
 359  
 360      }
 361  }


Generated: Fri Oct 30 01:01:33 2020 Cross-referenced by PHPXref 0.7.1