How to Extend/Override User Ignore Methods?

Alteran Ancient

Well-known member
Heya!

So basically, I wanted to modify my Ignore facility so that users are unable to ignore people considered as extended members of staff, because one or two of my members see fit to abuse the facility to ignore those that are "in power", purely for their rank within the community.

So, I set about making this impossible. What I wanted to do was modify the XenForo_Model_UserIgnore::canIgnoreUser method, but haven't found a way to override this feature with the add-on way to get it to do what I want it to do. I was able to directly edit the method to add my checks, but I'd rather do it via an add-on if I can.

Long story short, this is the re-written method:
PHP:
    public function canIgnoreUser($userId, array $user, &$error = '')
    {
        $setError = (func_num_args() >= 3);

        if (!$userId)
        {
            return false;
        }

        if ($user['is_moderator'] || $user['is_admin'])
        {
            if ($setError)
            {
                $error = new XenForo_Phrase('staff_members_may_not_be_ignored');
            }
            return false;
        }
       
        //Check against all staff...
        $groups = array($user['user_group_id']);
        $sGroups = explode(",", $user['secondary_group_ids']);
        $groups = array_merge($groups, $sGroups);
       
        if(in_array("6", $groups) || in_array("11", $groups) || in_array("13", $groups) || in_array("15", $groups)){
            if ($setError)
            {
                $error = new XenForo_Phrase('staff_members_may_not_be_ignored');
            }
            return false;
        }
        //END Check against all staff...

        if ($userId == $user['user_id'])
        {
            if ($setError)
            {
                $error = new XenForo_Phrase('you_may_not_ignore_yourself');
            }
            return false;
        }

        return true;
    }

How might I extend or override the default method so I can avoid directly tinkering with the source in future?
 
You can use the load_class_model event to extend this function.

As you can see there, the function only ever returns true or false in every scenario. When extending the function you need to ascertain what the parent is returning, do some stuff and either modify that response based on some custom code and then return that modified response.

PHP:
    public function canIgnoreUser($userId, array $user, &$error = '')
    {
        $parent = parent::canIgnoreUser($userId, $user, $error);

        // Do something here.

        return $parent;
    }

All you need to do is add your code.
 
  • Like
Reactions: Bob
To extend on what @Chris Deeming has said, you can do a simple conditional check to see if you need to perform any work:
PHP:
public function canIgnoreUser($userId, array $user, &$error = '')
    {
        $parent = parent::canIgnoreUser($userId, $user, $error);
        $setError = (func_num_args() >= 3);

        // If parent is already false, we know they cannot ignore, performing more checks is a waste of power
        if ($parent) {
          // Do all your checks here, so...
          $groups = array($user['user_group_id']);
          $sGroups = explode(",", $user['secondary_group_ids']);
          $groups = array_merge($groups, $sGroups);
         
          if(in_array("6", $groups) || in_array("11", $groups) || in_array("13", $groups) || in_array("15", $groups)){
              if ($setError)
              {
                  $error = new XenForo_Phrase('staff_members_may_not_be_ignored');
              }
              return false;
          }
        }

        return $parent;
    }
 
I couldn't get the parent:: callback to work properly, but I was able to override the function as a whole by using the load_class_model event.

Thanks for the help!
 
Feel free to let us know how it wasn't working or what errors were happening. Getting it working as a proper add-on is preferable just in case any further functionality is added in the future or you have an add-on that extends this in future. Overriding the function prevents that.
 
Top Bottom