Skip to:
Content

BuddyPress.org

Ticket #4636: 4636.02.patch

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

    diff --git a/bp-xprofile/bp-xprofile-activity.php b/bp-xprofile/bp-xprofile-activity.php
    index 01ffcc3..3c53ff0 100644
    a b 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 * @return bool True on success, false on failure.
     146 */
     147function bp_xprofile_updated_profile_activity( $user_id, $field_ids, $errors ) {
     148        // If there were errors, don't post
     149        if ( ! empty( $errors ) ) {
     150                return false;
     151        }
     152
     153        // Throttle to one activity of this type per 2 hours
     154        $existing = bp_activity_get( array(
     155                'max' => 1,
     156                'filter' => array(
     157                        'user_id' => $user_id,
     158                        'object' => buddypress()->profile->id,
     159                        'action' => 'updated_profile',
     160                ),
     161        ) );
     162
     163        if ( empty( $existing['activities'] ) ) {
     164                $throttle = false;
     165        } else {
     166                // Default throttle time is 2 hours. Filter to change (in seconds)
     167                $throttle_period = apply_filters( 'bp_xprofile_updated_profile_activity_throttle_time', 60*60*2 );
     168                $then = strtotime( $existing['activities'][0]->date_recorded );
     169                $now  = strtotime( bp_core_current_time() );
     170
     171                $throttle = ( $now - $then ) < $throttle_period;
     172        }
     173
     174        if ( $throttle ) {
     175                return false;
     176        }
     177
     178        // If we've reached this point, assemble and post the activity item
     179
     180        // Note for translators: The natural phrasing in English, "Joe updated
     181        // his profile", requires that we know Joe's gender, which we don't. If
     182        // your language doesn't have this restriction, feel free to use a more
     183        // natural translation.
     184        $profile_link = trailingslashit( bp_core_get_user_domain( $user_id ) . buddypress()->profile->slug );
     185        $action = sprintf( __( '%1$s&#8217;s profile was updated', 'buddypress' ), '<a href="' . $profile_link . '">' . bp_core_get_user_displayname( $user_id ) . '</a>' );
     186
     187        $retval = xprofile_record_activity( array(
     188                'user_id'      => $user_id,
     189                'action'       => $action,
     190                'primary_link' => $profile_link,
     191                'component'    => buddypress()->profile->id,
     192                'type'         => 'updated_profile',
     193        ) );
     194
     195        return (bool) $retval;
     196}
     197add_action( 'xprofile_updated_profile', 'bp_xprofile_updated_profile_activity', 10, 3 );
     198
     199/**
     200 * Add filters for xprofile activity types to Show dropdowns.
     201 *
     202 * @since BuddyPress (2.0.0)
     203 */
     204function xprofile_activity_filter_options() {
     205        ?>
     206
     207        <option value="updated_profile"><?php _e( 'Profile Updates', 'buddypress' ) ?></option>
     208
     209        <?php
     210}
     211add_action( 'bp_activity_filter_options', 'xprofile_activity_filter_options' );
  • new file tests/testcases/xprofile/activity.php

    diff --git a/tests/testcases/xprofile/activity.php b/tests/testcases/xprofile/activity.php
    new file mode 100644
    index 0000000..16a2169
    - +  
     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                $this->assertFalse( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), true ) );
     16        }
     17
     18        /**
     19         * @group bp_xprofile_updated_profile_activity
     20         */
     21        public function test_bp_xprofile_updated_profile_activity_throttled() {
     22                $d = $this->setup_updated_profile_data();
     23
     24                $time = time();
     25                $prev_time = date( 'Y-m-d h:i:s', $time - ( 119 * 60 ) );
     26                $now_time = date( 'Y-m-d h:i:s', $time );
     27
     28                $this->factory->activity->create( array(
     29                        'user_id' => $d['u'],
     30                        'component' => buddypress()->profile->id,
     31                        'type' => 'updated_profile',
     32                        'date_recorded' => $prev_time,
     33                ) );
     34
     35                $this->assertFalse( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false ) );
     36        }
     37
     38        /**
     39         * @group bp_xprofile_updated_profile_activity
     40         */
     41        public function test_bp_xprofile_updated_profile_activity_outside_of_throttle() {
     42                $d = $this->setup_updated_profile_data();
     43
     44                $time = time();
     45                $prev_time = date( 'Y-m-d h:i:s', $time - ( 121 * 60 ) );
     46                $now_time = date( 'Y-m-d h:i:s', $time );
     47
     48                $this->factory->activity->create( array(
     49                        'user_id' => $d['u'],
     50                        'component' => buddypress()->profile->id,
     51                        'type' => 'updated_profile',
     52                        'recorded_time' => $prev_time,
     53                ) );
     54
     55                $this->assertTrue( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false ) );
     56
     57                $existing = bp_activity_get( array(
     58                        'max' => 1,
     59                        'filter' => array(
     60                                'user_id' => $user_id,
     61                                'object' => buddypress()->profile->id,
     62                                'action' => 'updated_profile',
     63                        ),
     64                ) );
     65
     66                $this->assertEquals( 1, $existing['total'] );
     67        }
     68
     69        /**
     70         * @group bp_xprofile_updated_profile_activity
     71         */
     72        public function test_bp_xprofile_updated_profile_activity_no_existing_activity() {
     73                $d = $this->setup_updated_profile_data();
     74
     75                $this->assertTrue( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false ) );
     76
     77                $existing = bp_activity_get( array(
     78                        'max' => 1,
     79                        'filter' => array(
     80                                'user_id' => $user_id,
     81                                'object' => buddypress()->profile->id,
     82                                'action' => 'updated_profile',
     83                        ),
     84                ) );
     85
     86                $this->assertEquals( 1, $existing['total'] );
     87        }
     88
     89        protected function setup_updated_profile_data() {
     90                $this->updated_profile_data['u'] = $this->create_user();
     91                $this->updated_profile_data['g'] = $this->factory->xprofile_group->create();
     92                $this->updated_profile_data['f'] = $this->factory->xprofile_field->create( array(
     93                        'type' => 'textbox',
     94                        'field_group_id' => $this->updated_profile_data['g']->id,
     95                ) );
     96
     97        }
     98}