Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
05/11/2019 01:33:14 PM (5 years ago)
Author:
imath
Message:

Improve i18n of Groups activity updates action string

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

File:
1 edited

Legend:

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

    r12393 r12395  
    5555    );
    5656
     57    bp_activity_set_action(
     58        $bp->groups->id,
     59        'activity_update',
     60        __( 'Posted a status update in a Group', 'buddypress' ),
     61        'bp_groups_format_activity_action_group_activity_update',
     62        __( 'Group Activity Updates', 'buddypress' ),
     63        array( 'activity', 'group', 'member', 'member_groups' )
     64    );
     65
    5766    /**
    5867     * Fires at end of registration of the default activity actions for the Groups component.
     
    6372}
    6473add_action( 'bp_register_activity_actions', 'groups_register_activity_actions' );
     74
     75/**
     76 * Get the group object the activity belongs to.
     77 *
     78 * @since 5.0.0
     79 *
     80 * @param integer $group_id The group ID the activity is linked to.
     81 * @return BP_Groups_Group  The group object the activity belongs to.
     82 */
     83function bp_groups_get_activity_group( $group_id = 0 ) {
     84    // If displaying a specific group, check the activity belongs to it.
     85    if ( bp_is_group() && bp_get_current_group_id() === (int) $group_id ) {
     86        $group = groups_get_current_group();
     87
     88        // Otherwise get the group the activity belongs to.
     89    } else {
     90        $group = groups_get_group( $group_id );
     91    }
     92
     93    return $group;
     94}
    6595
    6696/**
     
    76106    $user_link = bp_core_get_userlink( $activity->user_id );
    77107
    78     $group      = groups_get_group( $activity->item_id );
     108    $group      = bp_groups_get_activity_group( $activity->item_id );
    79109    $group_link = '<a href="' . esc_url( bp_get_group_permalink( $group ) ) . '">' . esc_html( $group->name ) . '</a>';
    80110
     
    104134    $user_link = bp_core_get_userlink( $activity->user_id );
    105135
    106     $group      = groups_get_group( $activity->item_id );
     136    $group      = bp_groups_get_activity_group( $activity->item_id );
    107137    $group_link = '<a href="' . esc_url( bp_get_group_permalink( $group ) ) . '">' . esc_html( $group->name ) . '</a>';
    108138
     
    143173    $user_link = bp_core_get_userlink( $activity->user_id );
    144174
    145     $group      = groups_get_group( $activity->item_id );
     175    $group      = bp_groups_get_activity_group( $activity->item_id );
    146176    $group_link = '<a href="' . esc_url( bp_get_group_permalink( $group ) ) . '">' . esc_html( $group->name ) . '</a>';
    147177
     
    182212     */
    183213    return apply_filters( 'bp_groups_format_activity_action_joined_group', $action, $activity );
     214}
     215
     216/**
     217 * Format the action for activity updates posted in a Group.
     218 *
     219 * @since 5.0.0
     220 *
     221 * @param string $action   Static activity action.
     222 * @param object $activity Activity data object.
     223 * @return string          The formatted action for activity updates posted in a Group.
     224 */
     225function bp_groups_format_activity_action_group_activity_update( $action, $activity ) {
     226    $user_link = bp_core_get_userlink( $activity->user_id );
     227    $group     = bp_groups_get_activity_group( $activity->item_id );
     228
     229    $group_link = '<a href="' . esc_url( bp_get_group_permalink( $group ) ) . '">' . esc_html( $group->name ) . '</a>';
     230
     231    // Set the Activity update posted in a Group action.
     232    $action = sprintf( esc_html__( '%1$s posted an update in the group %2$s', 'buddypress' ), $user_link, $group_link );
     233
     234    /** This filter is documented in wp-includes/deprecated.php */
     235    $action = apply_filters_deprecated( 'groups_activity_new_update_action', array( $action ), '5.0.0', 'bp_groups_format_activity_action_group_activity_update' );
     236
     237    /**
     238     * Filters the Group's activity update action.
     239     *
     240     * @since 5.0.0
     241     *
     242     * @param string $action   The Group's activity update action.
     243     * @param object $activity Activity data object.
     244     */
     245    return apply_filters( 'bp_groups_format_activity_action_group_activity_update', $action, $activity );
    184246}
    185247
     
    441503    // Set the default for hide_sitewide by checking the status of the group.
    442504    $hide_sitewide = false;
    443     if ( !empty( $args['item_id'] ) ) {
    444         if ( bp_get_current_group_id() == $args['item_id'] ) {
    445             $group = groups_get_current_group();
    446         } else {
    447             $group = groups_get_group( $args['item_id'] );
    448         }
     505    if ( ! empty( $args['item_id'] ) ) {
     506        $group = bp_groups_get_activity_group( $args['item_id'] );
    449507
    450508        if ( isset( $group->status ) && 'public' != $group->status ) {
     
    472530
    473531/**
     532 * Post an Activity status update affiliated with a group.
     533 *
     534 * @since 1.2.0
     535 * @since 2.6.0 Added 'error_type' parameter to $args.
     536 *
     537 * @param array|string $args {
     538 *     Array of arguments.
     539 *     @type string $content  The content of the update.
     540 *     @type int    $user_id  Optional. ID of the user posting the update. Default:
     541 *                            ID of the logged-in user.
     542 *     @type int    $group_id Optional. ID of the group to be affiliated with the
     543 *                            update. Default: ID of the current group.
     544 * }
     545 * @return WP_Error|bool|int Returns the ID of the new activity item on success, or false on failure.
     546 */
     547function groups_post_update( $args = '' ) {
     548    $bp = buddypress();
     549
     550    $r = bp_parse_args( $args, array(
     551        'content'    => false,
     552        'user_id'    => bp_loggedin_user_id(),
     553        'group_id'   => 0,
     554        'error_type' => 'bool'
     555    ), 'groups_post_update' );
     556
     557    $group_id = (int) $r['group_id'];
     558    if ( ! $group_id && ! empty( $bp->groups->current_group->id ) ) {
     559        $group_id = (int) $bp->groups->current_group->id;
     560    }
     561
     562    $content = $r['content'];
     563    $user_id = (int) $r['user_id'];
     564    if ( ! $content || ! strlen( trim( $content ) ) || ! $user_id || ! $group_id ) {
     565        return false;
     566    }
     567
     568    $bp->groups->current_group = groups_get_group( $group_id );
     569
     570    // Be sure the user is a member of the group before posting.
     571    if ( ! bp_current_user_can( 'bp_moderate' ) && ! groups_is_user_member( $user_id, $group_id ) ) {
     572        return false;
     573    }
     574
     575    /**
     576     * Filters the content for the new group activity update.
     577     *
     578     * @since 1.2.0
     579     *
     580     * @param string $content The content of the update.
     581     */
     582    $content_filtered = apply_filters( 'groups_activity_new_update_content', $content );
     583
     584    $activity_id = groups_record_activity( array(
     585        'user_id'    => $user_id,
     586        'content'    => $content_filtered,
     587        'type'       => 'activity_update',
     588        'item_id'    => $group_id,
     589        'error_type' => $r['error_type'],
     590    ) );
     591
     592    groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
     593
     594    /**
     595     * Fires after posting of an Activity status update affiliated with a group.
     596     *
     597     * @since 1.2.0
     598     *
     599     * @param string $content     The content of the update.
     600     * @param int    $user_id     ID of the user posting the update.
     601     * @param int    $group_id    ID of the group being posted to.
     602     * @param bool   $activity_id Whether or not the activity recording succeeded.
     603     */
     604    do_action( 'bp_groups_posted_update', $content, $user_id, $group_id, $activity_id );
     605
     606    return $activity_id;
     607}
     608
     609/**
    474610 * Function used to determine if a user can comment on a group activity item.
    475611 *
Note: See TracChangeset for help on using the changeset viewer.