Skip to:
Content

BuddyPress.org

Ticket #5266: 5266.activity.patch

File 5266.activity.patch, 13.3 KB (added by johnjamesjacoby, 12 years ago)

Activity

  • plugins/buddypress/bp-activity/bp-activity-functions.php

    diff --git a/plugins/buddypress/bp-activity/bp-activity-functions.php b/plugins/buddypress/bp-activity/bp-activity-functions.php
    index 7c5ccc2..5a36b8b 100644
    a b  
    191191}
    192192
    193193/**
    194  * Format notifications related to activity.
    195  *
    196  * @since BuddyPress (1.5)
    197  *
    198  * @uses bp_loggedin_user_domain()
    199  * @uses bp_get_activity_slug()
    200  * @uses bp_core_get_user_displayname()
    201  * @uses apply_filters() To call the 'bp_activity_multiple_at_mentions_notification' hook.
    202  * @uses apply_filters() To call the 'bp_activity_single_at_mentions_notification' hook.
    203  * @uses do_action() To call 'activity_format_notifications' hook.
    204  *
    205  * @param string $action The type of activity item. Just 'new_at_mention' for now.
    206  * @param int $item_id The activity ID.
    207  * @param int $secondary_item_id In the case of at-mentions, this is the mentioner's ID.
    208  * @param int $total_items The total number of notifications to format.
    209  * @param string $format 'string' to get a BuddyBar-compatible notification, 'array' otherwise.
    210  * @return string $return Formatted @mention notification.
    211  */
    212 function bp_activity_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
    213 
    214         switch ( $action ) {
    215                 case 'new_at_mention':
    216                         $activity_id      = $item_id;
    217                         $poster_user_id   = $secondary_item_id;
    218                         $at_mention_link  = bp_loggedin_user_domain() . bp_get_activity_slug() . '/mentions/';
    219                         $at_mention_title = sprintf( __( '@%s Mentions', 'buddypress' ), bp_get_loggedin_user_username() );
    220 
    221                         if ( (int) $total_items > 1 ) {
    222                                 $text = sprintf( __( 'You have %1$d new mentions', 'buddypress' ), (int) $total_items );
    223                                 $filter = 'bp_activity_multiple_at_mentions_notification';
    224                         } else {
    225                                 $user_fullname = bp_core_get_user_displayname( $poster_user_id );
    226                                 $text =  sprintf( __( '%1$s mentioned you', 'buddypress' ), $user_fullname );
    227                                 $filter = 'bp_activity_single_at_mentions_notification';
    228                         }
    229                 break;
    230         }
    231 
    232         if ( 'string' == $format ) {
    233                 $return = apply_filters( $filter, '<a href="' . $at_mention_link . '" title="' . $at_mention_title . '">' . $text . '</a>', $at_mention_link, (int) $total_items, $activity_id, $poster_user_id );
    234         } else {
    235                 $return = apply_filters( $filter, array(
    236                         'text' => $text,
    237                         'link' => $at_mention_link
    238                 ), $at_mention_link, (int) $total_items, $activity_id, $poster_user_id );
    239         }
    240 
    241         do_action( 'activity_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
    242 
    243         return $return;
    244 }
    245 
    246 /**
    247194 * Determine a user's "mentionname", the name used for that user in @-mentions.
    248195 *
    249196 * @since BuddyPress (1.9.0)
     
    12661213 * @return int|bool The ID of the comment on success, otherwise false.
    12671214 */
    12681215function bp_activity_new_comment( $args = '' ) {
    1269         global $bp;
    12701216
    1271         $defaults = array(
     1217        $params = wp_parse_args( $args, array(
    12721218                'id'          => false,
    12731219                'content'     => false,
    12741220                'user_id'     => bp_loggedin_user_id(),
    12751221                'activity_id' => false, // ID of the root activity item
    12761222                'parent_id'   => false  // ID of a parent comment (optional)
    1277         );
     1223        ) );
    12781224
    1279         $params = wp_parse_args( $args, $defaults );
    12801225        extract( $params, EXTR_SKIP );
    12811226
    1282         if ( empty( $content ) || empty( $user_id ) || empty( $activity_id ) )
     1227        // Bail if missing necessary data
     1228        if ( empty( $content ) || empty( $user_id ) || empty( $activity_id ) ) {
    12831229                return false;
     1230        }
    12841231
    1285         if ( empty( $parent_id ) )
     1232        // Maybe set current activity ID as the parent
     1233        if ( empty( $parent_id ) ) {
    12861234                $parent_id = $activity_id;
     1235        }
    12871236
    12881237        // Check to see if the parent activity is hidden, and if so, hide this comment publically.
    1289         $activity = new BP_Activity_Activity( $activity_id );
     1238        $activity  = new BP_Activity_Activity( $activity_id );
    12901239        $is_hidden = ( (int) $activity->hide_sitewide ) ? 1 : 0;
    12911240
    12921241        // Insert the activity comment
     
    12941243                'id'                => $id,
    12951244                'action'            => apply_filters( 'bp_activity_comment_action', sprintf( __( '%s posted a new activity comment', 'buddypress' ), bp_core_get_userlink( $user_id ) ) ),
    12961245                'content'           => apply_filters( 'bp_activity_comment_content', $content ),
    1297                 'component'         => $bp->activity->id,
     1246                'component'         => buddypress()->activity->id,
    12981247                'type'              => 'activity_comment',
    12991248                'user_id'           => $user_id,
    13001249                'item_id'           => $activity_id,
    13011250                'secondary_item_id' => $parent_id,
    13021251                'hide_sitewide'     => $is_hidden
    13031252        ) );
    1304 
    1305         // Send an email notification if settings allow
    1306         bp_activity_new_comment_notification( $comment_id, $user_id, $params );
    13071253
    13081254        // Clear the comment cache for this activity
    13091255        wp_cache_delete( 'bp_activity_comments_' . $parent_id );
  • plugins/buddypress/bp-activity/bp-activity-notifications.php

    diff --git a/plugins/buddypress/bp-activity/bp-activity-notifications.php b/plugins/buddypress/bp-activity/bp-activity-notifications.php
    index a65a879..952e059 100644
    a b  
    1010// Exit if accessed directly
    1111if ( !defined( 'ABSPATH' ) ) exit;
    1212
     13/* Emails *********************************************************************/
     14
    1315/**
    1416 * Send email and BP notifications when a user is mentioned in an update.
    1517 *
    1618 * @since BuddyPress (1.2)
    1719 *
    18  * @uses bp_core_add_notification()
     20 * @uses bp_notifications_add_notification()
    1921 * @uses bp_get_user_meta()
    2022 * @uses bp_core_get_user_displayname()
    2123 * @uses bp_activity_get_permalink()
     
    5355        $subject = '';
    5456        $message = '';
    5557        $content = '';
    56 
    57         // Add the BP notification
    58         bp_core_add_notification( $activity_id, $receiver_user_id, 'activity', 'new_at_mention', $activity->user_id );
    5958
    6059        // Now email the user with the contents of the message (if they have enabled email notifications)
    6160        if ( 'no' != bp_get_user_meta( $receiver_user_id, 'notification_activity_new_mention', true ) ) {
     
    10099                        $message .= sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress' ), $settings_link );
    101100                }
    102101
    103                 /* Send the message */
     102                // Send the message
    104103                $to      = apply_filters( 'bp_activity_at_message_notification_to', $to );
    105104                $subject = apply_filters( 'bp_activity_at_message_notification_subject', $subject, $poster_name );
    106105                $message = apply_filters( 'bp_activity_at_message_notification_message', $message, $poster_name, $content, $message_link, $settings_link );
     
    108107                wp_mail( $to, $subject, $message );
    109108        }
    110109
    111         do_action( 'bp_activity_sent_mention_email', $activity, $subject, $message, $content );
     110        do_action( 'bp_activity_sent_mention_email', $activity, $subject, $message, $content, $receiver_user_id );
    112111}
    113112
    114113/**
     
    140139 * @param int $commenter_id The ID of the user who posted the comment.
    141140 * @param array $params {@link bp_activity_new_comment()}
    142141 */
    143 function bp_activity_new_comment_notification( $comment_id, $commenter_id, $params ) {
     142function bp_activity_new_comment_notification( $comment_id = 0, $commenter_id = 0, $params = array() ) {
    144143
    145144        // Set some default parameters
    146145        $activity_id = 0;
     
    192191         * If this is a reply to another comment, send an email notification to the
    193192         * author of the immediate parent comment.
    194193         */
    195         if ( empty( $parent_id ) || ( $activity_id == $parent_id ) )
     194        if ( empty( $parent_id ) || ( $activity_id == $parent_id ) ) {
    196195                return false;
     196        }
    197197
    198198        $parent_comment = new BP_Activity_Activity( $parent_id );
    199199
     
    236236                do_action( 'bp_activity_sent_reply_to_reply_email', $original_activity->user_id, $subject, $message, $comment_id, $commenter_id, $params );
    237237        }
    238238}
     239
     240/**
     241 * Helper method to map action arguments to function parameters
     242 *
     243 * @since BuddyPress (1.9.0)
     244 * @param int $comment_id
     245 * @param array $params
     246 */
     247function bp_activity_new_comment_notification_helper( $comment_id, $params ) {
     248        bp_activity_new_comment_notification( $comment_id, $params['user_id'], $params );
     249}
     250add_action( 'bp_activity_comment_posted', 'bp_activity_new_comment_notification_helper', 10, 2 );
     251
     252/** Notifications *************************************************************/
     253
     254/**
     255 * Format notifications related to activity.
     256 *
     257 * @since BuddyPress (1.5)
     258 *
     259 * @uses bp_loggedin_user_domain()
     260 * @uses bp_get_activity_slug()
     261 * @uses bp_core_get_user_displayname()
     262 * @uses apply_filters() To call the 'bp_activity_multiple_at_mentions_notification' hook.
     263 * @uses apply_filters() To call the 'bp_activity_single_at_mentions_notification' hook.
     264 * @uses do_action() To call 'activity_format_notifications' hook.
     265 *
     266 * @param string $action The type of activity item. Just 'new_at_mention' for now.
     267 * @param int $item_id The activity ID.
     268 * @param int $secondary_item_id In the case of at-mentions, this is the mentioner's ID.
     269 * @param int $total_items The total number of notifications to format.
     270 * @param string $format 'string' to get a BuddyBar-compatible notification, 'array' otherwise.
     271 * @return string $return Formatted @mention notification.
     272 */
     273function bp_activity_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
     274
     275        switch ( $action ) {
     276                case 'new_at_mention':
     277                        $activity_id      = $item_id;
     278                        $poster_user_id   = $secondary_item_id;
     279                        $at_mention_link  = bp_loggedin_user_domain() . bp_get_activity_slug() . '/mentions/';
     280                        $at_mention_title = sprintf( __( '@%s Mentions', 'buddypress' ), bp_get_loggedin_user_username() );
     281
     282                        if ( (int) $total_items > 1 ) {
     283                                $text = sprintf( __( 'You have %1$d new mentions', 'buddypress' ), (int) $total_items );
     284                                $filter = 'bp_activity_multiple_at_mentions_notification';
     285                        } else {
     286                                $user_fullname = bp_core_get_user_displayname( $poster_user_id );
     287                                $text =  sprintf( __( '%1$s mentioned you', 'buddypress' ), $user_fullname );
     288                                $filter = 'bp_activity_single_at_mentions_notification';
     289                        }
     290                break;
     291        }
     292
     293        if ( 'string' == $format ) {
     294                $return = apply_filters( $filter, '<a href="' . esc_url( $at_mention_link ) . '" title="' . esc_attr( $at_mention_title ) . '">' . esc_html( $text ) . '</a>', $at_mention_link, (int) $total_items, $activity_id, $poster_user_id );
     295        } else {
     296                $return = apply_filters( $filter, array(
     297                        'text' => $text,
     298                        'link' => $at_mention_link
     299                ), $at_mention_link, (int) $total_items, $activity_id, $poster_user_id );
     300        }
     301
     302        do_action( 'activity_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
     303
     304        return $return;
     305}
     306
     307/**
     308 * Notify a member when their nicename is mentioned in an activity stream item.
     309 *
     310 * Hooked to the 'bp_activity_sent_mention_email' action, we piggy back off the
     311 * existing email code for now, since it does the heavy lifting for us. In the
     312 * future when we separate emails from Notifications, this will need its own
     313 * 'bp_activity_at_name_send_emails' equivalent helper function.
     314 *
     315 * @since BuddyPress (1.9.0)
     316 *
     317 * @param obj $activity
     318 * @param string $subject (not used)
     319 * @param string $message (not used)
     320 * @param string $content (not used)
     321 * @param int $receiver_user_id
     322 */
     323function bp_activity_at_mention_add_notification( $activity, $subject, $message, $content, $receiver_user_id ) {
     324        if ( bp_is_active( 'notifications' ) ) {
     325                bp_notifications_add_notification( array(
     326                        'user_id'           => $receiver_user_id,
     327                        'item_id'           => $activity->id,
     328                        'secondary_item_id' => $activity->user_id,
     329                        'component_name'    => buddypress()->activity->id,
     330                        'component_action'  => 'new_at_mention',
     331                        'date_notified'     => bp_core_current_time(),
     332                        'is_new'            => 1,
     333                ) );
     334        }
     335}
     336add_action( 'bp_activity_sent_mention_email', 'bp_activity_at_mention_add_notification', 10, 5 );
     337
     338/**
     339 * Remove activity notifications when a user clicks on them.
     340 *
     341 * @since BuddyPress (1.5)
     342 *
     343 * @uses bp_notifications_mark_all_notifications_by_type()
     344 */
     345function bp_activity_remove_screen_notifications() {
     346        if ( bp_is_active( 'notifications' ) ) {
     347                bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->activity->id, 'new_at_mention' );
     348        }
     349}
     350add_action( 'bp_activity_screen_my_activity',               'bp_activity_remove_screen_notifications' );
     351add_action( 'bp_activity_screen_single_activity_permalink', 'bp_activity_remove_screen_notifications' );
     352add_action( 'bp_activity_screen_mentions',                  'bp_activity_remove_screen_notifications' );
     353
  • plugins/buddypress/bp-activity/bp-activity-screens.php

    diff --git a/plugins/buddypress/bp-activity/bp-activity-screens.php b/plugins/buddypress/bp-activity/bp-activity-screens.php
    index 66d51b0..fdc026e 100644
    a b  
    129129}
    130130
    131131/**
    132  * Remove activity notifications when a user clicks on them.
    133  *
    134  * @since BuddyPress (1.5)
    135  *
    136  * @uses bp_core_mark_all_notifications_by_type()
    137  */
    138 function bp_activity_remove_screen_notifications() {
    139         bp_core_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->activity->id, 'new_at_mention' );
    140 }
    141 add_action( 'bp_activity_screen_my_activity',               'bp_activity_remove_screen_notifications' );
    142 add_action( 'bp_activity_screen_single_activity_permalink', 'bp_activity_remove_screen_notifications' );
    143 add_action( 'bp_activity_screen_mentions',                  'bp_activity_remove_screen_notifications' );
    144 
    145 /**
    146132 * Reset the logged-in user's new mentions data when he visits his mentions screen.
    147133 *
    148134 * @since BuddyPress (1.5)