/bp-members/classes/class-bp-core-members-template.php

  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. } else { 
  159. $this->member_count = count( $this->members ); 
  160.  
  161. if ( (int) $this->total_member_count && (int) $this->pag_num ) { 
  162. $pag_args = array( 
  163. $this->pag_arg => '%#%',  
  164. ); 
  165.  
  166. if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) { 
  167. $base = remove_query_arg( 's', wp_get_referer() ); 
  168. } else { 
  169. $base = ''; 
  170.  
  171. /** 
  172. * Defaults to an empty array to make sure paginate_links() 
  173. * won't add the $page_arg to the links which would break 
  174. * pagination in case JavaScript is disabled. 
  175. */ 
  176. $add_args = array(); 
  177.  
  178. if ( ! empty( $search_terms ) ) { 
  179. $query_arg = bp_core_get_component_search_query_arg( 'members' ); 
  180. $add_args[ $query_arg ] = urlencode( $search_terms ); 
  181.  
  182. $this->pag_links = paginate_links( array( 
  183. 'base' => add_query_arg( $pag_args, $base ),  
  184. 'format' => '',  
  185. 'total' => ceil( (int) $this->total_member_count / (int) $this->pag_num ),  
  186. 'current' => (int) $this->pag_page,  
  187. 'prev_text' => _x( '←', 'Member pagination previous text', 'buddypress' ),  
  188. 'next_text' => _x( '→', 'Member pagination next text', 'buddypress' ),  
  189. 'mid_size' => 1,  
  190. 'add_args' => $add_args,  
  191. ) ); 
  192.  
  193. /** 
  194. * Whether there are members available in the loop. 
  195. * 
  196. * @since 1.0.0 
  197. * 
  198. * @see bp_has_members() 
  199. * 
  200. * @return bool True if there are items in the loop, otherwise false. 
  201. */ 
  202. function has_members() { 
  203. if ( $this->member_count ) 
  204. return true; 
  205.  
  206. return false; 
  207.  
  208. /** 
  209. * Set up the next member and iterate index. 
  210. * 
  211. * @since 1.0.0 
  212. * 
  213. * @return object The next member to iterate over. 
  214. */ 
  215. function next_member() { 
  216. $this->current_member++; 
  217. $this->member = $this->members[$this->current_member]; 
  218.  
  219. return $this->member; 
  220.  
  221. /** 
  222. * Rewind the members and reset member index. 
  223. * 
  224. * @since 1.0.0 
  225. */ 
  226. function rewind_members() { 
  227. $this->current_member = -1; 
  228. if ( $this->member_count > 0 ) { 
  229. $this->member = $this->members[0]; 
  230.  
  231. /** 
  232. * Whether there are members left in the loop to iterate over. 
  233. * 
  234. * This method is used by {@link bp_members()} as part of the while loop 
  235. * that controls iteration inside the members loop, eg: 
  236. * while ( bp_members() ) { ... 
  237. * 
  238. * @since 1.2.0 
  239. * 
  240. * @see bp_members() 
  241. * 
  242. * @return bool True if there are more members to show, otherwise false. 
  243. */ 
  244. function members() { 
  245. if ( $this->current_member + 1 < $this->member_count ) { 
  246. return true; 
  247. } elseif ( $this->current_member + 1 == $this->member_count ) { 
  248.  
  249. /** 
  250. * Fires right before the rewinding of members listing. 
  251. * 
  252. * @since 1.5.0 
  253. */ 
  254. do_action('member_loop_end'); 
  255. // Do some cleaning up after the loop. 
  256. $this->rewind_members(); 
  257.  
  258. $this->in_the_loop = false; 
  259. return false; 
  260.  
  261. /** 
  262. * Set up the current member inside the loop. 
  263. * 
  264. * Used by {@link bp_the_member()} to set up the current member data 
  265. * while looping, so that template tags used during that iteration make 
  266. * reference to the current member. 
  267. * 
  268. * @since 1.0.0 
  269. * 
  270. * @see bp_the_member() 
  271. */ 
  272. function the_member() { 
  273.  
  274. $this->in_the_loop = true; 
  275. $this->member = $this->next_member(); 
  276.  
  277. // Loop has just started. 
  278. if ( 0 == $this->current_member ) { 
  279.  
  280. /** 
  281. * Fires if the current member is the first in the loop. 
  282. * 
  283. * @since 1.5.0 
  284. */ 
  285. do_action( 'member_loop_start' ); 
  286.  
.