bp_activity_get_favorite_users_string( $activity_id )

Get like count for activity

Description

Parameters

$activity_id

(Required)

Return

(int|string)

Source

File: bp-activity/bp-activity-functions.php

function bp_activity_get_favorite_users_string( $activity_id ) {

	if ( ! bp_is_activity_like_active() ) {
		return 0;
	}

	$like_count      = bp_activity_get_meta( $activity_id, 'favorite_count', true );
	$like_count      = ( isset( $like_count ) && ! empty( $like_count ) ) ? $like_count : 0;
	$favorited_users = bp_activity_get_meta( $activity_id, 'bp_favorite_users', true );

	if ( empty( $favorited_users ) || ! is_array( $favorited_users ) ) {
		return 0;
	}

	if ( $like_count > sizeof( $favorited_users ) ) {
		$like_count = sizeof( $favorited_users );
	}

	$current_user_fav = false;
	if ( bp_loggedin_user_id() && in_array( bp_loggedin_user_id(), $favorited_users ) ) {
		$current_user_fav = true;
		if ( sizeof( $favorited_users ) > 1 ) {
			$pos = array_search( bp_loggedin_user_id(), $favorited_users );
			unset( $favorited_users[ $pos ] );
		}
	}

	$return_str = '';
	if ( 1 == $like_count ) {
		if ( $current_user_fav ) {
			$return_str = __( 'You like this', 'buddyboss' );
		} else {
			$user_data         = get_userdata( array_pop( $favorited_users ) );
			$user_display_name = ! empty( $user_data ) ? $user_data->display_name : __( 'Unknown', 'buddyboss' );
			$return_str        = $user_display_name . ' ' . __( 'likes this', 'buddyboss' );
		}
	} else if ( 2 == $like_count ) {
		if ( $current_user_fav ) {
			$return_str .= __( 'You and', 'buddyboss' ) . ' ';

			$user_data         = get_userdata( array_pop( $favorited_users ) );
			$user_display_name = ! empty( $user_data ) ? $user_data->display_name : __( 'Unknown', 'buddyboss' );
			$return_str        .= $user_display_name . ' ' . __( 'like this', 'buddyboss' );
		} else {
			$user_data         = get_userdata( array_pop( $favorited_users ) );
			$user_display_name = ! empty( $user_data ) ? $user_data->display_name : __( 'Unknown', 'buddyboss' );
			$return_str        .= $user_display_name . ' ' . __( 'and', 'buddyboss' ) . ' ';

			$user_data         = get_userdata( array_pop( $favorited_users ) );
			$user_display_name = ! empty( $user_data ) ? $user_data->display_name : __( 'Unknown', 'buddyboss' );
			$return_str        .= $user_display_name . ' ' . __( 'like this', 'buddyboss' );
		}
	} else if ( 3 == $like_count ) {

		if ( $current_user_fav ) {
			$return_str .= __( 'You,', 'buddyboss' ) . ' ';

			$user_data         = get_userdata( array_pop( $favorited_users ) );
			$user_display_name = ! empty( $user_data ) ? $user_data->display_name : __( 'Unknown', 'buddyboss' );
			$return_str        .= $user_display_name . ' ' . __( 'and', 'buddyboss' ) . ' ';

			$return_str .= ' ' . __( '1 other like this', 'buddyboss' );
		} else {

			$user_data         = get_userdata( array_pop( $favorited_users ) );
			$user_display_name = ! empty( $user_data ) ? $user_data->display_name : __( 'Unknown', 'buddyboss' );
			$return_str        .= $user_display_name . ', ';

			$user_data         = get_userdata( array_pop( $favorited_users ) );
			$user_display_name = ! empty( $user_data ) ? $user_data->display_name : __( 'Unknown', 'buddyboss' );
			$return_str        .= $user_display_name . ' ' . __( 'and', 'buddyboss' ) . ' ';

			$return_str .= ' ' . __( '1 other like this', 'buddyboss' );
		}
	} else if ( 3 < $like_count ) {

		$like_count = ( isset( $like_count ) && ! empty( $like_count ) ) ? (int) $like_count - 2 : 0;

		if ( $current_user_fav ) {
			$return_str .= __( 'You,', 'buddyboss' ) . ' ';

			$user_data         = get_userdata( array_pop( $favorited_users ) );
			$user_display_name = ! empty( $user_data ) ? $user_data->display_name : __( 'Unknown', 'buddyboss' );
			$return_str        .= $user_display_name . ' ' . __( 'and', 'buddyboss' ) . ' ';
		} else {
			$user_data         = get_userdata( array_pop( $favorited_users ) );
			$user_display_name = ! empty( $user_data ) ? $user_data->display_name : __( 'Unknown', 'buddyboss' );
			$return_str        .= $user_display_name . ', ';

			$user_data         = get_userdata( array_pop( $favorited_users ) );
			$user_display_name = ! empty( $user_data ) ? $user_data->display_name : __( 'Unknown', 'buddyboss' );
			$return_str        .= $user_display_name . ' ' . __( 'and', 'buddyboss' ) . ' ';
		}

		if ( $like_count > 1 ) {
			$return_str .= $like_count . ' ' . __( 'others like this', 'buddyboss' );
		} else {
			$return_str .= $like_count . ' ' . __( 'other like this', 'buddyboss' );
		}
	} else {
		$return_str = $like_count;
	}

	return $return_str;
}

Changelog

Changelog
Version Description
BuddyBoss 1.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.