Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/29/2014 04:04:42 PM (12 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.

File:
1 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 *
Note: See TracChangeset for help on using the changeset viewer.