Skip to:
Content

BuddyPress.org

Ticket #4636: 4636.03.patch

File 4636.03.patch, 13.4 KB (added by boonebgorges, 11 years ago)
  • bp-xprofile/bp-xprofile-activity.php

    diff --git bp-xprofile/bp-xprofile-activity.php bp-xprofile/bp-xprofile-activity.php
    index 01ffcc3..98f2e0f 100644
    function bp_xprofile_new_avatar_activity() { 
    132132        ) );
    133133}
    134134add_action( 'xprofile_avatar_uploaded', 'bp_xprofile_new_avatar_activity' );
     135
     136/**
     137 * Add an activity item when a user has updated his profile.
     138 *
     139 * @since BuddyPress (2.0.0)
     140 *
     141 * @param int $user_id ID of the user who has updated his profile.
     142 * @param array $field_ids IDs of the fields submitted.
     143 * @param bool $errors True if validation or saving errors occurred, otherwise
     144 *        false.
     145 * @param array $old_values Pre-save xprofile field values and visibility
     146 *        levels.
     147 * @param array $new_values Post-save xprofile field values and visibility
     148 *        levels.
     149 * @return bool True on success, false on failure.
     150 */
     151function bp_xprofile_updated_profile_activity( $user_id, $field_ids, $errors, $old_values = array(), $new_values = array() ) {
     152        // If there were errors, don't post
     153        if ( ! empty( $errors ) ) {
     154                return false;
     155        }
     156
     157        // Don't post if there have been no changes, or if the changes are
     158        // related solely to non-public fields
     159        $public_changes = false;
     160        foreach ( $new_values as $field_id => $new_value ) {
     161                $old_value       = isset( $old_values[ $field_id ] ) ? $old_values[ $field_id ] : '';
     162                $old_value_value = isset( $old_value['value'] ) ? $old_value['value'] : '';
     163                $old_value_visibility = isset( $old_value['visibility'] ) ? $old_value['visibility'] : '';
     164
     165                // Don't register changes to private fields
     166                if ( 'public' !== $new_value['visibility'] ) {
     167                        continue;
     168                }
     169
     170                // Don't register if there have been no changes
     171                if ( $new_value === $old_value ) {
     172                        continue;
     173                }
     174
     175                // Looks like we have public changes - no need to keep checking
     176                $public_changes = true;
     177                break;
     178        }
     179
     180        if ( ! $public_changes ) {
     181                return false;
     182        }
     183
     184        // Throttle to one activity of this type per 2 hours
     185        $existing = bp_activity_get( array(
     186                'max' => 1,
     187                'filter' => array(
     188                        'user_id' => $user_id,
     189                        'object'  => buddypress()->profile->id,
     190                        'action'  => 'updated_profile',
     191                ),
     192        ) );
     193
     194        if ( empty( $existing['activities'] ) ) {
     195                $throttle = false;
     196        } else {
     197                // Default throttle time is 2 hours. Filter to change (in seconds)
     198                $throttle_period = apply_filters( 'bp_xprofile_updated_profile_activity_throttle_time', 60 * 60 * 2 );
     199                $then = strtotime( $existing['activities'][0]->date_recorded );
     200                $now  = strtotime( bp_core_current_time() );
     201
     202                $throttle = ( $now - $then ) < $throttle_period;
     203        }
     204
     205        if ( $throttle ) {
     206                return false;
     207        }
     208
     209        // If we've reached this point, assemble and post the activity item
     210
     211        // Note for translators: The natural phrasing in English, "Joe updated
     212        // his profile", requires that we know Joe's gender, which we don't. If
     213        // your language doesn't have this restriction, feel free to use a more
     214        // natural translation.
     215        $profile_link = trailingslashit( bp_core_get_user_domain( $user_id ) . buddypress()->profile->slug );
     216        $action = sprintf( __( '%1$s&#8217;s profile was updated', 'buddypress' ), '<a href="' . $profile_link . '">' . bp_core_get_user_displayname( $user_id ) . '</a>' );
     217
     218        $retval = xprofile_record_activity( array(
     219                'user_id'      => $user_id,
     220                'action'       => $action,
     221                'primary_link' => $profile_link,
     222                'component'    => buddypress()->profile->id,
     223                'type'         => 'updated_profile',
     224        ) );
     225
     226        return (bool) $retval;
     227}
     228add_action( 'xprofile_updated_profile', 'bp_xprofile_updated_profile_activity', 10, 5 );
     229
     230/**
     231 * Add filters for xprofile activity types to Show dropdowns.
     232 *
     233 * @since BuddyPress (2.0.0)
     234 */
     235function xprofile_activity_filter_options() {
     236        ?>
     237
     238        <option value="updated_profile"><?php _e( 'Profile Updates', 'buddypress' ) ?></option>
     239
     240        <?php
     241}
     242add_action( 'bp_activity_filter_options', 'xprofile_activity_filter_options' );
  • bp-xprofile/bp-xprofile-screens.php

    diff --git bp-xprofile/bp-xprofile-screens.php bp-xprofile/bp-xprofile-screens.php
    index 2f9a9e6..db39dba 100644
    function xprofile_screen_edit_profile() { 
    102102                        $errors = false;
    103103
    104104                        // Now we've checked for required fields, lets save the values.
     105                        $old_values = $new_values = array();
    105106                        foreach ( (array) $posted_field_ids as $field_id ) {
    106107
    107108                                // 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.
    function xprofile_screen_edit_profile() { 
    120121                                // Save the visibility level
    121122                                $visibility_level = !empty( $_POST['field_' . $field_id . '_visibility'] ) ? $_POST['field_' . $field_id . '_visibility'] : 'public';
    122123                                xprofile_set_field_visibility_level( $field_id, bp_displayed_user_id(), $visibility_level );
     124
     125                                $old_values[ $field_id ] = array(
     126                                        'value'      => xprofile_get_field_data( $field_id, bp_displayed_user_id() ),
     127                                        'visibility' => xprofile_get_field_visibility_level( $field_id, bp_displayed_user_id() ),
     128                                );
     129                                $new_values[ $field_id ] = array(
     130                                        'value'      => $value,
     131                                        'visibility' => $visibility_level,
     132                                );
    123133                        }
    124134
    125                         do_action( 'xprofile_updated_profile', bp_displayed_user_id(), $posted_field_ids, $errors );
     135                        do_action( 'xprofile_updated_profile', bp_displayed_user_id(), $posted_field_ids, $errors, $old_values, $new_values );
    126136
    127137                        // Set the feedback messages
    128138                        if ( !empty( $errors ) ) {
  • new file tests/testcases/xprofile/activity.php

    diff --git tests/testcases/xprofile/activity.php tests/testcases/xprofile/activity.php
    new file mode 100644
    index 0000000..358575c
    - +  
     1<?php
     2
     3/**
     4 * @group xprofile
     5 * @group activity
     6 */
     7class BP_Tests_XProfile_Activity extends BP_UnitTestCase {
     8        protected $updated_profile_data = array();
     9
     10        /**
     11         * @group bp_xprofile_updated_profile_activity
     12         */
     13        public function test_bp_xprofile_updated_profile_activity_with_errors() {
     14                $d = $this->setup_updated_profile_data();
     15
     16                // Fake new/old values to ensure a change
     17                $old_values = array(
     18                        $this->updated_profile_data['f']->id => array(
     19                                'value'      => 'foo',
     20                                'visibility' => 'public',
     21                        ),
     22                );
     23                $new_values = array(
     24                        $this->updated_profile_data['f']->id => array(
     25                                'value'      => 'foo2',
     26                                'visibility' => 'public',
     27                        ),
     28                );
     29
     30                $this->assertFalse( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), true ) );
     31        }
     32
     33        /**
     34         * @group bp_xprofile_updated_profile_activity
     35         */
     36        public function test_bp_xprofile_updated_profile_activity_throttled() {
     37                $d = $this->setup_updated_profile_data();
     38
     39                $time = time();
     40                $prev_time = date( 'Y-m-d h:i:s', $time - ( 119 * 60 ) );
     41                $now_time = date( 'Y-m-d h:i:s', $time );
     42
     43                $this->factory->activity->create( array(
     44                        'user_id' => $d['u'],
     45                        'component' => buddypress()->profile->id,
     46                        'type' => 'updated_profile',
     47                        'date_recorded' => $prev_time,
     48                ) );
     49
     50                // Fake new/old values to ensure a change
     51                $old_values = array(
     52                        $this->updated_profile_data['f']->id => array(
     53                                'value'      => 'foo',
     54                                'visibility' => 'public',
     55                        ),
     56                );
     57                $new_values = array(
     58                        $this->updated_profile_data['f']->id => array(
     59                                'value'      => 'foo2',
     60                                'visibility' => 'public',
     61                        ),
     62                );
     63
     64                $this->assertFalse( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false ) );
     65        }
     66
     67        /**
     68         * @group bp_xprofile_updated_profile_activity
     69         */
     70        public function test_bp_xprofile_updated_profile_activity_outside_of_throttle() {
     71                $d = $this->setup_updated_profile_data();
     72
     73                $time = time();
     74                $prev_time = date( 'Y-m-d h:i:s', $time - ( 121 * 60 ) );
     75                $now_time = date( 'Y-m-d h:i:s', $time );
     76
     77                $this->factory->activity->create( array(
     78                        'user_id' => $d['u'],
     79                        'component' => buddypress()->profile->id,
     80                        'type' => 'updated_profile',
     81                        'recorded_time' => $prev_time,
     82                ) );
     83
     84                // Fake new/old values to ensure a change
     85                $old_values = array(
     86                        $this->updated_profile_data['f']->id => array(
     87                                'value'      => 'foo',
     88                                'visibility' => 'public',
     89                        ),
     90                );
     91                $new_values = array(
     92                        $this->updated_profile_data['f']->id => array(
     93                                'value'      => 'foo2',
     94                                'visibility' => 'public',
     95                        ),
     96                );
     97
     98                $this->assertTrue( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) );
     99
     100                $existing = bp_activity_get( array(
     101                        'max' => 1,
     102                        'filter' => array(
     103                                'user_id' => $user_id,
     104                                'object' => buddypress()->profile->id,
     105                                'action' => 'updated_profile',
     106                        ),
     107                ) );
     108
     109                $this->assertEquals( 1, $existing['total'] );
     110        }
     111
     112        /**
     113         * @group bp_xprofile_updated_profile_activity
     114         */
     115        public function test_bp_xprofile_updated_profile_activity_no_existing_activity() {
     116                $d = $this->setup_updated_profile_data();
     117
     118                // Fake new/old values to ensure a change
     119                $old_values = array(
     120                        $this->updated_profile_data['f']->id => array(
     121                                'value'      => 'foo',
     122                                'visibility' => 'public',
     123                        ),
     124                );
     125                $new_values = array(
     126                        $this->updated_profile_data['f']->id => array(
     127                                'value'      => 'foo2',
     128                                'visibility' => 'public',
     129                        ),
     130                );
     131
     132                $this->assertTrue( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) );
     133
     134                $existing = bp_activity_get( array(
     135                        'max' => 1,
     136                        'filter' => array(
     137                                'user_id' => $user_id,
     138                                'object' => buddypress()->profile->id,
     139                                'action' => 'updated_profile',
     140                        ),
     141                ) );
     142
     143                $this->assertEquals( 1, $existing['total'] );
     144        }
     145
     146        /**
     147         * @group bp_xprofile_updated_profile_activity
     148         */
     149        public function test_bp_xprofile_updated_profile_activity_no_changes() {
     150                $d = $this->setup_updated_profile_data();
     151
     152                $old_values = array(
     153                        $this->updated_profile_data['f']->id => array(
     154                                'value'      => 'foo',
     155                                'visibility' => 'public',
     156                        ),
     157                );
     158                $new_values = array(
     159                        $this->updated_profile_data['f']->id => array(
     160                                'value'      => 'foo',
     161                                'visibility' => 'public',
     162                        ),
     163                );
     164
     165                $this->assertFalse( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) );
     166        }
     167
     168        /**
     169         * @group bp_xprofile_updated_profile_activity
     170         */
     171        public function test_bp_xprofile_updated_profile_activity_no_public_changes() {
     172                $d = $this->setup_updated_profile_data();
     173
     174                $old_values = array(
     175                        $this->updated_profile_data['f']->id => array(
     176                                'value'      => 'foo',
     177                                'visibility' => 'loggedin',
     178                        ),
     179                );
     180                $new_values = array(
     181                        $this->updated_profile_data['f']->id => array(
     182                                'value'      => 'bar',
     183                                'visibility' => 'loggedin',
     184                        ),
     185                );
     186
     187                $this->assertFalse( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) );
     188        }
     189
     190        /**
     191         * @group bp_xprofile_updated_profile_activity
     192         */
     193        public function test_bp_xprofile_updated_profile_activity_public_changed_to_private() {
     194                $d = $this->setup_updated_profile_data();
     195
     196                $old_values = array(
     197                        $this->updated_profile_data['f']->id => array(
     198                                'value'      => 'foo',
     199                                'visibility' => 'public',
     200                        ),
     201                );
     202                $new_values = array(
     203                        $this->updated_profile_data['f']->id => array(
     204                                'value'      => 'bar',
     205                                'visibility' => 'loggedin',
     206                        ),
     207                );
     208
     209                $this->assertFalse( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) );
     210        }
     211
     212        /**
     213         * @group bp_xprofile_updated_profile_activity
     214         */
     215        public function test_bp_xprofile_updated_profile_activity_private_changed_to_public() {
     216                $d = $this->setup_updated_profile_data();
     217
     218                $old_values = array(
     219                        $this->updated_profile_data['f']->id => array(
     220                                'value'      => 'foo',
     221                                'visibility' => 'loggedin',
     222                        ),
     223                );
     224                $new_values = array(
     225                        $this->updated_profile_data['f']->id => array(
     226                                'value'      => 'foo',
     227                                'visibility' => 'public',
     228                        ),
     229                );
     230
     231                $this->assertTrue( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) );
     232        }
     233
     234        /**
     235         * @group bp_xprofile_updated_profile_activity
     236         */
     237        public function test_bp_xprofile_updated_profile_activity_field_didnt_previously_exist() {
     238                $d = $this->setup_updated_profile_data();
     239
     240                $old_values = array();
     241                $new_values = array(
     242                        $this->updated_profile_data['f']->id => array(
     243                                'value'      => 'bar',
     244                                'visibility' => 'public',
     245                        ),
     246                );
     247
     248                $this->assertTrue( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) );
     249        }
     250
     251        /**
     252         * @group bp_xprofile_updated_profile_activity
     253         */
     254        public function test_bp_xprofile_updated_profile_activity_public_changes() {
     255                $d = $this->setup_updated_profile_data();
     256
     257                $old_values = array(
     258                        $this->updated_profile_data['f']->id => array(
     259                                'value'      => 'foo',
     260                                'visibility' => 'public',
     261                        ),
     262                );
     263                $new_values = array(
     264                        $this->updated_profile_data['f']->id => array(
     265                                'value'      => 'bar',
     266                                'visibility' => 'public',
     267                        ),
     268                );
     269
     270                $this->assertTrue( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) );
     271        }
     272
     273
     274        protected function setup_updated_profile_data() {
     275                $this->updated_profile_data['u'] = $this->create_user();
     276                $this->updated_profile_data['g'] = $this->factory->xprofile_group->create();
     277                $this->updated_profile_data['f'] = $this->factory->xprofile_field->create( array(
     278                        'type' => 'textbox',
     279                        'field_group_id' => $this->updated_profile_data['g']->id,
     280                ) );
     281
     282        }
     283}