BP_Groups_Group_Members_Template

Group Members Loop template class.

Defined (1)

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

/bp-groups/classes/class-bp-groups-group-members-template.php  
  1. class BP_Groups_Group_Members_Template { 
  2.  
  3. /** 
  4. * @since 1.0.0 
  5. * @var int 
  6. */ 
  7. public $current_member = -1; 
  8.  
  9. /** 
  10. * @since 1.0.0 
  11. * @var int 
  12. */ 
  13. public $member_count; 
  14.  
  15. /** 
  16. * @since 1.0.0 
  17. * @var array 
  18. */ 
  19. public $members; 
  20.  
  21. /** 
  22. * @since 1.0.0 
  23. * @var object 
  24. */ 
  25. public $member; 
  26.  
  27. /** 
  28. * @since 1.0.0 
  29. * @var bool 
  30. */ 
  31. public $in_the_loop; 
  32.  
  33. /** 
  34. * @since 1.0.0 
  35. * @var int 
  36. */ 
  37. public $pag_page; 
  38.  
  39. /** 
  40. * @since 1.0.0 
  41. * @var int 
  42. */ 
  43. public $pag_num; 
  44.  
  45. /** 
  46. * @since 1.0.0 
  47. * @var array|string|void 
  48. */ 
  49. public $pag_links; 
  50.  
  51. /** 
  52. * @since 1.0.0 
  53. * @var int 
  54. */ 
  55. public $total_group_count; 
  56.  
  57. /** 
  58. * Constructor. 
  59. * @since 1.5.0 
  60. * @param array $args { 
  61. * An array of optional arguments. 
  62. * @type int $group_id ID of the group whose members are being 
  63. * queried. Default: current group ID. 
  64. * @type int $page Page of results to be queried. Default: 1. 
  65. * @type int $per_page Number of items to return per page of 
  66. * results. Default: 20. 
  67. * @type int $max Optional. Max number of items to return. 
  68. * @type array $exclude Optional. Array of user IDs to exclude. 
  69. * @type bool|int $exclude_admin_mods True (or 1) to exclude admins and mods from 
  70. * results. Default: 1. 
  71. * @type bool|int $exclude_banned True (or 1) to exclude banned users from results. 
  72. * Default: 1. 
  73. * @type array $group_role Optional. Array of group roles to include. 
  74. * @type string $search_terms Optional. Search terms to match. 
  75. * } 
  76. */ 
  77. public function __construct( $args = array() ) { 
  78.  
  79. // Backward compatibility with old method of passing arguments. 
  80. if ( ! is_array( $args ) || func_num_args() > 1 ) { 
  81. _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__ ) ); 
  82.  
  83. $old_args_keys = array( 
  84. 0 => 'group_id',  
  85. 1 => 'per_page',  
  86. 2 => 'max',  
  87. 3 => 'exclude_admins_mods',  
  88. 4 => 'exclude_banned',  
  89. 5 => 'exclude',  
  90. 6 => 'group_role',  
  91. ); 
  92.  
  93. $args = bp_core_parse_args_array( $old_args_keys, func_get_args() ); 
  94.  
  95. $r = wp_parse_args( $args, array( 
  96. 'group_id' => bp_get_current_group_id(),  
  97. 'page' => 1,  
  98. 'per_page' => 20,  
  99. 'page_arg' => 'mlpage',  
  100. 'max' => false,  
  101. 'exclude' => false,  
  102. 'exclude_admins_mods' => 1,  
  103. 'exclude_banned' => 1,  
  104. 'group_role' => false,  
  105. 'search_terms' => false,  
  106. 'type' => 'last_joined',  
  107. ) ); 
  108.  
  109. $this->pag_arg = sanitize_key( $r['page_arg'] ); 
  110. $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page'] ); 
  111. $this->pag_num = bp_sanitize_pagination_arg( 'num', $r['per_page'] ); 
  112.  
  113. /** 
  114. * Check the current group is the same as the supplied group ID. 
  115. * It can differ when using {@link bp_group_has_members()} outside the Groups screens. 
  116. */ 
  117. $current_group = groups_get_current_group(); 
  118. if ( empty( $current_group ) || ( $current_group && $current_group->id !== bp_get_current_group_id() ) ) { 
  119. $current_group = groups_get_group( $r['group_id'] ); 
  120.  
  121. // Assemble the base URL for pagination. 
  122. $base_url = trailingslashit( bp_get_group_permalink( $current_group ) . bp_current_action() ); 
  123. if ( bp_action_variable() ) { 
  124. $base_url = trailingslashit( $base_url . bp_action_variable() ); 
  125.  
  126. $members_args = $r; 
  127.  
  128. $members_args['page'] = $this->pag_page; 
  129. $members_args['per_page'] = $this->pag_num; 
  130.  
  131. // Get group members for this loop. 
  132. $this->members = groups_get_group_members( $members_args ); 
  133.  
  134. if ( empty( $r['max'] ) || ( $r['max'] >= (int) $this->members['count'] ) ) { 
  135. $this->total_member_count = (int) $this->members['count']; 
  136. } else { 
  137. $this->total_member_count = (int) $r['max']; 
  138.  
  139. // Reset members array for subsequent looping. 
  140. $this->members = $this->members['members']; 
  141.  
  142. if ( empty( $r['max'] ) || ( $r['max'] >= count( $this->members ) ) ) { 
  143. $this->member_count = (int) count( $this->members ); 
  144. } else { 
  145. $this->member_count = (int) $r['max']; 
  146.  
  147. $this->pag_links = paginate_links( array( 
  148. 'base' => add_query_arg( array( $this->pag_arg => '%#%' ), $base_url ),  
  149. 'format' => '',  
  150. 'total' => ! empty( $this->pag_num ) ? ceil( $this->total_member_count / $this->pag_num ) : $this->total_member_count,  
  151. 'current' => $this->pag_page,  
  152. 'prev_text' => '←',  
  153. 'next_text' => '→',  
  154. 'mid_size' => 1,  
  155. 'add_args' => array(),  
  156. ) ); 
  157.  
  158. /** 
  159. * Whether or not there are members to display. 
  160. * @since 1.0.0 
  161. * @return bool 
  162. */ 
  163. public function has_members() { 
  164. if ( ! empty( $this->member_count ) ) { 
  165. return true; 
  166.  
  167. return false; 
  168.  
  169. /** 
  170. * Increments to the next member to display. 
  171. * @since 1.0.0 
  172. * @return object 
  173. */ 
  174. public function next_member() { 
  175. $this->current_member++; 
  176. $this->member = $this->members[ $this->current_member ]; 
  177.  
  178. return $this->member; 
  179.  
  180. /** 
  181. * Rewinds to the first member to display. 
  182. * @since 1.0.0 
  183. */ 
  184. public function rewind_members() { 
  185. $this->current_member = -1; 
  186. if ( $this->member_count > 0 ) { 
  187. $this->member = $this->members[0]; 
  188.  
  189. /** 
  190. * Finishes up the members for display. 
  191. * @since 1.0.0 
  192. * @return bool 
  193. */ 
  194. public function members() { 
  195. $tick = intval( $this->current_member + 1 ); 
  196. if ( $tick < $this->member_count ) { 
  197. return true; 
  198. } elseif ( $tick == $this->member_count ) { 
  199.  
  200. /** 
  201. * Fires right before the rewinding of members list. 
  202. * @since 1.0.0 
  203. * @since 2.3.0 `$this` parameter added. 
  204. * @since 2.7.0 Action renamed from `loop_end`. 
  205. * @param BP_Groups_Group_Members_Template $this Instance of the current Members template. 
  206. */ 
  207. do_action( 'group_members_loop_end', $this ); 
  208.  
  209. // Do some cleaning up after the loop. 
  210. $this->rewind_members(); 
  211.  
  212. $this->in_the_loop = false; 
  213. return false; 
  214.  
  215. /** 
  216. * Sets up the member to display. 
  217. * @since 1.0.0 
  218. */ 
  219. public function the_member() { 
  220. $this->in_the_loop = true; 
  221. $this->member = $this->next_member(); 
  222.  
  223. // Loop has just started. 
  224. if ( 0 == $this->current_member ) { 
  225.  
  226. /** 
  227. * Fires if the current member item is the first in the members list. 
  228. * @since 1.0.0 
  229. * @since 2.3.0 `$this` parameter added. 
  230. * @since 2.7.0 Action renamed from `loop_start`. 
  231. * @param BP_Groups_Group_Members_Template $this Instance of the current Members template. 
  232. */ 
  233. do_action( 'group_members_loop_start', $this );