Ticket #3980: 3980-with-stuffing.01.patch
File 3980-with-stuffing.01.patch, 11.4 KB (added by , 12 years ago) |
---|
-
bp-activity/bp-activity-filters.php
add_filter( 'bp_get_activity_feed_item_description', 'bp_activity_make_nofollow_ 89 89 add_filter( 'pre_comment_content', 'bp_activity_at_name_filter' ); 90 90 add_filter( 'group_forum_topic_text_before_save', 'bp_activity_at_name_filter' ); 91 91 add_filter( 'group_forum_post_text_before_save', 'bp_activity_at_name_filter' ); 92 add_filter( 'the_content', 92 add_filter( 'the_content', 'bp_activity_at_name_filter' ); 93 93 94 94 add_filter( 'bp_get_activity_parent_content', 'bp_create_excerpt' ); 95 95 … … add_filter( 'bp_get_activity_content', 'bp_activity_truncate_entry', 5 ); 99 99 /** Actions *******************************************************************/ 100 100 101 101 // At-name filter 102 add_action( 'bp_activity_ after_save', 'bp_activity_at_name_filter_updates' );102 add_action( 'bp_activity_before_save', 'bp_activity_at_name_filter_updates' ); 103 103 104 104 // Activity stream moderation 105 105 add_action( 'bp_activity_before_save', 'bp_activity_check_moderation_keys', 2, 1 ); … … function bp_activity_filter_kses( $content ) { 194 194 } 195 195 196 196 /** 197 * Finds and links @-mentioned users in the contents of a ctivity items197 * Finds and links @-mentioned users in the contents of a given item. 198 198 * 199 199 * @since BuddyPress (1.2) 200 200 * 201 * @param string $content The activity content202 * @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. 203 203 * 204 204 * @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()208 205 * @uses bp_core_get_user_domain() 209 * @uses bp_activity_adjust_mention_count()210 206 * 211 207 * @return string $content Content filtered for mentions 212 208 */ 213 209 function 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 }221 210 211 // Try to find mentions 222 212 $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 else227 $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 notifications233 if ( $activity_id && apply_filters( 'bp_activity_at_name_do_notifications', true ) ) {234 bp_activity_at_message_notification( $activity_id, $user_id );235 }236 213 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 ) { 237 220 $content = preg_replace( '/(@' . $username . '\b)/', "<a href='" . bp_core_get_user_domain( $user_id ) . "' rel='nofollow'>@$username</a>", $content ); 238 221 } 239 222 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 244 224 return $content; 245 225 } 246 226 247 227 /** 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. 249 232 * 250 233 * @since BuddyPress (1.5) 251 234 * 252 235 * @param obj $activity 253 236 * 254 * @uses remove_filter() To remove the 'bp_activity_at_name_filter_updates' hook.255 237 * @uses bp_activity_at_name_filter() 256 238 * @uses BP_Activity_Activity::save() {@link BP_Activity_Activity} 257 239 */ 258 240 function 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; 261 244 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 } 264 254 265 // Resave the activity with the new content 266 $activity->save(); 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 // temporary variable to avoid having to run bp_activity_find_mentions() again 259 buddypress()->activity->mentioned_users = $usernames; 260 } 261 } 262 263 /** 264 * Sends emails and BP notifications for @-mentioned users in the contents of 265 * an activity item. 266 * 267 * @since BuddyPress (1.7) 268 * 269 * @param obj $activity The BP_Activity_Activity_Object 270 * 271 * @uses bp_activity_find_mentions() 272 * @uses bp_activity_at_message_notification() 273 * @uses bp_activity_adjust_mention_count() 274 */ 275 function bp_activity_at_name_send_emails( $activity ) { 276 // If our temporary variable doesn't exist, stop now. 277 if ( empty( buddypress()->activity->mentioned_users ) ) 278 return; 279 280 // Grab our temporary variable from bp_activity_at_name_filter_updates() 281 $usernames = buddypress()->activity->mentioned_users; 282 283 // Get rid of temporary variable 284 unset( buddypress()->activity->mentioned_users ); 285 286 // Send @mentions and setup BP notifications 287 foreach( (array) $usernames as $user_id => $username ) { 288 // If you want to disable notifications, you can use this filter to stop email sending 289 if ( apply_filters( 'bp_activity_at_name_do_notifications', true, $usernames ) ) { 290 bp_activity_at_message_notification( $activity->id, $user_id ); 291 } 292 293 // Updates mention count for the user 294 bp_activity_update_mention_count_for_user( $user_id, $activity->id ); 295 } 267 296 } 268 297 269 298 /** -
bp-activity/bp-activity-functions.php
function bp_activity_has_directory() { 32 32 * 33 33 * @since BuddyPress (1.5) 34 34 * 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. 38 37 */ 39 38 function bp_activity_find_mentions( $content ) { 40 39 $pattern = '/[@]+([A-Za-z0-9-_\.@]+)\b/'; … … function bp_activity_find_mentions( $content ) { 44 43 if ( !$usernames = array_unique( $usernames[1] ) ) 45 44 return false; 46 45 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; 48 62 } 49 63 50 64 /** … … function bp_activity_clear_new_mentions( $user_id ) { 68 82 * @param int $activity_id The unique id for the activity item 69 83 * @param string $action Can be 'delete' or 'add'. Defaults to 'add' 70 84 * 71 * @uses BP_Activity_Activity() {@link BP_Activity_Activity}72 85 * @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 */ 88 function 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 * 75 111 * @uses bp_get_user_meta() 76 112 * @uses bp_update_user_meta() 113 * @return bool 77 114 */ 78 function bp_activity_adjust_mention_count( $activity_id, $action = 'add' ) { 79 $activity = new BP_Activity_Activity( $activity_id ); 115 function bp_activity_update_mention_count_for_user( $user_id, $activity_id, $action = 'add' ) { 116 if ( empty( $user_id ) || empty( $activity_id ) ) 117 return false; 80 118 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(); 111 123 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; 114 131 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; 119 138 } 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; 120 148 } 121 149 122 150 /**