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/activity/functions.php

    r8076 r8125  
    539539
    540540        remove_filter( 'bp_is_username_compatibility_mode', '__return_true' );
     541    }
     542
     543    /**
     544     * @group activity_action
     545     * @group bp_activity_format_activity_action_activity_update
     546     */
     547    public function test_bp_activity_format_activity_action_activity_update() {
     548        $u = $this->create_user();
     549        $a = $this->factory->activity->create( array(
     550            'component' => buddypress()->activity->id,
     551            'type' => 'activity_update',
     552            'user_id' => $u,
     553        ) );
     554
     555        $a_obj = new BP_Activity_Activity( $a );
     556
     557        $expected = sprintf( '%s posted an update', bp_core_get_userlink( $u ) );
     558
     559        $this->assertSame( $expected, $a_obj->action );
     560    }
     561
     562    /**
     563     * @group activity_action
     564     * @group bp_activity_format_activity_action_activity_comment
     565     */
     566    public function test_bp_activity_format_activity_action_activity_comment() {
     567        $u = $this->create_user();
     568        $a = $this->factory->activity->create( array(
     569            'component' => buddypress()->activity->id,
     570            'type' => 'activity_comment',
     571            'user_id' => $u,
     572        ) );
     573
     574        $a_obj = new BP_Activity_Activity( $a );
     575
     576        $expected = sprintf( '%s posted a new activity comment', bp_core_get_userlink( $u ) );
     577
     578        $this->assertSame( $expected, $a_obj->action );
    541579    }
    542580
Note: See TracChangeset for help on using the changeset viewer.