| | 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 | */ |
| | 148 | function 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 | // Changed group details are stored in groupmeta, keyed by the activity |
| | 158 | // timestamp. See {@link bp_groups_group_details_updated_add_activity()}. |
| | 159 | $changed = groups_get_groupmeta( $activity->item_id, 'updated_details_' . $activity->date_recorded ); |
| | 160 | |
| | 161 | // No changed details were found, so use a generic message |
| | 162 | if ( empty( $changed ) ) { |
| | 163 | $action = sprintf( __( '%1$s updated details for the group %2$s', 'buddypress' ), $user_link, $group_link ); |
| | 164 | |
| | 165 | // Name and description changed - to keep things short, don't describe |
| | 166 | // changes in detail |
| | 167 | } else if ( isset( $changed['name'] ) && isset( $changed['description'] ) ) { |
| | 168 | $action = sprintf( __( '%1$s changed the name and description of the group %2$s', 'buddypress' ), $user_link, $group_link ); |
| | 169 | |
| | 170 | // Name only |
| | 171 | } else if ( ! empty( $changed['name']['old'] ) && ! empty( $changed['name']['new'] ) ) { |
| | 172 | $action = sprintf( __( '%1$s changed the name of the group %2$s from "%3$s" to "%4$s"', 'buddypress' ), $user_link, $group_link, $changed['name']['old'], $changed['name']['new'] ); |
| | 173 | |
| | 174 | // Description only |
| | 175 | } else if ( ! empty( $changed['description']['old'] ) && ! empty( $changed['description']['new'] ) ) { |
| | 176 | $action = sprintf( __( '%1$s changed the description of the group %2$s from "%3$s" to "%4$s"', 'buddypress' ), $user_link, $group_link, $changed['description']['old'], $changed['description']['new'] ); |
| | 177 | |
| | 178 | } |
| | 179 | |
| | 180 | return apply_filters( 'bp_groups_format_activity_action_joined_group', $action, $activity ); |
| | 181 | } |
| | 182 | |
| | 183 | /** |
| | 346 | * Add an activity item when a group's details are updated. |
| | 347 | * |
| | 348 | * @since BuddyPress (2.2.0) |
| | 349 | * |
| | 350 | * @param int $group_id ID of the group. |
| | 351 | * @param BP_Groups_Group Group object before the details had been changed. |
| | 352 | * @param bool $notify_members True if the admin has opted to notify group |
| | 353 | * members, otherwise false. |
| | 354 | * @return int|bool The ID of the activity on success. False on error. |
| | 355 | */ |
| | 356 | function 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 |
| | 368 | // item either |
| | 369 | if ( empty( $notify_members ) ) { |
| | 370 | return; |
| | 371 | } |
| | 372 | |
| | 373 | $group = groups_get_group( array( |
| | 374 | 'group_id' => $group_id, |
| | 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 | $changed = array(); |
| | 382 | |
| | 383 | if ( $group->name !== $old_group->name ) { |
| | 384 | $changed['name'] = array( |
| | 385 | 'old' => $old_group->name, |
| | 386 | 'new' => $group->name, |
| | 387 | ); |
| | 388 | } |
| | 389 | |
| | 390 | if ( $group->description !== $old_group->description ) { |
| | 391 | $changed['description'] = array( |
| | 392 | 'old' => $old_group->description, |
| | 393 | 'new' => $group->description, |
| | 394 | ); |
| | 395 | } |
| | 396 | |
| | 397 | // If there are no changes, don't post an activity item |
| | 398 | if ( empty( $changed ) ) { |
| | 399 | return; |
| | 400 | } |
| | 401 | |
| | 402 | $time = bp_core_current_time(); |
| | 403 | groups_update_groupmeta( $group_id, 'updated_details_' . $time, $changed ); |
| | 404 | |
| | 405 | // Record in activity streams |
| | 406 | return groups_record_activity( array( |
| | 407 | 'type' => 'group_details_updated', |
| | 408 | 'item_id' => $group_id, |
| | 409 | 'user_id' => bp_loggedin_user_id(), |
| | 410 | 'recorded_time' => $time, |
| | 411 | |
| | 412 | ) ); |
| | 413 | |
| | 414 | } |
| | 415 | add_action( 'groups_details_updated', 'bp_groups_group_details_updated_add_activity', 10, 3 ); |
| | 416 | |
| | 417 | /** |