Skip to:
Content

BuddyPress.org

Changeset 7624


Ignore:
Timestamp:
11/30/2013 06:14:08 AM (12 years ago)
Author:
johnjamesjacoby
Message:

Refactor Groups component's approach to Notifications and Activity integration:

  • Introduce helper functions for handling the adding/marking/deleting of notifications and adding/deleting activity stream entries. Hook these new functions into their respective actions rather than have them hardcoded and interspersed amongst the first-class code.
  • Inadvertently fixes a bug where banning or removing a member from a group within 5 minutes of their having joined would not delete the recent activity stream update.
  • See #5266.
Location:
trunk/bp-groups
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-groups/bp-groups-activity.php

    r7561 r7624  
    6060 */
    6161function groups_record_activity( $args = '' ) {
    62     global $bp;
    6362
    64     if ( !bp_is_active( 'activity' ) )
     63    if ( ! bp_is_active( 'activity' ) ) {
    6564        return false;
     65    }
    6666
    6767    // Set the default for hide_sitewide by checking the status of the group
     
    7979    }
    8080
    81     $defaults = array (
     81    $r = wp_parse_args( $args, array(
    8282        'id'                => false,
    8383        'user_id'           => bp_loggedin_user_id(),
     
    8585        'content'           => '',
    8686        'primary_link'      => '',
    87         'component'         => $bp->groups->id,
     87        'component'         => buddypress()->groups->id,
    8888        'type'              => false,
    8989        'item_id'           => false,
     
    9191        'recorded_time'     => bp_core_current_time(),
    9292        'hide_sitewide'     => $hide_sitewide
    93     );
     93    ) );
    9494
    95     $r = wp_parse_args( $args, $defaults );
    96     extract( $r );
    97 
    98     return bp_activity_add( array( 'id' => $id, 'user_id' => $user_id, 'action' => $action, 'content' => $content, 'primary_link' => $primary_link, 'component' => $component, 'type' => $type, 'item_id' => $item_id, 'secondary_item_id' => $secondary_item_id, 'recorded_time' => $recorded_time, 'hide_sitewide' => $hide_sitewide ) );
     95    return bp_activity_add( $r );
    9996}
    10097
     
    106103 */
    107104function groups_update_last_activity( $group_id = 0 ) {
    108     global $bp;
    109105
    110     if ( empty( $group_id ) )
    111         $group_id = $bp->groups->current_group->id;
     106    if ( empty( $group_id ) ) {
     107        $group_id = buddypress()->groups->current_group->id;
     108    }
    112109
    113     if ( empty( $group_id ) )
     110    if ( empty( $group_id ) ) {
    114111        return false;
     112    }
    115113
    116114    groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
     
    121119add_action( 'groups_new_forum_topic_post', 'groups_update_last_activity' );
    122120
    123 function groups_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
     121/**
     122 * Add an activity stream item when a member joins a group
     123 *
     124 * @since BuddyPress (1.9.0)
     125 * @param int $user_id
     126 * @param int $group_id
     127 */
     128function bp_groups_membership_accepted_add_activity( $user_id, $group_id ) {
    124129
    125     switch ( $action ) {
    126         case 'new_membership_request':
    127             $group_id = $secondary_item_id;
    128             $requesting_user_id = $item_id;
    129 
    130             $group = groups_get_group( array( 'group_id' => $group_id ) );
    131             $group_link = bp_get_group_permalink( $group );
    132 
    133             // Set up the string and the filter
    134             // Because different values are passed to the filters, we'll return the
    135             // values inline
    136             if ( (int) $total_items > 1 ) {
    137                 $text = sprintf( __( '%1$d new membership requests for the group "%2$s"', 'buddypress' ), (int) $total_items, $group->name );
    138                 $filter = 'bp_groups_multiple_new_membership_requests_notification';
    139                 $notification_link = $group_link . 'admin/membership-requests/?n=1';
    140 
    141                 if ( 'string' == $format ) {
    142                     return apply_filters( $filter, '<a href="' . $notification_link . '" title="' . __( 'Group Membership Requests', 'buddypress' ) . '">' . $text . '</a>', $group_link, $total_items, $group->name, $text, $notification_link );
    143                 } else {
    144                     return apply_filters( $filter, array(
    145                         'link' => $notification_link,
    146                         'text' => $text
    147                     ), $group_link, $total_items, $group->name, $text, $notification_link );
    148                 }
    149             } else {
    150                 $user_fullname = bp_core_get_user_displayname( $requesting_user_id );
    151                 $text = sprintf( __( '%s requests group membership', 'buddypress' ), $user_fullname );
    152                 $filter = 'bp_groups_single_new_membership_request_notification';
    153                 $notification_link = $group_link . 'admin/membership-requests/?n=1';
    154 
    155                 if ( 'string' == $format ) {
    156                     return apply_filters( $filter, '<a href="' . $notification_link . '" title="' . sprintf( __( '%s requests group membership', 'buddypress' ), $user_fullname ) . '">' . $text . '</a>', $group_link, $user_fullname, $group->name, $text, $notification_link );
    157                 } else {
    158                     return apply_filters( $filter, array(
    159                         'link' => $notification_link,
    160                         'text' => $text
    161                     ), $group_link, $user_fullname, $group->name, $text, $notification_link );
    162                 }
    163             }
    164 
    165             break;
    166 
    167         case 'membership_request_accepted':
    168             $group_id = $item_id;
    169 
    170             $group = groups_get_group( array( 'group_id' => $group_id ) );
    171             $group_link = bp_get_group_permalink( $group );
    172 
    173             if ( (int) $total_items > 1 ) {
    174                 $text = sprintf( __( '%d accepted group membership requests', 'buddypress' ), (int) $total_items, $group->name );
    175                 $filter = 'bp_groups_multiple_membership_request_accepted_notification';
    176                 $notification_link = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() ) . '?n=1';
    177 
    178                 if ( 'string' == $format ) {
    179                     return apply_filters( $filter, '<a href="' . $notification_link . '" title="' . __( 'Groups', 'buddypress' ) . '">' . $text . '</a>', $total_items, $group->name, $text, $notification_link );
    180                 } else {
    181                     return apply_filters( $filter, array(
    182                         'link' => $notification_link,
    183                         'text' => $text
    184                     ), $total_items, $group->name, $text, $notification_link );
    185                 }
    186             } else {
    187                 $text = sprintf( __( 'Membership for group "%s" accepted', 'buddypress' ), $group->name );
    188                 $filter = 'bp_groups_single_membership_request_accepted_notification';
    189                 $notification_link = $group_link . '?n=1';
    190 
    191                 if ( 'string' == $format ) {
    192                     return apply_filters( $filter, '<a href="' . $notification_link . '">' . $text . '</a>', $group_link, $group->name, $text, $notification_link );
    193                 } else {
    194                     return apply_filters( $filter, array(
    195                         'link' => $notification_link,
    196                         'text' => $text
    197                     ), $group_link, $group->name, $text, $notification_link );
    198                 }
    199             }
    200 
    201             break;
    202 
    203         case 'membership_request_rejected':
    204             $group_id = $item_id;
    205 
    206             $group = groups_get_group( array( 'group_id' => $group_id ) );
    207             $group_link = bp_get_group_permalink( $group );
    208 
    209             if ( (int) $total_items > 1 ) {
    210                 $text = sprintf( __( '%d rejected group membership requests', 'buddypress' ), (int) $total_items, $group->name );
    211                 $filter = 'bp_groups_multiple_membership_request_rejected_notification';
    212                 $notification_link = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() ) . '?n=1';
    213 
    214                 if ( 'string' == $format ) {
    215                     return apply_filters( $filter, '<a href="' . $notification_link . '" title="' . __( 'Groups', 'buddypress' ) . '">' . $text . '</a>', $total_items, $group->name );
    216                 } else {
    217                     return apply_filters( $filter, array(
    218                         'link' => $notification_link,
    219                         'text' => $text
    220                     ), $total_items, $group->name, $text, $notification_link );
    221                 }
    222             } else {
    223                 $text = sprintf( __( 'Membership for group "%s" rejected', 'buddypress' ), $group->name );
    224                 $filter = 'bp_groups_single_membership_request_rejected_notification';
    225                 $notification_link = $group_link . '?n=1';
    226 
    227                 if ( 'string' == $format ) {
    228                     return apply_filters( $filter, '<a href="' . $notification_link . '">' . $text . '</a>', $group_link, $group->name, $text, $notification_link );
    229                 } else {
    230                     return apply_filters( $filter, array(
    231                         'link' => $notification_link,
    232                         'text' => $text
    233                     ), $group_link, $group->name, $text, $notification_link );
    234                 }
    235             }
    236 
    237             break;
    238 
    239         case 'member_promoted_to_admin':
    240             $group_id = $item_id;
    241 
    242             $group = groups_get_group( array( 'group_id' => $group_id ) );
    243             $group_link = bp_get_group_permalink( $group );
    244 
    245             if ( (int) $total_items > 1 ) {
    246                 $text = sprintf( __( 'You were promoted to an admin in %d groups', 'buddypress' ), (int) $total_items );
    247                 $filter = 'bp_groups_multiple_member_promoted_to_admin_notification';
    248                 $notification_link = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() ) . '?n=1';
    249 
    250                 if ( 'string' == $format ) {
    251                     return apply_filters( $filter, '<a href="' . $notification_link . '" title="' . __( 'Groups', 'buddypress' ) . '">' . $text . '</a>', $total_items, $text, $notification_link );
    252                 } else {
    253                     return apply_filters( $filter, array(
    254                         'link' => $notification_link,
    255                         'text' => $text
    256                     ), $total_items, $text, $notification_link );
    257                 }
    258             } else {
    259                 $text = sprintf( __( 'You were promoted to an admin in the group "%s"', 'buddypress' ), $group->name );
    260                 $filter = 'bp_groups_single_member_promoted_to_admin_notification';
    261                 $notification_link = $group_link . '?n=1';
    262 
    263                 if ( 'string' == $format ) {
    264                     return apply_filters( $filter, '<a href="' . $notification_link . '">' . $text . '</a>', $group_link, $group->name, $text, $notification_link );
    265                 } else {
    266                     return apply_filters( $filter, array(
    267                         'link' => $notification_link,
    268                         'text' => $text
    269                     ), $group_link, $group->name, $text, $notification_link );
    270                 }
    271             }
    272 
    273             break;
    274 
    275         case 'member_promoted_to_mod':
    276             $group_id = $item_id;
    277 
    278             $group = groups_get_group( array( 'group_id' => $group_id ) );
    279             $group_link = bp_get_group_permalink( $group );
    280 
    281             if ( (int) $total_items > 1 ) {
    282                 $text = sprintf( __( 'You were promoted to a mod in %d groups', 'buddypress' ), (int) $total_items );
    283                 $filter = 'bp_groups_multiple_member_promoted_to_mod_notification';
    284                 $notification_link = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() ) . '?n=1';
    285 
    286                 if ( 'string' == $format ) {
    287                     return apply_filters( $filter, '<a href="' . $notification_link . '" title="' . __( 'Groups', 'buddypress' ) . '">' . $text . '</a>', $total_items, $text, $notification_link );
    288                 } else {
    289                     return apply_filters( $filter, array(
    290                         'link' => $notification_link,
    291                         'text' => $text
    292                     ), $total_items, $text, $notification_link );
    293                 }
    294             } else {
    295                 $text = sprintf( __( 'You were promoted to a mod in the group "%s"', 'buddypress' ), $group->name );
    296                 $filter = 'bp_groups_single_member_promoted_to_mod_notification';
    297                 $notification_link = $group_link . '?n=1';
    298 
    299                 if ( 'string' == $format ) {
    300                     return apply_filters( $filter, '<a href="' . $notification_link . '">' . $text . '</a>', $group_link, $group->name, $text, $notification_link );
    301                 } else {
    302                     return apply_filters( $filter, array(
    303                         'link' => $notification_link,
    304                         'text' => $text
    305                     ), $group_link, $group->name, $text, $notification_link );
    306                 }
    307             }
    308 
    309             break;
    310 
    311         case 'group_invite':
    312             $group_id = $item_id;
    313             $group = groups_get_group( array( 'group_id' => $group_id ) );
    314             $group_link = bp_get_group_permalink( $group );
    315 
    316             $notification_link = bp_loggedin_user_domain() . bp_get_groups_slug() . '/invites/?n=1';
    317 
    318             if ( (int) $total_items > 1 ) {
    319                 $text = sprintf( __( 'You have %d new group invitations', 'buddypress' ), (int) $total_items );
    320                 $filter = 'bp_groups_multiple_group_invite_notification';
    321 
    322                 if ( 'string' == $format ) {
    323                     return apply_filters( $filter, '<a href="' . $notification_link . '" title="' . __( 'Group Invites', 'buddypress' ) . '">' . $text . '</a>', $total_items, $text, $notification_link );
    324                 } else {
    325                     return apply_filters( $filter, array(
    326                         'link' => $notification_link,
    327                         'text' => $text
    328                     ), $total_items, $text, $notification_link );
    329                 }
    330             } else {
    331                 $text = sprintf( __( 'You have an invitation to the group: %s', 'buddypress' ), $group->name );
    332                 $filter = 'bp_groups_single_group_invite_notification';
    333 
    334                 if ( 'string' == $format ) {
    335                     return apply_filters( $filter, '<a href="' . $notification_link . '">' . $text . '</a>', $group_link, $group->name, $text, $notification_link );
    336                 } else {
    337                     return apply_filters( $filter, array(
    338                         'link' => $notification_link,
    339                         'text' => $text
    340                     ), $group_link, $group->name, $text, $notification_link );
    341                 }
    342             }
    343 
    344             break;
     130    // Bail if Activity is not active
     131    if ( ! bp_is_active( 'activity' ) ) {
     132        return false;
    345133    }
    346134
    347     do_action( 'groups_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
     135    // Get the group so we can get it's name
     136    $group = groups_get_group( array( 'group_id' => $group_id ) );
    348137
    349     return false;
     138    // Record in activity streams
     139    groups_record_activity( array(
     140        'action'  => apply_filters_ref_array( 'groups_activity_membership_accepted_action', array( sprintf( __( '%1$s joined the group %2$s', 'buddypress' ), bp_core_get_userlink( $user_id ), '<a href="' . bp_get_group_permalink( $group ) . '">' . esc_attr( $group->name ) . '</a>' ), $user_id, &$group ) ),
     141        'type'    => 'joined_group',
     142        'item_id' => $group_id,
     143        'user_id' => $user_id
     144    ) );   
    350145}
     146add_action( 'groups_membership_accepted', 'bp_groups_membership_accepted_add_activity', 10, 2 );
     147
     148/**
     149 * Delete all group activity from activity streams
     150 *
     151 * @since BuddyPress (1.9.0)
     152 */
     153function bp_groups_delete_group_delete_all_activity( $group_id ) {
     154    if ( bp_is_active( 'activity' ) ) {
     155        bp_activity_delete_by_item_id( array(
     156            'item_id'   => $group_id,
     157            'component' => buddypress()->groups->id
     158        ) );
     159    }
     160}
     161add_action( 'groups_delete_group', 'bp_groups_delete_group_delete_all_activity', 10 );
     162
     163/**
     164 * Delete group member activity if they leave or are removed within 5 minutes of
     165 * membership modification.
     166 *
     167 * If the user joined this group less than five minutes ago, remove the
     168 * joined_group activity so users cannot flood the activity stream by
     169 * joining/leaving the group in quick succession.
     170 *
     171 * @since BuddyPress (1.9.0)
     172 */
     173function bp_groups_leave_group_delete_recent_activity( $group_id, $user_id ) {
     174
     175    // Bail if Activity component is not active
     176    if ( ! bp_is_active( 'activity' ) ) {
     177        return;
     178    }
     179
     180    // Get the member's group membership information
     181    $membership = new BP_Groups_Member( $user_id, $group_id );
     182
     183    // Check the time period, and maybe delete their recent group activity
     184    if ( time() <= strtotime( '+5 minutes', (int) strtotime( $membership->date_modified ) ) ) {
     185        bp_activity_delete( array(
     186            'component' => buddypress()->groups->id,
     187            'type'      => 'joined_group',
     188            'user_id'   => $user_id,
     189            'item_id'   => $group_id
     190        ) );
     191    }
     192}
     193add_action( 'groups_leave_group',   'bp_groups_leave_group_delete_recent_activity', 10, 2 );
     194add_action( 'groups_remove_member', 'bp_groups_leave_group_delete_recent_activity', 10, 2 );
     195add_action( 'groups_ban_member',    'bp_groups_leave_group_delete_recent_activity', 10, 2 );
  • trunk/bp-groups/bp-groups-forums.php

    r7228 r7624  
    77 * have a template screen associated with them. Usually they will send the user
    88 * back to the default screen after execution.
     9 *
     10 * Note that this file is only used for the retired version of bbPress (1.x) and
     11 * will see minimal updates as of BuddyPress 1.9.0.
    912 *
    1013 * @package BuddyPress
  • trunk/bp-groups/bp-groups-functions.php

    r7588 r7624  
    201201 */
    202202function groups_delete_group( $group_id ) {
    203     global $bp;
    204203
    205204    do_action( 'groups_before_delete_group', $group_id );
     
    207206    // Get the group object
    208207    $group = groups_get_group( array( 'group_id' => $group_id ) );
    209     if ( !$group->delete() )
    210         return false;
    211 
    212     // Delete all group activity from activity streams
    213     if ( bp_is_active( 'activity' ) )
    214         bp_activity_delete_by_item_id( array( 'item_id' => $group_id, 'component' => $bp->groups->id ) );
     208
     209    // Bail if group cannot be deleted
     210    if ( ! $group->delete() ) {
     211        return false;
     212    }
    215213
    216214    // Remove all outstanding invites for this group
    217215    groups_delete_all_group_invites( $group_id );
    218216
    219     // Remove all notifications for any user belonging to this group
    220     bp_core_delete_all_notifications_by_type( $group_id, $bp->groups->id );
    221 
    222     do_action( 'groups_delete_group', $group_id);
     217    do_action( 'groups_delete_group', $group_id );
    223218
    224219    return true;
     
    289284    }
    290285
    291     $membership = new BP_Groups_Member( $user_id, $group_id );
    292 
    293286    // This is exactly the same as deleting an invite, just is_confirmed = 1 NOT 0.
    294     if ( !groups_uninvite_user( $user_id, $group_id ) )
    295         return false;
    296 
    297     /**
    298      * If the user joined this group less than five minutes ago, remove the
    299      * joined_group activity so users cannot flood the activity stream by
    300      * joining/leaving the group in quick succession.
    301      */
    302     if ( bp_is_active( 'activity' ) && time() <= strtotime( '+5 minutes', (int)strtotime( $membership->date_modified ) ) )
    303         bp_activity_delete( array( 'component' => $bp->groups->id, 'type' => 'joined_group', 'user_id' => $user_id, 'item_id' => $group_id ) );
     287    if ( !groups_uninvite_user( $user_id, $group_id ) ) {
     288        return false;
     289    }
    304290
    305291    bp_core_add_message( __( 'You successfully left the group.', 'buddypress' ) );
     
    697683    // slip through), delete all existing invitations/requests and return true
    698684    if ( groups_is_user_member( $user_id, $group_id ) ) {
    699         if ( groups_check_user_has_invite( $user_id, $group_id ) )
     685        if ( groups_check_user_has_invite( $user_id, $group_id ) ) {
    700686            groups_delete_invite( $user_id, $group_id );
    701 
    702         if ( groups_check_for_membership_request( $user_id, $group_id ) )
     687        }
     688
     689        if ( groups_check_for_membership_request( $user_id, $group_id ) ) {
    703690            groups_delete_membership_request( $user_id, $group_id );
     691        }
    704692
    705693        return true;
     
    709697    $member->accept_invite();
    710698
    711     if ( !$member->save() )
    712         return false;
     699    if ( !$member->save() ) {
     700        return false;
     701    }
    713702
    714703    // Remove request to join
    715     if ( $member->check_for_membership_request( $user_id, $group_id ) )
     704    if ( $member->check_for_membership_request( $user_id, $group_id ) ) {
    716705        $member->delete_request( $user_id, $group_id );
     706    }
    717707
    718708    // Modify group meta
    719709    groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
    720710
    721     bp_core_mark_notifications_by_item_id( $user_id, $group_id, buddypress()->groups->id, 'group_invite' );
    722 
    723711    do_action( 'groups_accept_invite', $user_id, $group_id );
     712
    724713    return true;
    725714}
    726715
    727716function groups_reject_invite( $user_id, $group_id ) {
    728     if ( !BP_Groups_Member::delete( $user_id, $group_id ) )
     717    if ( ! BP_Groups_Member::delete( $user_id, $group_id ) )
    729718        return false;
    730719
     
    735724
    736725function groups_delete_invite( $user_id, $group_id ) {
    737 
    738     $delete = BP_Groups_Member::delete_invite( $user_id, $group_id );
    739 
    740     if ( !empty( $delete ) ) {
    741         bp_core_mark_notifications_by_item_id( $user_id, $group_id, buddypress()->groups->id, 'group_invite' );
    742     }
    743 
    744     return $delete;
     726    if ( ! BP_Groups_Member::delete_invite( $user_id, $group_id ) )
     727        return false;
     728
     729    do_action( 'groups_delete_invite', $user_id, $group_id );
     730
     731    return true;
    745732}
    746733
     
    906893function groups_accept_membership_request( $membership_id, $user_id = 0, $group_id = 0 ) {
    907894
    908     if ( !empty( $user_id ) && !empty( $group_id ) )
     895    if ( !empty( $user_id ) && !empty( $group_id ) ) {
    909896        $membership = new BP_Groups_Member( $user_id, $group_id );
    910     else
     897    } else {
    911898        $membership = new BP_Groups_Member( false, false, $membership_id );
     899    }
    912900
    913901    $membership->accept_request();
    914902
    915     if ( !$membership->save() )
    916         return false;
     903    if ( !$membership->save() ) {
     904        return false;
     905    }
    917906
    918907    // Check if the user has an outstanding invite, if so delete it.
    919     if ( groups_check_user_has_invite( $membership->user_id, $membership->group_id ) )
     908    if ( groups_check_user_has_invite( $membership->user_id, $membership->group_id ) ) {
    920909        groups_delete_invite( $membership->user_id, $membership->group_id );
    921 
    922     // Record this in activity streams
    923     $group = groups_get_group( array( 'group_id' => $membership->group_id ) );
    924 
    925     groups_record_activity( array(
    926         'action'  => apply_filters_ref_array( 'groups_activity_membership_accepted_action', array( sprintf( __( '%1$s joined the group %2$s', 'buddypress'), bp_core_get_userlink( $membership->user_id ), '<a href="' . bp_get_group_permalink( $group ) . '">' . esc_attr( $group->name ) . '</a>' ), $membership->user_id, &$group ) ),
    927         'type'    => 'joined_group',
    928         'item_id' => $membership->group_id,
    929         'user_id' => $membership->user_id
    930     ) );
    931 
    932     // Send a notification to the user.
    933     groups_notification_membership_request_completed( $membership->user_id, $membership->group_id, true );
    934 
    935     do_action( 'groups_membership_accepted', $membership->user_id, $membership->group_id );
     910    }
     911
     912    do_action( 'groups_membership_accepted', $membership->user_id, $membership->group_id, true );
    936913
    937914    return true;
     
    939916
    940917function groups_reject_membership_request( $membership_id, $user_id = 0, $group_id = 0 ) {
    941     if ( !$membership = groups_delete_membership_request( $membership_id, $user_id, $group_id ) )
    942         return false;
    943 
    944     // Send a notification to the user.
    945     groups_notification_membership_request_completed( $membership->user_id, $membership->group_id, false );
    946 
    947     do_action( 'groups_membership_rejected', $membership->user_id, $membership->group_id );
     918    if ( !$membership = groups_delete_membership_request( $membership_id, $user_id, $group_id ) ) {
     919        return false;
     920    }
     921
     922    do_action( 'groups_membership_rejected', $membership->user_id, $membership->group_id, false );
    948923
    949924    return true;
     
    10751050
    10761051function groups_remove_data_for_user( $user_id ) {
    1077     global $bp;
    1078 
    10791052    BP_Groups_Member::delete_all_for_user( $user_id );
    1080 
    1081     bp_core_delete_notifications_from_user( $user_id, $bp->groups->id, 'new_membership_request' );
    10821053
    10831054    do_action( 'groups_remove_data_for_user', $user_id );
  • trunk/bp-groups/bp-groups-notifications.php

    r6917 r7624  
    1313// Exit if accessed directly
    1414if ( !defined( 'ABSPATH' ) ) exit;
     15
     16/** Emails ********************************************************************/
    1517
    1618function groups_notification_group_updated( $group_id ) {
     
    5759function groups_notification_new_membership_request( $requesting_user_id, $admin_id, $group_id, $membership_id ) {
    5860
    59     bp_core_add_notification( $requesting_user_id, $admin_id, 'groups', 'new_membership_request', $group_id );
     61    if ( bp_is_active( 'notifications' ) ) {
     62        bp_notifications_add_notification( $requesting_user_id, $admin_id, 'groups', 'new_membership_request', $group_id );
     63    }
    6064
    6165    if ( 'no' == bp_get_user_meta( $admin_id, 'notification_groups_membership_request', true ) )
     
    106110
    107111    // Post a screen notification first.
    108     if ( $accepted )
    109         bp_core_add_notification( $group_id, $requesting_user_id, 'groups', 'membership_request_accepted' );
    110     else
    111         bp_core_add_notification( $group_id, $requesting_user_id, 'groups', 'membership_request_rejected' );
     112    if ( bp_is_active( 'notifications' ) ) {
     113        if ( $accepted ) {
     114            bp_notifications_add_notification( $group_id, $requesting_user_id, 'groups', 'membership_request_accepted' );
     115        } else {
     116            bp_notifications_add_notification( $group_id, $requesting_user_id, 'groups', 'membership_request_rejected' );
     117        }
     118    }
    112119
    113120    if ( 'no' == bp_get_user_meta( $requesting_user_id, 'notification_membership_request_completed', true ) )
     
    160167    do_action( 'bp_groups_sent_membership_approved_email', $requesting_user_id, $subject, $message, $group_id );
    161168}
     169add_action( 'groups_membership_accepted', 'groups_notification_membership_request_completed', 10, 3 );
     170add_action( 'groups_membership_rejected', 'groups_notification_membership_request_completed', 10, 3 );
    162171
    163172function groups_notification_promoted_member( $user_id, $group_id ) {
     
    172181
    173182    // Post a screen notification first.
    174     bp_core_add_notification( $group_id, $user_id, 'groups', $type );
     183    if ( bp_is_active( 'notifications' ) ) {
     184        bp_notifications_add_notification( $group_id, $user_id, 'groups', $type );
     185    }
    175186
    176187    if ( 'no' == bp_get_user_meta( $user_id, 'notification_groups_admin_promotion', true ) )
     
    223234
    224235        // Post a screen notification first.
    225         bp_core_add_notification( $group->id, $invited_user_id, 'groups', 'group_invite' );
     236        if ( bp_is_active( 'notifications' ) ) {
     237            bp_notifications_add_notification( $group->id, $invited_user_id, 'groups', 'group_invite' );
     238        }
    226239
    227240        if ( 'no' == bp_get_user_meta( $invited_user_id, 'notification_groups_invite', true ) )
     
    265278    }
    266279}
     280
     281/** Notifications *************************************************************/
     282
     283/**
     284 * Format the BuddyBar/Toolbar notifications for the Groups component
     285 *
     286 * @since BuddyPress (1.0)
     287 * @param string $action The kind of notification being rendered
     288 * @param int $item_id The primary item id
     289 * @param int $secondary_item_id The secondary item id
     290 * @param int $total_items The total number of messaging-related notifications waiting for the user
     291 * @param string $format 'string' for BuddyBar-compatible notifications; 'array' for WP Toolbar
     292 */
     293function groups_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
     294
     295    switch ( $action ) {
     296        case 'new_membership_request':
     297            $group_id = $secondary_item_id;
     298            $requesting_user_id = $item_id;
     299
     300            $group = groups_get_group( array( 'group_id' => $group_id ) );
     301            $group_link = bp_get_group_permalink( $group );
     302
     303            // Set up the string and the filter
     304            // Because different values are passed to the filters, we'll return the
     305            // values inline
     306            if ( (int) $total_items > 1 ) {
     307                $text = sprintf( __( '%1$d new membership requests for the group "%2$s"', 'buddypress' ), (int) $total_items, $group->name );
     308                $filter = 'bp_groups_multiple_new_membership_requests_notification';
     309                $notification_link = $group_link . 'admin/membership-requests/?n=1';
     310
     311                if ( 'string' == $format ) {
     312                    return apply_filters( $filter, '<a href="' . $notification_link . '" title="' . __( 'Group Membership Requests', 'buddypress' ) . '">' . $text . '</a>', $group_link, $total_items, $group->name, $text, $notification_link );
     313                } else {
     314                    return apply_filters( $filter, array(
     315                        'link' => $notification_link,
     316                        'text' => $text
     317                    ), $group_link, $total_items, $group->name, $text, $notification_link );
     318                }
     319            } else {
     320                $user_fullname = bp_core_get_user_displayname( $requesting_user_id );
     321                $text = sprintf( __( '%s requests group membership', 'buddypress' ), $user_fullname );
     322                $filter = 'bp_groups_single_new_membership_request_notification';
     323                $notification_link = $group_link . 'admin/membership-requests/?n=1';
     324
     325                if ( 'string' == $format ) {
     326                    return apply_filters( $filter, '<a href="' . $notification_link . '" title="' . sprintf( __( '%s requests group membership', 'buddypress' ), $user_fullname ) . '">' . $text . '</a>', $group_link, $user_fullname, $group->name, $text, $notification_link );
     327                } else {
     328                    return apply_filters( $filter, array(
     329                        'link' => $notification_link,
     330                        'text' => $text
     331                    ), $group_link, $user_fullname, $group->name, $text, $notification_link );
     332                }
     333            }
     334
     335            break;
     336
     337        case 'membership_request_accepted':
     338            $group_id = $item_id;
     339
     340            $group = groups_get_group( array( 'group_id' => $group_id ) );
     341            $group_link = bp_get_group_permalink( $group );
     342
     343            if ( (int) $total_items > 1 ) {
     344                $text = sprintf( __( '%d accepted group membership requests', 'buddypress' ), (int) $total_items, $group->name );
     345                $filter = 'bp_groups_multiple_membership_request_accepted_notification';
     346                $notification_link = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() ) . '?n=1';
     347
     348                if ( 'string' == $format ) {
     349                    return apply_filters( $filter, '<a href="' . $notification_link . '" title="' . __( 'Groups', 'buddypress' ) . '">' . $text . '</a>', $total_items, $group->name, $text, $notification_link );
     350                } else {
     351                    return apply_filters( $filter, array(
     352                        'link' => $notification_link,
     353                        'text' => $text
     354                    ), $total_items, $group->name, $text, $notification_link );
     355                }
     356            } else {
     357                $text = sprintf( __( 'Membership for group "%s" accepted', 'buddypress' ), $group->name );
     358                $filter = 'bp_groups_single_membership_request_accepted_notification';
     359                $notification_link = $group_link . '?n=1';
     360
     361                if ( 'string' == $format ) {
     362                    return apply_filters( $filter, '<a href="' . $notification_link . '">' . $text . '</a>', $group_link, $group->name, $text, $notification_link );
     363                } else {
     364                    return apply_filters( $filter, array(
     365                        'link' => $notification_link,
     366                        'text' => $text
     367                    ), $group_link, $group->name, $text, $notification_link );
     368                }
     369            }
     370
     371            break;
     372
     373        case 'membership_request_rejected':
     374            $group_id = $item_id;
     375
     376            $group = groups_get_group( array( 'group_id' => $group_id ) );
     377            $group_link = bp_get_group_permalink( $group );
     378
     379            if ( (int) $total_items > 1 ) {
     380                $text = sprintf( __( '%d rejected group membership requests', 'buddypress' ), (int) $total_items, $group->name );
     381                $filter = 'bp_groups_multiple_membership_request_rejected_notification';
     382                $notification_link = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() ) . '?n=1';
     383
     384                if ( 'string' == $format ) {
     385                    return apply_filters( $filter, '<a href="' . $notification_link . '" title="' . __( 'Groups', 'buddypress' ) . '">' . $text . '</a>', $total_items, $group->name );
     386                } else {
     387                    return apply_filters( $filter, array(
     388                        'link' => $notification_link,
     389                        'text' => $text
     390                    ), $total_items, $group->name, $text, $notification_link );
     391                }
     392            } else {
     393                $text = sprintf( __( 'Membership for group "%s" rejected', 'buddypress' ), $group->name );
     394                $filter = 'bp_groups_single_membership_request_rejected_notification';
     395                $notification_link = $group_link . '?n=1';
     396
     397                if ( 'string' == $format ) {
     398                    return apply_filters( $filter, '<a href="' . $notification_link . '">' . $text . '</a>', $group_link, $group->name, $text, $notification_link );
     399                } else {
     400                    return apply_filters( $filter, array(
     401                        'link' => $notification_link,
     402                        'text' => $text
     403                    ), $group_link, $group->name, $text, $notification_link );
     404                }
     405            }
     406
     407            break;
     408
     409        case 'member_promoted_to_admin':
     410            $group_id = $item_id;
     411
     412            $group = groups_get_group( array( 'group_id' => $group_id ) );
     413            $group_link = bp_get_group_permalink( $group );
     414
     415            if ( (int) $total_items > 1 ) {
     416                $text = sprintf( __( 'You were promoted to an admin in %d groups', 'buddypress' ), (int) $total_items );
     417                $filter = 'bp_groups_multiple_member_promoted_to_admin_notification';
     418                $notification_link = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() ) . '?n=1';
     419
     420                if ( 'string' == $format ) {
     421                    return apply_filters( $filter, '<a href="' . $notification_link . '" title="' . __( 'Groups', 'buddypress' ) . '">' . $text . '</a>', $total_items, $text, $notification_link );
     422                } else {
     423                    return apply_filters( $filter, array(
     424                        'link' => $notification_link,
     425                        'text' => $text
     426                    ), $total_items, $text, $notification_link );
     427                }
     428            } else {
     429                $text = sprintf( __( 'You were promoted to an admin in the group "%s"', 'buddypress' ), $group->name );
     430                $filter = 'bp_groups_single_member_promoted_to_admin_notification';
     431                $notification_link = $group_link . '?n=1';
     432
     433                if ( 'string' == $format ) {
     434                    return apply_filters( $filter, '<a href="' . $notification_link . '">' . $text . '</a>', $group_link, $group->name, $text, $notification_link );
     435                } else {
     436                    return apply_filters( $filter, array(
     437                        'link' => $notification_link,
     438                        'text' => $text
     439                    ), $group_link, $group->name, $text, $notification_link );
     440                }
     441            }
     442
     443            break;
     444
     445        case 'member_promoted_to_mod':
     446            $group_id = $item_id;
     447
     448            $group = groups_get_group( array( 'group_id' => $group_id ) );
     449            $group_link = bp_get_group_permalink( $group );
     450
     451            if ( (int) $total_items > 1 ) {
     452                $text = sprintf( __( 'You were promoted to a mod in %d groups', 'buddypress' ), (int) $total_items );
     453                $filter = 'bp_groups_multiple_member_promoted_to_mod_notification';
     454                $notification_link = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() ) . '?n=1';
     455
     456                if ( 'string' == $format ) {
     457                    return apply_filters( $filter, '<a href="' . $notification_link . '" title="' . __( 'Groups', 'buddypress' ) . '">' . $text . '</a>', $total_items, $text, $notification_link );
     458                } else {
     459                    return apply_filters( $filter, array(
     460                        'link' => $notification_link,
     461                        'text' => $text
     462                    ), $total_items, $text, $notification_link );
     463                }
     464            } else {
     465                $text = sprintf( __( 'You were promoted to a mod in the group "%s"', 'buddypress' ), $group->name );
     466                $filter = 'bp_groups_single_member_promoted_to_mod_notification';
     467                $notification_link = $group_link . '?n=1';
     468
     469                if ( 'string' == $format ) {
     470                    return apply_filters( $filter, '<a href="' . $notification_link . '">' . $text . '</a>', $group_link, $group->name, $text, $notification_link );
     471                } else {
     472                    return apply_filters( $filter, array(
     473                        'link' => $notification_link,
     474                        'text' => $text
     475                    ), $group_link, $group->name, $text, $notification_link );
     476                }
     477            }
     478
     479            break;
     480
     481        case 'group_invite':
     482            $group_id = $item_id;
     483            $group = groups_get_group( array( 'group_id' => $group_id ) );
     484            $group_link = bp_get_group_permalink( $group );
     485
     486            $notification_link = bp_loggedin_user_domain() . bp_get_groups_slug() . '/invites/?n=1';
     487
     488            if ( (int) $total_items > 1 ) {
     489                $text = sprintf( __( 'You have %d new group invitations', 'buddypress' ), (int) $total_items );
     490                $filter = 'bp_groups_multiple_group_invite_notification';
     491
     492                if ( 'string' == $format ) {
     493                    return apply_filters( $filter, '<a href="' . $notification_link . '" title="' . __( 'Group Invites', 'buddypress' ) . '">' . $text . '</a>', $total_items, $text, $notification_link );
     494                } else {
     495                    return apply_filters( $filter, array(
     496                        'link' => $notification_link,
     497                        'text' => $text
     498                    ), $total_items, $text, $notification_link );
     499                }
     500            } else {
     501                $text = sprintf( __( 'You have an invitation to the group: %s', 'buddypress' ), $group->name );
     502                $filter = 'bp_groups_single_group_invite_notification';
     503
     504                if ( 'string' == $format ) {
     505                    return apply_filters( $filter, '<a href="' . $notification_link . '">' . $text . '</a>', $group_link, $group->name, $text, $notification_link );
     506                } else {
     507                    return apply_filters( $filter, array(
     508                        'link' => $notification_link,
     509                        'text' => $text
     510                    ), $group_link, $group->name, $text, $notification_link );
     511                }
     512            }
     513
     514            break;
     515    }
     516
     517    do_action( 'groups_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
     518
     519    return false;
     520}
     521
     522/**
     523 * Remove all notifications for any member belonging to a specific group
     524 *
     525 * @since BuddyPress (1.9.0)
     526 */
     527function bp_groups_delete_group_delete_all_notifications( $group_id ) {
     528    if ( bp_is_active( 'notifications' ) ) {
     529        bp_notifications_delete_all_notifications_by_type( $group_id, buddypress()->groups->id );
     530    }   
     531}
     532add_action( 'groups_delete_group', 'bp_groups_delete_group_delete_all_notifications', 10 );
     533
     534/**
     535 * Mark notifications read when a member accepts a group invitation
     536 *
     537 * @since BuddyPress (1.9.0)
     538 * @param int $user_id
     539 * @param int $group_id
     540 */
     541function bp_groups_accept_invite_mark_notifications( $user_id, $group_id ) {
     542    if ( bp_is_active( 'notifications' ) ) {
     543        bp_notifications_mark_notifications_by_item_id( $user_id, $group_id, buddypress()->groups->id, 'group_invite' );
     544    }
     545}
     546add_action( 'groups_accept_invite', 'bp_groups_accept_invite_mark_notifications', 10, 2 );
     547add_action( 'groups_reject_invite', 'bp_groups_accept_invite_mark_notifications', 10, 2 );
     548add_action( 'groups_delete_invite', 'bp_groups_accept_invite_mark_notifications', 10, 2 );
     549
     550/**
     551 * Mark notifications read when a member views their group memberships
     552 *
     553 * @since BuddyPress (1.9.0)
     554 */
     555function bp_groups_screen_my_groups_mark_notifications() {
     556
     557    // Delete group request notifications for the user
     558    if ( isset( $_GET['n'] ) && bp_is_active( 'notifications' ) ) {
     559
     560        // Get the necessary ID's
     561        $group_id = buddypress()->groups->id;
     562        $user_id  = bp_loggedin_user_id();
     563
     564        // Mark notifications read
     565        bp_notifications_mark_notifications_by_type( $user_id, $group_id, 'membership_request_accepted' );
     566        bp_notifications_mark_notifications_by_type( $user_id, $group_id, 'membership_request_rejected' );
     567        bp_notifications_mark_notifications_by_type( $user_id, $group_id, 'member_promoted_to_mod'      );
     568        bp_notifications_mark_notifications_by_type( $user_id, $group_id, 'member_promoted_to_admin'    );
     569    }
     570}
     571add_action( 'groups_screen_my_groups', 'bp_groups_screen_my_groups_mark_notifications', 10 );
     572add_action( 'groups_screen_home',      'bp_groups_screen_my_groups_mark_notifications', 10 );
     573
     574/**
     575 * Mark group invitation notifications read when a member views their invitations
     576 *
     577 * @since BuddyPress (1.9.0)
     578 */
     579function bp_groups_screen_invites_mark_notifications() {
     580    if ( bp_is_active( 'notifications' ) ) {
     581        bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->groups->id, 'group_invite' );
     582    }
     583}
     584add_action( 'groups_screen_invites', 'bp_groups_screen_invites_mark_notifications', 10 );
     585
     586/**
     587 * Mark group join requests read when an admin or moderator visits the group
     588 * administration area.
     589 *
     590 * @since BuddyPress (1.9.0)
     591 * @param int $group_id
     592 */
     593function bp_groups_screen_group_admin_requests_mark_notifications( $group_id ) {
     594    if ( bp_is_active( 'notifications' ) ) {
     595        bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), $group_id, 'new_membership_request' );
     596    }
     597}
     598add_action( 'groups_screen_group_admin_requests', 'bp_groups_screen_group_admin_requests_mark_notifications', 10 );
     599
     600/**
     601 * Delete new group membership notifications when a user is being deleted.
     602 *
     603 * @since BuddyPress (1.9.0)
     604 * @param int $user_id
     605 */
     606function bp_groups_remove_data_for_user_notifications( $user_id ) {
     607    if ( bp_is_active( 'notifications' ) ) {
     608        bp_notifications_delete_notifications_from_user( $user_id, buddypress()->groups->id, 'new_membership_request' );
     609    }
     610}
     611add_action( 'groups_remove_data_for_user', 'bp_groups_remove_data_for_user_notifications', 10 );
  • trunk/bp-groups/bp-groups-screens.php

    r7537 r7624  
    2828function groups_screen_my_groups() {
    2929
    30     $bp = buddypress();
    31 
    32     // Delete group request notifications for the user
    33     if ( isset( $_GET['n'] ) ) {
    34         bp_core_mark_notifications_by_type( bp_loggedin_user_id(), $bp->groups->id, 'membership_request_accepted' );
    35         bp_core_mark_notifications_by_type( bp_loggedin_user_id(), $bp->groups->id, 'membership_request_rejected' );
    36         bp_core_mark_notifications_by_type( bp_loggedin_user_id(), $bp->groups->id, 'member_promoted_to_mod'      );
    37         bp_core_mark_notifications_by_type( bp_loggedin_user_id(), $bp->groups->id, 'member_promoted_to_admin'    );
    38     }
    39 
    4030    do_action( 'groups_screen_my_groups' );
    4131
     
    9484    }
    9585
    96     // Remove notifications
    97     bp_core_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->groups->id, 'group_invite' );
    98 
    9986    do_action( 'groups_screen_group_invites', $group_id );
    10087
     
    10491function groups_screen_group_home() {
    10592
    106     if ( ! bp_is_single_item() )
    107         return false;
    108 
    109     $bp = buddypress();
    110 
    111     if ( isset( $_GET['n'] ) ) {
    112         bp_core_mark_notifications_by_type( bp_loggedin_user_id(), $bp->groups->id, 'membership_request_accepted' );
    113         bp_core_mark_notifications_by_type( bp_loggedin_user_id(), $bp->groups->id, 'membership_request_rejected' );
    114         bp_core_mark_notifications_by_type( bp_loggedin_user_id(), $bp->groups->id, 'member_promoted_to_mod'      );
    115         bp_core_mark_notifications_by_type( bp_loggedin_user_id(), $bp->groups->id, 'member_promoted_to_admin'    );
     93    if ( ! bp_is_single_item() ) {
     94        return false;
    11695    }
    11796
     
    805784
    806785function groups_screen_group_admin_requests() {
    807     global $bp;
    808 
    809     if ( 'membership-requests' != bp_get_group_current_admin_tab() )
    810         return false;
    811 
    812     if ( ! bp_is_item_admin() || ( 'public' == $bp->groups->current_group->status ) )
    813         return false;
    814 
    815     // Remove any screen notifications
    816     bp_core_mark_notifications_by_type( bp_loggedin_user_id(), $bp->groups->id, 'new_membership_request' );
    817 
    818     $request_action = (string)bp_action_variable( 1 );
    819     $membership_id  = (int)bp_action_variable( 2 );
     786    $bp = buddypress();
     787
     788    if ( 'membership-requests' != bp_get_group_current_admin_tab() ) {
     789        return false;
     790    }
     791
     792    if ( ! bp_is_item_admin() || ( 'public' == $bp->groups->current_group->status ) ) {
     793        return false;
     794    }
     795
     796    $request_action = (string) bp_action_variable( 1 );
     797    $membership_id  = (int) bp_action_variable( 2 );
    820798
    821799    if ( !empty( $request_action ) && !empty( $membership_id ) ) {
Note: See TracChangeset for help on using the changeset viewer.