BuddyDev

Search

by Brajesh Singh

Permalink: #662

Show the faces of users who liked a BuddyPress Activity

if( !function_exists('bp_activity_add_meta')):
function bp_activity_add_meta( $activity_id, $meta_key, $meta_value ) {
    global $wpdb, $bp;

    // Make sure activity_id is valid
    if ( !is_numeric( $activity_id ) )
        return false;

    // Sanitize key
    $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );

    // Sanitize value
    if ( is_string( $meta_value ) )
        $meta_value = stripslashes( esc_sql( $meta_value ) );

    // Maybe, just maybe... serialize
    $meta_value = maybe_serialize( $meta_value );


    $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->activity->table_name_meta} ( activity_id, meta_key, meta_value ) VALUES ( %d, %s, %s )", $activity_id, $meta_key, $meta_value ));


    //should we consider invalidating the cache
    wp_cache_delete( 'bp_activity_meta_' . $activity_id . '_' . $meta_key,  'bp' );

    // Victory is ours!
    return true;
}
endif;
add_action( 'bp_activity_add_user_favorite', 'bpdev_track_user_favorite', 10, 2);

function bpdev_track_user_favorite( $activity_id, $user_id ){

    bp_activity_add_meta( $activity_id, 'favorited_by_user', $user_id );
}
add_action( 'bp_activity_remove_user_favorite', 'bpdev_track_user_unfavorite', 10, 2);

function bpdev_track_user_unfavorite( $activity_id, $user_id ){

    bp_activity_delete_meta( $activity_id, 'favorited_by_user', $user_id );
}

//show faces, yay!

add_action( 'bp_activity_entry_content', 'bpdev_show_who_favorited_activities' );
function bpdev_show_who_favorited_activities(){
    $output = '';
    $favorited_users = bp_activity_get_meta( bp_get_activity_id(), 'favorited_by_user' );
    //print_r($favorited_users);
    if( !empty( $favorited_users ) ){

        foreach( (array) $favorited_users as $user_id )
            $output .= "<a href='" . bp_core_get_user_domain ( $user_id ) . "'>". bp_core_fetch_avatar ( array( 'type'=&gt; 'thumb', 'height'=&gt; 25, 'width'=&gt;25, 'item_id'=&gt; $user_id ) ) . "</a>";

    }
    if( $output )
        echo "<div class='clearfix activity-favorited-by'>{$output}</div>";
}

Put the above code in bp-custom.php

#activity #bp-hacks

by Brajesh Singh

Permalink: #637

Get the total number of favorites accumulated by a user on his activities

/**
 * Get the total no. of favorites accumulated by a user on his activities 
 * @global type $bp
 * @global type $wpdb
 * @param int $user_id
 * @return int count of total favories by other users on given users activity 
 */
function bpdev_count_user_acivity_favs( $user_id ){

    global $bp, $wpdb;
    $query =$wpdb->prepare("SELECT SUM(meta_value) as total FROM {$bp->activity->table_name_meta} WHERE meta_key = %s AND activity_id IN ( SELECT id FROM {$bp->activity->table_name} WHERE user_id = %d)", 'favorite_count', $user_id );


    return (int) $wpdb->get_var( $query );

}

#activity, #bp-hacks

by Brajesh Singh

Permalink: #170

Remove Activity Link(Permalink) from Activity time since on Activity Lists


/** * Remove permalink link from time since on activity list */ add_filter( 'bp_activity_permalink', 'bpdev_custom_rtemove_perlink_from_timesince',10,2 ); function bpdev_custom_rtemove_perlink_from_timesince( $time_since, $activity ){ $action = $activity-&gt;action ; $time_since = '<span class="time-since">' . bp_core_time_since( $activity-&gt;date_recorded ) . '</span>'; $content = sprintf( '%1$s %2$s', $action, $time_since ); return $content; }

You can put the above code in your theme's functions.php or bp-custom.php in plugins directory

#buddypress #activity #bp-hack