Changeset 6649 for trunk/bp-activity/bp-activity-functions.php
- Timestamp:
- 12/18/2012 12:18:32 AM (13 years ago)
- File:
-
- 1 edited
-
trunk/bp-activity/bp-activity-functions.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
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.