Skip to:
Content

BuddyPress.org

Ticket #5266: 5266.friends.patch

File 5266.friends.patch, 11.5 KB (added by johnjamesjacoby, 12 years ago)

Friends Notifications abstraction

  • bp-friends-cache.php

     
    1010 * @subpackage FriendsCaching
    1111 */
    1212
    13 
    1413// Exit if accessed directly
    1514if ( !defined( 'ABSPATH' ) ) exit;
    1615
     
    2827        wp_cache_delete( 'friends_friend_ids_' .    $friendship->friend_user_id,    'bp' );
    2928}
    3029
    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 
    4130// List actions to clear object caches on
    4231add_action( 'friends_friendship_accepted', 'friends_clear_friend_object_cache' );
    4332add_action( 'friends_friendship_deleted',  'friends_clear_friend_object_cache' );
  • bp-friends-functions.php

     
    2929 * @return bool True on success, false on failure.
    3030 */
    3131function friends_add_friend( $initiator_userid, $friend_userid, $force_accept = false ) {
    32         global $bp;
    3332
    3433        $friendship = new BP_Friends_Friendship;
    3534
    36         if ( (int) $friendship->is_confirmed )
     35        if ( (int) $friendship->is_confirmed ) {
    3736                return true;
     37        }
    3838
    3939        $friendship->initiator_user_id = $initiator_userid;
    4040        $friendship->friend_user_id    = $friend_userid;
     
    4242        $friendship->is_limited        = 0;
    4343        $friendship->date_created      = bp_core_current_time();
    4444
    45         if ( $force_accept )
     45        if ( !empty( $force_accept ) ) {
    4646                $friendship->is_confirmed = 1;
     47        }
    4748
    4849        if ( $friendship->save() ) {
    4950
    50                 if ( !$force_accept ) {
    51                         // Add the on screen notification
    52                         bp_core_add_notification( $friendship->initiator_user_id, $friendship->friend_user_id, $bp->friends->id, 'friendship_request' );
    53 
    54                         // Send the email notification
     51                // Send the email notification
     52                if ( empty( $force_accept ) ) {
    5553                        friends_notification_new_request( $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
    5654
    5755                        do_action( 'friends_friendship_requested', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
     56
     57                // Update friend totals
    5858                } else {
    59                         // Update friend totals
    6059                        friends_update_friend_totals( $friendship->initiator_user_id, $friendship->friend_user_id, 'add' );
    6160
    6261                        do_action( 'friends_friendship_accepted', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
     
    111110 * @return bool True on success, false on failure.
    112111 */
    113112function friends_accept_friendship( $friendship_id ) {
    114         global $bp;
    115113
    116114        $friendship = new BP_Friends_Friendship( $friendship_id, true, false );
    117115
    118116        if ( !$friendship->is_confirmed && BP_Friends_Friendship::accept( $friendship_id ) ) {
    119117                friends_update_friend_totals( $friendship->initiator_user_id, $friendship->friend_user_id );
    120118
    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' );
    123 
    124                 // Add a friend accepted notice for the initiating user
    125                 bp_core_add_notification( $friendship->friend_user_id, $friendship->initiator_user_id, $bp->friends->id, 'friendship_accepted' );
    126 
    127119                $initiator_link = bp_core_get_userlink( $friendship->initiator_user_id );
    128                 $friend_link = bp_core_get_userlink( $friendship->friend_user_id );
     120                $friend_link    = bp_core_get_userlink( $friendship->friend_user_id    );
    129121
    130122                // Record in activity streams for the initiator
    131123                friends_record_activity( array(
     
    164156 * @return bool True on success, false on failure.
    165157 */
    166158function friends_reject_friendship( $friendship_id ) {
    167         global $bp;
    168 
    169159        $friendship = new BP_Friends_Friendship( $friendship_id, true, false );
    170160
    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 
     161        if ( empty( $friendship->is_confirmed ) && BP_Friends_Friendship::reject( $friendship_id ) ) {
    176162                do_action_ref_array( 'friends_friendship_rejected', array( $friendship_id, &$friendship ) );
    177163                return true;
    178164        }
     
    189175 * @return bool True on success, false on failure.
    190176 */
    191177function friends_withdraw_friendship( $initiator_userid, $friend_userid ) {
    192         global $bp;
    193 
    194178        $friendship_id = BP_Friends_Friendship::get_friendship_id( $initiator_userid, $friend_userid );
    195179        $friendship    = new BP_Friends_Friendship( $friendship_id, true, false );
    196180
    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 
     181        if ( empty( $friendship->is_confirmed ) && BP_Friends_Friendship::withdraw( $friendship_id ) ) {
    201182                do_action_ref_array( 'friends_friendship_whithdrawn', array( $friendship_id, &$friendship ) );
    202183                return true;
    203184        }
     
    566547 * @param int $user_id ID of the user whose friend data is being removed.
    567548 */
    568549function friends_remove_data( $user_id ) {
    569         global $bp;
    570550
    571551        do_action( 'friends_before_remove_data', $user_id );
    572552
     
    575555        // Remove usermeta
    576556        bp_delete_user_meta( $user_id, 'total_friend_count' );
    577557
    578         // Remove friendship requests FROM user
    579         bp_core_delete_notifications_from_user( $user_id, $bp->friends->id, 'friendship_request' );
    580 
    581558        do_action( 'friends_remove_data', $user_id );
    582559}
    583560add_action( 'wpmu_delete_user',  'friends_remove_data' );
  • bp-friends-notifications.php

     
    1313// Exit if accessed directly
    1414if ( !defined( 'ABSPATH' ) ) exit;
    1515
     16/** Emails ********************************************************************/
     17
    1618/**
    1719 * Send notifications related to a new friendship request.
    1820 *
     
    111113
    112114        do_action( 'bp_friends_sent_accepted_email', $initiator_id, $subject, $message, $friendship_id, $friend_id );
    113115}
     116
     117/** Notifications *************************************************************/
     118
     119/**
     120 * Clear friend-related notifications when ?new=1
     121 */
     122function friends_clear_friend_notifications() {
     123        if ( isset( $_GET['new'] ) && bp_is_active( 'notifications' ) ) {
     124                bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );
     125        }
     126}
     127add_action( 'bp_activity_screen_my_activity', 'friends_clear_friend_notifications' );
     128
     129/**
     130 * Delete any friendship request notifications for the logged in user.
     131 *
     132 * @since BuddyPress (1.9.0)
     133 */
     134function bp_friends_mark_friendship_request_notifications_by_type() {
     135        if ( isset( $_GET['new'] ) && bp_is_active( 'notifications' ) ) {
     136                bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_request' );
     137        }
     138}
     139add_action( 'friends_screen_requests', 'bp_friends_mark_friendship_request_notifications_by_type' );
     140
     141/**
     142 * Delete any friendship acceptance notifications for the logged in user.
     143 *
     144 * @since BuddyPress (1.9.0)
     145 */
     146function bp_friends_mark_friendship_accepted_notifications_by_type() {
     147        if ( bp_is_active( 'notifications' ) ) {
     148                bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );
     149        }
     150}
     151add_action( 'friends_screen_my_friends', 'bp_friends_mark_friendship_accepted_notifications_by_type' );
     152
     153/**
     154 * Notify one use that another user has requested their virtual friendship.
     155 *
     156 * @since BuddyPress (1.9.0)
     157 */
     158function bp_friends_friendship_requested_notification( $friendship_id, $initiator_user_id, $friend_user_id ) {
     159        if ( bp_is_active( 'notifications' ) ) {
     160                bp_notifications_add_notification( $initiator_user_id, $friend_user_id, $friendship_id, 'friendship_request' );
     161        }
     162}
     163add_action( 'friends_friendship_requested', 'friends_friendship_requested' );
     164
     165/**
     166 * Remove friend request notice when a member rejects another members
     167 *
     168 * @since BuddyPress (1.9.0)
     169 *
     170 * @param int $friendship_id (not used)
     171 * @param object $friendship
     172 */
     173function bp_friends_mark_friendship_rejected_notifications_by_item_id( $friendship_id, $friendship ) {
     174        if ( bp_is_active( 'notifications' ) ) {
     175                bp_notifications_mark_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' );
     176        }
     177}
     178add_action( 'friends_friendship_rejected', 'bp_friends_mark_friendship_rejected_notifications_by_item_id' );
     179
     180/**
     181 * Notify a member when another member accepts their virtual friendsip request.
     182 *
     183 * @since BuddyPress (1.9.0)
     184 * @param int $relationship_id
     185 * @param int $initiator_user_id
     186 * @param int $friend_user_id
     187 */
     188function bp_friends_add_friendship_accepted_notification( $friendship_id, $initiator_user_id, $friend_user_id ) {
     189
     190        // Bail if notifications is not active
     191        if ( ! bp_is_active( 'notifications' ) ) {
     192                return;
     193        }
     194
     195        // Remove the friend request notice
     196        bp_notifications_mark_notifications_by_item_id( $friend_user_id, $initiator_user_id, buddypress()->friends->id, 'friendship_request' );
     197
     198        // Add a friend accepted notice for the initiating user
     199        bp_notifications_add_notification(  array(
     200                'user_id'           => $initiator_user_id,
     201                'item_id'           => $friend_user_id,
     202                'secondary_item_id' => 0,
     203                'component_name'    => buddypress()->friends->id,
     204                'component_action'  => 'friendship_accepted',
     205                'date_notified'     => bp_core_current_time(),
     206                'is_new'            => 1,
     207        ) );
     208}
     209add_action( 'friends_friendship_accepted', 'bp_friends_add_friendship_accepted_notification' );
     210
     211/**
     212 * Remove friend request notice when a member withdraws their friend request
     213 *
     214 * @since BuddyPress (1.9.0)
     215 *
     216 * @param int $friendship_id (not used)
     217 * @param object $friendship
     218 */
     219function bp_friends_mark_friendship_withdrawn_notifications_by_item_id( $friendship_id, $friendship ) {
     220        if ( bp_is_active( 'notifications' ) ) {
     221                bp_notifications_delete_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' );
     222        }
     223}
     224add_action( 'friends_friendship_withdrawn', 'bp_friends_mark_friendship_withdrawn_notifications_by_item_id' );
     225
     226/**
     227 * Remove friendship requests FROM user, used primarily when a user is deleted
     228 *
     229 * @since BuddyPress (1.9.0)
     230 * @param int $user_id
     231 */
     232function bp_friends_remove_notifications_data( $user_id = 0 ) {
     233        if ( bp_is_active( 'notifications' ) ) {
     234                bp_notifications_delete_notifications_from_user( $user_id, buddypress()->friends->id, 'friendship_request' );
     235        }
     236}
     237add_action( 'friends_remove_data', 'bp_friends_remove_notifications_data' );
  • bp-friends-screens.php

     
    1919 */
    2020function friends_screen_my_friends() {
    2121
    22         // Delete any friendship acceptance notifications for the user when viewing a profile
    23         bp_core_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );
    24 
    2522        do_action( 'friends_screen_my_friends' );
    2623
    2724        bp_core_load_template( apply_filters( 'friends_template_my_friends', 'members/single/home' ) );
     
    6764
    6865        do_action( 'friends_screen_requests' );
    6966
    70         if ( isset( $_GET['new'] ) ) {
    71                 bp_core_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_request' );
    72         }
    73 
    7467        bp_core_load_template( apply_filters( 'friends_template_requests', 'members/single/home' ) );
    7568}
    7669