<?php
/**
 * Here's an example on how to completely override Favorites to behave as likes
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;


class BP_Custom_Like {

	public static function start() {
		// Bail if activity component is inactive
		if ( ! bp_is_active( 'activity' ) ) {
			return;
		}

		$bp = buddypress();

		if ( empty( $bp->custom_like ) ) {
			$bp->custom_like = new self;
		}

		return $bp->custom_like;
	}

	public function __construct() {
		$this->setup_hooks();
	}

	public function setup_hooks() {
		// First remove the setting screen
		add_filter( 'bp_activity_favorites_user_settings', '__return_false' );

		// Force favorites to be public
		add_filter( 'bp_activity_favorites_privacy', '__return_true' );

		// Define your custom strings
		add_filter( 'bp_activity_favorites_globals', array( $this, 'like_strings' ), 10, 1 );

		// Change the unfav link to go to user's likes instead of unfavoriting the activity
		add_filter( 'bp_get_activity_unfavorite_link', array( $this, 'user_likes' ), 10, 1 );

		// Neutralize unfav ajax action
		add_filter( 'bp_core_get_js_strings', array( $this, 'neutralize_unfav' ), 10, 1 );
	}

	public function like_strings( $strings = array() ) {
		return array(
			// Activity action args
			'description'   => _x( 'Liked an update', 'likes activity description', 'bp-custom-like' ),
			'label'         => _x( 'Likes', 'likes activity dropdown label', 'bp-custom-like' ),
			// Wether you want to show the favorites in Activity Directory / group single item activities, 
			// member single item activities, or the groups tab of member single item activities
			'contexts'      => array( 'activity', 'group', 'member', 'member_groups' ),
			// Strings
			'directory_tab' => _x( 'My Likes', 'likes directory tab', 'bp-custom-like' ),
			// The action button in activity stream to favorite an update
			'fav_button'    => _x( 'Like', 'like button caption', 'bp-custom-like' ),
			// The action button in activity stream to remove a favorited update
			'unfav_button'  => _x( 'Liked', 'liked button caption', 'bp-custom-like' ),
			// Nav name
			'subnav'        => _x( 'Likes', 'likes member subnav', 'bp-custom-like' ),
			// Slug
			'slug'          => 'likes'
		);
	}

	public function user_likes( $unfav_link = '' ) {
		return bp_core_get_user_domain( bp_loggedin_user_id() ) . bp_get_activity_slug() . '/' . bp_get_activity_favorites_slug();
	}

	public function neutralize_unfav( $params = array() ) {
		$params['no_unfav'] = 1;
		return $params;
	}
}
add_action( 'bp_core_components_included', array( 'BP_Custom_Like', 'start' ), 10 );
