Skip to:
Content

BuddyPress.org

Changeset 12395


Ignore:
Timestamp:
05/11/2019 01:33:14 PM (7 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

Location:
trunk
Files:
7 edited

Legend:

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

    r11907 r12395  
    841841
    842842                for ( $i = 0, $i_count = count( $action ); $i < $i_count; $i++ ) {
     843                        /**
     844                         * Don't take in account:
     845                         * - a mis-named Friends activity type from before BP 1.6,
     846                         * - The Group's component 'activity_update' one as the Activity component is using it.
     847                         */
     848                        if ( 'friends_register_activity_action' === $action[$i]['key'] || 'bp_groups_format_activity_action_group_activity_update' === $action[$i]['format_callback'] ) {
     849                                continue;
     850                        }
     851
    843852                        $actions[ $action[$i]['key'] ] = $action[$i]['value'];
    844853                }
    845854        }
    846855
    847         // This was a mis-named activity type from before BP 1.6.
    848         unset( $actions['friends_register_activity_action'] );
    849 
    850856        // Sort array by the human-readable value.
    851857        natsort( $actions );
     
    871877                $action = array_values( (array) $action );
    872878
    873                 for ( $i = 0, $i_count = count( $action ); $i < $i_count; $i++ )
     879                for ( $i = 0, $i_count = count( $action ); $i < $i_count; $i++ ) {
     880                        /**
     881                         * Don't take in account:
     882                         * - a mis-named Friends activity type from before BP 1.6,
     883                         * - The Group's component 'activity_update' one as the Activity component is using it.
     884                         */
     885                        if ( 'friends_register_activity_action' === $action[$i]['key'] || 'bp_groups_format_activity_action_group_activity_update' === $action[$i]['format_callback'] ) {
     886                                continue;
     887                        }
     888
    874889                        $actions[ $action[$i]['key'] ] = $action[$i]['value'];
     890                }
    875891        }
    876 
    877         // This was a mis-named activity type from before BP 1.6.
    878         unset( $actions['friends_register_activity_action'] );
    879892
    880893        // Sort array by the human-readable value.
  • trunk/src/bp-activity/bp-activity-template.php

    r11959 r12395  
    38493849                        }
    38503850
     3851                        // The 'activity_update' filter is already used by the Activity component.
     3852                        if ( 'bp_groups_format_activity_action_group_activity_update' === $action['format_callback'] ) {
     3853                                continue;
     3854                        }
     3855
    38513856                        $filters[ $action['key'] ] = $action['label'];
    38523857                }
  • trunk/src/bp-activity/classes/class-bp-activity-list-table.php

    r12331 r12395  
    436436                                        }
    437437
     438                                        // The 'activity_update' filter is already used by the Activity component.
     439                                        if ( isset( $actions->activity_update ) && 'bp_groups_format_activity_action_group_activity_update' === $actions->activity_update['format_callback'] ) {
     440                                                unset( $actions->activity_update );
     441                                        }
     442
    438443                                        if ( bp_is_active( $component ) ) {
    439444                                                if ( $component === 'xprofile' ) {
     
    456461
    457462                                                        // Skip the incorrectly named pre-1.6 action.
    458                                                         if ( 'friends_register_activity_action' !== $action_key ) : ?>
     463                                                        if ( 'friends_register_activity_action' !== $action_key  ) : ?>
    459464
    460465                                                                <option value="<?php echo esc_attr( $action_key ); ?>" <?php selected( $action_key,  $selected ); ?>><?php echo esc_html( $action_values[ 'value' ] ); ?></option>
  • 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 *
  • trunk/src/bp-groups/bp-groups-functions.php

    r12393 r12395  
    12671267}
    12681268
    1269 /** Group Activity Posting ****************************************************/
    1270 
    1271 /**
    1272  * Post an Activity status update affiliated with a group.
    1273  *
    1274  * @since 1.2.0
    1275  * @since 2.6.0 Added 'error_type' parameter to $args.
    1276  *
    1277  * @param array|string $args {
    1278  *     Array of arguments.
    1279  *     @type string $content  The content of the update.
    1280  *     @type int    $user_id  Optional. ID of the user posting the update. Default:
    1281  *                            ID of the logged-in user.
    1282  *     @type int    $group_id Optional. ID of the group to be affiliated with the
    1283  *                            update. Default: ID of the current group.
    1284  * }
    1285  * @return WP_Error|bool|int Returns the ID of the new activity item on success, or false on failure.
    1286  */
    1287 function groups_post_update( $args = '' ) {
    1288         if ( ! bp_is_active( 'activity' ) ) {
    1289                 return false;
    1290         }
    1291 
    1292         $bp = buddypress();
    1293 
    1294         $r = bp_parse_args( $args, array(
    1295                 'content'    => false,
    1296                 'user_id'    => bp_loggedin_user_id(),
    1297                 'group_id'   => 0,
    1298                 'error_type' => 'bool'
    1299         ), 'groups_post_update' );
    1300         extract( $r, EXTR_SKIP );
    1301 
    1302         if ( empty( $group_id ) && !empty( $bp->groups->current_group->id ) )
    1303                 $group_id = $bp->groups->current_group->id;
    1304 
    1305         if ( empty( $content ) || !strlen( trim( $content ) ) || empty( $user_id ) || empty( $group_id ) )
    1306                 return false;
    1307 
    1308         $bp->groups->current_group = groups_get_group( $group_id );
    1309 
    1310         // Be sure the user is a member of the group before posting.
    1311         if ( !bp_current_user_can( 'bp_moderate' ) && !groups_is_user_member( $user_id, $group_id ) )
    1312                 return false;
    1313 
    1314         // Record this in activity streams.
    1315         $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>' );
    1316         $activity_content = $content;
    1317 
    1318         /**
    1319          * Filters the action for the new group activity update.
    1320          *
    1321          * @since 1.2.0
    1322          *
    1323          * @param string $activity_action The new group activity update.
    1324          */
    1325         $action = apply_filters( 'groups_activity_new_update_action',  $activity_action  );
    1326 
    1327         /**
    1328          * Filters the content for the new group activity update.
    1329          *
    1330          * @since 1.2.0
    1331          *
    1332          * @param string $activity_content The content of the update.
    1333          */
    1334         $content_filtered = apply_filters( 'groups_activity_new_update_content', $activity_content );
    1335 
    1336         $activity_id = groups_record_activity( array(
    1337                 'user_id'    => $user_id,
    1338                 'action'     => $action,
    1339                 'content'    => $content_filtered,
    1340                 'type'       => 'activity_update',
    1341                 'item_id'    => $group_id,
    1342                 'error_type' => $error_type
    1343         ) );
    1344 
    1345         groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
    1346 
    1347         /**
    1348          * Fires after posting of an Activity status update affiliated with a group.
    1349          *
    1350          * @since 1.2.0
    1351          *
    1352          * @param string $content     The content of the update.
    1353          * @param int    $user_id     ID of the user posting the update.
    1354          * @param int    $group_id    ID of the group being posted to.
    1355          * @param bool   $activity_id Whether or not the activity recording succeeded.
    1356          */
    1357         do_action( 'bp_groups_posted_update', $content, $user_id, $group_id, $activity_id );
    1358 
    1359         return $activity_id;
    1360 }
    1361 
    13621269/** Group Invitations *********************************************************/
    13631270
  • trunk/tests/phpunit/testcases/blogs/activity.php

    r12311 r12395  
    728728         * @group bp_blogs_sync_activity_edit_to_post_comment
    729729         * @group post_type_comment_activities
    730          * @group imath
    731730         */
    732731        public function test_bp_blogs_sync_activity_edit_to_post_comment_trash_comment_ham_activity() {
  • trunk/tests/phpunit/testcases/groups/activity.php

    r12393 r12395  
    66 */
    77class BP_Tests_Groups_Activity extends BP_UnitTestCase {
     8        protected $groups_post_update_args;
     9
    810        /**
    911         * @group activity_action
     
    223225
    224226                $this->set_current_user( $old_user );
     227        }
     228
     229        /**
     230         * @group activity_action
     231         * @group bp_groups_format_activity_action_group_activity_update
     232         */
     233        public function test_bp_groups_format_activity_action_group_activity_update() {
     234                $u = self::factory()->user->create();
     235                $g = self::factory()->group->create();
     236                $a = self::factory()->activity->create( array(
     237                        'component' => buddypress()->groups->id,
     238                        'type' => 'activity_update',
     239                        'user_id' => $u,
     240                        'item_id' => $g,
     241                ) );
     242
     243                $a_obj = new BP_Activity_Activity( $a );
     244                $g_obj = groups_get_group( $g );
     245
     246                $expected = sprintf( esc_html__( '%1$s posted an update in the group %2$s', 'buddypress' ), bp_core_get_userlink( $u ),  '<a href="' . esc_url( bp_get_group_permalink( $g_obj ) ) . '">' . esc_html( $g_obj->name ) . '</a>' );
     247
     248                $this->assertSame( $expected, $a_obj->action );
     249        }
     250
     251        /**
     252         * @group groups_post_update
     253         */
     254        public function test_groups_post_update() {
     255                $u = self::factory()->user->create();
     256                $g = self::factory()->group->create();
     257
     258                // The user is a group member.
     259                groups_join_group( $g, $u );
     260
     261                $activity_args = array(
     262                        'content'    => 'Test group_post_update',
     263                        'user_id'    => $u,
     264                        'group_id'   => $g,
     265                        'error_type' => 'wp_error',
     266                );
     267
     268                add_filter( 'bp_before_groups_record_activity_parse_args', array( $this, 'groups_post_update_args' ), 10, 1 );
     269
     270                groups_post_update( $activity_args );
     271
     272                remove_filter( 'bp_before_groups_record_activity_parse_args', array( $this, 'groups_post_update_args' ), 10, 1 );
     273
     274                $expected = array_merge( $activity_args, array( 'item_id' => $g ) );
     275                unset( $expected['group_id'] );
     276
     277                $this->assertEquals( $expected, $this->groups_post_update_args );
     278        }
     279
     280        /**
     281         * @group groups_post_update
     282         */
     283        public function test_groups_post_update_in_group() {
     284                $bp = buddypress();
     285                $u  = self::factory()->user->create();
     286                $g  = self::factory()->group->create();
     287
     288                // The user is a group member.
     289                groups_join_group( $g, $u );
     290
     291                $bp->groups->current_group = groups_get_group( $g );
     292
     293                $activity_args = array(
     294                        'content' => 'Test group_post_update in a group',
     295                        'user_id' => $u,
     296                );
     297
     298                $a = groups_post_update( $activity_args );
     299                $a_obj = new BP_Activity_Activity( $a );
     300
     301                $this->assertSame( $a_obj->item_id, $g );
     302                $this->assertSame( $a_obj->component, 'groups' );
     303
     304                unset( $bp->groups->current_group );
    225305        }
    226306
     
    263343                $this->set_current_user( $old_user );
    264344        }
     345
     346        public function groups_post_update_args( $args = array() ) {
     347                $this->groups_post_update_args = array_intersect_key( $args, array(
     348                        'content'    => true,
     349                        'user_id'    => true,
     350                        'item_id'    => true,
     351                        'error_type' => true,
     352                ) );
     353
     354                return $args;
     355        }
    265356}
Note: See TracChangeset for help on using the changeset viewer.