Block an User

I am trying to use "block_users" plugin  by Arck Interactive [ Elgg 3.0.1, php 7x ]

Installation : smooth. "Block this user" option seen on hover card. Blocked user is seen is Blocked user list in the settings. However, blocked user can still see the content/comments I made and still can send messages to me. Can anyone suggest what codes need to be added or modified so that

  • Block user option is also seen in Profile page
  • Block user can be unblocked from settings page (or from hover card/profile menu)
  • Blocked user has no access to my blog posts, statuses, comments, photos etc or at least blog posts and photos

block.php

<?php

namespace MFP\BlockUsers;

/**
 * Block a user
 */

$blocked_user = get_entity(get_input('blocked_user_guid'));
$blocking_user = get_entity(get_input('blocking_user_guid', elgg_get_logged_in_user_guid()));

if ($blocked_user == $blocking_user) {
    register_error(elgg_echo('block_users:block:cannot_block'));
    forward(REFERRER);
}

if (block_user($blocked_user, $blocking_user)) {
    // unfriend
    remove_entity_relationship($blocked_user->getGUID(), 'friend', $blocking_user->getGUID());
    remove_entity_relationship($blocking_user->getGUID(), 'friend', $blocked_user->getGUID());
    system_message(elgg_echo('block_users:block:blocked_user'));
} else {
    register_error(elgg_echo('block_users:block:cannot_block'));
}

forward(REFERRER);

 

unblock.php

namespace MFP\BlockUsers;

/**
 * Unblock a user
 */

$blocked_user = get_entity(get_input('blocked_user_guid'));
$blocking_user = get_entity(get_input('blocking_user_guid', elgg_get_logged_in_user_guid()));

if (!is_blocked($blocked_user, $blocking_user)) {
    register_error(elgg_echo('block_users:unblock:cannot_unblock'));
    forward(REFERRER);
}

if (unblock_user($blocked_user, $blocking_user)) {
    system_message(elgg_echo('block_users:unblock:unblocked_user'));
} else {
    register_error(elgg_echo('block_users:unblock:cannot_unblock'));
}

$next = get_input('next', REFERRER);
forward($next);

functions.php

<?php

namespace MFP\BlockUsers;

/**
 * Unblocks $blocked_user as blocked by $blocking_user.
 *
 * @param ElggUser $blocked_user
 * @param ElggUser $blocking_user
 * @return bool
 */
function block_user(\ElggUser $blocked_user, \ElggUser $blocking_user) {
    if (!$blocked_user instanceof \ElggUser) {
        return false;
    }

    if ($blocking_user && ! ($blocking_user instanceof \ElggUser)) {
        return false;
    } elseif (!$blocking_user) {
        $blocking_user = elgg_get_logged_in_user_entity();
    }

    // can't block admins
    if ($blocked_user->isAdmin()) {
        return false;
    }

    return add_entity_relationship($blocking_user->getGUID(), 'blocked', $blocked_user->getGUID());
}

/**
 * Unblocks $blocked_user as blocked by $blocking_user.
 *
 * @param ElggUser $blocked_user
 * @param ElggUser $blocking_user
 * @return type bool
 */
function unblock_user(\ElggUser $blocked_user, \ElggUser $blocking_user) {
    return remove_entity_relationship($blocking_user->getGUID(), 'blocked', $blocked_user->getGUID());
}

/**
 * Is $blocked_user blocked by $blocking_user?
 *
 * @param ElggUser $blocked_user
 * @param ElggUser $blocking_user
 * @return type bool
 */
function is_blocked(\ElggUser $blocked_user, \ElggUser $blocking_user) {
    if (!($blocked_user instanceof \ElggUser) || !($blocking_user instanceof \ElggUser)) {
        return false;
    }
    return (bool) check_entity_relationship($blocking_user->getGUID(), 'blocked', $blocked_user->getGUID());
}
  • block user should be checked both ways. right? but I guess in the is_blocked function, the check_entity_relationship function only checks it from one side.

    Try this code:

    function is_blocked(\ElggUser $blocked_user, \ElggUser $blocking_user) {
        if (!($blocked_user instanceof \ElggUser) || !($blocking_user instanceof \ElggUser)) {
            return false;
        }
        $chk1 = (bool)check_entity_relationship($blocking_user->getGUID(), 'blocked', $blocked_user->getGUID());
        $chk2 = (bool)check_entity_relationship($blocked_user->getGUID(), 'blocked', $blocking_user->getGUID());
        return ($chk1 || $chk2)
    }
  • @Rohit Gupta Thanks for the prompt reply and help with the codes. I will try and run these codes and post the results. I see you have updated many 2x plugins to work with 3x. Thats a great job, it will help many of the users here. If you have time to spare please consider updating the "block_users" plugin also. I guess there are 5 types of 1:1 user to user action, which has sort of become norm.

    • User can friend and/or follow another user on mutual request and consent
    • User can send private message ( and some fancy things, as "nudge" "wink" etc)
    • User can mention or tag another user
    • User can block another user, who can then no more pm/comment/read content etc
    • User can delete comments of another user in one's own blog (bad/spam comments etc)

    In Elgg 2x the "block_users" plugin worked at least partially.

  • If you have time to spare please consider updating the "block_users" plugin also.

    The plugin that I have updated are those which I am using on my website. I can't promise anything but if I get time I will definitely look into the block_users plugin.