BP_Activity_Template

The main activity template loop class.

Defined (1)

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

/bp-activity/classes/class-bp-activity-template.php  
  1. class BP_Activity_Template { 
  2.  
  3. /** 
  4. * The loop iterator. 
  5. * @since 1.0.0 
  6. * @var int 
  7. */ 
  8. public $current_activity = -1; 
  9.  
  10. /** 
  11. * The activity count. 
  12. * @since 1.0.0 
  13. * @var int 
  14. */ 
  15. public $activity_count; 
  16.  
  17. /** 
  18. * The total activity count. 
  19. * @since 1.0.0 
  20. * @var int 
  21. */ 
  22. public $total_activity_count; 
  23.  
  24. /** 
  25. * Array of activities located by the query. 
  26. * @since 1.0.0 
  27. * @var array 
  28. */ 
  29. public $activities; 
  30.  
  31. /** 
  32. * The activity object currently being iterated on. 
  33. * @since 1.0.0 
  34. * @var object 
  35. */ 
  36. public $activity; 
  37.  
  38. /** 
  39. * A flag for whether the loop is currently being iterated. 
  40. * @since 1.0.0 
  41. * @var bool 
  42. */ 
  43. public $in_the_loop; 
  44.  
  45. /** 
  46. * URL parameter key for activity pagination. Default: 'acpage'. 
  47. * @since 2.1.0 
  48. * @var string 
  49. */ 
  50. public $pag_arg; 
  51.  
  52. /** 
  53. * The page number being requested. 
  54. * @since 1.0.0 
  55. * @var int 
  56. */ 
  57. public $pag_page; 
  58.  
  59. /** 
  60. * The number of items being requested per page. 
  61. * @since 1.0.0 
  62. * @var int 
  63. */ 
  64. public $pag_num; 
  65.  
  66. /** 
  67. * An HTML string containing pagination links. 
  68. * @since 1.0.0 
  69. * @var string 
  70. */ 
  71. public $pag_links; 
  72.  
  73. /** 
  74. * The displayed user's full name. 
  75. * @since 1.0.0 
  76. * @var string 
  77. */ 
  78. public $full_name; 
  79.  
  80. /** 
  81. * Constructor method. 
  82. * The arguments passed to this class constructor are of the same 
  83. * format as {@link BP_Activity_Activity::get()}. 
  84. * @since 1.5.0 
  85. * @see BP_Activity_Activity::get() for a description of the argument 
  86. * structure, as well as default values. 
  87. * @param array $args { 
  88. * Array of arguments. Supports all arguments from 
  89. * BP_Activity_Activity::get(), as well as 'page_arg' and 
  90. * 'include'. Default values for 'per_page' and 'display_comments' 
  91. * differ from the originating function, and are described below. 
  92. * @type string $page_arg The string used as a query parameter in 
  93. * pagination links. Default: 'acpage'. 
  94. * @type array|bool $include Pass an array of activity IDs to 
  95. * retrieve only those items, or false to noop the 'include' 
  96. * parameter. 'include' differs from 'in' in that 'in' forms 
  97. * an IN clause that works in conjunction with other filters 
  98. * passed to the function, while 'include' is interpreted as 
  99. * an exact list of items to retrieve, which skips all other 
  100. * filter-related parameters. Default: false. 
  101. * @type int|bool $per_page Default: 20. 
  102. * @type string|bool $display_comments Default: 'threaded'. 
  103. * } 
  104. */ 
  105. public function __construct( $args ) { 
  106. $bp = buddypress(); 
  107.  
  108. // Backward compatibility with old method of passing arguments. 
  109. if ( !is_array( $args ) || func_num_args() > 1 ) { 
  110. _deprecated_argument( __METHOD__, '1.6', 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__ ) ); 
  111.  
  112. $old_args_keys = array( 
  113. 0 => 'page',  
  114. 1 => 'per_page',  
  115. 2 => 'max',  
  116. 3 => 'include',  
  117. 4 => 'sort',  
  118. 5 => 'filter',  
  119. 6 => 'search_terms',  
  120. 7 => 'display_comments',  
  121. 8 => 'show_hidden',  
  122. 9 => 'exclude',  
  123. 10 => 'in',  
  124. 11 => 'spam',  
  125. 12 => 'page_arg' 
  126. ); 
  127.  
  128. $args = bp_core_parse_args_array( $old_args_keys, func_get_args() ); 
  129.  
  130. $defaults = array( 
  131. 'page' => 1,  
  132. 'per_page' => 20,  
  133. 'page_arg' => 'acpage',  
  134. 'max' => false,  
  135. 'fields' => 'all',  
  136. 'count_total' => false,  
  137. 'sort' => false,  
  138. 'include' => false,  
  139. 'exclude' => false,  
  140. 'in' => false,  
  141. 'filter' => false,  
  142. 'scope' => false,  
  143. 'search_terms' => false,  
  144. 'meta_query' => false,  
  145. 'date_query' => false,  
  146. 'filter_query' => false,  
  147. 'display_comments' => 'threaded',  
  148. 'show_hidden' => false,  
  149. 'spam' => 'ham_only',  
  150. 'update_meta_cache' => true,  
  151. ); 
  152. $r = wp_parse_args( $args, $defaults ); 
  153. extract( $r ); 
  154.  
  155. $this->pag_arg = sanitize_key( $r['page_arg'] ); 
  156. $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page'] ); 
  157. $this->pag_num = bp_sanitize_pagination_arg( 'num', $r['per_page'] ); 
  158.  
  159. // Check if blog/forum replies are disabled. 
  160. $this->disable_blogforum_replies = (bool) bp_core_get_root_option( 'bp-disable-blogforum-comments' ); 
  161.  
  162. // Get an array of the logged in user's favorite activities. 
  163. $this->my_favs = maybe_unserialize( bp_get_user_meta( bp_loggedin_user_id(), 'bp_favorite_activities', true ) ); 
  164.  
  165. // Fetch specific activity items based on ID's. 
  166. if ( !empty( $include ) ) { 
  167. $this->activities = bp_activity_get_specific( array( 
  168. 'activity_ids' => explode( ', ', $include ),  
  169. 'max' => $max,  
  170. 'count_total' => $count_total,  
  171. 'page' => $this->pag_page,  
  172. 'per_page' => $this->pag_num,  
  173. 'sort' => $sort,  
  174. 'display_comments' => $display_comments,  
  175. 'show_hidden' => $show_hidden,  
  176. 'spam' => $spam,  
  177. 'update_meta_cache' => $update_meta_cache,  
  178. ) ); 
  179.  
  180. // Fetch all activity items. 
  181. } else { 
  182. $this->activities = bp_activity_get( array( 
  183. 'display_comments' => $display_comments,  
  184. 'max' => $max,  
  185. 'count_total' => $count_total,  
  186. 'per_page' => $this->pag_num,  
  187. 'page' => $this->pag_page,  
  188. 'sort' => $sort,  
  189. 'search_terms' => $search_terms,  
  190. 'meta_query' => $meta_query,  
  191. 'date_query' => $date_query,  
  192. 'filter_query' => $filter_query,  
  193. 'filter' => $filter,  
  194. 'scope' => $scope,  
  195. 'show_hidden' => $show_hidden,  
  196. 'exclude' => $exclude,  
  197. 'in' => $in,  
  198. 'spam' => $spam,  
  199. 'update_meta_cache' => $update_meta_cache,  
  200. ) ); 
  201.  
  202. // The total_activity_count property will be set only if a 
  203. // 'count_total' query has taken place. 
  204. if ( ! is_null( $this->activities['total'] ) ) { 
  205. if ( ! $max || $max >= (int) $this->activities['total'] ) { 
  206. $this->total_activity_count = (int) $this->activities['total']; 
  207. } else { 
  208. $this->total_activity_count = (int) $max; 
  209.  
  210. $this->has_more_items = $this->activities['has_more_items']; 
  211.  
  212. $this->activities = $this->activities['activities']; 
  213.  
  214. if ( $max ) { 
  215. if ( $max >= count($this->activities) ) { 
  216. $this->activity_count = count( $this->activities ); 
  217. } else { 
  218. $this->activity_count = (int) $max; 
  219. } else { 
  220. $this->activity_count = count( $this->activities ); 
  221.  
  222. $this->full_name = bp_get_displayed_user_fullname(); 
  223.  
  224. // Fetch parent content for activity comments so we do not have to query in the loop. 
  225. foreach ( (array) $this->activities as $activity ) { 
  226. if ( 'activity_comment' != $activity->type ) { 
  227. continue; 
  228.  
  229. $parent_ids[] = $activity->item_id; 
  230.  
  231. if ( !empty( $parent_ids ) ) { 
  232. $activity_parents = bp_activity_get_specific( array( 'activity_ids' => $parent_ids ) ); 
  233.  
  234. if ( !empty( $activity_parents['activities'] ) ) { 
  235. foreach( $activity_parents['activities'] as $parent ) { 
  236. $this->activity_parents[$parent->id] = $parent; 
  237.  
  238. unset( $activity_parents ); 
  239.  
  240. if ( (int) $this->total_activity_count && (int) $this->pag_num ) { 
  241. $this->pag_links = paginate_links( array( 
  242. 'base' => add_query_arg( $this->pag_arg, '%#%' ),  
  243. 'format' => '',  
  244. 'total' => ceil( (int) $this->total_activity_count / (int) $this->pag_num ),  
  245. 'current' => (int) $this->pag_page,  
  246. 'prev_text' => _x( '←', 'Activity pagination previous text', 'buddypress' ),  
  247. 'next_text' => _x( '→', 'Activity pagination next text', 'buddypress' ),  
  248. 'mid_size' => 1,  
  249. 'add_args' => array(),  
  250. ) ); 
  251.  
  252. /** 
  253. * Whether there are activity items available in the loop. 
  254. * @since 1.0.0 
  255. * @see bp_has_activities() 
  256. * @return bool True if there are items in the loop, otherwise false. 
  257. */ 
  258. function has_activities() { 
  259. if ( $this->activity_count ) { 
  260. return true; 
  261.  
  262. return false; 
  263.  
  264. /** 
  265. * Set up the next activity item and iterate index. 
  266. * @since 1.0.0 
  267. * @return object The next activity item to iterate over. 
  268. */ 
  269. public function next_activity() { 
  270. $this->current_activity++; 
  271. $this->activity = $this->activities[ $this->current_activity ]; 
  272.  
  273. return $this->activity; 
  274.  
  275. /** 
  276. * Rewind the posts and reset post index. 
  277. * @since 1.0.0 
  278. */ 
  279. public function rewind_activities() { 
  280. $this->current_activity = -1; 
  281. if ( $this->activity_count > 0 ) { 
  282. $this->activity = $this->activities[0]; 
  283.  
  284. /** 
  285. * Whether there are activity items left in the loop to iterate over. 
  286. * This method is used by {@link bp_activities()} as part of the while loop 
  287. * that controls iteration inside the activities loop, eg: 
  288. * while ( bp_activities() ) { ... 
  289. * @since 1.0.0 
  290. * @see bp_activities() 
  291. * @return bool True if there are more activity items to show,  
  292. * otherwise false. 
  293. */ 
  294. public function user_activities() { 
  295. if ( ( $this->current_activity + 1 ) < $this->activity_count ) { 
  296. return true; 
  297. } elseif ( ( $this->current_activity + 1 ) == $this->activity_count ) { 
  298.  
  299. /** 
  300. * Fires right before the rewinding of activity posts. 
  301. * @since 1.1.0 
  302. */ 
  303. do_action( 'activity_loop_end' ); 
  304.  
  305. // Do some cleaning up after the loop. 
  306. $this->rewind_activities(); 
  307.  
  308. $this->in_the_loop = false; 
  309.  
  310. return false; 
  311.  
  312. /** 
  313. * Set up the current activity item inside the loop. 
  314. * Used by {@link bp_the_activity()} to set up the current activity item 
  315. * data while looping, so that template tags used during that iteration 
  316. * make reference to the current activity item. 
  317. * @since 1.0.0 
  318. * @see bp_the_activity() 
  319. */ 
  320. public function the_activity() { 
  321.  
  322. $this->in_the_loop = true; 
  323. $this->activity = $this->next_activity(); 
  324.  
  325. if ( is_array( $this->activity ) ) { 
  326. $this->activity = (object) $this->activity; 
  327.  
  328. // Loop has just started. 
  329. if ( $this->current_activity == 0 ) { 
  330.  
  331. /** 
  332. * Fires if the current activity item is the first in the activity loop. 
  333. * @since 1.1.0 
  334. */ 
  335. do_action('activity_loop_start');