/bp-friends/bp-friends-activity.php

  1. <?php 
  2. /** 
  3. * BuddyPress Friends Activity Functions. 
  4. * 
  5. * These functions handle the recording, deleting and formatting of activity 
  6. * for the user and for this specific component. 
  7. * 
  8. * @package BuddyPress 
  9. * @subpackage FriendsActivity 
  10. * @since 1.5.0 
  11. */ 
  12.  
  13. // Exit if accessed directly. 
  14. defined( 'ABSPATH' ) || exit; 
  15.  
  16. /** 
  17. * Record an activity item related to the Friends component. 
  18. * 
  19. * A wrapper for {@link bp_activity_add()} that provides some Friends-specific 
  20. * defaults. 
  21. * 
  22. * @since 1.0.0 
  23. * 
  24. * @see bp_activity_add() for more detailed description of parameters and 
  25. * return values. 
  26. * 
  27. * @param array|string $args { 
  28. * An array of arguments for the new activity item. Accepts all parameters 
  29. * of {@link bp_activity_add()}. The one difference is the following 
  30. * argument, which has a different default here: 
  31. * @type string $component Default: the id of your Friends component 
  32. * (usually 'friends'). 
  33. * } 
  34. * @return bool See {@link bp_activity_add()}. 
  35. */ 
  36. function friends_record_activity( $args = '' ) { 
  37.  
  38. if ( ! bp_is_active( 'activity' ) ) { 
  39. return false; 
  40.  
  41. $r = wp_parse_args( $args, array( 
  42. 'user_id' => bp_loggedin_user_id(),  
  43. 'action' => '',  
  44. 'content' => '',  
  45. 'primary_link' => '',  
  46. 'component' => buddypress()->friends->id,  
  47. 'type' => false,  
  48. 'item_id' => false,  
  49. 'secondary_item_id' => false,  
  50. 'recorded_time' => bp_core_current_time(),  
  51. 'hide_sitewide' => false 
  52. ) ); 
  53.  
  54. return bp_activity_add( $r ); 
  55.  
  56. /** 
  57. * Delete an activity item related to the Friends component. 
  58. * 
  59. * @since 1.0.0 
  60. * 
  61. * @param array $args { 
  62. * An array of arguments for the item to delete. 
  63. * @type int $item_id ID of the 'item' associated with the activity item. 
  64. * For Friends activity items, this is usually the user ID of one 
  65. * of the friends. 
  66. * @type string $type The 'type' of the activity item (eg 
  67. * 'friendship_accepted'). 
  68. * @type int $user_id ID of the user associated with the activity item. 
  69. * } 
  70. * @return bool True on success, false on failure. 
  71. */ 
  72. function friends_delete_activity( $args ) { 
  73. if ( ! bp_is_active( 'activity' ) ) { 
  74. return; 
  75.  
  76. bp_activity_delete_by_item_id( array( 
  77. 'component' => buddypress()->friends->id,  
  78. 'item_id' => $args['item_id'],  
  79. 'type' => $args['type'],  
  80. 'user_id' => $args['user_id'] 
  81. ) ); 
  82.  
  83. /** 
  84. * Register the activity actions for bp-friends. 
  85. * 
  86. * @since 1.1.0 
  87. */ 
  88. function friends_register_activity_actions() { 
  89.  
  90. if ( !bp_is_active( 'activity' ) ) { 
  91. return false; 
  92.  
  93. $bp = buddypress(); 
  94.  
  95. // These two added in BP 1.6. 
  96. bp_activity_set_action( 
  97. $bp->friends->id,  
  98. 'friendship_accepted',  
  99. __( 'Friendships accepted', 'buddypress' ),  
  100. 'bp_friends_format_activity_action_friendship_accepted',  
  101. __( 'Friendships', 'buddypress' ),  
  102. array( 'activity', 'member' ) 
  103. ); 
  104.  
  105. bp_activity_set_action( 
  106. $bp->friends->id,  
  107. 'friendship_created',  
  108. __( 'New friendships', 'buddypress' ),  
  109. 'bp_friends_format_activity_action_friendship_created',  
  110. __( 'Friendships', 'buddypress' ),  
  111. array( 'activity', 'member' ) 
  112. ); 
  113.  
  114. // < BP 1.6 backpat. 
  115. bp_activity_set_action( $bp->friends->id, 'friends_register_activity_action', __( 'New friendship created', 'buddypress' ) ); 
  116.  
  117. /** 
  118. * Fires after all default bp-friends activity actions have been registered. 
  119. * 
  120. * @since 1.1.0 
  121. */ 
  122. do_action( 'friends_register_activity_actions' ); 
  123. add_action( 'bp_register_activity_actions', 'friends_register_activity_actions' ); 
  124.  
  125. /** 
  126. * Format 'friendship_accepted' activity actions. 
  127. * 
  128. * @since 2.0.0 
  129. * 
  130. * @param string $action Activity action string. 
  131. * @param object $activity Activity data. 
  132. * @return string $action Formatted activity action. 
  133. */ 
  134. function bp_friends_format_activity_action_friendship_accepted( $action, $activity ) { 
  135. $initiator_link = bp_core_get_userlink( $activity->user_id ); 
  136. $friend_link = bp_core_get_userlink( $activity->secondary_item_id ); 
  137.  
  138. $action = sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $initiator_link, $friend_link ); 
  139.  
  140. // Backward compatibility for legacy filter 
  141. // The old filter has the $friendship object passed to it. We want to 
  142. // avoid having to build this object if it's not necessary. 
  143. if ( has_filter( 'friends_activity_friendship_accepted_action' ) ) { 
  144. $friendship = new BP_Friends_Friendship( $activity->item_id ); 
  145. $action = apply_filters( 'friends_activity_friendsip_accepted_action', $action, $friendship ); 
  146.  
  147. /** 
  148. * Filters the 'friendship_accepted' activity action format. 
  149. * 
  150. * @since 2.0.0 
  151. * 
  152. * @param string $action String text for the 'friendship_accepted' action. 
  153. * @param object $activity Activity data. 
  154. */ 
  155. return apply_filters( 'bp_friends_format_activity_action_friendship_accepted', $action, $activity ); 
  156.  
  157. /** 
  158. * Format 'friendship_created' activity actions. 
  159. * 
  160. * @since 2.0.0 
  161. * 
  162. * @param string $action Static activity action. 
  163. * @param object $activity Activity data. 
  164. * @return string $action Formatted activity action. 
  165. */ 
  166. function bp_friends_format_activity_action_friendship_created( $action, $activity ) { 
  167. $initiator_link = bp_core_get_userlink( $activity->user_id ); 
  168. $friend_link = bp_core_get_userlink( $activity->secondary_item_id ); 
  169.  
  170. $action = sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $initiator_link, $friend_link ); 
  171.  
  172. // Backward compatibility for legacy filter 
  173. // The old filter has the $friendship object passed to it. We want to 
  174. // avoid having to build this object if it's not necessary. 
  175. if ( has_filter( 'friends_activity_friendship_accepted_action' ) ) { 
  176. $friendship = new BP_Friends_Friendship( $activity->item_id ); 
  177. $action = apply_filters( 'friends_activity_friendsip_accepted_action', $action, $friendship ); 
  178.  
  179. /** 
  180. * Filters the 'friendship_created' activity action format. 
  181. * 
  182. * @since 2.0.0 
  183. * 
  184. * @param string $action String text for the 'friendship_created' action. 
  185. * @param object $activity Activity data. 
  186. */ 
  187. return apply_filters( 'bp_friends_format_activity_action_friendship_created', $action, $activity ); 
  188.  
  189. /** 
  190. * Fetch data related to friended users at the beginning of an activity loop. 
  191. * 
  192. * This reduces database overhead during the activity loop. 
  193. * 
  194. * @since 2.0.0 
  195. * 
  196. * @param array $activities Array of activity items. 
  197. * @return array 
  198. */ 
  199. function bp_friends_prefetch_activity_object_data( $activities ) { 
  200. if ( empty( $activities ) ) { 
  201. return $activities; 
  202.  
  203. $friend_ids = array(); 
  204.  
  205. foreach ( $activities as $activity ) { 
  206. if ( buddypress()->friends->id !== $activity->component ) { 
  207. continue; 
  208.  
  209. $friend_ids[] = $activity->secondary_item_id; 
  210.  
  211. if ( ! empty( $friend_ids ) ) { 
  212. // Fire a user query to prime user caches. 
  213. new BP_User_Query( array( 
  214. 'user_ids' => $friend_ids,  
  215. 'populate_extras' => false,  
  216. 'update_meta_cache' => false,  
  217. ) ); 
  218.  
  219. return $activities; 
  220. add_filter( 'bp_activity_prefetch_object_data', 'bp_friends_prefetch_activity_object_data' ); 
  221.  
  222. /** 
  223. * Set up activity arguments for use with the 'friends' scope. 
  224. * 
  225. * For details on the syntax, see {@link BP_Activity_Query}. 
  226. * 
  227. * @since 2.2.0 
  228. * 
  229. * @param array $retval Empty array by default. 
  230. * @param array $filter Current activity arguments. 
  231. * @return array 
  232. */ 
  233. function bp_friends_filter_activity_scope( $retval = array(), $filter = array() ) { 
  234.  
  235. // Determine the user_id. 
  236. if ( ! empty( $filter['user_id'] ) ) { 
  237. $user_id = $filter['user_id']; 
  238. } else { 
  239. $user_id = bp_displayed_user_id() 
  240. ? bp_displayed_user_id() 
  241. : bp_loggedin_user_id(); 
  242.  
  243. // Determine friends of user. 
  244. $friends = friends_get_friend_user_ids( $user_id ); 
  245. if ( empty( $friends ) ) { 
  246. $friends = array( 0 ); 
  247.  
  248. $retval = array( 
  249. 'relation' => 'AND',  
  250. array( 
  251. 'column' => 'user_id',  
  252. 'compare' => 'IN',  
  253. 'value' => (array) $friends 
  254. ),  
  255.  
  256. // We should only be able to view sitewide activity content for friends. 
  257. array( 
  258. 'column' => 'hide_sitewide',  
  259. 'value' => 0 
  260. ),  
  261.  
  262. // Overrides. 
  263. 'override' => array( 
  264. 'filter' => array( 'user_id' => 0 ),  
  265. 'show_hidden' => true 
  266. ),  
  267. ); 
  268.  
  269. return $retval; 
  270. add_filter( 'bp_activity_set_friends_scope_args', 'bp_friends_filter_activity_scope', 10, 2 ); 
  271.  
  272. /** 
  273. * Set up activity arguments for use with the 'just-me' scope. 
  274. * 
  275. * For details on the syntax, see {@link BP_Activity_Query}. 
  276. * 
  277. * @since 2.2.0 
  278. * 
  279. * @param array $retval Empty array by default. 
  280. * @param array $filter Current activity arguments. 
  281. * @return array 
  282. */ 
  283. function bp_friends_filter_activity_just_me_scope( $retval = array(), $filter = array() ) { 
  284.  
  285. // Determine the user_id. 
  286. if ( ! empty( $filter['user_id'] ) ) { 
  287. $user_id = $filter['user_id']; 
  288. } else { 
  289. $user_id = bp_displayed_user_id() 
  290. ? bp_displayed_user_id() 
  291. : bp_loggedin_user_id(); 
  292.  
  293. // Get the requested action. 
  294. $action = $filter['filter']['action']; 
  295.  
  296. // Make sure actions are listed in an array. 
  297. if ( ! is_array( $action ) ) { 
  298. $action = explode( ', ', $filter['filter']['action'] ); 
  299.  
  300. $action = array_flip( array_filter( $action ) ); 
  301.  
  302. /** 
  303. * If filtering activities for something other than the friendship_created 
  304. * action return without changing anything 
  305. */ 
  306. if ( ! empty( $action ) && ! isset( $action['friendship_created'] ) ) { 
  307. return $retval; 
  308.  
  309. // Juggle existing override value. 
  310. $override = array(); 
  311. if ( ! empty( $retval['override'] ) ) { 
  312. $override = $retval['override']; 
  313. unset( $retval['override'] ); 
  314.  
  315. /** 
  316. * Else make sure to get the friendship_created action, the user is involved in 
  317. * - user initiated the friendship 
  318. * - user has been requested a friendship 
  319. */ 
  320. $retval = array( 
  321. 'relation' => 'OR',  
  322. $retval,  
  323. array( 
  324. 'relation' => 'AND',  
  325. array( 
  326. 'column' => 'component',  
  327. 'value' => 'friends',  
  328. ),  
  329. array( 
  330. 'column' => 'secondary_item_id',  
  331. 'value' => $user_id,  
  332. ),  
  333. ); 
  334.  
  335. // Juggle back override value. 
  336. if ( ! empty( $override ) ) { 
  337. $retval['override'] = $override; 
  338.  
  339. return $retval; 
  340. add_filter( 'bp_activity_set_just-me_scope_args', 'bp_friends_filter_activity_just_me_scope', 20, 2 ); 
  341.  
  342. /** 
  343. * Add activity stream items when one members accepts another members request 
  344. * for virtual friendship. 
  345. * 
  346. * @since 1.9.0 
  347. * 
  348. * @param int $friendship_id ID of the friendship. 
  349. * @param int $initiator_user_id ID of friendship initiator. 
  350. * @param int $friend_user_id ID of user whose friendship is requested. 
  351. * @param object|bool $friendship Optional Friendship object. 
  352. */ 
  353. function bp_friends_friendship_accepted_activity( $friendship_id, $initiator_user_id, $friend_user_id, $friendship = false ) { 
  354. if ( ! bp_is_active( 'activity' ) ) { 
  355. return; 
  356.  
  357. // Record in activity streams for the initiator. 
  358. friends_record_activity( array( 
  359. 'user_id' => $initiator_user_id,  
  360. 'type' => 'friendship_created',  
  361. 'item_id' => $friendship_id,  
  362. 'secondary_item_id' => $friend_user_id 
  363. ) ); 
  364. add_action( 'friends_friendship_accepted', 'bp_friends_friendship_accepted_activity', 10, 4 ); 
  365.  
  366. /** 
  367. * Deletes friendship activity items when a user is deleted. 
  368. * 
  369. * @since 2.5.0 
  370. * 
  371. * @param int $user_id The ID of the user being deleted. 
  372. */ 
  373. function bp_friends_delete_activity_on_user_delete( $user_id = 0 ) { 
  374. if ( ! bp_is_active( 'activity' ) ) { 
  375. return; 
  376.  
  377. bp_activity_delete( array( 
  378. 'component' => buddypress()->friends->id,  
  379. 'type' => 'friendship_created',  
  380. 'secondary_item_id' => $user_id 
  381. ) ); 
  382. add_action( 'friends_remove_data', 'bp_friends_delete_activity_on_user_delete' ); 
.