BP_Groups_Template

The main Groups template loop class.

Defined (1)

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

/bp-groups/classes/class-bp-groups-template.php  
  1. class BP_Groups_Template { 
  2.  
  3. /** 
  4. * The loop iterator. 
  5. * @var int 
  6. * @since 1.2.0 
  7. */ 
  8. public $current_group = -1; 
  9.  
  10. /** 
  11. * The number of groups returned by the paged query. 
  12. * @var int 
  13. * @since 1.2.0 
  14. */ 
  15. public $group_count; 
  16.  
  17. /** 
  18. * Array of groups located by the query. 
  19. * @var array 
  20. * @since 1.2.0 
  21. */ 
  22. public $groups; 
  23.  
  24. /** 
  25. * The group object currently being iterated on. 
  26. * @var object 
  27. * @since 1.2.0 
  28. */ 
  29. public $group; 
  30.  
  31. /** 
  32. * A flag for whether the loop is currently being iterated. 
  33. * @var bool 
  34. * @since 1.2.0 
  35. */ 
  36. public $in_the_loop; 
  37.  
  38. /** 
  39. * The page number being requested. 
  40. * @var string 
  41. * @since 1.2.0 
  42. */ 
  43. public $pag_page; 
  44.  
  45. /** 
  46. * The number of items being requested per page. 
  47. * @var string 
  48. * @since 1.2.0 
  49. */ 
  50. public $pag_num; 
  51.  
  52. /** 
  53. * An HTML string containing pagination links. 
  54. * @var string 
  55. * @since 1.2.0 
  56. */ 
  57. public $pag_links; 
  58.  
  59. /** 
  60. * The total number of groups matching the query parameters. 
  61. * @var int 
  62. * @since 1.2.0 
  63. */ 
  64. public $total_group_count; 
  65.  
  66. /** 
  67. * Whether the template loop is for a single group page. 
  68. * @var bool 
  69. * @since 1.2.0 
  70. */ 
  71. public $single_group = false; 
  72.  
  73. /** 
  74. * Field to sort by. 
  75. * @var string 
  76. * @since 1.2.0 
  77. */ 
  78. public $sort_by; 
  79.  
  80. /** 
  81. * Sort order. 
  82. * @var string 
  83. * @since 1.2.0 
  84. */ 
  85. public $order; 
  86.  
  87. /** 
  88. * Constructor method. 
  89. * @see BP_Groups_Group::get() for an in-depth description of arguments. 
  90. * @param array $args { 
  91. * Array of arguments. Accepts all arguments accepted by 
  92. * {@link BP_Groups_Group::get()}. In cases where the default 
  93. * values of the params differ, they have been discussed below. 
  94. * @type int $per_page Default: 20. 
  95. * @type int $page Default: 1. 
  96. * } 
  97. */ 
  98. function __construct( $args = array() ) { 
  99.  
  100. // Backward compatibility with old method of passing arguments. 
  101. if ( ! is_array( $args ) || func_num_args() > 1 ) { 
  102. _deprecated_argument( __METHOD__, '1.7', 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__ ) ); 
  103.  
  104. $old_args_keys = array( 
  105. 0 => 'user_id',  
  106. 1 => 'type',  
  107. 2 => 'page',  
  108. 3 => 'per_page',  
  109. 4 => 'max',  
  110. 5 => 'slug',  
  111. 6 => 'search_terms',  
  112. 7 => 'populate_extras',  
  113. 8 => 'include',  
  114. 9 => 'exclude',  
  115. 10 => 'show_hidden',  
  116. 11 => 'page_arg',  
  117. ); 
  118.  
  119. $args = bp_core_parse_args_array( $old_args_keys, func_get_args() ); 
  120.  
  121. $defaults = array( 
  122. 'page' => 1,  
  123. 'per_page' => 20,  
  124. 'page_arg' => 'grpage',  
  125. 'max' => false,  
  126. 'type' => 'active',  
  127. 'order' => 'DESC',  
  128. 'orderby' => 'date_created',  
  129. 'show_hidden' => false,  
  130. 'user_id' => 0,  
  131. 'slug' => false,  
  132. 'include' => false,  
  133. 'exclude' => false,  
  134. 'parent_id' => null,  
  135. 'search_terms' => '',  
  136. 'search_columns' => array(),  
  137. 'group_type' => '',  
  138. 'group_type__in' => '',  
  139. 'group_type__not_in' => '',  
  140. 'meta_query' => false,  
  141. 'update_meta_cache' => true,  
  142. 'update_admin_cache' => false,  
  143. ); 
  144.  
  145. $r = wp_parse_args( $args, $defaults ); 
  146. extract( $r ); 
  147.  
  148. $this->pag_arg = sanitize_key( $r['page_arg'] ); 
  149. $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page'] ); 
  150. $this->pag_num = bp_sanitize_pagination_arg( 'num', $r['per_page'] ); 
  151.  
  152. if ( bp_current_user_can( 'bp_moderate' ) || ( is_user_logged_in() && $user_id == bp_loggedin_user_id() ) ) { 
  153. $show_hidden = true; 
  154.  
  155. if ( 'invites' == $type ) { 
  156. $this->groups = groups_get_invites_for_user( $user_id, $this->pag_num, $this->pag_page, $exclude ); 
  157. } elseif ( 'single-group' == $type ) { 
  158. $this->single_group = true; 
  159.  
  160. if ( groups_get_current_group() ) { 
  161. $group = groups_get_current_group(); 
  162.  
  163. } else { 
  164. $group = groups_get_group( BP_Groups_Group::get_id_from_slug( $r['slug'] ) ); 
  165.  
  166. // Backwards compatibility - the 'group_id' variable is not part of the 
  167. // BP_Groups_Group object, but we add it here for devs doing checks against it 
  168. // 
  169. // @see https://buddypress.trac.wordpress.org/changeset/3540 
  170. // 
  171. // this is subject to removal in a future release; devs should check against 
  172. // $group->id instead. 
  173. $group->group_id = $group->id; 
  174.  
  175. $this->groups = array( $group ); 
  176.  
  177. } else { 
  178. $this->groups = groups_get_groups( array( 
  179. 'type' => $type,  
  180. 'order' => $order,  
  181. 'orderby' => $orderby,  
  182. 'per_page' => $this->pag_num,  
  183. 'page' => $this->pag_page,  
  184. 'user_id' => $user_id,  
  185. 'search_terms' => $search_terms,  
  186. 'search_columns' => $search_columns,  
  187. 'meta_query' => $meta_query,  
  188. 'group_type' => $group_type,  
  189. 'group_type__in' => $group_type__in,  
  190. 'group_type__not_in' => $group_type__not_in,  
  191. 'include' => $include,  
  192. 'exclude' => $exclude,  
  193. 'parent_id' => $parent_id,  
  194. 'update_meta_cache' => $update_meta_cache,  
  195. 'update_admin_cache' => $update_admin_cache,  
  196. 'show_hidden' => $show_hidden,  
  197. ) ); 
  198.  
  199. if ( 'invites' == $type ) { 
  200. $this->total_group_count = (int) $this->groups['total']; 
  201. $this->group_count = (int) $this->groups['total']; 
  202. $this->groups = $this->groups['groups']; 
  203. } elseif ( 'single-group' == $type ) { 
  204. if ( empty( $group->id ) ) { 
  205. $this->total_group_count = 0; 
  206. $this->group_count = 0; 
  207. } else { 
  208. $this->total_group_count = 1; 
  209. $this->group_count = 1; 
  210. } else { 
  211. if ( empty( $max ) || $max >= (int) $this->groups['total'] ) { 
  212. $this->total_group_count = (int) $this->groups['total']; 
  213. } else { 
  214. $this->total_group_count = (int) $max; 
  215.  
  216. $this->groups = $this->groups['groups']; 
  217.  
  218. if ( !empty( $max ) ) { 
  219. if ( $max >= count( $this->groups ) ) { 
  220. $this->group_count = count( $this->groups ); 
  221. } else { 
  222. $this->group_count = (int) $max; 
  223. } else { 
  224. $this->group_count = count( $this->groups ); 
  225.  
  226. // Build pagination links. 
  227. if ( (int) $this->total_group_count && (int) $this->pag_num ) { 
  228. $pag_args = array( 
  229. $this->pag_arg => '%#%' 
  230. ); 
  231.  
  232. if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) { 
  233. $base = remove_query_arg( 's', wp_get_referer() ); 
  234. } else { 
  235. $base = ''; 
  236.  
  237. $add_args = array( 
  238. 'num' => $this->pag_num,  
  239. 'sortby' => $this->sort_by,  
  240. 'order' => $this->order,  
  241. ); 
  242.  
  243. if ( ! empty( $search_terms ) ) { 
  244. $query_arg = bp_core_get_component_search_query_arg( 'groups' ); 
  245. $add_args[ $query_arg ] = urlencode( $search_terms ); 
  246.  
  247. $this->pag_links = paginate_links( array( 
  248. 'base' => add_query_arg( $pag_args, $base ),  
  249. 'format' => '',  
  250. 'total' => ceil( (int) $this->total_group_count / (int) $this->pag_num ),  
  251. 'current' => $this->pag_page,  
  252. 'prev_text' => _x( '←', 'Group pagination previous text', 'buddypress' ),  
  253. 'next_text' => _x( '→', 'Group pagination next text', 'buddypress' ),  
  254. 'mid_size' => 1,  
  255. 'add_args' => $add_args,  
  256. ) ); 
  257.  
  258. /** 
  259. * Whether there are groups available in the loop. 
  260. * @since 1.2.0 
  261. * @see bp_has_groups() 
  262. * @return bool True if there are items in the loop, otherwise false. 
  263. */ 
  264. function has_groups() { 
  265. if ( $this->group_count ) { 
  266. return true; 
  267.  
  268. return false; 
  269.  
  270. /** 
  271. * Set up the next group and iterate index. 
  272. * @since 1.2.0 
  273. * @return object The next group to iterate over. 
  274. */ 
  275. function next_group() { 
  276. $this->current_group++; 
  277. $this->group = $this->groups[$this->current_group]; 
  278.  
  279. return $this->group; 
  280.  
  281. /** 
  282. * Rewind the groups and reset member index. 
  283. * @since 1.2.0 
  284. */ 
  285. function rewind_groups() { 
  286. $this->current_group = -1; 
  287. if ( $this->group_count > 0 ) { 
  288. $this->group = $this->groups[0]; 
  289.  
  290. /** 
  291. * Whether there are groups left in the loop to iterate over. 
  292. * This method is used by {@link bp_groups()} as part of the while loop 
  293. * that controls iteration inside the groups loop, eg: 
  294. * while ( bp_groups() ) { ... 
  295. * @since 1.2.0 
  296. * @see bp_groups() 
  297. * @return bool True if there are more groups to show, otherwise false. 
  298. */ 
  299. function groups() { 
  300. if ( $this->current_group + 1 < $this->group_count ) { 
  301. return true; 
  302. } elseif ( $this->current_group + 1 == $this->group_count ) { 
  303.  
  304. /** 
  305. * Fires right before the rewinding of groups list. 
  306. * @since 1.5.0 
  307. */ 
  308. do_action('group_loop_end'); 
  309. // Do some cleaning up after the loop. 
  310. $this->rewind_groups(); 
  311.  
  312. $this->in_the_loop = false; 
  313. return false; 
  314.  
  315. /** 
  316. * Set up the current group inside the loop. 
  317. * Used by {@link bp_the_group()} to set up the current group data 
  318. * while looping, so that template tags used during that iteration make 
  319. * reference to the current member. 
  320. * @since 1.2.0 
  321. * @see bp_the_group() 
  322. */ 
  323. function the_group() { 
  324. $this->in_the_loop = true; 
  325. $this->group = $this->next_group(); 
  326.  
  327. if ( 0 == $this->current_group ) { 
  328.  
  329. /** 
  330. * Fires if the current group item is the first in the loop. 
  331. * @since 1.1.0 
  332. */ 
  333. do_action( 'group_loop_start' );