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