<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][6649] trunk/bp-activity: Only fire the activity save hooks once.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://buddypress.trac.wordpress.org/changeset/6649">6649</a></dd>
<dt>Author</dt> <dd>r-a-y</dd>
<dt>Date</dt> <dd>2012-12-18 00:18:32 +0000 (Tue, 18 Dec 2012)</dd>
</dl>
<h3>Log Message</h3>
<pre>Only fire the activity save hooks once. Fixes <a href="http://buddypress.trac.wordpress.org/ticket/3980">#3980</a>.
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</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpactivitybpactivityfiltersphp">trunk/bp-activity/bp-activity-filters.php</a></li>
<li><a href="#trunkbpactivitybpactivityfunctionsphp">trunk/bp-activity/bp-activity-functions.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpactivitybpactivityfiltersphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-activity/bp-activity-filters.php (6648 => 6649)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-activity/bp-activity-filters.php        2012-12-17 18:01:16 UTC (rev 6648)
+++ trunk/bp-activity/bp-activity-filters.php        2012-12-18 00:18:32 UTC (rev 6649)
</span><span class="lines">@@ -89,7 +89,7 @@
</span><span class="cx"> add_filter( 'pre_comment_content', 'bp_activity_at_name_filter' );
</span><span class="cx"> add_filter( 'group_forum_topic_text_before_save', 'bp_activity_at_name_filter' );
</span><span class="cx"> add_filter( 'group_forum_post_text_before_save', 'bp_activity_at_name_filter' );
</span><del>-add_filter( 'the_content',                          'bp_activity_at_name_filter' );
</del><ins>+add_filter( 'the_content', 'bp_activity_at_name_filter' );
</ins><span class="cx">
</span><span class="cx"> add_filter( 'bp_get_activity_parent_content', 'bp_create_excerpt' );
</span><span class="cx">
</span><span class="lines">@@ -99,7 +99,7 @@
</span><span class="cx"> /** Actions *******************************************************************/
</span><span class="cx">
</span><span class="cx"> // At-name filter
</span><del>-add_action( 'bp_activity_after_save', 'bp_activity_at_name_filter_updates' );
</del><ins>+add_action( 'bp_activity_before_save', 'bp_activity_at_name_filter_updates' );
</ins><span class="cx">
</span><span class="cx"> // Activity stream moderation
</span><span class="cx"> add_action( 'bp_activity_before_save', 'bp_activity_check_moderation_keys', 2, 1 );
</span><span class="lines">@@ -194,79 +194,106 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><del>- * Finds and links @-mentioned users in the contents of activity items
</del><ins>+ * Finds and links @-mentioned users in the contents of a given item.
</ins><span class="cx"> *
</span><span class="cx"> * @since BuddyPress (1.2)
</span><span class="cx"> *
</span><del>- * @param string $content The activity content
- * @param int $activity_id The activity id
</del><ins>+ * @param string $content The contents of a given item.
+ * @param int $activity_id The activity id. Deprecated.
</ins><span class="cx"> *
</span><span class="cx"> * @uses bp_activity_find_mentions()
</span><del>- * @uses bp_is_username_compatibility_mode()
- * @uses bp_core_get_userid_from_nicename()
- * @uses bp_activity_at_message_notification()
</del><span class="cx"> * @uses bp_core_get_user_domain()
</span><del>- * @uses bp_activity_adjust_mention_count()
</del><span class="cx"> *
</span><span class="cx"> * @return string $content Content filtered for mentions
</span><span class="cx"> */
</span><span class="cx"> function bp_activity_at_name_filter( $content, $activity_id = 0 ) {
</span><del>-        if ( $activity_id & bp_is_active( 'activity' ) ) {
-                $activity = new BP_Activity_Activity( $activity_id );
</del><span class="cx">
</span><del>-                // If this activity has been marked as spam, don't do anything. This prevents @notifications being sent.
-                if ( !empty( $activity ) && $activity->is_spam )
-                        return $content;
-        }
-
</del><ins>+        // Try to find mentions
</ins><span class="cx">         $usernames = bp_activity_find_mentions( $content );
</span><del>-        foreach( (array) $usernames as $username ) {
-                if ( bp_is_username_compatibility_mode() )
-                        $user_id = username_exists( $username );
-                else
-                        $user_id = bp_core_get_userid_from_nicename( $username );
</del><span class="cx">
</span><del>-                if ( empty( $user_id ) )
-                        continue;
</del><ins>+        // No mentions? Stop now!
+        if ( empty( $usernames ) )
+                return $content;
</ins><span class="cx">
</span><del>-                // If an activity_id is provided, we can send email and BP notifications
-                if ( $activity_id && apply_filters( 'bp_activity_at_name_do_notifications', true ) ) {
-                        bp_activity_at_message_notification( $activity_id, $user_id );
-                }
-
</del><ins>+        // Linkify the mentions with the username
+        foreach( (array) $usernames as $user_id => $username ) {
</ins><span class="cx">                 $content = preg_replace( '/(@' . $username . '\b)/', "<a href='" . bp_core_get_user_domain( $user_id ) . "' rel='nofollow'>@$username</a>", $content );
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        // Adjust the activity count for this item
-        if ( $activity_id )
-                bp_activity_adjust_mention_count( $activity_id, 'add' );
-
</del><ins>+        // Return the content
</ins><span class="cx">         return $content;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><del>- * Catch mentions in saved activity items
</del><ins>+ * Catch mentions in activity items before they are saved into the database.
</ins><span class="cx"> *
</span><ins>+ * If mentions are found, replace @mention text with user links and add our
+ * hook to send mentions after the activity item is saved.
+ *
</ins><span class="cx"> * @since BuddyPress (1.5)
</span><span class="cx"> *
</span><span class="cx"> * @param obj $activity
</span><span class="cx"> *
</span><del>- * @uses remove_filter() To remove the 'bp_activity_at_name_filter_updates' hook.
- * @uses bp_activity_at_name_filter()
- * @uses BP_Activity_Activity::save() {@link BP_Activity_Activity}
</del><ins>+ * @uses bp_activity_find_mentions()
</ins><span class="cx"> */
</span><span class="cx"> function bp_activity_at_name_filter_updates( $activity ) {
</span><del>-        // Only run this function once for a given activity item
-        remove_filter( 'bp_activity_after_save', 'bp_activity_at_name_filter_updates' );
</del><ins>+        // If activity was marked as spam, stop the rest of this function.
+        if ( ! empty( $activity->is_spam ) )
+                return;
</ins><span class="cx">
</span><del>-        // Run the content through the linking filter, making sure to increment mention count
-        $activity->content = bp_activity_at_name_filter( $activity->content, $activity->id );
</del><ins>+        // Try to find mentions
+        $usernames = bp_activity_find_mentions( $activity->content );
</ins><span class="cx">
</span><del>-        // Resave the activity with the new content
-        $activity->save();
</del><ins>+        // We have mentions!
+        if ( ! empty( $usernames ) ) {
+                // Replace @mention text with userlinks
+                foreach( (array) $usernames as $user_id => $username ) {
+                        $activity->content = preg_replace( '/(@' . $username . '\b)/', "<a href='" . bp_core_get_user_domain( $user_id ) . "' rel='nofollow'>@$username</a>", $activity->content );
+                }
+
+                // Add our hook to send @mention emails after the activity item is saved
+                add_action( 'bp_activity_after_save', 'bp_activity_at_name_send_emails' );
+
+                // temporary variable to avoid having to run bp_activity_find_mentions() again
+                buddypress()->activity->mentioned_users = $usernames;
+        }
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * Sends emails and BP notifications for @-mentioned users in the contents of
+ * an activity item.
+ *
+ * @since BuddyPress (1.7)
+ *
+ * @param obj $activity The BP_Activity_Activity object
+ *
+ * @uses bp_activity_at_message_notification()
+ * @uses bp_activity_update_mention_count_for_user()
+ */
+function bp_activity_at_name_send_emails( $activity ) {
+        // If our temporary variable doesn't exist, stop now.
+        if ( empty( buddypress()->activity->mentioned_users ) )
+                return;
+
+        // Grab our temporary variable from bp_activity_at_name_filter_updates()
+        $usernames = buddypress()->activity->mentioned_users;
+
+        // Get rid of temporary variable
+        unset( buddypress()->activity->mentioned_users );
+
+        // Send @mentions and setup BP notifications
+        foreach( (array) $usernames as $user_id => $username ) {
+                // If you want to disable notifications, you can use this filter to stop email sending
+                if ( apply_filters( 'bp_activity_at_name_do_notifications', true, $usernames ) ) {
+                        bp_activity_at_message_notification( $activity->id, $user_id );
+                }
+
+                // Updates mention count for the user
+                bp_activity_update_mention_count_for_user( $user_id, $activity->id );
+        }
+}
+
+/**
</ins><span class="cx"> * Catches links in activity text so rel=nofollow can be added
</span><span class="cx"> *
</span><span class="cx"> * @since BuddyPress (1.2)
</span></span></pre></div>
<a id="trunkbpactivitybpactivityfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-activity/bp-activity-functions.php (6648 => 6649)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-activity/bp-activity-functions.php        2012-12-17 18:01:16 UTC (rev 6648)
+++ trunk/bp-activity/bp-activity-functions.php        2012-12-18 00:18:32 UTC (rev 6649)
</span><span class="lines">@@ -28,13 +28,13 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><del>- * Searches through the content of an activity item to locate usernames, designated by an @ sign
</del><ins>+ * Searches through the content of an activity item to locate usernames,
+ * designated by an @ sign.
</ins><span class="cx"> *
</span><span class="cx"> * @since BuddyPress (1.5)
</span><span class="cx"> *
</span><del>- * @param string $content The content of the activity, usually found in $activity->content
- *
- * @return bool|array $usernames Array of the found usernames that match existing users. False if no matches
</del><ins>+ * @param string $content The content of the activity, usually found in $activity->content.
+ * @return mixed Associative array with user ID as key and username as value. Boolean false if no mentions found.
</ins><span class="cx"> */
</span><span class="cx"> function bp_activity_find_mentions( $content ) {
</span><span class="cx">         $pattern = '/[@]+([A-Za-z0-9-_\.@]+)\b/';
</span><span class="lines">@@ -44,7 +44,26 @@
</span><span class="cx">         if ( !$usernames = array_unique( $usernames[1] ) )
</span><span class="cx">                 return false;
</span><span class="cx">
</span><del>-        return $usernames;
</del><ins>+        $mentioned_users = array();
+
+        // We've found some mentions! Check to see if users exist
+        foreach( (array) $usernames as $key => $username ) {
+                if ( bp_is_username_compatibility_mode() ) {
+                        $user_id = username_exists( $username );
+                } else {
+                        $user_id = bp_core_get_userid_from_nicename( $username );
+                }
+
+                // user ID exists, so let's add it to our array
+                if ( ! empty( $user_id ) ) {
+                        $mentioned_users[$user_id] = $username;
+                }
+        }
+
+        if ( empty( $mentioned_users ) )
+                return false;
+
+        return $mentioned_users;
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="lines">@@ -61,62 +80,93 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><del>- * Adjusts new mention count for mentioned users when activity items are deleted or created
</del><ins>+ * Adjusts mention count for mentioned users in activity items.
</ins><span class="cx"> *
</span><ins>+ * This function is useful if you only have the activity ID handy and you
+ * haven't parsed an activity item for @mentions yet.
+ *
+ * Currently, only used in {@link bp_activity_delete()}.
+ *
</ins><span class="cx"> * @since BuddyPress (1.5)
</span><span class="cx"> *
</span><span class="cx"> * @param int $activity_id The unique id for the activity item
</span><ins>+ * @param string $action Can be 'delete' or 'add'. Defaults to 'add'.
+ *
+ * @uses bp_activity_find_mentions()
+ * @uses bp_activity_update_mention_count_for_user()
+ */
+function bp_activity_adjust_mention_count( $activity_id = 0, $action = 'add' ) {
+        if ( empty( $activity_id ) )
+                return false;
+
+        // Get activity object
+        $activity = new BP_Activity_Activity( (int) $activity_id );
+
+        // Try to find mentions
+        $usernames = bp_activity_find_mentions( strip_tags( $activity->content ) );
+
+        // Still empty? Stop now
+        if ( empty( $usernames ) )
+                return false;
+
+        // Increment mention count foreach mentioned user
+        foreach( (array) $usernames as $user_id => $username ) {
+                bp_activity_update_mention_count_for_user( $user_id, $activity_id, $action );
+        }
+}
+
+/**
+ * Updates the mention count for the user in question.
+ *
+ * This function should be used when you've already parsed your activity item
+ * for @mentions.
+ *
+ * @since BuddyPress (1.7)
+ *
+ * @param int $user_id The user ID
+ * @param int $activity_id The unique id for the activity item
</ins><span class="cx"> * @param string $action Can be 'delete' or 'add'. Defaults to 'add'
</span><span class="cx"> *
</span><del>- * @uses BP_Activity_Activity() {@link BP_Activity_Activity}
- * @uses bp_activity_find_mentions()
- * @uses bp_is_username_compatibility_mode()
- * @uses bp_core_get_userid_from_nicename()
</del><span class="cx"> * @uses bp_get_user_meta()
</span><span class="cx"> * @uses bp_update_user_meta()
</span><ins>+ * @return bool
</ins><span class="cx"> */
</span><del>-function bp_activity_adjust_mention_count( $activity_id, $action = 'add' ) {
-        $activity = new BP_Activity_Activity( $activity_id );
</del><ins>+function bp_activity_update_mention_count_for_user( $user_id, $activity_id, $action = 'add' ) {
+        if ( empty( $user_id ) || empty( $activity_id ) )
+                return false;
</ins><span class="cx">
</span><del>-        if ( $usernames = bp_activity_find_mentions( strip_tags( $activity->content ) ) ) {
-                foreach( (array) $usernames as $username ) {
-                        if ( bp_is_username_compatibility_mode() )
-                                $user_id = username_exists( $username );
-                        else
-                                $user_id = bp_core_get_userid_from_nicename( $username );
</del><ins>+        // Adjust the mention list and count for the member
+        $new_mention_count = (int) bp_get_user_meta( $user_id, 'bp_new_mention_count', true );
+        if ( !$new_mentions = bp_get_user_meta( $user_id, 'bp_new_mentions', true ) )
+                $new_mentions = array();
</ins><span class="cx">
</span><del>-                        if ( empty( $user_id ) )
-                                continue;
</del><ins>+        switch ( $action ) {
+                case 'delete' :
+                        $key = array_search( $activity_id, $new_mentions );
</ins><span class="cx">
</span><del>-                        // Adjust the mention list and count for the member
-                        $new_mention_count = (int)bp_get_user_meta( $user_id, 'bp_new_mention_count', true );
-                        if ( !$new_mentions = bp_get_user_meta( $user_id, 'bp_new_mentions', true ) )
-                                $new_mentions = array();
</del><ins>+                        if ( $key !== false ) {
+                                unset( $new_mentions[$key] );
+                        }
</ins><span class="cx">
</span><del>-                        switch ( $action ) {
-                                case 'delete' :
-                                        $key = array_search( $activity_id, $new_mentions );
-                                        if ( $key !== false ) {
-                                                unset( $new_mentions[$key] );
-                                        }
-                                        break;
</del><ins>+                        break;
</ins><span class="cx">
</span><del>-                                case 'add' :
-                                default :
-                                        if ( !in_array( $activity_id, $new_mentions ) ) {
-                                                $new_mentions[] = (int) $activity_id;
-                                        }
-                                        break;
</del><ins>+                case 'add' :
+                default :
+                        if ( !in_array( $activity_id, $new_mentions ) ) {
+                                $new_mentions[] = (int) $activity_id;
</ins><span class="cx">                         }
</span><span class="cx">
</span><del>-                        // Get an updated mention count
-                        $new_mention_count = count( $new_mentions );
</del><ins>+                        break;
+        }
</ins><span class="cx">
</span><del>-                        // Resave the user_meta
-                        bp_update_user_meta( $user_id, 'bp_new_mention_count', $new_mention_count );
-                        bp_update_user_meta( $user_id, 'bp_new_mentions', $new_mentions );
-                }
-        }
</del><ins>+        // Get an updated mention count
+        $new_mention_count = count( $new_mentions );
+
+        // Resave the user_meta
+        bp_update_user_meta( $user_id, 'bp_new_mention_count', $new_mention_count );
+        bp_update_user_meta( $user_id, 'bp_new_mentions', $new_mentions );
+
+        return true;
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span></span></pre>
</div>
</div>
</body>
</html>