Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/30/2013 06:14:08 AM (13 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.
File:
1 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 );
Note: See TracChangeset for help on using the changeset viewer.