Skip to:
Content

BuddyPress.org

Changeset 9204


Ignore:
Timestamp:
11/29/2014 04:04:42 PM (10 years ago)
Author:
boonebgorges
Message:

Create an activity item when a group's details are updated.

When a name or description alone are updated, the activity item will display
the "from" and "to" values as part of the formatted activity action string. If
both items have been changed, the activity action will read "x changed the name
and description of the group y".

Fixes #5880.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-activity.php

    r9026 r9204  
    4141        'bp_groups_format_activity_action_joined_group',
    4242        __( 'Group Memberships', 'buddypress' ),
     43        array( 'activity', 'group', 'member', 'member_groups' )
     44    );
     45
     46    bp_activity_set_action(
     47        $bp->groups->id,
     48        'group_details_updated',
     49        __( 'Group details edited', 'buddypress' ),
     50        'bp_groups_format_activity_action_group_details_updated',
     51        __( 'Group Updates', 'buddypress' ),
    4352        array( 'activity', 'group', 'member', 'member_groups' )
    4453    );
     
    129138
    130139/**
     140 * Format 'group_details_updated' activity actions.
     141 *
     142 * @since BuddyPress (2.2.0)
     143 *
     144 * @param  string $action   Static activity action.
     145 * @param  object $activity Activity data object.
     146 * @return string
     147 */
     148function bp_groups_format_activity_action_group_details_updated( $action, $activity ) {
     149    $user_link = bp_core_get_userlink( $activity->user_id );
     150
     151    $group = groups_get_group( array(
     152        'group_id'        => $activity->item_id,
     153        'populate_extras' => false,
     154    ) );
     155    $group_link = '<a href="' . esc_url( bp_get_group_permalink( $group ) ) . '">' . esc_html( $group->name ) . '</a>';
     156
     157    /*
     158     * Changed group details are stored in groupmeta, keyed by the activity
     159     * timestamp. See {@link bp_groups_group_details_updated_add_activity()}.
     160     */
     161    $changed = groups_get_groupmeta( $activity->item_id, 'updated_details_' . $activity->date_recorded );
     162
     163    // No changed details were found, so use a generic message.
     164    if ( empty( $changed ) ) {
     165        $action = sprintf( __( '%1$s updated details for the group %2$s', 'buddypress' ), $user_link, $group_link );
     166
     167    // Name and description changed - to keep things short, don't describe changes in detail.
     168    } else if ( isset( $changed['name'] ) && isset( $changed['description'] ) ) {
     169        $action = sprintf( __( '%1$s changed the name and description of the group %2$s', 'buddypress' ), $user_link, $group_link );
     170
     171    // Name only.
     172    } else if ( ! empty( $changed['name']['old'] ) && ! empty( $changed['name']['new'] ) ) {
     173        $action = sprintf( __( '%1$s changed the name of the group %2$s from "%3$s" to "%4$s"', 'buddypress' ), $user_link, $group_link, esc_html( $changed['name']['old'] ), esc_html( $changed['name']['new'] ) );
     174
     175    // Description only.
     176    } else if ( ! empty( $changed['description']['old'] ) && ! empty( $changed['description']['new'] ) ) {
     177        $action = sprintf( __( '%1$s changed the description of the group %2$s from "%3$s" to "%4$s"', 'buddypress' ), $user_link, $group_link, esc_html( $changed['description']['old'] ), esc_html( $changed['description']['new'] ) );
     178
     179    }
     180
     181    return apply_filters( 'bp_groups_format_activity_action_joined_group', $action, $activity );
     182}
     183
     184/**
    131185 * Fetch data related to groups at the beginning of an activity loop.
    132186 *
     
    291345
    292346/**
     347 * Add an activity item when a group's details are updated.
     348 *
     349 * @since BuddyPress (2.2.0)
     350 *
     351 * @param  int             $group_id       ID of the group.
     352 * @param  BP_Groups_Group $old_grop       Group object before the details had been changed.
     353 * @param  bool            $notify_members True if the admin has opted to notify group members, otherwise false.
     354 * @return int|bool The ID of the activity on success. False on error.
     355 */
     356function bp_groups_group_details_updated_add_activity( $group_id, $old_group, $notify_members ) {
     357
     358    // Bail if Activity is not active.
     359    if ( ! bp_is_active( 'activity' ) ) {
     360        return false;
     361    }
     362
     363    if ( ! isset( $old_group->name ) || ! isset( $old_group->description ) ) {
     364        return false;
     365    }
     366
     367    // If the admin has opted not to notify members, don't post an activity item either
     368    if ( empty( $notify_members ) ) {
     369        return;
     370    }
     371
     372    $group = groups_get_group( array(
     373        'group_id' => $group_id,
     374    ) );
     375
     376    /*
     377     * Store the changed data, which will be used to generate the activity
     378     * action. Since we haven't yet created the activity item, we store the
     379     * old group data in groupmeta, keyed by the timestamp that we'll put
     380     * on the activity item.
     381     */
     382    $changed = array();
     383
     384    if ( $group->name !== $old_group->name ) {
     385        $changed['name'] = array(
     386            'old' => $old_group->name,
     387            'new' => $group->name,
     388        );
     389    }
     390
     391    if ( $group->description !== $old_group->description ) {
     392        $changed['description'] = array(
     393            'old' => $old_group->description,
     394            'new' => $group->description,
     395        );
     396    }
     397
     398    // If there are no changes, don't post an activity item.
     399    if ( empty( $changed ) ) {
     400        return;
     401    }
     402
     403    $time = bp_core_current_time();
     404    groups_update_groupmeta( $group_id, 'updated_details_' . $time, $changed );
     405
     406    // Record in activity streams.
     407    return groups_record_activity( array(
     408        'type'          => 'group_details_updated',
     409        'item_id'       => $group_id,
     410        'user_id'       => bp_loggedin_user_id(),
     411        'recorded_time' => $time,
     412
     413    ) );
     414
     415}
     416add_action( 'groups_details_updated', 'bp_groups_group_details_updated_add_activity', 10, 3 );
     417
     418/**
    293419 * Delete all activity items related to a specific group.
    294420 *
  • trunk/src/bp-groups/bp-groups-functions.php

    r9152 r9204  
    191191        return false;
    192192
    193     $group              = groups_get_group( array( 'group_id' => $group_id ) );
     193    $group     = groups_get_group( array( 'group_id' => $group_id ) );
     194    $old_group = clone $group;
     195
    194196    $group->name        = $group_name;
    195197    $group->description = $group_desc;
     
    202204    }
    203205
    204     do_action( 'groups_details_updated', $group->id );
     206    /**
     207     * Fired after a group's details are updated.
     208     *
     209     * @since BuddyPress (2.2.0)
     210     *
     211     * @param int             $value          ID of the group.
     212     * @param BP_Groups_Group $old_group      Group object, before being modified.
     213     * @param bool            $notify_members Whether to send an email notification to members about the change.
     214     */
     215    do_action( 'groups_details_updated', $group->id, $old_group, $notify_members );
    205216
    206217    return true;
  • trunk/tests/phpunit/testcases/groups/activity.php

    r9139 r9204  
    4949        $this->assertSame( $expected, $a_obj->action );
    5050    }
     51
     52    /**
     53     * @group activity_action
     54     * @group bp_groups_format_activity_action_group_details_updated
     55     */
     56    public function test_bp_groups_format_activity_action_group_details_updated_with_no_change() {
     57        $group = $this->factory->group->create_and_get();
     58        groups_edit_base_group_details( $group->id, $group->name, $group->description, true );
     59
     60        $a = bp_activity_get( array(
     61            'component' => buddypress()->groups->id,
     62            'action' => 'group_details_updated',
     63            'item_id' => $group->id,
     64        ) );
     65
     66        $this->assertTrue( empty( $a['activities'] ) );
     67    }
     68
     69    /**
     70     * @group activity_action
     71     * @group bp_groups_format_activity_action_group_details_updated
     72     */
     73    public function test_bp_groups_format_activity_action_group_details_updated_with_notify_members_false() {
     74        $group = $this->factory->group->create_and_get();
     75        groups_edit_base_group_details( $group->id, 'Foo', $group->description, false );
     76
     77        $a = bp_activity_get( array(
     78            'component' => buddypress()->groups->id,
     79            'action' => 'group_details_updated',
     80            'item_id' => $group->id,
     81        ) );
     82
     83        $this->assertTrue( empty( $a['activities'] ) );
     84    }
     85
     86    /**
     87     * @group activity_action
     88     * @group bp_groups_format_activity_action_group_details_updated
     89     */
     90    public function test_bp_groups_format_activity_action_group_details_updated_with_updated_name() {
     91        $old_user = get_current_user_id();
     92        $u = $this->factory->user->create();
     93        $this->set_current_user( $u );
     94
     95        $group = $this->factory->group->create_and_get();
     96        groups_edit_base_group_details( $group->id, 'Foo', $group->description, true );
     97
     98        $a = bp_activity_get( array(
     99            'component' => buddypress()->groups->id,
     100            'action' => 'group_details_updated',
     101            'item_id' => $group->id,
     102        ) );
     103
     104        $this->assertNotEmpty( $a['activities'] );
     105
     106        $expected = sprintf( __( '%s changed the name of the group %s from "%s" to "%s"', 'buddypress' ), bp_core_get_userlink( $u ),  '<a href="' . bp_get_group_permalink( $group ) . '">Foo</a>', $group->name, 'Foo' );
     107        $this->assertSame( $expected, $a['activities'][0]->action );
     108
     109        $this->set_current_user( $old_user );
     110    }
     111
     112    /**
     113     * @group activity_action
     114     * @group bp_groups_format_activity_action_group_details_updated
     115     */
     116    public function test_bp_groups_format_activity_action_group_details_updated_with_updated_description() {
     117        $old_user = get_current_user_id();
     118        $u = $this->factory->user->create();
     119        $this->set_current_user( $u );
     120
     121        $group = $this->factory->group->create_and_get();
     122        groups_edit_base_group_details( $group->id, $group->name, 'Bar', true );
     123
     124        $a = bp_activity_get( array(
     125            'component' => buddypress()->groups->id,
     126            'action' => 'group_details_updated',
     127            'item_id' => $group->id,
     128        ) );
     129
     130        $this->assertNotEmpty( $a['activities'] );
     131
     132        $expected = sprintf( __( '%s changed the description of the group %s from "%s" to "%s"', 'buddypress' ), bp_core_get_userlink( $u ),  '<a href="' . bp_get_group_permalink( $group ) . '">' . $group->name . '</a>', $group->description, 'Bar' );
     133        $this->assertSame( $expected, $a['activities'][0]->action );
     134
     135        $this->set_current_user( $old_user );
     136    }
     137
     138    /**
     139     * @group activity_action
     140     * @group bp_groups_format_activity_action_group_details_updated
     141     */
     142    public function test_bp_groups_format_activity_action_group_details_updated_with_updated_name_and_description() {
     143        $old_user = get_current_user_id();
     144        $u = $this->factory->user->create();
     145        $this->set_current_user( $u );
     146
     147        $group = $this->factory->group->create_and_get();
     148        groups_edit_base_group_details( $group->id, 'Foo', 'Bar', true );
     149
     150        $a = bp_activity_get( array(
     151            'component' => buddypress()->groups->id,
     152            'action' => 'group_details_updated',
     153            'item_id' => $group->id,
     154        ) );
     155
     156        $this->assertNotEmpty( $a['activities'] );
     157
     158        $expected = sprintf( __( '%s changed the name and description of the group %s', 'buddypress' ), bp_core_get_userlink( $u ),  '<a href="' . bp_get_group_permalink( $group ) . '">Foo</a>' );
     159        $this->assertSame( $expected, $a['activities'][0]->action );
     160
     161        $this->set_current_user( $old_user );
     162    }
    51163}
Note: See TracChangeset for help on using the changeset viewer.