Skip to content

Commit

Permalink
Improve i18n of Groups activity updates action string
Browse files Browse the repository at this point in the history
Dynamically generated activity action strings were introduced in BuddyPress 2.0 to ensure these strings are always up to date and multilingual-friendly (see #3856).

It appeared although the {{{activity_update}}} type enjoys this feature when activities are shared by users on their profiles, it was not the case for activity updates posted within Groups.

Thanks to this commit, BuddyPress is now taking it in charge by:
- Registering a new group activity action to reference a specific formatting callback function for activities posted within Groups.
- Making sure this new activity action type does not interfere with the one of regular activity updates in dropdown filters or into the Activity Administration screens.

BTW happy 4th WPTranslationDay to everyone :)

Fixes #8089

git-svn-id: https://buddypress.svn.wordpress.org/trunk@12395 cdf35c40-ae34-48e0-9cc9-0c9da1808c22
  • Loading branch information
imath committed May 11, 2019
1 parent 056eca0 commit fa59957
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 111 deletions.
27 changes: 20 additions & 7 deletions src/bp-activity/bp-activity-admin.php
Expand Up @@ -840,13 +840,19 @@ function bp_activity_admin_get_activity_actions() {
$action = array_values( (array) $action );

for ( $i = 0, $i_count = count( $action ); $i < $i_count; $i++ ) {
/**
* Don't take in account:
* - a mis-named Friends activity type from before BP 1.6,
* - The Group's component 'activity_update' one as the Activity component is using it.
*/
if ( 'friends_register_activity_action' === $action[$i]['key'] || 'bp_groups_format_activity_action_group_activity_update' === $action[$i]['format_callback'] ) {
continue;
}

$actions[ $action[$i]['key'] ] = $action[$i]['value'];
}
}

// This was a mis-named activity type from before BP 1.6.
unset( $actions['friends_register_activity_action'] );

// Sort array by the human-readable value.
natsort( $actions );

Expand All @@ -870,13 +876,20 @@ function bp_activity_admin_edit_metabox_type( $item ) {
foreach ( bp_activity_get_actions() as $action ) {
$action = array_values( (array) $action );

for ( $i = 0, $i_count = count( $action ); $i < $i_count; $i++ )
for ( $i = 0, $i_count = count( $action ); $i < $i_count; $i++ ) {
/**
* Don't take in account:
* - a mis-named Friends activity type from before BP 1.6,
* - The Group's component 'activity_update' one as the Activity component is using it.
*/
if ( 'friends_register_activity_action' === $action[$i]['key'] || 'bp_groups_format_activity_action_group_activity_update' === $action[$i]['format_callback'] ) {
continue;
}

$actions[ $action[$i]['key'] ] = $action[$i]['value'];
}
}

// This was a mis-named activity type from before BP 1.6.
unset( $actions['friends_register_activity_action'] );

// Sort array by the human-readable value.
natsort( $actions );

Expand Down
5 changes: 5 additions & 0 deletions src/bp-activity/bp-activity-template.php
Expand Up @@ -3848,6 +3848,11 @@ function bp_get_activity_show_filters( $context = '' ) {
$action['key'] = 'friendship_accepted,friendship_created';
}

// The 'activity_update' filter is already used by the Activity component.
if ( 'bp_groups_format_activity_action_group_activity_update' === $action['format_callback'] ) {
continue;
}

$filters[ $action['key'] ] = $action['label'];
}

Expand Down
7 changes: 6 additions & 1 deletion src/bp-activity/classes/class-bp-activity-list-table.php
Expand Up @@ -435,6 +435,11 @@ function extra_tablenav( $which ) {
$component = 'xprofile';
}

// The 'activity_update' filter is already used by the Activity component.
if ( isset( $actions->activity_update ) && 'bp_groups_format_activity_action_group_activity_update' === $actions->activity_update['format_callback'] ) {
unset( $actions->activity_update );
}

