Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
12/18/2012 12:18:32 AM (13 years ago)
Author:
r-a-y
Message:

Only fire the activity save hooks once. Fixes #3980.

Previously, bp_activity_at_name_filter_updates() would run the
BP_Activity_Activity::save() method again. So basically, all activity save
hooks would run twice; this caused problems for plugins that hooked into
any activity save hook as their code would run twice.

This commit changes the logic of how @mention activity items are linked and
sent so the activity save hooks are only run once.

bp_activity_at_name_filter_updates() now runs before an activity item is
saved and detects if @mentions are found. If @mentions exists, we add a
hook to send the emails after the activity item is saved -
bp_activity_at_name_send_emails().

Hat-tip boonebgorges for feedback.

For a full list of changes, view:
https://buddypress.trac.wordpress.org/ticket/3980#comment:11

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-activity/bp-activity-functions.php

    r6479 r6649  
    2929
    3030/**
    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.
    3233 *
    3334 * @since BuddyPress (1.5)
    3435 *
    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.
    3838 */
    3939function bp_activity_find_mentions( $content ) {
     
    4545        return false;
    4646
    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;
    4867}
    4968
     
    6281
    6382/**
    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()}.
    6589 *
    6690 * @since BuddyPress (1.5)
    6791 *
     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 */
     98function 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
    68127 * @param int $activity_id The unique id for the activity item
    69128 * @param string $action Can be 'delete' or 'add'. Defaults to 'add'
    70129 *
    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()
    75130 * @uses bp_get_user_meta()
    76131 * @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 */
     134function 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] );
    110149            }
    111150
    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;
    120170}
    121171
Note: See TracChangeset for help on using the changeset viewer.