Añadir like post en Buddypress activity stream para el plugin WTI Like Post

En este tutorial veremos como añadir una entrada en el activity stream de Buddypress cada vez que un usuario le da al like usando el plugin WTI Like Post Pro V. 2.0. Por lo visto en la versión Pro 2.1 tienen activada esta función. No lo he probado. En la versión 2.0 (que es la que tengo activada) no existía, y como tuve que tunear bastante el plugin he preferido añadirla yo. Supongo esto sirva también para la versión no pro. En functions.php de nuestro tema activo añadimos lo siguiente

function inauditas_likePost_activity() {
 global $bp;
 global $post;
 if ( !function_exists( 'bp_activity_add' ) )
 return false;

 if(is_user_logged_in()){
    $user_id =bp_loggedin_user_id(); //get the current logged in user id
    $user1_link =  bp_core_get_userlink( $user_id); //get the current logged in user url
    } else { $user1_link='Anonimous user';} // if no user is logged in we will print Anonimous
 $post_id = (int)$_REQUEST['post_id'];

 $post_data = get_post($post_id); //get the id of the post that recives the like
 $wti_like_count = GetWtiLikeCount($post_id); /get the total like count for this post, incase we need to print it
 $post_author = $post_data->post_author; //get the post author
 $post_permalibk = get_permalink($post_data->ID); //get the post permalink

// get all author data 
$user_firstName=get_user_meta($post_author,'first_name',true);
$user_lastName=get_user_meta($post_author,'last_name',true);
$user_nicename=bp_core_get_username($post_author);

    
 $author_link = bp_core_get_userlink( $post_author );

 bp_activity_add( array(
 'user_id' => $user_id,
 'action' =>  sprintf( __( '%1$s liked %2$s work', 'buddypress' ), $user1_link, $author_link), 
 'component' => 'activity',
 'type' => 'like_post',
 'primary_link' => $post_permalibk,
 'item_id' => $post_id,
 'secondary_item_id' => $post_author //record the post autho un the secondary_item_id field of the database
 ) );

}

add_action( 'wp_ajax_wti_like_post_process_vote', 'inauditas_likePost_activity',1 );
add_action( 'wp_ajax_nopriv_wti_like_post_process_vote', 'inauditas_likePost_activity',1 );
add_action('wp_ajax_wti_like_post_update_meta', 'inauditas_likePost_activity',1);


en activity-loop.php de Buddypress tenemos que incluir las entradas que acabamos de crear para el activity stream. Añadiremos las siguientes líneas

<!--?php 
global $wpdb;
global $bp; 
$displayed_user=$bp--->displayed_user->id;
$activity_ids= array();?>
<!--?php $activity_ids = $wpdb--->get_results( "SELECT id FROM {$bp->activity->table_name} WHERE secondary_item_id LIKE $displayed_user AND type LIKE 'like_post'" );
//Add activity entry to the loop . This only shows the entry in the activity stream for the author who recieved the like, not for the user who 'liked' the post. 
//For that the query should be
//$activity_ids = $wpdb->get_results( "SELECT id FROM {$bp->activity->table_name} WHERE (secondary_item_id LIKE $displayed_user OR  user_id LIKE $displayed_user) AND type LIKE 'like_post'" );
foreach ( $activity_ids as $activity_id ) {
    $query_aids[] = $activity_id->id;
}?>
<!--?php 
$activity_user_ids = implode( ',', array_unique( (array) $query_aids ) );
$args=array(
        'include' =--> $activity_user_ids,  
    );
?>

<!--?php if ( bp_has_activities( $args) ) : ?-->

Deja un comentario