[wp-hackers] A Basic Activity Feed - Is This Efficient?

Abdussamad Abdurrazzaq abdussamad at abdussamad.com
Tue Oct 29 11:53:49 UTC 2013


Use $wpdb->prefix in activity_tracker as well

I hope you've sanitized authorid in that select query. You should stick 
author id in quotes.

A function to return message based on activity would look like this:

function activity_message( $activity_id ) {
	//map activity ids to messages here.
	$activity_messages = array( 	'update_something' => __( 'Updated 
something' ),	
							'deleted_something' => __( 'Deleted something' )
					);
	if( array_key_exists( $activity_id, $activity_messages ) ){
		return $activity_messages[ $activity_id ];
	} else {
		return __( 'Unknown activity' );
	}
}


On 10/29/2013 04:32 PM, BenderisGreat wrote:
> Creating a basic activity feed for users. First wrote a simple function that
> takes args as user_id, activity_type, and source_page.  Here is the
> function:
>
> /function activity_tracker($user_id, $activity_type, $source_id ) {
> global $wpdb, $current_user;
>        get_currentuserinfo();
>
> 	$time = current_time('mysql');
>
> 	$wpdb->insert( 'wp_jo_activity_feed', array(
> 	'user_id' => $current_user->ID,
> 	'activity_type' => $activity_type,
> 	'source_id' => $source_id,
> 	'time' => $time
> 	));
> 	/
>
> Simple enough, then I drop this function call all over the place:
>
> /	$runme = activity_tracker($_current_member->ID, 'update_info', 'prof');/
>
> So everytime someone submits a form, or deletes an entry, or changes profile
> information, friends another user, likes a post, etc... they each have a
> specific activity name (that I enter manually) and is logged this way.
>
> Then, I query the DB to show the most recent 10-50 activities, on the
> activity feed page, like so:
>
> /        <?php //activity call
> 	$activity_calls = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix .
> 'jo_activity_feed' .' WHERE user_id = '. $author_id .' ORDER BY id ASC');
> 		$i = 0;
> 	$limit = 15;
> 	$count = count($activity_calls);
> 	while ($i < $limit && $i < $count)
> 	{
> 	$row = $activity_calls[$i];
> 	$get_row_user_id = $row->user_id;
> 	$convert_name = get_userdata( $get_row_user_id );
> 	$time = $row->time;
> 	
> 	if ($row->activity_type == 'new_record') {
> 		
> 		$message = ' created a new log entry at ';
> 		$points = '+10';} elseif
> 		
> 		( $row->activity_type == 'deleted_record') {
> 		
> 		$message = ' deleted an existing log entry at ';
> 		$points = 0; } else {
> 		
> 		$message = ' updated his profile information at ';
> 		$points = 5; }
> 	
> 	echo '<li class="list-group-item">'.$points.''.$convert_name->user_login.
> $message .$time. '</li>';
> 		++$i; }
> 		?>
>          </ul>
> };/
>
> You get the idea, it's obviously very simple - but it's also VERY DB
> intensive (is that the correct terminology?).  I am making a ton of DB
> calls, which isnt a problem yet, but I would like to know if anyone has a
> more efficient method to achieve the same result.
>
> Additionally, for the message being returned in the activity feed, I am
> using if, elseif, and else statements, which limits me.  I would prefer to
> have a more streamlined way to return the message.  If I were to write a
> function that selected the response based on the activity_type entered- what
> would something like that look like?
>
> Before it's recommended, I am aware buddypress is an option for activity
> feeds.  I want to do this so I can learn more, and create something unique
> for myself.
>
>
>
> --
> View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/A-Basic-Activity-Feed-Is-This-Efficient-tp42662.html
> Sent from the Wordpress Hackers mailing list archive at Nabble.com.
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list