Skip to:
Content

BuddyPress.org

Ticket #3980: 3980-sans-stuffing.01.patch

File 3980-sans-stuffing.01.patch, 11.0 KB (added by r-a-y, 12 years ago)
  • bp-activity/bp-activity-filters.php

    add_filter( 'bp_get_activity_feed_item_description', 'bp_activity_make_nofollow_ 
    8989add_filter( 'pre_comment_content',                   'bp_activity_at_name_filter' );
    9090add_filter( 'group_forum_topic_text_before_save',    'bp_activity_at_name_filter' );
    9191add_filter( 'group_forum_post_text_before_save',     'bp_activity_at_name_filter' );
    92 add_filter( 'the_content',                           'bp_activity_at_name_filter' );
     92add_filter( 'the_content',                           'bp_activity_at_name_filter' );
    9393
    9494add_filter( 'bp_get_activity_parent_content',        'bp_create_excerpt' );
    9595
    add_filter( 'bp_get_activity_content', 'bp_activity_truncate_entry', 5 ); 
    9999/** Actions *******************************************************************/
    100100
    101101// At-name filter
    102 add_action( 'bp_activity_after_save', 'bp_activity_at_name_filter_updates' );
     102add_action( 'bp_activity_before_save', 'bp_activity_at_name_filter_updates' );
    103103
    104104// Activity stream moderation
    105105add_action( 'bp_activity_before_save', 'bp_activity_check_moderation_keys', 2, 1 );
    function bp_activity_filter_kses( $content ) { 
    194194}
    195195
    196196/**
    197  * Finds and links @-mentioned users in the contents of activity items
     197 * Finds and links @-mentioned users in the contents of a given item.
    198198 *
    199199 * @since BuddyPress (1.2)
    200200 *
    201  * @param string $content The activity content
    202  * @param int $activity_id The activity id
     201 * @param string $content The contents of a given item.
     202 * @param int $activity_id The activity id. Deprecated.
    203203 *
    204204 * @uses bp_activity_find_mentions()
    205  * @uses bp_is_username_compatibility_mode()
    206  * @uses bp_core_get_userid_from_nicename()
    207  * @uses bp_activity_at_message_notification()
    208205 * @uses bp_core_get_user_domain()
    209  * @uses bp_activity_adjust_mention_count()
    210206 *
    211207 * @return string $content Content filtered for mentions
    212208 */
    213209function bp_activity_at_name_filter( $content, $activity_id = 0 ) {
    214         if ( $activity_id & bp_is_active( 'activity' ) ) {
    215                 $activity = new BP_Activity_Activity( $activity_id );
    216 
    217                 // If this activity has been marked as spam, don't do anything. This prevents @notifications being sent.
    218                 if ( !empty( $activity ) && $activity->is_spam )
    219                         return $content;
    220         }
    221210
     211        // Try to find mentions
    222212        $usernames = bp_activity_find_mentions( $content );
    223         foreach( (array) $usernames as $username ) {
    224                 if ( bp_is_username_compatibility_mode() )
    225                         $user_id = username_exists( $username );
    226                 else
    227                         $user_id = bp_core_get_userid_from_nicename( $username );
    228 
    229                 if ( empty( $user_id ) )
    230                         continue;
    231 
    232                 // If an activity_id is provided, we can send email and BP notifications
    233                 if ( $activity_id && apply_filters( 'bp_activity_at_name_do_notifications', true ) ) {
    234                         bp_activity_at_message_notification( $activity_id, $user_id );
    235                 }
    236213
     214        // No mentions? Stop now!
     215        if ( empty( $usernames ) )
     216                return $content;
     217
     218        // Linkify the mentions with the username
     219        foreach( (array) $usernames as $user_id => $username ) {
    237220                $content = preg_replace( '/(@' . $username . '\b)/', "<a href='" . bp_core_get_user_domain( $user_id ) . "' rel='nofollow'>@$username</a>", $content );
    238221        }
    239222
    240         // Adjust the activity count for this item
    241         if ( $activity_id )
    242                 bp_activity_adjust_mention_count( $activity_id, 'add' );
    243 
     223        // Return the content
    244224        return $content;
    245225}
    246226
    247227/**
    248  * Catch mentions in saved activity items
     228 * Catch mentions in activity items before they are saved into the database.
     229 *
     230 * If mentions are found, replace @mention text with user links and add our
     231 * hook to send mentions after the activity item is saved.
    249232 *
    250233 * @since BuddyPress (1.5)
    251234 *
    252235 * @param obj $activity
    253236 *
    254  * @uses remove_filter() To remove the 'bp_activity_at_name_filter_updates' hook.
    255237 * @uses bp_activity_at_name_filter()
    256238 * @uses BP_Activity_Activity::save() {@link BP_Activity_Activity}
    257239 */
    258240function bp_activity_at_name_filter_updates( $activity ) {
    259         // Only run this function once for a given activity item
    260         remove_filter( 'bp_activity_after_save', 'bp_activity_at_name_filter_updates' );
     241        // If activity was marked as spam, stop the rest of this function.
     242        if ( ! empty( $activity->is_spam ) )
     243                return;
    261244
    262         // Run the content through the linking filter, making sure to increment mention count
    263         $activity->content = bp_activity_at_name_filter( $activity->content, $activity->id );
     245        // Try to find mentions
     246        $usernames = bp_activity_find_mentions( $activity->content );
     247
     248        // We have mentions!
     249        if ( ! empty( $usernames ) ) {
     250                // Replace @mention text with userlinks
     251                foreach( (array) $usernames as $user_id => $username ) {
     252                        $activity->content = preg_replace( '/(@' . $username . '\b)/', "<a href='" . bp_core_get_user_domain( $user_id ) . "' rel='nofollow'>@$username</a>", $activity->content );
     253                }
     254
     255                // Add our hook to send @mention emails after the activity item is saved
     256                add_action( 'bp_activity_after_save', 'bp_activity_at_name_send_emails' );
     257        }
     258}
    264259
    265         // Resave the activity with the new content
    266         $activity->save();
     260/**
     261 * Sends emails and BP notifications for @-mentioned users in the contents of
     262 * an activity item.
     263 *
     264 * @since BuddyPress (1.7)
     265 *
     266 * @param obj $activity The BP_Activity_Activity_Object
     267 *
     268 * @uses bp_activity_find_mentions()
     269 * @uses bp_activity_at_message_notification()
     270 * @uses bp_activity_adjust_mention_count()
     271 */
     272function bp_activity_at_name_send_emails( $activity ) {
     273        // Try to find mentions
     274        $usernames = bp_activity_find_mentions( $activity->content );
     275
     276        // Still empty? Stop now!
     277        if ( empty( $usernames ) )
     278                return;
     279
     280        // Send @mentions and setup BP notifications
     281        foreach( (array) $usernames as $user_id => $username ) {
     282                // If you want to disable notifications, you can use this filter to stop email sending
     283                if ( apply_filters( 'bp_activity_at_name_do_notifications', true, $usernames ) ) {
     284                        bp_activity_at_message_notification( $activity->id, $user_id );
     285                }
     286
     287                // Updates mention count for the user
     288                bp_activity_update_mention_count_for_user( $user_id, $activity->id );
     289        }
    267290}
    268291
    269292/**
  • bp-activity/bp-activity-functions.php

    function bp_activity_has_directory() { 
    3232 *
    3333 * @since BuddyPress (1.5)
    3434 *
    35  * @param string $content The content of the activity, usually found in $activity->content
    36  *
    37  * @return bool|array $usernames Array of the found usernames that match existing users. False if no matches
     35 * @param string $content The content of the activity, usually found in $activity->content.
     36 * @return mixed Associative array with user ID as key and username as value. Boolean false if no mentions found.
    3837 */
    3938function bp_activity_find_mentions( $content ) {
    4039        $pattern = '/[@]+([A-Za-z0-9-_\.@]+)\b/';
    function bp_activity_find_mentions( $content ) { 
    4443        if ( !$usernames = array_unique( $usernames[1] ) )
    4544                return false;
    4645
    47         return $usernames;
     46        $mentioned_users = array();
     47
     48        foreach( (array) $usernames as $key => $username ) {
     49                if ( bp_is_username_compatibility_mode() )
     50                        $user_id = username_exists( $username );
     51                else
     52                        $user_id = bp_core_get_userid_from_nicename( $username );
     53
     54                if ( ! empty( $user_id ) )
     55                        $mentioned_users[$user_id] = $username;
     56        }
     57
     58        if ( empty( $mentioned_users ) )
     59                return false;
     60
     61        return $mentioned_users;
    4862}
    4963
    5064/**
    function bp_activity_clear_new_mentions( $user_id ) { 
    6882 * @param int $activity_id The unique id for the activity item
    6983 * @param string $action Can be 'delete' or 'add'. Defaults to 'add'
    7084 *
    71  * @uses BP_Activity_Activity() {@link BP_Activity_Activity}
    7285 * @uses bp_activity_find_mentions()
    73  * @uses bp_is_username_compatibility_mode()
    74  * @uses bp_core_get_userid_from_nicename()
     86 * @uses bp_activity_update_mention_count_for_user()
     87 */
     88function bp_activity_adjust_mention_count( $activity_id, $action = 'add' ) {
     89        // Try to find mentions
     90        $usernames = bp_activity_find_mentions( strip_tags( $activity->content ) );
     91
     92        // Still empty? Stop now
     93        if ( empty( $usernames ) )
     94                return;
     95
     96        // Increment mention count foreach mentioned user
     97        foreach( (array) $usernames as $user_id => $username ) {
     98                bp_activity_update_mention_count_for_user( $user_id, $activity_id, $action );
     99        }
     100}
     101
     102/**
     103 * Updates the mention count for the user in question.
     104 *
     105 * @since BuddyPress (1.7)
     106 *
     107 * @param int $user_id The user ID
     108 * @param int $activity_id The unique id for the activity item
     109 * @param string $action Can be 'delete' or 'add'. Defaults to 'add'
     110 *
    75111 * @uses bp_get_user_meta()
    76112 * @uses bp_update_user_meta()
     113 * @return bool
    77114 */
    78 function bp_activity_adjust_mention_count( $activity_id, $action = 'add' ) {
    79         $activity = new BP_Activity_Activity( $activity_id );
     115function bp_activity_update_mention_count_for_user( $user_id, $activity_id, $action = 'add' ) {
     116        if ( empty( $user_id ) || empty( $activity_id ) )
     117                return false;
    80118
    81         if ( $usernames = bp_activity_find_mentions( strip_tags( $activity->content ) ) ) {
    82                 foreach( (array) $usernames as $username ) {
    83                         if ( bp_is_username_compatibility_mode() )
    84                                 $user_id = username_exists( $username );
    85                         else
    86                                 $user_id = bp_core_get_userid_from_nicename( $username );
    87 
    88                         if ( empty( $user_id ) )
    89                                 continue;
    90 
    91                         // Adjust the mention list and count for the member
    92                         $new_mention_count = (int)bp_get_user_meta( $user_id, 'bp_new_mention_count', true );
    93                         if ( !$new_mentions = bp_get_user_meta( $user_id, 'bp_new_mentions', true ) )
    94                                 $new_mentions = array();
    95 
    96                         switch ( $action ) {
    97                                 case 'delete' :
    98                                         $key = array_search( $activity_id, $new_mentions );
    99                                         if ( $key !== false ) {
    100                                                 unset( $new_mentions[$key] );
    101                                         }
    102                                         break;
    103 
    104                                 case 'add' :
    105                                 default :
    106                                         if ( !in_array( $activity_id, $new_mentions ) ) {
    107                                                 $new_mentions[] = (int) $activity_id;
    108                                         }
    109                                         break;
    110                         }
     119        // Adjust the mention list and count for the member
     120        $new_mention_count = (int)bp_get_user_meta( $user_id, 'bp_new_mention_count', true );
     121        if ( !$new_mentions = bp_get_user_meta( $user_id, 'bp_new_mentions', true ) )
     122                $new_mentions = array();
    111123
    112                         // Get an updated mention count
    113                         $new_mention_count = count( $new_mentions );
     124        switch ( $action ) {
     125                case 'delete' :
     126                        $key = array_search( $activity_id, $new_mentions );
     127                        if ( $key !== false ) {
     128                                unset( $new_mentions[$key] );
     129                        }
     130                        break;
    114131
    115                         // Resave the user_meta
    116                         bp_update_user_meta( $user_id, 'bp_new_mention_count', $new_mention_count );
    117                         bp_update_user_meta( $user_id, 'bp_new_mentions', $new_mentions );
    118                 }
     132                case 'add' :
     133                default :
     134                        if ( !in_array( $activity_id, $new_mentions ) ) {
     135                                $new_mentions[] = (int) $activity_id;
     136                        }
     137                        break;
    119138        }
     139
     140        // Get an updated mention count
     141        $new_mention_count = count( $new_mentions );
     142
     143        // Resave the user_meta
     144        bp_update_user_meta( $user_id, 'bp_new_mention_count', $new_mention_count );
     145        bp_update_user_meta( $user_id, 'bp_new_mentions', $new_mentions );
     146
     147        return true;
    120148}
    121149
    122150/**