Skip to:
Content

BuddyPress.org


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.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 );
Note: See TracChangeset for help on using the changeset viewer.