Herding Cats
with the
BuddyPress
Activity
Component

Boone B Gorges · @boone

WordCamp Europe 2013

Boone

  • is a Lead Developer for BuddyPress
  • lives in New York City
  • is a freelance consultant and developer
  • never uses fewer than four bullet points
  •  
    Activity Streams
  •  
    Blog Tracking
  •  
    Friendships
  •  
    Groups
  •  
    Private Messaging
  •  
    User Profiles

Out of the box

Activity interface

Half Twitter

Activity interface

Half aggregator

Why aggregate?

  • Posts
  • Comments
  • New memberships
  • Events
  • Groups
  • Friendships
  • Form submissions
  • Blog posts from other sites
  • Tweets
  • Your mom

Why aggregate?

  • Posts
    wp_posts
  • Comments
    wp_comments
  • New memberships
    wp_users
  • Events
    wp_posts (CPT)
  • Groups
    wp_bp_groups
  • Friendships
    wp_bp_friends
  • Form submissions
    wp_rg_lead
  • Blog posts from other sites
    http://teleogistic.net/feed
  • Tweets
    GET https://api.twitter.com/1.1/statuses/
    user_timeline.json?screen_name=boone&count=20
  • Your mom
    GET https://api.yourmom.com/1.0/
    wceu-attendees-moms?fields=phone

Now go fetch

Show me all of Boone's activity


$sql = "SELECT {$all_the_things} 
        FROM {$all_the_places} 
	WHERE {$the_user_index} = {$boones_user_id}
	ORDER BY {$omg_wtf} DESC;"
					

Halp

Lots of cats

bp-activity provides:

  1. a flexible schema for storing arbitrary activity data
  2. API functions for creating, retrieving, updating, and deleting activity items
  3. Template loops and template files for displaying activity content

Schema


mysql > DESCRIBE wp_bp_activity;

        id                | bigint(20)
        user_id           | bigint(20)
        component         | varchar(75)
        type              | varchar(75)
        action            | text      
        content           | longtext 
        primary_link      | varchar(255)
        item_id           | bigint(20) 
        secondary_item_id | bigint(20)
        date_recorded     | datetime 
        hide_sitewide     | tinyint(1)
        mptt_left         | int(11)  
        mptt_right        | int(11) 
        is_spam           | tinyint(1)
					

user_id

is the user id


content

is the content


action

is an HTML string of the form "Boone posted a new comment on the post 'Foo'"

component

and

type


are non-localized strings that allow categorization and sorting


component (eg 'groups') is more general than type (eg 'joined_group'

item_id

and

secondary_item_id


are integer values representing relationships with arbitrary items

Example: Group creation

Smurfy

  • user_id
    2 (Boone's user id)
  • action
    '<a href="http://example.com/members/boone/" title="Boone Gorges">Boone Gorges</a> created the group <a href="http://example.com/groups/smurfs/">Smurfs</a>'
  • content
    ''
  • component
    'groups'
  • type
    'created_group'
  • item_id
    31 (the id of the Smurfs group)
  • secondary_item_id
    0

Example: Blog post

Blog post

  • user_id
    2 (Boone's user id)
  • action
    <a href="http://example.com/members/admin/" title="Boone Gorges">Boone Gorges</a> wrote a new post, [...]'
  • content
    'This weekend, I am attending WordCamp Europe. It is the bomb. [...]'
  • component
    'blogs'
  • type
    'new_blog_post'
  • item_id
    6 (the id of the Foo 6 site)
  • secondary_item_id
    13 (the id of the post)

The API

Create new activity


$activity_id = bp_activity_add( array(
	'action' => 'Boone wrote a new Twilight
	             fanfic, Vampire Luv',	
	'component' => 'fanfic',
	'type' => 'new_fanfic',
	'primary_link' => 'http://example.com/fanfic/vampire-luv',
	'user_id' => $user_id,
	'item_id' => $item_id, // The numeric id of the fanfic object
) );
					

Pass an 'id' to update an existing item.

Delete existing activity


// Know the id?
$deleted = bp_activity_delete( array( 'id' => $activity_id, ) );

// Otherwise provide some specifics, and let BP find it
$deleted = bp_activity_delete( array(
	'component' => 'fanfic',
	'type' => 'new_fanfic',
	'user_id' => $user_id,
	'item_id' => $item_id, // The numeric id of the fanfic object
) );
					

Fetch activity


// By user id
$activities = bp_activity_get( array(
	'page' => 1,
	'per_page' => 5,
	'filter' => array(
            'user_id' => $user_id,
	),
) );

// By type and item_id
$activities = bp_activity_get( array(
	// ...
	'filter' => array(
	    'action' => 'new_fanfic',
	    'primary_id' => $fanfic_item_id,
	),
) );
					

Drilling down with bp_activity_get()

  • meta_query
  • exclude
  • in
  • filter: user_id
  • filter: object
  • filter: action
  • filter: primary_id
  • filter: secondary_id

Getting Creative

wordpress.org profiles

Mert Yazıcıoğlu, GSoC 2013

Blog post

wordpress.org: Data, data everywhere

  • Core commits, core props
        core.trac.wordpress.org (Trac)
  • Plugin/theme commits
        [plugins|themes].trac.wordpress.org (Trac)
  • make.wordpress.org posts/comments
        make.wordpress.org (WordPress)
  • Support forum topics/responses
        wordpress.org/support (bbPress 1.x)
  • New plugin/theme registration
        wordpress.org/[plugins|themes] (bbPress 1.x)

wordpress.org: Herd those cats

  1. Plugins for Trac, bbPress, and WordPress that catch new activity and send a POST request to a WP API
  2. WordPress plugin that takes API requests and uses bp_activity_add()
    
    $user = get_user_by('login', $_POST['user']);
    
    if ( $_POST['newTopic'] ) {
    	$action = 'Created a new topic in Support Forums';
    	$content = $_POST['title'];
    	$type = 'new_topic';
    } else {
    	$action = 'Posted a reply to ' . $_POST['title'] . ' in Support Forums';
    	$content = $_POST['message'];
    	$type = 'new_post';
    }
    
    $args = array(
    	'user_id'           => $user->ID,
    	'action'            => $action,
    	'content'           => $content,
    	'primary_link'      => $_POST['url'],
    	'component'         => 'dotorg_activities',
    	'type'              => $type,
    	'item_id'           => $user->ID,
    	'secondary_item_id' => $_POST['topic'],
    	'hide_sitewide'     => false
    );
    
    bp_activity_add( $args );
    							

http://gsoc.svn.wordpress.org/2013/merty/

Resources

http://codex.buddypress.org/developer/function-examples/bp_activity_add/

http://codex.buddypress.org/developer/loops-reference/the-activity-stream-loop/

Thanks!

Boone B Gorges · @boone