Changeset 6649
- Timestamp:
- 12/18/2012 12:18:32 AM (12 years ago)
- Location:
- trunk/bp-activity
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-activity/bp-activity-filters.php
r6342 r6649 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' ); … … 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 … … 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() 205 * @uses bp_core_get_user_domain() 206 * 207 * @return string $content Content filtered for mentions 208 */ 209 function bp_activity_at_name_filter( $content, $activity_id = 0 ) { 210 211 // Try to find mentions 212 $usernames = bp_activity_find_mentions( $content ); 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 ) { 220 $content = preg_replace( '/(@' . $username . '\b)/', "<a href='" . bp_core_get_user_domain( $user_id ) . "' rel='nofollow'>@$username</a>", $content ); 221 } 222 223 // Return the content 224 return $content; 225 } 226 227 /** 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. 232 * 233 * @since BuddyPress (1.5) 234 * 235 * @param obj $activity 236 * 237 * @uses bp_activity_find_mentions() 238 */ 239 function bp_activity_at_name_filter_updates( $activity ) { 240 // If activity was marked as spam, stop the rest of this function. 241 if ( ! empty( $activity->is_spam ) ) 242 return; 243 244 // Try to find mentions 245 $usernames = bp_activity_find_mentions( $activity->content ); 246 247 // We have mentions! 248 if ( ! empty( $usernames ) ) { 249 // Replace @mention text with userlinks 250 foreach( (array) $usernames as $user_id => $username ) { 251 $activity->content = preg_replace( '/(@' . $username . '\b)/', "<a href='" . bp_core_get_user_domain( $user_id ) . "' rel='nofollow'>@$username</a>", $activity->content ); 252 } 253 254 // Add our hook to send @mention emails after the activity item is saved 255 add_action( 'bp_activity_after_save', 'bp_activity_at_name_send_emails' ); 256 257 // temporary variable to avoid having to run bp_activity_find_mentions() again 258 buddypress()->activity->mentioned_users = $usernames; 259 } 260 } 261 262 /** 263 * Sends emails and BP notifications for @-mentioned users in the contents of 264 * an activity item. 265 * 266 * @since BuddyPress (1.7) 267 * 268 * @param obj $activity The BP_Activity_Activity object 269 * 207 270 * @uses bp_activity_at_message_notification() 208 * @uses bp_core_get_user_domain() 209 * @uses bp_activity_adjust_mention_count() 210 * 211 * @return string $content Content filtered for mentions 212 */ 213 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 222 $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 ); 271 * @uses bp_activity_update_mention_count_for_user() 272 */ 273 function bp_activity_at_name_send_emails( $activity ) { 274 // If our temporary variable doesn't exist, stop now. 275 if ( empty( buddypress()->activity->mentioned_users ) ) 276 return; 277 278 // Grab our temporary variable from bp_activity_at_name_filter_updates() 279 $usernames = buddypress()->activity->mentioned_users; 280 281 // Get rid of temporary variable 282 unset( buddypress()->activity->mentioned_users ); 283 284 // Send @mentions and setup BP notifications 285 foreach( (array) $usernames as $user_id => $username ) { 286 // If you want to disable notifications, you can use this filter to stop email sending 287 if ( apply_filters( 'bp_activity_at_name_do_notifications', true, $usernames ) ) { 288 bp_activity_at_message_notification( $activity->id, $user_id ); 235 289 } 236 290 237 $content = preg_replace( '/(@' . $username . '\b)/', "<a href='" . bp_core_get_user_domain( $user_id ) . "' rel='nofollow'>@$username</a>", $content ); 238 } 239 240 // Adjust the activity count for this item 241 if ( $activity_id ) 242 bp_activity_adjust_mention_count( $activity_id, 'add' ); 243 244 return $content; 245 } 246 247 /** 248 * Catch mentions in saved activity items 249 * 250 * @since BuddyPress (1.5) 251 * 252 * @param obj $activity 253 * 254 * @uses remove_filter() To remove the 'bp_activity_at_name_filter_updates' hook. 255 * @uses bp_activity_at_name_filter() 256 * @uses BP_Activity_Activity::save() {@link BP_Activity_Activity} 257 */ 258 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' ); 261 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 ); 264 265 // Resave the activity with the new content 266 $activity->save(); 291 // Updates mention count for the user 292 bp_activity_update_mention_count_for_user( $user_id, $activity->id ); 293 } 267 294 } 268 295 -
trunk/bp-activity/bp-activity-functions.php
r6479 r6649 29 29 30 30 /** 31 * Searches through the content of an activity item to locate usernames, designated by an @ sign 31 * Searches through the content of an activity item to locate usernames, 32 * designated by an @ sign. 32 33 * 33 34 * @since BuddyPress (1.5) 34 35 * 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 36 * @param string $content The content of the activity, usually found in $activity->content. 37 * @return mixed Associative array with user ID as key and username as value. Boolean false if no mentions found. 38 38 */ 39 39 function bp_activity_find_mentions( $content ) { … … 45 45 return false; 46 46 47 return $usernames; 47 $mentioned_users = array(); 48 49 // We've found some mentions! Check to see if users exist 50 foreach( (array) $usernames as $key => $username ) { 51 if ( bp_is_username_compatibility_mode() ) { 52 $user_id = username_exists( $username ); 53 } else { 54 $user_id = bp_core_get_userid_from_nicename( $username ); 55 } 56 57 // user ID exists, so let's add it to our array 58 if ( ! empty( $user_id ) ) { 59 $mentioned_users[$user_id] = $username; 60 } 61 } 62 63 if ( empty( $mentioned_users ) ) 64 return false; 65 66 return $mentioned_users; 48 67 } 49 68 … … 62 81 63 82 /** 64 * Adjusts new mention count for mentioned users when activity items are deleted or created 83 * Adjusts mention count for mentioned users in activity items. 84 * 85 * This function is useful if you only have the activity ID handy and you 86 * haven't parsed an activity item for @mentions yet. 87 * 88 * Currently, only used in {@link bp_activity_delete()}. 65 89 * 66 90 * @since BuddyPress (1.5) 67 91 * 92 * @param int $activity_id The unique id for the activity item 93 * @param string $action Can be 'delete' or 'add'. Defaults to 'add'. 94 * 95 * @uses bp_activity_find_mentions() 96 * @uses bp_activity_update_mention_count_for_user() 97 */ 98 function bp_activity_adjust_mention_count( $activity_id = 0, $action = 'add' ) { 99 if ( empty( $activity_id ) ) 100 return false; 101 102 // Get activity object 103 $activity = new BP_Activity_Activity( (int) $activity_id ); 104 105 // Try to find mentions 106 $usernames = bp_activity_find_mentions( strip_tags( $activity->content ) ); 107 108 // Still empty? Stop now 109 if ( empty( $usernames ) ) 110 return false; 111 112 // Increment mention count foreach mentioned user 113 foreach( (array) $usernames as $user_id => $username ) { 114 bp_activity_update_mention_count_for_user( $user_id, $activity_id, $action ); 115 } 116 } 117 118 /** 119 * Updates the mention count for the user in question. 120 * 121 * This function should be used when you've already parsed your activity item 122 * for @mentions. 123 * 124 * @since BuddyPress (1.7) 125 * 126 * @param int $user_id The user ID 68 127 * @param int $activity_id The unique id for the activity item 69 128 * @param string $action Can be 'delete' or 'add'. Defaults to 'add' 70 129 * 71 * @uses BP_Activity_Activity() {@link BP_Activity_Activity}72 * @uses bp_activity_find_mentions()73 * @uses bp_is_username_compatibility_mode()74 * @uses bp_core_get_userid_from_nicename()75 130 * @uses bp_get_user_meta() 76 131 * @uses bp_update_user_meta() 77 */ 78 function bp_activity_adjust_mention_count( $activity_id, $action = 'add' ) { 79 $activity = new BP_Activity_Activity( $activity_id ); 80 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; 132 * @return bool 133 */ 134 function bp_activity_update_mention_count_for_user( $user_id, $activity_id, $action = 'add' ) { 135 if ( empty( $user_id ) || empty( $activity_id ) ) 136 return false; 137 138 // Adjust the mention list and count for the member 139 $new_mention_count = (int) bp_get_user_meta( $user_id, 'bp_new_mention_count', true ); 140 if ( !$new_mentions = bp_get_user_meta( $user_id, 'bp_new_mentions', true ) ) 141 $new_mentions = array(); 142 143 switch ( $action ) { 144 case 'delete' : 145 $key = array_search( $activity_id, $new_mentions ); 146 147 if ( $key !== false ) { 148 unset( $new_mentions[$key] ); 110 149 } 111 150 112 // Get an updated mention count 113 $new_mention_count = count( $new_mentions ); 114 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 } 119 } 151 break; 152 153 case 'add' : 154 default : 155 if ( !in_array( $activity_id, $new_mentions ) ) { 156 $new_mentions[] = (int) $activity_id; 157 } 158 159 break; 160 } 161 162 // Get an updated mention count 163 $new_mention_count = count( $new_mentions ); 164 165 // Resave the user_meta 166 bp_update_user_meta( $user_id, 'bp_new_mention_count', $new_mention_count ); 167 bp_update_user_meta( $user_id, 'bp_new_mentions', $new_mentions ); 168 169 return true; 120 170 } 121 171
Note: See TracChangeset
for help on using the changeset viewer.