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

    r7624 r8125  
    2424    }
    2525
    26     bp_activity_set_action( $bp->groups->id, 'created_group',   __( 'Created a group',       'buddypress' ) );
    27     bp_activity_set_action( $bp->groups->id, 'joined_group',    __( 'Joined a group',        'buddypress' ) );
     26    bp_activity_set_action(
     27        $bp->groups->id,
     28        'created_group',
     29        __( 'Created a group', 'buddypress' ),
     30        'bp_groups_format_activity_action_created_group'
     31    );
     32
     33    bp_activity_set_action(
     34        $bp->groups->id,
     35        'joined_group',
     36        __( 'Joined a group', 'buddypress' ),
     37        'bp_groups_format_activity_action_joined_group'
     38    );
    2839
    2940    // These actions are for the legacy forums
     
    3849}
    3950add_action( 'bp_register_activity_actions', 'groups_register_activity_actions' );
     51
     52/**
     53 * Format 'created_group' activity actions.
     54 *
     55 * @since BuddyPress (2.0.0)
     56 *
     57 * @param object $activity Activity data object.
     58 * @return string
     59 */
     60function bp_groups_format_activity_action_created_group( $activity ) {
     61    $user_link = bp_core_get_userlink( $activity->user_id );
     62
     63    $group = groups_get_group( array(
     64        'group_id'        => $activity->item_id,
     65        'populate_extras' => false,
     66    ) );
     67    $group_link = '<a href="' . esc_url( bp_get_group_permalink( $group ) ) . '">' . esc_html( $group->name ) . '</a>';
     68
     69    $action = sprintf( __( '%1$s created the group %2$s', 'buddypress'), $user_link, $group_link );
     70
     71    return apply_filters( 'groups_activity_created_group_action', $action, $activity );
     72}
     73
     74/**
     75 * Format 'joined_group' activity actions.
     76 *
     77 * @since BuddyPress (2.0.0)
     78 *
     79 * @param object $activity Activity data object.
     80 * @return string
     81 */
     82function bp_groups_format_activity_action_joined_group( $activity ) {
     83    $user_link = bp_core_get_userlink( $activity->user_id );
     84
     85    $group = groups_get_group( array(
     86        'group_id'        => $activity->item_id,
     87        'populate_extras' => false,
     88    ) );
     89    $group_link = '<a href="' . esc_url( bp_get_group_permalink( $group ) ) . '">' . esc_html( $group->name ) . '</a>';
     90
     91    $action = sprintf( __( '%1$s joined the group %2$s', 'buddypress' ), $user_link, $group_link );
     92
     93    // Legacy filters (do not follow parameter patterns of other activity
     94    // action filters, and requires apply_filters_ref_array())
     95    if ( has_filter( 'groups_activity_membership_accepted_action' ) ) {
     96        $action = apply_filters_ref_array( 'groups_activity_membership_accepted_action', array( $action, $user_link, &$group ) );
     97    }
     98
     99    // Another legacy filter
     100    if ( has_filter( 'groups_activity_accepted_invite_action' ) ) {
     101        $action = apply_filters_ref_array( 'groups_activity_accepted_invite_action', array( $action, $activity->user_id, &$group ) );
     102    }
     103
     104    return apply_filters( 'bp_groups_format_activity_action_joined_group', $action, $activity );
     105}
     106
     107/**
     108 * Fetch data related to groups at the beginning of an activity loop.
     109 *
     110 * This reduces database overhead during the activity loop.
     111 *
     112 * @since BuddyPress (2.0.0)
     113 *
     114 * @param array $activities Array of activity items.
     115 * @return array
     116 */
     117function bp_groups_prefetch_activity_object_data( $activities ) {
     118    $group_ids = array();
     119
     120    if ( empty( $activities ) ) {
     121        return $activities;
     122    }
     123
     124    foreach ( $activities as $activity ) {
     125        if ( buddypress()->groups->id !== $activity->component ) {
     126            continue;
     127        }
     128
     129        $group_ids[] = $activity->item_id;
     130    }
     131
     132    if ( ! empty( $group_ids ) ) {
     133
     134        // TEMPORARY - Once the 'populate_extras' issue is solved
     135        // in the groups component, we can do this with groups_get_groups()
     136        // rather than manually
     137        $uncached_ids = array();
     138        foreach ( $group_ids as $group_id ) {
     139            if ( false === wp_cache_get( $group_id, 'bp_groups' ) ) {
     140                $uncached_ids[] = $group_id;
     141            }
     142        }
     143
     144        global $wpdb, $bp;
     145        $uncached_ids_sql = implode( ',', wp_parse_id_list( $uncached_ids ) );
     146        $groups = $wpdb->get_results( "SELECT * FROM {$bp->groups->table_name} WHERE id IN ({$uncached_ids_sql})" );
     147        foreach ( $groups as $group ) {
     148            wp_cache_set( $group->id, $group, 'bp_groups' );
     149        }
     150    }
     151}
     152add_filter( 'bp_activity_prefetch_object_data', 'bp_groups_prefetch_activity_object_data' );
    40153
    41154/**
Note: See TracChangeset for help on using the changeset viewer.