if ( bp_is_active( $component ) ) {
if ( $component === 'xprofile' ) {
$component_name = buddypress()->profile->name;
Expand All @@ -455,7 +460,7 @@ function extra_tablenav( $which ) {
<?php

// Skip the incorrectly named pre-1.6 action.
if ( 'friends_register_activity_action' !== $action_key ) : ?>
if ( 'friends_register_activity_action' !== $action_key ) : ?>

<option value="<?php echo esc_attr( $action_key ); ?>" <?php selected( $action_key, $selected ); ?>><?php echo esc_html( $action_values[ 'value' ] ); ?></option>

Expand Down
154 changes: 145 additions & 9 deletions src/bp-groups/bp-groups-activity.php
Expand Up @@ -54,6 +54,15 @@ function groups_register_activity_actions() {
array( 'activity', 'group', 'member', 'member_groups' )
);

bp_activity_set_action(
$bp->groups->id,
'activity_update',
__( 'Posted a status update in a Group', 'buddypress' ),
'bp_groups_format_activity_action_group_activity_update',
__( 'Group Activity Updates', 'buddypress' ),
array( 'activity', 'group', 'member', 'member_groups' )
);

/**
* Fires at end of registration of the default activity actions for the Groups component.
*
Expand All @@ -63,6 +72,27 @@ function groups_register_activity_actions() {
}
add_action( 'bp_register_activity_actions', 'groups_register_activity_actions' );

/**
* Get the group object the activity belongs to.
*
* @since 5.0.0
*
* @param integer $group_id The group ID the activity is linked to.
* @return BP_Groups_Group The group object the activity belongs to.
*/
function bp_groups_get_activity_group( $group_id = 0 ) {
// If displaying a specific group, check the activity belongs to it.
if ( bp_is_group() && bp_get_current_group_id() === (int) $group_id ) {
$group = groups_get_current_group();

// Otherwise get the group the activity belongs to.
} else {
$group = groups_get_group( $group_id );
}

return $group;
}

/**
* Format 'created_group' activity actions.
*
Expand All @@ -75,7 +105,7 @@ function groups_register_activity_actions() {
function bp_groups_format_activity_action_created_group( $action, $activity ) {
$user_link = bp_core_get_userlink( $activity->user_id );

$group = groups_get_group( $activity->item_id );
$group = bp_groups_get_activity_group( $activity->item_id );
$group_link = '<a href="' . esc_url( bp_get_group_permalink( $group ) ) . '">' . esc_html( $group->name ) . '</a>';

$action = sprintf( esc_html__( '%1$s created the group %2$s', 'buddypress'), $user_link, $group_link );
Expand Down Expand Up @@ -103,7 +133,7 @@ function bp_groups_format_activity_action_created_group( $action, $activity ) {
function bp_groups_format_activity_action_joined_group( $action, $activity ) {
$user_link = bp_core_get_userlink( $activity->user_id );

$group = groups_get_group( $activity->item_id );
$group = bp_groups_get_activity_group( $activity->item_id );
$group_link = '<a href="' . esc_url( bp_get_group_permalink( $group ) ) . '">' . esc_html( $group->name ) . '</a>';

$action = sprintf( esc_html__( '%1$s joined the group %2$s', 'buddypress' ), $user_link, $group_link );
Expand Down Expand Up @@ -142,7 +172,7 @@ function bp_groups_format_activity_action_joined_group( $action, $activity ) {
function bp_groups_format_activity_action_group_details_updated( $action, $activity ) {
$user_link = bp_core_get_userlink( $activity->user_id );

$group = groups_get_group( $activity->item_id );
$group = bp_groups_get_activity_group( $activity->item_id );
$group_link = '<a href="' . esc_url( bp_get_group_permalink( $group ) ) . '">' . esc_html( $group->name ) . '</a>';

/*
Expand Down Expand Up @@ -183,6 +213,38 @@ function bp_groups_format_activity_action_group_details_updated( $action, $activ
return apply_filters( 'bp_groups_format_activity_action_joined_group', $action, $activity );
}

/**
* Format the action for activity updates posted in a Group.
*
* @since 5.0.0
*
* @param string $action Static activity action.
* @param object $activity Activity data object.
* @return string The formatted action for activity updates posted in a Group.
*/
function bp_groups_format_activity_action_group_activity_update( $action, $activity ) {
$user_link = bp_core_get_userlink( $activity->user_id );
$group = bp_groups_get_activity_group( $activity->item_id );

$group_link = '<a href="' . esc_url( bp_get_group_permalink( $group ) ) . '">' . esc_html( $group->name ) . '</a>';

// Set the Activity update posted in a Group action.
$action = sprintf( esc_html__( '%1$s posted an update in the group %2$s', 'buddypress' ), $user_link, $group_link );

/** This filter is documented in wp-includes/deprecated.php */
$action = apply_filters_deprecated( 'groups_activity_new_update_action', array( $action ), '5.0.0', 'bp_groups_format_activity_action_group_activity_update' );

/**
* Filters the Group's activity update action.
*
* @since 5.0.0
*
* @param string $action The Group's activity update action.
* @param object $activity Activity data object.
*/
return apply_filters( 'bp_groups_format_activity_action_group_activity_update', $action, $activity );
}

/**
* Fetch data related to groups at the beginning of an activity loop.
*
Expand Down Expand Up @@ -440,12 +502,8 @@ function groups_record_activity( $args = '' ) {

// Set the default for hide_sitewide by checking the status of the group.
$hide_sitewide = false;
if ( !empty( $args['item_id'] ) ) {
if ( bp_get_current_group_id() == $args['item_id'] ) {
$group = groups_get_current_group();
} else {
$group = groups_get_group( $args['item_id'] );
}
if ( ! empty( $args['item_id'] ) ) {
$group = bp_groups_get_activity_group( $args['item_id'] );

if ( isset( $group->status ) && 'public' != $group->status ) {
$hide_sitewide = true;
Expand All @@ -470,6 +528,84 @@ function groups_record_activity( $args = '' ) {
return bp_activity_add( $r );
}

/**
* Post an Activity status update affiliated with a group.
*
* @since 1.2.0
* @since 2.6.0 Added 'error_type' parameter to $args.
*
* @param array|string $args {
* Array of arguments.
* @type string $content The content of the update.
* @type int $user_id Optional. ID of the user posting the update. Default:
* ID of the logged-in user.
* @type int $group_id Optional. ID of the group to be affiliated with the
* update. Default: ID of the current group.
* }
* @return WP_Error|bool|int Returns the ID of the new activity item on success, or false on failure.
*/
function groups_post_update( $args = '' ) {
$bp = buddypress();

$r = bp_parse_args( $args, array(
'content' => false,
'user_id' => bp_loggedin_user_id(),
'group_id' => 0,
'error_type' => 'bool'
), 'groups_post_update' );

$group_id = (int) $r['group_id'];
if ( ! $group_id && ! empty( $bp->groups->current_group->id ) ) {
$group_id = (int) $bp->groups->current_group->id;
}

$content = $r['content'];
$user_id = (int) $r['user_id'];
if ( ! $content || ! strlen( trim( $content ) ) || ! $user_id || ! $group_id ) {
return false;
}

$bp->groups->current_group = groups_get_group( $group_id );

// Be sure the user is a member of the group before posting.
if ( ! bp_current_user_can( 'bp_moderate' ) && ! groups_is_user_member( $user_id, $group_id ) ) {
return false;
}

/**
* Filters the content for the new group activity update.
*
* @since 1.2.0
*
* @param string $content The content of the update.
*/
$content_filtered = apply_filters( 'groups_activity_new_update_content', $content );

$activity_id = groups_record_activity( array(
'user_id' => $user_id,
'content' => $content_filtered,
'type' => 'activity_update',
'item_id' => $group_id,
'error_type' => $r['error_type'],
) );

groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );

/**
* Fires after posting of an Activity status update affiliated with a group.
*
* @since 1.2.0
*
* @param string $content The content of the update.
* @param int $user_id ID of the user posting the update.
* @param int $group_id ID of the group being posted to.
* @param bool $activity_id Whether or not the activity recording succeeded.
*/
do_action( 'bp_groups_posted_update', $content, $user_id, $group_id, $activity_id );

return $activity_id;
}

/**
* Function used to determine if a user can comment on a group activity item.
*
Expand Down
93 changes: 0 additions & 93 deletions src/bp-groups/bp-groups-functions.php
Expand Up @@ -1266,99 +1266,6 @@ function groups_is_user_creator( $user_id, $group_id ) {
return BP_Groups_Member::check_is_creator( $user_id, $group_id );
}

/** Group Activity Posting ****************************************************/

/**
* Post an Activity status update affiliated with a group.
*
* @since 1.2.0
* @since 2.6.0 Added 'error_type' parameter to $args.
*
* @param array|string $args {
* Array of arguments.
* @type string $content The content of the update.
* @type int $user_id Optional. ID of the user posting the update. Default:
* ID of the logged-in user.
* @type int $group_id Optional. ID of the group to be affiliated with the
* update. Default: ID of the current group.
* }
* @return WP_Error|bool|int Returns the ID of the new activity item on success, or false on failure.
*/
function groups_post_update( $args = '' ) {
if ( ! bp_is_active( 'activity' ) ) {
return false;
}

$bp = buddypress();

$r = bp_parse_args( $args, array(
'content' => false,
'user_id' => bp_loggedin_user_id(),
'group_id' => 0,
'error_type' => 'bool'
), 'groups_post_update' );
extract( $r, EXTR_SKIP );

if ( empty( $group_id ) && !empty( $bp->groups->current_group->id ) )
$group_id = $bp->groups->current_group->id;

if ( empty( $content ) || !strlen( trim( $content ) ) || empty( $user_id ) || empty( $group_id ) )
return false;

$bp->groups->current_group = groups_get_group( $group_id );

// Be sure the user is a member of the group before posting.
if ( !bp_current_user_can( 'bp_moderate' ) && !groups_is_user_member( $user_id, $group_id ) )
return false;

// Record this in activity streams.
$activity_action = sprintf( esc_html__( '%1$s posted an update in the group %2$s', 'buddypress'), bp_core_get_userlink( $user_id ), '<a href="' . esc_url( bp_get_group_permalink( $bp->groups->current_group ) ) . '">' . esc_html( $bp->groups->current_group->name ) . '</a>' );
$activity_content = $content;

/**
* Filters the action for the new group activity update.
*
* @since 1.2.0
*
* @param string $activity_action The new group activity update.
*/
$action = apply_filters( 'groups_activity_new_update_action', $activity_action );

/**
* Filters the content for the new group activity update.
*
* @since 1.2.0
*
* @param string $activity_content The content of the update.
*/
$content_filtered = apply_filters( 'groups_activity_new_update_content', $activity_content );

$activity_id = groups_record_activity( array(
'user_id' => $user_id,
'action' => $action,
'content' => $content_filtered,
'type' => 'activity_update',
'item_id' => $group_id,
'error_type' => $error_type
) );

groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );

/**
* Fires after posting of an Activity status update affiliated with a group.
*
* @since 1.2.0
*
* @param string $content The content of the update.
* @param int $user_id ID of the user posting the update.
* @param int $group_id ID of the group being posted to.
* @param bool $activity_id Whether or not the activity recording succeeded.
*/
do_action( 'bp_groups_posted_update', $content, $user_id, $group_id, $activity_id );

return $activity_id;
}

/** Group Invitations *********************************************************/

/**
Expand Down
1 change: 0 additions & 1 deletion tests/phpunit/testcases/blogs/activity.php
Expand Up @@ -727,7 +727,6 @@ public function test_bp_blogs_sync_activity_edit_to_post_comment_spam_activity_c
/**
* @group bp_blogs_sync_activity_edit_to_post_comment
* @group post_type_comment_activities
* @group imath
*/
public function test_bp_blogs_sync_activity_edit_to_post_comment_trash_comment_ham_activity() {
$old_user = get_current_user_id();
Expand Down

0 comments on commit fa59957

Please sign in to comment.