[ 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       *
 114       * @see BP_User_Query for an in-depth description of parameters.
 115       *
 116       * @param string       $type                Sort order.
 117       * @param int          $page_number         Page of results.
 118       * @param int          $per_page            Number of results per page.
 119       * @param int          $max                 Max number of results to return.
 120       * @param int          $user_id             Limit to friends of a user.
 121       * @param string       $search_terms        Limit to users matching search terms.
 122       * @param array        $include             Limit results by these user IDs.
 123       * @param bool         $populate_extras     Fetch optional extras.
 124       * @param array        $exclude             Exclude these IDs from results.
 125       * @param array        $meta_key            Limit to users with a meta_key.
 126       * @param array        $meta_value          Limit to users with a meta_value (with meta_key).
 127       * @param string       $page_arg            Optional. The string used as a query parameter in pagination links.
 128       *                                          Default: 'upage'.
 129       * @param array|string $member_type         Array or comma-separated string of member types to limit results to.
 130       * @param array|string $member_type__in     Array or comma-separated string of member types to limit results to.
 131       * @param array|string $member_type__not_in Array or comma-separated string of member types to exclude
 132       *                                          from results.
 133       */
 134  	function __construct( $type, $page_number, $per_page, $max, $user_id, $search_terms, $include, $populate_extras, $exclude, $meta_key, $meta_value, $page_arg = 'upage', $member_type = '', $member_type__in = '', $member_type__not_in = '' ) {
 135  
 136          $this->pag_arg  = sanitize_key( $page_arg );
 137          $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $page_number );
 138          $this->pag_num  = bp_sanitize_pagination_arg( 'num',          $per_page    );
 139          $this->type     = $type;
 140  
 141          if ( !empty( $_REQUEST['letter'] ) )
 142              $this->members = BP_Core_User::get_users_by_letter( $_REQUEST['letter'], $this->pag_num, $this->pag_page, $populate_extras, $exclude );
 143          else
 144              $this->members = bp_core_get_users( array( 'type' => $this->type, 'per_page' => $this->pag_num, 'page' => $this->pag_page, 'user_id' => $user_id, 'include' => $include, 'search_terms' => $search_terms, 'populate_extras' => $populate_extras, 'exclude' => $exclude, 'meta_key' => $meta_key, 'meta_value' => $meta_value, 'member_type' => $member_type, 'member_type__in' => $member_type__in, 'member_type__not_in' => $member_type__not_in ) );
 145  
 146          if ( !$max || $max >= (int) $this->members['total'] )
 147              $this->total_member_count = (int) $this->members['total'];
 148          else
 149              $this->total_member_count = (int) $max;
 150  
 151          $this->members = $this->members['users'];
 152  
 153          if ( $max ) {
 154              if ( $max >= count( $this->members ) ) {
 155                  $this->member_count = count( $this->members );
 156              } else {
 157                  $this->member_count = (int) $max;
 158              }
 159          } else {
 160              $this->member_count = count( $this->members );
 161          }
 162  
 163          if ( (int) $this->total_member_count && (int) $this->pag_num ) {
 164              $pag_args = array(
 165                  $this->pag_arg => '%#%',
 166              );
 167  
 168              if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) {
 169                  $base = remove_query_arg( 's', wp_get_referer() );
 170              } else {
 171                  $base = '';
 172              }
 173  
 174              /**
 175               * Defaults to an empty array to make sure paginate_links()
 176               * won't add the $page_arg to the links which would break
 177               * pagination in case JavaScript is disabled.
 178               */
 179              $add_args = array();
 180  
 181              if ( ! empty( $search_terms ) ) {
 182                  $query_arg = bp_core_get_component_search_query_arg( 'members' );
 183                  $add_args[ $query_arg ] = urlencode( $search_terms );
 184              }
 185  
 186              $this->pag_links = paginate_links( array(
 187                  'base'      => add_query_arg( $pag_args, $base ),
 188                  'format'    => '',
 189                  'total'     => ceil( (int) $this->total_member_count / (int) $this->pag_num ),
 190                  'current'   => (int) $this->pag_page,
 191                  'prev_text' => _x( '&larr;', 'Member pagination previous text', 'buddypress' ),
 192                  'next_text' => _x( '&rarr;', 'Member pagination next text', 'buddypress' ),
 193                  'mid_size'  => 1,
 194                  'add_args'  => $add_args,
 195              ) );
 196          }
 197      }
 198  
 199      /**
 200       * Whether there are members available in the loop.
 201       *
 202       * @since 1.0.0
 203       *
 204       * @see bp_has_members()
 205       *
 206       * @return bool True if there are items in the loop, otherwise false.
 207       */
 208  	function has_members() {
 209          if ( $this->member_count )
 210              return true;
 211  
 212          return false;
 213      }
 214  
 215      /**
 216       * Set up the next member and iterate index.
 217       *
 218       * @since 1.0.0
 219       *
 220       * @return object The next member to iterate over.
 221       */
 222  	function next_member() {
 223          $this->current_member++;
 224          $this->member = $this->members[$this->current_member];
 225  
 226          return $this->member;
 227      }
 228  
 229      /**
 230       * Rewind the members and reset member index.
 231       *
 232       * @since 1.0.0
 233       */
 234  	function rewind_members() {
 235          $this->current_member = -1;
 236          if ( $this->member_count > 0 ) {
 237              $this->member = $this->members[0];
 238          }
 239      }
 240  
 241      /**
 242       * Whether there are members left in the loop to iterate over.
 243       *
 244       * This method is used by {@link bp_members()} as part of the while loop
 245       * that controls iteration inside the members loop, eg:
 246       *     while ( bp_members() ) { ...
 247       *
 248       * @since 1.2.0
 249       *
 250       * @see bp_members()
 251       *
 252       * @return bool True if there are more members to show, otherwise false.
 253       */
 254  	function members() {
 255          if ( $this->current_member + 1 < $this->member_count ) {
 256              return true;
 257          } elseif ( $this->current_member + 1 == $this->member_count ) {
 258  
 259              /**
 260               * Fires right before the rewinding of members listing.
 261               *
 262               * @since 1.5.0
 263               */
 264              do_action('member_loop_end');
 265              // Do some cleaning up after the loop.
 266              $this->rewind_members();
 267          }
 268  
 269          $this->in_the_loop = false;
 270          return false;
 271      }
 272  
 273      /**
 274       * Set up the current member inside the loop.
 275       *
 276       * Used by {@link bp_the_member()} to set up the current member data
 277       * while looping, so that template tags used during that iteration make
 278       * reference to the current member.
 279       *
 280       * @since 1.0.0
 281       *
 282       * @see bp_the_member()
 283       */
 284  	function the_member() {
 285  
 286          $this->in_the_loop = true;
 287          $this->member      = $this->next_member();
 288  
 289          // Loop has just started.
 290          if ( 0 == $this->current_member ) {
 291  
 292              /**
 293               * Fires if the current member is the first in the loop.
 294               *
 295               * @since 1.5.0
 296               */
 297              do_action( 'member_loop_start' );
 298          }
 299  
 300      }
 301  }


Generated: Fri Dec 6 01:01:42 2019 Cross-referenced by PHPXref 0.7.1