Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/13/2014 06:34:06 PM (11 years ago)
Author:
boonebgorges
Message:

Generate activity actions dynamically, instead of using static values stored in the database

Activity actions - like 'Joe became a registered member' - have always been
stored as static strings in the activity table. This has been a perennial thorn
in the side of BP site administrators. For one thing, static strings cannot be
translated for multilingual audiences (without unreasonable workarounds).
For another, information in these static strings becomes out of date as users
change their display names, groups change their names, and so on.

This changeset makes it possible for activity types to be registered with a
new $format_callback function, passed to bp_activity_set_action(), which BP
will use to generate the action dynamically when building the activity loop.
This makes the activity stream fully localizable, continually up-to-date, and
generally the bomb dot com.

Existing plugins that do not register format_callback functions will continue
to have their actions pulled directly from the database. Likewise, since a copy
of the static string is saved in the activity table, static strings can be
served if the given component is disabled at any point (and thus the callback
is no longer available).

The original argument for caching the action strings was that generating them
inline was too resource intensive; it often requires pulling up display names,
user permalinks, group links, blog post names, and so forth. To address this
problem, each component can now prefetch required data at the beginning of an
activity loop by hooking to bp_activity_prefetch_object_data, and loading any
relevant data into the cache. The Activity, Friends, Groups, and Blogs
component now do this natively. It's likely that this prefetching will have
other performance benefits, as plugin authors will now be able to access user
data etc inline without performance penalties.

The case of the Blogs component is a special one; it's not practical to
prefetch data from multiple blogs at the beginning of a loop, due to the
resources required by switch_to_blog(). For this reason, the format_callback
functions in the blog component cache some relevant data (like the post name)
in blogmeta, where it's readily accessible within the loop.

Fixes #3856

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/testcases/xprofile/activity.php

    r8016 r8125  
    271271    }
    272272
     273    /**
     274     * @group activity_action
     275     * @group bp_xprofile_format_activity_action_new_avatar
     276     */
     277    public function test_bp_xprofile_format_activity_action_new_avatar() {
     278        $u = $this->create_user();
     279        $a = $this->factory->activity->create( array(
     280            'component' => buddypress()->profile->id,
     281            'type' => 'new_avatar',
     282            'user_id' => $u,
     283        ) );
     284
     285        $expected = sprintf( __( '%s changed their profile picture', 'buddypress' ), bp_core_get_userlink( $u ) );
     286
     287        $a_obj = new BP_Activity_Activity( $a );
     288
     289        $this->assertSame( $expected, $a_obj->action );
     290    }
     291
     292    /**
     293     * @group activity_action
     294     * @group bp_xprofile_format_activity_action_new_member
     295     */
     296    public function test_bp_xprofile_format_activity_action_new_member_xprofile_on() {
     297        $active = bp_is_active( 'xprofile' );
     298        buddypress()->active_components['xprofile'] = '1';
     299
     300        $u = $this->create_user();
     301        $a = $this->factory->activity->create( array(
     302            'component' => buddypress()->profile->id,
     303            'type' => 'new_member',
     304            'user_id' => $u,
     305        ) );
     306
     307        $expected = sprintf( __( '%s became a registered member', 'buddypress' ), bp_core_get_userlink( $u ) );
     308
     309        $a_obj = new BP_Activity_Activity( $a );
     310
     311        $this->assertSame( $expected, $a_obj->action );
     312
     313        if ( ! $active ) {
     314            unset( buddypress()->active_components['xprofile'] );
     315        }
     316    }
     317
     318    /**
     319     * @group activity_action
     320     * @group bp_xprofile_format_activity_action_new_member
     321     */
     322    public function test_bp_xprofile_format_activity_action_new_member_xprofile_off() {
     323        $active = bp_is_active( 'xprofile' );
     324        unset( buddypress()->active_components['xprofile'] );
     325
     326        $u = $this->create_user();
     327        $a = $this->factory->activity->create( array(
     328            'component' => buddypress()->profile->id,
     329            'type' => 'new_member',
     330            'user_id' => $u,
     331        ) );
     332
     333        $expected = sprintf( __( '%s became a registered member', 'buddypress' ), bp_core_get_userlink( $u ) );
     334
     335        $a_obj = new BP_Activity_Activity( $a );
     336
     337        $this->assertSame( $expected, $a_obj->action );
     338
     339        if ( $active ) {
     340            buddypress()->active_components['xprofile'] = '1';
     341        }
     342    }
     343
     344    /**
     345     * @group activity_action
     346     * @group bp_xprofile_format_activity_action_updated_profile
     347     */
     348    public function test_bp_xprofile_format_activity_action_updated_profile() {
     349        $u = $this->create_user();
     350        $a = $this->factory->activity->create( array(
     351            'component' => buddypress()->profile->id,
     352            'type' => 'updated_profile',
     353            'user_id' => $u,
     354        ) );
     355
     356        $expected = sprintf( __( '%s&#8217;s profile was updated', 'buddypress' ), '<a href="' . bp_core_get_user_domain( $u ) . buddypress()->profile->slug . '/">' . bp_core_get_user_displayname( $u ) . '</a>' );
     357
     358        $a_obj = new BP_Activity_Activity( $a );
     359
     360        $this->assertSame( $expected, $a_obj->action );
     361    }
    273362
    274363    protected function setup_updated_profile_data() {
Note: See TracChangeset for help on using the changeset viewer.