bp_activity_remove_user_favorite( int $activity_id, int $user_id )

Remove an activity feed item as a favorite for a user.

Description

Parameters

$activity_id

(Required) ID of the activity item being unfavorited.

$user_id

(Required) ID of the user unfavoriting the activity item.

Return

(bool) True on success, false on failure.

Source

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

function bp_activity_remove_user_favorite( $activity_id, $user_id = 0 ) {

	// Fallback to logged in user if no user_id is passed.
	if ( empty( $user_id ) ) {
		$user_id = bp_loggedin_user_id();
	}

	$my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );
	$my_favs = array_flip( (array) $my_favs );

	// Bail if the user has not previously favorited the item.
	if ( ! isset( $my_favs[ $activity_id ] ) ) {
		return false;
	}

	// Remove the fav from the user's favs.
	unset( $my_favs[$activity_id] );
	$my_favs = array_unique( array_flip( $my_favs ) );

	// Update the total number of users who have favorited this activity.
	$fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' );

	// Update the users who have favorited this activity.
	$users = bp_activity_get_meta( $activity_id, 'bp_favorite_users', true );
	if ( empty( $users ) || ! is_array( $users ) ) {
		$users = array();
	}

	if ( in_array( $user_id, $users ) ) {
		$pos = array_search( $user_id, $users );
		unset( $users[ $pos ] );
	}

	// Update activity meta
	bp_activity_update_meta( $activity_id, 'bp_favorite_users', array_unique( $users ) );

	if ( ! empty( $fav_count ) ) {

		// Deduct from total favorites.
		if ( bp_activity_update_meta( $activity_id, 'favorite_count', (int) $fav_count - 1 ) ) {

			// Update users favorites.
			if ( bp_update_user_meta( $user_id, 'bp_favorite_activities', $my_favs ) ) {

				/**
				 * Fires if bp_update_user_meta() is successful and before returning a true value for success.
				 *
				 * @since BuddyPress 1.2.1
				 *
				 * @param int $activity_id ID of the activity item being unfavorited.
				 * @param int $user_id     ID of the user doing the unfavoriting.
				 */
				do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id );

				// Success.
				return true;

			// Error updating.
			} else {
				return false;
			}

		// Error updating favorite count.
		} else {
			return false;
		}

	// Error getting favorite count.
	} else {
		return false;
	}
}

Changelog

Changelog
Version Description
BuddyPress 1.2.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.