/bp-groups/classes/class-bp-groups-template.php

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