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

  1. <?php 
  2. /** 
  3. * BuddyPress Groups Invitation template loop class. 
  4. * 
  5. * @package BuddyPress 
  6. * @since 1.1.0 
  7. */ 
  8.  
  9. // Exit if accessed directly. 
  10. defined( 'ABSPATH' ) || exit; 
  11.  
  12. /** 
  13. * Group invitation template loop class. 
  14. * 
  15. * @since 1.1.0 
  16. */ 
  17. class BP_Groups_Invite_Template { 
  18.  
  19. /** 
  20. * @since 1.1.0 
  21. * @var int 
  22. */ 
  23. public $current_invite = -1; 
  24.  
  25. /** 
  26. * @since 1.1.0 
  27. * @var int 
  28. */ 
  29. public $invite_count; 
  30.  
  31. /** 
  32. * @since 1.1.0 
  33. * @var array 
  34. */ 
  35. public $invites; 
  36.  
  37. /** 
  38. * @since 1.1.0 
  39. * @var object 
  40. */ 
  41. public $invite; 
  42.  
  43. /** 
  44. * @since 1.1.0 
  45. * @var bool 
  46. */ 
  47. public $in_the_loop; 
  48.  
  49. /** 
  50. * @since 1.1.0 
  51. * @var int 
  52. */ 
  53. public $pag_page; 
  54.  
  55. /** 
  56. * @since 1.1.0 
  57. * @var int 
  58. */ 
  59. public $pag_num; 
  60.  
  61. /** 
  62. * @since 1.1.0 
  63. * @var string 
  64. */ 
  65. public $pag_links; 
  66.  
  67. /** 
  68. * @since 1.1.0 
  69. * @var int 
  70. */ 
  71. public $total_invite_count; 
  72.  
  73. /** 
  74. * BP_Groups_Invite_Template constructor. 
  75. * 
  76. * @since 1.5.0 
  77. * 
  78. * @param array $args 
  79. */ 
  80. public function __construct( $args = array() ) { 
  81.  
  82. // Backward compatibility with old method of passing arguments. 
  83. if ( ! is_array( $args ) || func_num_args() > 1 ) { 
  84. _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__ ) ); 
  85.  
  86. $old_args_keys = array( 
  87. 0 => 'user_id',  
  88. 1 => 'group_id',  
  89. ); 
  90.  
  91. $args = bp_core_parse_args_array( $old_args_keys, func_get_args() ); 
  92.  
  93. $r = wp_parse_args( $args, array( 
  94. 'page' => 1,  
  95. 'per_page' => 10,  
  96. 'page_arg' => 'invitepage',  
  97. 'user_id' => bp_loggedin_user_id(),  
  98. 'group_id' => bp_get_current_group_id(),  
  99. ) ); 
  100.  
  101. $this->pag_arg = sanitize_key( $r['page_arg'] ); 
  102. $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page'] ); 
  103. $this->pag_num = bp_sanitize_pagination_arg( 'num', $r['per_page'] ); 
  104.  
  105. $iquery = new BP_Group_Member_Query( array( 
  106. 'group_id' => $r['group_id'],  
  107. 'type' => 'first_joined',  
  108. 'per_page' => $this->pag_num,  
  109. 'page' => $this->pag_page,  
  110.  
  111. // These filters ensure we get only pending invites. 
  112. 'is_confirmed' => false,  
  113. 'inviter_id' => $r['user_id'],  
  114. ) ); 
  115.  
  116. $this->invite_data = $iquery->results; 
  117. $this->total_invite_count = $iquery->total_users; 
  118. $this->invites = array_values( wp_list_pluck( $this->invite_data, 'ID' ) ); 
  119. $this->invite_count = count( $this->invites ); 
  120.  
  121. // If per_page is set to 0 (show all results), don't generate 
  122. // pag_links. 
  123. if ( ! empty( $this->pag_num ) ) { 
  124. $this->pag_links = paginate_links( array( 
  125. 'base' => add_query_arg( $this->pag_arg, '%#%' ),  
  126. 'format' => '',  
  127. 'total' => ceil( $this->total_invite_count / $this->pag_num ),  
  128. 'current' => $this->pag_page,  
  129. 'prev_text' => '←',  
  130. 'next_text' => '→',  
  131. 'mid_size' => 1,  
  132. 'add_args' => array(),  
  133. ) ); 
  134. } else { 
  135. $this->pag_links = ''; 
  136.  
  137. /** 
  138. * Whether or not there are invites to show. 
  139. * 
  140. * @since 1.1.0 
  141. * 
  142. * @return bool 
  143. */ 
  144. public function has_invites() { 
  145. if ( ! empty( $this->invite_count ) ) { 
  146. return true; 
  147.  
  148. return false; 
  149.  
  150. /** 
  151. * Increments up to the next invite to show. 
  152. * 
  153. * @since 1.1.0 
  154. * 
  155. * @return object 
  156. */ 
  157. public function next_invite() { 
  158. $this->current_invite++; 
  159. $this->invite = $this->invites[ $this->current_invite ]; 
  160.  
  161. return $this->invite; 
  162.  
  163. /** 
  164. * Rewinds to the first invite to show. 
  165. * 
  166. * @since 1.1.0 
  167. */ 
  168. public function rewind_invites() { 
  169. $this->current_invite = -1; 
  170. if ( $this->invite_count > 0 ) { 
  171. $this->invite = $this->invites[0]; 
  172.  
  173. /** 
  174. * Finishes up the invites to show. 
  175. * 
  176. * @since 1.1.0 
  177. * 
  178. * @return bool 
  179. */ 
  180. public function invites() { 
  181. $tick = intval( $this->current_invite + 1 ); 
  182. if ( $tick < $this->invite_count ) { 
  183. return true; 
  184. } elseif ( $tick == $this->invite_count ) { 
  185.  
  186. /** 
  187. * Fires right before the rewinding of invites list. 
  188. * 
  189. * @since 1.1.0 
  190. * @since 2.3.0 `$this` parameter added. 
  191. * @since 2.7.0 Action renamed from `loop_start`. 
  192. * 
  193. * @param BP_Groups_Invite_Template $this Instance of the current Invites template. 
  194. */ 
  195. do_action( 'group_invitation_loop_end', $this ); 
  196.  
  197. // Do some cleaning up after the loop 
  198. $this->rewind_invites(); 
  199.  
  200. $this->in_the_loop = false; 
  201. return false; 
  202.  
  203. /** 
  204. * Sets up the invite to show. 
  205. * 
  206. * @since 1.1.0 
  207. */ 
  208. public function the_invite() { 
  209. global $group_id; 
  210.  
  211. $this->in_the_loop = true; 
  212. $user_id = $this->next_invite(); 
  213.  
  214. $this->invite = new stdClass; 
  215. $this->invite->user = $this->invite_data[ $user_id ]; 
  216.  
  217. // This method previously populated the user object with 
  218. // BP_Core_User. We manually configure BP_Core_User data for 
  219. // backward compatibility. 
  220. if ( bp_is_active( 'xprofile' ) ) { 
  221. $this->invite->user->profile_data = BP_XProfile_ProfileData::get_all_for_user( $user_id ); 
  222.  
  223. $this->invite->user->avatar = bp_core_fetch_avatar( array( 'item_id' => $user_id, 'type' => 'full', 'alt' => sprintf( __( 'Profile photo of %s', 'buddypress' ), $this->invite->user->fullname ) ) ); 
  224. $this->invite->user->avatar_thumb = bp_core_fetch_avatar( array( 'item_id' => $user_id, 'type' => 'thumb', 'alt' => sprintf( __( 'Profile photo of %s', 'buddypress' ), $this->invite->user->fullname ) ) ); 
  225. $this->invite->user->avatar_mini = bp_core_fetch_avatar( array( 'item_id' => $user_id, 'type' => 'thumb', 'alt' => sprintf( __( 'Profile photo of %s', 'buddypress' ), $this->invite->user->fullname ), 'width' => 30, 'height' => 30 ) ); 
  226. $this->invite->user->email = $this->invite->user->user_email; 
  227. $this->invite->user->user_url = bp_core_get_user_domain( $user_id, $this->invite->user->user_nicename, $this->invite->user->user_login ); 
  228. $this->invite->user->user_link = "<a href='{$this->invite->user->user_url}' title='{$this->invite->user->fullname}'>{$this->invite->user->fullname}</a>"; 
  229. $this->invite->user->last_active = bp_core_get_last_activity( $this->invite->user->last_activity, __( 'active %s', 'buddypress' ) ); 
  230.  
  231. if ( bp_is_active( 'groups' ) ) { 
  232. $total_groups = BP_Groups_Member::total_group_count( $user_id ); 
  233. $this->invite->user->total_groups = sprintf( _n( '%d group', '%d groups', $total_groups, 'buddypress' ), $total_groups ); 
  234.  
  235. if ( bp_is_active( 'friends' ) ) { 
  236. $this->invite->user->total_friends = BP_Friends_Friendship::total_friend_count( $user_id ); 
  237.  
  238. $this->invite->user->total_blogs = null; 
  239.  
  240. // Global'ed in bp_group_has_invites() 
  241. $this->invite->group_id = $group_id; 
  242.  
  243. // loop has just started 
  244. if ( 0 == $this->current_invite ) { 
  245.  
  246. /** 
  247. * Fires if the current invite item is the first in the loop. 
  248. * 
  249. * @since 1.1.0 
  250. * @since 2.3.0 `$this` parameter added. 
  251. * @since 2.7.0 Action renamed from `loop_start`. 
  252. * 
  253. * @param BP_Groups_Invite_Template $this Instance of the current Invites template. 
  254. */ 
  255. do_action( 'group_invitation_loop_start', $this ); 
.