Ticket #5266: 5266.friends.2.patch
File 5266.friends.2.patch, 20.6 KB (added by , 12 years ago) |
---|
-
bp-friends-activity.php
100 100 add_action( 'bp_register_activity_actions', 'friends_register_activity_actions' ); 101 101 102 102 /** 103 * Notification formatting callback for bp-friends notifications. 103 * Add activity stream items when one members accepts another members request 104 * for virtual friendship. 104 105 * 105 * @param string $action The kind of notification being rendered. 106 * @param int $item_id The primary item ID. 107 * @param int $secondary_item_id The secondary item ID. 108 * @param int $total_items The total number of messaging-related notifications 109 * waiting for the user. 110 * @param string $format 'string' for BuddyBar-compatible notifications; 111 * 'array' for WP Toolbar. Default: 'string'. 112 * @return array|string 106 * @since BuddyPress (1.9.0) 107 * 108 * @param int $friendship_id 109 * @param int $initiator_user_id 110 * @param int $friend_user_id 111 * @param object $friendship 113 112 */ 114 function friends_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string') {113 function bp_friends_friendship_accepted_activity( $friendship_id, $initiator_user_id, $friend_user_id, $friendship ) { 115 114 116 switch ( $action ) { 117 case 'friendship_accepted': 118 $link = trailingslashit( bp_loggedin_user_domain() . bp_get_friends_slug() . '/my-friends' ); 119 120 // Set up the string and the filter 121 if ( (int) $total_items > 1 ) { 122 $text = sprintf( __( '%d friends accepted your friendship requests', 'buddypress' ), (int) $total_items ); 123 $filter = 'bp_friends_multiple_friendship_accepted_notification'; 124 } else { 125 $text = sprintf( __( '%s accepted your friendship request', 'buddypress' ), bp_core_get_user_displayname( $item_id ) ); 126 $filter = 'bp_friends_single_friendship_accepted_notification'; 127 } 128 129 break; 130 131 case 'friendship_request': 132 $link = bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/?new'; 133 134 // Set up the string and the filter 135 if ( (int) $total_items > 1 ) { 136 $text = sprintf( __( 'You have %d pending friendship requests', 'buddypress' ), (int) $total_items ); 137 $filter = 'bp_friends_multiple_friendship_request_notification'; 138 } else { 139 $text = sprintf( __( 'You have a friendship request from %s', 'buddypress' ), bp_core_get_user_displayname( $item_id ) ); 140 $filter = 'bp_friends_single_friendship_request_notification'; 141 } 142 143 break; 115 // Bail if Activity component is not active 116 if ( ! bp_is_active( 'activity' ) ) { 117 return; 144 118 } 145 119 146 // Return either an HTML link or an array, depending on the requested format 147 if ( 'string' == $format ) { 148 $return = apply_filters( $filter, '<a href="' . $link . '">' . $text . '</a>', (int) $total_items ); 149 } else { 150 $return = apply_filters( $filter, array( 151 'link' => $link, 152 'text' => $text 153 ), (int) $total_items ); 154 } 120 // Get links to both members profiles 121 $initiator_link = bp_core_get_userlink( $initiator_user_id ); 122 $friend_link = bp_core_get_userlink( $friend_user_id ); 155 123 156 do_action( 'friends_format_notifications', $action, $item_id, $secondary_item_id, $total_items, $return ); 124 // Record in activity streams for the initiator 125 friends_record_activity( array( 126 'user_id' => $initiator_user_id, 127 'type' => 'friendship_created', 128 'action' => apply_filters( 'friends_activity_friendship_accepted_action', sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $initiator_link, $friend_link ), $friendship ), 129 'item_id' => $friendship_id, 130 'secondary_item_id' => $friend_user_id 131 ) ); 157 132 158 return $return; 133 // Record in activity streams for the friend 134 friends_record_activity( array( 135 'user_id' => $friend_user_id, 136 'type' => 'friendship_created', 137 'action' => apply_filters( 'friends_activity_friendship_accepted_action', sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $friend_link, $initiator_link ), $friendship ), 138 'item_id' => $friendship_id, 139 'secondary_item_id' => $initiator_user_id, 140 'hide_sitewide' => true // We've already got the first entry site wide 141 ) ); 159 142 } 143 add_action( 'friends_friendship_accepted', 'bp_friends_friendship_accepted_activity', 10, 4 ); -
bp-friends-cache.php
10 10 * @subpackage FriendsCaching 11 11 */ 12 12 13 14 13 // Exit if accessed directly 15 14 if ( !defined( 'ABSPATH' ) ) exit; 16 15 … … 28 27 wp_cache_delete( 'friends_friend_ids_' . $friendship->friend_user_id, 'bp' ); 29 28 } 30 29 31 /**32 * Clear friend-related notifications when ?new=1.33 */34 function friends_clear_friend_notifications() {35 if ( isset( $_GET['new'] ) ) {36 bp_core_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );37 }38 }39 add_action( 'bp_activity_screen_my_activity', 'friends_clear_friend_notifications' );40 41 30 // List actions to clear object caches on 42 31 add_action( 'friends_friendship_accepted', 'friends_clear_friend_object_cache' ); 43 32 add_action( 'friends_friendship_deleted', 'friends_clear_friend_object_cache' ); -
bp-friends-functions.php
29 29 * @return bool True on success, false on failure. 30 30 */ 31 31 function friends_add_friend( $initiator_userid, $friend_userid, $force_accept = false ) { 32 global $bp;33 32 33 // Check if already friends, and bail if so 34 34 $friendship = new BP_Friends_Friendship; 35 36 if ( (int) $friendship->is_confirmed ) 35 if ( (int) $friendship->is_confirmed ) { 37 36 return true; 37 } 38 38 39 // Setup the friendship data 39 40 $friendship->initiator_user_id = $initiator_userid; 40 41 $friendship->friend_user_id = $friend_userid; 41 42 $friendship->is_confirmed = 0; 42 43 $friendship->is_limited = 0; 43 44 $friendship->date_created = bp_core_current_time(); 44 45 45 if ( $force_accept )46 if ( !empty( $force_accept ) ) { 46 47 $friendship->is_confirmed = 1; 48 } 47 49 48 if ( $friendship->save() ) { 50 // Bail if friendship could not be saved (how sad!) 51 if ( ! $friendship->save() ) { 52 return false; 53 } 49 54 50 if ( !$force_accept ) {51 // Add the on screen notification52 bp_core_add_notification( $friendship->initiator_user_id, $friendship->friend_user_id, $bp->friends->id, 'friendship_request' );55 // Send notifications 56 if ( empty( $force_accept ) ) { 57 $action = 'friends_friendship_requested'; 53 58 54 // Send the email notification 55 friends_notification_new_request( $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id ); 56 57 do_action( 'friends_friendship_requested', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id ); 58 } else { 59 // Update friend totals 60 friends_update_friend_totals( $friendship->initiator_user_id, $friendship->friend_user_id, 'add' ); 61 62 do_action( 'friends_friendship_accepted', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id ); 63 } 64 65 return true; 59 // Update friend totals 60 } else { 61 $action = 'friends_friendship_accepted'; 62 friends_update_friend_totals( $friendship->initiator_user_id, $friendship->friend_user_id, 'add' ); 66 63 } 67 64 68 return false; 65 // Call the above titled action and pass friendship data into it 66 do_action( $action, $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id ); 67 68 return true; 69 69 } 70 70 71 71 /** … … 111 111 * @return bool True on success, false on failure. 112 112 */ 113 113 function friends_accept_friendship( $friendship_id ) { 114 global $bp;115 114 115 // Get the friesdhip data 116 116 $friendship = new BP_Friends_Friendship( $friendship_id, true, false ); 117 117 118 if ( !$friendship->is_confirmed && BP_Friends_Friendship::accept( $friendship_id ) ) { 118 // Accepting friendship 119 if ( empty( $friendship->is_confirmed ) && BP_Friends_Friendship::reject( $friendship_id ) ) { 120 121 // Bump the friendship counts 119 122 friends_update_friend_totals( $friendship->initiator_user_id, $friendship->friend_user_id ); 120 123 121 // Remove the friend request notice 122 bp_core_mark_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, $bp->friends->id, 'friendship_request' ); 124 do_action( 'friends_friendship_accepted', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id, $friendship ); 123 125 124 // Add a friend accepted notice for the initiating user125 bp_core_add_notification( $friendship->friend_user_id, $friendship->initiator_user_id, $bp->friends->id, 'friendship_accepted' );126 127 $initiator_link = bp_core_get_userlink( $friendship->initiator_user_id );128 $friend_link = bp_core_get_userlink( $friendship->friend_user_id );129 130 // Record in activity streams for the initiator131 friends_record_activity( array(132 'user_id' => $friendship->initiator_user_id,133 'type' => 'friendship_created',134 'action' => apply_filters( 'friends_activity_friendship_accepted_action', sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $initiator_link, $friend_link ), $friendship ),135 'item_id' => $friendship_id,136 'secondary_item_id' => $friendship->friend_user_id137 ) );138 139 // Record in activity streams for the friend140 friends_record_activity( array(141 'user_id' => $friendship->friend_user_id,142 'type' => 'friendship_created',143 'action' => apply_filters( 'friends_activity_friendship_accepted_action', sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $friend_link, $initiator_link ), $friendship ),144 'item_id' => $friendship_id,145 'secondary_item_id' => $friendship->initiator_user_id,146 'hide_sitewide' => true // We've already got the first entry site wide147 ) );148 149 // Send the email notification150 friends_notification_accepted_request( $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );151 152 do_action( 'friends_friendship_accepted', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );153 154 126 return true; 155 127 } 156 128 … … 164 136 * @return bool True on success, false on failure. 165 137 */ 166 138 function friends_reject_friendship( $friendship_id ) { 167 global $bp;168 169 139 $friendship = new BP_Friends_Friendship( $friendship_id, true, false ); 170 140 171 if ( !$friendship->is_confirmed && BP_Friends_Friendship::reject( $friendship_id ) ) { 172 173 // Remove the friend request notice 174 bp_core_mark_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, $bp->friends->id, 'friendship_request' ); 175 141 if ( empty( $friendship->is_confirmed ) && BP_Friends_Friendship::reject( $friendship_id ) ) { 176 142 do_action_ref_array( 'friends_friendship_rejected', array( $friendship_id, &$friendship ) ); 177 143 return true; 178 144 } … … 189 155 * @return bool True on success, false on failure. 190 156 */ 191 157 function friends_withdraw_friendship( $initiator_userid, $friend_userid ) { 192 global $bp;193 194 158 $friendship_id = BP_Friends_Friendship::get_friendship_id( $initiator_userid, $friend_userid ); 195 159 $friendship = new BP_Friends_Friendship( $friendship_id, true, false ); 196 160 197 if ( !$friendship->is_confirmed && BP_Friends_Friendship::withdraw( $friendship_id ) ) { 198 // Remove the friend request notice 199 bp_core_delete_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, $bp->friends->id, 'friendship_request' ); 200 161 if ( empty( $friendship->is_confirmed ) && BP_Friends_Friendship::withdraw( $friendship_id ) ) { 201 162 do_action_ref_array( 'friends_friendship_whithdrawn', array( $friendship_id, &$friendship ) ); 202 163 return true; 203 164 } … … 566 527 * @param int $user_id ID of the user whose friend data is being removed. 567 528 */ 568 529 function friends_remove_data( $user_id ) { 569 global $bp;570 530 571 531 do_action( 'friends_before_remove_data', $user_id ); 572 532 … … 575 535 // Remove usermeta 576 536 bp_delete_user_meta( $user_id, 'total_friend_count' ); 577 537 578 // Remove friendship requests FROM user579 bp_core_delete_notifications_from_user( $user_id, $bp->friends->id, 'friendship_request' );580 581 538 do_action( 'friends_remove_data', $user_id ); 582 539 } 583 540 add_action( 'wpmu_delete_user', 'friends_remove_data' ); -
bp-friends-notifications.php
13 13 // Exit if accessed directly 14 14 if ( !defined( 'ABSPATH' ) ) exit; 15 15 16 /** Emails ********************************************************************/ 17 16 18 /** 17 19 * Send notifications related to a new friendship request. 18 20 * … … 63 65 64 66 do_action( 'bp_friends_sent_request_email', $friend_id, $subject, $message, $friendship_id, $initiator_id ); 65 67 } 68 add_action( 'friends_friendship_requested', 'friends_notification_new_request', 10, 3 ); 66 69 67 70 /** 68 71 * Send notifications related to the acceptance of a friendship request. … … 111 114 112 115 do_action( 'bp_friends_sent_accepted_email', $initiator_id, $subject, $message, $friendship_id, $friend_id ); 113 116 } 117 add_action( 'friends_friendship_accepted', 'friends_notification_accepted_request', 10, 3 ); 118 119 /** Notifications *************************************************************/ 120 121 /** 122 * Notification formatting callback for bp-friends notifications. 123 * 124 * @param string $action The kind of notification being rendered. 125 * @param int $item_id The primary item ID. 126 * @param int $secondary_item_id The secondary item ID. 127 * @param int $total_items The total number of messaging-related notifications 128 * waiting for the user. 129 * @param string $format 'string' for BuddyBar-compatible notifications; 130 * 'array' for WP Toolbar. Default: 'string'. 131 * @return array|string 132 */ 133 function friends_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) { 134 135 switch ( $action ) { 136 case 'friendship_accepted': 137 $link = trailingslashit( bp_loggedin_user_domain() . bp_get_friends_slug() . '/my-friends' ); 138 139 // Set up the string and the filter 140 if ( (int) $total_items > 1 ) { 141 $text = sprintf( __( '%d friends accepted your friendship requests', 'buddypress' ), (int) $total_items ); 142 $filter = 'bp_friends_multiple_friendship_accepted_notification'; 143 } else { 144 $text = sprintf( __( '%s accepted your friendship request', 'buddypress' ), bp_core_get_user_displayname( $item_id ) ); 145 $filter = 'bp_friends_single_friendship_accepted_notification'; 146 } 147 148 break; 149 150 case 'friendship_request': 151 $link = bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/?new'; 152 153 // Set up the string and the filter 154 if ( (int) $total_items > 1 ) { 155 $text = sprintf( __( 'You have %d pending friendship requests', 'buddypress' ), (int) $total_items ); 156 $filter = 'bp_friends_multiple_friendship_request_notification'; 157 } else { 158 $text = sprintf( __( 'You have a friendship request from %s', 'buddypress' ), bp_core_get_user_displayname( $item_id ) ); 159 $filter = 'bp_friends_single_friendship_request_notification'; 160 } 161 162 break; 163 } 164 165 // Return either an HTML link or an array, depending on the requested format 166 if ( 'string' == $format ) { 167 $return = apply_filters( $filter, '<a href="' . $link . '">' . $text . '</a>', (int) $total_items ); 168 } else { 169 $return = apply_filters( $filter, array( 170 'link' => $link, 171 'text' => $text 172 ), (int) $total_items ); 173 } 174 175 do_action( 'friends_format_notifications', $action, $item_id, $secondary_item_id, $total_items, $return ); 176 177 return $return; 178 } 179 180 /** 181 * Clear friend-related notifications when ?new=1 182 */ 183 function friends_clear_friend_notifications() { 184 if ( isset( $_GET['new'] ) && bp_is_active( 'notifications' ) ) { 185 bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' ); 186 } 187 } 188 add_action( 'bp_activity_screen_my_activity', 'friends_clear_friend_notifications' ); 189 190 /** 191 * Delete any friendship request notifications for the logged in user. 192 * 193 * @since BuddyPress (1.9.0) 194 */ 195 function bp_friends_mark_friendship_request_notifications_by_type() { 196 if ( isset( $_GET['new'] ) && bp_is_active( 'notifications' ) ) { 197 bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_request' ); 198 } 199 } 200 add_action( 'friends_screen_requests', 'bp_friends_mark_friendship_request_notifications_by_type' ); 201 202 /** 203 * Delete any friendship acceptance notifications for the logged in user. 204 * 205 * @since BuddyPress (1.9.0) 206 */ 207 function bp_friends_mark_friendship_accepted_notifications_by_type() { 208 if ( bp_is_active( 'notifications' ) ) { 209 bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' ); 210 } 211 } 212 add_action( 'friends_screen_my_friends', 'bp_friends_mark_friendship_accepted_notifications_by_type' ); 213 214 /** 215 * Notify one use that another user has requested their virtual friendship. 216 * 217 * @since BuddyPress (1.9.0) 218 */ 219 function bp_friends_friendship_requested_notification( $friendship_id, $initiator_user_id, $friend_user_id ) { 220 if ( bp_is_active( 'notifications' ) ) { 221 bp_notifications_add_notification( $initiator_user_id, $friend_user_id, $friendship_id, 'friendship_request' ); 222 } 223 } 224 add_action( 'friends_friendship_requested', 'friends_friendship_requested', 10, 3 ); 225 226 /** 227 * Remove friend request notice when a member rejects another members 228 * 229 * @since BuddyPress (1.9.0) 230 * 231 * @param int $friendship_id (not used) 232 * @param object $friendship 233 */ 234 function bp_friends_mark_friendship_rejected_notifications_by_item_id( $friendship_id, $friendship ) { 235 if ( bp_is_active( 'notifications' ) ) { 236 bp_notifications_mark_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' ); 237 } 238 } 239 add_action( 'friends_friendship_rejected', 'bp_friends_mark_friendship_rejected_notifications_by_item_id', 10, 2 ); 240 241 /** 242 * Notify a member when another member accepts their virtual friendsip request. 243 * 244 * @since BuddyPress (1.9.0) 245 * @param int $relationship_id 246 * @param int $initiator_user_id 247 * @param int $friend_user_id 248 */ 249 function bp_friends_add_friendship_accepted_notification( $friendship_id, $initiator_user_id, $friend_user_id ) { 250 251 // Bail if notifications is not active 252 if ( ! bp_is_active( 'notifications' ) ) { 253 return; 254 } 255 256 // Remove the friend request notice 257 bp_notifications_mark_notifications_by_item_id( $friend_user_id, $initiator_user_id, buddypress()->friends->id, 'friendship_request' ); 258 259 // Add a friend accepted notice for the initiating user 260 bp_notifications_add_notification( array( 261 'user_id' => $initiator_user_id, 262 'item_id' => $friend_user_id, 263 'secondary_item_id' => 0, 264 'component_name' => buddypress()->friends->id, 265 'component_action' => 'friendship_accepted', 266 'date_notified' => bp_core_current_time(), 267 'is_new' => 1, 268 ) ); 269 } 270 add_action( 'friends_friendship_accepted', 'bp_friends_add_friendship_accepted_notification', 10, 3 ); 271 272 /** 273 * Remove friend request notice when a member withdraws their friend request 274 * 275 * @since BuddyPress (1.9.0) 276 * 277 * @param int $friendship_id (not used) 278 * @param object $friendship 279 */ 280 function bp_friends_mark_friendship_withdrawn_notifications_by_item_id( $friendship_id, $friendship ) { 281 if ( bp_is_active( 'notifications' ) ) { 282 bp_notifications_delete_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' ); 283 } 284 } 285 add_action( 'friends_friendship_withdrawn', 'bp_friends_mark_friendship_withdrawn_notifications_by_item_id', 10, 2 ); 286 287 /** 288 * Remove friendship requests FROM user, used primarily when a user is deleted 289 * 290 * @since BuddyPress (1.9.0) 291 * @param int $user_id 292 */ 293 function bp_friends_remove_notifications_data( $user_id = 0 ) { 294 if ( bp_is_active( 'notifications' ) ) { 295 bp_notifications_delete_notifications_from_user( $user_id, buddypress()->friends->id, 'friendship_request' ); 296 } 297 } 298 add_action( 'friends_remove_data', 'bp_friends_remove_notifications_data', 10, 1 ); -
bp-friends-screens.php
19 19 */ 20 20 function friends_screen_my_friends() { 21 21 22 // Delete any friendship acceptance notifications for the user when viewing a profile23 bp_core_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );24 25 22 do_action( 'friends_screen_my_friends' ); 26 23 27 24 bp_core_load_template( apply_filters( 'friends_template_my_friends', 'members/single/home' ) ); … … 67 64 68 65 do_action( 'friends_screen_requests' ); 69 66 70 if ( isset( $_GET['new'] ) ) {71 bp_core_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_request' );72 }73 74 67 bp_core_load_template( apply_filters( 'friends_template_requests', 'members/single/home' ) ); 75 68 } 76 69