<!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][7998] trunk: Reinstate updated_profile activity item</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 { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { 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/7998">7998</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2014-02-27 19:51:28 +0000 (Thu, 27 Feb 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Reinstate updated_profile activity item

Once upon a time, a user's activity stream was updated when that user updated
his/her profile. This changeset reintroduces (and rethinks) that functionality.

- New phrasing is "Boone's profile was updated", to avoid gendered pronouns in
  English
- updated_profile items are limited to one per user per two-hour period
- Activity items are posted only when at least one publicly-visible xprofile
  field has been changed

Fixes <a href="http://buddypress.trac.wordpress.org/ticket/4636">#4636</a>

Props ericlewis, boonebgorges</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpxprofilebpxprofileactivityphp">trunk/bp-xprofile/bp-xprofile-activity.php</a></li>
<li><a href="#trunkbpxprofilebpxprofilescreensphp">trunk/bp-xprofile/bp-xprofile-screens.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkteststestcasesxprofileactivityphp">trunk/tests/testcases/xprofile/activity.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpxprofilebpxprofileactivityphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-xprofile/bp-xprofile-activity.php (7997 => 7998)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-xprofile/bp-xprofile-activity.php       2014-02-27 19:48:22 UTC (rev 7997)
+++ trunk/bp-xprofile/bp-xprofile-activity.php  2014-02-27 19:51:28 UTC (rev 7998)
</span><span class="lines">@@ -132,3 +132,111 @@
</span><span class="cx">  ) );
</span><span class="cx"> }
</span><span class="cx"> add_action( 'xprofile_avatar_uploaded', 'bp_xprofile_new_avatar_activity' );
</span><ins>+
+/**
+ * Add an activity item when a user has updated his profile.
+ *
+ * @since BuddyPress (2.0.0)
+ *
+ * @param int $user_id ID of the user who has updated his profile.
+ * @param array $field_ids IDs of the fields submitted.
+ * @param bool $errors True if validation or saving errors occurred, otherwise
+ *        false.
+ * @param array $old_values Pre-save xprofile field values and visibility
+ *        levels.
+ * @param array $new_values Post-save xprofile field values and visibility
+ *        levels.
+ * @return bool True on success, false on failure.
+ */
+function bp_xprofile_updated_profile_activity( $user_id, $field_ids, $errors, $old_values = array(), $new_values = array() ) {
+       // If there were errors, don't post
+       if ( ! empty( $errors ) ) {
+               return false;
+       }
+
+       // Don't post if there have been no changes, or if the changes are
+       // related solely to non-public fields
+       $public_changes = false;
+       foreach ( $new_values as $field_id => $new_value ) {
+               $old_value            = isset( $old_values[ $field_id ] ) ? $old_values[ $field_id ] : '';
+               $old_value_value      = isset( $old_value['value'] ) ? $old_value['value'] : '';
+               $old_value_visibility = isset( $old_value['visibility'] ) ? $old_value['visibility'] : '';
+
+               // Don't register changes to private fields
+               if ( 'public' !== $new_value['visibility'] ) {
+                       continue;
+               }
+
+               // Don't register if there have been no changes
+               if ( $new_value === $old_value ) {
+                       continue;
+               }
+
+               // Looks like we have public changes - no need to keep checking
+               $public_changes = true;
+               break;
+       }
+
+       if ( ! $public_changes ) {
+               return false;
+       }
+
+       // Throttle to one activity of this type per 2 hours
+       $existing = bp_activity_get( array(
+               'max' => 1,
+               'filter' => array(
+                       'user_id' => $user_id,
+                       'object'  => buddypress()->profile->id,
+                       'action'  => 'updated_profile',
+               ),
+       ) );
+
+       if ( empty( $existing['activities'] ) ) {
+               $throttle = false;
+       } else {
+               // Default throttle time is 2 hours. Filter to change (in seconds)
+               $throttle_period = apply_filters( 'bp_xprofile_updated_profile_activity_throttle_time', 60 * 60 * 2 );
+               $then = strtotime( $existing['activities'][0]->date_recorded );
+               $now  = strtotime( bp_core_current_time() );
+
+               $throttle = ( $now - $then ) < $throttle_period;
+       }
+
+       if ( $throttle ) {
+               return false;
+       }
+
+       // If we've reached this point, assemble and post the activity item
+
+       // Note for translators: The natural phrasing in English, "Joe updated
+       // his profile", requires that we know Joe's gender, which we don't. If
+       // your language doesn't have this restriction, feel free to use a more
+       // natural translation.
+       $profile_link = trailingslashit( bp_core_get_user_domain( $user_id ) . buddypress()->profile->slug );
+       $action = sprintf( __( '%1$s&#8217;s profile was updated', 'buddypress' ), '<a href="' . $profile_link . '">' . bp_core_get_user_displayname( $user_id ) . '</a>' );
+
+       $retval = xprofile_record_activity( array(
+               'user_id'      => $user_id,
+               'action'       => $action,
+               'primary_link' => $profile_link,
+               'component'    => buddypress()->profile->id,
+               'type'         => 'updated_profile',
+       ) );
+
+       return (bool) $retval;
+}
+add_action( 'xprofile_updated_profile', 'bp_xprofile_updated_profile_activity', 10, 5 );
+
+/**
+ * Add filters for xprofile activity types to Show dropdowns.
+ *
+ * @since BuddyPress (2.0.0)
+ */
+function xprofile_activity_filter_options() {
+       ?>
+
+       <option value="updated_profile"><?php _e( 'Profile Updates', 'buddypress' ) ?></option>
+
+       <?php
+}
+add_action( 'bp_activity_filter_options', 'xprofile_activity_filter_options' );
</ins></span></pre></div>
<a id="trunkbpxprofilebpxprofilescreensphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-xprofile/bp-xprofile-screens.php (7997 => 7998)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-xprofile/bp-xprofile-screens.php        2014-02-27 19:48:22 UTC (rev 7997)
+++ trunk/bp-xprofile/bp-xprofile-screens.php   2014-02-27 19:51:28 UTC (rev 7998)
</span><span class="lines">@@ -102,6 +102,7 @@
</span><span class="cx">                  $errors = false;
</span><span class="cx"> 
</span><span class="cx">                  // Now we've checked for required fields, lets save the values.
</span><ins>+                       $old_values = $new_values = array();
</ins><span class="cx">                   foreach ( (array) $posted_field_ids as $field_id ) {
</span><span class="cx"> 
</span><span class="cx">                          // Certain types of fields (checkboxes, multiselects) may come through empty. Save them as an empty array so that they don't get overwritten by the default on the next edit.
</span><span class="lines">@@ -120,9 +121,21 @@
</span><span class="cx">                          // Save the visibility level
</span><span class="cx">                          $visibility_level = !empty( $_POST['field_' . $field_id . '_visibility'] ) ? $_POST['field_' . $field_id . '_visibility'] : 'public';
</span><span class="cx">                          xprofile_set_field_visibility_level( $field_id, bp_displayed_user_id(), $visibility_level );
</span><ins>+
+                               // Save the old and new values. They will be
+                               // passed to the filter and used to determine
+                               // whether an activity item should be posted
+                               $old_values[ $field_id ] = array(
+                                       'value'      => xprofile_get_field_data( $field_id, bp_displayed_user_id() ),
+                                       'visibility' => xprofile_get_field_visibility_level( $field_id, bp_displayed_user_id() ),
+                               );
+                               $new_values[ $field_id ] = array(
+                                       'value'      => $value,
+                                       'visibility' => $visibility_level,
+                               );
</ins><span class="cx">                   }
</span><span class="cx"> 
</span><del>-                       do_action( 'xprofile_updated_profile', bp_displayed_user_id(), $posted_field_ids, $errors );
</del><ins>+                        do_action( 'xprofile_updated_profile', bp_displayed_user_id(), $posted_field_ids, $errors, $old_values, $new_values );
</ins><span class="cx"> 
</span><span class="cx">                  // Set the feedback messages
</span><span class="cx">                  if ( !empty( $errors ) ) {
</span></span></pre></div>
<a id="trunkteststestcasesxprofileactivityphp"></a>
<div class="addfile"><h4>Added: trunk/tests/testcases/xprofile/activity.php (0 => 7998)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/testcases/xprofile/activity.php                              (rev 0)
+++ trunk/tests/testcases/xprofile/activity.php 2014-02-27 19:51:28 UTC (rev 7998)
</span><span class="lines">@@ -0,0 +1,283 @@
</span><ins>+<?php
+
+/**
+ * @group xprofile
+ * @group activity
+ */
+class BP_Tests_XProfile_Activity extends BP_UnitTestCase {
+       protected $updated_profile_data = array();
+
+       /**
+        * @group bp_xprofile_updated_profile_activity
+        */
+       public function test_bp_xprofile_updated_profile_activity_with_errors() {
+               $d = $this->setup_updated_profile_data();
+
+               // Fake new/old values to ensure a change
+               $old_values = array(
+                       $this->updated_profile_data['f']->id => array(
+                               'value'      => 'foo',
+                               'visibility' => 'public',
+                       ),
+               );
+               $new_values = array(
+                       $this->updated_profile_data['f']->id => array(
+                               'value'      => 'foo2',
+                               'visibility' => 'public',
+                       ),
+               );
+
+               $this->assertFalse( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), true ) );
+       }
+
+       /**
+        * @group bp_xprofile_updated_profile_activity
+        */
+       public function test_bp_xprofile_updated_profile_activity_throttled() {
+               $d = $this->setup_updated_profile_data();
+
+               $time = time();
+               $prev_time = date( 'Y-m-d h:i:s', $time - ( 119 * 60 ) );
+               $now_time = date( 'Y-m-d h:i:s', $time );
+
+               $this->factory->activity->create( array(
+                       'user_id' => $d['u'],
+                       'component' => buddypress()->profile->id,
+                       'type' => 'updated_profile',
+                       'date_recorded' => $prev_time,
+               ) );
+
+               // Fake new/old values to ensure a change
+               $old_values = array(
+                       $this->updated_profile_data['f']->id => array(
+                               'value'      => 'foo',
+                               'visibility' => 'public',
+                       ),
+               );
+               $new_values = array(
+                       $this->updated_profile_data['f']->id => array(
+                               'value'      => 'foo2',
+                               'visibility' => 'public',
+                       ),
+               );
+
+               $this->assertFalse( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false ) );
+       }
+
+       /**
+        * @group bp_xprofile_updated_profile_activity
+        */
+       public function test_bp_xprofile_updated_profile_activity_outside_of_throttle() {
+               $d = $this->setup_updated_profile_data();
+
+               $time = time();
+               $prev_time = date( 'Y-m-d h:i:s', $time - ( 121 * 60 ) );
+               $now_time = date( 'Y-m-d h:i:s', $time );
+
+               $this->factory->activity->create( array(
+                       'user_id' => $d['u'],
+                       'component' => buddypress()->profile->id,
+                       'type' => 'updated_profile',
+                       'recorded_time' => $prev_time,
+               ) );
+
+               // Fake new/old values to ensure a change
+               $old_values = array(
+                       $this->updated_profile_data['f']->id => array(
+                               'value'      => 'foo',
+                               'visibility' => 'public',
+                       ),
+               );
+               $new_values = array(
+                       $this->updated_profile_data['f']->id => array(
+                               'value'      => 'foo2',
+                               'visibility' => 'public',
+                       ),
+               );
+
+               $this->assertTrue( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) );
+
+               $existing = bp_activity_get( array(
+                       'max' => 1,
+                       'filter' => array(
+                               'user_id' => $user_id,
+                               'object' => buddypress()->profile->id,
+                               'action' => 'updated_profile',
+                       ),
+               ) );
+
+               $this->assertEquals( 1, $existing['total'] );
+       }
+
+       /**
+        * @group bp_xprofile_updated_profile_activity
+        */
+       public function test_bp_xprofile_updated_profile_activity_no_existing_activity() {
+               $d = $this->setup_updated_profile_data();
+
+               // Fake new/old values to ensure a change
+               $old_values = array(
+                       $this->updated_profile_data['f']->id => array(
+                               'value'      => 'foo',
+                               'visibility' => 'public',
+                       ),
+               );
+               $new_values = array(
+                       $this->updated_profile_data['f']->id => array(
+                               'value'      => 'foo2',
+                               'visibility' => 'public',
+                       ),
+               );
+
+               $this->assertTrue( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) );
+
+               $existing = bp_activity_get( array(
+                       'max' => 1,
+                       'filter' => array(
+                               'user_id' => $user_id,
+                               'object' => buddypress()->profile->id,
+                               'action' => 'updated_profile',
+                       ),
+               ) );
+
+               $this->assertEquals( 1, $existing['total'] );
+       }
+
+       /**
+        * @group bp_xprofile_updated_profile_activity
+        */
+       public function test_bp_xprofile_updated_profile_activity_no_changes() {
+               $d = $this->setup_updated_profile_data();
+
+               $old_values = array(
+                       $this->updated_profile_data['f']->id => array(
+                               'value'      => 'foo',
+                               'visibility' => 'public',
+                       ),
+               );
+               $new_values = array(
+                       $this->updated_profile_data['f']->id => array(
+                               'value'      => 'foo',
+                               'visibility' => 'public',
+                       ),
+               );
+
+               $this->assertFalse( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) );
+       }
+
+       /**
+        * @group bp_xprofile_updated_profile_activity
+        */
+       public function test_bp_xprofile_updated_profile_activity_no_public_changes() {
+               $d = $this->setup_updated_profile_data();
+
+               $old_values = array(
+                       $this->updated_profile_data['f']->id => array(
+                               'value'      => 'foo',
+                               'visibility' => 'loggedin',
+                       ),
+               );
+               $new_values = array(
+                       $this->updated_profile_data['f']->id => array(
+                               'value'      => 'bar',
+                               'visibility' => 'loggedin',
+                       ),
+               );
+
+               $this->assertFalse( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) );
+       }
+
+       /**
+        * @group bp_xprofile_updated_profile_activity
+        */
+       public function test_bp_xprofile_updated_profile_activity_public_changed_to_private() {
+               $d = $this->setup_updated_profile_data();
+
+               $old_values = array(
+                       $this->updated_profile_data['f']->id => array(
+                               'value'      => 'foo',
+                               'visibility' => 'public',
+                       ),
+               );
+               $new_values = array(
+                       $this->updated_profile_data['f']->id => array(
+                               'value'      => 'bar',
+                               'visibility' => 'loggedin',
+                       ),
+               );
+
+               $this->assertFalse( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) );
+       }
+
+       /**
+        * @group bp_xprofile_updated_profile_activity
+        */
+       public function test_bp_xprofile_updated_profile_activity_private_changed_to_public() {
+               $d = $this->setup_updated_profile_data();
+
+               $old_values = array(
+                       $this->updated_profile_data['f']->id => array(
+                               'value'      => 'foo',
+                               'visibility' => 'loggedin',
+                       ),
+               );
+               $new_values = array(
+                       $this->updated_profile_data['f']->id => array(
+                               'value'      => 'foo',
+                               'visibility' => 'public',
+                       ),
+               );
+
+               $this->assertTrue( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) );
+       }
+
+       /**
+        * @group bp_xprofile_updated_profile_activity
+        */
+       public function test_bp_xprofile_updated_profile_activity_field_didnt_previously_exist() {
+               $d = $this->setup_updated_profile_data();
+
+               $old_values = array();
+               $new_values = array(
+                       $this->updated_profile_data['f']->id => array(
+                               'value'      => 'bar',
+                               'visibility' => 'public',
+                       ),
+               );
+
+               $this->assertTrue( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) );
+       }
+
+       /**
+        * @group bp_xprofile_updated_profile_activity
+        */
+       public function test_bp_xprofile_updated_profile_activity_public_changes() {
+               $d = $this->setup_updated_profile_data();
+
+               $old_values = array(
+                       $this->updated_profile_data['f']->id => array(
+                               'value'      => 'foo',
+                               'visibility' => 'public',
+                       ),
+               );
+               $new_values = array(
+                       $this->updated_profile_data['f']->id => array(
+                               'value'      => 'bar',
+                               'visibility' => 'public',
+                       ),
+               );
+
+               $this->assertTrue( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) );
+       }
+
+
+       protected function setup_updated_profile_data() {
+               $this->updated_profile_data['u'] = $this->create_user();
+               $this->updated_profile_data['g'] = $this->factory->xprofile_group->create();
+               $this->updated_profile_data['f'] = $this->factory->xprofile_field->create( array(
+                       'type' => 'textbox',
+                       'field_group_id' => $this->updated_profile_data['g']->id,
+               ) );
+
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>