function friends_add_friend($initiator_userid, $friend_userid, $force_accept = false) { global $bp; $friendship = new BP_Friends_Friendship(); if ((int) $friendship->is_confirmed) { return true; } $friendship->initiator_user_id = $initiator_userid; $friendship->friend_user_id = $friend_userid; $friendship->is_confirmed = 0; $friendship->is_limited = 0; $friendship->date_created = bp_core_current_time(); if ($force_accept) { $friendship->is_confirmed = 1; } if ($friendship->save()) { if (!$force_accept) { // Add the on screen notification bp_core_add_notification($friendship->initiator_user_id, $friendship->friend_user_id, $bp->friends->id, 'friendship_request'); // Send the email notification friends_notification_new_request($friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id); do_action('friends_friendship_requested', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id); } else { do_action('friends_friendship_accepted', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id); } return true; } return false; }
/** * Create a new friendship. * * @since 1.0.0 * * @param int $initiator_userid ID of the "initiator" user (the user who is * sending the friendship request). * @param int $friend_userid ID of the "friend" user (the user whose friendship * is being requested). * @param bool $force_accept Optional. Whether to force acceptance. When false, * running friends_add_friend() will result in a friendship request. * When true, running friends_add_friend() will result in an accepted * friendship, with no notifications being sent. Default: false. * @return bool True on success, false on failure. */ function friends_add_friend($initiator_userid, $friend_userid, $force_accept = false) { // You cannot be friends with yourself! if ($initiator_userid == $friend_userid) { return false; } // Check if already friends, and bail if so. if (friends_check_friendship($initiator_userid, $friend_userid)) { return true; } // Setup the friendship data. $friendship = new BP_Friends_Friendship(); $friendship->initiator_user_id = $initiator_userid; $friendship->friend_user_id = $friend_userid; $friendship->is_confirmed = 0; $friendship->is_limited = 0; $friendship->date_created = bp_core_current_time(); if (!empty($force_accept)) { $friendship->is_confirmed = 1; } // Bail if friendship could not be saved (how sad!). if (!$friendship->save()) { return false; } // Send notifications. if (empty($force_accept)) { $action = 'requested'; // Update friend totals. } else { $action = 'accepted'; friends_update_friend_totals($friendship->initiator_user_id, $friendship->friend_user_id, 'add'); } /** * Fires at the end of initiating a new friendship connection. * * This is a variable hook, depending on context. * The two potential hooks are: friends_friendship_requested, friends_friendship_accepted. * * @since 1.0.0 * * @param int $id ID of the pending friendship connection. * @param int $initiator_user_id ID of the friendship initiator. * @param int $friend_user_id ID of the friend user. * @param object $friendship BuddyPress Friendship Object. */ do_action('friends_friendship_' . $action, $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id, $friendship); return true; }
/** * @group friends_get_friendship_request_user_ids * @group friends_add_friend */ public function test_requests_on_request() { $u1 = $this->factory->user->create(); $u2 = $this->factory->user->create(); $u3 = $this->factory->user->create(); // request friendship friends_add_friend($u2, $u1); // Set the time of the earlier friendship for reliable ordering of the results. $fid = friends_get_friendship_id($u2, $u1); $friendship = new BP_Friends_Friendship($fid, false, false); $friendship->date_created = date('Y-m-d H:i:s', time() - 60); $friendship->save(); // get request count for user 1 and assert $requests = friends_get_friendship_request_user_ids($u1); $this->assertEquals(array($u2), $requests); // request another friendship friends_add_friend($u3, $u1); // refetch request count for user 1 and assert $requests = friends_get_friendship_request_user_ids($u1); $this->assertEquals(array($u3, $u2), $requests); }
/** * Create a new friendship. * * @param int $initiator_userid ID of the "initiator" user (the user who is * sending the friendship request). * @param int $friend_userid ID of the "friend" user (the user whose friendship * is being requested). * @param bool $force_accept Optional. Whether to force acceptance. When false, * running friends_add_friend() will result in a friendship request. * When true, running friends_add_friend() will result in an accepted * friendship, with no notifications being sent. Default: false. * @return bool True on success, false on failure. */ function friends_add_friend($initiator_userid, $friend_userid, $force_accept = false) { // You cannot be friends with yourself! if ($initiator_userid == $friend_userid) { return false; } // Check if already friends, and bail if so if (friends_check_friendship($initiator_userid, $friend_userid)) { return true; } // Setup the friendship data $friendship = new BP_Friends_Friendship(); $friendship->initiator_user_id = $initiator_userid; $friendship->friend_user_id = $friend_userid; $friendship->is_confirmed = 0; $friendship->is_limited = 0; $friendship->date_created = bp_core_current_time(); if (!empty($force_accept)) { $friendship->is_confirmed = 1; } // Bail if friendship could not be saved (how sad!) if (!$friendship->save()) { return false; } // Send notifications if (empty($force_accept)) { $action = 'friends_friendship_requested'; // Update friend totals } else { $action = 'friends_friendship_accepted'; friends_update_friend_totals($friendship->initiator_user_id, $friendship->friend_user_id, 'add'); } // Call the above titled action and pass friendship data into it do_action($action, $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id, $friendship); return true; }
/** * @group BP6247 */ public function test_save_method_should_update_existing_row() { $u1 = $this->factory->user->create(); $u2 = $this->factory->user->create(); $friendship = new BP_Friends_Friendship(); $friendship->initiator_user_id = $u1; $friendship->friend_user_id = $u2; $friendship->is_confirmed = 0; $friendship->is_limited = 0; $friendship->date_created = bp_core_current_time(); $friendship->is_confirmed = 1; $friendship->save(); $fid = $friendship->id; $f = new BP_Friends_Friendship($fid); $f->is_confirmed = 1; $f->save(); $f2 = new BP_Friends_Friendship($fid); $this->assertEquals(1, $f2->is_confirmed); }
function friends_add_friend($initiator_userid, $friend_userid) { global $bp; /* Check the nonce */ if (!check_admin_referer('friends_add_friend')) { return false; } $friendship = new BP_Friends_Friendship(); if ((int) $friendship->is_confirmed) { return true; } $friendship->initiator_user_id = $initiator_userid; $friendship->friend_user_id = $friend_userid; $friendship->is_confirmed = 0; $friendship->is_limited = 0; $friendship->date_created = time(); if ($friendship->save()) { // Add the on screen notification bp_core_add_notification($friendship->initiator_user_id, $friendship->friend_user_id, 'friends', 'friendship_request'); // Send the email notification require_once BP_PLUGIN_DIR . '/bp-friends/bp-friends-notifications.php'; friends_notification_new_request($friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id); do_action('friends_friendship_requested', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id); return true; } return false; }
/** * Create friendship object. * * @since 2.7.0 * * @param array $args Array of arguments. * @return int Friendship ID. */ public function create_object($args) { $friendship = new BP_Friends_Friendship(); foreach (array('initiator_user_id', 'friend_user_id') as $arg) { if (isset($args[$arg])) { $friendship->{$arg} = $args[$arg]; } else { $friendship->{$arg} = $this->factory->user->create(); } } foreach (array('is_confirmed', 'is_limited', 'date_created') as $arg) { if (isset($args[$arg])) { $friendship->{$arg} = $args[$arg]; } } $friendship->save(); return $friendship->id; }