BP_Core_Members_Template

The main member template loop class.

Defined (1)

The class is defined in the following location(s).

/bp-members/classes/class-bp-core-members-template.php  
  1. class BP_Core_Members_Template { 
  2.  
  3. /** 
  4. * The loop iterator. 
  5. * @since 1.0.0 
  6. * @var int 
  7. */ 
  8. public $current_member = -1; 
  9.  
  10. /** 
  11. * The number of members returned by the paged query. 
  12. * @since 1.0.0 
  13. * @var int 
  14. */ 
  15. public $member_count; 
  16.  
  17. /** 
  18. * Array of members located by the query. 
  19. * @since 1.0.0 
  20. * @var array 
  21. */ 
  22. public $members; 
  23.  
  24. /** 
  25. * The member object currently being iterated on. 
  26. * @since 1.0.0 
  27. * @var object 
  28. */ 
  29. public $member; 
  30.  
  31. /** 
  32. * A flag for whether the loop is currently being iterated. 
  33. * @since 1.0.0 
  34. * @var bool 
  35. */ 
  36. public $in_the_loop; 
  37.  
  38. /** 
  39. * The type of member being requested. Used for ordering results. 
  40. * @since 2.3.0 
  41. * @var string 
  42. */ 
  43. public $type; 
  44.  
  45. /** 
  46. * The unique string used for pagination queries. 
  47. * @since 2.2.0 
  48. * @var string 
  49. */ 
  50. public $pag_arg; 
  51.  
  52. /** 
  53. * The page number being requested. 
  54. * @since 1.0.0 
  55. * @var string 
  56. */ 
  57. public $pag_page; 
  58.  
  59. /** 
  60. * The number of items being requested per page. 
  61. * @since 1.0.0 
  62. * @var string 
  63. */ 
  64. public $pag_num; 
  65.  
  66. /** 
  67. * An HTML string containing pagination links. 
  68. * @since 1.0.0 
  69. * @var string 
  70. */ 
  71. public $pag_links; 
  72.  
  73. /** 
  74. * The total number of members matching the query parameters. 
  75. * @since 1.0.0 
  76. * @var int 
  77. */ 
  78. public $total_member_count; 
  79.  
  80. /** 
  81. * Constructor method. 
  82. * @since 1.5.0 
  83. * @see BP_User_Query for an in-depth description of parameters. 
  84. * @param string $type Sort order. 
  85. * @param int $page_number Page of results. 
  86. * @param int $per_page Number of results per page. 
  87. * @param int $max Max number of results to return. 
  88. * @param int $user_id Limit to friends of a user. 
  89. * @param string $search_terms Limit to users matching search terms. 
  90. * @param array $include Limit results by these user IDs. 
  91. * @param bool $populate_extras Fetch optional extras. 
  92. * @param array $exclude Exclude these IDs from results. 
  93. * @param array $meta_key Limit to users with a meta_key. 
  94. * @param array $meta_value Limit to users with a meta_value (with meta_key). 
  95. * @param string $page_arg Optional. The string used as a query parameter in pagination links. 
  96. * Default: 'upage'. 
  97. * @param array|string $member_type Array or comma-separated string of member types to limit results to. 
  98. * @param array|string $member_type__in Array or comma-separated string of member types to limit results to. 
  99. * @param array|string $member_type__not_in Array or comma-separated string of member types to exclude 
  100. * from results. 
  101. */ 
  102. 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 = '' ) { 
  103.  
  104. $this->pag_arg = sanitize_key( $page_arg ); 
  105. $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $page_number ); 
  106. $this->pag_num = bp_sanitize_pagination_arg( 'num', $per_page ); 
  107. $this->type = $type; 
  108.  
  109. if ( !empty( $_REQUEST['letter'] ) ) 
  110. $this->members = BP_Core_User::get_users_by_letter( $_REQUEST['letter'], $this->pag_num, $this->pag_page, $populate_extras, $exclude ); 
  111. else 
  112. $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 ) ); 
  113.  
  114. if ( !$max || $max >= (int) $this->members['total'] ) 
  115. $this->total_member_count = (int) $this->members['total']; 
  116. else 
  117. $this->total_member_count = (int) $max; 
  118.  
  119. $this->members = $this->members['users']; 
  120.  
  121. if ( $max ) { 
  122. if ( $max >= count( $this->members ) ) { 
  123. $this->member_count = count( $this->members ); 
  124. } else { 
  125. $this->member_count = (int) $max; 
  126. } else { 
  127. $this->member_count = count( $this->members ); 
  128.  
  129. if ( (int) $this->total_member_count && (int) $this->pag_num ) { 
  130. $pag_args = array( 
  131. $this->pag_arg => '%#%',  
  132. ); 
  133.  
  134. if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) { 
  135. $base = remove_query_arg( 's', wp_get_referer() ); 
  136. } else { 
  137. $base = ''; 
  138.  
  139. /** 
  140. * Defaults to an empty array to make sure paginate_links() 
  141. * won't add the $page_arg to the links which would break 
  142. * pagination in case JavaScript is disabled. 
  143. */ 
  144. $add_args = array(); 
  145.  
  146. if ( ! empty( $search_terms ) ) { 
  147. $query_arg = bp_core_get_component_search_query_arg( 'members' ); 
  148. $add_args[ $query_arg ] = urlencode( $search_terms ); 
  149.  
  150. $this->pag_links = paginate_links( array( 
  151. 'base' => add_query_arg( $pag_args, $base ),  
  152. 'format' => '',  
  153. 'total' => ceil( (int) $this->total_member_count / (int) $this->pag_num ),  
  154. 'current' => (int) $this->pag_page,  
  155. 'prev_text' => _x( '←', 'Member pagination previous text', 'buddypress' ),  
  156. 'next_text' => _x( '→', 'Member pagination next text', 'buddypress' ),  
  157. 'mid_size' => 1,  
  158. 'add_args' => $add_args,  
  159. ) ); 
  160.  
  161. /** 
  162. * Whether there are members available in the loop. 
  163. * @since 1.0.0 
  164. * @see bp_has_members() 
  165. * @return bool True if there are items in the loop, otherwise false. 
  166. */ 
  167. function has_members() { 
  168. if ( $this->member_count ) 
  169. return true; 
  170.  
  171. return false; 
  172.  
  173. /** 
  174. * Set up the next member and iterate index. 
  175. * @since 1.0.0 
  176. * @return object The next member to iterate over. 
  177. */ 
  178. function next_member() { 
  179. $this->current_member++; 
  180. $this->member = $this->members[$this->current_member]; 
  181.  
  182. return $this->member; 
  183.  
  184. /** 
  185. * Rewind the members and reset member index. 
  186. * @since 1.0.0 
  187. */ 
  188. function rewind_members() { 
  189. $this->current_member = -1; 
  190. if ( $this->member_count > 0 ) { 
  191. $this->member = $this->members[0]; 
  192.  
  193. /** 
  194. * Whether there are members left in the loop to iterate over. 
  195. * This method is used by {@link bp_members()} as part of the while loop 
  196. * that controls iteration inside the members loop, eg: 
  197. * while ( bp_members() ) { ... 
  198. * @since 1.2.0 
  199. * @see bp_members() 
  200. * @return bool True if there are more members to show, otherwise false. 
  201. */ 
  202. function members() { 
  203. if ( $this->current_member + 1 < $this->member_count ) { 
  204. return true; 
  205. } elseif ( $this->current_member + 1 == $this->member_count ) { 
  206.  
  207. /** 
  208. * Fires right before the rewinding of members listing. 
  209. * @since 1.5.0 
  210. */ 
  211. do_action('member_loop_end'); 
  212. // Do some cleaning up after the loop. 
  213. $this->rewind_members(); 
  214.  
  215. $this->in_the_loop = false; 
  216. return false; 
  217.  
  218. /** 
  219. * Set up the current member inside the loop. 
  220. * Used by {@link bp_the_member()} to set up the current member data 
  221. * while looping, so that template tags used during that iteration make 
  222. * reference to the current member. 
  223. * @since 1.0.0 
  224. * @see bp_the_member() 
  225. */ 
  226. function the_member() { 
  227.  
  228. $this->in_the_loop = true; 
  229. $this->member = $this->next_member(); 
  230.  
  231. // Loop has just started. 
  232. if ( 0 == $this->current_member ) { 
  233.  
  234. /** 
  235. * Fires if the current member is the first in the loop. 
  236. * @since 1.5.0 
  237. */ 
  238. do_action( 'member_loop_start' ); 
  239.